Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write gl_FragDepth while still executing depth pre-test

Given a depth-prepass renderer, I have the minimum depth values a given fragment can possibly contain- thus, it makes no sense to consider any fragments farther than denoted.

Now, I have a shader which writes to gl_FragDepth, however is guaranteed to write a value greater than or equal to the depth value of its polygonal face. How can I still execute a depth-pretest (ie, if the fragment depth is farther than the buffer value, discard without shader execution), but allows me to write a value different (greater) than interpolated face depth if it passes the pre-test?

like image 272
Precursor Avatar asked Feb 11 '23 10:02

Precursor


1 Answers

Starting with OpenGL 4.2 (GLSL 4.20), the functionality you're looking for is available as a layout qualifier on gl_FragDepth. It allows you to specify your intent on how you are going to modify the depth output in the fragment shader. For example, the following specifies that you are only going to change the depth value to make in greater:

layout (depth_greater) out float gl_FragDepth;

This will allow the early depth test to still be used when the depth is modified in the fragment shader. If you do not follow the "contract" you establish with this qualifier, you will get undefined behavior. For example, with the qualifier above, if you make the depth smaller, fragments that would otherwise be visible may get eliminated.

The functionality is based on the GL_AMD_conservative_depth and GL_ARB_conservative_depth extensions. If you want to use it with OpenGL versions lower than 4.2, you could check for the presence of these extension, and use it in one of its extension forms if it's available.

like image 184
Reto Koradi Avatar answered Feb 13 '23 03:02

Reto Koradi