Purpose of Depth Stencil Views in DirectX
This article provides an overview of Depth Stencil Views (DSVs) in DirectX, focusing on their essential role during the rasterization and output merger phases of the rendering pipeline. It details how DSVs interface with GPU resources to manage per-pixel visibility, execute conditional rendering effects through stencil operations, and optimize graphics performance through early depth testing.
What is a Depth Stencil View?
In DirectX (Direct3D 11 and 12), a Depth Stencil View (DSV) is a resource view that binds a texture resource to the Output Merger (OM) stage of the rendering pipeline as a depth-stencil buffer. While textures store raw numerical data, views define how the pipeline interprets and accesses that data format. The DSV grants the GPU hardware write and read access to depth and stencil buffers during primitive rasterization and pixel processing.
The Role of DSVs in Depth Testing
The primary purpose of a DSV during the rasterization pipeline is managing object occlusion via depth testing (or Z-testing).
When a primitive is rasterized into fragments (potential pixels), each fragment carries an interpolated depth value (\(Z\)) representing its distance from the camera viewport. The depth test functions as follows:
- Comparison: The GPU compares the incoming fragment's depth value against the current depth value stored at that pixel coordinate within the DSV.
- Evaluation: Based on a predefined comparison
function (such as
LESS,GREATER, orEQUAL), the pipeline determines whether the new fragment is closer to the viewer than the existing content. - Rejection or Acceptance: If the test fails, the fragment is discarded, preventing it from overwriting pixels that should be visible. If the test passes, the fragment is kept, and the DSV is updated with the new, closer depth value.
This mechanism ensures correct visual ordering without requiring the CPU to sort individual geometry primitives from back to front.
The Role of DSVs in Stencil Testing
The stencil portion of a DSV enables per-pixel masking. Unlike the continuous floating-point values of depth buffers, stencil buffers typically use unsigned 8-bit integers.
During the stencil test:
- Reference Comparison: The hardware compares a preconfigured reference value against the value present in the stencil buffer at the fragment’s location.
- Conditional Masking: If the stencil test fails, the fragment can be discarded immediately, regardless of depth.
- Buffer Modification: Depending on whether depth or stencil tests pass or fail, the value in the DSV can be updated using operations like replace, increment, decrement, or invert.
Stencil operations facilitate advanced rendering techniques, such as rendering non-rectangular decals, drawing outlines around objects, managing portals, and calculating stencil shadow volumes.
Early-Z Optimization
Although depth and stencil tests are logically part of the Output Merger stage at the end of the pipeline, modern hardware uses DSVs to perform tests immediately after rasterization—before the Pixel Shader runs. This feature is known as Early-Z.
By evaluating the DSV before executing complex lighting calculations or texture lookups, the GPU can discard occluded fragments immediately. This drastically reduces overdraw and saves significant memory bandwidth and processing cycles.
Read-Only Depth Stencil Views
DirectX also supports binding DSVs with read-only flags (e.g.,
D3D11_DSV_READ_ONLY_DEPTH or Direct3D 12 DSV flags). This
allows the depth buffer to continue performing depth rejection tests in
the Output Merger while simultaneously allowing other pipeline stages,
such as the Pixel Shader or Compute Shader, to read from the depth
buffer as a Shader Resource View (SRV). This capability is fundamental
for screen-space techniques like Screen Space Ambient Occlusion (SSAO),
depth-of-field, and soft particles.