Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Nathan Reed Jan 12 '12 at 20:41
    
thanks for suggestion but GraphicsDevice.DepthStencilState = DepthStencilState.Default; don't affect result –  Vodáček Jan 12 '12 at 20:59
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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/… –  Vodáček Feb 14 '12 at 8:15
    
@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. –  Jonathan Dickinson Feb 14 '12 at 8:42
add comment

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.

share|improve this answer
    
yes "color" information is saved and what about depth buffer? –  Vodáček Jan 15 '12 at 8:07
    
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 –  Nic Foster Jan 16 '12 at 15:51
add comment

Your Answer

 
discard

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

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