Issue with multiplying floats in shader

Hi. I’m fairly new to Monogame and game development in general. I have just started looking into shaders. I have been looking around online for simple lighting shaders for a 2d game.

As I am just testing around with it there isn’t anything complex, to it. So far it just takes in a rendertarget2d, which would be my camera view, and a rendertarget2d for a mask. However, the shader just returns black pixels. I have an example which is a near exact version of what I am doing and that runs fine. So I am unsure as to what may be causing it.

This is my code in the draw method:

        //Draw to mask
        spriteBatch.Begin(blendState: BlendState.Additive);
        spriteBatch.Draw(LightMaskTexture, Vector2.Zero, Color.Orange);

        //Draw the 'camera view'
        spriteBatch.Begin(blendState: BlendState.AlphaBlend);
        spriteBatch.Draw(Background, Vector2.Zero, Color.White);

        //Draw to screen using shader 
        spriteBatch.Begin(blendState: BlendState.AlphaBlend, effect: plainEffect);
        spriteBatch.Draw(BackgroundRT, Vector2.Zero, Color.White);

Here is the shader

#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1

Texture2D BackgroundRT;
Texture2D LightMaskRT;

sampler2D BackgroundRTSampler = sampler_state
    Texture = <BackgroundRT>;
sampler2D LightMaskRTSampler = sampler_state
    Texture = <LightMaskRT>;

float4 LightPS(float2 TextureCoords : TEXCOORD0) : COLOR
	float4 BackgroundPixelColour = tex2D(BackgroundRTSampler, TextureCoords);
	float4 LightMaskPixelColour = tex2D(LightMaskRTSampler, TextureCoords);

	return BackgroundPixelColour * LightMaskPixelColour;

technique Lighting
        pass P0
   		PixelShader = compile PS_SHADERMODEL LightPS();

Any help on why it seems to be just displaying black would be great. Thanks!

We cannot tell, as we don’t know the content of Background & LightMask - plot them to screen (Just draw them like a texture) to see what their content is first. Looks like one of them is always black or they are the invert of each other etc

This is the light mask that is drawn onto the LightMask Rendertarget which is cleared to black.

This is the background texture drawn to the Background rendertarget.

This is them drawn to screen normally.

And this is them drawn with the code above.

Do they have alpha channels? How do you compile them in your mgcb?

Maybe try something like this

return float4(BackgroundPixelColour.rgb * LightMaskPixelColour.rgb, 1);

I would eliminate errors one by one, when I experience some behaviour I cannot explain:

try changing .AlphaBlend -> .Opaque … looks like it blends to black backbuffer - you can also try to clear with .TransparentBlack instead

try outputing float4(1,1,1,1) in shader, just to see, if the result is indeed white, thus the shader called correctly.


that way you can break the fault more down - I don’t see an actual issue with the shader itself, so it’s either not called, or some values are handled as exptected.

There are a lot of things it could be.

Looking at the shader, the pixel shader is wrong.

When you create a sprite effect from mcgb, the vertex shader ouput is this.

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

You cannot then define your pixel shader as …

 float4 LightPS(float2 TextureCoords : TEXCOORD0) : COLOR

You are ignoring the other elements in the vertex shader output

My guess is your texture coords will always be 1,1 as it is picking up the colour instead of the uv coordinates

Change it to

float4 LightPS(VertexShaderOutput input) : COLOR

The second thing that may or may not be a problem is the textures.

You are assuming that the order they are defined in the shader is the order they get assigned texture slots.
You may be lucky and that happens, but I would assign your slots to be sure.

Hope this helps