GPU: deduplicate per-submission write-watch askings over nested buffers (#776) #1351

Closed
github-bot wants to merge 1 commit from gh-pr/1351/head into gh-pr/1351/base
Owner

@⁠wbkepler on GitHub · 2026-11-24 17:34 UTC · original
wbkepler:gpu/write-watch-dedupe-nested → main · +469 −30 · 2 files · 1 commits · closed without merging

Summary

Implements #776:

  • Deduplicates per-submission MEM_WRITE_WATCH askings over nested/overlapping buffers in crates/victory-gpu/src/{watch.rs,host.rs} via largest-first Batch::flush and per-submission sweep tracking.
  • Eliminates redundant GetWriteWatch / page-reset overhead when multiple nested views reference the same title memory region.
  • Adds unit tests verifying nested/overlapping write-watch deduplication and page freshness.

Verification

  • cargo clippy -p victory-gpu --all-targets — passed
  • cargo test -p victory-gpu — passed
  • cargo xtask policy — passed
**@⁠wbkepler** on GitHub · 2026-11-24 17:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1351) `wbkepler:gpu/write-watch-dedupe-nested` → `main` · +469 −30 · 2 files · 1 commits · closed without merging ## Summary Implements #776: - Deduplicates per-submission `MEM_WRITE_WATCH` askings over nested/overlapping buffers in `crates/victory-gpu/src/{watch.rs,host.rs}` via largest-first `Batch::flush` and per-submission sweep tracking. - Eliminates redundant `GetWriteWatch` / page-reset overhead when multiple nested views reference the same title memory region. - Adds unit tests verifying nested/overlapping write-watch deduplication and page freshness. ## Verification - `cargo clippy -p victory-gpu --all-targets` — passed - `cargo test -p victory-gpu` — passed - `cargo xtask policy` — passed
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:28:26 +00:00
github-bot closed this pull request 2026-11-26 04:28:26 +00:00
Author
Owner

@⁠maplerouteadmin-dev on GitHub · 2026-11-24 19:17 UTC · original

