texture: capture picked textures ahead of submission (VICTORY_PICK_REFRESH_AHEAD) #555

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

@⁠just-another-dude84 on GitHub · 2026-12-05 03:24 UTC · original
just-another-dude84:pick-refresh-ahead → main · +341 −10 · 6 files · 1 commits · open

Problem

On camera turns the title streams in hundreds of textures a frame. On this machine, heavy turns average about 350 uploads a frame. Each upload's staging is captured (read, hashed, detiled) at its first refresh(), which is the submitting thread's front refresh (Batch::refresh_front → refresh_spans → shadow::wide_each). The submitting thread takes jobs of its own there, then waits for the wide workers.

On turns with 250 or more uploads a frame, that join took 6.6–6.9 ms per frame, 4.9–5.1 ms of it the submitting thread's own share of the refreshes. The card sat idle for 13–16 ms per frame on those turns, waiting for submissions.

About 85% of those stagings are made by the texture pass (heap_textures). The pass picks them on a list-recording thread, well before the list is submitted. So the capture can start there.

Change

With VICTORY_PICK_REFRESH_AHEAD=1 (off by default):

  • When the texture pass picks a texture for upload (upload_from with PICKING set, a once staging not captured yet), the staging goes to a pool of 3 threads of its own ("victory-pick-refresh-N", fast cores).
    • The queue is capped at 4096; a full queue drops the job, and submission captures it as before.
    • Jobs run under catch_unwind. A job that panics clears the staging's hash, so the submission captures it again.
  • The job asks the kernel write watch for the staging's range first, then captures, all under the staging's last lock.
  • At submission, refresh_timed sees the pending ahead capture and settles it, still under last:
    • watched, no page written since the asking: the capture stands; nothing is read again.
    • written since: the staging is captured again, detiling even an all-zero read, with the partial throttle reset.
    • memory the kernel watch does not see (partly mapped, a pool window, scattered, the card's memory): hashed again at submission. The detile is skipped when the hash is the same.
  • A job that finds the staging already captured or uploaded does nothing.
  • Only the kernel watch (watch::writes(.., false)) is asked from the pool. Guards are never installed from it.

Uploads still land in the same submission as before, so nothing is shown later than it was.

The per-second line pick refresh ahead per frame counts what happened (diagnostics builds only).

Measured (same save, same rotation route, diag build, VICTORY_TEXTURE_COSTS=1 VICTORY_CARD_SPANS=1)

Seconds with 250 or more texture uploads a frame:

off (run A) off (run B) on
seconds 29 32 32
front refresh join, ms/frame 6.86 6.60 4.91
of it, the submitting thread's own jobs 5.09 4.89 3.19
submitting thread, lists' steps ms/frame 23.3 23.3 21.4
fps per 5 s window, min / p10 / median 10.9 / 12.7 / 21.1 8.9 / 12.8 / 21.4 12.8 / 13.4 / 22.3

On this setup every pick-ahead capture settled as "not watched". The textures are read through pool windows, so the submitting thread still reads and hashes them, but no longer detiles them: 254 a frame, 253 with the same hash. The gain is the detile moved off the submitting thread. If writes into pool windows were noted for the kernel watch, the read and hash could be skipped too.

No visual difference while turning: no stale, late or flickering textures.

Test PC: RX 6800 16 GB, i7-12700K, RADV, Arch (zen kernel), X11, Proton Experimental, Series S, RT on, 720p → 1080p.

Cost

  • 3 more threads. Their CPU time is the detile that moved, plus the extra hash for unwatched memory, about 8–11 ms a frame across them on heavy turns.
  • A pick that is never submitted still gets captured.
  • With the switch off, the code path is the same as before. refresh_timed's body moved into capture unchanged.
  • A queued job reads title memory a little earlier than the submission would. If the title frees that memory in between, the job reads freed memory. The submitting thread's own capture has the same race; the window is just longer here.

Smoke test

Same save, clean branch build: 251–336 captures a frame on heavy turns, none dropped, no panics, no device errors.

🤖 Generated with Claude Code

**@⁠just-another-dude84** on GitHub · 2026-12-05 03:24 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/555) `just-another-dude84:pick-refresh-ahead` → `main` · +341 −10 · 6 files · 1 commits · open ## Problem On camera turns the title streams in hundreds of textures a frame. On this machine, heavy turns average about 350 uploads a frame. Each upload's staging is captured (read, hashed, detiled) at its first `refresh()`, which is the submitting thread's front refresh (`Batch::refresh_front` → `refresh_spans` → `shadow::wide_each`). The submitting thread takes jobs of its own there, then waits for the wide workers. On turns with 250 or more uploads a frame, that join took 6.6–6.9 ms per frame, 4.9–5.1 ms of it the submitting thread's own share of the refreshes. The card sat idle for 13–16 ms per frame on those turns, waiting for submissions. About 85% of those stagings are made by the texture pass (`heap_textures`). The pass picks them on a list-recording thread, well before the list is submitted. So the capture can start there. ## Change With `VICTORY_PICK_REFRESH_AHEAD=1` (off by default): - When the texture pass picks a texture for upload (`upload_from` with `PICKING` set, a `once` staging not captured yet), the staging goes to a pool of 3 threads of its own ("victory-pick-refresh-N", fast cores). - The queue is capped at 4096; a full queue drops the job, and submission captures it as before. - Jobs run under `catch_unwind`. A job that panics clears the staging's hash, so the submission captures it again. - The job asks the kernel write watch for the staging's range first, then captures, all under the staging's `last` lock. - At submission, `refresh_timed` sees the pending ahead capture and settles it, still under `last`: - **watched, no page written since the asking:** the capture stands; nothing is read again. - **written since:** the staging is captured again, detiling even an all-zero read, with the partial throttle reset. - **memory the kernel watch does not see** (partly mapped, a pool window, scattered, the card's memory): hashed again at submission. The detile is skipped when the hash is the same. - A job that finds the staging already captured or uploaded does nothing. - Only the kernel watch (`watch::writes(.., false)`) is asked from the pool. Guards are never installed from it. Uploads still land in the same submission as before, so nothing is shown later than it was. The per-second line `pick refresh ahead per frame` counts what happened (diagnostics builds only). ## Measured (same save, same rotation route, diag build, `VICTORY_TEXTURE_COSTS=1 VICTORY_CARD_SPANS=1`) Seconds with 250 or more texture uploads a frame: | | off (run A) | off (run B) | **on** | |---|---|---|---| | seconds | 29 | 32 | 32 | | front refresh join, ms/frame | 6.86 | 6.60 | **4.91** | | of it, the submitting thread's own jobs | 5.09 | 4.89 | **3.19** | | submitting thread, lists' steps ms/frame | 23.3 | 23.3 | **21.4** | | fps per 5 s window, min / p10 / median | 10.9 / 12.7 / 21.1 | 8.9 / 12.8 / 21.4 | **12.8 / 13.4 / 22.3** | On this setup every pick-ahead capture settled as "not watched". The textures are read through pool windows, so the submitting thread still reads and hashes them, but no longer detiles them: 254 a frame, 253 with the same hash. The gain is the detile moved off the submitting thread. If writes into pool windows were noted for the kernel watch, the read and hash could be skipped too. No visual difference while turning: no stale, late or flickering textures. Test PC: RX 6800 16 GB, i7-12700K, RADV, Arch (zen kernel), X11, Proton Experimental, Series S, RT on, 720p → 1080p. ## Cost - 3 more threads. Their CPU time is the detile that moved, plus the extra hash for unwatched memory, about 8–11 ms a frame across them on heavy turns. - A pick that is never submitted still gets captured. - With the switch off, the code path is the same as before. `refresh_timed`'s body moved into `capture` unchanged. - A queued job reads title memory a little earlier than the submission would. If the title frees that memory in between, the job reads freed memory. The submitting thread's own capture has the same race; the window is just longer here. ## Smoke test Same save, clean branch build: 251–336 captures a frame on heavy turns, none dropped, no panics, no device errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On camera turns the submitting thread's front refresh of picked textures
is the cost. Picked `once` stagings are now read, hashed and detiled on 3
threads of their own as soon as they are picked.
At submission the kernel write watch says whether the title wrote them
since (clean: kept; rewritten: captured again). Memory it does not watch
is hashed again, and the detile is skipped when the hash is the same.
Off by default; the per-second stats line counts it with diagnostics.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 03:24:19 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 03:53 UTC · original

