Advertisement Jump to content
Sign in to follow this  

[D3D12] Depth Buffer problem (maybe)

This topic is 769 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

Hi, community.


I have a bug with Screen Space Ambient Occlusion and I suspect is the depth buffer information I use.


Depth Stencil View Format is DXGI_FORMAT_D24_UNORM_S8_UINT

Depth Stencil Shader Resource View Format is DXGI_FORMAT_R24_UNORM_X8_TYPELESS

Then its values should be between [0, 1]


I made a test to check if depth buffer is sampled correctly. This is the pixel shader (geometry is a full-screen quad). I disabled depth test.

struct Input {
	float4 mPosH : SV_POSITION;

Texture2D<float> DepthTexture : register(t0);

struct Output {
	float4 mColor : SV_Target0;

Output main(const in Input input){
	Output output = (Output)0;

	const int3 screenCoord = int3(input.mPosH.xy, 0);
	const float depthNDC = DepthTexture.Load(screenCoord);
	output.mColor = float4(depthNDC, depthNDC, depthNDC, 1.0f);
	return output;

Near Z is 1.0f

Far Z is 2000.0f

so I expect to get total white (1.0f, 1.0f, 1.0f) when the object is at 2000.0f Z and total black (0.0f, 0.0f, 0.0f) when object is at 1.0f Z


I recorded the following video, and if I modify Far Z to something like 500.0f or 5000.0f, I get the same black-white gradient results than the video.

That is why I think something is wrong.



Thoughts? Am I missing something?

Share this post

Link to post
Share on other sites
Depth contains 1/Z? In the case of a far plane Z of 5000, you will have a value near 0 (black color) and in the case of near plane Z of 1, you will have a value of 1 (white color). I think that is not correct, because when you clear depth buffer, you should use 1.0f.

Share this post

Link to post
Share on other sites
Yeah my statement was a little fast, i was just saying we do not store linear depth in a dept buffer. We store z/w to be precise, if you look at the projection matrix, you will find that w ends to be a z, while z is a mixture of scale and bias of z against near and far.

For the we clear at 1.f, it is because of the projection and a dept test set as less or equal.

It is possible to clear at 0.f, using a test set as greater while using afloating point depth buffer.

Share this post

Link to post
Share on other sites

Thanks for your suggestions, both links were useful. Finally, I understood why I see that color (due depth curve shape). Then the bug is not in the depth information. I will continue debugging.


Thanks !

Share this post

Link to post
Share on other sites

FYI a common trick used to visualize depth buffers is to just look at the last 10% of the depth range. Like this: 


float visualizedDepth = saturate((depthBufferVal - 0.9f) * 10.0f);
Edited by MJP

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!