[PR #690] xdxr: a paired BLAS keeps one 64-byte leaf a pair (RT-XDXR-PAIR-DENSE) #730

Open
opened 2026-11-26 20:18:51 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/690
Author: @kawaii-shakal
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: rt-xdxr-pair-dense


📝 Commits (10+)

  • dcac757 d3d12: serialize and deserialize XDXR acceleration structures (RT-XDXR-SERIALIZE)
  • c779bd3 xdxr: a paired BLAS keeps one 64-byte leaf a pair (RT-XDXR-PAIR-DENSE)
  • a98d676 xdxr: write a pair's leaf when placing it, not in place (RT-XDXR-PAIR-DENSE)
  • acb9bb5 test: the XDXR postbuild readback-stage case takes the postbuild type argument
  • 489d2b2 test: the XDXR postbuild readback-stage case takes the postbuild type argument
  • 78d3d92 Merge upstream XDXR address handling into serialization
  • fb2ea85 Merge updated XDXR serialization into dense pairing
  • 8f7ea15 Size title-placed XDXR scratch for dense pairing
  • b8d18a9 Merge upstream main into XDXR serialization
  • 7e63e1d Merge updated XDXR serialization into dense pairing

📊 Changes

16 files changed (+13084 additions, -8413 deletions)

View changed files

📝 README.md (+1 -1)
📝 docs/bvh.md (+56 -16)
📝 docs/d3d12_vulkan.md (+4 -2)
📝 docs/xdxr.md (+1 -1)
📝 runtime/shims/d3d12_vulkan_device.cpp (+30 -15)
📝 runtime/src/vulkan_backend.h (+11 -8)
📝 runtime/src/vulkan_replay.h (+28 -11)
📝 runtime/src/xdxr_build.comp (+291 -74)
📝 runtime/src/xdxr_build.h (+82 -15)
📝 runtime/src/xdxr_build.inc (+12164 -8248)
📝 runtime/test/d3d12_xdxr_rt_test.cpp (+240 -16)
📝 runtime/test/replay_khr_as_copy_postbuild_test.cpp (+48 -2)
📝 runtime/test/replay_readback_stages_test.cpp (+1 -1)
📝 runtime/test/vk_xdxr_build_test.cpp (+76 -3)
📝 tests/test_xdxr_bvh.py (+28 -0)
📝 tools/xdxr_bvh.py (+23 -0)

📄 Description

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

Depends on: #626 (the same shader; this branch is on top of it, so the first commit here is #626's: the change is d0021a43e)

What and why

TRI1 pairing in the XDXR GPU builder left the sibling's leaf unused in the element region, so a paired BLAS was exactly as large as an unpaired one. README calls this "TRI1 pairing (no memory saved yet)"; docs/bvh.md §7a lists it under "Not done yet". Now a paired build keeps one 64-byte leaf record a pair, as the console's runtime BLASes do (§4a: one Leaf per entry, an entry holding the second primitive). The nodes follow directly. The build, COMPACTED_SIZE and a COMPACT copy are 64 bytes a pair smaller.

How (runtime/src/xdxr_build.comp, .h):

  • With FLAG_PAIR, MODE_TRIANGLES writes the leaves to scratch.
  • MODE_PAIR, split out of MODE_NODES, chooses the pairs once the tree is known (same rule as before). It records each pair and its rotation and marks the sibling's leaf as dropped; it writes no leaf (see the fix below).
  • A tile scan over the leaves that stay (MODE_LEAF_COUNT / SCAN / INDEX, sharing tile_index with the kept-node scan) gives each its place. MODE_LEAF_PLACE copies them densely into the element region, building a pair's leaf from the two staged ones.
  • MODE_NODES and everything after use nodes_off() = elements_off + 64 * kept, also written to the header's offsetToNodes. Pointers and parent table entries are written for the final places, so nothing moves afterwards.
  • numElements stays the triangle count, as tools/xdxr_bvh.py build_blas writes it.
  • The result room is unchanged: a leaf per triangle, since the pairs are not known at prebuild time.
  • The scratch grows by 80 bytes a triangle, only for a bottom-level build without ALLOW_UPDATE. This is XdxrLayout::compute(..., pairing), and record_blas pairs only with such a layout. GetRaytracingAccelerationStructurePrebuildInfo now maps ALLOW_UPDATE and asks for the same layout as the build.
  • The pairing arrays sit between the boxes and the accumulators, because every offset after the accumulators depends on pc.arg0, which is the vertex stride in MODE_TRIANGLES.

Checks:

  • check-build reports any 64-byte unit of a BLAS's element region that no triangle pointer names (unreferenced_leaves). A unit test shows it catches the old layout.

  • vk_xdxr_build_test:

    • builds the paired grid's mesh unpaired too (blas_grid_unpaired) and requires the sizes to differ by exactly 64 bytes per TRI1 child;
    • reads the paired build's COMPACTED_SIZE;
    • COMPACT-copies it (blas_paired_compacted, traced by check-build).
  • d3d12_xdxr_rt_test:

    • gives the paired BLAS its own prebuild scratch, which must be larger than the ALLOW_UPDATE one;
    • checks that the element region holds (triangles − pairs) records.

    It used to reuse the ALLOW_UPDATE build's scratch, which is now too small for a paired build.

Fix after review (1b859ca6d)

On an Apple M5 Pro (MoltenVK 1.4.2) the paired cases failed, and the counts changed between runs (thanks @GeorgiyDemo). MODE_PAIR rewrote the first triangle's staged leaf from vertices it had loaded from that same leaf in the same dispatch. The SPIRV-Cross in MoltenVK 1.4.2 can move such a load after the store, which is the rule at the top of xdxr_build.comp. Now MODE_PAIR only records the pair (FIRST | sibling and k | kd << 2 for the first triangle), and MODE_LEAF_PLACE builds the leaf word by word from the two unchanged staged leaves. Sizes are unchanged; the scratch takes 4 more bytes a triangle. On the RTX 5070 Ti, ctest -R "xdxr|khr_as_copy" passes 7/7 in three runs without and three with LEONIDA_VK_VALIDATION=1. Not re-run on Apple here.

Tested

Windows 11 Pro 26300, MSVC 19.50.35721.0, Vulkan SDK 1.4.363.0, shader regenerated with glslc --target-env=vulkan1.2 -O -mfmt=c (shaderc v2026.4), spirv-val clean.

  • ctest -R "xdxr|khr_as_copy": 7/7 passed, both without and with LEONIDA_VK_VALIDATION=1 (synchronization validation). Every xdxr_build_check and d3d12_xdxr_rt_check scene: 0 validation errors, 0 rays differing from brute force.

    case before now
    blas_paired_grid (288 triangles, 80 pairs) 39,168 bytes 34,048
    blas_paired_sort_tiles (7,200 triangles, 2,208 pairs) 988,160 846,848 (−14%)
    d3d12_blas_paired (428 triangles, 61 pairs) 58,368 54,464
    blas_paired_grid_box16 (162 triangles, 81 pairs) 17,344 12,160
  • Whole ctest -C RelWithDebInfo: 293 run, 292 passed. The one failure is d3d12_execute_indirect_validation, which fails on main too (#337 / #306).

  • python -I tests/test_xdxr_bvh.py (49 tests), python -I tools/xdxr_bvh.py selftest --seed 1 --rays 200, every tests/test_docs_*.py: OK.

  • python -I scripts/check_no_game_data.py on every changed file: clean.

Not measured: build time. A paired build adds five dispatches, and builds that do not pair are unchanged.

GPUs tested: NVIDIA GeForce RTX 5070 Ti (Blackwell, PCI 10de:2c05), driver 617.42, Vulkan 1.4.351, Windows 11 Pro 26300

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase). (on top of #626, which merges cleanly with main)
  • 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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/690 **Author:** [@kawaii-shakal](https://github.com/kawaii-shakal) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt-xdxr-pair-dense` --- ### 📝 Commits (10+) - [`dcac757`](https://github.com/ubiquitarianism/leonida/commit/dcac757ab0606b7facda55357363819f94ebc9ed) d3d12: serialize and deserialize XDXR acceleration structures (RT-XDXR-SERIALIZE) - [`c779bd3`](https://github.com/ubiquitarianism/leonida/commit/c779bd3e2be1937af5db67608767203e168a1af5) xdxr: a paired BLAS keeps one 64-byte leaf a pair (RT-XDXR-PAIR-DENSE) - [`a98d676`](https://github.com/ubiquitarianism/leonida/commit/a98d67626dcb411f6433053afa03bf39e929d490) xdxr: write a pair's leaf when placing it, not in place (RT-XDXR-PAIR-DENSE) - [`acb9bb5`](https://github.com/ubiquitarianism/leonida/commit/acb9bb5e4a217a7b50f99b3d2fae0e7a0964ec61) test: the XDXR postbuild readback-stage case takes the postbuild type argument - [`489d2b2`](https://github.com/ubiquitarianism/leonida/commit/489d2b28587e7a1547ce357c21200f6b4283d8d7) test: the XDXR postbuild readback-stage case takes the postbuild type argument - [`78d3d92`](https://github.com/ubiquitarianism/leonida/commit/78d3d923f7335753af365bb8b7ebf002a046b9a2) Merge upstream XDXR address handling into serialization - [`fb2ea85`](https://github.com/ubiquitarianism/leonida/commit/fb2ea8504060da5d7a904942038bfcf69e5364f2) Merge updated XDXR serialization into dense pairing - [`8f7ea15`](https://github.com/ubiquitarianism/leonida/commit/8f7ea1543814ff89dbc5efebfec2b39435f4dd78) Size title-placed XDXR scratch for dense pairing - [`b8d18a9`](https://github.com/ubiquitarianism/leonida/commit/b8d18a97eff0325de35e254ab583e78f9a7f66b7) Merge upstream main into XDXR serialization - [`7e63e1d`](https://github.com/ubiquitarianism/leonida/commit/7e63e1d60665651b8040531e57a4af774dc4ab03) Merge updated XDXR serialization into dense pairing ### 📊 Changes **16 files changed** (+13084 additions, -8413 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+1 -1) 📝 `docs/bvh.md` (+56 -16) 📝 `docs/d3d12_vulkan.md` (+4 -2) 📝 `docs/xdxr.md` (+1 -1) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+30 -15) 📝 `runtime/src/vulkan_backend.h` (+11 -8) 📝 `runtime/src/vulkan_replay.h` (+28 -11) 📝 `runtime/src/xdxr_build.comp` (+291 -74) 📝 `runtime/src/xdxr_build.h` (+82 -15) 📝 `runtime/src/xdxr_build.inc` (+12164 -8248) 📝 `runtime/test/d3d12_xdxr_rt_test.cpp` (+240 -16) 📝 `runtime/test/replay_khr_as_copy_postbuild_test.cpp` (+48 -2) 📝 `runtime/test/replay_readback_stages_test.cpp` (+1 -1) 📝 `runtime/test/vk_xdxr_build_test.cpp` (+76 -3) 📝 `tests/test_xdxr_bvh.py` (+28 -0) 📝 `tools/xdxr_bvh.py` (+23 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-XDXR-PAIR-DENSE ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7130477975)) Depends on: #626 (the same shader; this branch is on top of it, so the first commit here is #626's: the change is d0021a43e) ### What and why TRI1 pairing in the XDXR GPU builder left the sibling's leaf unused in the element region, so a paired BLAS was exactly as large as an unpaired one. README calls this "TRI1 pairing (no memory saved yet)"; `docs/bvh.md` §7a lists it under "Not done yet". Now a paired build keeps one 64-byte leaf record a pair, as the console's runtime BLASes do (§4a: one `Leaf` per entry, an entry holding the second primitive). The nodes follow directly. The build, `COMPACTED_SIZE` and a `COMPACT` copy are 64 bytes a pair smaller. How (`runtime/src/xdxr_build.comp`, `.h`): - With `FLAG_PAIR`, `MODE_TRIANGLES` writes the leaves to scratch. - `MODE_PAIR`, split out of `MODE_NODES`, chooses the pairs once the tree is known (same rule as before). It records each pair and its rotation and marks the sibling's leaf as dropped; it writes no leaf (see the fix below). - A tile scan over the leaves that stay (`MODE_LEAF_COUNT` / `SCAN` / `INDEX`, sharing `tile_index` with the kept-node scan) gives each its place. `MODE_LEAF_PLACE` copies them densely into the element region, building a pair's leaf from the two staged ones. - `MODE_NODES` and everything after use `nodes_off()` = `elements_off + 64 * kept`, also written to the header's `offsetToNodes`. Pointers and parent table entries are written for the final places, so nothing moves afterwards. - `numElements` stays the triangle count, as `tools/xdxr_bvh.py build_blas` writes it. - The result room is unchanged: a leaf per triangle, since the pairs are not known at prebuild time. - The scratch grows by 80 bytes a triangle, only for a bottom-level build without `ALLOW_UPDATE`. This is `XdxrLayout::compute(..., pairing)`, and `record_blas` pairs only with such a layout. `GetRaytracingAccelerationStructurePrebuildInfo` now maps `ALLOW_UPDATE` and asks for the same layout as the build. - The pairing arrays sit between the boxes and the accumulators, because every offset after the accumulators depends on `pc.arg0`, which is the vertex stride in `MODE_TRIANGLES`. Checks: - `check-build` reports any 64-byte unit of a BLAS's element region that no triangle pointer names (`unreferenced_leaves`). A unit test shows it catches the old layout. - `vk_xdxr_build_test`: - builds the paired grid's mesh unpaired too (`blas_grid_unpaired`) and requires the sizes to differ by exactly 64 bytes per TRI1 child; - reads the paired build's `COMPACTED_SIZE`; - `COMPACT`-copies it (`blas_paired_compacted`, traced by `check-build`). - `d3d12_xdxr_rt_test`: - gives the paired BLAS its own prebuild scratch, which must be larger than the `ALLOW_UPDATE` one; - checks that the element region holds (triangles − pairs) records. It used to reuse the `ALLOW_UPDATE` build's scratch, which is now too small for a paired build. ### Fix after review (1b859ca6d) On an Apple M5 Pro (MoltenVK 1.4.2) the paired cases failed, and the counts changed between runs (thanks @GeorgiyDemo). `MODE_PAIR` rewrote the first triangle's staged leaf from vertices it had loaded from that same leaf in the same dispatch. The SPIRV-Cross in MoltenVK 1.4.2 can move such a load after the store, which is the rule at the top of `xdxr_build.comp`. Now `MODE_PAIR` only records the pair (`FIRST | sibling` and `k | kd << 2` for the first triangle), and `MODE_LEAF_PLACE` builds the leaf word by word from the two unchanged staged leaves. Sizes are unchanged; the scratch takes 4 more bytes a triangle. On the RTX 5070 Ti, `ctest -R "xdxr|khr_as_copy"` passes 7/7 in three runs without and three with `LEONIDA_VK_VALIDATION=1`. Not re-run on Apple here. ### Tested Windows 11 Pro 26300, MSVC 19.50.35721.0, Vulkan SDK 1.4.363.0, shader regenerated with `glslc --target-env=vulkan1.2 -O -mfmt=c` (shaderc v2026.4), `spirv-val` clean. - `ctest -R "xdxr|khr_as_copy"`: 7/7 passed, both without and with `LEONIDA_VK_VALIDATION=1` (synchronization validation). Every `xdxr_build_check` and `d3d12_xdxr_rt_check` scene: 0 validation errors, 0 rays differing from brute force. | case | before | now | |---|---|---| | `blas_paired_grid` (288 triangles, 80 pairs) | 39,168 bytes | 34,048 | | `blas_paired_sort_tiles` (7,200 triangles, 2,208 pairs) | 988,160 | 846,848 (−14%) | | `d3d12_blas_paired` (428 triangles, 61 pairs) | 58,368 | 54,464 | | `blas_paired_grid_box16` (162 triangles, 81 pairs) | 17,344 | 12,160 | - Whole `ctest -C RelWithDebInfo`: 293 run, 292 passed. The one failure is `d3d12_execute_indirect_validation`, which fails on `main` too (#337 / #306). - `python -I tests/test_xdxr_bvh.py` (49 tests), `python -I tools/xdxr_bvh.py selftest --seed 1 --rays 200`, every `tests/test_docs_*.py`: OK. - `python -I scripts/check_no_game_data.py` on every changed file: clean. Not measured: build time. A paired build adds five dispatches, and builds that do not pair are unchanged. GPUs tested: NVIDIA GeForce RTX 5070 Ti (Blackwell, PCI 10de:2c05), driver 617.42, Vulkan 1.4.351, Windows 11 Pro 26300 AI-assisted: yes ### Checklist - [ ] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). (on top of #626, which merges cleanly with `main`) - [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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#730
No description provided.