Early Fragment Test

From OpenGL.org
Revision as of 18:50, 15 September 2009 by Alfonse (talk | contribs) (Writing full article.)

Jump to: navigation, search

Early Depth Test is a hardware feature supported by many GPUs that allow the depth test to proceed before fragment processing in the rendering pipeline, so long as certain conditions are met.


This is an optimization. If the fragment is behind other geometry, based on the depth values in the depth buffer, it saves performance to not bother executing the fragment shader. So the performance saved is only in the fragment shader.

The most effective way to use early depth test hardware is to run a depth-only pre-processing pass. This pass uses a minimal shader, one that transforms position values only. It masks color writes, so it only writes to the depth buffer.

This gives the best performance effect if your fragment shaders are expensive, or if you intend to use multiple passes across the geometry.


The OpenGL Specification states that depth testing happens after fragment processing. A fragment's depth is part of the input to the fragment processing, and the fragment shader is free to modify this value or create an arbitrary depth. Thus, doing the depth test before fragment processing would make this impossible.

However, despite the wording of the spec, it is possible to do the depth test earlier in the pipeline. What the spec requires is that the depth test functions as if it were done after the fragment shader.

Thus the first restriction on early depth tests is that they cannot happen if the fragment shader writes the depth value. If the fragment shader modifies the depth, then the depth test must wait until after the fragment shader executes.

There can be other hardware-based limitations as well. For example, some hardware will not execute an early depth test if the (deprecated) alpha test is active, as these use the same hardware on that platform. Because this is a hardware-based optimization, OpenGL has no direct controls that will tell you if early depth testing will happen.