xbox_bvh_builder: scan every postbuild description for 0x10; rehydrate scratch regions cannot wrap (RT-XDXR-POSTBUILD-SCAN-AND-SCRATCH) #2544

Merged
leonida/mandy
Tyrannys merged 1 commit from rt-xdxr-postbuild-scan-scratch into main 2026-11-29 01:03:53 +00:00

Pull request successfully merged and closed

Tyrannys commented 2026-11-28 01:39:24 +00:00

Claimed in the coordination issue as: RT-XDXR-POSTBUILD-SCAN-AND-SCRATCH (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231769520)

What and why

Item 3 of @kevincommits' Finding 19 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231649637). Thanks for the report.

runtime/shims/xbox_bvh_builder.cpp serves XboxBvhBuilder.dll's exports with the layer's own builder:

  • builder_build_batched2x, postbuild descriptions past 16. The DLL refuses an element that asks for the offline-statistics postbuild type (0x10), so the shim skips such an element. But the shim kept 16 other descriptions and then break. A 0x10 description at index 17 or later was never seen, and the element was built instead of skipped. Now the descriptions past 16 are still dropped (logged once) but scanned, so 0x10 at any index skips the element.
  • builder_rehydrate_batched2x, scratch regions. Each element with no scratch gets a region of one shared scratch buffer. It was sized as (size + 255) / 256 * 256 and summed without a bound. A size within 255 of 2^64 wrapped to 0, and a large total wrapped to a small one. The buffer was then too small and the regions overlapped. Now an element whose aligned region would not fit in the 64-bit total gets no region: it is skipped as one without scratch, with a log line, and the rest of the batch is unaffected.

Both rules move unchanged otherwise into a small header, shims/xbox_bvh_builder_rules.h (filter_postbuild, add_scratch_region), so they can be tested without a device.

Overlap: no open PR touches xbox_bvh_builder.cpp.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 431ceba4f.

  • New CPU test xbox_bvh_builder_rules:
    • 20 descriptions with 0x10 at index 0, 5, 16, 17 or 19: each skips the element, and of the other 19, 16 are kept and the rest dropped;
    • 20 without 0x10: the first 16 kept in order; 3: all kept; no array: nothing;
    • scratch regions aligned in order (size 0 counts as 1);
    • a size within 255 of 2^64 refused;
    • a region that would push the total past 2^64 refused with nothing changed;
    • a region that ends exactly in range fits.
  • With the old break and arithmetic in the header, 5 checks fail (0x10 at 17 and 19, and the three wrap cases). All pass with the change.
  • ctest -R "^xbox_bvh_builder": 4/4 pass, including the GPU test xbox_bvh_builder, its console variant and the xdxr_bvh.py check-build ray check.
  • scripts/check_no_game_data.py: clean.

Not tested: a game boot. No title is known to send more than 16 postbuild descriptions or a scratch size near 2^64; these are guards.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: RT-XDXR-POSTBUILD-SCAN-AND-SCRATCH (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231769520) ### What and why Item 3 of @kevincommits' Finding 19 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231649637). Thanks for the report. `runtime/shims/xbox_bvh_builder.cpp` serves XboxBvhBuilder.dll's exports with the layer's own builder: - **`builder_build_batched2x`, postbuild descriptions past 16.** The DLL refuses an element that asks for the offline-statistics postbuild type (0x10), so the shim skips such an element. But the shim kept 16 other descriptions and then `break`. A 0x10 description at index 17 or later was never seen, and the element was built instead of skipped. Now the descriptions past 16 are still dropped (logged once) but scanned, so 0x10 at any index skips the element. - **`builder_rehydrate_batched2x`, scratch regions.** Each element with no scratch gets a region of one shared scratch buffer. It was sized as `(size + 255) / 256 * 256` and summed without a bound. A size within 255 of 2^64 wrapped to 0, and a large total wrapped to a small one. The buffer was then too small and the regions overlapped. Now an element whose aligned region would not fit in the 64-bit total gets no region: it is skipped as one without scratch, with a log line, and the rest of the batch is unaffected. Both rules move unchanged otherwise into a small header, `shims/xbox_bvh_builder_rules.h` (`filter_postbuild`, `add_scratch_region`), so they can be tested without a device. Overlap: no open PR touches `xbox_bvh_builder.cpp`. ### Tested Windows 11, MSVC (VS 18), RelWithDebInfo, on `main` 431ceba4f. - New CPU test `xbox_bvh_builder_rules`: - 20 descriptions with 0x10 at index 0, 5, 16, 17 or 19: each skips the element, and of the other 19, 16 are kept and the rest dropped; - 20 without 0x10: the first 16 kept in order; 3: all kept; no array: nothing; - scratch regions aligned in order (size 0 counts as 1); - a size within 255 of 2^64 refused; - a region that would push the total past 2^64 refused with nothing changed; - a region that ends exactly in range fits. - With the old `break` and arithmetic in the header, 5 checks fail (0x10 at 17 and 19, and the three wrap cases). All pass with the change. - `ctest -R "^xbox_bvh_builder"`: 4/4 pass, including the GPU test `xbox_bvh_builder`, its console variant and the `xdxr_bvh.py check-build` ray check. - `scripts/check_no_game_data.py`: clean. Not tested: a game boot. No title is known to send more than 16 postbuild descriptions or a scratch size near 2^64; these are guards. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
zmasi commented 2026-11-28 03:18:21 +00:00

