Skip to content

victory-shader: preserve high bits in root-derived BVH node addresses (#2368) - #2467

Open
cloph-dsp wants to merge 5 commits into
Marvisak:mainfrom
cloph-dsp:fix/bvh-root-address-width
Open

cloph-dsp wants to merge 5 commits into
Marvisak:mainfrom
cloph-dsp:fix/bvh-root-address-width

Conversation

@cloph-dsp

Copy link
Copy Markdown

Claim on the coordination issue #1633: claiming: new item, issue #2368 — shaders: preserve high pointer bits and 256-byte descriptor alignment for root-derived BVH node addresses; CPU-only arithmetic fix + synthetic unit test, no game data, AI-assisted (Hy4 flash) (comment 7163470612).

Problem: in crates/victory-shader/src/emit/lower/bvh.rs, the Source::Built + root_address path computed the node offset as a 32-bit i32 and then zero-extended it with wide64(at, zero). High pointer bits and carries from node_hi, k and dynamic were lost; the explicit-address path already reconstructed the full 64-bit pointer.

Change: one file, +42/−2. The title-memory root path now widens lo|node_hi to 64 bits, shifts in i64, widens k with Cast::ZExt and dynamic with Cast::SExt, and adds everything in 64 bits before constructing Nodes::Raw. The Nodes::Buffer fallback keeps its 32-bit offset. A #[cfg(test)] module adds a pure CPU arithmetic mirror test for the two counterexamples: lo=0x20000005 → base+4GiB, lo=5, node_hi=1 → base+32GiB.

I initially also applied the explicit descriptor path's 256-byte base quantization (base << 8), but that made the existing hardware BVH tests fail: the root descriptor in the synthetic fixture carries the base directly, already aligned. The quantization is therefore deliberately NOT applied here; the fix covers the high-bits/carry loss only.

Validation (Windows, Rust 1.100.0/MSVC, no game data):

  • cargo test --locked -p victory-shader --lib emit::lower::bvh::tests::root_derived_node_address_keeps_high_pointer_bits -- --exact — 1 passed
  • cargo test --locked -p victory-shader --lib emit::bvh_tests::intersections_on_nodes_in_title_memory_equal_the_reference -- --exact — 1 passed
  • cargo test --locked -p victory-shader --lib emit::bvh_tests::intersections_on_the_gpu_equal_the_reference -- --exact — passed (hardware, RTX 5060 Laptop)
  • cargo test --locked -p victory-shader --lib — 248 passed, 0 failed, 3 ignored
  • cargo test --workspace --locked — exit 0
  • git diff --check — clean

AI-assisted (Hy4 flash). GPU/driver: NVIDIA GeForce RTX 5060 Laptop, driver 621.24, Vulkan 1.4.357. No game data, assets, executables or extracted content were used, created or uploaded.

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

Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Preserving high pointer bits in root-derived BVH node addresses (#2368) passes all unit and live GPU BVH intersection tests.
  • cargo test -p victory-shader bvh: 15/15 passed on RTX 5090, 15/15 passed on WARP (including root_derived_node_address_keeps_high_pointer_bits and all GPU intersection tests).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

Overlap check: #2402 (Xvania7, opened 12:57Z, "fixes #2368") and #2040 (mgunes1311-lab) target the same issue, and andrewpozdnakov7-jpg has been comparing #2402 with #2040 on WARP. From reading the diffs against main 940d32f, this is a different approach, not a copy:

So the three need one pick, or a combination. I haven't run any of them.

AI-assisted (Claude Code). Read from the diffs only.

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

Tested locally: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — high bits preserved in root-derived buffer addresses.

@oni-swr oni-swr 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.

Side-by-side check of #2501 (ca57ed43) and #2467 (c79cb207). Both change only the Source::Built + root_address (Nodes::Raw) arm of crates/victory-shader/src/emit/lower/bvh.rs, for #2368. I read both diffs in full.

Overlap: both stop shifting the node offset in 32 bits and then zero-extending it. Both build base64 + (node << 3) + k (+ dynamic) in 64 bits. The Nodes::Buffer fallback is unchanged in behaviour in both.

Differences

#2501 #2467
image_bvh64 high node word (node_hi) still ignored on this arm; only lo<<3 is widened folded in: ptr = lo | node_hi<<32, then <<3
dynamic term ZExt to 64 bits SExt to 64 bits
k constant i64 ZExt of the 32-bit value
tests none added one test, a Rust mirror of the arithmetic; it does not call the lowering. It is also not rustfmt-clean (rustfmt --check fails on the test fn signature)
buffer path rewritten in an explicit else branch (same ops) untouched

Edge cases.

  • With node_hi != 0, #2501 still drops the high pointer bits. It fixes the lo<<3 overflow for BVHs above 512 MiB, but not 64-bit pointers. #2467 handles both, and the already-passing intersections_with_64_bit_pointers_on_the_gpu_equal_the_reference test exercises the None path rather than this one.
  • dynamic sign: the two disagree for values >= 2^31. Under #2501, a negative dynamic that used to wrap correctly in 32 bits, with the sum below 2^32, now lands 2^32 too high. Under #2467 it is correct for negative values but wrong for an unsigned offset >= 2^31. Nothing I read in Address.dynamic or built_offset states which is intended. The authors should say, ideally with a test through the real lowering.
  • Neither adds a GPU/WARP test of the Nodes::Raw path, so the new arithmetic is not exercised end to end by the suite.

Supersede or complement? They are alternative fixes to the same lines and will conflict, so only one can merge as written. #2467 is the more complete one (it includes node_hi). #2501 has the cleaner 64-bit constants, and its comment about negative offsets does not match its own ZExt of dynamic. If #2467 is picked, its dynamic sign question and a real lowering test should be settled first.

Verification (Windows, VICTORY_D3D12_WARP=1, WARP only):

  • #2501 ca57ed43289e97748adb277cbfcbcec4994dcc0e: cargo test -p victory-shader --locked --lib — 247 passed, 0 failed, 3 ignored; ... --lib bvh — 14 passed, 0 failed, 0 ignored. rustfmt clean.
  • #2467 c79cb207dcbea305a35f91a541b6bea0a522f9d8: cargo test -p victory-shader --locked --lib — 248 passed, 0 failed, 3 ignored; ... --lib bvh — 15 passed, 0 failed, 0 ignored (includes its new test).

AI-assisted (Claude Code / Claude Sonnet 5.5)

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work (the test). Thanks for the update. In Lower::bvh (emit/lower/bvh.rs, root-address arm), the lowering now does what was asked. It widens lo/node_hi before the << 3, zero-extends k and dynamic separately, and adds in 64 bits. The comment now agrees with shortcut:.

The new test intersections_when_a_node_pointer_carries_into_the_high_word does not tell the old lowering from the new one. page_table_at(1, 3, base, TITLE) maps both base (TITLE - 2^32) and TITLE to the same view at the same low word. So the address the old code computed (carry lost, base + offset) reads the same nodes as the right one (TITLE + offset). Map only the carried-to address (for example, leave base's page empty or point it at the null pair), so the test fails on main and passes here. The test also uses wide = false, so node_hi is not exercised; a wide = true case with a non-zero high word would cover it.

This still overlaps #2501 on the same lines, and the maintainer should pick one or combine them.

CI: check passing.


Generated by Claude Code

@kotekompotek

Copy link
Copy Markdown

Real fix, two bugs in the old path: lo << 3 computed the byte offset in 32 bits (high pointer bits lost), and node_hi was read for wide nodes then dropped entirely on the root-address path — wide64(at, zero) widened a value whose carries had already been truncated. The new order widens first (wide64(lo, node_hi)), then shifts/adds in 64 bits, with correct extension signedness (ZExt for the static k, SExt for dynamic). The Nodes::Buffer path keeping a 32-bit at is fine — it's an offset within a buffer view, which is 32-bit.

Minor: let at = lo << three is still computed before root_address and used only by the None arm — dead work on the root path, harmless.

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

Tested on RTX 3080 (617.42), Windows 10 (19045) plus WARP, main 67e8ae1 + #486 + #2596 (the suite crashes on NVIDIA without #2596, #2571). cargo test -p victory-shader --lib --locked -- --test-threads=1: 268 passed, 3 failed (the #1835 ones, also on the base). Nothing regresses. Two things, though:

  1. dynamic is sign-extended (Cast::SExt), but it's the byte offset the built descriptor adds to the root address (built_offset). The console adds that with an unsigned carry, and this PR zero-extends the constant part k. A dynamic offset of 2 GiB or more would come out 4 GiB too low. #2501 zero-extends it.
  2. The test can't fail on the lowering. built_root_node_address re-implements the arithmetic in plain Rust and checks it against itself, so it would pass even if bvh.rs were unchanged. #2501 has an end-to-end test (a_node_pointer_above_two_gib_reads_where_it_points) that passes on an RTX 3080.

#2501 is the same 64-bit shift and add without node_hi. The node_hi part is what this PR adds over it (it matches the explicit-descriptor path on main), so it might be easiest to rebase that onto #2501 with zero-extension and a hardware test.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@cloph-dsp
cloph-dsp force-pushed the fix/bvh-root-address-width branch from c79cb20 to 90ee09d Compare November 25, 2026 22:50
@rminzao

rminzao commented Nov 26, 2026

Copy link
Copy Markdown

Tested head 90ee09d on Windows 11 with an RTX 4050 Laptop (595.97) and WARP, running cargo test -p victory-shader --lib bvh: 14 passed on both. The existing BVH tests stay green.

The two PRs fix the same lines, each covering what the other misses. The simplest landing is #2501 plus node_hi. Otherwise this PR could take #2501's test and the per-part widening.

AI-assisted (Claude Code).

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

Retested 90ee09d on the same RTX 3080 / Windows 10, now on main 63bcca4 (+ #486, #2596 for #2571).

  • The sign extension is gone. The offsets now go through zero extension, and the self-checking test is removed.
  • To exercise the lowering itself, I applied only #2501's bvh_tests.rs change (the 4 GiB-higher page and node pointer with bit 29 set) on top of this branch. a_node_pointer_above_two_gib_reads_where_it_points passes on the RTX 3080 with this PR and fails on main, and all 15 bvh_tests pass.
  • Full cargo test -p victory-shader --lib -- --test-threads=1: 298 passed, 4 failed, the same four as the base. Nothing regresses.

The one thing left is that the PR has no test of its own now. Carrying #2501's end-to-end test over, or landing the two together, would cover it. One difference from #2501 for whoever picks between them: here k + dynamic is summed in 32 bits before widening, so a carry out of that sum is dropped, while #2501 adds each part in 64 bits. That only matters when the two together reach 4 GiB.

@cloph-dsp

Copy link
Copy Markdown
Author

Pushed three follow-up commits addressing the triage and review points:

  • c106e19 adds intersections_when_a_node_pointer_carries_into_the_high_word in src/emit/bvh_tests.rs. It goes through the real lowering (page table for the carried base + node pointer above 2^29), not a CPU mirror of the arithmetic.
  • 5680f77 zero-extends k and dynamic separately to 64 bits before adding them, so a carry out of k + dynamic is preserved, matching resource::Address::offset ("modulo 2^32 with carry into the high half") and the hardware s_add_u32/s_addc_u32 pair. The node_hi widening is kept.
  • 8676134 rewords the root-descriptor comment so it agrees with the 256-byte shortcut: above it.

Validation (Windows, Rust 1.100.0/MSVC, CARGO_TARGET_DIR=D:\Victory-Contrib\shared-target):

  • cargo test -p victory-shader --lib bvh — 15 passed, 0 failed (including the new carry test and the existing GPU/WARP intersection tests).
  • Full cargo test -p victory-shader — 277 passed, 3 ignored (run before the comment-only commit).
  • git diff --check — clean.
  • GPU available on this host: RTX 5060 Laptop (driver 621.24); Radeon 780M RDNA3 is also present. The BVH tests select an available D3D12/WARP device.

Thanks @Marvisak, @rminzao and @fyernald for the precise reviews. Please re-check the updated head 8676134.

AI-assisted (Hy4 flash). No game assets, extracted data, shaders or DRM-related material were used or uploaded.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026

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

Reviewed 8676134 merged onto main (63bcca4). The 64-bit widening in crates/victory-shader/src/emit/lower/bvh.rs:138-158 (super::flags::INT64 with wide64 and Cast::ZExt on k and dynamic) correctly propagates carries into the upper 32 bits of Nodes::Raw, and policy (191 files ok), clippy, and bvh tests pass on Windows 11 (AMD Radeon RX 7600 + VICTORY_WARP=1, 15 passed; 0 failed).

However, two test gaps should be addressed before merge:

  1. intersections_when_a_node_pointer_carries_into_the_high_word (crates/victory-shader/src/emit/bvh_tests.rs:818-854) does not fail if the 64-bit carry is dropped.
    It calls run_cases_at(false, 4, false, TITLE - 0x1_0000_0000, 0x2000_0000). In page_table_at (bvh_tests.rs:825-837), both base (0x11_3450_0000) and target (0x12_3450_0000) share the exact same low 32 bits (0x3450_0000), map both L1 indices (275 and 291) to the same L2 block 1, and populate the same L2 slot [view, 0x3450_0000] backed by View::Memory(&data). If the carry out of the low word were truncated and the shader loaded from base (0x11_3450_0000) instead of TITLE (0x12_3450_0000), the page table would still return identical data and the test would still pass. Giving base and target distinct page-table targets (e.g., mapping base to null or a different offset) would make it genuinely catch a dropped high-word carry.
  2. No coverage with wide = true (node_hi != 0) in title memory.
    intersections_when_a_node_pointer_carries_into_the_high_word passes wide = false (node_hi = None), while run_cases(true, 8, false) passes a zero node-pointer base (node_hi == 0). Adding a wide = true case where node_hi != 0 contributes to (base + (ptr << 3)) resolving to a distinct high-word title page would cover bvh.rs:145 with node_hi non-zero.

AI-assisted; synthetic tests only, no game files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants