0
\$\begingroup\$

I am currently trying to understand why using discard inside of a fragment shader disables early z.

I am using a simple cutout shader, which discards fully transparent pixels.

In a scenario where I render two overlapping triangles (with one draw call), each with a texture containing a few fully transparent pixels (a sprite with a transparent background), in a front to back order, the early z test should be no problem for the front triangle. The fragment shader is called for each pixel of the first triangle and only the opaque pixels update the depth buffer.

After that, the back triangle will be rasterized. Now early z should also work, because the depth buffer is updated. For each pixel that should be visible after early z test the fragment shader simply gets executed, for those which don't it doesn't matter if they are discarded or not, because they do not update the depth buffer anyways.

Where exactly is my mistake? Does it not work because the triangles are rendered in parallel? That would confuse me even more because I know that for rendering translucent pixels the order is crucial.

Thank you!

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The gpu is highly parallel, which means that the 2 triangles are shaded simultaneously (as in the second triangle is started before the first is finished) and the outputs of the fragment shader are then ordered in api order and depth tested and blended.

The gpu has special circuitry to ensure the final blending step (the part that actually cares about order) happens in api order.

This means that many small triangles can still make full use of the highly parallel shading units even if they all fully overlap.

Though if your drawcall is large enough then there will be enough time between the first fragments being fully blended and their new depth written out for the early Z to see the new depth to test against.

\$\endgroup\$
1
  • \$\begingroup\$ There's a more detailed explanation of what's going on under the hood at: fgiesen.wordpress.com/2011/07/08/… \$\endgroup\$ Commented Oct 9 at 1:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.