Ok, so I’m trying to get my head around HLSL in Monogame 3.4. Part of the problem is that many of the existing examples I find don’t work properly so I can’t figure out what I’m doing wrong and the error I’m getting isn’t helpful!
I’m trying to pass a texture and a palette texture to a pixel shader, at the moment I’m using SpriteBatch to do the drawing but eventually I’ll be applying it to polygons (hopefully). The texture is going in through the SpriteBatch draw method (I assume S0) whereas I need to pass the palleteImage through using Parameters, however when I try to do with the following I get a NullException on the SetValue as it doesn’t think there is a parameter. Any ideas? Or more to the point is there a working example that I can see as many of the existing pixel shader stuff I find doesn’t work on MonoGame 3.4!
HLSL
sampler TextureSampler : register(s0);
texture paletteImage;
sampler2D PaletteSampler = sampler_state {
Texture = <paletteImage>;
};
float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
float4 tex = tex2D(TextureSampler, texCoord);
float2 coords = float2(0.5f, 0.5f);
float4 palColour = tex2D(PaletteSampler, coords);
return tex * color;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_4_0 PixelShaderFunction();
}
}
Draw code
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
effect.Parameters["paletteImage"].SetValue(paletteImage);
// draw here
spriteBatch.Draw(texture, new Rectangle(10, 10, texture.Width * 3, texture.Height * 3), Color.White)
spriteBatch.End();
The exception occurs at SetValue, as far as I can tell the HLSL is fine, but something is amiss… I’ve noticed that if it doesn’t think a parameter is being used it throws an error (for example, I tried passing in a colour but it wasn’t until I used that colour in the HLSL code that it was accepted… is this the same thing?)