Jump to content
  • Advertisement
Sign in to follow this  
CoderFelix

Access Z buffer in D3D

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How to access the Z buffer for a pixel's depth value in D3D? Or is there any other way to compare the pixel depth value with the current corresponding Z buffer value?

Share this post


Link to post
Share on other sites
Advertisement
In in pixel shader you can access the pixel depth value of all pixels that are in the depth buffer:

void PS(VSOUT pIn) : SV_TARGET
{
//Depth value of the current pixel
float depth = pIn.posH.z;
}

Share this post


Link to post
Share on other sites
The above code snippet is actually retrieving the depth value of the current pixel being rendered, but not the depth value currently in the depth buffer. You cannot read and write to the depth buffer at the same time so that leaves you with only one option.

Do a pre z pass, then bind the depth buffer as a resource view to your pixel shader.

What are you trying to use this for? There might be other suggestions if you can provide more information. Which version of dx are you using?s

Share this post


Link to post
Share on other sites
Quote:
Original post by DieterVW
The above code snippet is actually retrieving the depth value of the current pixel being rendered, but not the depth value currently in the depth buffer. You cannot read and write to the depth buffer at the same time so that leaves you with only one option.

Do a pre z pass, then bind the depth buffer as a resource view to your pixel shader.

What are you trying to use this for? There might be other suggestions if you can provide more information. Which version of dx are you using?s



It's very kind of you guys, thanks!

Actually, I just wanna implement a "Cel-Shading" effect (not a simply one, I need the analytic line hidden information for stylistic strokes ). In order to make my Line Hidden algorithm work, I need a z - buffer testing for each segment in 3D space to see which part of it is visible.

I need the segment's visible information for further processing, not only simply render it on screen.

Any suggestion ? Thanks in advance.



Share this post


Link to post
Share on other sites
We need to know which version of the API you're using. D3D9? D3D10? SlimDX? XNA?

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
We need to know which version of the API you're using. D3D9? D3D10? SlimDX? XNA?


Sorry for that. I'm using D3D9.

Share this post


Link to post
Share on other sites
For D3D9 you have to use vendor specific extensions. For ATI hardware check out this document. This functionality is available on Radeon HD 2000 cards and up. IIRC this functionality is also available on NVIDIA cards, though I can't find the reference at the moment (finding the ATI one was hard enough).

Share this post


Link to post
Share on other sites
Accessing data in back-front screen buffer is I see rough way to go.
If you need rendered data and you want to be using screen buffer, bind another surface as a rander target 2, by optimization it should be allready 32bit color surface with z buffer. outputing pixel color more times is not expensive that much.

pixel function that ouputs value to more surfaces is defined like this

struct RETURNS
{
float4 OutColor0 : COLOR0;
float4 OutColor1 : COLOR1;
};
RETURNS PixelFunction( ) : COLOR
{
RETURNS out;
out.OutColor0 = float4(1,0,1,1);// to screen buffer
out.OutColor1 = float4(1,0,1,1);// to bound target
return out;
}
You then can normaly usualy lock surface that you bound as render target 2.
At locking all GPU command using this surface must finish.

Share this post


Link to post
Share on other sites
Yeah rendering depth to a second render target or to an alpha channel is definitely the easiest way to do it in D3D9. The vendor hacks only work on certain hardware, and you also have to do a bit of math to get a linear depth value back from the z-buffer value.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!