Skip to content

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

Open
adritoyalser wants to merge 1 commit into
fxceless:mainfrom
adritoyalser:fix/late-staging-hazard
Open

adritoyalser wants to merge 1 commit into
fxceless:mainfrom
adritoyalser:fix/late-staging-hazard

Conversation

@adritoyalser

Copy link
Copy Markdown

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

@tycosnh

tycosnh commented Nov 25, 2026

Copy link
Copy Markdown

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

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 RTX 4090 (Windows, native D3D12): RELAY=2 and the late-wait set are about 40% faster on 4e17478, and both lose the first room's walls #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.

@adritoyalser

Copy link
Copy Markdown
Author

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).

GromX0 pushed a commit to GromX0/victory-fork that referenced this pull request Nov 25, 2026
…fxceless#141)

fxceless#141 marked every texture a submission samples as in flight with it, and
made each later submission that samples one of them wait for that flight
before preparing, changed or not: most textures are sampled every frame,
so late/relay submissions waited for each other (confirmed by review).

Now only a list that records a copy from a staging buffer notes it
(staging_copies, from texture::upload's copy path), and only those are
marked in flight at submission. With the relay exact the wait moves to
Staging::detile, which runs only when the texture's memory changed: the
last flight that copies from the buffer lands first (host::staging_wait,
a relay_wait, safe under the submission lock). Without the relay exact
the settle before preparing stays, now for staging buffers a flight
actually copies from.

Test: the first list that uses a texture records the copy and notes its
staging buffer; a later list that only samples the unchanged texture notes
nothing but keeps its check at submission. It fails without the note.

(cherry picked from commit 3907f191ce587aebd86a312c1909e720bfce544f)
(cherry picked from commit b1d3e1c)
@Revens2

Revens2 commented Nov 25, 2026

Copy link
Copy Markdown

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.

@adritoyalser

adritoyalser commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

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.

…r VICTORY_LATE_WAIT/RELAY

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.
e4glevlr added a commit to e4glevlr/victory-fork that referenced this pull request Nov 25, 2026
…xceless#768)

- fxceless#822: scale staging/shadow cache idle timeouts with commit limit as well as physical RAM
- fxceless#903: add opt-in VICTORY_PLACED_TEX=1 64 MiB heap suballocator for placed host textures (default off; README untouched)
- fxceless#886: let DirectStorage admit fitting reads from other queues into spare staging space
- fxceless#768: add opt-in VICTORY_WIDE_TEXTURES=1 parallel texture refresh in Batch::flush (default off, preserving fxceless#141 in_flight guard)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants