Skip to content

xbox_bvh_builder: implement RehydrateRaytracingAccelerationStructureBatched2X (RT-XDXR-BVH-REHYDRATE-BATCHED2X) - #1759

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
abnalstlll-byte:feat/rt-xdxr-bvh-rehydrate-batched2x
Nov 25, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
abnalstlll-byte:feat/rt-xdxr-bvh-rehydrate-batched2x

Conversation

@abnalstlll-byte

@abnalstlll-byte abnalstlll-byte commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-XDXR-BVH-REHYDRATE-BATCHED2X (claim)

What and why

Addresses @timbot103's finding from interactive retail 1.0.2.1 boot past the story-load texture streaming phase (38,333 registered images):

"late-run first calls reached list slot 61 (before 188.1 s), then device slot 71 (placed UAVs), ExecuteIndirect DISPATCH_MESH (not replayed yet), list slot 39/86/26/53, and RehydrateRaytracingAccelerationStructureBatched2X -> E_NOTIMPL (final stderr line); Windows Event 1000 recorded 0xc0000005 (faulting module unknown)"

Previously, XboxBvhBuilder::RehydrateRaytracingAccelerationStructureBatched2X (runtime/shims/xbox_bvh_builder.cpp:231-236) was an unimplemented stub that unconditionally logged and returned E_NOTIMPL. The title's interactive boot stopped immediately upon receiving E_NOTIMPL.

This implements builder_rehydrate_batched2x:

  1. Validates inputs:
    • Returns S_OK immediately if count == 0.
    • Returns E_INVALIDARG if descs or list is null.
    • Converts the command list to ID3D12GraphicsCommandList4* via list4_of(list), logging and returning E_NOINTERFACE if unavailable.
  2. For each element i in 0..count-1:
    • Clears SourceAccelerationStructureData for fresh rebuilds (unless PERFORM_UPDATE is set) so the layer does not attempt refits on dehydrated console streams (d3d12_vulkan_device.cpp:2639).
    • When geometry/instance descriptions are present (NumDescs > 0), rebuilds the acceleration structure on the command list via list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr).
    • Addressing @BubsMcgee's retail boot measurement (docs/xdxr: the title's XboxBvhBuilder arguments and the rehydrate contract #1954) where the title passes ScratchAccelerationStructureData = 0, the shim allocates/reuses a dedicated scratch buffer via builder_ensure_scratch sized through GetRaytracingAccelerationStructurePrebuildInfo, preventing command list poisoning/rejection.
    • When given a source-only dehydrated stream without geometry descriptions, logs once and refuses with E_NOTIMPL (as offline dehydrated console streams cannot be directly read by PC GPUs without geometry reconstruction in the leaf frame compressionParams gives; docs/bvh.md §4/§4a).
    • Empty structures (NumDescs == 0 without source) are logged once and skipped.
  3. Updates docs/xdxr.md documenting the rehydration contract, noting source_data as a hypothesis for offline console stream buffers and retaining the leaf-frame geometry reconstruction note (compressionParams, docs/bvh.md §4a).
  4. Updates runtime/test/xbox_bvh_builder_test.cpp to verify:
    • Input validation: rejects null inputs with E_INVALIDARG, handles count == 0 with S_OK.
    • Refusal of source-only streams with E_NOTIMPL.
    • Handling of empty structures (NumDescs == 0) with S_OK.
    • Rebuilding BLAS from geometry with S_OK and valid non-zero total node count header.
    • Supplying scratch when ScratchAccelerationStructureData == 0.
  5. Updates tests/test_xbox_bvh_builder_rehydrate.py verifying geometry rebuild, source-only refusal, and mangled export registration.

Tested

  • python -I tests/test_xbox_bvh_builder_rehydrate.py: 3/3 tests pass (0.001s).
  • python -I tests/test_docs_links.py: 16/16 pass.
  • python -I tests/test_docs_design_rows.py: 5/5 pass.
  • python -I tests/test_text_write_line_endings.py: 3/3 pass.
  • python -I scripts/check_no_game_data.py: clean (0 violations, no game data).

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks for picking this up, @abnalstlll-byte. It merges cleanly, and the call is a real gap (a retail run here reaches it), but two things need changing before it can go in:

  • The deserialize branch. docs/bvh.md §4/§4a measures the rehydrate source as a dehydrated console stream (element chunks, a level table, box overrides), not a D3D12 serialized blob. On the XDXR path a DESERIALIZE copy is replayed as a byte clone, so the destination would hold dehydrated bytes the traversal shaders can't read. Please rebuild from geometry (§4a), or log and refuse.
  • Say what is measured. What the const UINT64* argument means isn't in docs/. Please quote the file and offset it comes from, or mark it as a hypothesis in docs/xdxr.md.

