Reputation: 11
I'm trying to read the depth values to create soft particles, but I couldn't find information about the method. Is there a built-in variable in hlsl to reach the depth values of a pixel position?
Upvotes: 1
Views: 3762
Reputation: 1819
You can't read the depth buffer while you're rendering your objects for the first time using a pixel shader. However, if you'd do a depth prepass first you can then transition the resulting depth buffer to a shader resource and sample from it like a texture in your pixel shader. To do a depth prepass or z-pass you just use a vertex shader without binding a pixel shader and render all your geometry. Then render again using a pixel shader.
Upvotes: 4
Reputation: 8598
No, this is not possible. The corresponding semantic is SV_Depth
, but it's write only.
For performance reasons vendors have opted not to implement anything that allows read-synchronization on depth and target buffers. The GPU is simply faster if it doesn't have to care about the possibility that someone might want to read those values during pixel shader execution.
See also here the docs for HLSL semantics including SV_Depth
and SV_Target
: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
This is also an interesting thread where people discuss this with links to papers if you like to dive a bit deeper into the topic (from 2008, but it aged well): https://forum.beyond3d.com/threads/reading-sv_depth-and-sv_target-in-pixel-shader-hardware-implementation-issues.44816/
Upvotes: 3