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

Closed
github-bot wants to merge 2 commits from github-bot/gh-3219 into main AGit
Owner

@⁠danielibabet on GitHub · 2026-11-26 13:33 UTC · original
danielibabet:pr/cpu-gpu-overlap → main · +277 −77 · 11 files · 2 commits · closed without merging

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
**@⁠danielibabet** on GitHub · 2026-11-26 13:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3219) `danielibabet:pr/cpu-gpu-overlap` → `main` · +277 −77 · 11 files · 2 commits · closed without merging ## 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
Move texture::recycle() from bindless::uploads() (start of execute) into
Host::copy_back() immediately after wait_for(): staging chunk memory and
upload buffers from submission N are recycled as soon as the GPU fence for
N fires, while the submission lock is held.  This guarantees two things:

1. Post-fence texture invalidation: refresh_all() / hash comparison for
   submission N+1 always runs after copy_back(N) has completed and after
   the GPU's writes are fully committed to title memory.  The staging pool
   is in a known-clean state before each new execute() builds its upload
   list, so the hash sees the final post-GPU state of title memory.

2. Back-pressure (depth-1 in-flight): within a pump pass, run() calls
   copy_back(previous) -- which now includes recycle() -- before every
   execute(next).  Staging is never recycled while the GPU is still reading
   it.  Between passes the between-pass flush waits for the last in-flight
   submission before a new pass begins.  The queue depth is capped at 1
   at all points.

cargo test --workspace: 233 passed, 2 pre-existing failures unchanged
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: ok
cargo run -p xtask -- policy: 192 files ok
cargo build --release -p victory --locked: ok
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:35:54 +00:00
github-bot closed this pull request 2026-11-26 13:41:59 +00:00
Author
Owner

@⁠danielibabet on GitHub · 2026-11-26 13:39 UTC · original

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.

**@⁠danielibabet** on GitHub · 2026-11-26 13:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/3219#issuecomment-7193658811) 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

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