GPU: overlap CPU preparation with GPU execution via per-submission fences #3217

Closed
danielibabet wants to merge 2 commits from upstream-pr/3219 into main
danielibabet commented 2026-11-26 13:33:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T13:33:50Z.


Summary

Overlap CPU preparation with GPU execution by splitting Host::execute into a non-blocking submit and a deferred copy_back. This addresses the shortcut noted in host.rs: "every submission is waited for, so CPU and GPU never overlap".

Problem

Host::execute called ExecuteCommandLists, then immediately signalled the fence and blocked with WaitForSingleObject(INFINITE) before returning. At ~1.5 fps with ~500 ms of GPU work per frame, the CPU spent essentially the entire frame idle waiting for the GPU.

Solution

Two-phase submit:

  • Host::execute — submits the command lists, signals the fence, returns immediately with a SubmissionData token (fence value + batch data). CPU continues preparing the next submission.
  • Host::copy_back — waits for the fence, then performs all shadow/BOP writes back to title memory. Called at the start of the next submission, so the GPU runs frame N while the CPU prepares frame N+1.

A new COPY_BACK_WAIT_US atomic exposes the residual fence wait time as a waiting GPU field in the fps log line.

The texture recycle() call was moved from bindless::uploads() (before execute) into copy_back() (after fence wait), so staging chunk memory is released strictly post-GPU and hash comparison always sees the final post-GPU state of title memory — preventing spurious full-texture invalidations.

Measured results (i9-14900KF + RTX 4070, GTA VI world)

Metric Before After Delta
fps ~1.5 ~1.6–1.9 +~15%
ms/frame ~650 ms ~550–640 ms −~10%
preparing ~100 ms ~87–110 ms similar
textures ~5–14 ms, 2–7 MB ~1–4 ms, 1–5 MB ✅
on the GPU ~500 ms ~1.5 ms moved to waiting GPU
waiting GPU — ~400–500 ms GPU exec time, now measured
copying back ~52 ms ~40–45 ms −15%

The waiting GPU field replaces on the GPU as the measure of GPU execution time. The overlap benefit is currently limited by the GPU execution time (~450 ms) dominating CPU preparation (~90 ms); the CPU finishes preparing before the GPU finishes executing, so the overlap window is not fully exploited. Further gains require reducing GPU execution time (more pipelines compiled, indirect dispatch fixes).

Tests

  • cargo test --workspace: 233 passed, 2 pre-existing failures on main unrelated to this change
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask -- policy: 192 files OK
  • cargo build --release -p victory --locked: clean
<!-- greyfinch-sync:Marvisak/VIctory:issue:6627785950 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3219). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T13:33:50Z. --- ## Summary Overlap CPU preparation with GPU execution by splitting `Host::execute` into a non-blocking submit and a deferred `copy_back`. This addresses the `shortcut` noted in `host.rs`: "every submission is waited for, so CPU and GPU never overlap". ## Problem `Host::execute` called `ExecuteCommandLists`, then immediately signalled the fence and blocked with `WaitForSingleObject(INFINITE)` before returning. At ~1.5 fps with ~500 ms of GPU work per frame, the CPU spent essentially the entire frame idle waiting for the GPU. ## Solution Two-phase submit: - **`Host::execute`** — submits the command lists, signals the fence, returns immediately with a `SubmissionData` token (fence value + batch data). CPU continues preparing the next submission. - **`Host::copy_back`** — waits for the fence, then performs all shadow/BOP writes back to title memory. Called at the start of the *next* submission, so the GPU runs frame N while the CPU prepares frame N+1. A new `COPY_BACK_WAIT_US` atomic exposes the residual fence wait time as a `waiting GPU` field in the fps log line. The texture `recycle()` call was moved from `bindless::uploads()` (before execute) into `copy_back()` (after fence wait), so staging chunk memory is released strictly post-GPU and hash comparison always sees the final post-GPU state of title memory — preventing spurious full-texture invalidations. ## Measured results (i9-14900KF + RTX 4070, GTA VI world) | Metric | Before | After | Delta | |---|---|---|---| | fps | ~1.5 | ~1.6–1.9 | +~15% | | ms/frame | ~650 ms | ~550–640 ms | −~10% | | preparing | ~100 ms | ~87–110 ms | similar | | textures | ~5–14 ms, 2–7 MB | ~1–4 ms, 1–5 MB | ✅ | | on the GPU | ~500 ms | ~1.5 ms | moved to waiting GPU | | waiting GPU | — | ~400–500 ms | GPU exec time, now measured | | copying back | ~52 ms | ~40–45 ms | −15% | The `waiting GPU` field replaces `on the GPU` as the measure of GPU execution time. The overlap benefit is currently limited by the GPU execution time (~450 ms) dominating CPU preparation (~90 ms); the CPU finishes preparing before the GPU finishes executing, so the overlap window is not fully exploited. Further gains require reducing GPU execution time (more pipelines compiled, indirect dispatch fixes). ## Tests - `cargo test --workspace`: 233 passed, 2 pre-existing failures on `main` unrelated to this change - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings - `cargo run -p xtask -- policy`: 192 files OK - `cargo build --release -p victory --locked`: clean
upstream-sync closed this pull request 2026-11-26 14:01:40 +00:00
danielibabet commented 2026-11-26 14:01:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T13:39:41Z.


Closing after review: \pump()\ still calls \copy_back()\ (and blocks on \wait_for()) before the next \xecute(), so CPU preparation does not overlap with GPU execution yet, and #3120 already covers submission overlap. Keeping #3218 open as a focused change.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7193658811 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3219#issuecomment-7193658811). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T13:39:41Z. --- Closing after review: \pump()\ still calls \copy_back()\ (and blocks on \wait_for()\) before the next \xecute()\, so CPU preparation does not overlap with GPU execution yet, and #3120 already covers submission overlap. Keeping #3218 open as a focused change.

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