Question

Is it possible to access depth buffer via pixel shader 2.0 in DX 9.0c? I've google'd a bit and the only solution I've found describe GPU hack that works only on GeForce 6 & 7.

What I am trying to achieve is to write depth of field shader effect. I can't simple grab Z coords of vertices, because I am doing render to texture trick too, used for other post processing.

Edit:

I've tried this:

D3DXCreateTexture(lpD3Dev9, width, height, 1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24S8, D3DPOOL_DEFAULT, &lpD3DepthBuffer);
lpD3DepthBuffer->GetSurfaceLevel(0,&lpNewDepthBuffer);

lpD3Dev9->GetDepthStencilSurface(&lpPrevDepthBuffer);
lpD3Dev9->SetDepthStencilSurface(lpNewDepthBuffer);
lpD3Dev9->BeginScene();
// Rendering...
lpD3Dev9->EndScene();
lpD3Dev9->SetDepthStencilSurface(lpPrevDepthBuffer);

// this function fails:
D3DXSaveTextureToFile("C:\backBuffer.png", D3DXIFF_PNG, lpD3DepthBuffer, NULL);

lpD3Dev9->BeginScene(); 
UINT passes;
D3DXHANDLE tech;
lpD3DXfxScreen->FindNextValidTechnique(0, &tech);
lpD3DXfxScreen->SetTechnique(tech);
lpD3DXfxScreen->Begin(&passes,0);
lpD3DXfxScreen->SetTexture("texDepth", lpD3DepthBuffer);
// render shader...

SaveTextureToFile fails, and shader gets texture that is pure white RGB(1, 1, 1)

Was it helpful?

Solution

To my knowledge there is no standardised way of doing it as you are trying to do it. You are better off just creating a D3FMT_R32F (or whatever) texture as a D3DUSAGE_RENDERTARGET and then write depths to it as if its a normal texture (ie writing the depth in the r component from the pixel shader). You can then use that texture for whatever purpose you like purely by binding it to a sampler and reading the r value straight out of it to use in whatever pixel shader you are messing about with.

That said try going into the DirectX control panel and setting yourself to debug runtime. You'll see a tonne of useful error message spewed out in to the debug output stream by the DirectX runtime. There really is no reason not to be doing this as it would save you a lot of bother on problems like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top