Rendering Depth Buffer and Shadow Map (debug)

I’ve just about got my engine converted from XNA to Monogame with a few headaches.

Shadows don’t work and animation is broken too (models just don’t appear for some reason i can’t fathom).

My engine uses deferred lighting which seems to be working nicely but for somer reason my debug shadowmap render target and depth buffers don’t contain anything sensible or there’s some new way of displaying them in Monogame.

Currently my depthmap and shadowmap targets (multiple due to cascaded shadow mapping) are defined as:

shadowHQRT = new RenderTarget2D(GraphicsDevice, 4096, 4096, false, SurfaceFormat.Single, DepthFormat.Depth24);

And I render my little debug boxes (so i can visualize the depthRT and ShadowRTs) with:

_debugSB.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone, debugshadowRTeffect);
            _debugSB.Draw(shadowHQRT, new Rectangle(1200, 300, 300, 150), Color.White);
            _debugSB.End();

The shadow used for SpriteBath is just:

uniform extern texture ScreenTexture;

sampler screen = sampler_state
{
Texture = ;
};

float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{
float4 color = tex2D(screen, inCoord);

color.rgb = (color.r+color.g+color.b)/3.0f;

//color = pow(color, 30.0f);

return color;

}

technique
{
pass P0
{
PixelShader = compile ps_4_0 PixelShaderFunction();
}
}

My depthRT just renders as a grey square.

Here’s a debug of the render targets:

Top is how it worked in XNA and bottom is MonoGame.

uhm, does it work if you simply draw the shadow map to the spritebatch (without Shader?)

1 Like

Hmm yeah it kinda looks like a depth map at least. It’s red but that fine. Strange that my shader doesn’t work anymore but since it’s only for debugging red will do.

Thanks :slight_smile:

uniform extern texture ScreenTexture;

sampler screen = sampler_state
{
Texture = ;
};

this is your problem. I don’t think this syntax was ever properly supported.

If you go with the default way of

Texture2D ScreenTexture;

sampler pointSampler = sampler_state
{
    Texture = (ScreenTexture);
}

that wiill probably work right away

1 Like

How is that supposed to work or how do you know what texture is used? I didn’t know this was legal effect syntax.

The name of the texture was probably surrounded by angle brackets, which means the forum interpreted it as an HTML tag and hid it. It probably actually looks like this in the code:

sampler screen = sampler_state
{
    Texture = <ScreenTexture>;
}

Oh, right :stuck_out_tongue: I had that happen before…

It doesn’t matter if you use angled brackets or parentheses, MG interprets them the same. So switching to parentheses won’t fix the issue unfortunately.

What probably will help though, is if you add parameters to your pixel shader so it’s input matches the output of the vertex shader used by SpriteBatch. The parameters are

float4 position : POSITION, float4 color : COLOR, float2 texCoord : TEXCOORD

They all need to be there, in that order to ensure parameters are mapped correctly.