Why does a fragment program that writes its own depth or discards fragments weaken early depth testing, and how do you keep it working?
answer
- the test is logically after shading
- hardware hoists it when unobservable
- unknown depth, unknown survival
- a conservative depth promise
basics
~20 sEarly depth testing tests fragments before shading only when the result cannot differ. Program-written depth is unknown until the program runs and a discard leaves survival unknown, so the GPU tests or writes depth late and shades more hidden fragments.
solid answer
~50 sThe specification places the depth/stencil test after the fragment program; GPUs move it earlier, and add a coarse per-tile depth that rejects whole blocks, whenever that cannot change the image. If the program writes depth, the value to test does not exist until it has run, so the test runs late and coarse rejection is often lost. If it discards, many GPUs still test early but must delay the depth and stencil write until the program decides; some move the whole test late. A program with side-effect writes must run even for hidden fragments unless early tests are explicitly requested. To keep the early test: restrict depth output and discard to the materials that need them, draw those after the solid geometry, prefer a **conservative depth** promise (with a less-than test on standard depth, written depth only ever increases), and request early tests only when the program neither writes depth nor discards.
go deeper
Know that the depth test can reject a hidden fragment before its program runs, and that this saves shading work.
Explain the three conditions for testing early and how program-written depth, discard and side-effect writes each break one of them.
From a frame that got slower after a depth-writing or discarding material was added, find the cause, then fix it with draw order, a conservative depth promise or a separate pass.
Decide which material features the project allows on opaque geometry, such as per-pixel depth or dissolve discards, given what each costs in early rejection across the target hardware.
## The specification says late, the hardware tries early In the logical pipeline the **depth/stencil test** comes after the fragment program: the program runs, then its fragment is compared with the depth buffer, kept or rejected, and the depth buffer is updated. Run literally, that would shade every hidden fragment of every overlapping surface. So GPUs perform **early depth testing**: they test, and usually write, depth before the program runs, and skip the program for fragments that fail. Many GPUs add a **coarse depth** level as well, a per-tile record of depth bounds kept on the chip, which rejects whole blocks of fragments in one step. Early testing is legal only when it cannot change the result. Before the program runs, the hardware must be sure of three things: 1. **The depth to test is already known**: it is the interpolated depth from the rasterizer. 2. **The fragment will survive**, so writing its depth and stencil now cannot leave a value that a discarded fragment should never have written. 3. **Skipping the program has no visible effect**, so a rejected fragment owes no write anywhere else. ## Writing depth from the program A fragment program that outputs its own depth breaks the first condition. Per-pixel depth is written for impostor spheres drawn on flat cards, for surface-offset effects, and for logarithmic depth. The value to compare does not exist until the program has run, so the test moves after shading, and many GPUs also stop using coarse depth for that draw. Writing a value that happens to equal the interpolated depth does not help: the hardware cannot know it is equal. The remedy, where it is offered, is a **conservative depth** declaration: a promise that the program only moves depth in one direction. With a less-than test on standard depth (0 near, 1 far), a promise that the written depth is never smaller than the interpolated one lets the hardware reject early, because if the interpolated depth already fails, any larger value fails too. Fragments that pass that early check still get the full test after the program. ## Discarding fragments A program that discards fragments, for example a dissolve effect eating through a character, breaks the second condition. Depending on the GPU, the hardware either runs the whole test late, or tests early and delays only the depth and stencil write until the program has decided. Either way some benefit is lost: the depth buffer and coarse depth are updated later, so surfaces drawn behind in the same area are rejected less efficiently. Programs that change the sample coverage mask behave the same way. ## Side effects A fragment program that writes to a buffer or image with side effects breaks the third condition: by the specification it must run even for fragments that end up hidden. Most APIs let you request early tests explicitly, so the hardware tests before the program and skips hidden fragments. That request changes the program's meaning: a depth value it writes is ignored and a discard no longer undoes the depth write, so use it only when the program does neither. ## Program features and early testing | The fragment program... | Early test | Early depth write | |---|---|---| | does none of the below | yes | yes | | discards or changes coverage | on many GPUs | no, after the program | | writes depth, no promise | no | no | | writes depth with a conservative promise | early rejection only | no | | writes side-effect buffers | only if requested | only if requested | ## Keeping the early test working - Keep depth output and discard on the materials that need them, rather than on every variant of a shared material. - Draw discarding and depth-writing materials **after** the solid opaque geometry, so they are tested against an almost complete depth buffer. - For heavy discarding content, a depth-only pass that runs the discard first lets the main pass test with an `equal` comparison and no discard; whether that extra pass pays off is a question of fragment cost. - Prefer a conservative depth promise over unrestricted depth output. - Avoid flipping the depth comparison direction inside a frame; on some GPUs it disables coarse depth until the next depth clear. - Remember that **blending does not block early testing**: blending happens after the test, and a transparent surface with depth writes off still benefits from early rejection.
- A dissolve effect discards fragments on a few characters; how do you limit its cost to the rest of the frame?Draw the dissolving characters after the solid opaque geometry, so they test against a nearly complete depth buffer and occlude little that is drawn later. Use the discarding variant only while a character is actually dissolving, not on every character with a threshold of zero. If many are on screen, a depth-only pass with the discard lets the main pass use an equal test without discarding.
- Does enabling alpha blending for a draw disable early depth testing?No. Blending happens after the depth test and does not affect whether a fragment survives it, so a blended fragment hidden behind opaque geometry is still rejected early. Transparent surfaces usually turn depth writes off but keep the test on, which is exactly the case early rejection handles well.
- What is coarse or hierarchical depth in hardware, and what can disable it?Many GPUs keep per-tile depth bounds on the chip and reject a whole block of fragments when the triangle's depth range there fails against them. Depending on the GPU it can be disabled for a draw by program-written depth, by flipping the comparison direction mid-frame, or by writes that bypass it, and it is typically restored after a depth clear.
- Early depth testing must be switched on by the application; the GPU never does it automatically.
- Discarding a fragment is free because the fragment is thrown away anyway.
- Alpha blending turns off early depth testing for the whole draw.
- Writing depth is harmless if the program writes the same value as the interpolated depth.
- Forcing early tests is always safe, whatever the fragment program does.