Skip to content

bvh_rehydrate: a finite box bound past the half range keeps 65504, not an infinity - #8450

Open
ne1i wants to merge 1 commit into
ItsNotRexed:mainfrom
ne1i:bvh-rehydrate-half-range-inward-bound
Open

ne1i wants to merge 1 commit into
ItsNotRexed:mainfrom
ne1i:bvh-rehydrate-half-range-inward-bound

Conversation

@ne1i

@ne1i ne1i commented Nov 23, 2026 •

Copy link
Copy Markdown

half_down and half_up are documented as "largest half not above x (a box16 minimum)" and "smallest half not below x (a box16 maximum)". Both ended in float_to_half, whose overflow saturates to the infinity on the side of the magnitude. So on the inward side a finite bound came back infinite: half_down(70000.0) was 0x7c00 (+inf) where the largest half not above it is 65504 (0x7bff), and half_up(-70000.0) was 0xfc00 where it should be 0xfbff. A node whose whole box lies above the half range was then written with minimum [inf, inf, inf], which is the value unused slots use for an empty box.

The inward sides now stop at ±65504. Unchanged: infinite bounds still give infinities, the outward sides still saturate to the infinity (half_down(-70000), half_up(70000)), and every in-range value.

tools/xdxr_bvh.py half_round states the same rule in its docstring ("±inf on their outward side ... and ±HALF_MAX on their inward side, so an empty box stays empty"), so the two builders now agree for finite bounds. Differences left alone on purpose: the sign of a zero result; the infinite inward sides (half_down(+inf) stays +inf and half_up(-inf) stays -inf here, where half_round gives ±65504); and, for the same reason, the words of an empty box (±inf here, ±65504 from encode_box16). Both encodings of an empty box are empty (min > max).

Scope, plainly: this only matters for geometry more than 65504 units from the origin on some axis. I have not checked whether any real scene has such bounds.

Tested on Linux, CPython 3.14, CPU only, synthetic geometry: python -I tests/test_bvh_rehydrate.py 19 OK; with only the tool reverted, the new test fails (31744 != 31743). compileall, check_no_game_data.py and git diff --check are clean. The agent also compared the two helpers with xdxr_bvh.half_round over about 42,000 values each; I did not repeat that comparison.

Claim: #8047.

AI-assisted (DeepSeek V4.1 Flash wrote it; gated and re-run with Claude Code).

@neoisg8

neoisg8 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested d94dee4b on Windows 11 (Python 3.14.3, CPU-only, no game files): pass.

Test Suite Execution

  • python -I tests/test_bvh_rehydrate.py -v: 19/19 passed in 0.09s.
    • Passes new test test_box16_bound_past_the_half_range_keeps_a_finite_value: confirms half_down(70000.0) == 0x7BFF (65504), half_up(-70000.0) == 0xFBFF, and that a leaf whose geometry box exceeds the half range preserves a finite minimum instead of decaying into an empty-box infinity.

Negative Control

Reverted tools/bvh_rehydrate.py to origin/main while testing against tests/test_bvh_rehydrate.py:

  • python -I tests/test_bvh_rehydrate.py -v reproduces FAILED (failures=1):
    • AssertionError: 31744 != 31743 at line 190 (0x7C00 [+inf] != 0x7BFF).
      Restoring d94dee4b cleanly restores 19/19 OK.

Hygiene Checks

  • python -m compileall -q tools/bvh_rehydrate.py tests/test_bvh_rehydrate.py: clean.
  • python -I scripts/check_no_game_data.py tools/bvh_rehydrate.py tests/test_bvh_rehydrate.py: clean, no game data.
  • git diff --check: clean.
  • Line endings: strict Unix LF (\n) preserved.

LGTM!

AI-assisted: verified and reviewed on Windows 11 environment.

@Grimkeh

Grimkeh commented Nov 23, 2026

Copy link
Copy Markdown

Tested d94dee4bd on Linux (CPython 3.13), CPU-only, synthetic inputs, no game files:

python -I tests/test_bvh_rehydrate.py → 19/19 OK. Pass.

AI-assisted (Cursor / Composer).

@molodayamama

Copy link
Copy Markdown

Verified commit d94dee4bdca29a8c82f7b9709164792568c227f8. All 19 BVH rehydration tests passed on Windows. The added fixture confirms that rounding a finite positive bound downward preserves the largest finite half and that the negative round-up case is symmetric. Real infinities and unused bounding-box slots retain their sentinel encodings.

AI-assisted verification.

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

Independent Linux CPU review (d94dee4b)

What changed: half_down / half_up clamp a finite float that floors/ceils past the half range to ±65504 instead of becoming ±Inf, so a box bound just outside the half range stays finite.

Checked:

  • Diff is two clamps + HALF_MAX (+ tests).
  • python3 -I tests/test_bvh_rehydrate.py → 19 OK.

Looks correct; no game files.

@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 finite clamp is correct: half_down/half_up agree with xdxr_bvh.half_round for 65504/65505/65520/65536/70000; half_down(-70000)/half_up(70000) still go outward to infinities; the in-range leaf test is fine; NaN is 0x7E00 in both. The infinite inward sides and empty box still differ:

input bvh_rehydrate xdxr_bvh.half_round
+inf, down 0x7C00 (+inf) 0x7BFF (65504)
-inf, up 0xFC00 (-inf) 0xFBFF (-65504)

