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

Closed
kawaii-shakal wants to merge 10 commits from upstream-pr/626 into main
kawaii-shakal commented 2026-11-24 10:11:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-24T10:11:01Z.


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 (#337, #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 21625bca8 and with #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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5470151213 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/626). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-24T10:11:01Z. --- Claimed in the coordination issue as: RT-XDXR-SERIALIZE ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7127809054)) ### 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 (#337, #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` 21625bca8 and with #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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
Sangonapou commented 2026-11-24 15:44:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Sangonapou on 2026-11-24T15:44:42Z.


Tested 387cdbf9 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.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136538946 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/626#issuecomment-7136538946). Originally posted by [Sangonapou](https://github.com/Sangonapou) on 2026-11-24T15:44:42Z. --- Tested 387cdbf9 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`.
kawaii-shakal commented 2026-11-24 23:53:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-24T23:53:57Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7147916824 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/626#issuecomment-7147916824). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-24T23:53:57Z. --- 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.
kawaii-shakal commented 2026-11-25 02:31:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-25T02:31:56Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7150843936 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/626#issuecomment-7150843936). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-25T02:31:56Z. --- 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.
kawaii-shakal commented 2026-11-25 08:43:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-25T08:43:10Z.


CI fix: runtime/test/replay_readback_stages_test.cpp (added on main in dfadf4ce5) 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7157384257 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/626#issuecomment-7157384257). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-25T08:43:10Z. --- CI fix: `runtime/test/replay_readback_stages_test.cpp` (added on main in dfadf4ce5) 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).
aslan2142 commented 2026-12-01 17:21:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T17:21:12Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7319162699 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/626#issuecomment-7319162699). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T17:21:12Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!626
No description provided.