barebones shader not working.

I’m have a barebones shader. It’s supposed to return the sprite but all I get is a blue screen. If I comment out the vertex shader it works. But I can’t do that because I plan to add some code in the vertex shader.

#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
	#define PS_SHADERMODEL ps_4_0
#endif

sampler2D myTexture : register(s0);

matrix WorldViewProjection;

struct VertexShaderInput
{
	float4 Position : POSITION0;
	float4 Color : COLOR0;
	float2 TextureCoordinates : TEXCOORD0;
};

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

VertexShaderOutput MainVS(VertexShaderInput input)
{
	VertexShaderOutput output = (VertexShaderOutput)0;
	output.Position = mul(input.Position, WorldViewProjection);
	output.Color = input.Color;
	output.TextureCoordinates = input.TextureCoordinates;

	return output;
}

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

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

/// draw code.
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, effect);
            spriteBatch.Draw(texture: texture, destinationRectangle: new Rectangle(0, 0, 800, 600), sourceRectangle: new Rectangle(0, 0, texture.Width, texture.Height), color: Color.White, layerDepth: 0.5f,
                rotation:0, origin: Vector2.Zero, effects: SpriteEffects.None);
            spriteBatch.End();

            base.Draw(gameTime);

I think you just need to rename your matrix to MatrixTransform, so MonoGame can set it up. See the standard template for sprite effects: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Platform/Graphics/Effect/Resources/SpriteEffect.fx

I tried that and that didn’t work. Shaders seems like black magic to me…

OK, then I would suggest to try the MonoGame sprite effect code I linked directly. I don’t see why your code would behave differently, but there has to be some difference I guess. Just don’t forget to also get the Macros.fxh file that is included.

Try this setup:

It’s close to what you have so you can compare.

1 Like

I did something very similar to your example and it worked. Thanks. Below is my code. I think when using spritebatch, monogame doesn’t create the maxtrix for you. You have to create it yourself.

            Matrix.CreateOrthographicOffCenter(0, 800, 600, 0, 0, -1, out projection);
            effect.Parameters["ProjectMatrix"].SetValue(projection);