Skip to content

[Metal] Make dispatchRays a self-contained encoder operation#1346

Open
MarijnS95 wants to merge 1 commit into
llvm:mainfrom
Traverse-Research:mtl-dispatchrays-upstream
Open

[Metal] Make dispatchRays a self-contained encoder operation#1346
MarijnS95 wants to merge 1 commit into
llvm:mainfrom
Traverse-Research:mtl-dispatchrays-upstream

Conversation

@MarijnS95

@MarijnS95 MarijnS95 commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

The DX and Vulkan backends record a ray dispatch by creating an encoder and calling Encoder.dispatchRays() directly. Metal instead routed through createRayTracingCommands, an MTLDevice method that bound the descriptor heap and top-level argument buffer, synthesized and bound the IRDispatchRaysArgument, marked the RT resources resident, and only then called a thin dispatchRays().

Since Metal lowers raygen to a compute kernel, ray tracing now records through the shared createComputeCommands path — which already binds the heap and argument buffer and marks the common resources resident — by branching on P.isRayTracing(). The IRDispatchRaysArgument construction, upload, binding, and RT-specific residency move into MTLComputeEncoder::dispatchRays, defined out-of-line so it can allocate via CB->Dev and keep the buffer alive in CB->KeepAliveOwned. IRDispatchRaysArgument::ResDescHeap needs the resource heap address, which isn't in the cross-backend dispatchRays signature, so the encoder remembers the bound heap via bindResourceHeap(). createRayTracingCommands is removed.

No behavioral change. check-hlsl-mtl-feature-rt and check-hlsl-mtl-basic pass.

Comment thread lib/API/MTL/MTLDevice.cpp
Comment thread lib/API/MTL/MTLDevice.cpp Outdated
Comment thread lib/API/MTL/MTLDevice.cpp Outdated
Comment thread lib/API/MTL/MTLDevice.cpp Outdated
manon-traverse

This comment was marked as resolved.

@MarijnS95

This comment was marked as resolved.

@MarijnS95 MarijnS95 force-pushed the mtl-dispatchrays-upstream branch from 176f60b to 9d708d8 Compare June 30, 2026 14:11
@MarijnS95

This comment was marked as resolved.

The DX and Vulkan backends record a ray dispatch by creating an encoder and
calling Encoder.dispatchRays() directly. Metal instead went through
createRayTracingCommands, a device method that bound the descriptor heap and
argument buffer, built and bound the IRDispatchRaysArgument, marked the RT
resources resident, and only then called a thin dispatchRays().

Since Metal lowers raygen to a compute kernel, ray tracing now records through
the shared createComputeCommands path (which already binds the heap and
argument buffer and marks the common resources resident) by branching on
P.isRayTracing(). The IRDispatchRaysArgument setup and RT-specific residency
move into MTLComputeEncoder::dispatchRays, defined out-of-line so it can
allocate via CB->Dev and keep the buffer alive in CB->KeepAliveOwned. The heap
address for ResDescHeap, absent from the cross-backend signature, is remembered
on the encoder via bindResourceHeap(). createRayTracingCommands is removed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MarijnS95 MarijnS95 force-pushed the mtl-dispatchrays-upstream branch from 9d708d8 to f3899bd Compare June 30, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants