How gl_HitTEXT Retrieves Hit Distance in GLSL
This article explains how the built-in variable
gl_HitTEXT retrieves the parametric hit distance inside a
GLSL closest hit shader within hardware-accelerated ray tracing
pipelines. It details the underlying hardware traversal process, the
handoff between intersection tests and closest hit execution, and how
the resulting \(t\) value is exposed to
reconstruct geometric contact points in ray tracing applications.
Understanding
gl_HitTEXT
In the Vulkan ray tracing pipeline (GL_EXT_ray_tracing),
gl_HitTEXT is a built-in input variable of type
float. It represents the scalar hit distance \(t\) along the ray vector where an
intersection occurs. A ray is mathematically represented as:
\[\mathbf{P}(t) = \mathbf{O} + t \cdot \mathbf{D}\]
where \(\mathbf{O}\) is the ray
origin and \(\mathbf{D}\) is the
normalized ray direction. The variable gl_HitTEXT stores
the exact scalar \(t\) that satisfies
this equation for the confirmed closest surface.
Hardware Traversal and Intersection Evaluation
The value assigned to gl_HitTEXT is not calculated
inside the closest hit shader itself; rather, it is determined during
the acceleration structure traversal stage by the GPU's fixed-function
ray tracing hardware (such as RT Cores on NVIDIA architectures):
- Ray Launch: The ray generation shader calls
traceRayEXT(), initializing a search range defined bytMinandtMax. - BVH Traversal: The hardware traverses the bounding volume hierarchy (BVH). When a ray intersects a leaf node containing primitive geometry, it triggers an intersection test.
- Distance Calculation:
- Built-in Triangles: The hardware uses dedicated ray-triangle intersection units (e.g., Möller–Trumbore or similar algorithms) to calculate the barycentric coordinates and the exact parametric distance \(t\).
- Custom Procedural Geometry: The user-defined
intersection shader computes \(t\)
manually and communicates it to the pipeline using
reportIntersectionEXT(t, hitKind).
- Hit Confirmation: If the calculated \(t\) falls within the current valid interval \([t_{\text{min}}, t_{\text{curr\_closest}}]\), the pipeline conditionally executes the any-hit shader (if present). If the hit is accepted, the internal register tracking \(t_{\text{curr\_closest}}\) is updated with the new smaller value.
Pipeline Handoff to the Closest Hit Shader
Once the traversal unit exhaustively tests the BVH and confirms no closer intersections exist, traversal terminates. The execution context transitions to the Closest Hit Shader (CHS) associated with the hit geometry's shader binding table (SBT) entry.
During this transition, the driver and GPU dispatch architecture map
the internal hardware register containing the final \(t_{\text{curr\_closest}}\) value into the
execution thread's input registers. In GLSL, this read-only register is
directly mapped to gl_HitTEXT.
Reconstructing World and Object Coordinates
Because gl_HitTEXT provides the parametric scalar,
shaders commonly use it alongside ray properties to compute the exact
position of the hit in world space:
vec3 hitPositionWorld = gl_WorldRayOriginEXT + gl_WorldRayDirectionEXT * gl_HitTEXT;Similarly, the position in object-local space can be reconstructed using the object-space ray vectors:
vec3 hitPositionObject = gl_ObjectRayOriginEXT + gl_ObjectRayDirectionEXT * gl_HitTEXT;By decoupling ray traversal from shading, gl_HitTEXT
allows the closest hit shader to receive the exact geometric contact
point without incurring redundant intersection mathematics.