Skip to content

XboxBvhBuilder shim: rehydrate dehydrated BLASes on the GPU, byte-exact with bvh_rehydrate.py (RT-5) - #7903

Open
romainveurrier wants to merge 6 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xdxr-rehydrate
Open

romainveurrier wants to merge 6 commits into
ItsNotRexed:mainfrom
romainveurrier:rt/xdxr-rehydrate

Conversation

@romainveurrier

Copy link
Copy Markdown
Contributor

Item: RT-5, stage 2 of my XboxBvhBuilder claim (claim).
Depends on: #7516 (stage 1; its commit is the first one here, the two new commits are on top).

Why

The executable calls RehydrateRaytracingAccelerationStructureBatched2X from 4 sites, to turn the dehydrated BLASes shipped in the *_bvh4.rpf packs into runtime structures. In #7516 the shim answered E_NOTIMPL. The title sizes each destination from the dehydrated header, so a fresh build from geometry can overflow it: console trees pair leaves and use BOX16 nodes. The safe answer is real rehydration, which writes exactly the dehydrated size.

What

1. src/xdxr_rehydrate.comp + xdxr_rehydrate.h: rehydration on the GPU. It follows tools/bvh_rehydrate.py, the model that matches XboxBvhBuilder.dll byte for byte (docs/bvh.md §4a), step for step and with its float32 rounding (precise, and fma where the model rounds a*b + c once):

  • A one-thread plan pass: the dehydrated header, leaf count and level table are in GPU memory and may be written by earlier GPU work, so the CPU never reads them. The plan pass writes every later pass's VkDispatchIndirectCommand into the scratch.
  • The passes: clear; the kept header (dehydrated flag cleared, root parent entry); the leaves, one thread each, through the leaf frame of compressionParams with the 64-rotation table; then each level's BOX32 and BOX16 groups bottom-up. Each group is one workgroup: boxes recomputed from the rehydrated children, stored overrides, BOX16 rounding, parent entries.
  • Size: every node and leaf is written where its packed bytes are, so the result has exactly the dehydrated size. The source and destination must differ.

