   Access Z buffer in D3D
  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?   
  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 pixelfloat depth = pIn.posH.z;};  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   ;Quote:Original post by DieterVWThe 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?sIt'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.;  We need to know which version of the API you're using. D3D9? D3D10? SlimDX? XNA?   ;Quote:Original post by MJPWe need to know which version of the API you're using. D3D9? D3D10? SlimDX? XNA?Sorry for that. I'm using D3D9.;  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).   ;  If you want to read a depth buffer as texture you need to use a vendor hack. Check this page: http://aras-p.info/texts/D3D9GPUHacks.html   ;  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 thisstruct 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 targetreturn 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.;  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.   