Keep no-wait texture refreshes coherent during page remaps #1156

Open
github-bot wants to merge 3 commits from github-bot/gh-1156 into main AGit
Owner

@⁠BradRoland on GitHub · 2026-11-25 21:59 UTC · original
BradRoland:codex/defer-remapped-textures → main · +51473 −140 · 15 files · 18 commits · open

With VICTORY_TEXTURE_NO_WAIT=1, a successful read could publish a texture from the middle of a multi-page remap. On 9c184b3 a synthetic 128 KiB texture stayed readable throughout the change. The staging buffer took the replacement first page (0x77) and the previous second page (0x11) and accepted that combination as its new contents.

A fresh staging buffer now takes the normal mapping read lock. This matters for textures picked by heap index, whose first upload can also be their last. Once staging has a previous sample, a contended refresh keeps it, resets the guard checkpoint, and retries on a later submission. The frame log counts deferred refreshes. Repeated contention can delay an update, and first uploads can wait.

The regression uses two writable views of the same pagefile mapping and real D3D12 staging. It checks first use, both pages of the retained image while the writer holds the lock, and the complete replacement image after an alias write that produced no further guard notification.

  • cargo test --locked -p victory-gpu --features diagnostics -- --test-threads=1 with VICTORY_D3D12_WARP=1 passed all 202 enabled tests.
  • The regression also passed in the default build on the RTX 4080. A parallel workspace run encountered the separately tracked indirect-draw test failure in #1118. The serial workspace rerun passed all 587 enabled tests (12 ignored).

Related to #1061. The synthetic case confirms a mixed-page read. Whether it caused the reported prologue-window artifact on the RX 7700 XT still needs a gameplay run. Comparing the same camera and switches from that issue would also show how often the new path defers.

