How it works...

The following statement forces the OpenGL implementation to always perform the early depth test:

layout(early_fragment_tests) in; 

We must keep in mind that if we attempt to modify the depth anywhere within the shader by writing to gl_FragDepth, the value that is written will be ignored.

If your fragment shader needs to modify the depth value, then we can't force early fragment tests. However, we can help the pipeline to determine when it can still apply the early test. We do so by using one of the layout qualifiers for gl_FragDepth as shown before. This places some limits on how the value will be modified. The OpenGL implementation can then determine if the fragment shader can be skipped. If it can be determined that the depth will not be changed in such a way that it would cause the result of the test to change, the implementation can still use the optimization.

The layout qualifier for the output variable gl_FragDepth tells the OpenGL implementation specifically how the depth might change within the fragment shader. The qualifier depth_any indicates that it could change in any way. This is the default.

The other qualifiers describe how the value may change with respect to gl_FragCoord.z:

  • depth_greater: This fragment shader promises to only increase the depth.
  • depth_less: This fragment shader promises to only decrease the depth.
  • depth_unchanged: This fragment shader promises not to change the depth. If it writes to gl_FragDepth, the value will be equal to gl_FragCoord.z.

If you use one of these qualifiers, but then go on to modify the depth in an incompatible way, the results are undefined. For example, if you declare gl_FragDepth with depth_greater, but decrease the depth of the fragment, the code will compile and execute, but you shouldn't expect to see accurate results.

If your fragment shader writes to gl_FragDepth, then it must be sure to write a value in all circumstances. In other words, it must write a value no matter which branches are taken within the code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.126.241