Checked 9003f4a on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).

victory-gpu lib tests (4 threads, skipping descriptor::tests::views_die_with_their_resource) give 353 passed and 4 failed on both this branch and main b581f7e. The 4 are the same on both, the two indirect host_list_* and the two pipeline::host library loads that already fail on main. Same result with VICTORY_PICK_REFRESH_AHEAD=1 set, though no existing test reaches the pick path, so that run only shows nothing else changed. clippy on victory-gpu with --all-targets has no new warnings, and clippy with --features diagnostics is clean for the new code. cargo check -p victory passes and the victory-core tests pass (35).

With the switch off I read it as the same behaviour as main. enabled() is the first operand in upload_from (texture.rs:1279) so the try_lock and the send never happen, ahead_pending is never set so the early return at the top of refresh_timed is as before, and capture() only adds two stores to last.unwatched and the !force && check, with force always false on that path.

To test the staleness question I wrote a throwaway test (not pushed) that builds a Staging over title memory with an upload buffer on WARP. It runs refresh_ahead, lets the title write, then calls the submission's refresh and compares the staging bytes with a fresh detile of the memory as it is now. I ran it on write-watched memory, plain memory and partly mapped memory, with a full rewrite, a one-byte change in the last row, zeroing, and no write at all. A stress version does 3600 captures with the ahead read racing a writer thread on 6 textures at once, and another goes through pick_refresh::send so the real pool races the submission's refresh. No stale uploads in 5 runs out of 5. On write-watched memory the clean case really skips the read (refresh_now returns 0) and a write makes it read again (returns the size), so the trust path works on real watched memory. That's worth knowing since the body says every capture in the game run settled as not watched, so this path never ran there. With the clean check forced to always pass, the stress test reports 1060 stale out of 3600, so the test does catch it.

