Advertisement

Reverse Depth Buffer

Started by November 07, 2017 12:49 PM
10 comments, last by matt77hias 7 years, 3 months ago

After reading loads about depth buffer precision (kindly linked/discussed by some people on here) I can't seem to find an actual example of how to do it.

In my model shader I currently just write out z/w. Is it as simple as writing out 1 - z/w? That almost seems too simple.

 

 

  1. Change the DXGI_FORMATs of the resources/SRVs/DSVs of your depth buffer, shadow maps, etc. optional
  2. Swap the near and far plane while constructing the view-to-projection and projection-to-view transformation matrices of your camera, lights, etc.
  3. Change the D3D11_COMPARISON_LESS_EQUAL (D3D11_COMPARISON_LESS) to D3D11_COMPARISON_GREATER_EQUAL (D3D11_COMPARISON_GREATER) of your depth-stencil and PCF sampling states.
  4. Clear the depths of the DSVs of your depth buffer, shadow maps, etc. to 0.0f instead of 1.0f.
  5. Negate DepthBias, SlopeScaledDepthBias and DepthBiasClamp in the rasterizer states for your shadow maps.
  6. Set all far plane primitives to the near plane and vice versa. (Ideally using a macro for Z_FAR and Z_NEAR in your shaders.)

For more info: 

 

🧙

Advertisement

I wanted to have both options. So with one macro positioned in two files you can switch the z behavior in my complete engine :D

🧙

Thanks, I think I know how to do all of that in Monogame except:

  1. Swap the near and far plane while constructing the view-to-projection and projection-to-view transformation matrices of your camera, lights, etc.

CreateLookAt errors telling me that far must be greater than near.

So I don't do anything in the actual depth writing part of the shader?

 

15 minutes ago, skyemaidstone said:

CreateLookAt errors telling me that far must be greater than near.

Any chance of creating the matrix yourself or do you need to use the API method? The best solution would, however, remove that check from the API since this is common practice.

15 minutes ago, skyemaidstone said:

So I don't do anything in the actual depth writing part of the shader?

You need another depth test. The shader code itself stays the same, unless you use the far plane value somewhere. For instance, you have sky texture at the far plane, then you need to use 0.0f instead of 1.0f for the far plane.

🧙

That's too bad that MonoGame's functions complain about near > far, since it's now a pretty common way to create projection matrices. Perhaps you should create an issue on GitHub to see if they'll change the code. For now, you should be able to copy/paste their implementations into your own functions for creating a projection matrix, and then remove the check that far > near.

Advertisement

ok thanks guys :)

On 11/7/2017 at 8:31 AM, matt77hias said:

Change the DXGI_FORMATs of the resources/SRVs/DSVs of your depth buffer, shadow maps, etc. optional

You left out the to 32bit floating point format.

-potential energy is easily made kinetic-

18 minutes ago, Infinisearch said:

You left out the to 32bit floating point format.

How do you mean? That is the "changing the DXGI_FORMAT part", but it is not completely necessary (though it will give you the best precision at large distances) if one likes to work with 16 bit UNORM.

🧙

11 minutes ago, matt77hias said:

How do you mean?

Read what I quoted, you didn't say what to change it to.

-potential energy is easily made kinetic-

This topic is closed to new replies.

Advertisement