How does Hi-Z occlusion culling test an object's bounds against a depth pyramid, and why do many renderers draw last frame's visible objects before building it?
answer
- a mip chain of depth
- keep the farthest value per texel
- pick the level where bounds fit 2x2
- current frame's depth does not exist yet
- two phases: draw the known-visible, test the rest
basics
~20 sHi-Z culling builds a mip chain of depth keeping each region's farthest value, then rejects an object whose nearest depth is behind it. Drawing last frame's visible set first gives a current pyramid without stale-depth false culls.
solid answer
~50 sAssume standard depth, 0 at the near plane and 1 at the far plane. The **depth pyramid** is a mip chain in which each texel stores the **maximum** (farthest) depth of the 2x2 texels below it. To test an object, project its bounding box to a screen rectangle, find the box's nearest depth, pick the mip level at which the rectangle is at most one texel wide so it touches at most 2x2 texels, and fetch those. If the box's nearest depth is farther than the farthest of them, every visible pixel there is in front of the object, so it is occluded; a box crossing the near plane is reported visible. The catch is that this frame's depth does not exist until you draw. Reusing last frame's pyramid culls objects that motion has just revealed, so a **two-phase** scheme draws last frame's visible set first, builds the pyramid from that depth, then tests everything else against it and draws what passes.
code
pseudocode · 14 lines// standard depth: 0 at the near plane, 1 at the far plane
// each pyramid texel holds the FARTHEST depth of the region it covers
function isOccluded(box, viewProj, pyramid):
rect = emptyRect(); nearestDepth = 1
for each corner c of box:
clip = viewProj * (c, 1)
if clip.w < nearPlaneDistance:
return false // crosses the near plane: keep it
rect = grow(rect, toPixels(clip.xy / clip.w))
nearestDepth = min(nearestDepth, toDepth01(clip))
level = ceil(log2(max(rect.width, rect.height, 1)))
level = min(level, pyramid.mipCount - 1)
farthest = max of the 2x2 texels of pyramid[level] covering rect
return nearestDepth > farthestgo deeper
Recall that occlusion culling skips objects hidden behind others, and that a depth pyramid is a mip chain built from the depth buffer.
Explain why each texel keeps the farthest depth, how the mip level is chosen from the projected rectangle, and why near-plane crossings are kept.
Diagnose stale-depth false culling and query latency, and justify the two-phase scheme, including re-testing phase-1 objects and the cost after a camera cut.
Weigh where occlusion culling lives for the project, GPU pyramid, CPU occlusion buffer or precomputed data, against content type, platform and how much the team can debug.
## What Hi-Z culling is for In a dense city, the frustum still contains thousands of buildings hidden behind the facades of one street. **Hi-Z occlusion culling** (hierarchical depth) rejects them by comparing each object's bounds with a coarse picture of what is already in front. It suits GPU and compute-pass culling because the whole test is a handful of texture reads per object, with no readback to the CPU. Throughout, **standard depth** is assumed: 0 at the near plane, 1 at the far plane, smaller is nearer. With reversed depth every max below becomes a min and every comparison flips. ## Building the depth pyramid 1. Start from a depth buffer, often at reduced resolution. 2. Build each mip level by taking, for every texel, the **farthest** (maximum) of the 2x2 texels below it. 3. When a level has an odd width or height, fold the extra row or column into the neighbouring texel; otherwise a strip of depth is never represented and the test can cull visible objects. Keeping the farthest value is what makes the test **conservative**: a texel says that nothing visible in its region is farther than this depth. ## Testing one object 1. Project the eight corners of the object's bounding box to screen space. If any corner lies behind the near plane, stop and report the object visible. 2. Take the screen rectangle the corners cover and the **nearest** depth of any corner. 3. Choose the mip level at which the rectangle is at most one texel wide: `level = ceil(log2(max(widthPx, heightPx)))`. At that level the rectangle touches at most 2x2 texels. 4. Fetch those texels and take their maximum. 5. If the box's nearest depth is greater than that maximum, the object is behind everything already drawn there: it is **occluded**. The test is coarse: a large object reads a coarse level and is kept whenever any part of the region is open. That is acceptable, because a false positive only costs a draw. ## Where the depth comes from The pyramid must describe this frame's occluders, but this frame's depth only exists once you have drawn them. The options: | Source | How | Failure | |---|---|---| | Occluder pre-pass | draw selected large occluders, often simplified meshes, to depth first | needs authored or chosen occluders; small props occlude nothing | | Last frame's pyramid, reprojected | reproject last frame's depth by the camera motion | holes and stale occluders: newly revealed objects are culled for a frame and pop in | | Two-phase | draw last frame's visible set, build the pyramid, test the rest | an extra pyramid build and a split of the frame's draws | ## The two-phase scheme - **Phase 1**: frustum-cull and draw every object that was visible last frame. In a slow-moving camera this is nearly the whole visible set. - Build the depth pyramid from the depth phase 1 just wrote. It describes real occluders at their current positions. - **Phase 2**: test every object not drawn in phase 1 against that pyramid; draw the ones that pass. Re-test the phase-1 objects as well, so that objects which have become hidden drop out of next frame's set. - Store each object's visibility for the next frame. Nothing visible is ever culled by stale depth, because the only occluders in the pyramid are real this frame. After a camera cut phase 1 draws the wrong set and phase 2 does most of the work, so the frame costs more but is still correct. ## Compared with occlusion queries and CPU rasterization A hardware **occlusion query** draws an object's bounding box with colour and depth writes off and counts the samples that pass the depth test. Its result arrives only after the GPU has executed it; waiting for it stalls the CPU, and using last frame's answer makes objects appear a frame late. Thousands of queries also mean thousands of draws. A **software occlusion buffer** rasterizes low-poly occluders into a small depth buffer on the CPU with SIMD, then tests boxes against it: no GPU latency, but CPU time and authored occluders. Hi-Z in a compute pass avoids both readback and per-object draws, which is why GPU-driven renderers favour it.
- Why do hardware occlusion queries tend to cause either stalls or popping?A query's sample count is known only after the GPU has run that draw, typically a frame or more after the CPU recorded it. Waiting for it empties the queued work and stalls; using last frame's result instead means an object that has just come into view stays culled for a frame and pops in.
- Why do foliage and thin poles make poor occluders in a depth pyramid?Their depth is full of gaps, and each pyramid texel keeps the farthest value in its region, so any open sky or distant wall behind a leaf or a pole sets the texel far away. At the coarse levels used for large objects those regions occlude almost nothing.
- What does a camera cut do to a two-phase Hi-Z culler?Phase 1 draws last frame's visible set, which no longer matches the view, so the pyramid has few useful occluders and phase 2 has to draw most of the newly visible objects. The image stays correct, but that frame costs more; some renderers skip phase 1 after a cut.
- Each Hi-Z pyramid texel should store the nearest depth of its region.
- Last frame's depth pyramid can be reused unchanged without visible errors.
- An object crossing the near plane can be tested like any other.
- Reading an occlusion query back in the same frame is free.
- Hi-Z culling is exact, so it never draws hidden objects.