Skip to content

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

Open
kawaii-shakal wants to merge 11 commits into
ProjectLeonida:mainfrom
kawaii-shakal:rt-xdxr-pair-dense
Open

kawaii-shakal wants to merge 11 commits into
ProjectLeonida:mainfrom
kawaii-shakal:rt-xdxr-pair-dense

Conversation

@kawaii-shakal

@kawaii-shakal kawaii-shakal commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 d0021a4)

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 (1b859ca)

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 (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 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 d3d12: serialize and deserialize XDXR acceleration structures (RT-XDXR-SERIALIZE) #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

@abnalstlll-byte abnalstlll-byte 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.

Reviewed and verified RT-XDXR-PAIR-DENSE. Reclaiming unused 64-byte leaf units during TRI1 pairing in the GPU BVH builder produces densely packed element regions with zero unreferenced leaf gaps, reducing BLAS memory footprints. Cleanly validated by unreferenced_leaves in ools/xdxr_bvh.py and ests/test_xdxr_bvh.py. Approved.

@GeorgiyDemo

Copy link
Copy Markdown
Contributor

Tested d0021a43e on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), MoltenVK 1.4.2 (subgroup 4..32). ctest -R xdxr on a build of this head: xdxr_build_check fails; on main d42773d07 the same two tests pass (4/4 with xdxr_build/xdxr_build_output/xdxr_descs), and on the parent commit 387cdbf94 (#626 alone, built separately here) xdxr_build_check passes with blas_paired_grid 80 TRI1 / 0 validation errors / 0 of 1000 rays differing and blas_paired_rotations 128 TRI1 / 0 / 0. So the failure is in this pairing change, not in its base.

Three runs of xdxr_build_check at this head, same binary:

case run 1 run 2 run 3
blas_paired_compacted 7 errors, 5/1000 rays differ 7, 5/1000 9, 14/1000
blas_paired_grid 7, 2/1000 7, 2/1000 9, 8/1000
blas_paired_grid_box16 7, 47/1000 7, 47/1000 4, 17/1000
blas_paired_rotations 7, 22/1000 8, 19/1000 10, 39/1000
blas_paired_sort_tiles 3, 0/200 3, 0/200 3, 0/200

The messages are child box N of 0x… is not inside its parent's box and built with pairing, but no TRI1 node is reachable, followed by N of 1000 rays differ from brute force (the test reported 36 problems in total). blas_paired_mixed (60 TRI1) and blas_paired_transform (20 TRI1) pass; blas_paired_sort_tiles loses its TRI1 nodes entirely.

The counts vary between identical runs on the same binary, which looks like a race in the pairing step rather than a fixed layout error, and it may be specific to this driver — I have no Windows or AMD GPU here to compare. Worth checking the ordering of the sibling/TRI1 publication in xdxr_build.comp.

Everything else on this head passed: xdxr_build, xdxr_build_output, xdxr_descs. Synthetic fixtures only, no game data. AI-assisted.

@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

Thanks, that was a real bug, and your guess was right: it was the ordering in the pairing step. MODE_PAIR rewrote the first triangle's staged leaf (rotated vertices, slot 3) from values it had loaded from that same leaf earlier in the same dispatch. That is exactly what the note at the top of xdxr_build.comp forbids for MoltenVK 1.4.2's SPIRV-Cross, which can move such a load after the store. So the rotation read partly rewritten vertices, depending on scheduling.

Fixed in 1b859ca. MODE_PAIR now only records the pair: the sibling's partner, as before, and for the first triangle FIRST | sibling plus the rotation k | kd << 2. MODE_LEAF_PLACE then builds the pair's leaf word by word from the two staged leaves, which nothing writes after MODE_TRIANGLES, so no invocation stores to a word it loaded. Sizes are unchanged (34,048 / 846,848 / 12,160 bytes as before), and the scratch takes 4 more bytes a triangle.

On RTX 5070 Ti (driver 617.42): ctest -R "xdxr|khr_as_copy" 7/7 in three runs without and three with LEONIDA_VK_VALIDATION=1, 0 rays differing in every scene. I can't run it on Apple here; if you have a moment to re-run xdxr_build_check at 1b859ca, that would confirm it.

@LewisCodesWebsites

Copy link
Copy Markdown

Tested 1b859ca (with #626 underneath, on main 21625bc) on AMD Radeon 660M (RDNA2 iGPU, ray query supported), Adrenalin 32.0.21045.1000, Windows 11 26200: ctest -R "xdxr|rt" passes 26/26 with this PR, including xdxr_build_check and d3d12_xdxr_rt_check, the same 26/26 as on main without it. No regressions on AMD. AI-assisted.

@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 #626's new head f51ef5c85 (on main 364ab4eab) as 1700de85b. The only conflict was in tests/test_xdxr_bvh.py: main added test_nan_child_box_at_root_is_rejected next to this PR's dense-leaf tests, and both are kept. The file passes 56 of 56. xdxr_build.inc matches glslc output. On an RTX 5070 Ti (driver 617.42, Windows 11 26300), all 8 ctest -R xdxr tests pass, including xdxr_build_check, d3d12_xdxr_rt_check and vk_xdxr_postbuild_readback.

AI-assisted.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
kawaii-shakal and others added 3 commits November 25, 2026 07:26
…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>
TRI1 pairing left the sibling's leaf unused in the element region, so a
paired build was exactly as large as an unpaired one. A build with
FLAG_PAIR now writes its leaves to scratch, chooses the pairs (MODE_PAIR,
split from MODE_NODES), numbers the leaves that stay with a tile scan
(MODE_LEAF_COUNT / SCAN / INDEX), copies them densely into the element
region (MODE_LEAF_PLACE), and writes the nodes right after the last one,
with their pointers and parent table entries for those places. The header's
offsetToNodes says where. The build, COMPACTED_SIZE and a COMPACT copy are
64 bytes a pair smaller: 34,048 against 39,168 bytes on the 288-triangle
grid, 846,848 against 988,160 on the 7,200-triangle one.

The result room is unchanged; the scratch grows by 76 bytes a triangle,
only for a bottom-level build without ALLOW_UPDATE (XdxrLayout::pair),
which GetRaytracingAccelerationStructurePrebuildInfo now reads from the same
flag. The pairing arrays sit between the boxes and the accumulators, since
MODE_TRIANGLES's arg0 is the vertex stride.

check-build reports any unit of a BLAS's element region no triangle pointer
names (unreferenced_leaves). vk_xdxr_build_test compares a paired grid with
the same grid unpaired and compacts it; d3d12_xdxr_rt_test gives the paired
BLAS its own (larger) prebuild scratch and checks one record a pair.

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

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 (the rule at the top
of xdxr_build.comp), so on an Apple M5 Pro the rotated vertices came out
mixed and the paired cases failed with counts that changed between runs.

MODE_PAIR now only records each pair: the sibling's partner as before, and
for the first triangle FIRST | its sibling and the rotation (k | kd << 2).
MODE_LEAF_PLACE builds the pair's leaf word by word from the two unchanged
staged leaves (paired_word), so no invocation stores to a word it loaded.
The scratch grows by 4 more bytes a triangle (80). Sizes are unchanged.

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

Copy link
Copy Markdown
Contributor Author

Rebased onto #626's new head dcac757ab (main 3ff2a2adc) as a98d67626. The only conflict was the README ray-tracing row; I kept this PR's version, which drops the 'no memory saved yet' note and keeps serialization. tests/test_xdxr_bvh.py passes 56 of 56 and xdxr_build.inc matches glslc. On an RTX 5070 Ti (617.42, Windows 11 26300), ctest -R 'xdxr|khr_as_copy' passes 9 of 9.

AI-assisted.

kawaii-shakal and others added 2 commits November 25, 2026 13:42
… 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>
… 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 (489d2b285). replay_readback_stages_test and replay_khr_as_copy_postbuild_test build and pass on Windows 11 (MSVC 19.50).

kawaii-shakal and others added 5 commits November 25, 2026 12:43
Codex integrated title-placement bounds with serialization, regenerated the synthetic builder shader, and added a portable round-trip check.
Codex regenerated the builder shader with both dense pairing and upstream title-placement support.
Codex used the same pairing flag for address resolution and GPU replay.
Conflicts with ProjectLeonida#2222 (RT-XDXR-UPDATE-SOURCE-CHECK), resolved:
- Shader modes: this branch already uses 19-25 (serialize, deserialize and the
  pair modes), so main's MODE_CHECK = 19 becomes 26, in xdxr_build.comp and
  xdxr_build.h. Both sides' code is kept.
- Scratch: main's SCRATCH_REFIT was byte 28, which here is
  SCRATCH_LEAF_COUNT. An update that falls back to a full paired build would
  have overwritten the verdict with the leaf count and run both tails.
  SCRATCH_REFIT moves to byte 32, which nothing else uses.
- layout_node() (from ProjectLeonida#2222) bounded parent pointers by pc.nodes_off. A paired
  build places its nodes at nodes_off(), right after the kept leaves, so every
  parent was rejected and no box was propagated: all blas_paired_* cases of
  xdxr_build_check missed every ray. It now uses nodes_off(). Updates never
  pair (the layer pairs only builds without ALLOW_UPDATE), so refit_source_ok
  keeps pc.nodes_off.
- 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),
xdxr_build_check included.

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
kawaii-shakal pushed a commit to kawaii-shakal/leonida that referenced this pull request Nov 26, 2026
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
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

Merged current main in dbf2361 (Claude Code, AI-assisted). The conflict with #2222 needed more than a merge:

Full ctest, 113 of 113, on Mesa lavapipe, xdxr_build_check included. Worth a re-run on a hardware GPU.


Generated by Claude Code

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.

6 participants