Skip to content

Instantly share code, notes, and snippets.

@iddm
Last active January 26, 2023 19:49
Show Gist options
  • Save iddm/3de208ba5f81738b7c055a586ac3ecbb to your computer and use it in GitHub Desktop.
Save iddm/3de208ba5f81738b7c055a586ac3ecbb to your computer and use it in GitHub Desktop.
Vulkan Ray Tracing API documentation regarding gl_GeometryIndexID, gl_InstanceID, gl_PrimitiveID in the hit shader

Missing documentation found empirically for the aforementioned variables.

We can traverse only one TLAS at at time. While traversing it via traceRayEXT, we can hit just one BLAS at at a time. Hence, all the *ID references here are relative to the BLAS we hit (except gl_InstanceID):

  1. gl_GeometryIndexID - Geometry index within the BLAS this ray has hit.
  2. gl_InstanceID - BLAS index within the TLAS.
  3. gl_PrimitiveID - Triangle (or other primitive) index within the BLAS.

The gl_GeometryIndexID and all the mentioned variables are set automatically for the hit shader. If a BLAS is created with several geomeotries, then the gl_GeometryIndexID is the zero-based index of the geometry used during the BLAS creation.

When a TLAS is created from one or many of BLAS instances, the order of these BLAS instances is preserved and when a BLAS is hit, for the hit shader the gl_InstanceID is set.

I wish we had those documented better or it was easier to find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment