XNA - Obtaining depth from the scene's render target?

Posted by user1423893 on Game Development See other posts from Game Development or by user1423893
Published on 2012-10-13T17:18:12Z Indexed on 2012/10/13 21:53 UTC
Read the original article Hit count: 239

Filed under:
|
|

I'm currently rendering my scene to a render target so it can be used for rendering methods such as post processing and order independent transparency.

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
            );

I am required to use RenderTargetUsage.PreserveContents so that the same render target can be rendered to multiple times, once for each of the draw methods below.

  • DrawBackground
  • DrawDeferred
  • DrawForward
  • DrawTransparent

The problem is that DrawTransparent requires a copy of the scene's depth as a texture.

Is there any way to obtain this from the scene render target above (rtScene)?

I can't have more than one render target with RenderTargetUsage.PreserveContents as this causes problems on hardware such as the XBOX 360, so rendering the depth to a separate render target at the same time as I render the scene isn't possible as far as I can tell.

Would I be able to get around this problem by "Ping-Ponging" two render targets (using the more compatible RenderTargetUsage.DiscardContents) and using the result for the depth texture?

© Game Development or respective owner

Related posts about XNA

Related posts about c#