**@⁠BradRoland** on GitHub · 2026-11-25 21:59 UTC · [original](https://github.com/fxceless/victory-fork/pull/1156) `BradRoland:codex/defer-remapped-textures` → `main` · +51473 −140 · 15 files · 18 commits · open With `VICTORY_TEXTURE_NO_WAIT=1`, a successful read could publish a texture from the middle of a multi-page remap. On `9c184b3` a synthetic 128 KiB texture stayed readable throughout the change. The staging buffer took the replacement first page (0x77) and the previous second page (0x11) and accepted that combination as its new contents. A fresh staging buffer now takes the normal mapping read lock. This matters for textures picked by heap index, whose first upload can also be their last. Once staging has a previous sample, a contended refresh keeps it, resets the guard checkpoint, and retries on a later submission. The frame log counts deferred refreshes. Repeated contention can delay an update, and first uploads can wait. The regression uses two writable views of the same pagefile mapping and real D3D12 staging. It checks first use, both pages of the retained image while the writer holds the lock, and the complete replacement image after an alias write that produced no further guard notification. - `cargo test --locked -p victory-gpu --features diagnostics -- --test-threads=1` with `VICTORY_D3D12_WARP=1` passed all 202 enabled tests. - The regression also passed in the default build on the RTX 4080. A parallel workspace run encountered the separately tracked indirect-draw test failure in #1118. The serial workspace rerun passed all 587 enabled tests (12 ignored). Related to #1061. The synthetic case confirms a mixed-page read. Whether it caused the reported prologue-window artifact on the RX 7700 XT still needs a gameplay run. Comparing the same camera and switches from that issue would also show how often the new path defers.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:58:35 +00:00
Author
Owner

@⁠BradRoland on GitHub · 2026-11-26 00:06 UTC · original

Retested against upstream dc4c0fc on Windows. Commit 367d934 runs the remap regression in separate processes with VICTORY_LIGHT_REFRESH=0 and 1. With both settings, the first upload waited, the contested refresh preserved the old 128 KiB image, the retry staged the complete replacement image, and the next unchanged refresh read zero bytes. All four combinations passed across WARP and a GeForce RTX 4080 (621.14). These runs used generated texture data.

**@⁠BradRoland** on GitHub · 2026-11-26 00:06 UTC · [original](https://github.com/fxceless/victory-fork/pull/1156#issuecomment-7178410829) Retested against upstream `dc4c0fc` on Windows. Commit `367d934` runs the remap regression in separate processes with `VICTORY_LIGHT_REFRESH=0` and `1`. With both settings, the first upload waited, the contested refresh preserved the old 128 KiB image, the retry staged the complete replacement image, and the next unchanged refresh read zero bytes. All four combinations passed across WARP and a GeForce RTX 4080 (621.14). These runs used generated texture data.
Author
Owner

@⁠grosterr on GitHub · 2026-11-26 08:20 UTC · original

A gameplay data point for the prologue-window question, from an RTX 3060 12 GB (Ryzen 7 5700X, 32 GB, Windows 11, Series X profile). In short: with this PR in our tree, the blocky outline around silhouettes in the cell's sunlight still appeared, so in our case it is not (or not only) the mixed-page read.

Before this PR: with VICTORY_TEXTURE_NO_WAIT=1 and our usual set, about one launch in six or seven showed a blocky outline around silhouettes in the sun from the cell window, or black patches on walls. Each switch alone and repeats of the full set came out clean (about 20 runs), so it looked random between launches. That made this PR a good candidate.

With this PR: the tree was the #770 stack + main 6d20eed + #1057, #1112, #1044, #1101, #1014, this PR (e06a814), #1150, #1023, #1032, #1136 and #1031. Switches: LEVELS_ON_DEMAND EARLY_TEXTURES_MS=40 TEXTURE_NO_WAIT WIDE_TEXTURES ASYNC_SUBMIT QUEUE2 INDIRECT_WINDOW COPY_PLAIN JOIN_PARTS BACK_OWN_VIEW INDIRECT_GATHER CARD_HASH_WIDE. One 10-minute run on the same save and route:

  • The outline was there about five minutes in, on Lucia and the wall by the cell window in the sun. It looked the same as before.
  • The new line said 0 to 59 deferred refreshes a frame, 2.0 at the median. No errors and no device loss.

One more thing, with a caveat: in that run the first three minutes (the cutscene and the way into the world) were far slower than without these PRs: a median of 1.8 s a frame, of which 0.85 s was "textures", against 0.27 s and 18 ms on the same machine without them. Your description says first uploads can now wait for the mapping lock, which would fit. But #1031 and #1023 were in the same tree and change the texture path too, and I did not split them. So I can't say which of the three it was. I also learned later that a small page file had slowed other runs, and free memory in this one was not recorded. The steady frame later was 207 ms against 179.

How the texture.rs conflict with the #770 stack (05c5898 and #768's motion tracking) was resolved, including one test expectation: #770

AI-assisted (Claude Code); the runs and observations are mine.

**@⁠grosterr** on GitHub · 2026-11-26 08:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/1156#issuecomment-7186479548) A gameplay data point for the prologue-window question, from an RTX 3060 12 GB (Ryzen 7 5700X, 32 GB, Windows 11, Series X profile). In short: **with this PR in our tree, the blocky outline around silhouettes in the cell's sunlight still appeared**, so in our case it is not (or not only) the mixed-page read. **Before this PR:** with `VICTORY_TEXTURE_NO_WAIT=1` and our usual set, about one launch in six or seven showed a blocky outline around silhouettes in the sun from the cell window, or black patches on walls. Each switch alone and repeats of the full set came out clean (about 20 runs), so it looked random between launches. That made this PR a good candidate. **With this PR:** the tree was the #770 stack + main `6d20eed` + #1057, #1112, #1044, #1101, #1014, **this PR (`e06a814`)**, #1150, #1023, #1032, #1136 and #1031. Switches: `LEVELS_ON_DEMAND EARLY_TEXTURES_MS=40 TEXTURE_NO_WAIT WIDE_TEXTURES ASYNC_SUBMIT QUEUE2 INDIRECT_WINDOW COPY_PLAIN JOIN_PARTS BACK_OWN_VIEW INDIRECT_GATHER CARD_HASH_WIDE`. One 10-minute run on the same save and route: - **The outline was there** about five minutes in, on Lucia and the wall by the cell window in the sun. It looked the same as before. - The new line said 0 to 59 deferred refreshes a frame, 2.0 at the median. No errors and no device loss. **One more thing, with a caveat:** in that run the first three minutes (the cutscene and the way into the world) were far slower than without these PRs: a median of 1.8 s a frame, of which 0.85 s was "textures", against 0.27 s and 18 ms on the same machine without them. Your description says first uploads can now wait for the mapping lock, which would fit. But #1031 and #1023 were in the same tree and change the texture path too, and I did not split them. So I can't say which of the three it was. I also learned later that a small page file had slowed other runs, and free memory in this one was not recorded. The steady frame later was 207 ms against 179. How the texture.rs conflict with the #770 stack (`05c5898` and #768's motion tracking) was resolved, including one test expectation: https://git.gtahub.st/github/fxceless--victory-fork/pulls/770 AI-assisted (Claude Code); the runs and observations are mine.
Author
Owner

@⁠HLVM04 on GitHub · 2026-11-26 08:26 UTC · original

A gameplay data point for the first-upload case this PR names (textures picked by heap index), from an RTX 5070 Ti (Series S profile, fork main plus PRs and local changes). We have not run this PR's code.

  • With VICTORY_TEXTURE_NO_WAIT=1, our main character's skin colour and normal textures (BC7 sRGB and BC5, 512x512, sampled from mip 3 down) sometimes came out black. In a black run, title memory held the right bytes while both host copies were 100% zero.
  • In our tree the failure is a little different from the mixed-page read here: when the refresh landed during a remap, it read title memory through the kernel; for a partly mapped texture that read failed, the never-filled staging buffer stayed empty, and with no later refresh the zero upload was kept for the run.
  • Our local fix falls back to the partly-mapped copy path when that read fails on a never-filled staging buffer. The neck was lit in 4 of 4 runs against 1 of 4 before, judged on the base-colour G-buffer target (render target 0), not on the final image. Small sample. Details in #923.

So the "first upload can also be the last" case does occur in gameplay. This PR's lock on a fresh staging buffer looks like it should cover it as well, but that's untested here.

**@⁠HLVM04** on GitHub · 2026-11-26 08:26 UTC · [original](https://github.com/fxceless/victory-fork/pull/1156#issuecomment-7186595882) A gameplay data point for the first-upload case this PR names (textures picked by heap index), from an RTX 5070 Ti (Series S profile, fork main plus PRs and local changes). We have not run this PR's code. - With `VICTORY_TEXTURE_NO_WAIT=1`, our main character's skin colour and normal textures (BC7 sRGB and BC5, 512x512, sampled from mip 3 down) sometimes came out black. In a black run, title memory held the right bytes while both host copies were 100% zero. - In our tree the failure is a little different from the mixed-page read here: when the refresh landed during a remap, it read title memory through the kernel; for a partly mapped texture that read failed, the never-filled staging buffer stayed empty, and with no later refresh the zero upload was kept for the run. - Our local fix falls back to the partly-mapped copy path when that read fails on a never-filled staging buffer. The neck was lit in 4 of 4 runs against 1 of 4 before, judged on the base-colour G-buffer target (render target 0), not on the final image. Small sample. Details in #923. So the "first upload can also be the last" case does occur in gameplay. This PR's lock on a fresh staging buffer looks like it should cover it as well, but that's untested here.
Author
Owner

@⁠tikD1337 on GitHub · 2026-11-26 13:38 UTC · original

I measured this PR on a machine where it makes the opening cutscene much slower. The cause appears to be the first-sample wait. Details below; tell me if I misread the intent.

Setup

  • Machine: Windows 11, RTX 4070 Ti SUPER (driver 621.01), Ryzen 5 7600, 32 GB RAM, Series X profile, warm shader and pipeline cache.
  • Tree: fork main 06702fe with this PR (367d934) merged. One conflict in texture.rs: main has since added refresh_ahead beside refresh_unlocked, which this PR removes. I kept refresh_ahead.
  • Diagnostics: a diagnostics build with one extra frame-log line (process CPU and frame-time percentiles over every frame). It does not change behaviour. The main build I compare against has the same line, plus texture-check counters of mine.
  • Switches: VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_TEXTURE_NO_WAIT=1 VICTORY_WIDE_REFRESH=1 VICTORY_JOIN_PARTS=1 VICTORY_BACK_OWN_VIEW=1.
  • Run: a 300 s script from boot. It goes through the menus into the opening café cutscene, then about 40 s standing in the prison. Runs of different builds alternate.
  • Weighting: totals over the cutscene's frames, then divided by them. I did not average the 5 s readings.

Measured: café cutscene (n = 1 run each)

main 06702fe + #1156
textures, MB a frame 37.8 241.4
textures, ms a frame 11.9 1,554
frame time, median 141 ms 651 ms
frame time, p90 337 ms 6,174 ms
process CPU a frame 0.7 s 10.6 s

main + #1130 + this PR never reached the world in its 300 s run. It stayed in loading.

Measured: where the time goes. The frame line "texture refreshes per frame, all threads" splits the time. In the heaviest cutscene readings with this PR it says, for example:

texture refreshes per frame, all threads: 12816.9 ms, of them 0.0 ms waiting for a texture another thread is refreshing, 18.4 ms asking the write watch and the guards, 12631.2 ms waiting for title memory to hold still (the runtime remapping it)

The heaviest readings wait 6.5 to 12.6 s a frame, summed over threads, for title memory to hold still. On main with the same switches that part is 0.0 ms in every reading: TEXTURE_NO_WAIT never waits there.

Code reading: why (line numbers in 367d934, crates/victory-gpu/src/texture.rs)

  • Only sampled textures defer. Line 1072, if no_wait() && last.hash.is_some(), skips the wait only for a texture that has been sampled before. Every texture without a first sample (last.hash == None) goes to line 1082, waited(2, crate::watch::title_memory_reading), and blocks until the runtime has finished remapping. The doc comment at line 406 says so ("A fresh staging buffer still waits for its first sample").
  • A streaming scene is mostly first samples. In the opening cutscene most textures are new, while the runtime remaps title memory for about 100 ms or more of every streaming frame. The page-mapping copies hold the mapping lock alone.
  • So hundreds of first samples queue behind the remaps every frame. Each one blocks the thread that runs it (with WIDE_REFRESH, several helper threads).
  • A deferral forgets what was learnt. Line 1076 sets last.asked = 0, so the next check reads the whole texture again even when nothing more was written. That adds to the MB.

Suggestion

  • Never block the submission on a remap, first samples included. One way: try-lock for every texture, and if the lock is busy, defer and retry at the next submission. A texture without a first sample would show a frame or two later.
  • If a first sample has to be there: a short bounded wait (a few ms), then defer. Or read it through the kernel as main does, since main never shows it half read.
  • On deferral, put back the previous asked instead of 0. From code reading: watch::writes (writes_as in watch.rs) keeps every write it found in its journal, with the number of the asking that found it, and answers from that journal. So a write consumed by the deferred check still carries a number above the previous asked, and the next check sees it and reads the texture. Only a texture that was not written skips the whole read. My branch does this behind a switch on top of this PR. That variant was slower still in the one run I made (406 MB, 2,108 ms a frame), because the first-sample wait above dominates.

Not measured: whether the mixed-pages problem this PR fixes is visible on this machine with main's kernel read. I did not look for it.

AI-assisted (Claude Code). The measurements are from runs on the machine above. The cause is from the fork's own frame-line counters plus reading the code.

**@⁠tikD1337** on GitHub · 2026-11-26 13:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/1156#issuecomment-7193637181) I measured this PR on a machine where it makes the opening cutscene much slower. The cause appears to be the first-sample wait. Details below; tell me if I misread the intent. **Setup** - **Machine:** Windows 11, RTX 4070 Ti SUPER (driver 621.01), Ryzen 5 7600, 32 GB RAM, Series X profile, warm shader and pipeline cache. - **Tree:** fork `main` 06702fe with this PR (367d934) merged. One conflict in `texture.rs`: `main` has since added `refresh_ahead` beside `refresh_unlocked`, which this PR removes. I kept `refresh_ahead`. - **Diagnostics:** a diagnostics build with one extra frame-log line (process CPU and frame-time percentiles over every frame). It does not change behaviour. The `main` build I compare against has the same line, plus texture-check counters of mine. - **Switches:** `VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_TEXTURE_NO_WAIT=1 VICTORY_WIDE_REFRESH=1 VICTORY_JOIN_PARTS=1 VICTORY_BACK_OWN_VIEW=1`. - **Run:** a 300 s script from boot. It goes through the menus into the opening café cutscene, then about 40 s standing in the prison. Runs of different builds alternate. - **Weighting:** totals over the cutscene's frames, then divided by them. I did not average the 5 s readings. **Measured: café cutscene (n = 1 run each)** | | `main` 06702fe | + #1156 | |---|---|---| | textures, MB a frame | 37.8 | 241.4 | | textures, ms a frame | 11.9 | 1,554 | | frame time, median | 141 ms | 651 ms | | frame time, p90 | 337 ms | 6,174 ms | | process CPU a frame | 0.7 s | 10.6 s | `main` + #1130 + this PR never reached the world in its 300 s run. It stayed in loading. **Measured: where the time goes.** The frame line "texture refreshes per frame, all threads" splits the time. In the heaviest cutscene readings with this PR it says, for example: > texture refreshes per frame, all threads: 12816.9 ms, of them 0.0 ms waiting for a texture another thread is refreshing, 18.4 ms asking the write watch and the guards, 12631.2 ms waiting for title memory to hold still (the runtime remapping it) The heaviest readings wait 6.5 to 12.6 s a frame, summed over threads, for title memory to hold still. On `main` with the same switches that part is 0.0 ms in every reading: `TEXTURE_NO_WAIT` never waits there. **Code reading: why** (line numbers in 367d934, `crates/victory-gpu/src/texture.rs`) - **Only sampled textures defer.** Line 1072, `if no_wait() && last.hash.is_some()`, skips the wait only for a texture that has been sampled before. Every texture without a first sample (`last.hash == None`) goes to line 1082, `waited(2, crate::watch::title_memory_reading)`, and blocks until the runtime has finished remapping. The doc comment at line 406 says so ("A fresh staging buffer still waits for its first sample"). - **A streaming scene is mostly first samples.** In the opening cutscene most textures are new, while the runtime remaps title memory for about 100 ms or more of every streaming frame. The page-mapping copies hold the mapping lock alone. - **So hundreds of first samples queue behind the remaps every frame.** Each one blocks the thread that runs it (with `WIDE_REFRESH`, several helper threads). - **A deferral forgets what was learnt.** Line 1076 sets `last.asked = 0`, so the next check reads the whole texture again even when nothing more was written. That adds to the MB. **Suggestion** - **Never block the submission on a remap, first samples included.** One way: try-lock for every texture, and if the lock is busy, defer and retry at the next submission. A texture without a first sample would show a frame or two later. - **If a first sample has to be there:** a short bounded wait (a few ms), then defer. Or read it through the kernel as `main` does, since `main` never shows it half read. - **On deferral, put back the previous `asked` instead of 0.** From code reading: `watch::writes` (`writes_as` in `watch.rs`) keeps every write it found in its journal, with the number of the asking that found it, and answers from that journal. So a write consumed by the deferred check still carries a number above the previous `asked`, and the next check sees it and reads the texture. Only a texture that was not written skips the whole read. My branch does this behind a switch on top of this PR. That variant was slower still in the one run I made (406 MB, 2,108 ms a frame), because the first-sample wait above dominates. **Not measured:** whether the mixed-pages problem this PR fixes is visible on this machine with `main`'s kernel read. I did not look for it. AI-assisted (Claude Code). The measurements are from runs on the machine above. The cause is from the fork's own frame-line counters plus reading the code.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/texture.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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