DXR Any-Hit Shader: Alpha Testing and Performance
This article explores the function of the Any-Hit shader in DirectX Raytracing (DXR) and examines its traditional role in evaluating alpha-tested materials such as foliage and fences. It details the operational mechanics of the shader during hardware ray traversal, analyzes the significant performance penalties associated with frequent invocations, and explains why developers should restrict its usage in high-performance rendering pipelines.
What is the Any-Hit Shader in DXR?
In DirectX Raytracing, the Any-Hit shader is an optional programmable stage within the ray tracing pipeline that executes during ray traversal. When a ray intersects a primitive, the hardware checks if that primitive is associated with an Any-Hit shader. If it is, traversal pauses, and the Any-Hit shader executes to evaluate the candidate intersection before the hit is definitively confirmed.
Unlike the Closest-Hit shader—which executes only once per ray after
the traversal completes and identifies the nearest valid surface—the
Any-Hit shader can execute multiple times along a single ray's path.
Inside this shader, developers can inspect primitive attributes, sample
textures, and determine whether to accept the hit or discard it using
the intrinsic functions AcceptHitAndEndSearch() or
IgnoreHit().
The Role of Any-Hit Shaders in Alpha Testing
The primary use case for Any-Hit shaders is handling partially transparent surfaces, commonly known as alpha-tested, masked, or cutout geometry. Modern hardware ray tracing units test intersections against coarse geometric primitives (usually triangles), with no native knowledge of texture data or material properties.
When a ray intersects a polygon mapped with a cutout texture—such as a leaf with transparent borders—the Any-Hit shader is called to resolve visibility. The shader samples the material's alpha channel at the intersection UV coordinates:
- If the alpha value exceeds a defined threshold, the intersection is accepted, and traversal either continues or halts (for occlusion rays).
- If the alpha value falls below the threshold, the shader calls
IgnoreHit(), instructing the traversal engine to disregard the intersection and continue testing subsequent geometry.
Why Any-Hit Shaders Should Be Used Sparingly
While functionally convenient, relying heavily on Any-Hit shaders introduces substantial performance bottlenecks.
1. Interrupting Fixed-Function Hardware Traversal
Modern GPUs feature dedicated Ray Tracing Cores (RT Cores) optimized to process bounding volume hierarchy (BVH) traversal and ray-triangle intersection tests entirely in fixed-function hardware. When an Any-Hit shader is encountered, the hardware cannot resolve the hit automatically. Traversal must halt, transfer execution state to the programmable compute units (Streaming Multiprocessors / Compute Units), run the shader code, and then return control back to the fixed-function traversal hardware. This round-trip context switch significantly increases execution latency.
2. Out-of-Order Traversal and Redundant Evaluations
Hardware BVH traversal does not guarantee that candidate intersections are tested in strict depth order from front to back. Consequently, an Any-Hit shader may execute for distant, background surfaces that ultimately end up occluded by nearer, fully opaque geometry. Every rejected hit consumes memory bandwidth, texture cache, and register space for an operation that contributes nothing to the final image.
3. Thread Divergence and Latency
Ray tracing relies on executing groups of rays concurrently in warps or wavefronts. When rays encounter varying amounts of alpha-tested geometry, execution divergence occurs. Threads that complete their traversal early must stall and wait for sibling threads that are actively executing Any-Hit shaders. This divergence degrades GPU occupancy and compounds the cost of texture sampling within the traversal loop.
Optimization Strategies
To minimize Any-Hit overhead, graphics pipelines should adopt several mitigation techniques:
- Enforce Opaque Flags: Mark fully opaque geometry
with
RAY_FLAG_FORCE_OPAQUEor theD3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUEdescriptor to bypass the Any-Hit stage entirely. - Geometry Modeling: Model silhouettes more tightly with additional geometric detail rather than relying on large transparent texture areas, shifting the burden from programmable shading to efficient hardware intersection tests.
- Opacity Micromaps (OMMs): Utilize newer hardware extensions like Opacity Micromaps where available. OMMs bake alpha cutout states directly into micro-triangles within the BVH, allowing the fixed-function RT hardware to evaluate alpha masks without calling the Any-Hit shader.