When I use this RenderTarget:

renderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width,
    GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);

and draw something to it:

// some drawing code here

and then when I want to continue with drawing depth buffer seams to be clean. Why? Is possible to save it for future use?

  • \$\begingroup\$ Perhaps you need to enable depth testing/writing in the render state? I'm not familiar enough with XNA to say exactly how to do this, but that's the first thing I would check on. \$\endgroup\$ – Nathan Reed Jan 12 '12 at 20:41
  • \$\begingroup\$ thanks for suggestion but GraphicsDevice.DepthStencilState = DepthStencilState.Default; don't affect result \$\endgroup\$ – Vodáček Jan 12 '12 at 20:59

From the deferred rendering articles/tutorials I read it's a limitation in XNA (likely because the XBox 360 probably discards the depth buffer when you change render targets, hence they apply the limitation to Windows as well). What you need to do is use MRT (Multiple Render Targets) and write/test the depth against another RenderTarget2D that is something along the lines of Single.

When you have finished 'compositing' your depth buffer write it out to the real depth buffer using a screen quad and associated pixel shader.

The first article in J.Coluna's (quite brilliant) series on LPP describes the problem briefly - and his code should provide a nice reference for working around the issue.

  • \$\begingroup\$ Thanks for answer :-) I have allready solved it. I am using Light Pre pass so I have depth saved in Rentertarget. I am not sure that I can post answer on my questions. Anyway I write article on my blog about it: vodacekworks.blogspot.com/2012/02/… \$\endgroup\$ – Vodáček Feb 14 '12 at 8:15
  • \$\begingroup\$ @Vodáček remember to avoid switching render targets frequently - performance will suffer (especially on the XBox with predicated tiling if your total buffer size exceeds 10MB). Get as much done with one render target pass as possible. \$\endgroup\$ – Jonathan Dickinson Feb 14 '12 at 8:42

It's automatically saved. RenderTarget2D is derived from Texture2D. So in your code sample, 'renderTarget' is a texture that have the render information saved to it.

  • \$\begingroup\$ yes "color" information is saved and what about depth buffer? \$\endgroup\$ – Vodáček Jan 15 '12 at 8:07
  • \$\begingroup\$ That depends on the texture format of the render target. Microsoft has some sample code that uses RenderTarget2D to implement shadow mapping by storing the depth in the texture, I would take a look at this sample: create.msdn.com/en-US/education/catalog/sample/shadow_mapping_1 \$\endgroup\$ – Nic Foster Jan 16 '12 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.