Does the depth buffer change when changing rendering target

Started by
4 comments, last by zurekx 16 years, 1 month ago
Im doing some shadow mapping and I have a question. I set the render target to a texture. Then I render the scene. Then I render the scene as usual using the shadow map. My question is, is it the same depth buffer that I use in the two passes? (I dont do anything special except changing the render target). Because it doesnt seem to be the same z-buffer that is used in the two passes. It works, even though I dont clear the depthbuffer between the passes, so it seems like two different depth buffers are used. Is this the case? (I have multiple lights, so if it's the same buffer, it should def screw everything up) [Edited by - zurekx on March 7, 2008 10:24:51 AM]
Advertisement
Yup, it's the same depth buffer that you used previously. However if the second render-target isn't the same dimensions as the first (which is almost certainly the case for your shadow map), the pixels for each RT won't correspond to the same pixels on the depth buffer. This means z-culling won't work right, but of course it wouldn't work right anyway since you're probably switching camera locations.

[Edited by - MJP on March 7, 2008 6:03:57 PM]
But it's weird, because I dont clear the depth buffer between my passes and it still works. I render the scene to my texture. Then I render the scene to the screen. I change the render target, but not the depthbuffer, and it works as if I'd cleared the z-buffer, everything looks correct... but I dont think it should!
If you're changing anything between the two -- format, multisampling, etc -- or maybe even not, the driver could be forcing a new zbuffer. This type of behavior turns out to be a common source of image corruption in games, so depending on your exact behavior, you could be triggering a workaround.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I wouldn't count on this behaviour. My guess is that what you're rendering to the main scene is closer than what you render to the render target, which is why everything works out.

You might want to try running with the ref device, to make sure it's not a driver issue.
Well I moved the usual camera away from the scene (so the light is much closer), and it still works. But I guess it could be the hardware that does it as Promit said. Anyway, just to make it foolproof, I've tried to create a separate depthbuffer for the shadow map rendering. This is how I do it:

g_pd3dDevice->CreateDepthStencilSurface(SHADOW_MAP_SIZE,SHADOW_MAP_SIZE, D3DFMT_X8R8G8B8,D3DMULTISAMPLE_NONE,1, FALSE, &shadowDepthBuffer, NULL);

g_pd3dDevice->GetDepthStencilSurface(&oldDepthBuffer);
// Change rendering target
g_pd3dDevice->SetDepthStencilSurface(shadowDepthBuffer);
// render from the cameras point of view
// restore the rendering target and depthbuffer

But I dont get the same result as when I dont do anything at all. Am I having the wrong parameters to CreateDepthStencilSurface? Im not sure what to set the multisample-parameters to. I've tried to tweak them but it doesnt seem to work.

This topic is closed to new replies.

Advertisement