GPU: coalesce nearby shadow-buffer refreshes without spanning huge heap gaps #1554

Open
Texbirds wants to merge 1 commit from upstream-pr/1555 into main
Texbirds commented 2026-11-24 19:07:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-24T19:07:34Z.


Problem

In crates/victory-gpu/src/host.rs, Batch::add collapses all pending refreshes for a Shadow buffer into one [min(from), max(to)) bounding range.

When draws in a submission read small buffers at distant offsets inside a large shared GPU heap (e.g. a 174 MiB heap), Batch::flush calls shadow.update over the entire span between them—scanning write-watch/guard pages and copying unrelated dirty or unwatched megabytes via ReadProcessMemory synchronously before ExecuteCommandLists.

Fix

  • Record per-shadow [from, to) intervals in Batch and sort/coalesce overlapping or nearby intervals within COALESCE_GAP (64 KiB, one guard block) before flushing, so adjacent buffers still coalesce into a single update while megabyte-wide gaps are never spanned.
  • Add unit test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6589340623 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-24T19:07:34Z. --- ### Problem In `crates/victory-gpu/src/host.rs`, `Batch::add` collapses all pending refreshes for a `Shadow` buffer into one `[min(from), max(to))` bounding range. When draws in a submission read small buffers at distant offsets inside a large shared GPU heap (e.g. a 174 MiB heap), `Batch::flush` calls `shadow.update` over the entire span between them—scanning write-watch/guard pages and copying unrelated dirty or unwatched megabytes via `ReadProcessMemory` synchronously before `ExecuteCommandLists`. ### Fix - Record per-shadow `[from, to)` intervals in `Batch` and sort/coalesce overlapping or nearby intervals within `COALESCE_GAP` (64 KiB, one guard block) before flushing, so adjacent buffers still coalesce into a single update while megabyte-wide gaps are never spanned. - Add unit test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate`. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
wwwwwwww3333333 commented 2026-11-24 22:02:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wwwwwwww3333333 on 2026-11-24T21:51:11Z.


On Windows, cargo test -p victory-gpu --lib doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC.

error[E0425]: cannot find function `coalesce_ranges` in this scope
    --> crates\victory-gpu\src\host.rs:2055:9
error[E0425]: cannot find value `COALESCE_GAP` in this scope
    --> crates\victory-gpu\src\host.rs:2055:39
(the same two at 2068)

The new test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate sits in mod batch, which imports only super::Batch. coalesce_ranges and COALESCE_GAP are private items of host.rs (lines 1236 and 1240), so the module needs them too: use super::{Batch, COALESCE_GAP, coalesce_ranges};. This probably didn't show up off Windows because host.rs is Windows-only.

Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145474110 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7145474110). Originally posted by [wwwwwwww3333333](https://github.com/wwwwwwww3333333) on 2026-11-24T21:51:11Z. --- On Windows, `cargo test -p victory-gpu --lib` doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC. ``` error[E0425]: cannot find function `coalesce_ranges` in this scope --> crates\victory-gpu\src\host.rs:2055:9 error[E0425]: cannot find value `COALESCE_GAP` in this scope --> crates\victory-gpu\src\host.rs:2055:39 (the same two at 2068) ``` The new test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` sits in `mod batch`, which imports only `super::Batch`. `coalesce_ranges` and `COALESCE_GAP` are private items of `host.rs` (lines 1236 and 1240), so the module needs them too: `use super::{Batch, COALESCE_GAP, coalesce_ranges};`. This probably didn't show up off Windows because host.rs is Windows-only. Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).
Texbirds commented 2026-11-24 22:02:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-24T21:54:36Z.


On Windows, cargo test -p victory-gpu --lib doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC.

error[E0425]: cannot find function `coalesce_ranges` in this scope
    --> crates\victory-gpu\src\host.rs:2055:9
error[E0425]: cannot find value `COALESCE_GAP` in this scope
    --> crates\victory-gpu\src\host.rs:2055:39
(the same two at 2068)

The new test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate sits in mod batch, which imports only super::Batch. coalesce_ranges and COALESCE_GAP are private items of host.rs (lines 1236 and 1240), so the module needs them too: use super::{Batch, COALESCE_GAP, coalesce_ranges};. This probably didn't show up off Windows because host.rs is Windows-only.

Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).

