How to do it...

To ask the OpenGL pipeline to always perform the early depth test optimization, use the following layout qualifier in your fragment shader:

layout(early_fragment_tests) in; 

If your fragment shader will modify the fragment's depth, but you still would like to take advantage of the early depth test when possible, use the following layout qualifier in a declaration of gl_FragDepth within your fragment shader:

layout (depth_*) out float gl_FragDepth; 

In this, depth_* is one of the following: depth_any, depth_greater, depth_less, or depth_unchanged.

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.