Skip to content

XDXR: the BVH size field does not count the header, 64 * (field + 1) bytes (RT-151) - #2264

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
esteban-dcp:rt-151-xdxr-size-field
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
esteban-dcp:rt-151-xdxr-size-field

Conversation

@esteban-dcp

Copy link
Copy Markdown

RT-151: the XDXR BVH size field does not count the 64-byte header (finding by @wildheathen).

Rule (docs/bvh.md §3.1, 271,627 of 271,627 streams): a structure is 64 * (totalSizeIn64BNodes + 1) bytes. The doc's own line for u32@0x20 (0x140 + 64 * totalSizeIn64BNodes, header at 0x100) already says so. The code read the field as the whole size.

Changes

  • runtime/src/xdxr_build.comp: MODE_HEADER writes total_size / 64 - 1; MODE_COPY_AS and MODE_POSTBUILD use (field + 1) * 64. xdxr_build.inc is regenerated with the command in xdxr_build_spv.h (glslc 2026.4, so the file is rewritten whole, as in earlier PRs).
  • tools/xdxr_bvh.py: the reference builder writes end // 64 - 1; validate() checks (units + 1) * 64 == len(data).
  • tests/test_xdxr_bvh.py: the round-trip assertion uses the new size. runtime/test/d3d12_xdxr_rt_test.cpp: its dump reads (units + 1) * 64.
  • docs/bvh.md: the field and the copy/postbuild sentences.

Results (8ab01bbe, Linux): vk_xdxr_build_test then tools/xdxr_bvh.py check-build --rays 1000 (what the xdxr_build / xdxr_build_check CTest pair runs): every case has 0 validation errors and 0/1000 rays differ, including the clone and compaction copies (blas_clone, blas_compacted, tlas_refit_clone), on the RTX 4050 Laptop (driver 620.73.04) and the Radeon 740M (RADV, Mesa 26.3.1). With the old binary the new validator reports a validation error on every case, so the check does pin it. test_xdxr_bvh.py, test_docs_links.py and test_ctest_registration.py pass (36).

Not run: the Windows-only d3d12_xdxr_rt_test (its one-line change is read, not built). A console-built structure was not copied (no game data); the copy path is checked on layer-built structures only.

No game data. AI-assisted (Claude Code).

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 22, 2026 16:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@wildheathen

Copy link
Copy Markdown
Contributor

