Effects not working in DirectX

I’m getting started with creating effects, however I can’t get them to work when I target the DirectX platform but when I target the OpenGL platform, it works fine. Is there anything that I am doing wrong in my effect file? Here’s my current 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_level_9_1
	    #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

struct VertexShaderInput
{
	float4 Position : POSITION0;
	float4 Color : COLOR0;
};

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
};

VertexShaderOutput MainVS(in VertexShaderInput input)
{
	VertexShaderOutput output = (VertexShaderOutput)0;

output.Position = input.Position;
	output.Color = input.Color;

	return output;
}

float4 MainPS(VertexShaderOutput input) : COLOR0
{
return float4(1, 0, 0, 1);
}

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

How is it not working? Is not it compiling or are you just not seeing anything on screen?

I don’t see anything that jumps out in the shader file. I’m not familiar with exactly what limitations are added using level_9_1 instead of just vs_4_0 but I can’t imagine you’re doing anything not allowed by that as simple as this shader is.

Could you share you render call from C# where this shader gets applied? The only thing I could think of if you’re not seeing anything is that I don’t see any view or projection matrix applied, but you could be doing that in C# and I wouldn’t expect OpenGL to work without that either.

The effect compiles without errors, however it doesn’t show anything on the screen. My draw function is here:
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(planet, new Rectangle(0, 0, 200, 200), Color.White);
spriteBatch.End();
base.Draw(gameTime);
I also load my effect as
effect = Content.Load<Effect>("myeffect");
And everything else are the defualts.

That is not how you use shaders with SpriteBatch

You pass the shader in the SpriteBatch.Begin() method

Have a look at

https://www.shawnhargreaves.com/blog/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.html

1 Like

I’ve tried that, however visual studio says there is no begin function or end for an effect. I had tried the second neater option before, but it didn’t work either.

So you are saying this is incorrect?

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullCounterClockwise, Sprite, null);

Where Sprite is your effect?

Then you are not using Monogame.

Monogame has even better support for shaders than XNA did.

Simply use MCGB to create a Sprite Effect
You don’t need a vertex shader
Stick whatever you want in the pixel shader
And use the line of code above.

When you start typing a function call in VS it shows you the available methods, use this to explore your options.


I’ll let you be the judge…

1 Like

So have you removed the vertex shader from your effect?

There are lots of examples you can look to by searching the forum.

1 Like

Thanks, I figured that removing the vertex shader from the effect made the effect turn everything red (as intended), thanks @Stainless. But when I went to add a sampler, it didn’t work, so thanks to @willmotil, I’ve worked out that it must have been the parameters in the pixel shader and/or the creation of the sampler state which caused nothing to show. Thank you.