Skip to content

tools/bvh_geometry_check.py: check a BLAS rehydration's geometry against what the BLAS stores - #2428

Merged
ItsNotRexed merged 4 commits into
ItsNotRexed:mainfrom
wildheathen:tools/bvh-geometry-check
Nov 22, 2026
Merged

ItsNotRexed merged 4 commits into
ItsNotRexed:mainfrom
wildheathen:tools/bvh-geometry-check

Conversation

@wildheathen

Copy link
Copy Markdown
Contributor

Claim: #2300 (comment) (new item).

Depends on: #2205, which adds tools/bvh_rehydrate.py. This PR is built on its branch, so the diff shows that commit until #2205 lands. Also #2034, for the §4a it refers to.

tools/bvh_geometry_check.py says whether the geometry given to a BLAS rehydration is the geometry the BLAS was built from. A byte-identical library result can't tell you that, because the library and the CPU reference agree on any input. This tool uses what the dehydrated stream stored from the true triangles:

  • Bounds: every leaf is inside sceneBounds mapped through the leaf frame, and the union of the leaves equals it.
  • Overrides: each stored box override lies inside the range of the rehydrated slot it overrides and is no looser than it, within 4 float32 ulps.
  • Pairs: the two primitives of a paired leaf share an edge in the index buffer. This needs no positions and no transform.
  • Area: the internal box area, as a tie-break between candidates.

It is meant for TEST-376, to say whether the geometry fed to the rehydration programs on a GPU is right, and for anyone matching packs to drawables.

Testing

  • python -I tests/test_bvh_geometry_check.py: 6 synthetic tests pass. They cover the right geometry, a shifted one, a smaller one, a re-indexed one caught by pairs alone, overrides within and beyond tolerance, and the CLI.
  • test_bvh_rehydrate, test_docs_links, test_docs_repo_paths and test_text_write_line_endings pass, and compileall is clean.
  • Locally, with the retail package, on the 8 identified BLAS:
    • the right geometry passes every check: bounds gap 0 to 1.7e-7, 427 of 427 overrides consistent, every pair shares an edge (15,274 of 15,274 on the largest);
    • a wrong position decode fails: 175 of 181 or 207 of 273 leaves outside, or a bounds gap of 5.7e-4 with 11 of 23 overrides inconsistent.
  • No data is attached. CPU only, Windows 11.

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.
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.
…hat the BLAS stores

Bounds (leaves inside and equal to sceneBounds in the leaf frame), stored box overrides (inside the rehydrated slot and no looser, within 4 float32 ulps), paired leaves sharing an edge in the index buffer (position-free) and the internal box area as a tie-break, from docs/bvh.md §4a. On the 8 local samples the right geometry passes everything and a wrong decode fails. Six synthetic tests.
… must

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
wildheathen force-pushed the tools/bvh-geometry-check branch from 190bb44 to 3557964 Compare November 22, 2026 17:49
@wildheathen

Copy link
Copy Markdown
Contributor Author

Pushed 3557964: rebased onto #2205's dab5e2c (which adds the same stdout guard to bvh_rehydrate.py, and this branch carries that file) plus the guard for bvh_geometry_check.py. tests/test_tool_stdout_encoding.py passed for every tool that needs it on this branch before it, hence the push; tests/test_bvh_geometry_check.py and tests/test_bvh_rehydrate.py still pass. No other change.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 72a1fe7 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.
@mesterember104-cpu

Copy link
Copy Markdown

Reviewed 3557964 (own commits dab5e2c, 7b5ac4d, 3557964): clean, one non-blocking suggestion.

With #2205 now merged, the diff against main is this PR's own work: tools/bvh_geometry_check.py (+194), tests/test_bvh_geometry_check.py (+89), docs/bvh.md (+11) and a 4-line stdout tweak in tools/bvh_rehydrate.py. What I covered, reading the diff against the dependency:

  • The override parser mirrors write_box_groups exactly. stored_overrides() reproduces the dependency's layout walk one-for-one — same levels() level table, same group base (offset_to_nodes + start + g * (1<<13 / 1<<12)), same child/value cursor arithmetic (value_at = base + 4*(n + sum(w >> 24))), same 24-bit mask → component mapping (bit % 6 = lo/hi per axis, bit // 6 = child slot, guard bit // 6 < kids skips pad slots). So the check compares what the dehydrated stream really stores, not a misreading of it. The rehydrated side (R.node_bounds) is the same function rehydration itself writes through, so both sides of the comparison are consistent.
  • The bounds check compares like with like. Leaf AABBs come from the rehydrated vertices (already mapped through LeafFrame by write_leaves), and sceneBounds corners are mapped through the same frame before the min/max box is taken. The union-equality term is what makes this more than a containment test — test_smaller_geometry_inside_the_bounds_still_fails pins exactly that (0 leaves outside, still fails via the gap).
  • The 4-ulp override tolerance is right for both encodings. Wide (BOX32) stored overrides are float32 like the rehydrated bounds, so ±1–2 ulps of offline rounding is what the tolerance absorbs. Narrow (BOX16) is safe by construction rather than by tolerance: rehydrate rounds slot bounds with half_down/half_up, so the rehydrated box is a superset of the true one and any stored half-precision override can only be tighter or equal — test_overrides covers both directions plus the within-tolerance case.
  • internal_area is a reported tie-break only (not part of the verdict), correctly guarded against pad children (INVALID, inf bounds) and only counted over box nodes.
  • CLI behaviour matches the docstring: exit 0 consistent / 1 not / 2 on input error (OSError, ValueError, struct.error caught; the no---geom case exercises 2 in the CLI test), JSON report written with newline="\n", and both touched tools got the sys.stdout.reconfigure(errors="replace") idiom every other CLI tool in tools/ carries — which is what the two extra commits on top of the tool add to bvh_rehydrate.py.
  • Docs row (docs/bvh.md, after the rehydrate section) states the three verdict-bearing checks and quotes the same failure numbers as the description (175 of 181 leaves outside, 11 of 23 overrides inconsistent); the command examples match the CLI. No game data anywhere; python -I-safe; stdlib-only in both files; no runtime C++ touched.
  • Ran locally (Windows 11, Python 3.14, python -I, worktree at 3557964 on top of main):
    • python -I tests/test_bvh_geometry_check.py: 6/6 OK.
    • python -I tests/test_bvh_rehydrate.py (dependency touched by the stdout commit): 15/15 OK.
    • python -I tests/test_ctest_registration.py 4/4, test_docs_links.py 5/5, test_docs_repo_paths.py 2/2, test_text_write_line_endings.py 2/2 — the suites the description lists, plus the registration check.
    • python -I -m compileall -q make_game.py tools tests: clean.
    • The description's retail-package numbers are CPU-side counts from the author's own copy; I could not re-derive those here (no package), but the synthetic tests pin each check's pass and fail direction independently.

Non-blocking suggestion: the pairs check needs an index buffer, and the tool accepts --geom VB without one. Without an IB, Geometry.triangle() falls back to implicit adjacency (3*prim, 3*prim+1, 3*prim+2), whose index sets are always disjoint — so every paired leaf fails the pairs check and the verdict is NOT consistent even for perfectly right geometry. Consider skipping the pairs term for index-less geometries (or printing a hint next to the pairs sharing an edge: no pairs line) so a VB-only run can't read as a geometry failure.

AI-assisted (GLM, operated by the account owner).

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