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 ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.