tools/bvh_rehydrate.py:128-129,143-144 deliberately do not clamp an infinite input; the new test asserts both bvh_rehydrate values (tests/test_bvh_rehydrate.py:197,200). The PR body accounts only for half_down(+inf), not half_up(-inf), and tests/test_xdxr_bvh.py:97 explicitly asserts the opposite for both inward infinite sides.

The empty box is the same discrepancy: tools/bvh_rehydrate.py:394 writes [HALF_POS_INF]*3 + [HALF_NEG_INF]*3 (±inf), while xdxr_bvh.py's empty box via encode_box16 produces 0x7BFF/0xFBFF. Both boxes are empty (min > max) but the BOX16 words differ; the new test does not hang an unused BOX16 child or compare against xdxr_bvh, so the body's “the two builders now agree here” is true only for finite bounds.

Either align the infinite inward sides and empty box with xdxr_bvh (make half_down(+inf)/half_up(-inf) return ±HALF_MAX and use that for the empty slot), or keep the sentinel and list half_up(-inf) as a deliberate difference, and add an unused BOX16 child test/cross-check. The exact finite boundary half_up(65504)/half_down(-65504) is not directly tested (it is correct); NaN is not tested but both return 0x7E00. Synthetic only; no game files. AI-assisted (OpenCode).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown
Author

Note for whoever merges: #7814 (the bvh_rehydrate speed-up, older than this PR) rewrites the box writer around the same half_down/half_up call site, and a trial merge of the two conflicts in tools/bvh_rehydrate.py (an agent's trial merge; I did not repeat it). Different purposes, so not a duplicate; I will rebase this one if #7814 lands first.

@rideinstead

Copy link
Copy Markdown

TEST validation — Approve

Validated on CPU (no GPU, no game files). Pure Python unit test on synthetic geometry.

Tests added: test_box16_bound_past_the_half_range_keeps_a_finite_value in tests/test_bvh_rehydrate.py — tests half_down/half_up clamping at past ±65504 (HALF_MAX), infinite sentinel behavior, and end-to-end leaf rehydration. 19 tests total (18 pre-existing + 1 new).

Verification:

  • Author (Linux, CPython 3.14, CPU only): python -I tests/test_bvh_rehydrate.py → 19 OK
  • neoisg8 (Windows 11, Python 3.14.3, CPU-only): 19/19 passed; negative control (reverted tool) reproduces the failure 31744 != 31743
  • Grimkeh (Linux, CPython 3.13): 19/19 OK
  • molodayamama (Windows): all 19 passed
  • Hygiene: compileall, check_no_game_data.py, git diff --check, strict Unix LF — all clean

Note: Reviewer tupacfan04 flagged pre-existing differences with xdxr_bvh.half_round for infinite inward sides and empty-box encoding, but these are deliberately preserved and not introduced by this PR.

Recommendation: Approve / merge. Fully CPU-validatable, negative control confirms the test catches the regression, reviewed by 4 people across Linux and Windows.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested d94dee4b on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_bvh_rehydrate.py -> OK

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks, that is a fair correction of my PR body. The fix here is only the finite case, and I have reworded the body to say so.

What is and is not aligned with xdxr_bvh.half_round after this PR:

  • Finite bounds past the half range: aligned (inward side stops at ±65504, outward side goes to the infinity).
  • Infinite inward sides: still different, on purpose. half_down(+inf) stays 0x7C00 and half_up(-inf) stays 0xFC00 here, where half_round gives 0x7BFF / 0xFBFF. My body named only the first of the two; both are deliberate.
  • The empty box: same difference (±inf here, ±HALF_MAX from encode_box16). Both are empty (min > max), but the words differ.

I kept the infinite sentinel because this tool already writes [+inf]*3 + [-inf]*3 for unused slots and changing what an infinite bound encodes to would change every rehydrated file's unused children, which is a wider change than this bug and one I cannot check against real data. If maintainers want the two builders to produce identical words for empty boxes too, I think that deserves its own PR with a cross-check against xdxr_bvh, and I am happy to do it.

AI-assisted (reply written with Claude Code).

@wildheathen

Copy link
Copy Markdown
Contributor

Tested d94dee4bd on Windows 11, Python 3.14 (CPU only), with real-data context for the scope question in the description. Counts only, from my own 1.0.2.1 copy.

Security pass first: clean. The change is two clamps in tools/bvh_rehydrate.py plus a synthetic test.

  • python -I tests/test_bvh_rehydrate.py: 19 tests OK.
  • No change on the known samples: 8 console BLAS rehydrated with main and with this head are byte-identical, so they still match the library's CPU path.
  • Does the case occur? I mapped each indexed bvh4 stream's sceneBounds through its leaf frame, as the BOX16 boxes are written:
pack streams past ±65504 in object space past ±65504 in the leaf frame
shared_minigame000_bvh4 1,237 0 0
core_bvh4 271,627 0 265

All 265 have a BOX16 root and a large frame scale (exponents up to 2^19 on an axis): small objects scaled until their bounds pass the half range. In none does the whole scene lie past 65504 on an axis; it straddles the limit. So the inward-side case is reachable for a child box near the far corner of those 265 trees. Whether any child box really lies wholly past 65504 depends on the geometry, which I haven't matched for those streams.

  • What the console writes there: the 2,027 overrides stored in those 265 streams are all BOX32 values, none at or past ±65504. The BOX16 boxes are derived during rehydration, not stored, so the dehydrated data can't show whether the console writes 65504 or an infinity on that inward side. This PR's rule (65504, as xdxr_bvh.half_round states) is the one that keeps such a box non-empty.

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.

8 participants