Friday, November 16, 2018

c# - Depth buffer and render target


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...