# Depth test -opengl

Does depth test runs before fragment shader,hence not even processing the fragment with frag shader if fragment is discarded Or It first runs the fragment shader and then depth test I.e. fragment shader runs for even the discarded frags.

If prior is false,then can it be achieved some how?

## 1 Answer

The answer is: Both.

If your fragment shader writes to gl_FragDepth it will do the depth test after your shader.

If you leave gl_FragDepth alone it will do the depth test before your shader.

• Also if you use the discard keyword anywhere, even conditionally it will prevent using early tests – cozmic Feb 13 '17 at 11:03
• If you don't use either and using GLSL430 or higher you can use layout(early_fragment_tests) in; to force using early tests, otherwise it's driver-dependand – cozmic Feb 13 '17 at 11:05
• @cozmic If you use the discard keyword the GPU can still do an early Z test and skip pixels (and the fragment shader) that would not show. It does not affect the regular depth test. What discard prevents is doing the full hidden surface removal on deferred tile renderers when the Z test passes. – Stephane Hockenhull Feb 13 '17 at 16:35