Thanks for catching that — fixed in 51a046d by importing COALESCE_GAP and coalesce_ranges in mod batch.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145548929 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7145548929). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-24T21:54:36Z. --- > On Windows, `cargo test -p victory-gpu --lib` doesn't compile with this PR: head [bfffb01](https://github.com/Marvisak/VIctory/commit/bfffb01dd6b87459f5e3239e002076c90935c61d) on main [ffe310e](https://github.com/Marvisak/VIctory/commit/ffe310eb2098e52169f4c44db5ffdabf43a72622) (clean merge), Rust 1.98.1 MSVC. > > ``` > error[E0425]: cannot find function `coalesce_ranges` in this scope > --> crates\victory-gpu\src\host.rs:2055:9 > error[E0425]: cannot find value `COALESCE_GAP` in this scope > --> crates\victory-gpu\src\host.rs:2055:39 > (the same two at 2068) > ``` > > The new test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` sits in `mod batch`, which imports only `super::Batch`. `coalesce_ranges` and `COALESCE_GAP` are private items of `host.rs` (lines 1236 and 1240), so the module needs them too: `use super::{Batch, COALESCE_GAP, coalesce_ranges};`. This probably didn't show up off Windows because host.rs is Windows-only. > > Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine). Thanks for catching that — fixed in 51a046d by importing COALESCE_GAP and coalesce_ranges in mod batch.
Marvisak commented 2026-11-25 04:06:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:30:06Z.


Automated triage for the maintainers.

Needs work (measurement only). The PR changes Batch in crates/victory-gpu/src/host.rs. It keeps a list of spans per host copy instead of one bounding range. In flush, coalesce_ranges merges spans less than 64 KiB (watch::PAGE) apart, so distant buffers in a large heap no longer pull the whole gap into shadow.update. current keeps the disjoint spans that are up to date. add keeps main's shadow.clean() early return. The code reads correctly. coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate and disjoint_ranges_skip_dirty_gap_pages cover the merge and the dirty pages in a gap. CI: passing (check).

This is a performance change with a trade-off that needs data. add now scans every current span of a copy on each call, and pending spans grow until flush. On a world submission that asks for about 9,000 copies, that could cost more than the gap copies it saves. Please post --features diagnostics numbers from the same retail scene on main and on this branch: the preparing: copies … ms … MB frame summary and the copies per frame line. If they show a drop, this can go to the maintainer.

On your reply: the build and log instructions for testers are clear. The verdict stays until such numbers are posted.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151973248 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7151973248). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T03:30:06Z. --- <!-- victory-triage v1 head=6d5f103192aa08d57051d54dc33a78d4456167bb verdict=needs-work at=2026-11-25T18:41:54Z --> Automated triage for the maintainers. **Needs work (measurement only).** The PR changes `Batch` in `crates/victory-gpu/src/host.rs`. It keeps a list of spans per host copy instead of one bounding range. In `flush`, `coalesce_ranges` merges spans less than 64 KiB (`watch::PAGE`) apart, so distant buffers in a large heap no longer pull the whole gap into `shadow.update`. `current` keeps the disjoint spans that are up to date. `add` keeps main's `shadow.clean()` early return. The code reads correctly. `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` and `disjoint_ranges_skip_dirty_gap_pages` cover the merge and the dirty pages in a gap. CI: passing (`check`). This is a performance change with a trade-off that needs data. `add` now scans every current span of a copy on each call, and pending spans grow until `flush`. On a world submission that asks for about 9,000 copies, that could cost more than the gap copies it saves. Please post `--features diagnostics` numbers from the same retail scene on `main` and on this branch: the `preparing: copies … ms … MB` frame summary and the `copies per frame` line. If they show a drop, this can go to the maintainer. On your reply: the build and log instructions for testers are clear. The verdict stays until such numbers are posted. --- _Generated by [Claude Code](https://claude.ai/code)_
DenRakEiw commented 2026-11-25 05:05:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:06:47Z.

Review: COMMENTED


Tested locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib host::tests:: — all 12 baseline passing tests (including the new coalesce_ranges tests) pass on both RTX 5090 and WARP (the 1 HEAP_VIEWS failure reproduces identically on untouched origin/main 940d32f)
  • cargo xtask policy — ok (183 files ok)
  • Clippy CI blocker: cargo clippy -p victory-gpu --all-targets reports 2 warnings (which will fail -D warnings in CI):
warning: very complex type used. Consider factoring parts into `type` definitions
warning: very complex type used. Consider factoring parts into `type` definitions
warning: `victory-gpu` (lib) generated 2 warnings

Holding off approval until those 2 clippy::type_complexity warnings are resolved.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051714972 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#pullrequestreview-6051714972). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:06:47Z. **Review:** COMMENTED --- Tested locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib host::tests::` — all 12 baseline passing tests (including the new `coalesce_ranges` tests) pass on both RTX 5090 and WARP (the 1 `HEAP_VIEWS` failure reproduces identically on untouched `origin/main` `940d32f`) - `cargo xtask policy` — ok (`183 files ok`) - **Clippy CI blocker:** `cargo clippy -p victory-gpu --all-targets` reports 2 warnings (which will fail `-D warnings` in CI): ``` warning: very complex type used. Consider factoring parts into `type` definitions warning: very complex type used. Consider factoring parts into `type` definitions warning: `victory-gpu` (lib) generated 2 warnings ``` Holding off approval until those 2 `clippy::type_complexity` warnings are resolved. AI-assisted (Gemini 4 Neon).
Texbirds commented 2026-11-25 13:02:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T12:16:04Z.


