GPU: fix 8 overflow, lookup, and snapshot bugs in shadow.rs with regression tests #1186
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!1186
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1186/head"
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?
@geiyiliyabumo on GitHub · 2026-11-24 16:22 UTC · original
geiyiliyabumo:test/gpu-shadow-tests→main· +333 −40 · 1 files · 4 commits · closed without mergingSummary
Closes #906
Full verification details and change notes are documented in #906 (commit
c1dd5b1, based onmainffe310e).Verification (Windows 11 x64 MSVC)
rustfmt --edition 2024 --checkon edited files: PASScargo xtask policy: PASS (zero game files/data)cargo clippy --all-targets --locked -- -D warnings(default +--features diagnosticswhere applicable): PASSAI-assisted; synthetic tests only, no game files.
@Kal0s-design on GitHub · 2026-11-24 16:29 UTC · original
Independent check of
c1dd5b1, merged locally ontomainffe310e(clean merge). Windows 11, MSVC, Rust 1.98.1 as pinned.cargo test -p victory-gpu --locked: 52 passed, 0 failed, 1 ignored.VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --features diagnostics shadow: 8 passed, 0 failed (none skipped, they ran on the software adapter). Repeated 10 times: 10/10 green.cargo clippy -p victory-gpu --all-targets --locked -- -D warnings, with and without--all-features: clean.tests,warp_tests) on top ofmain'sshadow.rs, nothing else changed, and ran the same WARP command twice. 8 of 8 fail both times. Four are debug-build arithmetic panics (attempt to add with overflowinregister,updateandloose,attempt to subtract with overflowinrefresh); a release build wraps there instead of panicking. The other four are assertion failures, which fail in any build:lookup_checks_past_noncovering_buffers,equal_start_lookup_chooses_the_smallest_covering_buffer,a_newly_declared_gap_takes_its_own_baselineandoverlapping_copy_backs_do_not_replay_an_earlier_cpu_edit.Not covered: the
writingandcopy_backsnapshot changes were only exercised through the software adapter, not on a real GPU or with a title, so the timing between a submission and the title's own writes is untested here.AI-assisted: yes. Every number above is from my own runs.
@TG0000 on GitHub · 2026-11-24 16:57 UTC · original
One regression to look at, at
c1dd5b1:writing(shadow.rs:154) lostmain's early return when the snapshot already covers the declared range, so every distinct range copies the whole union again (pooled(finish - start), line 176). World frames declare overlapping ranges of a 174 MB heap dozens of times.A model of just the copies (64 distinct ranges, 4 times each, in 174 MB; not the code in the game):
main785 MB and 68 ms,c1dd5b18,363 MB and 658 ms. Suggested fix: keep the return when the union does not grow, and refresh only[from, to).The "newly declared gap" fix is right, and "overlapping copy-backs replay an earlier CPU edit" is a real bug on
main. I just opened a related PR (#1267) that keeps each declared byte once; it conflicts with this one in shadow.rs, and it does not fix that last bug.AI-assisted (Claude Code).
@geiyiliyabumo on GitHub · 2026-11-24 17:15 UTC · original
Thanks @TG0000 for catching the union reallocation regression in \Shadow::writing!
Pushed a follow-up commit that restores the in-place fast path when \efore\ already covers [from, to)\—updating only \ytes[from - *at..to - *at]\ in place without reallocating or recopying the entire union—while preserving both the newly-declared-gap baseline fix and the overlapping copy-back fix.
Added a regression test (\covered_subrange_refreshes_in_place_without_reallocating_snapshot) verifying the snapshot buffer pointer stays unchanged while the gap baseline refreshes. Re-verified with \VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lib shadow\ (9/9 passed), clippy (default + --all-features\ -D warnings), and \cargo xtask policy.
AI-assisted; synthetic tests only, no game files.
@TG0000 on GitHub · 2026-11-24 17:31 UTC · original
Thanks for the quick fix and the test. I have not rerun the model on the new head.
AI-assisted (Claude Code).
@wwwwwwww3333333 on GitHub · 2026-11-24 17:40 UTC · original
findis now O(registered buffers) on a miss, measured on hardware. @TG0000'swritingpoint is fixed at1d500e3; the timing below confirms it on the new head.findis unchanged by that commit, and it hasn't come up yet.On a miss (no registered buffer covers
[address, end)),findnow walks every entry whose start is at or belowaddressbefore falling back toloose;mainstops after 64. Release build, RX 6750 XT, Windows 11, Rust 1.98.1 MSVC, with a local ignored test that is not part of this PR: 20,000 registered 16-byte buffers below the address, 10,000 lookups. Forwriting, 50 ranges are declared inside a 64 MiB range declared first.mainc1dd5b11d500e3findmiss, per lookupwriting, 50 ranges inside a declared 64 MiB rangeThe world calls the buffer lookups about 160k times a frame (from a diag frame ledger shown on stream). I can't tell from here how many of those miss, or how many buffers the title has registered, so the real cost may be much smaller than this worst case.
The cap on
mainis wrong too, aslookup_checks_past_noncovering_buffersshows: with more than 64 non-covering entries in between, it misses the real buffer. One way to keep both: track the longest registered lengthmax_lenand stop the reverse scan oncek.0 + max_len < end, since no earlier entry can reachend. The scan is then bounded by how many buffers start withinmax_lenbelow the address, not by the whole map. If one huge heap makes that bound loose, a small separate index for the few large buffers would do it.Not runtime-tested with game files. AI-assisted (Claude wrote the timing test and the analysis; I ran it on my machine).
@geiyiliyabumo on GitHub · 2026-11-24 17:47 UTC · original
Thanks @wwwwwwww3333333 for benchmarking both \writing\ (confirming \
1d500e3\ brought it back from 419.6 ms to 0.05 ms) and catching the O(N) miss regression in \ind()!Pushed $sha\ to bound \ind()\ by the exact longest registered buffer length (\Buffers::max_len):
egister\ updates \max_len\ (and recomputes if replacing the longest entry with a shorter one); \unregister\ recomputes only when removing an entry whose length equals \max_len.
AI-assisted; synthetic tests only, no game files.
@Zipettte on GitHub · 2026-11-24 18:00 UTC · original
Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at
c1dd5b1:cargo test -p victory-gpupasses, 52 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@wwwwwwww3333333 on GitHub · 2026-11-24 18:55 UTC · original
46f9d80fixes the case I measured, but the miss is still O(registered buffers) as soon as one large buffer is registered anywhere below the address, andunregisterbecame O(n) per call when many buffers share the longest length. Measured with the realshadow.rs, plus a proposed alternative below.Release build, RX 6750 XT, i5-13400F, Windows 11, Rust 1.98.1 MSVC, base
ffe310e, local ignored timing tests that are not part of this PR:46f9d80findmiss past 20,000 16-byte buffers (the earlier case)unregisterof 20,000 equal-length bufferswriting, 50 ranges inside a declared 64 MiB rangeWhy:
max_lenis global, so one 64 MiB buffer widens the scan window to 64 MiB for every lookup, and every small buffer in it is visited again. Inunregister, removing any entry whose length equalsmax_lenrescans the whole map; with many equal lengths (common for a title's pools), that is every call. In a standalone model of the scan, 5,000 / 10,000 / 20,000 equal-length unregisters took 18 / 75 / 310 ms, so it grows quadratically.Proposal: keep
by_startas is (forget_mut,nearand the tests) and add a second index by power-of-two length class. A class-cbuffer haslen < 2^(c+1), so it can only cover[address, end)ifstart >= end - (2^(c+1) - 1).findscans each non-empty class over that window only and keeps the innermost match with the same tie-breaks as now: largest start, then smallest end, then the first met in descending key order.registerandunregisterare O(log n), andmax_lengoes away.Checks:
cargo test -p victory-gpu --lib shadowpasses on this machine, withmax_len_tracks_register_replace_and_unregisterreplaced by a class-index test.cargo clippy -p victory-gpu --all-targets -- -D warningsis clean.register/unregister/findcode compiled standalone and matched a brute-force innermost search on 120,000 random lookups. The random buffers were nested, included zero-length and replaced registrations, and everything was unregistered at the end with both indexes left empty.Diff against
46f9d80Source-level analysis plus unit tests and timings; not runtime-tested with game files. AI-assisted (Claude wrote the proposal and the timing tests; I ran them on my machine).
@geiyiliyabumo on GitHub · 2026-11-24 20:16 UTC · original
Thanks @wwwwwwww3333333 for benchmarking both the large-buffer miss case and equal-length \unregister\ scaling, and for designing the length-class index!
Pushed $sha1186\ adopting the power-of-two length-class index (\y_class: [BTreeMap<(u64, usize), u64>; 64]):
egister\ and \unregister\ stay O(log n) with zero full-map rescans.
esource) and updates the unit test to \class_index_follows_register_replace_and_unregister.
AI-assisted; synthetic tests only, no game files.
@wwwwwwww3333333 on GitHub · 2026-11-24 21:50 UTC · original
Confirmed at
ce94115on the RX 6750 XT (same machine and timing tests as before, release build):46f9d80ce94115findmiss past 20,000 small buffersunregisterof 20,000 equal-length bufferswriting, 50 ranges in a 64 MiB rangecargo test -p victory-gpu --lib -- --test-threads=1: 53 passed, 0 failed (main: 50).cargo clippy -p victory-gpu --all-targets -- -D warningsis clean. Thanks for taking the length-class index in.Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).
@wwwwwwww3333333 on GitHub · 2026-11-24 22:09 UTC · original
Heads-up: main moved to
70564ae0(28 commits, including a rewrite ofwatch.rsand about 470 changed lines inshadow.rs), andce94115now conflicts with it in 7 places inshadow.rs. Most of them are inShadow::writing/refresh, which main reworked around its new change journal.The lookup and overflow fixes here are still needed on the new main, though. Its
shadow.rsstill has:find:.range(..=(address, usize::MAX)).rev().take(64).find(|(_, e)| e.end >= end), so a covering buffer more than 64 starts below is still missed, and among equal starts it still takes the first match rather than the innermost.register:end: start + lenwith no overflow check.loose:end.next_multiple_of(WINDOW) - start, also unchecked.So the
find/register/unregisterpart of this PR (with the length-class index) should carry over almost as is. The snapshot/writingpart needs redoing against main's new journal. Once it's rebased, I'll rerun the same timings and tests on the RX 6750 XT.Source-level check of
70564ae0; nothing run on it for this PR yet. AI-assisted (Claude).@Marvisak on GitHub · 2026-11-25 00:34 UTC · original
Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current).
Eight changes in one against the shadow.rs main replaced today (bounding snapshot, buffer lookup by size class, saturating adds). What still matters is in focused PRs: #402 (lookup past 64 entries) and #261 (snapshot after a copy-back).
If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
Pull request closed