Commit 31c01cf
[Metal] Add ray tracing pipeline, SBT, and DispatchRays bring-up
DXR-style ray tracing reaches Metal through metal_irconverter: each RT
entry point is lowered from DXIL to a Metal IR function, raygen is
emitted as a kernel (IRRayGenerationCompilationKernel) so it can be
dispatched directly, and miss / closest-hit / any-hit / intersection /
callable functions are emitted as visible functions and pulled into a
MTLVisibleFunctionTable.
Implements the three virtuals the foundation PR left stubbed on Metal:
• MTLDevice::createPipelineRT compiles every Shaders[] entry against a
single IRRayTracingPipelineConfiguration (max attribute/recursion
from the YAML RTConfig), builds one MTL::Library per entry, hands
the raygen function to the compute pipeline as the kernel, and
registers the rest as LinkedFunctions. The freshly-built pipeline
then mints a MTLVisibleFunctionTable and resolves each callable
function's handle into a slot index that the SBT builder reuses.
• MTLDevice::createShaderBindingTable lays the four SBT regions out
via the shared computeSBTLayout helper sized for IRShaderIdentifier
records, looks up each region entry's ShaderName in the pipeline's
name → IRShaderIdentifier map, and memcpys the records into a
shared-storage MTL::Buffer the runtime will dereference at dispatch.
• MTLComputeEncoder::dispatchRays binds the raygen pipeline and runs
dispatchThreads(Width, Height, Depth) on the encoder. The caller
(createRayTracingCommands) is responsible for binding the global
descriptor heap, top-level argument buffer, IRDispatchRaysArgument
(slot 3), and marking the SBT buffer + function tables resident.
The IRDispatchRaysArgument struct is built per-dispatch in
createRayTracingCommands: SBT region addresses + sizes (read off the
MTLShaderBindingTable), GRS / ResDescHeap GPU pointers, and the
visible / intersection function table resourceIDs. It's parked in a
shared MTL::Buffer kept alive on the command buffer's KeepAlive list
and bound at kIRRayDispatchArgumentsBindPoint so callees reached via
TraceRay() inherit the same dispatch state through that pointer.
Plumbs the existing executeProgram RT branch on Metal the same way the
VK / DX backends already do (validate Shaders / SBT / RTConfig, build
RayTracingPipelineCreateDesc from the YAML pipeline, create PSO, build
SBT, record commands), and adds the raytracing-pipeline lit feature
on Metal so test/Feature/RT/raygen-roundtrip.test drops Metal from its
XFAIL list and passes natively on Apple Silicon (the 0xBEEF payload
roundtrip matches the DX / VK references, verified locally on
macOS 15 / metal-irconverter 3.1.1).
This PR1 bring-up only handles Triangle hit groups whose only member
is a ClosestHit shader — any-hit / intersection / procedural / local
root signatures land in follow-ups; createPipelineRT now returns a
clear unsupported error for those shapes instead of silently producing
wrong output.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 8a38ed2 commit 31c01cf
5 files changed
Lines changed: 610 additions & 16 deletions
File tree
- lib/API/MTL
- test
- Feature/RT
0 commit comments