[D3D12] Depth Buffer problem (maybe)

Started by
6 comments, last by MJP 7 years, 4 months ago

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?

Advertisement
It is normal, the depth contains 1/Z, so 500 or 1000 has no real impact :)
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.

Here this contains the exact formula for what's in the depth buffer.

https://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

-potential energy is easily made kinetic-

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.

This is another good article to read if you're interested in depth buffer precision: http://www.reedbeta.com/blog/depth-precision-visualized/

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 !

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);

This topic is closed to new replies.

Advertisement