skip to content

What is opaque overdraw when a crowd of characters is drawn in arbitrary order, and when does a depth pre-pass pay off?

level: middleimportance: should knowfreq 58%

answer

  1. shading pixels that end up hidden
  2. early depth test needs nearer depth
  3. random order: a harmonic series
  4. depth first, then an equal test
  5. the pre-pass repeats vertex work

basics

~20 s

Overdraw is shading a pixel more than once because nearer surfaces drawn later cover earlier ones. A depth pre-pass writes depth first so the colour pass shades each visible pixel about once; it pays when fragment cost outweighs drawing geometry twice.

solid answer

~50 s

**Overdraw** is fragment work spent on surfaces that end up hidden. With early depth testing, a fragment is rejected before shading only if something nearer is already in the depth buffer, so draw order matters: front to back shades each pixel about once, back to front shades every layer, and random order over `d` layers averages `1 + 1/2 + ... + 1/d` shades, about 2.08 for four. A **depth pre-pass** renders all opaque geometry with a position-only program first; the colour pass then tests for equal depth with writes off, so only the visible surface is shaded. It costs a second trip through vertex processing and submission, which hurts a skinned crowd, so it pays when the fragment program is expensive and depth complexity is high. Cheaper options are a coarse front-to-back sort or a pre-pass of only the big occluders.

code

pseudocode · 10 lines
pseudocode
// Convention: smaller depth = nearer
pass depth_prepass:
    depth_test = LESS;  depth_write = ON;  colour_write = OFF
    for draw in opaque_draws:
        draw_with(position_only_program(draw))   // keeps the cutout test for alpha-tested draws

pass colour:
    depth_test = EQUAL; depth_write = OFF; colour_write = ON
    for draw in opaque_draws:
        draw_with(material_program(draw))        // same position arithmetic as the pre-pass

go deeper

for a junior

Recall that overdraw means shading pixels that are later covered, and that drawing opaque objects front to back lets the depth test reject hidden ones before shading.

for a middle

Explain early depth rejection, the expected cost of random order, and how a depth pre-pass with an equal test shades each visible pixel about once.

for a senior

Decide from measurements whether the pre-pass pays: compare vertex and submission cost against the fragment work saved, and watch for skinned geometry and position mismatches between passes.

for a principal

Treat opaque overdraw as renderer policy: draw-order rules, which geometry joins a pre-pass, and an overdraw view content teams can check, weighed against each platform's vertex budget.

## What overdraw is **Depth complexity** is the number of opaque surfaces that cover a pixel; **overdraw** is the shading spent beyond the one run that produces the visible result. It is usually reported as fragment-program runs per covered pixel, averaged over the screen, where 1.0 means no waste. A crowd of characters standing behind each other has high depth complexity, and whether that turns into overdraw depends on the order the characters are drawn in. This answer is about opaque surfaces; transparent and particle layers must all be shaded by design and carry their own overdraw budget. ## Why order matters: the early depth test Most GPUs test a fragment's depth against the depth buffer **before** running the fragment program, and skip the program when the fragment is hidden. That test can only reject what is already behind something drawn earlier: - If a nearer character was drawn first, a farther one is rejected before shading. - If the farther one was drawn first, it is shaded, and later overwritten by the nearer one. - If the program writes its own depth or discards fragments, the GPU may have to run it before testing, which removes the saving; where that happens in the pipeline is a separate topic. ## The expected cost of random order A whiteboard derivation that interviewers like: 1. Take `d` opaque layers over one pixel, drawn in uniformly random order, with a depth test that passes only for strictly nearer fragments. 2. The k-th layer drawn is shaded only if it is the nearest of the first `k` layers drawn. 3. Each of those `k` layers is equally likely to be the nearest, so that probability is `1/k`. 4. The expected number of shades is `1 + 1/2 + ... + 1/d`, the harmonic number `H(d)`. | Layers `d` | Front to back | Random order | Back to front | |---|---|---|---| | 2 | 1 | 1.5 | 2 | | 4 | 1 | 2.08 | 4 | | 8 | 1 | 2.72 | 8 | As this scenario's figure, suppose the crowd covers half of a 1920x1080 frame, 1,036,800 pixels, with a depth complexity of four. Random order shades about 2.16 million fragments, back to front about 4.15 million, and a perfect order or a pre-pass about 1.04 million. ## The depth pre-pass A depth pre-pass buys the perfect order without sorting perfectly: - **Pass one** draws every opaque object with a position-only program, colour writes off and depth writes on. Alpha-tested geometry keeps its cutout test here, or holes would be filled with depth. - **Pass two** draws the same objects with the full material program, with an **equal** depth test (or less-or-equal, taking smaller depth as nearer) and depth writes off. Only the fragment whose depth matches the stored nearest depth is shaded. - Both passes must compute positions with **identical arithmetic**; if they differ in the last bits, the equal test fails in speckles. - The finished depth buffer is available before shading begins, which other techniques, such as screen-space effects and per-tile light lists, can reuse. ## When the pre-pass pays The saving is the fragment work avoided; the cost is a second pass of vertex processing, primitive setup and draw submission. It tends to pay when: - the fragment program is expensive, for example many lights or many texture reads; - depth complexity is high, as in dense crowds, foliage interiors or city streets; - vertex work is cheap relative to fragment work. It tends to lose when: - the scene is vertex-bound, for example a crowd of skinned characters whose skinning would run twice unless skinned vertices are written once and reused; - overlap is low; - the GPU removes hidden opaque fragments itself, as some tile-based mobile GPUs do before shading, which makes ordering matter far less. ## Cheaper alternatives - **Coarse front-to-back sorting** by object distance costs a CPU sort and captures much of the benefit, though a character overlapping itself still overdraws. - **A partial pre-pass** of only the large occluders, such as terrain, walls and buildings, rejects most hidden fragments for a fraction of the vertex cost. - **Measure first.** A debug view that adds a constant per shaded fragment shows where overdraw actually is; a GPU capture's fragment-invocation count divided by covered pixels gives the number to compare before and after.

  • Why must positions match exactly between the pre-pass and the colour pass?
    The colour pass keeps a fragment only if its depth equals the stored one. If the two programs compute position with different instruction order or precision, depths differ in the last bits and the equal test fails in random speckles. Use the same position arithmetic in both programs and ask the compiler to keep that output invariant across them.
  • Why is a pre-pass less attractive for a crowd of skinned characters?
    Skinning runs in the vertex stage, so a pre-pass pays for it twice unless skinned vertices are written to a buffer once and read by both passes. With a cheap fragment program the saved shading may not cover the doubled vertex work. A coarse front-to-back sort of the characters often captures most of the benefit for no extra vertex cost.
  • How do you measure opaque overdraw in a scene?
    Render a debug view that adds a constant per shaded fragment and maps the count to a colour, or read fragment-invocation counters from a GPU capture and divide by covered pixels. Compare the figure with and without sorting or a pre-pass, so the decision rests on the scene's real depth complexity.

Painting a wall in several coats when only the top coat will ever be seen: the cheaper plan is to mark first where the final coat goes, then paint each spot once. Marking is a pass of its own, so it only pays when each coat is expensive to apply.

  • The depth test makes draw order irrelevant for opaque objects.
  • A depth pre-pass is always faster because every pixel is shaded once.
  • Overdraw only matters for transparent surfaces.
  • After a pre-pass, a strict nearer-than depth test still shades the visible surface.
  • Sorting objects by distance removes all opaque overdraw.