Problem with depth render target creation and multi sample count (1041)


I’ve updated to the latest mono game a few days ago. Most things seem to work fine, but drawing to a second render target is no longer working for me.

This is how the render target is created and set:

if (depthRenderTarget == null) {    
    depthRenderTarget = new RenderTarget2D(game.GraphicsDevice, width, height, false, SurfaceFormat.Single, DepthFormat.None);

game.GraphicsDevice.SetRenderTargets(colorRenderTarget, depthRenderTarget);

This is the shader:

struct AtmosphereBackVSOut
    float4 Position : POSITION0;
    float Depth : TEXCOORD0;

AtmosphereBackVSOut AtmosphereBackVS(VertexShaderInput input)
    AtmosphereBackVSOut output; *= Size;
    float4 worldPos = mul(input.Position, World);
    float4 viewPos = mul(worldPos, View);
    output.Position = mul(viewPos, Projection);

    output.Depth = viewPos.z;

    return output;

float AtmosphereBackPS(AtmosphereBackVSOut input) : COLOR1
    float depth = -input.Depth / FarClipDistance;
    return float4(depth, 1, 1, 1);

Any idea on what might cause the problem? It worked fine with build 362.

Hi !

You only output to COLOR1 for AtmosphereBackPS…
In order to make MRT work, you have to output to both rendertargets at the same time from your pixelshader routine.
You have to do something like this for your MRT to work:

void AtmosphereBackPS(AtmosphereBackVSOut input, 
out float4 thecolortocolorRenderTarget:COLOR0, 
out float depth: DEPTH)
        //Do stuff concerning colors
        thecolorto_colorRenderTarget = thefinalcolor;
        depth = -input.Depth / FarClipDistance;

I usr this on 6 RT at the same time… on build 1041 too.

Thanks for your reply. That doesn’t seem to be the problem though (but helped me to identify an unneeded float4 in my pixel shader). It seems to be fine to write into any single render target from the pixel shader.

What solves the problem is when I change the creation of the render target:

depthRenderTarget = new RenderTarget2D(game.GraphicsDevice, width, height, false,
                          SurfaceFormat.Single, DepthFormat.None, pp.MultiSampleCount,

The crucial part is MultiSampleCount. When I don’t use the multi sample count of the current presentation parameters, it doesn’t work.

Any idea why this is the case? Is there a general restriction that all render targets used at the same time need to use the same multi sample count. Maybe we should add a check for this to the setrendertargets call.

Sure you can only write to one of them while both are binded, but what’s the purpose of mrt then ?

No idea why MultiSampleCount is needed for this…