Skip to content

d3d12: serialize and deserialize XDXR acceleration structures (RT-XDXR-SERIALIZE) - #626

Open
kawaii-shakal wants to merge 5 commits into
ProjectLeonida:mainfrom
kawaii-shakal:rt-xdxr-serialize
Open

kawaii-shakal wants to merge 5 commits into
ProjectLeonida:mainfrom
kawaii-shakal:rt-xdxr-serialize

Conversation

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-XDXR-SERIALIZE (claim)

What and why

The README's "Ray tracing (BVH)" row and docs/bvh.md listed serialization as the builder's gap ("Not done yet: serialization"): on main, CopyRaytracingAccelerationStructure with SERIALIZE / DESERIALIZE and the SERIALIZATION postbuild info were refused at replay as unsupported for XDXR structures. This implements them.

  • runtime/src/xdxr_build.comp: MODE_SERIALIZE, MODE_DESERIALIZE, and MODE_POSTBUILD answering SERIALIZATION (16 bytes) next to the two sizes. The serialized form is:

    • D3D12's 56-byte D3D12_SERIALIZED_RAYTRACING_ACCELERATION_STRUCTURE_HEADER: the layer's own driver matching identifier (xdxr_build.h xdxr_serialized_id; GUID = ASCII Leonida XDXR AS , versioning data = header version 6 and serialized layout version 1), the serialized size, the deserialized size (the structure's own) and the pointer count;
    • one 8-byte BLAS address per INSTANCE record of a TLAS, in record order, read from the record (+60, 256-byte units);
    • then the structure's bytes as they are (the records are position-independent).

    DESERIALIZE copies the bytes back and writes each INSTANCE record's BLAS address from the pointer list, so BLASes an application moved and re-pointed are the ones the TLAS names.

  • runtime/src/xdxr_build.inc is regenerated with the command in xdxr_build_spv.h (glslc --target-env=vulkan1.2 -O -mfmt=c, shaderc v2026.4). That glslc reproduces the committed include for main's shader byte for byte, so the large diff is only the ID renumbering of this change; spirv-val --target-env vulkan1.2 accepts the module.

  • Replay (vulkan_replay.h, vulkan_backend.h): passes the copy mode and postbuild type through, steps SERIALIZATION descs 16 bytes apart, and bounds SERIALIZE / DESERIALIZE by the destination only, since a serialized structure is larger than the structure (bounding by the source too truncated a TLAS whose buffer is exactly its size). VISUALIZATION_DECODE_FOR_TOOLS and TOOLS_VISUALIZATION stay refused.

  • CheckDriverMatchingIdentifier (was a stub answering UNRECOGNIZED): COMPATIBLE_WITH_DEVICE for the layer's identifier, INCOMPATIBLE_VERSION for the same GUID with other versioning data, UNRECOGNIZED otherwise and always with LEONIDA_RT_AS=khr (the KHR path still refuses serialization; out of scope).

  • Docs: docs/bvh.md gets a Serialization paragraph; docs/d3d12_vulkan.md, docs/xdxr.md and the README row are updated.

Not measured, and said so in docs/bvh.md: the console's own serialized payload. XboxBvhBuilder.dll writes SERIALIZATION postbuild info in a handler of its own (docs/xdxr.md section 4); this layer's payload after the pointers is its own, and a blob carrying the console's identifier would be UNRECOGNIZED. Whether the game serializes at all is still open, like the rehydrate-or-rebuild question the README row ends with.

Tested

Windows 11 Pro 26300, MSVC 14.50 (VS 2026 Build Tools), Vulkan SDK 1.4.363.0, cmake --build runtime/build --config RelWithDebInfo.

  • d3d12_xdxr_rt now serializes the test's BLAS and three-instance TLAS, checks the SERIALIZATION postbuild info (56 + size and 56 + 3*8 + size), the headers, the identifier through CheckDriverMatchingIdentifier (and the two negative answers), the TLAS's three pointers and the payload bytes; deserializes the BLAS to another address, rewrites the TLAS's serialized pointers to it with CopyBufferRegion (state transitions UNORDERED_ACCESS → COPY_SOURCE → COPY_DEST → NON_PIXEL_SHADER_RESOURCE), deserializes the TLAS, and requires both copies to equal the originals byte for byte except the three instance BLAS addresses. d3d12_xdxr_rt_check then traces rays through the dumped copies against brute force:
    • d3d12_blas_deserialized: 0/1000 rays differ (711 hits); d3d12_tlas_deserialized: 0/1000 rays differ (776 hits).
  • Negative control: with MODE_DESERIALIZE ignoring the pointer list, d3d12_xdxr_rt fails with "deserialized TLAS: an instance's BLAS address is not the moved BLAS's".
  • replay_khr_as_copy_postbuild covers the replay contract: modes 3/4 reach xdxr_copy with their mode and destination-only bound, mode 2 is unsupported, SERIALIZATION descs land 16 bytes apart, a 24-byte destination for two of them is invalid_copy, TOOLS_VISUALIZATION is unsupported.
  • All 11 ray-tracing CTests (xdxr|dxr|khr_as|indirect_rt) pass on both GPUs below (bvh_builder skips, as on main).
  • Whole runtime suite (ctest -C RelWithDebInfo, RTX 5070 Ti): 292/293 pass. The one failure, d3d12_execute_indirect_validation (5 of 49 parity cases), fails identically on main with this change stashed; it is the known argument-range parity issue (d3d12: ExecuteIndirect accepts the argument ranges newer D3D12 runtimes accept (D3D-EXECIND-RANGE) #337, d3d12_execute_indirect_validation_test: handle Windows 11 26300 relaxed max=0 and padded-stride bounds #306).
  • python -I scripts/check_no_game_data.py on every changed file, tests/test_text_write_line_endings.py: clean.
  • git merge-tree with main 21625bc and with XDXR-BUILDER-PREVALIDATE: pre-validate XdxrBuilder inputs and avoid 32-bit workgroup overflow #485 (which also touches xdxr_build.h): no conflicts.

GPUs tested: NVIDIA GeForce RTX 5070 Ti (Blackwell, driver 617.42, Vulkan 1.4.351); AMD Radeon 610M (Ryzen 9 7900X iGPU, AMD proprietary driver 2026.04.17, Vulkan 1.4.315, selected with LEONIDA_VK_DEVICE=radeon); Windows 11.

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 387cdbf on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

  • d3d12_xdxr_rt: passed, serialized BLAS 58424 bytes, TLAS 720 bytes (3 BLAS pointers); deserialized with the TLAS's BLAS moved.
  • replay_khr_as_copy_postbuild: passed, KHR AS copy (CLONE/COMPACT) and COMPACTED_SIZE postbuild OK.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 24, 2026
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

Rebased onto main 364ab4eab as f51ef5c85. There was one conflict, in runtime/src/vulkan_replay.h: main now marks the postbuild destination as a compute-written readback. I kept that and kept this PR's room < need bound, which covers the serialized-size strides. xdxr_build.inc matches glslc output for this .comp. On an RTX 5070 Ti (driver 617.42, Windows 11 26300), all 8 ctest -R xdxr tests pass at the #690 head, which contains this commit. tests/test_xdxr_bvh.py passes 56 of 56.

AI-assisted.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
…R-SERIALIZE)

CopyRaytracingAccelerationStructure with SERIALIZE / DESERIALIZE and the
SERIALIZATION postbuild info were refused at replay, the gap the README's
"Ray tracing (BVH)" row and docs/bvh.md listed for the XDXR builder.

xdxr_build.comp gains MODE_SERIALIZE and MODE_DESERIALIZE, and
MODE_POSTBUILD answers SERIALIZATION (16 bytes) next to the two sizes.
The serialized form is D3D12's 56-byte header (the layer's own driver
matching identifier, xdxr_build.h xdxr_serialized_id; the serialized size;
the deserialized size, which is the structure's; the pointer count), one
8-byte BLAS address per INSTANCE record of a TLAS read from the record
(+60, 256-byte units), then the structure's bytes. DESERIALIZE copies the
bytes back and writes each instance's BLAS address from the pointer list,
so BLASes the application moved and re-pointed are the ones the TLAS
names. xdxr_build.inc is regenerated with the command xdxr_build_spv.h
gives (glslc from shaderc v2026.4, which reproduces the committed include
for main's shader byte for byte).

The replay passes the copy mode and postbuild type through, steps
SERIALIZATION descs 16 bytes apart, and bounds SERIALIZE / DESERIALIZE by
the destination only (a serialized structure is larger than the
structure). CheckDriverMatchingIdentifier recognises the identifier.

d3d12_xdxr_rt_test serializes a BLAS and a three-instance TLAS, checks the
headers, pointers and bytes, deserializes the BLAS elsewhere, rewrites the
TLAS's pointers to it, deserializes the TLAS and dumps both for the ray
check; replay_khr_as_copy_postbuild_test covers the replay contract.

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

Copy link
Copy Markdown
Contributor Author

Rebased again, onto main 3ff2a2adc, as dcac757ab. Main now returns unsupported_at(__LINE__) / unresolved_at(__LINE__), so the XDXR copy and postbuild branches of runtime/src/vulkan_replay.h use those helpers, with this PR's mode and type checks and its serialized-size bound. In README the D3D12 row is main's (with the new console-ABI link) and the ray-tracing row is this PR's. xdxr_build.inc matches glslc. On an RTX 5070 Ti (617.42), ctest -R 'xdxr|khr_as_copy' passes 9 of 9 at the #690 head on top of this.

AI-assisted.

… argument

replay_readback_stages_test (from main's VK-XDXR-POSTBUILD-READBACK-STAGE)
assigned a three-argument lambda to xdxr_postbuild, which this branch gives
a fourth (the postbuild type), so the runtime tests did not build.

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

Copy link
Copy Markdown
Contributor Author

CI fix: runtime/test/replay_readback_stages_test.cpp (added on main in dfadf4c) assigned a three-argument lambda to xdxr_postbuild, which this branch gives a fourth (the postbuild type), so windows and linux-gpu-tools failed at Build. That case now takes the extra argument (acb9bb5e4). replay_readback_stages_test and replay_khr_as_copy_postbuild_test build and pass on Windows 11 (MSVC 19.50).

Codex integrated title-placement bounds with serialization, regenerated the synthetic builder shader, and added a portable round-trip check.
codex and others added 2 commits November 25, 2026 21:56
Conflicts with ProjectLeonida#2222 (RT-XDXR-UPDATE-SOURCE-CHECK), resolved:
- Shader modes: this branch uses 19 and 20 (serialize, deserialize), so
  main's MODE_CHECK = 19 becomes 26, in xdxr_build.comp and xdxr_build.h.
  21-25 stay free for the pair modes of ProjectLeonida#690, which builds on this branch, so
  the two branches number the modes alike. Both sides' code is kept.
- SCRATCH_REFIT moves from byte 28 to byte 32, as in ProjectLeonida#690, where byte 28 is
  the kept leaf count. Nothing here uses either byte.
- xdxr_build.inc regenerated with glslc (shaderc 2023.8, --target-env=vulkan1.2 -O -mfmt=c);
  spirv-val passes.

Tested: full ctest, 113 of 113, on Mesa lavapipe (llvmpipe, LLVM 19.1.7).

Changes made by Claude Code (AI-assisted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
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