Skip to content

Add ComputeEncoder::batchBuildAS() and shared AS-build orchestration helper#1244

Open
MarijnS95 wants to merge 1 commit into
llvm:mainfrom
Traverse-Research:compute-encoder-batch-build-as
Open

Add ComputeEncoder::batchBuildAS() and shared AS-build orchestration helper#1244
MarijnS95 wants to merge 1 commit into
llvm:mainfrom
Traverse-Research:compute-encoder-batch-build-as

Conversation

@MarijnS95
Copy link
Copy Markdown
Collaborator

@MarijnS95 MarijnS95 commented May 28, 2026

Depends on #1232
For #1158

Move acceleration-structure build commands behind the abstract ComputeEncoder interface so the orchestration (data upload, build-request creation, AS allocation, build recording) can live in one place rather than splitting across three backends.

ComputeEncoder gains a single batchBuildAS(ArrayRef<ASBuildItem>) method. Each item carries an AccelerationStructure plus a BLAS or TLAS build request via PointerUnion. The caller guarantees no inter-item memory dependencies inside a batch — backends record the whole batch with one barrier slot, no per-element barriers.

  • Vulkan: single vkCmdBuildAccelerationStructuresKHR call covering the whole batch. TLAS items serialize VkAccelerationStructureInstanceKHR into a device-address upload buffer; BLAS items pull addresses from each VulkanBuffer (new getDeviceAddress accessor). Storage buffers transparently gain SHADER_DEVICE_ADDRESS + ACCEL_BUILD_INPUT_READ_ONLY flags when ray tracing is supported, with the matching VkMemoryAllocateFlagsInfo chained on every allocation.
  • DX12: loop calling BuildRaytracingAccelerationStructure per item with no intermediate barriers; D3D12_RAYTRACING_INSTANCE_DESC is bit-identical to the Vulkan instance struct.
  • Metal: lazy transition to MTL::AccelerationStructureCommandEncoder, deduplicates BLAS handles into the MTL::InstanceAccelerationStructureDescriptor's instancedAccelerationStructures array (Metal references BLASes by index, not GPU address).

A shared helper buildPipelineAccelerationStructures in lib/API/Device.cpp walks Pipeline::AccelStructs, uploads vertex/index data via the new createBufferWithData, builds requests, allocates AS objects, and issues two batchBuildAS calls (BLAS batch then TLAS batch — VUID-03403 forbids referencing a sibling dstAccelerationStructure in one command). Each backend's executeProgram calls this helper to build the pipeline's AS objects.

Descriptor binding for AS resources lands in the next PR — the tests progress past AS-build but still need the descriptor-write step.

@MarijnS95 MarijnS95 force-pushed the compute-encoder-batch-build-as branch from 72e0f02 to 87674fe Compare May 29, 2026 21:02
@MarijnS95 MarijnS95 changed the title Add ComputeEncoder::batchBuildAS and shared AS-build orchestration helper Add ComputeEncoder::batchBuildAS() and shared AS-build orchestration helper Jun 1, 2026
@MarijnS95 MarijnS95 force-pushed the compute-encoder-batch-build-as branch 2 times, most recently from 6a080ef to 566d0ba Compare June 1, 2026 13:16
@MarijnS95 MarijnS95 requested review from EmilioLaiso and manon-traverse and removed request for manon-traverse June 2, 2026 08:40
@MarijnS95 MarijnS95 force-pushed the compute-encoder-batch-build-as branch 3 times, most recently from 8cd0059 to 0089566 Compare June 3, 2026 11:47
@MarijnS95 MarijnS95 marked this pull request as ready for review June 6, 2026 08:09
@MarijnS95 MarijnS95 force-pushed the compute-encoder-batch-build-as branch from 0089566 to 8460666 Compare June 6, 2026 08:09
…helper

Move acceleration-structure build commands behind the abstract
ComputeEncoder interface so the orchestration (data upload, build-request
creation, AS allocation, build recording) can live in one place rather
than splitting across three backends.

ComputeEncoder gains a single batchBuildAS(ArrayRef<ASBuildItem>) method.
Each item carries an AccelerationStructure plus a BLAS or TLAS build
request via PointerUnion. The caller guarantees no inter-item memory
dependencies inside a batch — backends record the whole batch with one
barrier slot, no per-element barriers.

  - Vulkan: single vkCmdBuildAccelerationStructuresKHR call covering the
    whole batch. TLAS items serialize VkAccelerationStructureInstanceKHR
    into a device-address upload buffer, BLAS items pull addresses from
    each VulkanBuffer (new getDeviceAddress accessor). Storage buffers
    transparently gain SHADER_DEVICE_ADDRESS + ACCEL_BUILD_INPUT_READ_ONLY
    flags when ray tracing is supported, with the matching
    VkMemoryAllocateFlagsInfo chained on every allocation.
  - DX12: loop calling BuildRaytracingAccelerationStructure per item with
    no intermediate barriers; D3D12_RAYTRACING_INSTANCE_DESC is
    bit-identical to the Vulkan instance struct.
  - Metal: lazy transition to MTL::AccelerationStructureCommandEncoder,
    deduplicates BLAS handles into the
    MTL::InstanceAccelerationStructureDescriptor's instancedAccelera-
    tionStructures array (Metal references BLASes by index, not GPU
    address).

Each backend's CommandBuffer now carries a back-pointer to its owning
Device so the encoder can reach device-loaded entry points and helpers,
plus a keep-alive list for AS scratch and instance buffers.

A shared helper buildPipelineAccelerationStructures in lib/API/Device.cpp
walks Pipeline::AccelStructs, uploads vertex/index data via the new
createBufferWithData, builds requests, allocates AS objects, and issues
two batchBuildAS calls (BLAS batch then TLAS batch — VUID-03403 forbids
referencing a sibling dstAccelerationStructure in one command). Each
backend's executeProgram calls this helper to build the pipeline's AS
objects.

Descriptor binding for AS resources is intentionally still missing — the
tests progress past AS-build now and surface only the descriptor-write
gap.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@MarijnS95 MarijnS95 force-pushed the compute-encoder-batch-build-as branch from 8460666 to 601f893 Compare June 6, 2026 08:17
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.

1 participant