gl_LaunchSizeEXT in GLSL Ray Tracing Explained
In GLSL ray tracing pipelines using the
GL_EXT_ray_tracing extension, the built-in variable
gl_LaunchSizeEXT provides the total dimensions of the ray
dispatch grid. It enables ray tracing shaders to determine the overall
resolution or extent of the invocation workload, which is essential for
tasks such as calculating screen-space coordinates, deriving camera
aspect ratios, and indexing output buffers.
Type and Components
The gl_LaunchSizeEXT variable is defined as a
3-component unsigned integer vector:
in uvec3 gl_LaunchSizeEXT;Its individual components represent the full size of the dispatch domain across three dimensions:
gl_LaunchSizeEXT.x: The width of the ray launch grid (number of invocations in the X dimension).gl_LaunchSizeEXT.y: The height of the ray launch grid (number of invocations in the Y dimension).gl_LaunchSizeEXT.z: The depth of the ray launch grid (number of invocations in the Z dimension).
Relationship to Host API Commands
The values exposed by gl_LaunchSizeEXT correspond
directly to the dimension arguments specified on the host CPU when
issuing the ray tracing dispatch command. In Vulkan, these match the
width, height, and depth
parameters passed to vkCmdTraceRaysKHR or defined within
the indirect buffer for vkCmdTraceRaysIndirectKHR.
Shader Stage Availability
gl_LaunchSizeEXT is available as a read-only built-in
variable across all ray tracing shader stages:
- Ray Generation
- Intersection
- Any-Hit
- Closest-Hit
- Miss
- Callable
While accessible in all of these stages, it is most frequently utilized in the Ray Generation shader, where individual primary rays are constructed and launched.
Common Use Cases
1. Generating Normalized Device Coordinates (NDC)
To construct primary camera rays, the ray generation shader converts
the current invocation's discrete coordinate
(gl_LaunchIDEXT) into continuous coordinates normalized
between [0.0, 1.0] or [-1.0, 1.0]. Dividing
gl_LaunchIDEXT by gl_LaunchSizeEXT provides
the normalized pixel center:
vec2 uv = (vec2(gl_LaunchIDEXT.xy) + vec2(0.5)) / vec2(gl_LaunchSizeEXT.xy);2. Determining Viewport Aspect Ratio
Calculating the correct projection matrix or field of view requires the display aspect ratio. This can be derived directly from the launch dimensions without needing separate uniform parameters:
float aspectRatio = float(gl_LaunchSizeEXT.x) / float(gl_LaunchSizeEXT.y);3. Linear Buffer Addressing
When writing ray tracing output to a 1D linear storage buffer instead
of a 2D storage image, gl_LaunchSizeEXT is used to compute
the flattened array index:
uint linearIndex = gl_LaunchIDEXT.x +
gl_LaunchIDEXT.y * gl_LaunchSizeEXT.x +
gl_LaunchIDEXT.z * (gl_LaunchSizeEXT.x * gl_LaunchSizeEXT.y);