#2544 | head e7280be1666f8ed3706a533be527be5bb8d442c4 (merged cleanly with main 02f71ccfe78160997a02950607be583af87e1180) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xbox_bvh_builder_rules|xbox_bvh_builder)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: xbox_bvh_builder_rules_test.cpp fails to compile (C1083: missing xbox_bvh_builder_rules.h; on main, xbox_bvh_builder.cpp stopped scanning postbuild descriptions at n_kept == 16 and could wrap 64-bit scratch_bytes alignment/addition) | green on PR+main: 2/2 CTests pass (xbox_bvh_builder_rules 20/20 CPU checks in 0.05s, and xbox_bvh_builder 26/26 GPU/shim checks in 1.59s on RTX 3090 with 0 Khronos validation errors); test_ci_registration.py 18/18 OK, check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2544 | head e7280be1666f8ed3706a533be527be5bb8d442c4 (merged cleanly with main 02f71ccfe78160997a02950607be583af87e1180) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(xbox_bvh_builder_rules|xbox_bvh_builder)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `xbox_bvh_builder_rules_test.cpp` fails to compile (`C1083`: missing `xbox_bvh_builder_rules.h`; on `main`, `xbox_bvh_builder.cpp` stopped scanning postbuild descriptions at `n_kept == 16` and could wrap 64-bit `scratch_bytes` alignment/addition) | green on PR+main: `2/2` CTests pass (`xbox_bvh_builder_rules` `20/20` CPU checks in `0.05s`, and `xbox_bvh_builder` `26/26` GPU/shim checks in `1.59s` on RTX 3090 with `0` Khronos validation errors); `test_ci_registration.py` `18/18` OK, `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
lixonik approved these changes 2026-11-28 08:51:50 +00:00
lixonik left a comment

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC).

Moving the BVH builder validation/sizing rules into xbox_bvh_builder_rules.h with dedicated CPU rule tests keeps xbox_bvh_builder.cpp cleaner while preserving full GPU BVH builder behaviour.

Tests run

  • ctest --output-on-failure -R "^(xbox_bvh_builder_rules|xbox_bvh_builder|xbox_bvh_builder_check)$": 3/3 passed (xbox_bvh_builder_rules 0.02 s, xbox_bvh_builder 1.43 s, xbox_bvh_builder_check 12.81 s).

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; VK_LAYER_KHRONOS_validation not installed locally).

AI-assisted (Antigravity, Gemini).

Reviewed the diff and tested merged onto `main` (`493867366`) with Ninja Release (MSVC). Moving the BVH builder validation/sizing rules into `xbox_bvh_builder_rules.h` with dedicated CPU rule tests keeps `xbox_bvh_builder.cpp` cleaner while preserving full GPU BVH builder behaviour. ### Tests run - `ctest --output-on-failure -R "^(xbox_bvh_builder_rules|xbox_bvh_builder|xbox_bvh_builder_check)$"`: **3/3 passed** (`xbox_bvh_builder_rules` 0.02 s, `xbox_bvh_builder` 1.43 s, `xbox_bvh_builder_check` 12.81 s). GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; VK_LAYER_KHRONOS_validation not installed locally). AI-assisted (Antigravity, Gemini).
enlight3d commented 2026-11-29 00:47:04 +00:00

RUN (Windows 11, red/green)

Tested heads: PR head e7280be1666f8ed3706a533be527be5bb8d442c4 (one commit on 431ceba4f); main at 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3. git merge-tree --write-tree main pr-2544 merges cleanly.

