Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a render target RenderTargetScene that holds my scene texture with the scene's depth buffer

            rtScene = new RenderTarget2D(
            graphicsDevice,
            graphicsDevice.PresentationParameters.BackBufferWidth,
            graphicsDevice.PresentationParameters.BackBufferHeight,
            false,
            SurfaceFormat.Rgba64,
            DepthFormat.Depth24Stencil8, // Requires a depth format for objects
              // to be drawn correctly (e.g. wireframe model surrounding model)
            0,
            RenderTargetUsage.PreserveContents
            );

If I attempt to copy the render target contents to another render target using the following code

        game.GraphicsDevice.SetRenderTargets(sceneCopy);

        game.GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 0f, 0);
        //game.GraphicsDevice.Clear(Color.Transparent);

        game.SpriteBatch.Begin(
            SpriteSortMode.Immediate,
            BlendState.Opaque,
            SamplerState.PointClamp,
            DepthStencilState.Default,
            RasterizerState.CullCounterClockwise
            );

        game.SpriteBatch.Draw(game.RenderTargetScene,
             new Rectangle(0, 0, sceneCopy.Width, sceneCopy.Height), Color.White);

        game.SpriteBatch.End();

Then the texture is copied across but the depth buffer appears to be lost.

This is apparent when using DepthStencilState.DepthRead as there is no depth for the objects drawn with that render state to read.

How can I make sure the depth buffer is copied across too?

EDIT:

I have a depth texture created via my deferred rendering system so I thought I would use that.

I'm now drawing a full screen quadrangle using a depth writing shader

    public void RestoreDepthBuffer(Texture2D depthTexture)
    {
        // Set the render states
        game.GraphicsDevice.BlendState = BlendState.Opaque;
        game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        game.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        game.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;

        Effect effect = restoreDepthBuffer;

        effect.CurrentTechnique = effect.Techniques["Default"];

        effect.Parameters["DepthTexture"].SetValue(depthTexture);

        effect.CurrentTechnique.Passes[0].Apply();

        game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(
            PrimitiveType.TriangleStrip,
            NearPlaneVerticesVPT,
            0,
            4,
            Indices,
            0,
            2
            );
    }

The important parts of the shader look like

struct PixelShaderOutput
{
   float4 Colour                        : COLOR0;
   float Depth                          : DEPTH;
};

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
    PixelShaderOutput output;

    output.Colour = float4(0, 0, 0, 0);
    output.Depth = 1.0f - tex2D(DepthSampler, input.TextureCoordinates).r;

    return output;
}

This is still not working though.

EDIT2: I've got it to work but I have to set the depth and draw all the objects when the RenderTarget is first set and not unset it again.

Should I post an answer based on my working method?

share|improve this question
I have found this link. I'll try that when I return from work. – user1423893 Jul 23 '12 at 16:24
This doesn't do what I want unfortunately without redrawing the entire scene. There must be a better way. – user1423893 Jul 23 '12 at 20:46
So, to summarize, you want to copy the content of one RenderTarget2D to another RenderTarget2D, without losing the associated Z-Buffer? Is that it? – Laurent Couvidou Jul 28 '12 at 0:20
Yes. That is correct. My current method is to use a depth texture. – user1423893 Jul 28 '12 at 8:59
1  
possible duplicate of Depth buffer and render target – ClassicThunder Oct 13 '12 at 17:49

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.