gpu: refresh the submission's buffer copies on several threads #3305

Closed
borosf wants to merge 1 commit from upstream-pr/3305 into main
borosf commented 2026-11-26 16:38:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T16:38:57Z.


What

Batch::flush ran each pending copy's Shadow::update on the submitting thread, one after another. A world frame asks for ~80 copies moving ~265 MB through a single ReadProcessMemory thread.

Each copy appears at most once in the batch (pending is keyed by the shadow's pointer), so the updates touch disjoint state — Shadow is Send + Sync (shadow.rs:137-139) and holds its own locks; the copy itself is ReadProcessMemory, which is thread-safe. The flush now drains the refreshes and runs the updates through the existing texture::in_parallel (up to 4 threads, same helper the texture refresh uses). The current-range merge stays serial afterwards, in the original order, so its result is unchanged. copystat/PREPARING accounting is atomic and per-item, unchanged in meaning.

Claimed on #1633 (parallelize serial submission-prep buffer copies). Implemented at the Batch::flush level, across shadows — not inside Shadow::update — to stay clear of NEW-… partial-page-granularity work on that function.

Measurement (Windows 11, Intel Arc B580 host, 12 cores, synthetic only — no game data)

New test host::batch::many_copies_refresh_in_parallel_and_copy_the_same_bytes: 16 write-watched pools x 64 pages (64 MiB), all pages dirty, one Batch holding all 16 copies:

cargo test -p victory-gpu --lib --locked -- --test-threads=1 --exact host::batch::many_copies_refresh_in_parallel_and_copy_the_same_bytes --nocapture
flush of 16 copies x 64 pages (64 MiB): parallel 6.547ms, serial 8.043ms
flush of 16 copies x 64 pages (64 MiB): parallel 7.117ms, serial 8.079ms
flush of 16 copies x 64 pages (64 MiB): parallel 6.514ms, serial 8.214ms

~1.2-1.3x on the pure copy path (same-process RPM already saturates ~8 GB/s single-thread on this host, so the synthetic ceiling is low); the test asserts the host copies hold exactly the title's bytes after both passes. On the real frame the per-copy kernel work that dominates (write-watch queries, soft faults on fresh snapshot buffers, the unmapped-page fallback walks) also runs inside update, so the ~927 ms copy phase of the 1,842 ms world frame is what this parallelizes across up to 4 threads — title-side number to be taken by whoever can run it.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings — clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — clean
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 230 passed, 2 failed: the known B580 baseline failures pristine main also fails (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views, #1744/#1829)
  • fmt gate: rustfmt --config-path rustfmt.toml --edition 2024 --check crates/victory-gpu/src/host.rs — 85 diff hunks, identical to the origin/main baseline of the same file (repo is deliberately not default-fmt clean; no new hunks)
  • cargo xtask policy — 193 files ok

No game data used anywhere; synthetic pools only.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5513821869 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T16:38:57Z. --- ## What `Batch::flush` ran each pending copy's `Shadow::update` on the submitting thread, one after another. A world frame asks for ~80 copies moving ~265 MB through a single `ReadProcessMemory` thread. Each copy appears at most once in the batch (`pending` is keyed by the shadow's pointer), so the updates touch disjoint state — `Shadow` is `Send + Sync` (shadow.rs:137-139) and holds its own locks; the copy itself is `ReadProcessMemory`, which is thread-safe. The flush now drains the refreshes and runs the updates through the existing `texture::in_parallel` (up to 4 threads, same helper the texture refresh uses). The `current`-range merge stays serial afterwards, in the original order, so its result is unchanged. `copystat`/`PREPARING` accounting is atomic and per-item, unchanged in meaning. Claimed on #1633 (parallelize serial submission-prep buffer copies). Implemented at the `Batch::flush` level, across shadows — not inside `Shadow::update` — to stay clear of NEW-… partial-page-granularity work on that function. ## Measurement (Windows 11, Intel Arc B580 host, 12 cores, synthetic only — no game data) New test `host::batch::many_copies_refresh_in_parallel_and_copy_the_same_bytes`: 16 write-watched pools x 64 pages (64 MiB), all pages dirty, one `Batch` holding all 16 copies: ``` cargo test -p victory-gpu --lib --locked -- --test-threads=1 --exact host::batch::many_copies_refresh_in_parallel_and_copy_the_same_bytes --nocapture ``` ``` flush of 16 copies x 64 pages (64 MiB): parallel 6.547ms, serial 8.043ms flush of 16 copies x 64 pages (64 MiB): parallel 7.117ms, serial 8.079ms flush of 16 copies x 64 pages (64 MiB): parallel 6.514ms, serial 8.214ms ``` ~1.2-1.3x on the pure copy path (same-process RPM already saturates ~8 GB/s single-thread on this host, so the synthetic ceiling is low); the test asserts the host copies hold exactly the title's bytes after both passes. On the real frame the per-copy kernel work that dominates (write-watch queries, soft faults on fresh snapshot buffers, the unmapped-page fallback walks) also runs inside `update`, so the ~927 ms copy phase of the 1,842 ms world frame is what this parallelizes across up to 4 threads — title-side number to be taken by whoever can run it. ## Verification - `cargo clippy --workspace --all-targets --locked -- -D warnings` — clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — clean - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 230 passed, 2 failed: the known B580 baseline failures pristine main also fails (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`, #1744/#1829) - fmt gate: `rustfmt --config-path rustfmt.toml --edition 2024 --check crates/victory-gpu/src/host.rs` — 85 diff hunks, identical to the origin/main baseline of the same file (repo is deliberately not default-fmt clean; no new hunks) - `cargo xtask policy` — 193 files ok No game data used anywhere; synthetic pools only.
borosf commented 2026-11-26 16:40:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T16:40:07Z.


Claimed on #1633 in comment 7195935695 (parallelize the serial submission-prep buffer copies). Head 4ed54fc. Will post done: NEW-n -> #3305 once the ledger bot assigns the id.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197943753 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305#issuecomment-7197943753). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T16:40:07Z. --- Claimed on #1633 in [comment 7195935695](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7195935695) (parallelize the serial submission-prep buffer copies). Head 4ed54fc. Will post `done: NEW-n -> #3305` once the ledger bot assigns the id.
Dukestar69 commented 2026-11-26 16:47:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T16:47:16Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Parallelizing disjoint Shadow::update calls in Batch::flush via texture::in_parallel (deduplicated per Shadow in pending, re-ordered by index before updating self.current) is clean and sound.
  • cargo test -p victory-gpu --lib host::batch:: -- --test-threads=1 (RTX 5090): 2 passed, 0 failed (including many_copies_refresh_in_parallel_and_copy_the_same_bytes).
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1 (RTX 5090): 10 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6077789237 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305#pullrequestreview-6077789237). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T16:47:16Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Parallelizing disjoint `Shadow::update` calls in `Batch::flush` via `texture::in_parallel` (deduplicated per `Shadow` in `pending`, re-ordered by index before updating `self.current`) is clean and sound. - `cargo test -p victory-gpu --lib host::batch:: -- --test-threads=1` (RTX 5090): 2 passed, 0 failed (including `many_copies_refresh_in_parallel_and_copy_the_same_bytes`). - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1` (RTX 5090): 10 passed, 0 failed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 22:24:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:24:24Z.

Review: APPROVED


Looks good. Batch::add deduplicates entries by Arc::as_ptr(shadow) via self.pending, so each work[i] in Batch::flush (crates/victory-gpu/src/host.rs:2440-2484) operates on a distinct Shadow, and sorting done by index before updating self.current keeps the state merge deterministic. Distinct shadows overlapping the same 64 KiB pages or physical-page aliases still serialize page protection and dirty-set transitions safely through GUARDS -> Changed.0 inside watch::Tracked::take_split.

Verified merged onto origin/main (63bcca43) on a 16C/32T host with RTX 5090 and WARP:

  • cargo xtask policy (191 files ok) and cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics) pass cleanly.
  • host::batch:: (2/2) and shadow::tests:: (9/9) pass on both RTX 5090 and WARP (--features diagnostics, VICTORY_D3D12_DEBUG=1).
  • Measured 64 MiB flush (many_copies_refresh_in_parallel_and_copy_the_same_bytes): 4.84 ms parallel vs 7.75 ms serial (default build); 6.09 ms vs 9.34 ms on RTX 5090 and 6.61 ms vs 9.08 ms on WARP (diagnostics + Debug Layer).
  • Stress-tested 20 rounds of parallel Batch::flush with 1 outer 2 MiB shadow, 8 distinct overlapping inner shadows, 1 twin_views physical-alias shadow, and 4 active non-marked GPU write snapshots (sync_before) on both RTX 5090 and WARP under VICTORY_D3D12_DEBUG=1: all pages matched across every overlapping/aliased shadow with zero lost dirty marks or deadlocks.

Minor non-blocking observation: PREPARING[0] (host.rs:2458) now sums per-worker thread microseconds inside in_parallel (matching PREPARING[2] in texture.rs:549), while copystat::C::FlushUs tracks wall-clock flush time.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081101964 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305#pullrequestreview-6081101964). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:24:24Z. **Review:** APPROVED --- Looks good. `Batch::add` deduplicates entries by `Arc::as_ptr(shadow)` via `self.pending`, so each `work[i]` in `Batch::flush` (`crates/victory-gpu/src/host.rs:2440-2484`) operates on a distinct `Shadow`, and sorting `done` by index before updating `self.current` keeps the state merge deterministic. Distinct shadows overlapping the same 64 KiB pages or physical-page aliases still serialize page protection and dirty-set transitions safely through `GUARDS -> Changed.0` inside `watch::Tracked::take_split`. Verified merged onto `origin/main` (`63bcca43`) on a 16C/32T host with RTX 5090 and WARP: - `cargo xtask policy` (191 files ok) and `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`) pass cleanly. - `host::batch::` (2/2) and `shadow::tests::` (9/9) pass on both RTX 5090 and WARP (`--features diagnostics`, `VICTORY_D3D12_DEBUG=1`). - Measured 64 MiB flush (`many_copies_refresh_in_parallel_and_copy_the_same_bytes`): 4.84 ms parallel vs 7.75 ms serial (default build); 6.09 ms vs 9.34 ms on RTX 5090 and 6.61 ms vs 9.08 ms on WARP (`diagnostics` + Debug Layer). - Stress-tested 20 rounds of parallel `Batch::flush` with 1 outer 2 MiB shadow, 8 distinct overlapping inner shadows, 1 `twin_views` physical-alias shadow, and 4 active non-marked GPU write snapshots (`sync_before`) on both RTX 5090 and WARP under `VICTORY_D3D12_DEBUG=1`: all pages matched across every overlapping/aliased shadow with zero lost dirty marks or deadlocks. Minor non-blocking observation: `PREPARING[0]` (`host.rs:2458`) now sums per-worker thread microseconds inside `in_parallel` (matching `PREPARING[2]` in `texture.rs:549`), while `copystat::C::FlushUs` tracks wall-clock flush time.
neonbyte1 commented 2026-11-27 07:05:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T07:05:50Z.


Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is main plus the unmerged PRs this setup needs (#3312, #3318, #2472 and others, listed in #3425). This PR merged on top, 600 s runs, medians of the last 30 frame-rate lines, against a control run of the same base taken next to it (frame times drift by up to ~25 ms between runs hours apart).

Frame Preparing Copy-back
control 402 ms 81 ms 53 ms
+ this PR 408 ms 79 ms 51 ms

About 2 ms off preparing, inside the spread of the controls (77-81 ms). In this scene refreshes cost ~24 ms of preparing, of which the change checks are ~13.5 ms and waiting on watch::GUARDS ~5 ms; the fan-out across shadows doesn't show up in the frame. Its parallel-flush test passes under Wine.

AI-assisted (Claude); measured on my machine, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7212138808 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305#issuecomment-7212138808). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-27T07:05:50Z. --- Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is `main` plus the unmerged PRs this setup needs (#3312, #3318, #2472 and others, listed in #3425). This PR merged on top, 600 s runs, medians of the last 30 frame-rate lines, against a control run of the same base taken next to it (frame times drift by up to ~25 ms between runs hours apart). | | Frame | Preparing | Copy-back | |---|---|---|---| | control | 402 ms | 81 ms | 53 ms | | + this PR | 408 ms | 79 ms | 51 ms | About 2 ms off preparing, inside the spread of the controls (77-81 ms). In this scene refreshes cost ~24 ms of preparing, of which the change checks are ~13.5 ms and waiting on `watch::GUARDS` ~5 ms; the fan-out across shadows doesn't show up in the frame. Its parallel-flush test passes under Wine. _AI-assisted (Claude); measured on my machine, no game data shared._
borosf commented 2026-11-27 07:33:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T07:33:27Z.


Closing: this duplicates #2051 (@0xkn0wh3r3, "a submission's refreshes copy on several threads"), which does the same thing at the same level — drains Batch::flush's pending refreshes through the existing texture::in_parallel and keeps the current merge serial in the original order. #2051 was opened first and is the one to take.

The one thing of mine worth carrying over is the measurement, since #2051's body has none. Windows 11, Intel Arc B580, 12 cores, synthetic (no game data): 16 write-watched pools of 64 pages (64 MiB) dirty in one Batch, three runs —

flush of 16 copies x 64 pages (64 MiB): parallel 6.547 ms, serial 8.043 ms
flush of 16 copies x 64 pages (64 MiB): parallel 7.117 ms, serial 8.079 ms
flush of 16 copies x 64 pages (64 MiB): parallel 6.514 ms, serial 8.214 ms

~1.2-1.3x on the pure copy path — the synthetic ceiling is low because same-process ReadProcessMemory already saturates ~8 GB/s single-threaded here, while on a real frame the per-copy kernel work (write-watch asks, soft faults on fresh snapshot buffers, the unmapped-page fallback) also runs inside update and is what a second thread actually takes off the critical path. I'll put these numbers on #2051 rather than leave them here.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7212588484 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3305#issuecomment-7212588484). Originally posted by [borosf](https://github.com/borosf) on 2026-11-27T07:33:27Z. --- Closing: this duplicates **#2051** (@0xkn0wh3r3, "a submission's refreshes copy on several threads"), which does the same thing at the same level — drains `Batch::flush`'s pending refreshes through the existing `texture::in_parallel` and keeps the `current` merge serial in the original order. #2051 was opened first and is the one to take. The one thing of mine worth carrying over is the measurement, since #2051's body has none. Windows 11, Intel Arc B580, 12 cores, synthetic (no game data): 16 write-watched pools of 64 pages (64 MiB) dirty in one `Batch`, three runs — ``` flush of 16 copies x 64 pages (64 MiB): parallel 6.547 ms, serial 8.043 ms flush of 16 copies x 64 pages (64 MiB): parallel 7.117 ms, serial 8.079 ms flush of 16 copies x 64 pages (64 MiB): parallel 6.514 ms, serial 8.214 ms ``` ~1.2-1.3x on the pure copy path — the synthetic ceiling is low because same-process `ReadProcessMemory` already saturates ~8 GB/s single-threaded here, while on a real frame the per-copy kernel work (write-watch asks, soft faults on fresh snapshot buffers, the unmapped-page fallback) also runs inside `update` and is what a second thread actually takes off the critical path. I'll put these numbers on #2051 rather than leave them here.

Pull request closed

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!3305
No description provided.