Hybrid View

gl_FragDeph not written to, but unexpected layout(early_fragment_tests) behavior

Hi,

My program behaves differently with and without layout(early_fragment_tests) in my fragment shader.
My fragment shader either writes out a pixel or discards, but I'm not writing to gl_FragDepth, I never use this variable in the code.
How can the result be different with and without early_fragment_tests enabled?

If you use atomic counters, image variables or shader storage buffers then early_fragment_tests has an effect:

a. If early_fragment_tests is used then no fragment shader invocations will be executed for those fragments that fail the depth or stencil test, thus atomic counter operations, image operations and shader storage buffer writes will not be performed for those fragments.
b. If early_fragment_tests is not used then there will be fragment shader invocations even for fragments that will eventually get discarded by the depth or stencil test, thus atomic counter operations, image operations and shader storage buffer writes will be performed for all fragments.

Other than that, there should be no difference. E.g. writes to framebuffer attachments will be identically performed in both cases.

Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
Technical Blog: http://www.rastergrid.com/blog/

I am not using atomics or image stores or loads in this shader, it is just a plain raster-or-discard routine.

I see that the problem happens only when discard has been called. Fragments that are behind a fragment that called discard are also discarded - I even see the background. It seems discarding the fragment still writes to the Z-buffer? Using 311.06 drivers. Weird

Okay, now I know what's your problem. You use discard to cull fragments, which actually works just fine, but you forget about that because you forced early tests depth test, and as a result the depth writes happen as well. that's why your depth buffer will reflect the data as if no fragments were culled using discard.

This is actually the correct behavior. If you want discard to also prevent depth writes, not just color writes, you cannot use early_fragment_tests.

Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
Technical Blog: http://www.rastergrid.com/blog/