GPU: wait before overwriting an in-flight texture staging buffer under VICTORY_LATE_WAIT/RELAY #141

Open
github-bot wants to merge 1 commit from github-bot/gh-141 into main AGit
Owner

@⁠adritoyalser on GitHub · 2026-11-24 21:49 UTC · original
adritoyalser:fix/late-staging-hazard → main · +183 −33 · 2 files · 1 commits · open

Problem

Each title-written texture uses a single mapped UPLOAD heap staging buffer (Staging::buffer). Under VICTORY_LATE_WAIT / VICTORY_RELAY, Host::submit checked late_blocked (whether an earlier flight's shaders might write the texture's title memory), but did not check whether an earlier unfinished flight was still reading that Staging::buffer on the GPU via CopyTextureRegion.

When two overlapping flights touch the same CPU-updated texture (such as subtitles, glyph atlases, or dynamic UI/video textures — see e.g. overlapping subtitles under VICTORY_RELAY=2 in #89), Staging::refresh → detile could overwrite Staging::buffer on the CPU while the earlier flight's GPU copy was still reading it.

Fix

  • Track the highest late-submission number that reads each Staging buffer (Staging::in_flight in crates/victory-gpu/src/texture.rs).
  • In Host::submit (crates/victory-gpu/src/host.rs), when late is enabled:
    • Before preparing runs, if any Step::Texture(staging) has staging.in_flight() > FINISHED, wait until it lands (relay_wait under VICTORY_RELAY=2, self.settle otherwise).
    • Mark staging.mark_in_flight(this_submission) when sending a late submission containing Step::Texture(staging).
  • Zero overhead when VICTORY_LATE_WAIT/VICTORY_RELAY are off, or when flights touch different textures.
  • Add unit test staging_in_flight_tracks_submission_completion in host::late_tests.

Verification

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — clean
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings — clean
  • cargo test -p victory-gpu --locked — 111 passed, 0 failed, 1 ignored
**@⁠adritoyalser** on GitHub · 2026-11-24 21:49 UTC · [original](https://github.com/fxceless/victory-fork/pull/141) `adritoyalser:fix/late-staging-hazard` → `main` · +183 −33 · 2 files · 1 commits · open ### Problem Each title-written texture uses a single mapped `UPLOAD` heap staging buffer (`Staging::buffer`). Under `VICTORY_LATE_WAIT` / `VICTORY_RELAY`, `Host::submit` checked `late_blocked` (whether an earlier flight's shaders might write the texture's title memory), but did not check whether an earlier unfinished flight was still reading that `Staging::buffer` on the GPU via `CopyTextureRegion`. When two overlapping flights touch the same CPU-updated texture (such as subtitles, glyph atlases, or dynamic UI/video textures — see e.g. overlapping subtitles under `VICTORY_RELAY=2` in #89), `Staging::refresh` → `detile` could overwrite `Staging::buffer` on the CPU while the earlier flight's GPU copy was still reading it. ### Fix - Track the highest late-submission number that reads each `Staging` buffer (`Staging::in_flight` in `crates/victory-gpu/src/texture.rs`). - In `Host::submit` (`crates/victory-gpu/src/host.rs`), when `late` is enabled: - Before preparing `runs`, if any `Step::Texture(staging)` has `staging.in_flight() > FINISHED`, wait until it lands (`relay_wait` under `VICTORY_RELAY=2`, `self.settle` otherwise). - Mark `staging.mark_in_flight(this_submission)` when sending a late submission containing `Step::Texture(staging)`. - Zero overhead when `VICTORY_LATE_WAIT`/`VICTORY_RELAY` are off, or when flights touch different textures. - Add unit test `staging_in_flight_tracks_submission_completion` in `host::late_tests`. ### Verification - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — clean - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` — clean - `cargo test -p victory-gpu --locked` — 111 passed, 0 failed, 1 ignored
Under `VICTORY_LATE_WAIT` / `VICTORY_RELAY`, a late submission can still be
executing `CopyTextureRegion` from a `Staging` upload buffer on the GPU
when a subsequent submission refreshes the same texture and detiles new
contents into that mapped upload buffer.

Track `Staging::in_flight` (the highest late submission number that
actually recorded a `CopyTextureRegion` reading that staging buffer, via
`Step::Texture(staging, uploads)`), and wait for that flight right before
`Staging::detile()` overwrites the buffer. Unchanged textures and skipped
uploads incur zero waits or settles.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:52 +00:00
Author
Owner

@⁠tycosnh on GitHub · 2026-11-25 00:42 UTC · original

Test of f909bf0 against #168 on an RTX 4090: walls not restored, late modes about 50 ms/frame slower

I repeated #168's method on 4e17478 (main) and on this PR's head f909bf0.

Short answer: on this machine the PR does not bring the first room's walls back under VICTORY_RELAY=2 or the late-wait set. It costs about +48 ms/frame under RELAY=2 and +52 ms/frame under the late-wait set, with no change on defaults. Here the walls also go missing in default runs, decided per run, so #168's split (walls in every default run, missing in every late run) did not reproduce.

Setup

  • GPU: NVIDIA GeForce RTX 4090, driver 621.24. TdrDelay/TdrDdiDelay raised to 60 s (#168 used the default).
  • Windows 11 Pro build 26300, native D3D12
  • Title: own copy of 1.0.2.1, Series X profile
  • Builds: cargo build --profile diag -p victory --features diagnostics --locked, toolchain 1.98.1, separate target dirs for the two commits
  • cargo test --workspace --locked on 4e17478: 496 passed, 0 failed, 7 ignored (same as #168)

Method

  • Save: one cold-cache warm-up run (25 min, defaults) autosaved the checkpoint. Every run below starts from its own fresh copy of that save (VICTORY_DATA_DIR) and loads to the player at the window of the first room, with the world showing about 56 s after launch.
  • Input and length: VICTORY_KEYS=10:Enter,15:Enter,20:Enter,25:Enter and nothing after that. Each run lasts 540 s and ends with WM_SYSCOMMAND/SC_CLOSE. All runs exited with code 0 within 5 s, none had to be killed.
  • Cache: the shader cache is keyed by the executable, so each build has its own cache, warmed by an extra run from the same save. No pipelines were still being made after about 3.5 min.
  • Order: runs were interleaved (A1 B1 P-B1 C1 P-C1 B2 P-B2 A2 P-A1). Each started with an environment holding only the basic Windows variables plus the VICTORY_* ones. No network endpoints were open for the process at any check.
  • Numbers: medians of the frame lines between 300 and 540 s. "Mesh groups" is the mean of indirect mesh dispatches: ... N groups asked per frame over the same window.
  • Frames: 3 to 6 per run in the same window, saved with VICTORY_CAPTURE_FRAMES and looked at locally (not attached).

Results

Run Build Switches ms/frame (min-max) fps draws preparing / GPU / copying back (ms) card busy waiting for the card mesh groups walls grille
A1 4e17478 none 292 (286-320) 3.4 4,800 76 / 170 / 40 58% 170 ms 24,847 missing drawn
A2 4e17478 none 295 (289-332) 3.4 4,812 77 / 172 / 40 58% 173 ms 26,120 missing drawn
B1 4e17478 VICTORY_RELAY=2 220 (215-554) 4.5 4,798 92 / 118 / 190 78% 118 ms 13,706 missing missing
B2 4e17478 VICTORY_RELAY=2 253 (244-276) 3.9 4,892 95 / 146 / 219 81% 147 ms 15,617 drawn missing
C1 4e17478 late-wait set¹ 182 (176-260) 5.5 4,651 57 / 88 / 25 77% 93 ms 20,820 drawn drawn
P-A1 f909bf0 none 298 (292-631) 3.4 4,779 77 / 177 / 40 59% 177 ms 22,897 drawn drawn
P-B1 f909bf0 VICTORY_RELAY=2 278 (271-286) 3.6 4,761 78 / 195 / 63 61% 195 ms 25,049 missing missing
P-B2 f909bf0 VICTORY_RELAY=2 290 (267-315) 3.5 4,844 76 / 208 / 64 64% 207 ms 25,824 missing missing
P-C1 f909bf0 late-wait set¹ 234 (218-262) 4.3 4,790 57 / 142 / 26 67% 144 ms 25,388 missing missing

¹ VICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=1024

None of them logged an ERROR line or lost the device. Every run refused the same pipelines once at startup with 0x80070057: compute 02384a60fda6f2d0, 2cc4c197ee29e291, 2da4beb386509b77 and 369cbe906ea72764, and mesh f5e50eb2108c425b with pixel ce3f0432a08f1d35. The 4e17478 runs also refused compute 6b82bc7d22016cb5.

1. The walls are not restored, and here they are lost in default runs too

  • Within a run the walls are either there in every captured frame or missing in every one.
  • Missing looks the same as in #168: sky and horizon on the left, the posters as dark rectangles hanging in front of sky on the right, and the window frame standing on its own.
  • Drawn means a dark block wall on the left and a wall with a photo poster on the right.
  • Main: lost the walls in both default runs and in B1, and drew them in B2 and C1.
  • This PR: drew them with defaults (P-A1) and lost them in all three late-mode runs.
  • Warm-up runs (not in the table): agree. The main build's default run from the save lost the walls, and this PR's default run drew them.

With one or two runs per arm and walls missing in default runs too, this can't show that the PR makes the walls worse. It does show it doesn't bring them back.

The grille follows the switches more closely than the walls. It is missing in all four RELAY=2 runs and in P-C1, and drawn in every default run and in C1. Skin is present in every run. No subtitles were on screen in any captured frame, so I couldn't check the overlapping subtitles from #89.

2. The wait serializes the late submissions

The new wait is counted as "title memory read" in the settle lines. Compared with the same mode on 4e17478:

4e17478 f909bf0
RELAY=2: settles per frame 3 about 15
RELAY=2: waiting under "title memory read" 9.5 ms/frame 196 ms/frame
Late-wait set: settles per frame 3 about 18
Late-wait set: flights per settle 10.2 2.3

All of the waiting in settles is now under that reason.

GPU time per frame goes up and the card is less busy. With the PR, RELAY=2 runs at 278 to 290 ms against 292 to 298 ms on defaults, so almost all of its speed-up is gone.

The mesh group count also comes back to the default level, about 25,000 against 13,700 to 15,600 on 4e17478 with RELAY=2. That fits #168's point that part of the relay's speed-up was geometry that wasn't drawn. Under the late-wait set it rises from 20,800 to 25,400.


Test report written with Claude Code (Claude Opus 5.5), from real runs on the hardware named above. No game data attached.

**@⁠tycosnh** on GitHub · 2026-11-25 00:42 UTC · [original](https://github.com/fxceless/victory-fork/pull/141#issuecomment-7148800187) ## Test of `f909bf0` against #168 on an RTX 4090: walls not restored, late modes about 50 ms/frame slower I repeated #168's method on `4e17478` (main) and on this PR's head `f909bf0`. **Short answer:** on this machine the PR does not bring the first room's walls back under `VICTORY_RELAY=2` or the late-wait set. It costs about +48 ms/frame under `RELAY=2` and +52 ms/frame under the late-wait set, with no change on defaults. Here the walls also go missing in default runs, decided per run, so #168's split (walls in every default run, missing in every late run) did not reproduce. ### Setup - GPU: NVIDIA GeForce RTX 4090, driver 621.24. `TdrDelay`/`TdrDdiDelay` raised to 60 s (#168 used the default). - Windows 11 Pro build 26300, native D3D12 - Title: own copy of 1.0.2.1, Series X profile - Builds: `cargo build --profile diag -p victory --features diagnostics --locked`, toolchain 1.98.1, separate target dirs for the two commits - `cargo test --workspace --locked` on `4e17478`: 496 passed, 0 failed, 7 ignored (same as #168) ### Method - **Save:** one cold-cache warm-up run (25 min, defaults) autosaved the checkpoint. Every run below starts from its own fresh copy of that save (`VICTORY_DATA_DIR`) and loads to the player at the window of the first room, with the world showing about 56 s after launch. - **Input and length:** `VICTORY_KEYS=10:Enter,15:Enter,20:Enter,25:Enter` and nothing after that. Each run lasts 540 s and ends with `WM_SYSCOMMAND`/`SC_CLOSE`. All runs exited with code 0 within 5 s, none had to be killed. - **Cache:** the shader cache is keyed by the executable, so each build has its own cache, warmed by an extra run from the same save. No pipelines were still being made after about 3.5 min. - **Order:** runs were interleaved (A1 B1 P-B1 C1 P-C1 B2 P-B2 A2 P-A1). Each started with an environment holding only the basic Windows variables plus the `VICTORY_*` ones. No network endpoints were open for the process at any check. - **Numbers:** medians of the frame lines between 300 and 540 s. "Mesh groups" is the mean of `indirect mesh dispatches: ... N groups asked per frame` over the same window. - **Frames:** 3 to 6 per run in the same window, saved with `VICTORY_CAPTURE_FRAMES` and looked at locally (not attached). ### Results | Run | Build | Switches | ms/frame (min-max) | fps | draws | preparing / GPU / copying back (ms) | card busy | waiting for the card | mesh groups | walls | grille | |---|---|---|---:|---:|---:|---:|---:|---:|---:|---|---| | A1 | `4e17478` | none | 292 (286-320) | 3.4 | 4,800 | 76 / 170 / 40 | 58% | 170 ms | 24,847 | missing | drawn | | A2 | `4e17478` | none | 295 (289-332) | 3.4 | 4,812 | 77 / 172 / 40 | 58% | 173 ms | 26,120 | missing | drawn | | B1 | `4e17478` | `VICTORY_RELAY=2` | 220 (215-554) | 4.5 | 4,798 | 92 / 118 / 190 | 78% | 118 ms | 13,706 | missing | missing | | B2 | `4e17478` | `VICTORY_RELAY=2` | 253 (244-276) | 3.9 | 4,892 | 95 / 146 / 219 | 81% | 147 ms | 15,617 | drawn | missing | | C1 | `4e17478` | late-wait set¹ | 182 (176-260) | 5.5 | 4,651 | 57 / 88 / 25 | 77% | 93 ms | 20,820 | drawn | drawn | | P-A1 | `f909bf0` | none | 298 (292-631) | 3.4 | 4,779 | 77 / 177 / 40 | 59% | 177 ms | 22,897 | drawn | drawn | | P-B1 | `f909bf0` | `VICTORY_RELAY=2` | 278 (271-286) | 3.6 | 4,761 | 78 / 195 / 63 | 61% | 195 ms | 25,049 | missing | missing | | P-B2 | `f909bf0` | `VICTORY_RELAY=2` | 290 (267-315) | 3.5 | 4,844 | 76 / 208 / 64 | 64% | 207 ms | 25,824 | missing | missing | | P-C1 | `f909bf0` | late-wait set¹ | 234 (218-262) | 4.3 | 4,790 | 57 / 142 / 26 | 67% | 144 ms | 25,388 | missing | missing | ¹ `VICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=1024` None of them logged an `ERROR` line or lost the device. Every run refused the same pipelines once at startup with 0x80070057: compute `02384a60fda6f2d0`, `2cc4c197ee29e291`, `2da4beb386509b77` and `369cbe906ea72764`, and mesh `f5e50eb2108c425b` with pixel `ce3f0432a08f1d35`. The `4e17478` runs also refused compute `6b82bc7d22016cb5`. ### 1. The walls are not restored, and here they are lost in default runs too - **Within a run** the walls are either there in every captured frame or missing in every one. - **Missing** looks the same as in #168: sky and horizon on the left, the posters as dark rectangles hanging in front of sky on the right, and the window frame standing on its own. - **Drawn** means a dark block wall on the left and a wall with a photo poster on the right. - **Main:** lost the walls in both default runs and in B1, and drew them in B2 and C1. - **This PR:** drew them with defaults (P-A1) and lost them in all three late-mode runs. - **Warm-up runs (not in the table):** agree. The main build's default run from the save lost the walls, and this PR's default run drew them. With one or two runs per arm and walls missing in default runs too, this can't show that the PR makes the walls worse. It does show it doesn't bring them back. The grille follows the switches more closely than the walls. It is missing in all four `RELAY=2` runs and in P-C1, and drawn in every default run and in C1. Skin is present in every run. No subtitles were on screen in any captured frame, so I couldn't check the overlapping subtitles from #89. ### 2. The wait serializes the late submissions The new wait is counted as "title memory read" in the settle lines. Compared with the same mode on `4e17478`: | | `4e17478` | `f909bf0` | |---|---:|---:| | `RELAY=2`: settles per frame | 3 | about 15 | | `RELAY=2`: waiting under "title memory read" | 9.5 ms/frame | 196 ms/frame | | Late-wait set: settles per frame | 3 | about 18 | | Late-wait set: flights per settle | 10.2 | 2.3 | All of the waiting in settles is now under that reason. GPU time per frame goes up and the card is less busy. With the PR, `RELAY=2` runs at 278 to 290 ms against 292 to 298 ms on defaults, so almost all of its speed-up is gone. The mesh group count also comes back to the default level, about 25,000 against 13,700 to 15,600 on `4e17478` with `RELAY=2`. That fits #168's point that part of the relay's speed-up was geometry that wasn't drawn. Under the late-wait set it rises from 20,800 to 25,400. --- Test report written with Claude Code (Claude Opus 5.5), from real runs on the hardware named above. No game data attached.
Author
Owner

@⁠adritoyalser on GitHub · 2026-11-25 01:29 UTC · original

Thanks for the thorough RTX 4090 benchmark @⁠tycosnh! The settle breakdown pinpointed the exact performance bug in f909bf0:

  • f909bf0 marked in_flight on every Step::Texture (even skipped uploads with no CopyTextureRegion) and waited eagerly at submission entry before checking whether the texture had changed, causing ~15–18 settles/frame on static textures.

Pushed a fix that:

  1. Tracks uploads: bool on Step::Texture and only marks in_flight when texture::upload actually recorded a CopyTextureRegion.
  2. Moves the wait into Staging::detile(), so unchanged textures never wait at all — only an actual detile into an in-flight staging buffer waits for that specific flight (relay_wait / settle).
  3. Updated late_tests::staging_wait_only_runs_on_detile_and_real_uploads to verify zero waits on unchanged refreshes, uploads = false on skipped copies, and a wait on detile when in_flight is set (clippy 0 warnings).

That should eliminate the +48–52 ms/frame overhead on static textures while keeping the data-hazard protection (and note that #272 is also exploring a similar per-upload fence approach).

**@⁠adritoyalser** on GitHub · 2026-11-25 01:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/141#issuecomment-7149613175) Thanks for the thorough RTX 4090 benchmark @⁠tycosnh! The settle breakdown pinpointed the exact performance bug in `f909bf0`: - `f909bf0` marked `in_flight` on every `Step::Texture` (even skipped uploads with no `CopyTextureRegion`) and waited eagerly at submission entry before checking whether the texture had changed, causing ~15–18 settles/frame on static textures. Pushed a fix that: 1. Tracks `uploads: bool` on `Step::Texture` and only marks `in_flight` when `texture::upload` actually recorded a `CopyTextureRegion`. 2. Moves the wait into `Staging::detile()`, so unchanged textures never wait at all — only an actual detile into an in-flight staging buffer waits for that specific flight (`relay_wait` / `settle`). 3. Updated `late_tests::staging_wait_only_runs_on_detile_and_real_uploads` to verify zero waits on unchanged refreshes, `uploads = false` on skipped copies, and a wait on detile when `in_flight` is set (`clippy` 0 warnings). That should eliminate the +48–52 ms/frame overhead on static textures while keeping the data-hazard protection (and note that #272 is also exploring a similar per-upload fence approach).
Author
Owner

@⁠Revens2 on GitHub · 2026-11-25 05:13 UTC · original

Merging this PR on top of main c96b4f2 (plus #115) conflicts in crates/victory-gpu/src/texture.rs: main added unreported_change after detile on the partial-hash path, while this PR changes detile to take wait_flight. Resolution: self.detile(read, wait_flight); followed by main's unreported block. It builds. In a local combo of eight PRs it ran in the game 3 times with the outcome described on #115 (not attributable to this PR). Not verified alone.

**@⁠Revens2** on GitHub · 2026-11-25 05:13 UTC · [original](https://github.com/fxceless/victory-fork/pull/141#issuecomment-7153563658) Merging this PR on top of main c96b4f2 (plus #115) conflicts in `crates/victory-gpu/src/texture.rs`: main added `unreported_change` after `detile` on the partial-hash path, while this PR changes `detile` to take `wait_flight`. Resolution: `self.detile(read, wait_flight);` followed by main's `unreported` block. It builds. In a local combo of eight PRs it ran in the game 3 times with the outcome described on #115 (not attributable to this PR). Not verified alone.
Author
Owner

@⁠adritoyalser on GitHub · 2026-11-25 06:32 UTC · original

Rebased cleanly onto current main (8dd30ab), preserving the new on-demand levels path in texture.rs. staging_wait_only_runs_on_detile_and_real_uploads passes.

**@⁠adritoyalser** on GitHub · 2026-11-25 06:32 UTC · [original](https://github.com/fxceless/victory-fork/pull/141#issuecomment-7154888063) Rebased cleanly onto current `main` (`8dd30ab`), preserving the new on-demand levels path in `texture.rs`. `staging_wait_only_runs_on_detile_and_real_uploads` passes.
github-bot force-pushed github-bot/gh-141 from 58ecb510c9 to ebb5c47ff8 2026-11-26 18:45:32 +00:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/141/head:github-bot/gh-141'
git switch 'github-bot/gh-141'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-141'
git switch 'github-bot/gh-141'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-141'
git switch 'github-bot/gh-141'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-141'
git switch 'main'
git merge --squash 'github-bot/gh-141'
git switch 'main'
git merge --ff-only 'github-bot/gh-141'
git switch 'main'
git merge 'github-bot/gh-141'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.