Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

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

Hybrid View

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jul 2010
    Posts
    123

    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?

    Cheers,
    Fred

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    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/

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jul 2010
    Posts
    123
    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

  4. #4
    Junior Member Regular Contributor
    Join Date
    Jul 2010
    Posts
    123
    Same result with the latest 320.18 drivers.

    I'm using FBO attachments, COLOR0 + DEPTH. Surely early_fragment_tests works correctly with these in place, I don't see any reason why it wouldn't?

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jul 2010
    Posts
    123
    early_fragment_tests doesn't work with discard, period.

    With early_fragment_tests disabled:

    Click image for larger version. 

Name:	early_fragment_tests_disabled.jpg 
Views:	54 
Size:	8.0 KB 
ID:	1061

    And now with early_fragment_tests enabled:

    Click image for larger version. 

Name:	early_fragment_tests_enabled.jpg 
Views:	51 
Size:	7.1 KB 
ID:	1062

    To perform my test, I did the following:

    1) Downloaded the following tutorial (Windows): http://www.opengl.org/sdk/docs/tutor...s/lighting.php
    2) Modified vertexShader.txt:

    Code :
    #version 420 compatibility
     
    void main(void)
    {
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
      // Add this line below
      gl_TexCoord[0].st = gl_MultiTexCoord0.st;
    }

    3) Modified fragmentShader.txt:

    Code :
    #version 420 compatibility
    //Uncomment this line to see the effect of discard with this layout:
    //layout(early_fragment_tests) in;
     
    void main (void)  
    {  
      if (gl_TexCoord[0].y > 0.5)
        discard;
      else
        gl_FragColor = vec4(gl_TexCoord[0].st, 0, 1);
    }

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    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/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •