4
\$\begingroup\$

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:

graphicsDevice.SetRenderTarget(renderTarget);
// some drawing code here
graphicsDevice.SetRenderTarget(null);

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

\$\endgroup\$
2
  • \$\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\$ Commented Jan 12, 2012 at 20:41
  • \$\begingroup\$ thanks for suggestion but GraphicsDevice.DepthStencilState = DepthStencilState.Default; don't affect result \$\endgroup\$
    – Vodáček
    Commented Jan 12, 2012 at 20:59

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\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
    Commented Feb 14, 2012 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\$ Commented Feb 14, 2012 at 8:42
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ yes "color" information is saved and what about depth buffer? \$\endgroup\$
    – Vodáček
    Commented Jan 15, 2012 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
    Commented Jan 16, 2012 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .