why this extremely simple effect doesn't work??

I don’t know whats going on anymore… I wrote this basic effect:

#if OPENGL
	#define SV_POSITION POSITION
	#define VS_SHADERMODEL vs_3_0
	#define PS_SHADERMODEL ps_3_0
#else
	#define VS_SHADERMODEL vs_4_0_level_9_1
	#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

Texture2D SpriteTexture;
matrix MatrixTransform;

sampler2D SpriteTextureSampler = sampler_state
{
	Texture = <SpriteTexture>;
};

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
	float2 TextureCoordinates : TEXCOORD0;
};

// vertex shader main
VertexShaderOutput MainVS(float4 position	: POSITION0,
	float4 color : COLOR0,
	float2 texCoord : TEXCOORD0)
{
	VertexShaderOutput output;
	output.Position = mul(position, MatrixTransform);
	output.Color = color;
	output.TextureCoordinates = texCoord;
	return output;
}

float4 MainPS(VertexShaderOutput input) : COLOR
{
	return tex2D(SpriteTextureSampler, input.TextureCoordinates) * input.Color;
}

technique SpriteDrawing
{
	pass P0
	{
		PixelShader = compile PS_SHADERMODEL MainPS();
		VertexShader = compile VS_SHADERMODEL MainVS();
	}
};

And I set the matrix like this:

var projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
            _hpGlobeEffect.Parameters["MatrixTransform"].SetValue(projection);

But I see nothing on the screen, no matter what I draw. I tried every possible value as the matrix, for example this:

Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
            _hpGlobeEffect.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);

I tried like million different examples I found and stuff which I think logically should work… and nada! except doing brute force using loops to generate every possible matrix in existence I think I tried everything. and nothing works…

Note that once I remove the vertex shader part and use only the pixel shader everything draws normally.

Anyone see the problem?

Thanks! :slight_smile:

I believe with MG you don’t need the half-pixel correction anymore, but that’s beside the point.

What geometry are you drawing? If you’re just providing a projection matrix, then it should be in view space. It’s possible that this is the case, but most common is drawing geometry that starts in object space, then transforming it by World, View, and Projection transforms.

thanks, should have mentioned I’m drawing with sprite batch using simple dest rect that suppose to represent pixels.

If I remove the vertex shader and leave everything else as-is it works as expected, I just want my vertex shader to do exactly what MonoGame spritebatch vertex shader do…

Looking at MG code here https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Effect/SpriteEffect.cs#L62 and here https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Effect/Resources/SpriteEffect.fx#L29 I don’t see why my effect wouldn’t work…

Thanks! :slight_smile:

Hmm yeah they look pretty much identical. Can you set a breakpoint and verify that the viewport’s width and height have the correct values at that point? Does your draw code work with the default SpriteEffect?

yes to all: viewport values are correct, default effect works, and furthermore - if I remove just the vertex shader part (but leave my pixel shader) it also works… strange isn’t it?

Yeah… I can’t think of anything else at the moment, sorry. Hopefully someone with more experience catches something.

1 Like

I guess the matrix is wrong. Does swapping the y values help?

Matrix.CreateOrthographicOffCenter(0, viewport.Width, 0, viewport.Height, 0, 1);

EDIT
Oh, you’re using the same code as MonoGame. Than it’s probably something else. How about renderstates? Are they also identical? Reversed culling could make things disappear.

Have you tried changing the level of your shader?
Or declaring a samplerstate instead of sampler2d? There is a subject somewhere here on the forum where we are wondering why some shaders work and others not

Thanks for the suggestions @Alkher and @markus, but I decided to give up and switch to Unity.

JK, I found a workaround to make the shader work without the vertex shader part (only with PS) and its even not bad. its a defeat, but at least it works.

Weird part is that I have other shaders in this project, including with vertex, and they all work just fine. That was one hell of a bug. :confused:

2 Likes

Did you ever solve this I just noticed the vertex shader input is :position0 did you try sv_position this is why I always define a vertex shader input struct as well.

Does that make a difference in DX? I have all of mine defined as POSITION0 and it works fine with the OpenGL profile.

@willmotil, @jnoyola sorry like I said I just scrapped the shader and wrote another one that only uses pixel shader without vertex. since it turned out to be the better option, I’m not trying to solve this for now.

btw this was the effect I was making (turned out to be really simple with just PS) :slight_smile:

2 Likes