nils
nils

Reputation: 410

Shader execution after writing to gl_FragDepth

Given a fragment shader that modifies the original depth of the fragment and writes to gl_FragDepth. Will the code after writing to gl_Fragdepth still be executed if the depth test fails at that point?

Example:

in float depth;
layout(depth_less) out float gl_FragDepth;
void main() {
  float newDepth = depth - someModification;
  gl_FragDepth = newDepth;
  A();
}

Will A be executed if newDepth is greater than the current value in gl_FragDepth?

If so, what would be the alternative to stop the shader from doing unneccessary computations in A - without using a custom depth buffer?

Upvotes: 1

Views: 916

Answers (1)

mOfl
mOfl

Reputation: 478

In your example, A() will always be executed as long as it contributes to any output value, e.g. a color (otherwise, the compiler will remove it as an optimization). The depth test is a per-sample operation usually performed after the fragment shader. Under special circumstances, it is possible to do this test before the fragment shader, but this requires that the fragment shader itself does not write to gl_FragDepth.

Is the modification you do a uniform one or is it different for each fragment? If it is uniform, you could do it in a geometry shader - just apply the depth modification to the whole primitive. Then you could use the early-z. If it's on per-fragment basis, you could try binding the current depth render target as a readonly image, fetch the stored depth value, perform a manual comparison in your shader and discard the fragment if it fails. However, I neither know whether you can actually bind the currently bound framebuffer's render targets as images, even as readonly, nor whether this would be more or less performant than just executing A().

Upvotes: 1

Related Questions