@DenRakEiw Thanks for testing and catching the clippy::type_complexity warnings — those are fixed in bf7bd1e using PendingRefresh / CurrentRefresh, and CI check is now passing (along with the Batch doc comment placement and the disjoint_ranges_skip_dirty_gap_pages test).

Regarding the remaining triage point on real-submission numbers: Step::Refresh (via Shadow::refresh) and Step::Heap (bindless::refresh) both submit sub-ranges into Batch::add for command lists that don't invoke memory::prepare (Step::Memory). I don't have a retail capture setup locally to collect --features diagnostics (PREPARING / copystat) before/after numbers — if a tester with a retail build can compare bf7bd1e vs main (940d32f) with --features diagnostics and share whether PREPARING bytes/time drop (or if Step::Memory dominates in practice and makes this unnecessary), that would decide whether this is worth merging.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162143200 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7162143200). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T12:16:04Z. --- @DenRakEiw Thanks for testing and catching the `clippy::type_complexity` warnings — those are fixed in `bf7bd1e` using `PendingRefresh` / `CurrentRefresh`, and CI `check` is now passing (along with the `Batch` doc comment placement and the `disjoint_ranges_skip_dirty_gap_pages` test). Regarding the remaining triage point on real-submission numbers: `Step::Refresh` (via `Shadow::refresh`) and `Step::Heap` (`bindless::refresh`) both submit sub-ranges into `Batch::add` for command lists that don't invoke `memory::prepare` (`Step::Memory`). I don't have a retail capture setup locally to collect `--features diagnostics` (`PREPARING` / `copystat`) before/after numbers — if a tester with a retail build can compare `bf7bd1e` vs `main` (`940d32f`) with `--features diagnostics` and share whether `PREPARING` bytes/time drop (or if `Step::Memory` dominates in practice and makes this unnecessary), that would decide whether this is worth merging.
Texbirds commented 2026-11-25 17:05:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T16:31:58Z.


Rebased onto main (67e8ae1) in 6d5f103. I kept main's new shadow.clean() early return in Batch::add, now len == 0 || shadow.clean(), and dropped the texture parts that main removed. The per-span PendingRefresh / CurrentRefresh logic and both tests are unchanged. The open triage point is the same: this still needs before/after PREPARING / copystat numbers from a real submission.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168130334 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7168130334). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T16:31:58Z. --- Rebased onto `main` (`67e8ae1`) in `6d5f103`. I kept `main`'s new `shadow.clean()` early return in `Batch::add`, now `len == 0 || shadow.clean()`, and dropped the texture parts that `main` removed. The per-span `PendingRefresh` / `CurrentRefresh` logic and both tests are unchanged. The open triage point is the same: this still needs before/after `PREPARING` / `copystat` numbers from a real submission.
Texbirds commented 2026-11-25 18:02:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T17:13:21Z.


For anyone able to run the diagnostics comparison, the current pair is 67e8ae1 (main) and 6d5f103 (this PR). Build both with:

cargo build --profile diag -p victory --features diagnostics

Please run the same world scene with the same settings after warm-up and share a few matching log intervals with:

  • The frame summary, including preparing: copies … ms … MB
  • The copies per frame line, including refresh batches and title memory

Those numbers will help show whether the time saved by skipping gap copies outweighs the cost of tracking spans. CI passed on 6d5f103. The retail comparison is still pending.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169186081 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7169186081). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T17:13:21Z. --- For anyone able to run the diagnostics comparison, the current pair is **`67e8ae1` (main)** and **`6d5f103` (this PR)**. Build both with: ```powershell cargo build --profile diag -p victory --features diagnostics ``` Please run the same world scene with the same settings after warm-up and share a few matching log intervals with: - The frame summary, including `preparing: copies … ms … MB` - The `copies per frame` line, including `refresh batches` and `title memory` Those numbers will help show whether the time saved by skipping gap copies outweighs the cost of tracking spans. [CI passed on `6d5f103`](https://github.com/Marvisak/VIctory/actions/runs/45654471905/job/139347347659). The retail comparison is still pending.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1555:upstream-pr/1555'
git switch 'upstream-pr/1555'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1555'
git switch 'upstream-pr/1555'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1555'
git switch 'upstream-pr/1555'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1555'
git switch 'main'
git merge --squash 'upstream-pr/1555'
git switch 'main'
git merge --ff-only 'upstream-pr/1555'
git switch 'main'
git merge 'upstream-pr/1555'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1554
No description provided.