I found one stale case. If once goes false between the job and the submission, refresh_timed skips the ahead_pending block and goes straight to capture(.., false) (the check at texture.rs:1945 only applies when alone). For partly mapped memory the job already stamped last.partial, so the 30 frame throttle in capture (the frame < at + 30 return at texture.rs:2074) returns 0 and the first upload carries the pick-time bytes. My test flips once after refresh_ahead, writes the committed part, then refreshes. Watched and plain memory come out fresh and partly mapped comes out stale. On main the first refresh of that staging reads, since partial is still None. upload_from clears once when a draw samples the texture through a table (texture.rs:1263), so a texture that's picked by the pass and also sampled by a draw in the same frame can hit this. It's bounded by the throttle, but main only lets later mips arrive late, not the first upload. Adding this right before the if alone && last.hash.is_some() check fixed it in my test, and the other cases still pass.

if !alone && last.ahead_pending {
    last.ahead_pending = false;
    last.ahead = None;
    last.partial = None;
}

There's also a small window on the panic path, from reading the code (I didn't trigger it). capture() sets last.hash before it detiles (texture.rs:2138 then detile), and ahead_pending is only set after capture returns. If the detile panics, the unwind drops the last guard, and only then does refresh_ahead_failed (pick_refresh.rs:73) lock it again to clear the hash. A submission refresh that takes last in that gap sees a hash with nothing pending, returns 0, and finish_refresh uploads a half detiled buffer because detile already set dirty. The staging is once, so nothing reads it again. A panic in detile should be rare, but clearing the hash inside the locked scope would close it, for example by catching the unwind in the ahead branch of refresh_timed before the guard drops, or by setting the hash only after the detile.

Things I checked that look fine.

  • When the job's asking is answered from the submission's ask_ahead/ask_once snapshot in the journal, any write after that snapshot gets a later number when it's noted, so it shows up as rewritten. The only window left is inside the same submission's refresh window, which main has too.
  • I wondered whether in-place heaps (inplace.rs) could get GPU writes the kernel watch never sees. They're opened for um arenas, and that memory takes the window/scattered/card path, which sets unwatched and gets hashed again at submission (texture.rs:2106). kernel_written also refuses shown pages (um::blind).
  • No deadlock path that I can see. The pool doesn't use shadow::wide_each, and the job only takes last, then the journal and the range hold, in the same order as the submission's refresh. The worst case is a wide worker or the submitting thread waiting on last for one capture already in progress.
  • note_held reads last.hash only in finish_refresh after the refresh has settled it, so it doesn't see an unsettled pick-time hash.
  • The job reads title memory in place after the same committed() check main does, so the check-then-read window is the same length. The new part is reading for lists that may never be submitted. Placed::for_upload stays with the PreludeUpload and holds only the resource object, not the memory, so an access violation there wouldn't be caught by catch_unwind (same as main's submission read).
  • The only Windows thread call is keep_on_fast_cores (SetThreadSelectedCpuSets), with no priority changes.

