Repository navigation
xdxr: a paired BLAS keeps one 64-byte leaf a pair (RT-XDXR-PAIR-DENSE) - #690
kawaii-shakal wants to merge 11 commits into
Conversation
abnalstlll-byte
left a comment
There was a problem hiding this comment.
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.
|
Tested Three runs of
The messages are 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 Everything else on this head passed: |
|
Thanks, that was a real bug, and your guess was right: it was the ordering in the pairing step. Fixed in 1b859ca. On RTX 5070 Ti (driver 617.42): |
|
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: |
1b859ca to
1700de8
Compare
|
Rebased onto #626's new head AI-assisted. |
…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>
1700de8 to
a98d676
Compare
|
Rebased onto #626's new head 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>
… 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>
|
CI fix: |
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
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
|
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, Generated by Claude Code |
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: oneLeafper entry, an entry holding the second primitive). The nodes follow directly. The build,COMPACTED_SIZEand aCOMPACTcopy are 64 bytes a pair smaller.How (
runtime/src/xdxr_build.comp,.h):FLAG_PAIR,MODE_TRIANGLESwrites the leaves to scratch.MODE_PAIR, split out ofMODE_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).MODE_LEAF_COUNT/SCAN/INDEX, sharingtile_indexwith the kept-node scan) gives each its place.MODE_LEAF_PLACEcopies them densely into the element region, building a pair's leaf from the two staged ones.MODE_NODESand everything after usenodes_off()=elements_off + 64 * kept, also written to the header'soffsetToNodes. Pointers and parent table entries are written for the final places, so nothing moves afterwards.numElementsstays the triangle count, astools/xdxr_bvh.py build_blaswrites it.ALLOW_UPDATE. This isXdxrLayout::compute(..., pairing), andrecord_blaspairs only with such a layout.GetRaytracingAccelerationStructurePrebuildInfonow mapsALLOW_UPDATEand asks for the same layout as the build.pc.arg0, which is the vertex stride inMODE_TRIANGLES.Checks:
check-buildreports 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:blas_grid_unpaired) and requires the sizes to differ by exactly 64 bytes per TRI1 child;COMPACTED_SIZE;COMPACT-copies it (blas_paired_compacted, traced bycheck-build).d3d12_xdxr_rt_test:ALLOW_UPDATEone;It used to reuse the
ALLOW_UPDATEbuild'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_PAIRrewrote 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 ofxdxr_build.comp. NowMODE_PAIRonly records the pair (FIRST | siblingandk | kd << 2for the first triangle), andMODE_LEAF_PLACEbuilds 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 withLEONIDA_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-valclean.ctest -R "xdxr|khr_as_copy": 7/7 passed, both without and withLEONIDA_VK_VALIDATION=1(synchronization validation). Everyxdxr_build_checkandd3d12_xdxr_rt_checkscene: 0 validation errors, 0 rays differing from brute force.blas_paired_grid(288 triangles, 80 pairs)blas_paired_sort_tiles(7,200 triangles, 2,208 pairs)d3d12_blas_paired(428 triangles, 61 pairs)blas_paired_grid_box16(162 triangles, 81 pairs)Whole
ctest -C RelWithDebInfo: 293 run, 292 passed. The one failure isd3d12_execute_indirect_validation, which fails onmaintoo (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, everytests/test_docs_*.py: OK.python -I scripts/check_no_game_data.pyon 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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase). (on top of d3d12: serialize and deserialize XDXR acceleration structures (RT-XDXR-SERIALIZE) #626, which merges cleanly withmain)docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code