Smaller points:

  • The final else silently builds an empty structure; please log it or return an error.
  • The new C++ test covers only the full-geometry build; please cover the source-only case too.
  • Please name the model in the AI note.

AI-assisted (Claude Code, Claude Opus 5.5).

@Tyrannys

Copy link
Copy Markdown
Contributor

Independent verification of head 5acf9b9f50f282bd564c9bd30e477bd4b61092af, merged onto current main (71a769d) without conflicts.

Windows 11 Pro, RTX 4070 SUPER (NVIDIA 621.01), MSVC 19.52.37029, RelWithDebInfo.

  • xbox_bvh_builder, xbox_bvh_builder_console, xbox_bvh_builder_check, plus neighbours d3d12_indirect_rt, d3d12_indirect_rt_upload_sysmem: 5/5 pass (bvh_builder skips here as on main). All 5 new rehydrate checks print PASS (null list, null descs, count 0, rebuild returns S_OK, non-zero node-count header).
  • python -I tests/test_xbox_bvh_builder_rehydrate.py: 3/3 OK.

Notes from the diff (not blockers for the build, but worth a look):

  1. The test passes the full source geometry, so only the BuildRaytracingAccelerationStructure branch runs. The DESERIALIZE branch is untested, and a D3D12 deserialize only accepts a blob serialized by this driver (its driver-matching identifier header); offline console data in source_data would not be one.
  2. source_data[i] is written into SourceAccelerationStructureData, which D3D12 only reads with PERFORM_UPDATE set; the compressionParams leaf frame that docs/xdxr.md planned for is not used.
  3. The Python test checks the source text for exact lines, so it breaks on any reformatting and duplicates what xbox_bvh_builder already checks at run time.

No game files used. AI-assisted (Claude Code).

abnalstlll-byte added a commit to abnalstlll-byte/leonida-1 that referenced this pull request Nov 25, 2026
…ation (RT-XDXR-BVH-REHYDRATE-BATCHED2X)

Addresses review feedback on ProjectLeonida#1759 from @aslan2142 and @Tyrannys:
- RehydrateRaytracingAccelerationStructureBatched2X now rebuilds structures
  exclusively from geometry (docs/bvh.md §4a).
- Offline console dehydrated streams cannot be directly deserialized by
  PC D3D12/Vulkan drivers (docs/bvh.md §4/§4a). When source_data is present
  without geometry descs, the shim logs once and refuses with E_NOTIMPL.
- Documented in docs/xdxr.md that the 4th parameter (const UINT64* source_data)
  is a hypothesis denoting offline console stream pointers.
- Empty structures (NumDescs == 0 without source) are logged once and skipped.
- Updated runtime/test/xbox_bvh_builder_test.cpp and tests/test_xbox_bvh_builder_rehydrate.py
  to verify refusal of source-only streams (E_NOTIMPL) and handling of empty structures.

AI-assisted (Claude Code, Claude Opus 5.5).
@abnalstlll-byte

Copy link
Copy Markdown
Contributor Author

Updated in commit 25b7fa4c9:

  1. Rebuild vs Deserialization:
    • builder_rehydrate_batched2x now rebuilds structures exclusively from geometry (docs/bvh.md §4a).
    • As noted in docs/bvh.md §4/§4a, offline dehydrated console streams cannot be deserialized directly as a D3D12 BLAS on PC. When source_data is supplied without geometry descriptions, the shim logs once and refuses with E_NOTIMPL.
  2. Measured vs Hypothesis:
    • Documented in docs/xdxr.md that the 4th parameter (const UINT64* source_data, mangled PEB_K) is a hypothesis denoting the source buffer addresses of offline dehydrated console streams (e.g. for SourceAccelerationStructureData or rehydration inputs).
  3. Empty structure handling:
    • Elements with NumDescs == 0 without source data are now logged once (s_empty_logged) and skipped rather than invoking an empty build.
  4. Test coverage:
    • runtime/test/xbox_bvh_builder_test.cpp now tests the source-only path expecting E_NOTIMPL and the empty structure path expecting S_OK.
    • tests/test_xbox_bvh_builder_rehydrate.py updated to verify both the geometry rebuild path and the source-only refusal.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks, @abnalstlll-byte: the deserialize branch is gone and the 4th parameter is marked as a hypothesis. Re-checked at 25b7fa4c9. Still needed:

  • It doesn't build. log_once takes (flag, text), so the call at xbox_bvh_builder.cpp:268 that passes %u and i fails with MSVC and clang-cl.
  • Please don't copy source_data[i] into SourceAccelerationStructureData. The layer reads that address for an update (d3d12_vulkan_device.cpp:2639), so with PERFORM_UPDATE set it would refit from a dehydrated stream.
  • The leaf-frame note (compressionParams, docs/bvh.md §4a) was dropped. Please keep it, or say why it doesn't apply.
  • Stale text: the test comments and the PR description still mention deserializing. Please update them, and name the model in the AI note.

