[SOLVED] Pixel shader returns black

Hello! I’m using a pixel shader to draw lights in my game, ported from XNA to Xbox/Windows 10 as a UWP. The .fx file builds through the MonoGame Content Pipeline, but returns all black when used. I apologize, my shader knowledge is minimal.

Shader code:

sampler s0;  
texture lightMask;  
sampler lightSampler = sampler_state
{
    Texture = <lightMask>;
	
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;

    AddressU = Clamp;
    AddressV = Clamp;
}; 
  
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0  
{  
    float4 color = tex2D(s0, coords);  
    float4 lightColor = tex2D(lightSampler, coords);

    color *= 1 + lightColor * 3;

    return color;
}  

technique Technique1
{
    pass Pass1
    {
	PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

And this is how I use it in game:

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
 
Game1.lightingEffect.Parameters["lightMask"].SetValue(Game1.lightsTarget);
Game1.lightingEffect.CurrentTechnique.Passes[0].Apply();
 
spriteBatch.Draw(Game1.mainTarget, Vector2.Zero, Color.White);

spriteBatch.End();

Thanks for any help!

Hey! Welcome to the forums :slight_smile:

The inputs of your pixel shader should match the outputs of the vertex shader. In this case that’s the vertex shader used by SpriteBatch. The signature of your pixel shader should be the following:

float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0

I think that should fix your issue :slight_smile:

The way you apply the effect works, but you can also pass your Effect to the SpriteBatch.Begin call instead of explicitly applying it.

2 Likes

That did solve the problem!

Thank you! For the help and the tip!

1 Like