Sign in to follow this  
krum

DX9: Is there a way to render a depth buffer?

Recommended Posts

I would like to render the contents of a depth buffer to a render target as a grayscale image. Is this possible to do in DX9? Any pointers? Thanks!

Share this post


Link to post
Share on other sites
I had posted a similar thread in the DirectX forum. Some suggestions were made but I could not get anything to work (except for OpenGL where glReadPixels makes it trivial). Folks seem to confirm that it is not possible to get this information out of DirectX. I gave up on getting depth buffer information into a render target. Instead, I switched to generating the depth itself in a shader (duplicating what the rasterizer is going to produce anyway). The Cg programs are


void VS_ProjectedDepth
(
in float4 modelPosition : POSITION,
out float4 clipPosition : POSITION,
out float depth : TEXCOORD0,
uniform float4x4 modelViewProjectionMatrix)
{
clipPosition = mul(modelPosition,modelViewProjectionMatrix);
depth = clipPosition.z/clipPosition.w;
}

void PS_ProjectedDepth
(
in float depth : TEXCOORD0,
out float4 pixelColor : COLOR)
{
pixelColor.rgb = depth;
pixelColor.a = 1.0f;
}



You create your render target to have format D3DFMT_A8R8G8B8, set the render target, and render the scene in the standard way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this