Hierarchical Z-Buffering for DirectX Occlusion Culling
Hierarchical Z-buffering (HZB) is an optimization technique that accelerates occlusion testing by constructing a mipmapped depth pyramid of the rendered scene. Instead of testing high-polygon meshes or individual pixels against a full-resolution depth buffer, HZB allows rendering engines to quickly test coarse bounding volumes against lower-resolution depth representations. In modern DirectX pipelines, this approach moves visibility determination entirely to the GPU, eliminating the CPU-GPU synchronization stalls typical of traditional hardware queries and dramatically reducing geometric overdraw.
Constructing the Depth Pyramid
HZB begins with the scene’s existing depth buffer, typically rendered during an early depth pre-pass (Z-prepass) or carried over from the previous frame. A compute shader in DirectX generates a sequence of downsampled depth maps (a MIP chain), where each level is half the width and height of the previous one.
Unlike standard color mipmapping, which averages pixel values, HZB uses conservative reduction:
- In a standard depth configuration (0.0 near, 1.0 far), each texel in mip level \(n+1\) stores the maximum depth of the corresponding \(2 \times 2\) texel block in level \(n\).
- In a reversed-Z configuration (1.0 near, 0.0 far), which is standard in DirectX to maximize floating-point precision, each texel stores the minimum depth of the \(2 \times 2\) block.
This conservative filtering guarantees that the value stored at any mip level represents the closest occluding surface within that entire block's footprint.
Software Occlusion Testing Workflow
Once the pyramid is constructed, the DirectX pipeline uses compute shaders to test the visibility of scene geometry before issuing draw commands.
- Bounding Volume Projection: Each instance's oriented or axis-aligned bounding box (AABB) is transformed into clip space to determine its screen-space extents and its closest depth value to the camera.
- Mip Level Selection: The compute shader calculates the screen-space size of the bounding box. It selects a mip level where the projected area of the bounds covers roughly a \(1 \times 1\) to \(2 \times 2\) texel footprint.
- Depth Fetch and Comparison: The shader reads the depth value from the selected HZB mip level. Because that texel covers the entire area of the bounding box, a single texture fetch reveals the closest occluder in that screen region.
- Visibility Determination: If the closest depth of the bounding box is farther from the camera than the value stored in the HZB, the entire object is guaranteed to be fully occluded and is rejected. If it is closer, the object is marked visible.
Acceleration Advantages in DirectX
HZB provides distinct performance advantages over standard DirectX hardware occlusion queries and brute-force pixel depth testing:
- Elimination of Pipeline Latency: Traditional
DirectX hardware occlusion queries
(
D3D12_QUERY_HEAP_TYPE_OCCLUSION) require issuing queries and waiting for the GPU to return results, causing pipeline bubbles or forcing a multi-frame latency delay. HZB executes entirely within compute passes on the GPU timeline, avoiding CPU readbacks. - Fewer Memory Accesses: Testing against a full-resolution depth buffer requires rasterizing bounding volumes and performing hundreds of depth comparisons. HZB resolves visibility with one to four texture samples per instance, minimizing memory bandwidth usage.
- DirectX 12 GPU-Driven Rendering: HZB integrates
directly with DirectX 12 indirect drawing
(
ExecuteIndirect). The compute shader that performs the HZB test directly populates the indirect argument buffer, allowing the GPU to cull occluded objects and dispatch only the visible draw calls without any CPU intervention.