Skip to content

D3D12: BuildRaytracingAccelerationStructure writes XDXR BVH4 records (D3D-178) - #1667

Closed
romainveurrier wants to merge 5 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xdxr-wiring
Closed

romainveurrier wants to merge 5 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xdxr-wiring

Conversation

@romainveurrier

@romainveurrier romainveurrier commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Item: D3D-178 (#313), PR 4: the wiring. Draft until the maintainer decides the question in #1548's "Decisions for the maintainer" (question). This PR implements the proposal there, XDXR by default with KHR opt-in. If another option is chosen, only the default in create_vulkan_device_standalone changes.

Depends on: #1572 (the GPU builder). This branch contains #1572's commits; this PR's own change is the last commit, f6c1bb9. Rebased with #1572 onto main at a0acd33 and retested (D3D12 RT tests on both GPUs and with LEONIDA_RT_AS=khr).

What

The game's translated RT shaders walk the console's XDXR BVH4 records themselves (docs/d3d12_vulkan.md §4.8), so a VkAccelerationStructureKHR is invisible to them. With this PR the standalone Vulkan device builds those records:

  • Mode: VulkanDeviceInfo::xdxr_acceleration_structures. The standalone device sets it when bufferDeviceAddress is available, and then leaves the KHR extensions off. So acceleration-structure buffers keep their console VAs (GetGPUVirtualAddress = arena offset), which is how the records name each other. LEONIDA_RT_AS=khr restores feat(d3d12): add ExecuteIndirect CountBuffer and optional BLAS/TLAS/RayQueryKHR support #743's behaviour exactly. Devices made by callers with their own VulkanDeviceInfo (like d3d12_indirect_rt_test) default to the old path.
  • Replay: VulkanContext owns an XdxrBuilder.
    • While a list is prepared, the replay refuses inputs the builder can't read: vertex formats other than R32G32B32_FLOAT or R32G32_FLOAT, index types other than none/R16/R32, and missing addresses.
    • It records the build with heap_base added to every VA. heap_base is also the bias for the BLAS addresses in instance descs.
    • The builder binds its own pipeline and push constants. Replay binds both per dispatch and draw and uses no descriptor sets, so the state around a build is not disturbed.
  • GetRaytracingAccelerationStructurePrebuildInfo: answered from XdxrLayout, exactly. An update rebuilds the structure, so it needs the same scratch.
  • Shim: the geometry's vertex format is now mapped per DXGI format. Previously every format except R32G32 became R32G32B32; an unknown one is now refused rather than misread. Its D3D12 geometry flags now reach the build.
  • tools/xdxr_bvh.py check-build: compares TLAS hits by (geometry, primitive). The old comparison was wrong for multi-geometry BLASes, which Runtime: build XDXR BVH4 acceleration structures on the GPU #1572's cases didn't have. It also skips (77) when the build step wrote no cases.

Not changed: the reported RaytracingTier stays NOT_SUPPORTED; reporting a tier is a separate question. Not implemented: compaction, copies, serialization, and the Xbox batched/streaming builds (XDXR_STREAM_DESC).

Tests

runtime/test/d3d12_xdxr_rt_test.cpp, through the D3D12 API on the standalone device:

  • a BLAS of two geometries: an unindexed soup of 300 triangles, and an R16-indexed 8×8 grid with a 16-byte stride and a 3x4 transform;
  • a TLAS of three instances: identity, rotated and translated, and masked out.

Sizes come from the prebuild info. The results are copied to readback buffers and dumped at their console VAs, then check-build validates them and traces 1,000 rays against brute force (ctest d3d12_xdxr_rt + d3d12_xdxr_rt_check).

Tested

Fedora 44, clang-cl 22.1.8 + xwin (SDK 10.0.26100), Wine 11.21:

  • d3d12_xdxr_rt + check pass on the RTX 2050 (NVIDIA 620.70.06) and the Iris Xe (Mesa 26.2.7 ANV): 0/1,000 rays differ for the BLAS and the TLAS.
  • LEONIDA_RT_AS=khr: d3d12_xdxr_rt and its check skip, and d3d12_indirect_rt (feat(d3d12): add ExecuteIndirect CountBuffer and optional BLAS/TLAS/RayQueryKHR support #743's BLAS + TLAS + RayQueryKHR) passes. In the default mode all three pass.
  • Full Wine suite: 84/89; the same 5 fail on main (fsr_dispatch and bvh_builder are the known clang 22.1.8 crash; device_proxy, d3d12_present and d3d12_stencil_copy compare against Wine's D3D12). Native Linux ctest: 26/26. python -I tests/test_xdxr_bvh.py: OK.
  • Not run: MSVC, a Windows driver, an AMD GPU, validation layers.

No game files. AI-assisted (Claude Code).

🤖 Generated with Claude Code

romainveurrier and others added 4 commits November 22, 2026 14:11
The game's translated ray-tracing shaders read the console's BVH records
themselves (docs/d3d12_vulkan.md 4.8), so the layer has to write them. This
adds the GPU builder, checked against tools/xdxr_bvh.py:

- src/xdxr_build.comp: one pipeline, one dispatch per step. Triangle leaves
  (any of R32G32B32/R32G32 vertices, none/R16/R32 indices, a stride, an
  optional 3x4 transform) or 128-byte INSTANCE records with world-to-object
  inverses; Morton keys and a bitonic sort; BOX32 nodes four consecutive
  children at a time, level by level, with the parent table; the header.
  Buffer device addresses as uvec2: needs bufferDeviceAddress, not
  shaderInt64. NaN triangles, mask-0 instances, instances without a BLAS and
  singular transforms get empty boxes.
- src/xdxr_build.h: XdxrLayout (exact result and scratch sizes from the
  element count, for the prebuild info too) and XdxrBuilder (records the
  dispatches). A bias maps the application's 40-bit BLAS addresses to device
  addresses.
- test/vk_xdxr_build_test.cpp: nine synthetic cases built on the GPU and
  dumped; ctest `xdxr_build` + `xdxr_build_check`, which runs
  `tools/xdxr_bvh.py check-build`: validate, then trace 1000 rays per case
  against brute force.
- vk_test_device.h: opt-in bufferDeviceAddress (request_buffer_device_address,
  device_address()).
- tools/xdxr_bvh.py: check-build; trace takes the bias; instances without a
  BLAS and inactive triangles are valid.

Not wired to BuildRaytracingAccelerationStructure yet: that path builds a
VkAccelerationStructureKHR today (ItsNotRexed#743), and which one the layer uses is a
maintainer decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`vk_xdxr_build_test <dir> <triangles>` times BLAS builds of random triangles
with the result and scratch in device-local memory; vk_test_device.h's
buffer() gives a device-local buffer when `mapped` is null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nic sort

The bitonic sort padded the keys to a power of two and needed O(log^2 n)
dispatches, so one triangle past a power of two doubled its work (2.1 ms ->
5.2 ms at 262,144 -> 262,145 triangles on an RTX 3060, reported on ItsNotRexed#1572).
It is now a stable LSD radix sort: 8 passes of 4-bit digits over the 30-bit
Morton keys, each a count per 2048-element tile, one exclusive scan and a
scatter; 24 dispatches whatever the count, shared memory only.

- vk_xdxr_build_test: a 5000-triangle case (three sort tiles, the last
  partial); stale cases from an earlier run are removed first, so a failed
  build cannot be checked against old files.
- tools/xdxr_bvh.py: validate reports a leaf reached twice; a case may ask
  check-build for fewer rays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The game's translated RT shaders read the console's XDXR records, which a
VkAccelerationStructureKHR is not (docs/d3d12_vulkan.md 4.8). The standalone
Vulkan device now builds them with src/xdxr_build.h, unless LEONIDA_RT_AS=khr
keeps ItsNotRexed#743's KHR builds (the proposal in the maintainer decisions of ItsNotRexed#1548).

- VulkanDeviceInfo::xdxr_acceleration_structures; the standalone device sets
  it with bufferDeviceAddress and then leaves the KHR extensions off, so
  acceleration-structure buffers keep their console VAs.
- VulkanContext owns an XdxrBuilder; replay checks the inputs while the list
  is prepared (R32G32B32/R32G32 vertices, none/R16/R32 indices, addresses,
  counts) and records the build with heap_base added to every VA and as the
  bias for instance BLAS addresses.
- GetRaytracingAccelerationStructurePrebuildInfo answers from XdxrLayout,
  exactly; an update rebuilds, so it needs the same scratch.
- The geometry's vertex format is mapped per DXGI format (an unknown one is
  refused instead of read as R32G32B32), and its D3D12 flags reach the build.
- test/d3d12_xdxr_rt_test.cpp: a two-geometry BLAS (a soup; an R16-indexed
  grid with stride 16 and a transform) and a TLAS of three instances through
  the D3D12 API; ctest d3d12_xdxr_rt + d3d12_xdxr_rt_check.
- tools/xdxr_bvh.py: check-build compares TLAS hits by (geometry, primitive),
  and skips (77) when the build step wrote no cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zneel

zneel commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 1c58d6cf merged onto current main a0acd33d (clean merge) on an AMD Radeon RX 9070 XT (RDNA4), AMD 26.11.2, Windows 11 Pro 10.0.26300, MSVC 14.51, Vulkan SDK 1.4.363.0: pass. This is the first AMD run of the builder and the first run of the D3D12 wiring outside your machine.

  • d3d12_xdxr_rt: BLAS 48,768 bytes, the same as the prebuild size, and TLAS within its 640-byte prebuild. check-build --rays 10000: 0 validation errors and 0/10,000 rays differ for both, with 6,636 BLAS hits and 7,176 TLAS hits.

  • xdxr_build (Runtime: build XDXR BVH4 acceleration structures on the GPU #1572's cases, built by the same code here): every case passes at --rays 10000, with 0 validation errors and 0 rays differing. This covers the TLAS (5,551 hits), the R16 grid with stride 16, R32 indices with R32G32 vertices, the transform, the two geometries and the empty structure. blas_three_sort_tiles passes at its 200-ray cap (146 hits). tests/test_xdxr_bvh.py passes 27/27.

  • Validation: I ran both tests under LEONIDA_VK_VALIDATION=1 with synchronization validation on (the startup report lists "Synchronization") and info/warn reporting on. Both gave 0 VUIDs, 0 sync hazards and 0 warnings, and their output still checks clean at 10,000 rays.

  • LEONIDA_RT_AS=khr: d3d12_xdxr_rt and its check skip with 77, and d3d12_indirect_rt passes.

  • Full ctest: 100/100. The 4 that need a game package skip (bink_decode, bink_audio, fsr_dispatch, bvh_builder).

  • Build time (vk_xdxr_build_test <dir> <n>, validation off): median of three runs, each the harness's best of 3, with the range in parentheses. The two sizes at 524,288 had a noisy first set, so they are the median of ten runs each; 2.52 ms at 524,288 was a single outlier. There is no power-of-two jump at either edge.

    Triangles ms
    10,000 1.06 (1.01–1.10)
    100,000 1.26 (1.25–1.32)
    262,144 1.45 (1.45–1.54)
    262,145 1.47 (1.46–1.51)
    524,288 1.99 (1.85–2.52)
    524,289 1.94 (1.90–2.15)
    1,000,000 3.14 (3.07–3.32)
    2,000,000 5.51 (5.47–6.04)

One nit from MSVC /W4: runtime/src/vulkan_replay.h now has two else if (const auto* build = std::get_if<BuildAccelerationStructure>(&command)) in the same chain. The second one hides the first and raises warning C4456 in every file that includes the header. Renaming one of them, for example to xdxr_build, removes it.

AI-assisted (Claude Code). No game files.

…R one

Two `const auto* build` in one else-if chain: the second hides the first
and MSVC /W4 warns (C4456) in every file that includes the header
(reported by @zneel on ItsNotRexed#1667).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@romainveurrier

Copy link
Copy Markdown
Contributor Author

Thanks @zneel. This is the first AMD and first non-local run of the wiring, and with synchronization validation clean too. Fixed the C4456 nit in 857bca9: the XDXR branch's binding is now xdxr_build, so it no longer hides the KHR branch's build. clang-cl doesn't raise that warning, which is why I missed it. The D3D12 RT tests and #1572's cases still pass under Wine on the RTX 2050.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): pass. ctest: 93/93 on the RTX 4080 SUPER. The Intel UHD 770 failures are the known ones, all reported in #1548: d3d12_vulkan_device (line 3768), d3d12_descriptor (min-LOD), d3d12_present, d3d12_copy_xfmt.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relecture du delta 857bca9 : la variable XDXR n’ombre plus build; le point C4456 signalé est réglé. Je ne vois pas de blocage dans ce delta. L’intégration reste en draft et GitHub la marque en conflit avec main (89b047c), donc ces deux points restent à traiter avant la revue finale.

@romainveurrier

Copy link
Copy Markdown
Contributor Author

Merged into main in 1b1d090 (merge round at 89b047c, #1006), from head 1c58d6c. The C4456 fix @zneel reported (857bca9) came after, so it is in #1743 together with the small-build sort. Closing.

ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
…R one

Two `const auto* build` in one else-if chain: the second hides the first
and MSVC /W4 warns (C4456) in every file that includes the header
(reported by @zneel on #1667).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wildheathen added a commit to wildheathen/Leonida that referenced this pull request Nov 22, 2026
…or do now

Three corrections from @romainveurrier's review (the D3D-178 owner):

- the translator reads all four triangle node types, TRI0-TRI3, with the barycentric remap byte
  (ItsNotRexed#1525), while the GPU builder writes TRI0 leaves; the row said "TRI0/TRI1 leaves" for both;
- the XDXR-vs-KHR default is decided: the layer builds XDXR records by default and
  `LEONIDA_RT_AS=khr` keeps ItsNotRexed#743's path (ItsNotRexed#1667), so only the rehydrate-or-rebuild question is
  still open;
- the row now says that `PERFORM_UPDATE` refits instead of rebuilding (ItsNotRexed#1789), and that clone,
  compaction and the compacted/current postbuild sizes work (ItsNotRexed#1844), checked against the CPU
  reference on synthetic scenes.

The measured parts of the row (the recovered build descriptions, the packs, the 8 of 10 sampled
BLAS) are unchanged. Docs only: the documentation suites pass, with the same three duplicated-row
failures the parent commit has.
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