Skip to content

tools/bvh_rehydrate.py: CPU reference that rehydrates a dehydrated BLAS (PROC-114) - #2205

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
wildheathen:tools/bvh-rehydrate
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
wildheathen:tools/bvh-rehydrate

Conversation

@wildheathen

Copy link
Copy Markdown
Contributor

PROC-114. Claim: #2078 (comment)

Depends on: #2034. The tool follows the packing that PR documents in docs/bvh.md §4a.

tools/bvh_rehydrate.py is a standard-library CPU reference that turns a dehydrated XDXR BLAS, as stored in the *_bvh4.rpf packs, back into the runtime layout. The input geometry is float3 vertices with uint16, uint32 or no indices, plus an optional 3x4 geometry transform (the instance descriptor's +0x50 matrix).

It does:

  • the header copy;
  • the leaf chunks: vertex-order slots, and the fourth vertex for a paired primitive;
  • the box groups, level by level, with their stored overrides;
  • BOX16 outward rounding, with +-inf on overflow;
  • the parent entries;
  • the compressionParams leaf frame.

Every float operation is rounded to float32, so the output is meant to be byte-exact.

Why it is useful: it is the reference for the builder's own Box16Rehydration, Box32Rehydration and LeafRehydration programs. Once #1968 and #1864 land, those can run on a GPU. My test on the real programs is in #1968 (comment).

Licence: the tool is written from the §4a spec. No code comes from americas-recomp, which docs/d3d12_vulkan.md §7.3 rules out.

Testing

  • python -I tests/test_bvh_rehydrate.py runs 15 synthetic tests and passes. They cover single and paired leaves, vertex-order slots, the fourth-vertex choice, no indices / 16-bit indices / stride, the geometry transform, the second 2,048-leaf chunk, BOX32 bounds with parents and overrides, BOX16 rounding and overflow, the leaf frame's scale and translation, the rotation table, errors and the command line.
  • test_bvh_index, test_xdxr_bvh, test_docs_links, test_docs_repo_paths, test_docs_list_items and test_text_write_line_endings pass. python -m compileall is clean.
  • Locally, with the retail package, on the 8 BLAS whose geometry is identified: the output is byte-identical to what XboxBvhBuilder.dll's RehydrateRaytracingAccelerationStructureX writes (384 to 2,512,512 bytes, 0 differing bytes each). No sample or output is in the PR. This ran on the CPU only, on Windows 11.

One thing this surfaced (not changed here): the console's size convention differs from this repo's builder.

  • In the console's structures the whole buffer is 64 * (totalSizeIn64BNodes + 1) bytes from the header. That matches §3.1, where the end is 0x140 + 64 * n with the header at 0x100.
  • tools/xdxr_bvh.py validate and the GPU builder (xdxr_build.comp:439, total_size / 64) use 64 * totalSizeIn64BNodes for the whole structure.
  • As a result, validate rejects all 8 rehydrated console structures with "totalSizeIn64BNodes n does not cover the buffer".

That is consistent inside Leonida, but a copy or compaction that sizes a console-built structure from its header would be one unit short. I'll post it as a finding for the builder's owner rather than change it here.

AI-assisted.

…BLAS

Written from the packing in docs/bvh.md §4a, standard library only. Inputs are float3 vertices with uint16, uint32 or no indices, and an optional 3x4 geometry transform. It does the header copy, the leaf chunks (vertex-order slots, the paired fourth vertex), the box groups by level with their stored overrides, BOX16 outward rounding with the +-inf overflow, the parent entries and the compressionParams leaf frame. Float arithmetic is rounded to float32 per operation. On the 8 measured samples whose geometry is identified, the output is byte-identical to XboxBvhBuilder.dll's RehydrateRaytracingAccelerationStructureX (checked locally; no sample is in the repository). The 15 tests in tests/test_bvh_rehydrate.py are synthetic. docs/bvh.md §7 documents it, including the console's size convention, 64 * (totalSizeIn64BNodes + 1) bytes, which xdxr_bvh.py validate reads as one unit less.
@UNYTIX

UNYTIX commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested db4be7ae6 with Python 3.15.2.

python -B -I tests/test_bvh_rehydrate.py -v

All 15 tests passed with synthetic BVH cases. This checks the CPU reference, with no external binary comparison.

Independent Windows 11 validation. AI-assisted.

A redirected stdout on Windows is encoded with the console's ANSI code page, and `python -I` ignores
PYTHONUTF8, so a path outside that code page ended the run. tests/test_tool_stdout_encoding.py
requires the guard in every tool's __main__ block, and it fails on this branch without it.

Synthetic data only; no game files.
@wildheathen

Copy link
Copy Markdown
Contributor Author

Pushed dab5e2c: the sys.stdout.reconfigure(errors="replace") guard in the __main__ block, which tests/test_tool_stdout_encoding.py requires of every CLI tool and which was missing here, so that test failed on this branch. Nothing else changed; tests/test_bvh_rehydrate.py still passes.

AI-assisted.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Independent counts-only CPU validation of dab5e2c on Windows. I scanned one complete retail pack with bvh_index, ran Header and leaf_records on every complete BLAS, and ran levels on box-root BLAS. Of 2,560 compressed streams, 1,237 classified as complete BLAS. All 1,237 yielded their declared leaf count and completed the applicable level-table parse.

Across 8,213,842 leaf records, 7,078,883 carried a second primitive. 686 BLAS referenced multiple geometry indices and 414 spanned multiple 2,048-leaf chunks. The level tables described 226,076 BOX16 and 2,965,004 BOX32 nodes.

This measures structural decoding only. Geometry matching and runtime output equality were outside this run. AI-assisted.

@throny

throny commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

For context: TEST-376 (running XboxBvhBuilder.dll's Box16Rehydration, Box32Rehydration and LeafRehydration on a GPU) waits on this PR, because tools/bvh_rehydrate.py is the CPU reference the GPU output gets compared against. Its other blocker is #1864 (buffer_store_short for Box16Rehydration). #2034, which this builds on, merged today. On my own copy, all three programs translate once #1864 is in (status: #2300 (comment)). When this lands, the GPU comparison follows under TEST-376. AI-assisted.

@wildheathen

Copy link
Copy Markdown
Contributor Author

Two corrections to the sequencing, from my side:

Two things that cost time on these samples:

  • a whole bvh4 stream is passed with --header-offset 256; a structure carved from its own 64-byte header takes 0;
  • the instance transform is the 3x4 at +0x50 of the 208-byte descriptor, passed with --transform; without it a rotated sample is rejected by the checks rather than mis-compared.

If you need the geometry buffers for a sample, --write in #2712 writes float3 and uint32 buffers plus an export.json, which is the shape bvh_rehydrate.py takes. tools/bvh_geometry_check.py (#2428) says whether the geometry fed to the comparison is the one the BLAS was built from. Neither replaces the byte comparison; they catch a wrong input before it is measured.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit f3fcda7 into ItsNotRexed:main Nov 22, 2026
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
…built from

Reads the leaf signature (triangles per geometry index, a leaf that holds two primitives counting
as two), keeps every (drawable, model) of an `rpf9dump models` manifest whose per-geometry triangle
counts contain it as an ordered subsequence, and checks the survivors with
tools/bvh_geometry_check.py from the model's exported OBJ and the 3x4 at +0x50 of the instance
descriptor. The geometries of the two shaders the BVH leaves out are filtered with --skip-shaders
(those name hashes are game data, so the rule is off unless the user passes them); a drawable whose
shader indices the manifest cannot resolve keeps every geometry and the checks decide.

All the BVH logic stays in Python: the C# side is only the model decoding and the export.

Measured on the ten BLAS of the sample pack against one 1 GiB window of levels.rpf: 1,228 models
exported, 1 to 374 candidates per BLAS from the signature, and exactly one consistent candidate
for each of the ten. The tool prints no drawable names unless --names is given.

The two tools that came with #2205 and #2428 lacked the stdout errors="replace" guard every CLI
tool needs (tests/test_tool_stdout_encoding.py), so this also adds it there; without it that test
fails for both.

Tests are synthetic (tests/test_bvh_match.py, 14 cases): the right geometry is found and verified,
a wrong one, a wrong shader rule and a missing transform are not, geometries are matched inside one
model, and a manifest without per-geometry counts is an input error.
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.

5 participants