-
Phase 3 — GPU and Vulkan Ray Tracing
- Moving from CPU to GPU. Vulkan’s ray tracing extension (
VK_KHR_ray_tracing_pipeline) exposes hardware RT units (NVIDIA RTX, AMD RDNA2+).
- Parent: PathTracer Learning
-
3.1 Vulkan Ray Tracing Overview
- PathTracer Learning - Vulkan RT Pipeline
- Full pipeline breakdown: ray gen, intersection, any-hit, closest-hit, miss shaders
- Shader Binding Table (SBT) construction and indexing
- PathTracer Learning - BLAS and TLAS
- How geometry is organized for hardware traversal
- Key extensions required
VK_KHR_ray_tracing_pipeline — the RT pipeline itself
VK_KHR_acceleration_structure — BLAS/TLAS management
VK_KHR_ray_query — inline ray queries from any shader stage
VK_KHR_buffer_device_address — GPU pointers (required for SBT)
VK_KHR_deferred_host_operations — async BLAS builds
VK_EXT_descriptor_indexing — bindless textures/buffers
-
3.2 Acceleration Structures in Vulkan
- PathTracer Learning - BLAS and TLAS
- BLAS (Bottom-Level Acceleration Structure)
- Contains actual geometry: triangles or AABBs
- Built once per mesh (or rebuilt for skinned meshes)
VkAccelerationStructureGeometryKHR — describes the geometry
VkAccelerationStructureBuildGeometryInfoKHR — build parameters
- Build flags:
PREFER_FAST_TRACE vs PREFER_FAST_BUILD vs ALLOW_UPDATE
- Compaction: reduces memory by 50-70% after build
- TLAS (Top-Level Acceleration Structure)
- Contains instances of BLASes with transform matrices
- Rebuilt every frame (instances move, appear, disappear)
VkAccelerationStructureInstanceKHR:
- PathTracer Learning - Concept - Device Address Bit
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT — required for SBT and AS input buffers
vkGetBufferDeviceAddress() returns a VkDeviceAddress (uint64)
- This is a raw GPU pointer — enables pointer arithmetic in shaders
-
3.3 Shader Binding Table (SBT)
- See PathTracer Learning - Vulkan RT Pipeline for full details
- The SBT maps ray types to shader programs
- Layout:
[RayGen | Miss shaders | Hit groups | Callable shaders]
- Each entry is a shader handle (32 bytes) + optional inline data
- Alignment requirements
shaderGroupHandleSize — typically 32 bytes
shaderGroupBaseAlignment — typically 64 bytes
- Each region must be aligned to
shaderGroupBaseAlignment
- Indexing formula
hitGroupIndex = instanceSBTOffset + geometryIndex * sbtStride + rayContributionToHitGroupIndex
-
3.4 Ray Tracing Shaders (GLSL)
- See PathTracer Learning - Vulkan RT Pipeline for complete shader examples
- Ray generation shader (
.rgen)
- Entry point for each pixel
- Calls
traceRayEXT() to launch rays
- For path tracing: implement the full bounce loop here (not recursively)
- Closest-hit shader (
.rchit)
- Called for the nearest confirmed hit
- Fetch vertex data via buffer device address (
GL_EXT_buffer_reference)
- Evaluate BRDF, trace shadow rays
- Miss shader (
.rmiss)
- Called when ray hits nothing
- Sample environment map
- Any-hit shader (
.rahit)
- Called for every potential hit — use for alpha testing
ignoreIntersectionEXT() to reject transparent pixels
-
3.5 Bindless Resources
- Path tracers need access to many textures and buffers (one per mesh/material)
- Bindless: bind a large array of descriptors, index at runtime
VK_EXT_descriptor_indexing — enables unbounded descriptor arrays
nonuniformEXT — required when index varies per invocation (non-uniform)
- Alternative: push constants or inline data in SBT entries
-
3.6 Iterative Path Tracing on GPU
- Recursive approach: hit shader calls
traceRayEXT → limited by maxPipelineRayRecursionDepth
- Iterative approach (preferred): loop in ray generation shader
- Payload carries all data needed between bounces
-
3.8 Ray Query (Inline RT)
- Alternative to RT pipeline: query rays from any shader stage
VK_KHR_ray_query — no SBT, no separate shader stages
- Useful for: shadow rays in compute shaders, AO, simple occlusion queries
- Less flexible than RT pipeline but simpler to integrate