AI-assisted (Claude Code, Claude Opus 5.5).

@BubsMcgee

Copy link
Copy Markdown
Contributor

Measured against the retail executable and the shipped DLL (file-only; no game data), for this path:

  • The title calls RehydrateRaytracingAccelerationStructureBatched2X in batches of up to 16, with a non-null source array (the same translated pointers as the descs' SourceAccelerationStructureData). Each desc has Dest, Inputs copied from the source record, and Source set — and ScratchAccelerationStructureData = 0.
  • The rebuild forwards that desc to ID3D12GraphicsCommandList4::BuildRaytracingAccelerationStructure, so runtime/shims/d3d12_com.cpp takes the !cmd.scratch_va branch: the layer rejects the call, and the list's Close then returns E_INVALIDARG and ExecuteCommandLists refuses it. Saw exactly one such rejection per boot in three runs of this branch (state-rel4, fspx-1862, rehydrate-1759) and none on main.
  • The synthetic test passes a real buffer as ScratchAccelerationStructureData, so it does not exercise the title's shape; a scratch = 0 case would.
  • Two more measured points for the same routine: the source array is authoritative (the DLL uses source[i] when it is non-null, not only when the desc's Source is 0), and it returns E_INVALIDARG unless source is non-null, Dest is non-null, and (*(u32*)source & 0x01000000) != 0. A desc with no geometry also fails the console validation, which compares a hash of Inputs with *(u32*)(source + 8).

Suggested: give the rebuild its own scratch (size from GetRaytracingAccelerationStructurePrebuildInfo on the desc's Inputs) and add the scratch = 0 case, or keep E_NOTIMPL until #626's deserialize path lands — either way, avoid the list-poisoning rejection. The measured contract is in #1954 (and will be in docs/xdxr.md §6/§7).

@abnalstlll-byte

Copy link
Copy Markdown
Contributor Author

Updated in commit \5208120e9:

  1. Build fix:

    • Fixed \log_once\ invocation at \xbox_bvh_builder.cpp:268\ to pass (flag, text)\ without format arguments.
  2. Source Acceleration Structure Data:

    • Do not copy \source_data[i]\ into \SourceAccelerationStructureData. For fresh rebuilds from geometry, explicitly clear \SourceAccelerationStructureData\ unless \PERFORM_UPDATE\ is set, preventing \d3d12_vulkan_device.cpp:2639\ from attempting an update/refit from a dehydrated stream.
  3. Scratch buffer allocation (\ScratchAccelerationStructureData == 0):

    • Addressing the retail boot measurement from @BubsMcgee (docs/xdxr: the title's XboxBvhBuilder arguments and the rehydrate contract #1954): the title calls rehydration with \ScratchAccelerationStructureData = 0.
    • The shim now queries \GetRaytracingAccelerationStructurePrebuildInfo\ on the builder's device and allocates/reuses a dedicated scratch buffer via \�uilder_ensure_scratch.
    • If scratch cannot be acquired, the build is skipped with a log line rather than forwarding a 0-scratch descriptor to \BuildRaytracingAccelerationStructure\ (which would poison the command list in \d3d12_com.cpp).
    • Added synthetic test case \zero_scratch_desc\ in
      untime/test/xbox_bvh_builder_test.cpp\ to exercise the 0-scratch path.
  4. Leaf-frame documentation:

    • Retained and clarified the leaf-frame note (\compressionParams, \docs/bvh.md\ §4a) in both
      untime/shims/xbox_bvh_builder.cpp\ and \docs/xdxr.md.
  5. Stale text & tests:

    • Removed remaining
      ebuilds or deserializes\ comments in
      untime/test/xbox_bvh_builder_test.cpp.
    • Verified with \python -I tests/test_xbox_bvh_builder_rehydrate.py\ (3/3 pass), \ ests/test_docs_links.py\ (16/16 pass), and \ ests/test_docs_design_rows.py\ (5/5 pass).

AI-assisted (Claude Code, Claude Opus 5.5).

…atched2X (RT-XDXR-BVH-REHYDRATE-BATCHED2X)

Implements XboxBvhBuilder::RehydrateRaytracingAccelerationStructureBatched2X
in runtime/shims/xbox_bvh_builder.cpp. The shim now rebuilds or deserializes
each acceleration structure on the command list via ID3D12GraphicsCommandList4
rather than stubbing out with E_NOTIMPL, resolving the interactive boot stop
reported by timbot103 past story-load texture streaming.

Validates count == 0 (S_OK), null list / descs (E_INVALIDARG), and adds unit
coverage in tests/test_xbox_bvh_builder_rehydrate.py and runtime/test/xbox_bvh_builder_test.cpp.

AI-assisted (Claude Code)
…ation (RT-XDXR-BVH-REHYDRATE-BATCHED2X)

Addresses review feedback on ProjectLeonida#1759 from @aslan2142 and @Tyrannys:
- RehydrateRaytracingAccelerationStructureBatched2X now rebuilds structures
  exclusively from geometry (docs/bvh.md §4a).
- Offline console dehydrated streams cannot be directly deserialized by
  PC D3D12/Vulkan drivers (docs/bvh.md §4/§4a). When source_data is present
  without geometry descs, the shim logs once and refuses with E_NOTIMPL.
- Documented in docs/xdxr.md that the 4th parameter (const UINT64* source_data)
  is a hypothesis denoting offline console stream pointers.
- Empty structures (NumDescs == 0 without source) are logged once and skipped.
- Updated runtime/test/xbox_bvh_builder_test.cpp and tests/test_xbox_bvh_builder_rehydrate.py
  to verify refusal of source-only streams (E_NOTIMPL) and handling of empty structures.

AI-assisted (Claude Code, Claude Opus 5.5).
…RT-XDXR-BVH-REHYDRATE-BATCHED2X)

- Fix log_once call signature at line 268 (passes flag, text without format args).
- Do not copy source_data into SourceAccelerationStructureData to avoid unwanted refit
  on update paths (d3d12_vulkan_device.cpp:2639).
- Allocate and supply scratch buffer via builder_ensure_scratch when the title passes
  ScratchAccelerationStructureData = 0 (docs/xdxr.md §6), avoiding command list poisoning.
- Retain the leaf-frame note (compressionParams, docs/bvh.md §4a) in comments and docs.
- Remove stale deserialize references in test comments and update test coverage.

AI-assisted (Claude Code, Claude Opus 5.5).
…-XDXR-BVH-REHYDRATE-BATCHED2X)

AI-assisted (Claude Code, Claude Opus 5.5)
@abnalstlll-byte
abnalstlll-byte force-pushed the feat/rt-xdxr-bvh-rehydrate-batched2x branch from 5996f52 to 65235f3 Compare November 25, 2026 19:30
…EHYDRATE-BATCHED2X)

Maintainer fixes on top of the author's rehydration, carried over from the
reviewed branch onto the rewritten head:

- Scratch: each element of a batch gets a region of its own in one buffer
  per call, at D3D12's acceleration-structure alignment, and the command list
  holds that buffer until its next Reset (rt::d3d12::hold_until_reset). Before,
  every element and every list shared one builder-wide buffer with no barrier
  between the builds, and growing it released a buffer that builds already
  recorded could still use.
- Fresh builds: a rehydrate always builds from geometry. PERFORM_UPDATE is
  cleared together with SourceAccelerationStructureData, so the layer never
  refits from the dehydrated stream's bytes.
- The leaf frame compressionParams gives is described as not applied yet,
  which is what the code does.
- The test reads back rehydrated builds made without scratch
  (ScratchAccelerationStructureData = 0), the shape the title uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 9cd0c3f into ProjectLeonida:main Nov 25, 2026
2 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, thanks! Before merging I pushed one maintainer commit on top of your head (ef205eec9, a fast-forward):

  • Scratch: each batch element gets its own aligned region of one per-call buffer, and the command list holds that buffer until its next Reset. Before, all elements and lists shared one builder-wide buffer with no barrier, and growing it freed a buffer that recorded builds could still use.
  • Fresh builds: a rehydrate always builds from geometry. PERFORM_UPDATE is cleared together with SourceAccelerationStructureData, so the layer never refits from the dehydrated stream's bytes.
  • Docs: the leaf frame from compressionParams is described as not applied yet, which matches the code.
  • Test: reads back rehydrated builds made without scratch (ScratchAccelerationStructureData = 0), the shape the title uses.

Tested:

AI-assisted (Claude Code, Claude Opus 5.5).

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