2. Through the layer.

  • The command: RehydrateAccelerationStructure in command_list.h. The layer's lists answer a private ILeonidaXdxrRehydrate (shims/d3d12_xdxr_rehydrate.h), so the shim needs no link to the layer.
  • Replay: it prepares the command (the scratch's buffer for the indirect dispatches, its room for the plan) and records it at the arena's addresses.
  • What it can't do: a rehydration the layer can't handle (a geometry that isn't R32G32B32_FLOAT, or a scratch too small for the plan of 3,712 + 32 bytes per geometry) is skipped with a log line instead of failing the whole list.

3. The export. Each element goes to the list, and the first failure is returned. On a list that isn't the layer's (the Windows D3D12 backend), it answers E_NOTIMPL.

Open, posted as a finding:: the call's last argument, one UINT64 per element. On my copy's 4 call sites, the descriptions and the UINT64 array are both on the caller's stack, and batches hold at most 16 elements (static read; offsets only). The DLL's single-structure RehydrateRaytracingAccelerationStructureX works without it. The shim logs the first values once and doesn't use them.

Tests

  • xdxr_rehydrate_cases + xdxr_rehydrate (Vulkan, Linux or Windows): synthetic dehydrated BLASes, with tools/bvh_rehydrate.py as the oracle.
    • What the cases cover: several levels of both widths, more than 64 nodes a level, two leaf chunks (3,000 leaves), overrides, paired leaves with every vertex order, u16, u32 and no indices, a 20-byte stride, a transform, the leaf frame with scale, translation and rotation indices 5 and 63, updatable parents, and a single-leaf root.
    • What's checked: the GPU result byte for byte, a guard past each structure, the plan's status, a structure that isn't dehydrated (left untouched), and source == destination (refused).
  • xbox_bvh_builder: the same cases through the shim's export, on the layer's Vulkan device. 0 bytes differ on each.

Tested

Fedora 44, NVIDIA GeForce RTX 2050 (proprietary driver), glslc / spirv-val (the SPIR-V passes spirv-val):

  • Native Vulkan (vk_xdxr_rehydrate_test): all 5 synthetic cases identical to the model.
  • clang-cl + xwin, Wine 11.21: xbox_bvh_builder (19 checks; the 5 cases through the shim, all identical), xbox_bvh_builder_console, xbox_bvh_builder_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check pass. No warnings in the changed files at /W4.
  • Real streams from my own 1.0.2.1 copy: local only, nothing posted but counts. Random geometry, since the source meshes aren't identified; every other byte (trees, overrides, frames, level tables) is the game's:
Pack Streams Identical to the model
shared_minigame000_bvh4.rpf 400 400
core_bvh4.rpf (1–15 levels, 1,524,515 leaves, a leaf frame on every one) 400 400

No game files in the repository. AI-assisted (Claude Code).

🤖 Generated with Claude Code

romainveurrier and others added 3 commits November 23, 2026 17:57
…, boot stop 18)

The package's XboxBvhBuilder.dll creates 52 console compute programs as
pipelines in its constructor, on the title's thread. Translated, they take
25-60 s to compile on a cold driver cache (ItsNotRexed#7231/ItsNotRexed#7049: 31.2 s for one on
an RTX 4080, 59.5 s for all 52 on an RX 9070 XT), longer than the title's
~30 s startup watchdog, so a first boot ends at the builder.

runtime/shims/xbox_bvh_builder.cpp serves the seven exports the executable
imports (measured statically on 1.0.2.1: constructor 1 call site,
destructor 1, Set64BitMortonCodes 8, GetRaytracingAccelerationStructure-
PrebuildInfo 3, BuildRaytracingAccelerationStructureBatched2X 6,
CopyRaytracingAccelerationStructure 2, RehydrateRaytracingAcceleration-
StructureBatched2X 4) with the layer's own XDXR builder (D3D-178). The
loader binds imports to a shim before a bundled module, so the DLL is not
loaded and its programs are never created.

- each BATCHED_DESC2 element (docs/xdxr.md §1) is one layer build with its
  postbuild descriptions; the offline-statistics type (0x10), which the DLL
  refuses by throwing, skips the element instead (a shim must not throw);
- XDXR stream records and Set64BitMortonCodes tune the console builder's
  heuristics and are ignored (logged once);
- prebuild sizes and copies are the layer's;
- the builder's state is kept in a table keyed by `this`: nothing is
  written into the title's object, whose size isn't known;
- rehydration answers E_NOTIMPL for now (stage 2);
- LEONIDA_BVH_BUILDER=console keeps the bundled DLL.

xbox_bvh_builder_test (12 checks) calls the exports through the shim
registry as the title binds them; xdxr_bvh.py check-build validates the
BLAS, TLAS and compacted BLAS it builds (0/1000 rays differ on each).

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

The GPU side of XboxBvhBuilder.dll's RehydrateRaytracingAccelerationStructure*X
(docs/bvh.md §4a), for the shim's rehydration export (RT-5, after ItsNotRexed#7516).
src/xdxr_rehydrate.comp follows tools/bvh_rehydrate.py, the model that matches
the DLL byte for byte, step for step and with its float32 rounding:

- a one-thread plan pass reads the dehydrated header and level table from GPU
  memory (they may be written by earlier GPU work) and writes the indirect
  dispatch arguments of every later pass;
- clear, kept header (dehydrated flag cleared, root parent entry), leaves (one
  thread each, through the leaf frame of compressionParams), then each level's
  BOX32 and BOX16 groups bottom-up (one workgroup each: boxes recomputed from
  the rehydrated children, stored overrides, BOX16 rounding, parent entries);
- every node and leaf is written where its packed bytes are, so the result has
  exactly the dehydrated size (the size the title allocates).

src/xdxr_rehydrate.h records it (XdxrRehydrator): the rotation and geometry
tables go into the plan with vkCmdUpdateBuffer, then vkCmdDispatchIndirect per
pass. The source and the destination must be different buffers.

vk_xdxr_rehydrate_test compares the GPU's result with the model byte for byte
on synthetic cases from runtime/test/xdxr_rehydrate_cases.py (several levels of
both widths, >64 nodes a level, two leaf chunks, overrides, paired leaves,
u16/u32/no indices, a stride, a transform, the leaf frame with rotations,
updatable parents), checks the guard past the structure and a structure that
isn't dehydrated. On an own 1.0.2.1 copy (local only, counts only): 400 real
streams of shared_minigame000_bvh4.rpf and 400 of core_bvh4.rpf (1 to 15
levels, 1.5 M leaves, leaf frame on every one), with random geometry, are all
identical to the model.

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

The shim's rehydration export (4 call sites in the executable) now records the
GPU rehydration of the previous commit instead of answering E_NOTIMPL:

- command_list.h: RehydrateAccelerationStructure (source, destination,
  scratch for the plan, the source geometry);
- d3d12_com.cpp: the layer's lists answer a private ILeonidaXdxrRehydrate
  (shims/d3d12_xdxr_rehydrate.h), so the shim needs no link to the layer; a
  description it can't record is E_INVALIDARG and leaves the list usable;
- vulkan_replay.h / vulkan_backend.h: the command is prepared (the scratch's
  buffer for the indirect dispatches, its room for the plan) and recorded by
  XdxrRehydrator at the arena's addresses. A rehydration the layer can't do
  (a geometry that isn't R32G32B32_FLOAT, a scratch too small for the plan)
  is skipped with a log line instead of failing the whole list;
- the export passes each element to the list and returns the first failure;
  on a list that isn't the layer's (the Windows D3D12 backend) it is
  E_NOTIMPL. The per-element UINT64 argument isn't measured: its first values
  are logged once and it is not used.

xbox_bvh_builder_test rehydrates every synthetic case of
runtime/test/xdxr_rehydrate_cases.py through the shim on the layer's Vulkan
device and compares the result with tools/bvh_rehydrate.py: 0 bytes differ on
each. docs/bvh.md §7b and docs/xdxr.md §4a describe it.

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

@tupacfan04 tupacfan04 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 synthetic/GPU rehydration is clean: 24/24 xdxr_rehydrate checks, 6/6 CTests, 0/1000 rays differ, and all five cases byte-exact against bvh_rehydrate.py. There is one real integration defect.

The private rehydration interface never reaches the layer through the d3d12_xs list proxy:

  • runtime/shims/xbox_bvh_builder.cpp:237-241 asks the title’s command list for ILeonidaXdxrRehydrate.
  • In the boot stack the title’s lists are ListProxy objects created through device slot 73. ListProxy::QueryInterface (runtime/shims/d3d12_xs_device.cpp:1480-1492) answers only console-list IIDs and the public list chain; every other IID returns E_NOINTERFACE and is not forwarded to self->real.
  • The shim therefore takes its “the list is not the layer’s” branch and returns E_NOTIMPL for any proxy-created list, even though the real layer list implements the interface.

The tests pass because they pass the real layer list directly; they never construct a ListProxy.

Fix: forward IID_ILeonidaXdxrRehydrate (or unknown private IIDs) to self->real, return the real interface with its reference, or add an unwrap helper for the shim. Add a synthetic test that gives the shim a ListProxy. The rehydration plan/header/leaf/BOX32/BOX16 path itself has no defect.

Synthetic/GPU only; no game files. AI-assisted (OpenCode).

…he proxy path

From @KarolKurzyna's review of 242a5db:
- builder_device() returned the builder's device without a reference, so a
  destructor on another thread could release it during
  GetRaytracingAccelerationStructurePrebuildInfo. It now takes a reference
  under the lock, and the caller releases it after the call.
- The test only passed an unwrapped device. It now also constructs a builder
  on a d3d12_xs device proxy (create_device_proxy, as D3D12XboxCreateDevice
  hands out) and checks that its prebuild sizes are the real device's
  (13 checks).

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

Copy link
Copy Markdown
Contributor

Synthetic ListProxy repro confirms the QueryInterface boundary:

This confirms the defect is the proxy QI boundary, not the GPU rehydration or the shim’s element loop. The 6/6 tests pass because they pass the real layer list directly; they never construct a ListProxy. Fix the QI forwarding (or add an unwrap helper) and add the synthetic proxy test. No game data/traces. AI-assisted (OpenCode).

romainveurrier added a commit to romainveurrier/Leonida that referenced this pull request Nov 23, 2026
… its interfaces

From @tupacfan04's review of ItsNotRexed#7903: on a boot stack with the console list
proxy (ItsNotRexed#7076), the title's lists are proxies whose QueryInterface answers
only the console and public list IIDs. The shim asked the proxy itself, so a
private interface was refused, and a PC interface it did answer would have
been called through a console-shaped vtable.

d3d12_xs_device exposes real_object_behind_proxy(), the real object behind any
child proxy (proxy_real_object, which a console list proxy joins, as the file
and ItsNotRexed#7076 say), and the shim asks that real list for
ID3D12GraphicsCommandList4. On main lists aren't wrapped, so nothing changes
there; with ItsNotRexed#7076 the shim follows its lists without a further change.

docs/xdxr.md: BuildRaytracingAccelerationStructureBatched2X is 6 call
[rip+disp32] sites through the import table; IDA counts 8 direct calls, two
of them in another form (@FJKiXfaR on ItsNotRexed#7516).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
romainveurrier and others added 2 commits November 23, 2026 19:11
… its interfaces

From @tupacfan04's review of ItsNotRexed#7903: on a boot stack with the console list
proxy (ItsNotRexed#7076), the title's lists are proxies whose QueryInterface answers
only the console and public list IIDs. The shim asked the proxy itself, so a
private interface was refused, and a PC interface it did answer would have
been called through a console-shaped vtable.

d3d12_xs_device exposes real_object_behind_proxy(), the real object behind any
child proxy (proxy_real_object, which a console list proxy joins, as the file
and ItsNotRexed#7076 say), and the shim asks that real list for
ID3D12GraphicsCommandList4. On main lists aren't wrapped, so nothing changes
there; with ItsNotRexed#7076 the shim follows its lists without a further change.

docs/xdxr.md: BuildRaytracingAccelerationStructureBatched2X is 6 call
[rip+disp32] sites through the import table; IDA counts 8 direct calls, two
of them in another form (@FJKiXfaR on ItsNotRexed#7516).

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

From @tupacfan04's review: a console list proxy (ItsNotRexed#7076) answers only console
and public list IIDs, so the rehydration export took its "not the layer's
list" branch (E_NOTIMPL) for every proxy-created list. It now asks the list
real_object_behind_proxy() gives, as list4_of does.

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

Copy link
Copy Markdown

Independent native Windows verification of 093259a.

The second check uses a synthetic COM backend on llvm-mingw, with the adapter-LUID SDK signature adjusted in the temporary build. The Vulkan test was compiled unchanged. AI-assisted.

@romainveurrier

Copy link
Copy Markdown
Contributor Author

@tupacfan04, right, and thanks for the repro. It's in both stages. list4_of (builds and copies, stage 1) asked the proxy too, and would have called the PC interface through a console-shaped vtable.

ListProxy isn't on main (lists aren't wrapped there); it comes with #7076, which routes lists through proxy_real_object. So the fix uses that hook:

On main nothing changes; with #7076 the shim follows its lists automatically. A test with a real ListProxy needs #7076. I'll add one when it lands, or @NiiRoZz could add it there. AI-assisted.

@romainveurrier

Copy link
Copy Markdown
Contributor Author

@Racurs156, thank you, that's the test I said would have to wait for #7076. With a real ListProxy from #7880:

  • the old head gave E_NOTIMPL with zero calls;
  • 093259a delivers all three elements in order, passes back the injected E_INVALIDARG, and leaves the private reference count balanced.

That covers @tupacfan04's finding end to end. Also thanks for the native Windows run with the validation layer on: 24/24, byte-exact, 0 diagnostics. 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.

3 participants