Introduction#
In a typical 3D scene, objects hide each other: a wall in front of a room, characters in front of buildings. Every time a pixel’s fragment shader runs for a surface that ends up hidden behind something else, that work is thrown away. Shading the same pixel more than once is called overdraw, and because fragment shaders are usually the most expensive part of drawing a frame (fragment shaders), it’s a big deal for performance. This post covers:
- What overdraw is, seen as a heat map counted on your own graphics card
- The early depth test, which lets the hardware skip hidden fragments
- Why draw order matters, even with a depth buffer
- The depth pre-pass, which gets overdraw down to almost nothing
Counting Shader Runs#
The demo below draws a crowd of 70 boxes. In heat map view, each pixel is coloured by how many times its fragment shader ran: blue for once, green for twice, yellow for about four times and red for eight or more. (It counts by having every shaded fragment add 1 to a render target with additive blending, then reads the totals back.) Drag to look around, and try the options.
A heat map of how many times each pixel’s fragment shader runs
Things to try:
- Turn the depth test off. Every fragment of every box is shaded, so the middle of the crowd glows red. (And the scene is wrong: whichever box is drawn last appears on top, as in the depth buffer post.)
- Turn it back on, with Back to front order. The picture is now correct, but it’s still mostly green and yellow: each box is drawn over the ones behind it, so pixels are shaded again and again before the nearest surface finally wins.
- Switch to Front to back. The nearest boxes are drawn first and fill the depth buffer, so later boxes behind them fail the depth test before their shader runs. The heat map turns mostly blue.
- Tick Depth pre-pass. Now every pixel is shaded exactly once, whatever the order.
The Early Depth Test#
In the pipeline, the depth test was described as part of output merging, after the fragment shader. But running a shader only to throw its result away is pure waste, so graphics cards try to do the depth test early: right after rasterisation, before the fragment shader. A fragment that’s already hidden is rejected and its shader never runs. The demo’s count models exactly this: it counts the fragments that pass the depth test at the moment they’re drawn.
Early depth testing only helps if the hidden surface is drawn after the surface that hides it. That’s why engines sort their solid objects roughly front to back: not for correctness (the depth buffer handles that), but for speed. (Transparent objects are the exception: they must be drawn back to front, which is why they overdraw so much.)
Some things force the hardware to fall back to testing after the shader, because the shader itself decides the final depth or whether the fragment exists: writing its own depth value, and, depending on the hardware, using discard (fragment shaders). That’s one reason alpha-tested foliage can be expensive.
The Depth Pre-Pass#
Sorting is approximate: objects overlap in complicated ways, and sorting individual triangles isn’t practical. A depth pre-pass (or z-prepass) gets it right every time. The scene is drawn twice:
- First with colour writes turned off and the simplest possible fragment shader, filling in only the depth buffer. This is very cheap per pixel.
- Then again, normally, with the depth test set to “equal”. Only the fragment that matches the nearest depth passes, so each pixel’s expensive shader runs exactly once.
The cost is drawing every triangle twice. When fragment shaders are expensive (complex lighting, many textures), that trade is often well worth it. When there’s lots of geometry and simple shading, it may not be.
Beyond testing individual fragments early, graphics cards keep a coarse summary of the depth buffer, often called hierarchical Z or Hi-Z: for each small tile of the screen, the furthest depth stored in it. The rasteriser can compare a whole tile of a new triangle against that summary and reject all of its pixels at once if they’re behind everything there, without touching the full depth buffer. Some hardware goes further. Most mobile graphics cards are tile-based: they collect all the triangles for a small tile of the screen before drawing it. Some, notably Apple’s and Imagination’s PowerVR designs (tile-based deferred renderers), then work out which surface is visible at each pixel before running any fragment shaders, removing most overdraw from solid objects whatever the draw order. Others, such as Arm’s Mali, use their own hidden-surface removal tricks that achieve much of the same.
Questions You Might Have#
Is overdraw always bad?
No: transparent effects like smoke, glass and particles need it, because they have to blend over what’s behind. It becomes a problem when there are many layers of an expensive shader, such as a screen full of overlapping smoke particles.
Summary#
- Overdraw is running a pixel’s fragment shader more than once; the hidden results are wasted.
- The early depth test rejects hidden fragments before their shader runs, but only for surfaces drawn after whatever hides them.
- Drawing solid objects front to back makes early rejection effective.
- A depth pre-pass fills the depth buffer first, so each pixel is shaded exactly once, at the cost of drawing geometry twice.
- Writing depth in the shader, and sometimes
discard, can disable early testing.
Further Reading#
- Fabian Giesen, A trip through the Graphics Pipeline, part 7: early and hierarchical depth testing in hardware.
- Khronos OpenGL Wiki, Early Fragment Test: exactly when early testing can and can’t happen.
- Arm, Mali GPU best practices: hidden surface removal: how tile-based mobile GPUs remove overdraw.
- RenderDoc: a free frame debugger whose “quad overdraw” overlay shows a heat map like this demo’s, for real games.