Search code examples
openglgraphicsdepth-testing

Will the depth be written immediately after Early Z passes?


If not, then why discard interrupted earlyZ? Imagine that there is an object A in front of B, A has a discard operation in fs.Assume that the pixel of A at screen coordinates (100, 100) pass the Early z test,then it is discarded in the fragment shader,so A has no chance to write depth.Now render B!Obviously A will not have any effect on B at (100,100). If this is the case, then why does A’s EarlyZ need to be interrupted?


Solution

  • Yes, the depth write is (as far as the hardware is concerned) part of the depth test. This is a atomic, ordered read/modify/write operation, so the hardware implements it that way.

    Therefore, fragment testing before the fragment shader also means writing those fragment values before the fragment shader. So even if the FS discards the fragment, part of its components have already been written and cannot be unwritten.

    So if an FS has a discard statement in it, the GPU will not use early tests unless the FS specifies that it must do so. And if it does specify that, then the discard will only partially discard the fragment.

    Note that this also includes things like occlusion query counts and stencil tests/writes.