   Best way to read Depth values in D3D9?
  Dear All,I'm coming from OpenGL, where its pretty simple to get the depth buffer of a scene using FBOs(this seems to be fast too, in my admitedly limited benchmarking tests).  My question is, whats the best way to get the depth values of a scene using d3d? D3DFMT_D24S8 seems to be what I'm looking for, is this efficant in your experience? If not whats the correct way to do this?   
  What do you need the depth buffer for?  I ask because you might not need to read it back at all if you really just need it for the GPU.  Otherwise, a function like IDirect3DDevice9::GetRenderTargetData might be what you're after.And last time I checked, pixel buffer objects were preferred over FBO's for read-back because they're more efficient in that regard and can be read asynchronously, however that was many months ago and it could have changed.   ;  You can only read from lockable depth buffers (D3DFMT_D16_LOCAKABLE or D3DFMT_D32_LOCKABLE), and that means no stencil data. The reason is that the driver is free to keep the depth buffer in whatever format it wants, not nessecarily a 32-bit float for each pixel. Some cards even compress the depth buffer.As Zipster said; what do you need the depth values for? There's almost certainly another way around this. Pixel shaders are another viable alternative for reading depth data.   ;  There's no good way to read the depth buffer back in DX9. The standard way to get "depth buffer" access is to render depth to a floating point render target in the pixel shader. See for example the ShadowMap sample in the DX SDK.Other ways include lockable depth formats, and special depth formats, but these are hardware specific.   ;  Ah I dont need it on the CPU, just the GPU.   ;Quote:Original post by laeuchliAh I dont need it on the CPU, just the GPU.Well in that case you're in luck, since that's not hard at all [smile]There are two ways to go about this (that I know of).  The easiest way is to create a depth-stencil texture (created with a depth format and D3DUSAGE_DEPTHSTENCIL), since you can bind the depth buffer directly as a texture.  The biggest problem with this approach is that it isn't supported on all hardware, so you need to check for it first using IDirect3D9::CheckDeviceFormat.If you're using depth-stencil surfaces directly (either the one created automatically by the device if you use EnableAutoDepthStencil with the presentation parameters, or one you created manually with CreateDepthStencilSurface), then you'll need to create a separate render-target texture and use MRT to render the depth to this target inside your shader.  Then you can bind it as a texture to another shader.  Instead of MRT, there might be a way to copy the depth-stencil surface to a surface obtained from a texture after the fact, but I don't know of any way to copy between two surfaces that are both in the default pool (well, maybe StrethRect).  In all honesty, this is the approach I'm least familiar with so someone else will have to pick up the slack :/   ;  Dear Zipster, Thanks a bunch of the info. The first method sounds like what I would rather do, but I was told that this method involved a slow and expensive copy, even on modern hardware. Is this incorrect? I guess the real answer would be the test it and see, but I'm lazy and would like to hear of your experince ;-). If anyone has tried the MRT method, be intrested to hear more that too. Thanks everyone for their time.Jesse;  In my experience, using MRT's to output your depth to a texture is the way to go.  You can use whatever format you'd like, and you don't have to worry about about it being supported on different types of hardware (ATI hardware doesn't support using depth formats, IIRC).  If you're looking for an example of this in action, check out the PostProcess sample in the DX SDK.  They use MRT's to render their scene, the depth, and the normals which are used in later post-processing passes.   