<p>When depth testing is enabled, the testing occurs after the fragment shader executes. The procedure is relatively simple (and spelled out pretty well <a href="https://www.khronos.org/opengl/wiki/Depth_Test" rel="nofollow noreferrer">here</a>):</p>

<ul>
<li>Get depth of fragment</li>
<li>Test against depth buffer</li>
<li>Discard if it fails</li>
</ul>

<p>Early-z discard is when a rasterizer discards a fragment before a fragment shader runs because it's clear early on that it won't pass the depth test. However, the rasterizer can't safely do this if certain things happen (such as calling <code>discard()</code> or modifying the fragment depth in a fragment shader). This is a hardware optimization and isn't required to occur unless you force it with OpenGL 4.2 or with an extension.</p>

<p>There are things that can <a href="https://www.khronos.org/opengl/wiki/Early_Fragment_Test#Limitations" rel="nofollow noreferrer">prevent early-z discard from occurring</a>. To achieve it:</p>

<ul>
<li>Don't use <code>discard()</code> in a fragment shader</li>
<li>Don't modify the depth of a fragment in a fragment shader</li>
<li>Don't use alpha testing (deprecated)</li>
<li>Make sure you have depth testing turned on</li>
<li>Make sure you have a depth buffer</li>
</ul>

CC BY-SA 3.0
2017-02-15T02:55:06.983Z
2017-02-15T03:01:45.460Z
2017-02-15T03:01:45.460Z
aces
<p>After Z-Buffer Testing does the fragment shader run on the discarded fragments?
Does the Z-Test happen after the fragment shader runs?</p>

<p>Please, tell me the step by step events that occur in the Z-Test.</p>

<p>Also what is early Z-Test and how to achieve it?</p>

<opengl><rasterizer><fragment-shader><buffers>
Z buffer working in OpenGL
VersesDev
# Z buffer working in OpenGL

(Asked by: username_0 on 2017-02-14)

After Z-Buffer Testing does the fragment shader run on the discarded fragments?
Does the Z-Test happen after the fragment shader runs?

Please, tell me the step by step events that occur in the Z-Test.

Also what is early Z-Test and how to achieve it?

**Tags:** opengl, rasterizer, fragment-shader, buffers


---

## 1 answers

### Answer by username_1 on 2017-02-15. Score: 4 | Accepted answer

When depth testing is enabled, the testing occurs after the fragment shader executes. The procedure is relatively simple (and spelled out pretty well [here](https://www.khronos.org/opengl/wiki/Depth_Test)):

* Get depth of fragment
* Test against depth buffer
* Discard if it fails

Early-z discard is when a rasterizer discards a fragment before a fragment shader runs because it's clear early on that it won't pass the depth test. However, the rasterizer can't safely do this if certain things happen (such as calling `discard()` or modifying the fragment depth in a fragment shader). This is a hardware optimization and isn't required to occur unless you force it with OpenGL 4.2 or with an extension.

There are things that can [prevent early-z discard from occurring](https://www.khronos.org/opengl/wiki/Early_Fragment_Test#Limitations). To achieve it:

* Don't use `discard()` in a fragment shader
* Don't modify the depth of a fragment in a fragment shader
* Don't use alpha testing (deprecated)
* Make sure you have depth testing turned on
* Make sure you have a depth buffer


---