Machine: Windows 11 Pro 10.0.26300, MSVC 14.44 (VS 2022 Build Tools), RelWithDebInfo, Ninja, 8 jobs. GPU NVIDIA GeForce RTX 3090 24 GB, driver 621.40.

Commands (only the two test targets and their dependencies were built, not the whole runtime):

cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSDL3_DIR=<SDL3 3.4.20 static>/cmake -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build --target xbox_bvh_builder_rules_test xbox_bvh_builder_test -j 8
ctest --test-dir runtime/build -R "^xbox_bvh_builder" --output-on-failure

GREEN (PR head): 4/4 passed: xbox_bvh_builder_rules (new, CPU), xbox_bvh_builder (GPU), xbox_bvh_builder_console, xbox_bvh_builder_check (the xdxr_bvh.py check-build ray check). The new test reports "0 failure(s)".

RED (behavioural): the PR's new header is the whole change under test, so reverting the non-test files would only remove the header and the target. Instead the header runtime/shims/xbox_bvh_builder_rules.h was kept and the two rules put back to the old logic (break after 16 kept descriptions; no overflow checks in add_scratch_region). Rebuilt xbox_bvh_builder_rules_test and ran ctest -R "^xbox_bvh_builder_rules$": FAILED with 5 failures, as the PR says:

  • FAIL 0x10 at index 17 of 20 skips the element
  • FAIL 0x10 at index 19 of 20 skips the element
  • FAIL a size within 255 of 2^64 is refused (its alignment would wrap to 0)
  • FAIL a size whose aligned region would push the total past 2^64 is refused
  • FAIL the next one does not fit, and the total stays
    The edit was reverted afterwards. The rules test covers the header only; I checked that xbox_bvh_builder.cpp calls the header's functions, so the tested code is the code the shim runs.
    Link check: merged into main 1bc055e6c, gta6rt.exe builds and links (RelWithDebInfo, MSVC 19.44); no new imports vs main.
RUN (Windows 11, red/green) Tested heads: PR head `e7280be1666f8ed3706a533be527be5bb8d442c4` (one commit on `431ceba4f`); `main` at `1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3`. `git merge-tree --write-tree main pr-2544` merges cleanly. Machine: Windows 11 Pro 10.0.26300, MSVC 14.44 (VS 2022 Build Tools), RelWithDebInfo, Ninja, 8 jobs. GPU NVIDIA GeForce RTX 3090 24 GB, driver 621.40. Commands (only the two test targets and their dependencies were built, not the whole runtime): ``` cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSDL3_DIR=<SDL3 3.4.20 static>/cmake -DLEONIDA_REQUIRE_VULKAN=ON cmake --build runtime/build --target xbox_bvh_builder_rules_test xbox_bvh_builder_test -j 8 ctest --test-dir runtime/build -R "^xbox_bvh_builder" --output-on-failure ``` GREEN (PR head): 4/4 passed: `xbox_bvh_builder_rules` (new, CPU), `xbox_bvh_builder` (GPU), `xbox_bvh_builder_console`, `xbox_bvh_builder_check` (the `xdxr_bvh.py check-build` ray check). The new test reports "0 failure(s)". RED (behavioural): the PR's new header is the whole change under test, so reverting the non-test files would only remove the header and the target. Instead the header `runtime/shims/xbox_bvh_builder_rules.h` was kept and the two rules put back to the old logic (`break` after 16 kept descriptions; no overflow checks in `add_scratch_region`). Rebuilt `xbox_bvh_builder_rules_test` and ran `ctest -R "^xbox_bvh_builder_rules$"`: FAILED with 5 failures, as the PR says: - FAIL 0x10 at index 17 of 20 skips the element - FAIL 0x10 at index 19 of 20 skips the element - FAIL a size within 255 of 2^64 is refused (its alignment would wrap to 0) - FAIL a size whose aligned region would push the total past 2^64 is refused - FAIL the next one does not fit, and the total stays The edit was reverted afterwards. The rules test covers the header only; I checked that `xbox_bvh_builder.cpp` calls the header's functions, so the tested code is the code the shim runs. Link check: merged into main 1bc055e6c, gta6rt.exe builds and links (RelWithDebInfo, MSVC 19.44); no new imports vs main.
zmasi commented 2026-11-29 01:00:06 +00:00

Windows 11 + MSVC 19.44 + RTX 3090 Verification — PASS (e7280be166)