Smaller things.

  • The doc comment at texture.rs:1854 got merged. "Reads the texture's memory ahead of submission, on a thread of" runs straight into the refresh_ahead_failed doc, and refresh_ahead (texture.rs:1868) lost its first sentence.
  • The SAFETY comment on unsafe impl Send for Staging (texture.rs:146) says the mapping is written only at submission. It's still under last's lock but now also from the pool.
  • The double work is as the body says. A pick that's never submitted still gets read and detiled, and memory with no watch is read and hashed twice. The queue also keeps up to 4096 Arc (and their staging buffers) alive until the jobs run.
  • The rest of the body checks out against the code. 3 threads (pick_refresh.rs:17), the 4096 cap that drops (pick_refresh.rs:21, 108), only writes(.., false) from the pool (texture.rs:1928), too-late or uploaded jobs doing nothing (texture.rs:1917), and the zero detile forced on recapture (texture.rs:2089).

What I didn't test is the game, any timing, the diagnostics line output, AMD or Intel cards, whether keep_on_fast_cores does anything useful on a hybrid CPU, and the panic window above.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 03:53 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/555#issuecomment-7431415200) Checked 9003f4a on native Windows (one NVIDIA desktop card plus WARP, didn't run the game). victory-gpu lib tests (4 threads, skipping descriptor::tests::views_die_with_their_resource) give 353 passed and 4 failed on both this branch and main b581f7e. The 4 are the same on both, the two indirect host_list_* and the two pipeline::host library loads that already fail on main. Same result with VICTORY_PICK_REFRESH_AHEAD=1 set, though no existing test reaches the pick path, so that run only shows nothing else changed. clippy on victory-gpu with --all-targets has no new warnings, and clippy with --features diagnostics is clean for the new code. cargo check -p victory passes and the victory-core tests pass (35). With the switch off I read it as the same behaviour as main. enabled() is the first operand in upload_from (texture.rs:1279) so the try_lock and the send never happen, ahead_pending is never set so the early return at the top of refresh_timed is as before, and capture() only adds two stores to last.unwatched and the `!force &&` check, with force always false on that path. To test the staleness question I wrote a throwaway test (not pushed) that builds a Staging over title memory with an upload buffer on WARP. It runs refresh_ahead, lets the title write, then calls the submission's refresh and compares the staging bytes with a fresh detile of the memory as it is now. I ran it on write-watched memory, plain memory and partly mapped memory, with a full rewrite, a one-byte change in the last row, zeroing, and no write at all. A stress version does 3600 captures with the ahead read racing a writer thread on 6 textures at once, and another goes through pick_refresh::send so the real pool races the submission's refresh. No stale uploads in 5 runs out of 5. On write-watched memory the clean case really skips the read (refresh_now returns 0) and a write makes it read again (returns the size), so the trust path works on real watched memory. That's worth knowing since the body says every capture in the game run settled as not watched, so this path never ran there. With the clean check forced to always pass, the stress test reports 1060 stale out of 3600, so the test does catch it. I found one stale case. If `once` goes false between the job and the submission, refresh_timed skips the ahead_pending block and goes straight to capture(.., false) (the check at texture.rs:1945 only applies when alone). For partly mapped memory the job already stamped last.partial, so the 30 frame throttle in capture (the `frame < at + 30` return at texture.rs:2074) returns 0 and the first upload carries the pick-time bytes. My test flips `once` after refresh_ahead, writes the committed part, then refreshes. Watched and plain memory come out fresh and partly mapped comes out stale. On main the first refresh of that staging reads, since partial is still None. upload_from clears `once` when a draw samples the texture through a table (texture.rs:1263), so a texture that's picked by the pass and also sampled by a draw in the same frame can hit this. It's bounded by the throttle, but main only lets later mips arrive late, not the first upload. Adding this right before the `if alone && last.hash.is_some()` check fixed it in my test, and the other cases still pass. ```rust if !alone && last.ahead_pending { last.ahead_pending = false; last.ahead = None; last.partial = None; } ``` There's also a small window on the panic path, from reading the code (I didn't trigger it). capture() sets last.hash before it detiles (texture.rs:2138 then detile), and ahead_pending is only set after capture returns. If the detile panics, the unwind drops the `last` guard, and only then does refresh_ahead_failed (pick_refresh.rs:73) lock it again to clear the hash. A submission refresh that takes `last` in that gap sees a hash with nothing pending, returns 0, and finish_refresh uploads a half detiled buffer because detile already set `dirty`. The staging is `once`, so nothing reads it again. A panic in detile should be rare, but clearing the hash inside the locked scope would close it, for example by catching the unwind in the ahead branch of refresh_timed before the guard drops, or by setting the hash only after the detile. Things I checked that look fine. - When the job's asking is answered from the submission's ask_ahead/ask_once snapshot in the journal, any write after that snapshot gets a later number when it's noted, so it shows up as rewritten. The only window left is inside the same submission's refresh window, which main has too. - I wondered whether in-place heaps (inplace.rs) could get GPU writes the kernel watch never sees. They're opened for um arenas, and that memory takes the window/scattered/card path, which sets unwatched and gets hashed again at submission (texture.rs:2106). kernel_written also refuses shown pages (um::blind). - No deadlock path that I can see. The pool doesn't use shadow::wide_each, and the job only takes last, then the journal and the range hold, in the same order as the submission's refresh. The worst case is a wide worker or the submitting thread waiting on last for one capture already in progress. - note_held reads last.hash only in finish_refresh after the refresh has settled it, so it doesn't see an unsettled pick-time hash. - The job reads title memory in place after the same committed() check main does, so the check-then-read window is the same length. The new part is reading for lists that may never be submitted. Placed::for_upload stays with the PreludeUpload and holds only the resource object, not the memory, so an access violation there wouldn't be caught by catch_unwind (same as main's submission read). - The only Windows thread call is keep_on_fast_cores (SetThreadSelectedCpuSets), with no priority changes. Smaller things. - The doc comment at texture.rs:1854 got merged. "Reads the texture's memory ahead of submission, on a thread of" runs straight into the refresh_ahead_failed doc, and refresh_ahead (texture.rs:1868) lost its first sentence. - The SAFETY comment on `unsafe impl Send for Staging` (texture.rs:146) says the mapping is written only at submission. It's still under last's lock but now also from the pool. - The double work is as the body says. A pick that's never submitted still gets read and detiled, and memory with no watch is read and hashed twice. The queue also keeps up to 4096 Arc<Staging> (and their staging buffers) alive until the jobs run. - The rest of the body checks out against the code. 3 threads (pick_refresh.rs:17), the 4096 cap that drops (pick_refresh.rs:21, 108), only writes(.., false) from the pool (texture.rs:1928), too-late or uploaded jobs doing nothing (texture.rs:1917), and the zero detile forced on recapture (texture.rs:2089). What I didn't test is the game, any timing, the diagnostics line output, AMD or Intel cards, whether keep_on_fast_cores does anything useful on a hybrid CPU, and the panic window above. AI-assisted: yes
Author
Owner

@⁠luisaugustz on GitHub · 2026-12-05 13:30 UTC · original

Measured on RX 9070 XT + Ryzen 7 5800X (RADV, 4K output, FSR 4 Performance, ray tracing on), on the downtown route from #554:

Runs Avg fps
Baseline 10 16.4
VICTORY_PICK_REFRESH_AHEAD=1 2 16.1 (15.8, 16.5)

preparing_ms dropped a little in one run (12.0 → 11.5), but the frame rate didn't follow. On this machine the city seems bound by the card's ray tracing rather than by the texture captures.

Details are in #591.

**@⁠luisaugustz** on GitHub · 2026-12-05 13:30 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/555#issuecomment-7438791269) Measured on RX 9070 XT + Ryzen 7 5800X (RADV, 4K output, FSR 4 Performance, ray tracing on), on the `downtown` route from #554: | | Runs | Avg fps | |---|---|---| | Baseline | 10 | 16.4 | | `VICTORY_PICK_REFRESH_AHEAD=1` | 2 | 16.1 (15.8, 16.5) | `preparing_ms` dropped a little in one run (12.0 → 11.5), but the frame rate didn't follow. On this machine the city seems bound by the card's ray tracing rather than by the texture captures. Details are in #591.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/texture.rs
  • docs/SETTINGS.md
  • run.conf.default
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/555/head:github-bot/gh-555'
git switch 'github-bot/gh-555'

Merge

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