Tested in the title on an RTX 5070 (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build. Base: main ffe310e + #966 + Peppacodes exp/mesh-host-lanes + a #951 port (needed to reach the world on NVIDIA) + #1350. Same scripted input every run (VICTORY_KEYS), 165 s per run, builds alternated, one process at a time. No game data used.

The deduplication works, but as is it makes copies larger, because some copies stop being write-watched.

When a sweep leaves a gap that starts in an unmapped hole of view memory, query_gap → guard gives up (guard checks VirtualQuery(start).Type == MEM_MAPPED at the gap's start, which is not mapped there, while the range's own start is). writes returns None, and Shadow::update_free marks the copy NOT_WATCHED for good: every later refresh copies the whole range. With a log line at that point, entering the world:

  • #1350 alone: 0 copies became unwatched
  • #1350 + #1351: 52 copies became unwatched within ~1 s of entering the world, 3 080 MB in all (mostly 64 MB copies at 0xb2…–0xcf…); GetWriteWatch fails with error 87 and guard returns None for the gap, e.g. gap 0xb3f7c0000..0xb415e0000 of a refresh 0xb3f5d2000..0xb415d5000 in the copy at 0xb3e000000+64 MB

Suggested fix (12 lines in writes): when a gap fails, ask about the whole range once, as without a sweep:

    // A gap can start where the whole range does not: in an unmapped hole of
    // view memory, where `guard` (which looks at the type at its start)
    // gives up although the range as a whole is guardable. Ask about the
    // whole range then, as without a sweep, so one such gap does not leave
    // the caller's copy unwatched (whole-range copies) from then on.
    if failed_any
        && gaps.as_slice() != [(first, last)]
        && query_gap(j, first, last, guarded, &mut dirty)
    {
        failed_any = false;
        swept_gaps = vec![(first, last)];
    }

With it: 0 copies became unwatched (the guard gave up on a gap 108 times; the whole-range retry succeeded every time). cargo test -p victory-gpu and both clippy configurations pass.

Medians over world frames (5 s frame-rate lines with frames over 500 ms, pooled per build):

#1350 alone + #1351 + #1351 + fix
runs / world lines 5 / 50 2 / 24 4 / 18
ms/frame 2 287 3 067 1 835
copies, ms/frame 841 1 902 577
copied per draw, MB 2.79 5.40 2.30
write-watch askings/frame 1 389 915 720
write-watch time, ms/frame 279 112 121
on the GPU, ms/frame 571 412 429

Draws per frame differ between runs (the scene moves), so per-draw figures are the fairer comparison.

Caveats. Frame times are noisy, and the world is not stable on this card: device removal (TDR) in several runs of every build, and one hard system freeze during a #1350-only run. Two runs of the fixed build ended in title access violations: one at the same title address as a crash on the plain base without either PR (+0x23cf683, write to 0), one at loading (+0x164a298, read 0x2c) seen once in about 20 runs. I can't tie either to these changes.

**@⁠maplerouteadmin-dev** on GitHub · 2026-11-24 19:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1351#issuecomment-7141878623) Tested in the title on an RTX 5070 (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build. Base: `main` `ffe310e` + #966 + Peppacodes `exp/mesh-host-lanes` + a #951 port (needed to reach the world on NVIDIA) + #1350. Same scripted input every run (`VICTORY_KEYS`), 165 s per run, builds alternated, one process at a time. No game data used. **The deduplication works, but as is it makes copies larger, because some copies stop being write-watched.** When a sweep leaves a gap that starts in an unmapped hole of view memory, `query_gap` → `guard` gives up (`guard` checks `VirtualQuery(start).Type == MEM_MAPPED` at the gap's start, which is not mapped there, while the range's own start is). `writes` returns `None`, and `Shadow::update_free` marks the copy `NOT_WATCHED` for good: every later refresh copies the whole range. With a log line at that point, entering the world: - #1350 alone: **0** copies became unwatched - #1350 + #1351: **52** copies became unwatched within ~1 s of entering the world, 3 080 MB in all (mostly 64 MB copies at `0xb2…`–`0xcf…`); `GetWriteWatch` fails with error 87 and `guard` returns `None` for the gap, e.g. gap `0xb3f7c0000..0xb415e0000` of a refresh `0xb3f5d2000..0xb415d5000` in the copy at `0xb3e000000+64 MB` **Suggested fix** (12 lines in `writes`): when a gap fails, ask about the whole range once, as without a sweep: ```rust // A gap can start where the whole range does not: in an unmapped hole of // view memory, where `guard` (which looks at the type at its start) // gives up although the range as a whole is guardable. Ask about the // whole range then, as without a sweep, so one such gap does not leave // the caller's copy unwatched (whole-range copies) from then on. if failed_any && gaps.as_slice() != [(first, last)] && query_gap(j, first, last, guarded, &mut dirty) { failed_any = false; swept_gaps = vec![(first, last)]; } ``` With it: **0** copies became unwatched (the guard gave up on a gap 108 times; the whole-range retry succeeded every time). `cargo test -p victory-gpu` and both clippy configurations pass. **Medians over world frames** (5 s frame-rate lines with frames over 500 ms, pooled per build): | | #1350 alone | + #1351 | + #1351 + fix | |---|---|---|---| | runs / world lines | 5 / 50 | 2 / 24 | 4 / 18 | | ms/frame | 2 287 | 3 067 | **1 835** | | copies, ms/frame | 841 | 1 902 | **577** | | copied **per draw**, MB | 2.79 | 5.40 | **2.30** | | write-watch askings/frame | 1 389 | 915 | **720** | | write-watch time, ms/frame | 279 | 112 | **121** | | on the GPU, ms/frame | 571 | 412 | 429 | Draws per frame differ between runs (the scene moves), so per-draw figures are the fairer comparison. **Caveats.** Frame times are noisy, and the world is not stable on this card: device removal (TDR) in several runs of every build, and one hard system freeze during a #1350-only run. Two runs of the fixed build ended in title access violations: one at the same title address as a crash on the plain base without either PR (`+0x23cf683`, write to 0), one at loading (`+0x164a298`, read 0x2c) seen once in about 20 runs. I can't tie either to these changes.
Author
Owner

@⁠maplerouteadmin-dev on GitHub · 2026-11-24 21:15 UTC · original

Correction to my earlier numbers on #1350 and #1351. After posting them I found a background GPU process on the test machine (a hidden CUDA miner, since removed) holding about 2.8 GB of VRAM and using the GPU. Every build was measured under the same conditions, so the comparisons between builds hold, but the absolute frame times and some of the device removals were inflated by it. Re-measured on the clean machine:

RTX 5070 12 GB (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build; same scripted input (VICTORY_KEYS), 240 s per run, builds alternated, one process at a time. Base = main ffe310e + #966 + Peppacodes exp/mesh-host-lanes + a #951 port. Medians over world frame-rate lines (frames over 500 ms). No game data used.

base + #1350 + #1350 + #1351 + #1350 + #1351 + gap fix
runs 2 2 2 3
ms/frame 4 667 2 434 2 000 1 633
copies, ms/frame 4 240 1 222 1 347 635
copied per draw, MB 2.69 3.20 4.13 2.54
write-watch, ms/frame 271 479 456 238
runs ending in device removal 1 1 1 1

Same conclusions as before: #1350 cuts the copy cost sharply; #1351 as is copies more per draw (the copies left unwatched after a failed gap, see my comment on #1351); with the gap fix it is the fastest of the four. The device removals are real on this card without the background process too: about one run in two, on every build, so they are not caused by either PR.

**@⁠maplerouteadmin-dev** on GitHub · 2026-11-24 21:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1351#issuecomment-7144689835) **Correction to my earlier numbers on #1350 and #1351.** After posting them I found a background GPU process on the test machine (a hidden CUDA miner, since removed) holding about 2.8 GB of VRAM and using the GPU. Every build was measured under the same conditions, so the comparisons between builds hold, but the absolute frame times and some of the device removals were inflated by it. Re-measured on the clean machine: RTX 5070 12 GB (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build; same scripted input (`VICTORY_KEYS`), 240 s per run, builds alternated, one process at a time. Base = `main` `ffe310e` + #966 + Peppacodes `exp/mesh-host-lanes` + a #951 port. Medians over world frame-rate lines (frames over 500 ms). No game data used. | | base | + #1350 | + #1350 + #1351 | + #1350 + #1351 + gap fix | |---|---|---|---|---| | runs | 2 | 2 | 2 | 3 | | ms/frame | 4 667 | 2 434 | 2 000 | **1 633** | | copies, ms/frame | 4 240 | 1 222 | 1 347 | **635** | | copied per draw, MB | 2.69 | 3.20 | 4.13 | **2.54** | | write-watch, ms/frame | 271 | 479 | 456 | **238** | | runs ending in device removal | 1 | 1 | 1 | 1 | Same conclusions as before: #1350 cuts the copy cost sharply; #1351 as is copies more per draw (the copies left unwatched after a failed gap, see my comment on #1351); with the gap fix it is the fastest of the four. The device removals are real on this card without the background process too: about one run in two, on every build, so they are not caused by either PR.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:38 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Deduplicates per-submission GetWriteWatch askings over nested buffers; main asks the system nothing per submission any more (per-copy trackers marked by guard faults).

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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1351#issuecomment-7148718657) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Deduplicates per-submission GetWriteWatch askings over nested buffers; main asks the system nothing per submission any more (per-copy trackers marked by guard faults). 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1351
No description provided.