Welcome, Unregistered.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Reply
Old 29-May-2012, 17:56   #1
jatobu
Registered
 
Join Date: May 2012
Posts: 4
Default read depthstencil in pixel shader

Basically that... on DX11 how can I read the depth buffer in the pixel shader?

Thanks!
jatobu is offline   Reply With Quote
Old 29-May-2012, 18:55   #2
MJP
Member
 
Join Date: Feb 2007
Location: Irvine, CA
Posts: 426
Default

1. Create the depth buffer's Texture2D using the appropriate typeless format for the desired depth/stencil format (DXGI_FORMAT_R24G8_TYPELESS for DXGI_FORMAT_D24_UNORM_S8_UINT, DXGI_FORMAT_R32G8X24_TYPELESS for DXGI_FORMAT_D32_FLOAT_S8X24_UINT, DXGI_FORMAT_R32_TYPELESS for DXGI_FORMAT_D32_FLOAT)
2. Create a depth stencil view for the Texture2D using the desired depth/stencil format
3. Create a shader resource view for the Texture2D that uses an appropriate texture format based on whether you want to read the depth values or the stencil values. So for instance if you want to read depth from a DXGI_FORMAT_D24_UNORM_S8_UINT depth buffer, you would use DXGI_FORMAT_R24_UNORM_X8_TYPELESS.
4. Bind the shader resource view to the pixel shader stage, and sample it like any other texture.
__________________
The Blog | The Book
MJP is offline   Reply With Quote
Old 30-May-2012, 20:48   #3
jatobu
Registered
 
Join Date: May 2012
Posts: 4
Default

thank you!.. that worked like a charm!
jatobu is offline   Reply With Quote
Old 31-May-2012, 17:39   #4
jatobu
Registered
 
Join Date: May 2012
Posts: 4
Default

uhmm... If I bind the DepthStencil only as a SRV (not as DSV) and do a ClearDepthStencilView() to some value (say 1.0f), and then read in the pixel shader the value of the DepthStencil, and writing that value into the RenderTarget using:

Texture2D depthStencil : register( t0 );
SamplerState ss : register ( s0 );

float PS( float2 tex : TEXCOORD0 ) : SV_TARGET
{
return depthStencil.Sample( ss, tex );
}


I always get 0.0f

for simplicity I have configured the DepthStencil to be a DXGI_FORMAT_D32_TYPELESS, and the RenderTarget with a DXGI_FORMAT_R23_FLOAT type.

am I doing something wrong?

Thanks
jatobu is offline   Reply With Quote
Old 31-May-2012, 18:22   #5
jatobu
Registered
 
Join Date: May 2012
Posts: 4
Default

I have found the problem, all the values I was clearing the depthbuffer with were beyond 1.0f, which DirectX resets back to 0.0f.
jatobu is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:39.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.