I tested 8ab01bb against real console structures: 8 BLASes from core_bvh4.rpf, rehydrated byte-identically to XboxBvhBuilder.dll with tools/bvh_rehydrate.py (#2205). This ran locally and no data is attached.

  • The size fix holds: all 8 are 64 * (totalSizeIn64BNodes + 1) bytes, and validate() no longer reports the size.
  • validate() then raises struct.error on 7 of the 8, at stored = struct.unpack_from("<I", data, parents_off + 4 * u). Their offsetToParents is negative as a signed 32-bit value: -576, -960, -2112, -320, -6720, -7232 and -87104 bytes from the header. Only the one 2-leaf structure has +0x40. It is read unsigned here, so parents_off + 4 * u lands about 4 GB past the buffer.

What the console layout does: the entry for unit u (counted from offsetToElements) is at offsetToParents + 4 * u, in 32-bit arithmetic. The entries of the low units, which are the leaves, fall before the header, and only the entries from the header end on (the box nodes and the last leaves) are stored in the structure, between 0x40 and offsetToElements. The library writes exactly those and nothing for the others, and #2205 does the same. That also answers the "signed/relative interpretation is still Open" note on offsetToParents in docs/bvh.md §3.1.

A suggestion for validate():

  • read offsetToParents signed;
  • check only the entries whose address lies in [0x40, offsetToElements);
  • treat the others as not stored.

A synthetic structure with a negative offset would cover it in tests/test_xdxr_bvh.py. This is CPU only, on Windows 11; no GPU run.

AI-assisted.

@Ghxstsec

Copy link
Copy Markdown

Tested 8ab01bbe on Intel Arrow Lake-S iGPU (Intel(R) Graphics (ARL), 0x8086:0x7d67), Mesa 26.1.6 ANV, Vulkan 1.4.354, Linux: pass.

  • ctest -R xdxr: xdxr_descs, xdxr_build and xdxr_build_check all pass (3/3).
  • python -I tests/test_xdxr_bvh.py: 27 tests OK.
  • I had the same change locally and checked it is sensitive: with the old xdxr_build.inc and the new validate(), xdxr_build_check fails.

@Flyander Flyander 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.

The size off-by-one correction is consistent with the reported rehydrated structures, but validate() still fails on the parent table for most real structures. An independent report on eight locally rehydrated BLASes found struct.error on seven: offsetToParents is a signed negative relative offset, while the current unpack format reads it unsigned and computes a location beyond the buffer.

Please decode this field as signed and only validate parent entries whose computed address lies within the stored parent-table range; the low leaf entries are not stored in the structure. A synthetic fixture with a negative parent offset should cover the case without including game data. I did not run tests.

SirAmong and others added 2 commits November 22, 2026 11:58
…4 * (field + 1) bytes (RT-151)

docs/bvh.md 3.1: u32@0x20 = 0x140 + 64 * totalSizeIn64BNodes, 271,627 of
271,627 streams. xdxr_build.comp wrote total_size / 64; the copy and
postbuild paths read the field as the whole size, and tools/xdxr_bvh.py
wrote and validated it the same way. Now the header writes total / 64 - 1,
MODE_COPY_AS and MODE_POSTBUILD use (field + 1) * 64, validate() and the
reference builder agree, xdxr_build.inc is regenerated (glslc 2026.4) and
d3d12_xdxr_rt_test reads the size the same way.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ies are checked (RT-151 review)

Console structures keep offsetToParents negative: the entries of the low units
(the leaves) fall before the header and are not stored; only the ones between
the header end and offsetToElements are. validate() read the field unsigned and
raised struct.error on 7 of 8 rehydrated BLASes (@wildheathen, @Flyander). It
now reads it signed and compares only entries whose address is in
[header end, offsetToElements). A synthetic structure in console layout
(negative offset, leaf entries dropped) covers it; a wrong stored entry and a
runtime build are still caught. docs/bvh.md 3.1 drops the Open note.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@esteban-dcp
esteban-dcp force-pushed the rt-151-xdxr-size-field branch from 8ab01bb to bd51308 Compare November 22, 2026 16:58
@esteban-dcp

Copy link
Copy Markdown
Author

Thanks @wildheathen and @Flyander. Fixed in bd513088 (rebased on main; the shader and xdxr_build.inc are unchanged by the rebase).

  • validate() reads offsetToParents signed ("<4I6fIiIIiI") and compares a parent entry only when its address offsetToParents + 4 * u lies in [header end, offsetToElements); the low units' entries are not stored, so there is nothing to compare for them. A runtime build (offset 0x40, every entry stored) is checked exactly as before.
  • Synthetic fixture, no game data: ConsoleParentTable in tests/test_xdxr_bvh.py rewrites a reference-built BLAS the way the console lays it out (negative offset, the leaf entries dropped, the rest stored from the header end on). It validates clean; corrupting every stored entry is still caught, in both layouts. With main's validate() the same fixture raises the reported struct.error (it asks for an offset about 4 GB past the buffer), so the test pins it.
  • docs/bvh.md 3.1: the "signed/relative interpretation is still Open" note now says it is a signed offset and that only the entries from the header end to offsetToElements are stored.

Re-checked here on Linux: test_xdxr_bvh.py, test_docs_links.py and test_ctest_registration.py pass (40); vk_xdxr_build_test + check-build still match on the RTX 4050 Laptop and the Radeon 740M. I have no console structures, so the real-structure run is still wildheathen's to repeat on this head.

AI-assisted (Claude Code).

@Flyander Flyander 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.

The new parser treats offsetToParents as the signed relative offset and bounds reads to the stored parent-table region. The updated fixture exercises a negative offset and still rejects corrupted entries without reading absent ones. I found no remaining static issue at this head. Approved.

@romainveurrier romainveurrier 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.

D3D-178 owner here: you're right, and thanks @wildheathen for the finding. docs/bvh.md §3.1's own 0x140 + 64 * n with the header at 0x100 already said the field doesn't count the header, and I misread it when writing the builder.

Reviewed bd51308. I grepped every reader and writer of the field in the tree:

  • xdxr_build.comp: the header write, MODE_POSTBUILD and MODE_COPY_AS;
  • xdxr_bvh.py: _write_header and validate();
  • the d3d12_xdxr_rt_test dump.

All are changed, and nothing else reads the field. bvh_index.py already used the console convention. The refit path rewrites the header through the same build_header, so updates are covered. vk_xdxr_build_test dumps layout.size and compares postbuild sizes against it, which this change keeps consistent. The signed offsetToParents and the "only entries between the header end and offsetToElements are stored" rule match the public XboxBvh4Node_GetParentTableOffset. A runtime build (0x40) is still checked entry by entry.

Tested bd51308 on Fedora 44:

  • Native: ctest -R xdxr_build (build + check-build) passes on an RTX 2050 (NVIDIA 620.70.06), an Iris Xe (Mesa 26.2.7) and llvmpipe. tests/test_xdxr_bvh.py is OK.
  • Wine 11.21 with clang-cl 22.1.8 and xwin, covering the Windows-only test the PR lists as not run: xdxr_descs, xdxr_build (+check), d3d12_xdxr_rt (+check) and d3d12_indirect_rt all pass on both GPUs. d3d12_blas, d3d12_blas_update, d3d12_blas_compacted and d3d12_tlas all give 0/1,000 differing rays, so the D3D12 postbuild sizes and the COMPACT copy agree with the new convention.
  • xdxr_build.inc differs from my glslc 2026.1 output only by compiler version; the tests above ran with the committed file.

AI-assisted (Claude Code).

vitkoviy pushed a commit to vitkoviy/Leonida that referenced this pull request Nov 22, 2026
@ItsNotRexed
ItsNotRexed merged commit 49f6da4 into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
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.

7 participants