victory-shader: preserve high bits in root-derived BVH node addresses (#2368) #2467
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2467
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2467"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@cloph-dsp on GitHub · 2026-11-25 14:10 UTC · original
cloph-dsp:fix/bvh-root-address-width→main· +41 −11 · 2 files · 5 commits · openClaim 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, theSource::Built+root_addresspath computed the node offset as a 32-biti32and then zero-extended it withwide64(at, zero). High pointer bits and carries fromnode_hi,kanddynamicwere 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_hito 64 bits, shifts in i64, widenskwithCast::ZExtanddynamicwithCast::SExt, and adds everything in 64 bits before constructingNodes::Raw. TheNodes::Bufferfallback 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 passedcargo test --locked -p victory-shader --lib emit::bvh_tests::intersections_on_nodes_in_title_memory_equal_the_reference -- --exact— 1 passedcargo 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 ignoredcargo test --workspace --locked— exit 0git diff --check— cleanAI-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.
@upipibi on GitHub · 2026-11-25 14:23 UTC · original
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
main940d32f, this is a different approach, not a copy:Nodes::Rawpath inlower/bvh.rsand widens the node offset to 64 bits:(ptr << 3) + k, whereptrcarriesnode_hi, plus any dynamic offset, added to the 64-bit root base.Source::Builtarm. A built root without a known root address goes through the buffer handle. The root-address case leaves that arm, and #2402 adds 137 lines ofbvh_tests.rs.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.
@Marvisak on GitHub · 2026-11-25 15:58 UTC · original · edited 2026-11-26 07:22 UTC
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 widenslo/node_hibefore the<< 3, zero-extendskanddynamicseparately, and adds in 64 bits. The comment now agrees withshortcut:.The new test
intersections_when_a_node_pointer_carries_into_the_high_worddoes not tell the old lowering from the new one.page_table_at(1, 3, base, TITLE)maps bothbase(TITLE - 2^32) andTITLEto 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, leavebase's page empty or point it at the null pair), so the test fails onmainand passes here. The test also useswide = false, sonode_hiis not exercised; awide = truecase 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:
checkpassing.Generated by Claude Code
@kotekompotek on GitHub · 2026-11-25 16:53 UTC · original
Real fix, two bugs in the old path:
lo << 3computed the byte offset in 32 bits (high pointer bits lost), andnode_hiwas read forwidenodes 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 (ZExtfor the statick,SExtfordynamic). TheNodes::Bufferpath keeping a 32-bitatis fine — it's an offset within a buffer view, which is 32-bit.Minor:
let at = lo << threeis still computed beforeroot_addressand used only by theNonearm — dead work on the root path, harmless.@rminzao on GitHub · 2026-11-26 01:52 UTC · original
Tested head
90ee09don Windows 11 with an RTX 4050 Laptop (595.97) and WARP, runningcargo test -p victory-shader --lib bvh: 14 passed on both. The existing BVH tests stay green.lower/bvh.rs, with no#[cfg(test)]module, soroot_derived_node_address_keeps_high_pointer_bitsdoesn't exist here. A CPU mirror of the arithmetic wouldn't check the lowering anyway. #2501'sa_node_pointer_above_two_gib_reads_where_it_pointsdoes: it fails on the base's lowering and passes on this PR's too (WARP, 15 passed with itsbvh_tests.rs).node_hi: this is the one change of the two that covers the issue's third counterexample (hi=1, base + 32 GiB). #2501 still widens with a zero high word.kand the computed part are summed in 32 bits and then zero-extended.resource::Address::offsetis "added modulo 2^32 (with carry into the high half)", once per part, so a sum that overflows 32 bits loses a carry the title's owns_add_u32/s_addc_u32pairs would keep. Widening each part before the add, as #2501 does, avoids that.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).
@DenRakEiw on GitHub · 2026-11-25 14:17 UTC · original · approved these changes
Verified against
origin/main(940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:#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 (includingroot_derived_node_address_keeps_high_pointer_bitsand 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).
@catminou4 on GitHub · 2026-11-25 14:57 UTC · original · approved these changes
Tested locally: merged into main
940d32f69b,cargo test -p victory-shader --lockedon 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 on GitHub · 2026-11-25 15:11 UTC · original · reviewed
Side-by-side check of #2501 (
ca57ed43) and #2467 (c79cb207). Both change only theSource::Built+root_address(Nodes::Raw) arm ofcrates/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. TheNodes::Bufferfallback is unchanged in behaviour in both.Differences
image_bvh64high node word (node_hi)lo<<3is widenedptr = lo | node_hi<<32, then<<3dynamictermZExtto 64 bitsSExtto 64 bitski64ZExtof the 32-bit valuerustfmt --checkfails on the test fn signature)Edge cases.
node_hi != 0, #2501 still drops the high pointer bits. It fixes thelo<<3overflow for BVHs above 512 MiB, but not 64-bit pointers. #2467 handles both, and the already-passingintersections_with_64_bit_pointers_on_the_gpu_equal_the_referencetest exercises theNonepath rather than this one.dynamicsign: the two disagree for values >= 2^31. Under #2501, a negativedynamicthat 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 inAddress.dynamicorbuilt_offsetstates which is intended. The authors should say, ideally with a test through the real lowering.Nodes::Rawpath, 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 ownZExtofdynamic. If #2467 is picked, itsdynamicsign question and a real lowering test should be settled first.Verification (Windows,
VICTORY_D3D12_WARP=1, WARP only):ca57ed43289e97748adb277cbfcbcec4994dcc0e:cargo test -p victory-shader --locked --lib— 247 passed, 0 failed, 3 ignored;... --lib bvh— 14 passed, 0 failed, 0 ignored. rustfmt clean.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)
@fyernald on GitHub · 2026-11-25 17:33 UTC · original · requested changes
Tested on RTX 3080 (617.42), Windows 10 (19045) plus WARP,
main67e8ae1+ #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:dynamicis 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 partk. A dynamic offset of 2 GiB or more would come out 4 GiB too low. #2501 zero-extends it.built_root_node_addressre-implements the arithmetic in plain Rust and checks it against itself, so it would pass even ifbvh.rswere 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. Thenode_hipart is what this PR adds over it (it matches the explicit-descriptor path onmain), so it might be easiest to rebase that onto #2501 with zero-extension and a hardware test.@fyernald on GitHub · 2026-11-26 02:02 UTC · original · approved these changes
Retested
90ee09don the same RTX 3080 / Windows 10, now onmain63bcca4(+ #486, #2596 for #2571).bvh_tests.rschange (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_pointspasses on the RTX 3080 with this PR and fails onmain, and all 15bvh_testspass.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 + dynamicis 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 on GitHub · 2026-11-26 05:59 UTC · original
Pushed three follow-up commits addressing the triage and review points:
c106e19addsintersections_when_a_node_pointer_carries_into_the_high_wordinsrc/emit/bvh_tests.rs. It goes through the real lowering (page table for the carried base + node pointer above2^29), not a CPU mirror of the arithmetic.5680f77zero-extendskanddynamicseparately to 64 bits before adding them, so a carry out ofk + dynamicis preserved, matchingresource::Address::offset("modulo 2^32 with carry into the high half") and the hardwares_add_u32/s_addc_u32pair. Thenode_hiwidening is kept.8676134rewords the root-descriptor comment so it agrees with the 256-byteshortcut: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).cargo test -p victory-shader— 277 passed, 3 ignored (run before the comment-only commit).git diff --check— clean.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.
@nathanbzrr on GitHub · 2026-11-26 07:48 UTC · original · requested changes
Reviewed
8676134merged ontomain(63bcca4). The 64-bit widening incrates/victory-shader/src/emit/lower/bvh.rs:138-158(super::flags::INT64withwide64andCast::ZExtonkanddynamic) correctly propagates carries into the upper 32 bits ofNodes::Raw, andpolicy(191 files ok),clippy, andbvhtests pass on Windows 11 (AMD Radeon RX 7600 +VICTORY_WARP=1,15 passed; 0 failed).However, two test gaps should be addressed before merge:
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). Inpage_table_at(bvh_tests.rs:825-837), bothbase(0x11_3450_0000) andtarget(0x12_3450_0000) share the exact same low 32 bits (0x3450_0000), map both L1 indices (275and291) to the same L2 block1, and populate the same L2 slot[view, 0x3450_0000]backed byView::Memory(&data). If the carry out of the low word were truncated and the shader loaded frombase(0x11_3450_0000) instead ofTITLE(0x12_3450_0000), the page table would still return identical data and the test would still pass. Givingbaseandtargetdistinct page-table targets (e.g., mappingbasetonullor a different offset) would make it genuinely catch a dropped high-word carry.wide = true(node_hi != 0) in title memory.intersections_when_a_node_pointer_carries_into_the_high_wordpasseswide = false(node_hi = None), whilerun_cases(true, 8, false)passes a zero node-pointer base (node_hi == 0). Adding awide = truecase wherenode_hi != 0contributes to(base + (ptr << 3))resolving to a distinct high-word title page would coverbvh.rs:145withnode_hinon-zero.AI-assisted; synthetic tests only, no game files.
@rezioliver-collab on GitHub · 2026-11-26 13:05 UTC · original
On an Apple M1, #2467 computed both the +4 GiB and +32 GiB node addresses correctly. #2501 handled the +4 GiB carry, but read the node at the base when the pointer’s high word was 1.
The table shows the address actually read. The root SRV base was
B = 0x1234500000.main(63bcca43)d916d066)86761342)(0x00000005, 0)(0x20000005, 0)(0x00000005, 1)I built each revision separately with Rust 1.98.1 on macOS 14.7 and translated the same 100-byte synthetic
image_bvh64_intersect_rayshader fromemit/bvh_tests.rs. For the CPU replay, Apple Clang 16 compiled the emitted DXIL function at-O0, with a small native adapter for DXIL resource operations. The disassembly step adapted the module’si8alignment for Apple LLVM and preserved the shader instructions.The synthetic page table mapped B, B + 4 GiB and B + 32 GiB to three distinct nodes. Their first child IDs were
0xa000,0xb000and0xc000. For one shader thread per case, I traced all 28 node dwords and checked all four child IDs written by the shader. In the +32 GiB case, #2501 returned0xa000and #2467 returned0xc000. In the emitted IR, #2501 supplies a zero high word before the 64-bit shift, while #2467 combines both pointer words before shifting.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.