Verified e7280be166 (RT-XDXR-POSTBUILD-SCAN-AND-SCRATCH) merged onto origin/main (e66c65b950 / 16f5053047, 0 conflicts, plus a 5-way combined merge alongside #1146, #988, #955, and #1738) on native Windows 11 (10.0.26200.9974) with MSVC 19.44.35228, NVIDIA GeForce RTX 3090, and VK_LAYER_KHRONOS_validation 1.4.363.1.

Checks Run

  • Red -> Green CPU rules test (xbox_bvh_builder_rules_test.exe, /W4 /WX):
    • Red (origin/main): runtime/shims/xbox_bvh_builder_rules.h absent (xbox_bvh_builder.cpp previously stopped scanning postbuild descriptors at n_kept == 16, missing 0x10 offline-statistics descriptors at index >= 16, and computed (size + kAlign - 1) / kAlign * kAlign without 64-bit overflow guards).
    • Green (xbox_bvh_builder_rules_test.exe): compiled with 0 warnings under /W4 /WX and passed (0 failure(s), rc=0), covering 0x10 at indices {0, 5, 16, 17, 19} of 20, truncation past 16 without 0x10, nullptr input, and UINT64_MAX alignment/total overflow boundaries.
  • End-to-end xbox_bvh_builder_test & BVH ray-check on NVIDIA GeForce RTX 3090:
    • xbox_bvh_builder_test.exe --console (LEONIDA_BVH_BUILDER=console): rc=0.
    • xbox_bvh_builder_test.exe <cases_dir> on NVIDIA GeForce RTX 3090 (LEONIDA_VK_VALIDATION=1): rc=0, 0 validation errors.
    • python -I tools/xdxr_bvh.py check-build <cases_dir> --rays 1000: rc=0.
  • Registration gates: tests/test_ctest_registration.py and tests/test_ci_registration.py passed (rc=0).

AI-assisted.

## Windows 11 + MSVC `19.44` + RTX 3090 Verification — PASS (`e7280be166`) Verified `e7280be166` (`RT-XDXR-POSTBUILD-SCAN-AND-SCRATCH`) merged onto `origin/main` (`e66c65b950` / `16f5053047`, `0` conflicts, plus a 5-way combined merge alongside `#1146`, `#988`, `#955`, and `#1738`) on native Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228`, **NVIDIA GeForce RTX 3090**, and `VK_LAYER_KHRONOS_validation` `1.4.363.1`. ### Checks Run - **Red -> Green CPU rules test (`xbox_bvh_builder_rules_test.exe`, `/W4 /WX`)**: - **Red (`origin/main`)**: `runtime/shims/xbox_bvh_builder_rules.h` absent (`xbox_bvh_builder.cpp` previously stopped scanning postbuild descriptors at `n_kept == 16`, missing `0x10` offline-statistics descriptors at index `>= 16`, and computed `(size + kAlign - 1) / kAlign * kAlign` without 64-bit overflow guards). - **Green (`xbox_bvh_builder_rules_test.exe`)**: compiled with **`0` warnings under `/W4 /WX`** and passed (`0 failure(s)`, `rc=0`), covering `0x10` at indices `{0, 5, 16, 17, 19}` of `20`, truncation past `16` without `0x10`, `nullptr` input, and `UINT64_MAX` alignment/total overflow boundaries. - **End-to-end `xbox_bvh_builder_test` & BVH ray-check on NVIDIA GeForce RTX 3090**: - `xbox_bvh_builder_test.exe --console` (`LEONIDA_BVH_BUILDER=console`): **`rc=0`**. - `xbox_bvh_builder_test.exe <cases_dir>` on **NVIDIA GeForce RTX 3090** (`LEONIDA_VK_VALIDATION=1`): **`rc=0`**, `0` validation errors. - `python -I tools/xdxr_bvh.py check-build <cases_dir> --rays 1000`: **`rc=0`**. - **Registration gates**: `tests/test_ctest_registration.py` and `tests/test_ci_registration.py` passed (`rc=0`). AI-assisted.
MoonEyes commented 2026-11-29 01:03:55 +00:00

Merged after enlight3d (Thib)'s red/green run (RUN, RTX 3090, driver 621.40, Windows 11: green with this PR, red without it, and gta6rt.exe still links) and a re-check here: no conflict with main 5c8a9de7d, same head as run, no-game-data green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on main or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ).

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

Merged after **enlight3d** (Thib)'s red/green run ([RUN](https://github.com/ProjectLeonida/mandy/pull/2544#issuecomment-7248347742), RTX 3090, driver 621.40, Windows 11: green with this PR, red without it, and `gta6rt.exe` still links) and a re-check here: no conflict with `main` 5c8a9de7d, same head as run, `no-game-data` green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on `main` or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No description provided.