Understanding gl_HitKindEXT in GLSL Closest-Hit Shaders
This article explores the purpose and data provided by the
gl_HitKindEXT built-in variable in GLSL closest-hit shaders
under the GL_EXT_ray_tracing extension. It covers how this
variable communicates geometry orientation for hardware-accelerated
triangles and how it transfers custom classification data from
procedural intersection shaders directly to closest-hit evaluation.
What is gl_HitKindEXT?
In GLSL ray tracing pipelines, gl_HitKindEXT is a
built-in input variable of type uint. It is accessible
within both closest-hit shaders and any-hit shaders. The variable
identifies the specific nature of the hit that caused the shader to
execute, distinguishing between front-facing triangles, back-facing
triangles, and custom procedural geometry hits.
Hit Kinds for Built-in Triangle Geometries
When a ray intersects standard hardware-accelerated triangle
geometry, gl_HitKindEXT automatically reflects the winding
orientation of the intersected primitive relative to the incoming ray.
It provides one of two standardized constants:
gl_HitKindFrontFacingTriangleEXT(value0xFE/254): Indicates that the ray intersected the front face of the triangle, determined by the vertex winding order defined in the acceleration structure.gl_HitKindBackFacingTriangleEXT(value0xFF/255): Indicates that the ray intersected the back face of the triangle.
This allows closest-hit shaders to quickly determine surface
orientation without needing to perform manual dot-product comparisons
between the geometric normal and the ray direction
(gl_WorldRayDirectionEXT).
Hit Kinds for Custom Procedural Geometries
For procedural geometry defined by Axis-Aligned Bounding Boxes
(AABBs), ray-primitive intersections are calculated manually inside an
intersection shader using the reportIntersectionEXT
function:
bool reportIntersectionEXT(float hitT, uint hitKind);When an intersection is accepted, the integer value passed into the
hitKind parameter is directly forwarded to the closest-hit
shader and made readable through gl_HitKindEXT.
Key characteristics for procedural hit kinds include:
- Valid Range: Custom hit kinds must be within the
range of
0to127(0x00to0x7F). Values from128to255are reserved for built-in implementation types. - Application-Specific Data: Developers can use this 7-bit integer to encode contextual data, such as sub-primitive types (e.g., distinguishing between the body and caps of a procedural cylinder), material IDs, or specific intersection surfaces within a complex procedural volume.
Summary of Usage in Closest-Hit Shaders
Inside the closest-hit shader, gl_HitKindEXT
delivers:
- Facing Information: Immediate identification of front-facing versus back-facing triangle hits to flip shading normals or adjust lighting models accordingly.
- Intersection Metadata: User-defined identification flags emitted by custom intersection shaders, eliminating the need to pass auxiliary classification data through hit attributes or payload structures.