Skip to content

XDXR builder: TRI1 pairing packs the leaves, a 64-byte leaf saved per pair - #7373

Open
iLucasRT wants to merge 2 commits into
ItsNotRexed:mainfrom
iLucasRT:xdxr-tri1-packed-leaves
Open

iLucasRT wants to merge 2 commits into
ItsNotRexed:mainfrom
iLucasRT:xdxr-tri1-packed-leaves

Conversation

@iLucasRT

Copy link
Copy Markdown

Claimed in the coordination issue as: PROC-401

Depends on: none. It conflicts with #5092 (docs/bvh.md, runtime/src/xdxr_build.inc) and #4640 (runtime/src/vulkan_backend.h); whichever lands second rebases.

What and why

TRI1 pairing (#3315) put a pair's two triangles in the first one's leaf but left the sibling's own leaf unused, so
a paired BLAS was as large as an unpaired one. docs/bvh.md 7a listed "a compaction that reclaims the leaves that
pairing leaves unused" as not done. This writes the leaves after the pairs are known, packed, so a paired build
ends a leaf earlier per pair, and COMPACTED_SIZE, CURRENT_SIZE and COMPACT give that back.

What changes (runtime/src/xdxr_build.comp, xdxr_build.h)

  • With FLAG_PAIR, MODE_TRIANGLES stages each triangle in scratch (its vertices' bits, GeometryIndexAndFlags, its
    primitive index) instead of writing its leaf.
  • New passes after MODE_KEPT_INDEX:
    • MODE_PAIR: one thread per kept node. Same pairing rule as before (siblings, shared edge in opposite directions,
      same geometry, both active), but it records the pairs instead of writing leaves.
    • MODE_LEAF_COUNT, MODE_LEAF_SCAN, MODE_LEAF_INDEX: number the leaves, one per unpaired triangle or pair, in element
      order. A count per tile of 256 elements, the existing one-workgroup scan, then an index per tile, as for the kept
      nodes.
    • MODE_LEAF_WRITE: writes them from offsetToElements on.
  • The nodes start right after the last leaf (nodes_base()). MODE_NODES points a paired sibling's child at its
    pair's leaf as TRI1, as before. The tree, the TRI1 leaf format, the remap bytes and the one parent per leaf do not
    change.
  • Builds without pairing are unchanged (same records, same size). Updates never pair (ALLOW_UPDATE), so a refit
    still finds the nodes at the layout's offset.
  • XdxrLayout::compute takes pair: the prebuild result size stays the unpaired one (the bound), and the scratch
    grows by 56 bytes per triangle plus 8 per tile of 256. The pairing arrays come before the box accumulators,
    because MODE_TRIANGLES uses pc.arg0 for the vertex stride. GetRaytracingAccelerationStructurePrebuildInfo now
    maps ALLOW_UPDATE so that it asks for that scratch exactly when the build may pair, and the replay computes the
    same layout. record_blas pairs only when the layout has room for it.
  • xdxr_build.inc regenerated with glslc --target-env=vulkan1.2 -O -mfmt=c (Vulkan SDK 1.4.363.0, the same output as
    main's .inc for main's .comp).

Tested

Tests (no game files)

  • tools/xdxr_bvh.py: leaf_packing(). check-build runs it on every BLAS case: each 64-byte unit from offsetToElements
    to offsetToNodes must be the leaf of exactly one reachable TRI0 pointer, a TRI1 pointer must share a TRI0's leaf,
    and no leaf may lie outside that range. tests/test_xdxr_bvh.py checks it on the CPU encoder's builds, plus a
    mutation that leaves a leaf unreferenced.
  • Negative control: check-build with leaf_packing on the cases written by XDXR builder: BOX16 nodes for MINIMIZE_MEMORY BLASes, and TRI1 pairing #3315's builder fails exactly the six
    paired cases ("leaf at ... has 0 TRI0 pointers"), and no other case.
  • d3d12_xdxr_rt_test, for the BLAS built without ALLOW_UPDATE:
    • its scratch comes from its own prebuild info, which must be larger than the ALLOW_UPDATE one's;
    • its COMPACTED_SIZE must be below the unpaired build's (same tree), by at most one leaf per two triangles;
    • it is compacted into a buffer of exactly that size and written as d3d12_blas_paired_compacted for check-build.
  • Measured on an RX 7900 XTX: blas_paired_sort_tiles (a 7,200-triangle grid) is 846,848 bytes instead of 988,160
    (2,208 pairs, 14% smaller). The D3D12 test's BLAS is 54,464 bytes instead of 58,368 (61 pairs).

Tested on an AMD Radeon RX 7900 XTX (Windows 11, driver 32.0.32029.2010, MSVC 14.44, Vulkan SDK 1.4.363.0):

  • xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check and
    d3d12_vulkan_device pass. Every ray matches brute force in every case.
  • tests/test_xdxr_bvh.py passes (48 tests).
  • Full ctest on main 9565440 plus this change: 254 of 258 pass. The 4 that fail (vk_queue_query_sync,
    d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host) fail the same way on main
    without it, on this machine.
  • run_gpu_tests.py: all 242 test shaders translate and pass spirv-val. 220 of 236 GPU tests pass, 16 are skipped,
    none fails.
  • Merged with main 0c46fa1 and built again: xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check,
    d3d12_xdxr_rt, d3d12_xdxr_rt_check, cs_bvh, cs_bvh32, tests/test_xdxr_bvh.py and the tests/test_docs_*.py checks
    pass. d3d12_vulkan_device fails there, and it fails the same way on that main without this change (3 of 3 runs
    on this machine), so it is not from this PR.

Not done, or not verified

  • Only tested on one GPU (RDNA3). Not run on NVIDIA, Intel, RADV, MoltenVK, llvmpipe or under Wine. The new passes
    are plain compute with shared memory and barriers, like the kept-node passes; no subgroup operations.
  • Build time with pairing was not measured: five more dispatches and a staging round trip per triangle.
  • Still not paired:
    • Structures that may be updated (ALLOW_UPDATE). An update would have to find each triangle's leaf again, and
      vertex-bit matching is not stable under motion.
    • Pairs across nodes. A leaf has one parent table entry, so both pointers must share a parent.

AI-assisted: yes (Claude)

Checklist

  • 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. (No new translator rule in this PR.)

🤖 Generated with Claude Code

iLucasRT and others added 2 commits November 23, 2026 12:48
… pair

TRI1 pairing (ItsNotRexed#3315) put a pair's two triangles in the first one's leaf but left the sibling's own leaf unused, so
a paired BLAS was as large as an unpaired one. docs/bvh.md 7a listed "a compaction that reclaims the leaves that
pairing leaves unused" as not done. This writes the leaves after the pairs are known, packed, so a paired build
ends a leaf earlier per pair, and COMPACTED_SIZE, CURRENT_SIZE and COMPACT give that back.

What changes (runtime/src/xdxr_build.comp, xdxr_build.h)

- With FLAG_PAIR, MODE_TRIANGLES stages each triangle in scratch (its vertices' bits, GeometryIndexAndFlags, its
  primitive index) instead of writing its leaf.
- New passes after MODE_KEPT_INDEX:
  - MODE_PAIR: one thread per kept node. Same pairing rule as before (siblings, shared edge in opposite directions,
    same geometry, both active), but it records the pairs instead of writing leaves.
  - MODE_LEAF_COUNT, MODE_LEAF_SCAN, MODE_LEAF_INDEX: number the leaves, one per unpaired triangle or pair, in element
    order. A count per tile of 256 elements, the existing one-workgroup scan, then an index per tile, as for the kept
    nodes.
  - MODE_LEAF_WRITE: writes them from offsetToElements on.
- The nodes start right after the last leaf (nodes_base()). MODE_NODES points a paired sibling's child at its
  pair's leaf as TRI1, as before. The tree, the TRI1 leaf format, the remap bytes and the one parent per leaf do not
  change.
- Builds without pairing are unchanged (same records, same size). Updates never pair (ALLOW_UPDATE), so a refit
  still finds the nodes at the layout's offset.
- XdxrLayout::compute takes `pair`: the prebuild result size stays the unpaired one (the bound), and the scratch
  grows by 56 bytes per triangle plus 8 per tile of 256. The pairing arrays come before the box accumulators,
  because MODE_TRIANGLES uses pc.arg0 for the vertex stride. GetRaytracingAccelerationStructurePrebuildInfo now
  maps ALLOW_UPDATE so that it asks for that scratch exactly when the build may pair, and the replay computes the
  same layout. record_blas pairs only when the layout has room for it.
- xdxr_build.inc regenerated with glslc --target-env=vulkan1.2 -O -mfmt=c (Vulkan SDK 1.4.363.0, the same output as
  main's .inc for main's .comp).

Tests (no game files)

- tools/xdxr_bvh.py: leaf_packing(). check-build runs it on every BLAS case: each 64-byte unit from offsetToElements
  to offsetToNodes must be the leaf of exactly one reachable TRI0 pointer, a TRI1 pointer must share a TRI0's leaf,
  and no leaf may lie outside that range. tests/test_xdxr_bvh.py checks it on the CPU encoder's builds, plus a
  mutation that leaves a leaf unreferenced.
- Negative control: check-build with leaf_packing on the cases written by ItsNotRexed#3315's builder fails exactly the six
  paired cases ("leaf at ... has 0 TRI0 pointers"), and no other case.
- d3d12_xdxr_rt_test, for the BLAS built without ALLOW_UPDATE:
  - its scratch comes from its own prebuild info, which must be larger than the ALLOW_UPDATE one's;
  - its COMPACTED_SIZE must be below the unpaired build's (same tree), by at most one leaf per two triangles;
  - it is compacted into a buffer of exactly that size and written as d3d12_blas_paired_compacted for check-build.
- Measured on an RX 7900 XTX: blas_paired_sort_tiles (a 7,200-triangle grid) is 846,848 bytes instead of 988,160
  (2,208 pairs, 14% smaller). The D3D12 test's BLAS is 54,464 bytes instead of 58,368 (61 pairs).

Tested on an AMD Radeon RX 7900 XTX (Windows 11, driver 32.0.32029.2010, MSVC 14.44, Vulkan SDK 1.4.363.0):

- xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check and
  d3d12_vulkan_device pass. Every ray matches brute force in every case.
- tests/test_xdxr_bvh.py passes (48 tests).
- Full ctest on main 9565440 plus this change: 254 of 258 pass. The 4 that fail (vk_queue_query_sync,
  d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host) fail the same way on main
  without it, on this machine.
- run_gpu_tests.py: all 242 test shaders translate and pass spirv-val. 220 of 236 GPU tests pass, 16 are skipped,
  none fails.
- On this branch (main 1cc6e3c plus this commit), built again: the XDXR tests above, cs_bvh, cs_bvh32,
  tests/test_xdxr_bvh.py and the tests/test_docs_*.py checks pass.

Not done, or not verified

- Only tested on one GPU (RDNA3). Not run on NVIDIA, Intel, RADV, MoltenVK, llvmpipe or under Wine. The new passes
  are plain compute with shared memory and barriers, like the kept-node passes; no subgroup operations.
- Build time with pairing was not measured: five more dispatches and a staging round trip per triangle.
- Still not paired:
  - Structures that may be updated (ALLOW_UPDATE). An update would have to find each triangle's leaf again, and
    vertex-bit matching is not stable under motion.
  - Pairs across nodes. A leaf has one parent table entry, so both pointers must share a parent.

AI-assisted (Claude).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflict in GetRaytracingAccelerationStructurePrebuildInfo: main checks desc before the XDXR branch now; the XDXR layout keeps the pairing scratch (ALLOW_UPDATE mapped, xdxr_pair).

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

Copy link
Copy Markdown

Independent NVIDIA check of b6f421b42 (RTX 4070 SUPER, driver 616.92, Windows 11 x64, MSVC 19.44 Release). I compared the PR’s merge head with its mainline parent 0c46fa16b using the stock synthetic Vulkan builder fixtures.

Of 30 emitted structures, exactly six shrank. All six were paired cases. For each, the byte reduction was exactly 64 times the number of TRI1 nodes reported by the checker. For example, the BOX16 paired grid went from 17,344 to 12,160 bytes (81 pairs); the sort-tiles case went from 988,160 to 846,848 (2,208 pairs). The two fixtures that request pairing but cannot form a legal pair kept their original sizes.

The PR passed xdxr_build_output, xdxr_build, xdxr_build_check, d3d12_xdxr_rt and d3d12_xdxr_rt_check. The two independent ray checks covered 26,200 and 8,000 rays respectively; zero differed from brute force. No game files used. This measures sizes and correctness, not build latency. AI-assisted verification.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested b6f421b on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_xdxr_bvh.py - all exit 0.

AI-assisted (Claude Code).

@Ueslii

Ueslii commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b6f421b42 on RDNA 1 (AMD Radeon RX 5600 XT): pass on all XDXR and Python BVH tests.

Tested on: Windows 11 Pro (Build 26200), AMD Ryzen 5 5500, 16 GB RAM, AMD Radeon RX 5600 XT (0x1002:0x731f, Adrenalin / Vulkan driver 26.11.2, Windows driver 32.0.21045.29003, Vulkan 1.4.315), MSVC 19.52.37029.0 (VS 2026), Vulkan SDK 1.4.363.1, RelWithDebInfo. Synthetic fixtures only, no game files.

  • ctest -R "xdxr": 6/6 passed (xdxr_build_output, xdxr_descs, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check).
  • xdxr_build_check: all 30 cases match (0 validation errors, 0 rays differ from brute force). Compared against main (8575d36ed) on the same GPU, all 6 paired cases shrank by 64 * TRI1 bytes:
    • blas_paired_grid: 39168 → 34048 bytes (80 TRI1)
    • blas_paired_grid_box16: 17344 → 12160 bytes (81 TRI1)
    • blas_paired_mixed: 26304 → 22464 bytes (60 TRI1)
    • blas_paired_rotations: 70080 → 61888 bytes (128 TRI1)
    • blas_paired_sort_tiles: 988160 → 846848 bytes (2208 TRI1)
    • blas_paired_transform: 9920 → 8640 bytes (20 TRI1)
  • d3d12_xdxr_rt + d3d12_xdxr_rt_check: all 8 cases match (0 validation errors, 0/1000 rays differ); d3d12_blas_paired and d3d12_blas_paired_compacted are 54464 bytes (61 leaves fewer than 58368 unpaired).
  • py -I tests/test_xdxr_bvh.py: 48/48 passed (2.001s).
  • scripts/check_no_game_data.py on the 12 changed files: exit 0.
  • d3d12_vulkan_device: failed at line 6564: tiles == 1; checked on clean main (dde82f27e) on this same GPU/driver and it fails the same way (line 6574: tiles == 1), so it is pre-existing here and not from this PR.

AI-assisted.

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