WIP: perf(vk): sparse mirror binds stop holding frames: unbinds unwaited, give-back capped, new mirrors bound together #2892

Closed
conongnang wants to merge 2 commits from (deleted):perf/sparse-bind-smoothing into main
Contributor

Summary

Sparse VRAM mirrors held frames in two ways (symbolized samples, AMD's Windows driver):

  • At an area's arrival: each new mirror's blocks were bound with their own vkQueueBindSparse (mirror_make -> sparse_fill -> sparse_bind_infos, under dev.present_mu). That cost about 65 ms per arrival; one frame spent 65 ms in NtGdiDdDDIUpdateGpuVirtualAddress.
  • 2-8 s later: every 64 submissions, mirrors_give_back freed every mirror idle past kLongIdle at once (one log line: 171 mirrors given back (1475 MB)). sparse_reap then unbound their spares in one bind, and memory_sparse_wait made every following submission wait for it: 40-110 ms frames with the replay idle.

This PR:

  • Unbinds not waited for: submissions wait for the latest bind later work may reach (sparse_tl_need), not for a freed mirror's spare unbind. A bind into a reused spare waits on the queue for that spare's unbind. RX_VK_SPARSE_UNBIND_ASYNC=0 is the old path whole.
  • Give-back capped: at most RX_VK_MIRROR_GIVE_BACK_MB (256) of long-idle mirrors per sweep, oldest first, and always at least one. Mirrors given back for room are never held. 0 gives them all back at once, as before.
  • New mirrors bound together: up to RX_VK_SPARSE_BIND_BATCH_MB (64) per bind, sent at the end of the mirror_make loop. A refused batch is retried per mirror. 0 binds each alone, as before.

Read first: gpu/vk/sparse_smooth.h, then memory_sparse_wait, mirrors_give_back and sparse_reap in gpu/vk/vk_memory.cpp.

Testing

Tested: Windows 11, AMD Radeon RX 6800, driver 26.10.1, with the other two PRs (Measurements); no visual differences seen.

  • In those runs every mirror was made at its first binding, with its own bind, so the batching never ran.
  • The cap held long-idle mirrors back twice in one run.

Checks:

  • tests/test_sparse_smooth.py (new).
  • test_sparse_bind_ranges.py (the wait value, the spare's wait, the switch off), test_sparse_mirror.py and test_sparse_spare.py pass.
  • clang++ -fsyntax-only with the builder's flags on vk_memory.cpp.

Still to test:

  • Linux RADV (Steam Deck, which has a separate sparse queue family) and NVIDIA: the tour, expecting no corruption and fewer late frames (@Beanstalk teleport through Market and Hyrule Field, stay 30 s in each).

Measurements

Verdict: each change does the work it targets (below), but on its own none moves total stutter at area changes beyond run-to-run noise. Together with its two sibling PRs, the worst frame at an arrival is 15-25% shorter. An earlier claim here of "-20% stutter in the loads" was mostly how long each load took: the loads' length alone varies from 3,300 to 4,900 frames between runs of the same build.

Setup:

  • Windows 11, AMD Radeon RX 6800 (16 GB), AMD driver 26.10.1, i7-13700. The player's own settings: 1920x1080 output, uncapped, windowed.
  • A scripted tour, each run through the job queue in timing mode: the repository save kokiri-start, then teleports to Market (child), Hyrule Field (child), Kakariko Village, Kokiri Forest, Hyrule Field (adult) and Lake Hylia (adult). 25 s in each with the camera turning, plus 20 s walking in circles in Hyrule Field.
  • The frame trace (RX_VK_FRAME_TRACE=1).
  • Visible stutter: what frames of 25 ms or more spend over 16.7 ms, from 1 s before control returns to 2 s after, summed over the six arrivals.
Runs (each a separate launch) Visible stutter at the 6 arrivals (ms) Worst frame at an arrival (ms) Texture creation in the loads (ms)
main @ 28cb22b9 (5) 2583, 2943, 2948, 3263, 3491 390-422 492-550
texture block reserve alone (2) 2624, 3245 355, 370 147-177
shader parse cache alone (2) 2543, 3608 357, 366 525-576
sparse bind smoothing alone (2) 2542, 3019 378, 395 495-560
all three together (3) 2309, 2819, 3091 306-348 187-247

In play (the walk and the camera turns, outside the arrivals), every run of every build has a median frame of 9.8-10.0 ms and 0-1 frames of 33 ms or more.

What the worst arrival frames are spent on, in the runs with all three: draw 100-440 ms, mostly in the recording's commands (up to 396 ms: the first bindings of vertex and index buffers, which import host memory and make mirrors) and descriptors. A change that imports pools ahead of their first binding is being measured on top of these three.

Risks

  • Ordering: if a driver orders later submissions behind a big unbind anyway, the cap is what helps. A needed bind after a big unbind still waits for it, since the timeline is in order.
  • A refused batched bind (out of memory, device lost) loses that mirror's inherited GPU-only data; the old path fell back to a whole mirror.
  • Held mirrors keep VRAM for a few more sweeps.
  • One unexplained hang: on the first run after building the three PRs together, the driver refused every submit with VK_ERROR_OUT_OF_DEVICE_MEMORY shortly after arriving in Market (7.8 of 15.5 GB of VRAM in use), and the game stopped drawing. It has not come back in nine runs since, and no run of main or the nightly (about 20) has refused a submit. Until it is explained, treat this PR as a possible cause.
  • Turning it off: RX_VK_SPARSE_UNBIND_ASYNC=0 RX_VK_SPARSE_BIND_BATCH_MB=0 RX_VK_MIRROR_GIVE_BACK_MB=0.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named). (Not run yet: the checks reading the changed files pass, apart from failures they share with main.)
  • Measured against main, or Measurements says why there is nothing to measure. (Measured: no stutter gain on its own beyond the noise.)
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def. (Rows added; defaults to be confirmed by this PR's own A/B.)
  • A check under tests/ that reads changed source is updated, not weakened.
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

## Summary Sparse VRAM mirrors held frames in two ways (symbolized samples, AMD's Windows driver): - **At an area's arrival:** each new mirror's blocks were bound with their own `vkQueueBindSparse` (`mirror_make` -> `sparse_fill` -> `sparse_bind_infos`, under `dev.present_mu`). That cost about 65 ms per arrival; one frame spent 65 ms in `NtGdiDdDDIUpdateGpuVirtualAddress`. - **2-8 s later:** every 64 submissions, `mirrors_give_back` freed every mirror idle past `kLongIdle` at once (one log line: `171 mirrors given back (1475 MB)`). `sparse_reap` then unbound their spares in one bind, and `memory_sparse_wait` made every following submission wait for it: 40-110 ms frames with the replay idle. This PR: - **Unbinds not waited for:** submissions wait for the latest bind later work may reach (`sparse_tl_need`), not for a freed mirror's spare unbind. A bind into a reused spare waits on the queue for that spare's unbind. `RX_VK_SPARSE_UNBIND_ASYNC=0` is the old path whole. - **Give-back capped:** at most `RX_VK_MIRROR_GIVE_BACK_MB` (256) of long-idle mirrors per sweep, oldest first, and always at least one. Mirrors given back for room are never held. 0 gives them all back at once, as before. - **New mirrors bound together:** up to `RX_VK_SPARSE_BIND_BATCH_MB` (64) per bind, sent at the end of the `mirror_make` loop. A refused batch is retried per mirror. 0 binds each alone, as before. Read first: `gpu/vk/sparse_smooth.h`, then `memory_sparse_wait`, `mirrors_give_back` and `sparse_reap` in `gpu/vk/vk_memory.cpp`. ## Testing **Tested:** Windows 11, AMD Radeon RX 6800, driver 26.10.1, with the other two PRs (Measurements); no visual differences seen. - In those runs every mirror was made at its first binding, with its own bind, so the batching never ran. - The cap held long-idle mirrors back twice in one run. **Checks:** - `tests/test_sparse_smooth.py` (new). - `test_sparse_bind_ranges.py` (the wait value, the spare's wait, the switch off), `test_sparse_mirror.py` and `test_sparse_spare.py` pass. - `clang++ -fsyntax-only` with the builder's flags on `vk_memory.cpp`. **Still to test:** - Linux RADV (Steam Deck, which has a separate sparse queue family) and NVIDIA: the tour, expecting no corruption and fewer late frames (@Beanstalk teleport through Market and Hyrule Field, stay 30 s in each). ## Measurements **Verdict:** each change does the work it targets (below), but on its own none moves total stutter at area changes beyond run-to-run noise. Together with its two sibling PRs, the worst frame at an arrival is 15-25% shorter. An earlier claim here of "-20% stutter in the loads" was mostly how long each load took: the loads' length alone varies from 3,300 to 4,900 frames between runs of the same build. **Setup:** - Windows 11, AMD Radeon RX 6800 (16 GB), AMD driver 26.10.1, i7-13700. The player's own settings: 1920x1080 output, uncapped, windowed. - A scripted tour, each run through the job queue in timing mode: the repository save `kokiri-start`, then teleports to Market (child), Hyrule Field (child), Kakariko Village, Kokiri Forest, Hyrule Field (adult) and Lake Hylia (adult). 25 s in each with the camera turning, plus 20 s walking in circles in Hyrule Field. - The frame trace (`RX_VK_FRAME_TRACE=1`). - **Visible stutter:** what frames of 25 ms or more spend over 16.7 ms, from 1 s before control returns to 2 s after, summed over the six arrivals. | Runs (each a separate launch) | Visible stutter at the 6 arrivals (ms) | Worst frame at an arrival (ms) | Texture creation in the loads (ms) | |---|---|---|---| | main @ 28cb22b9 (5) | 2583, 2943, 2948, 3263, 3491 | 390-422 | 492-550 | | texture block reserve alone (2) | 2624, 3245 | 355, 370 | 147-177 | | shader parse cache alone (2) | 2543, 3608 | 357, 366 | 525-576 | | sparse bind smoothing alone (2) | 2542, 3019 | 378, 395 | 495-560 | | all three together (3) | 2309, 2819, 3091 | 306-348 | 187-247 | In play (the walk and the camera turns, outside the arrivals), every run of every build has a median frame of 9.8-10.0 ms and 0-1 frames of 33 ms or more. What the worst arrival frames are spent on, in the runs with all three: draw 100-440 ms, mostly in the recording's commands (up to 396 ms: the first bindings of vertex and index buffers, which import host memory and make mirrors) and descriptors. A change that imports pools ahead of their first binding is being measured on top of these three. ## Risks - **Ordering:** if a driver orders later submissions behind a big unbind anyway, the cap is what helps. A needed bind after a big unbind still waits for it, since the timeline is in order. - **A refused batched bind** (out of memory, device lost) loses that mirror's inherited GPU-only data; the old path fell back to a whole mirror. - **Held mirrors** keep VRAM for a few more sweeps. - **One unexplained hang:** on the first run after building the three PRs together, the driver refused every submit with `VK_ERROR_OUT_OF_DEVICE_MEMORY` shortly after arriving in Market (7.8 of 15.5 GB of VRAM in use), and the game stopped drawing. It has not come back in nine runs since, and no run of main or the nightly (about 20) has refused a submit. Until it is explained, treat this PR as a possible cause. - **Turning it off:** `RX_VK_SPARSE_UNBIND_ASYNC=0 RX_VK_SPARSE_BIND_BATCH_MB=0 RX_VK_MIRROR_GIVE_BACK_MB=0`. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). (Not run yet: the checks reading the changed files pass, apart from failures they share with main.) - [ ] Measured against `main`, or Measurements says why there is nothing to measure. (Measured: no stutter gain on its own beyond the noise.) - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. (Rows added; defaults to be confirmed by this PR's own A/B.) - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
RX 6800 (Windows 11, AMD 26.10.1): an area's arrival spent ~65 ms of the replay in vkQueueBindSparse, one per new
mirror under dev.present_mu; and 2-8 s later 40-110 ms frames with the replay idle and the GPU queue waiting on the
sparse binds' timeline, one right after "171 mirrors given back (1475 MB)": the idle sweep gave every mirror past
kLongIdle back at once, sparse_reap unbound all their spares in one bind, and every submission of recordings waited for
the latest bind (memory_sparse_wait), that unbinding too, though nothing after it reaches a freed mirror's buffer.

- memory_sparse_wait waits for sparse_tl_need: the latest bind work after it may reach (new blocks, blocks given memory
  again, a live mirror's released blocks unbound). The spares' unbinding in sparse_reap is not one; a bind into a spare
  waits for its unbinding on the queue (VkBindSparseInfo wait on the same timeline). RX_VK_SPARSE_UNBIND_ASYNC=0: as before.
- mirrors_give_back gives back RX_VK_MIRROR_GIVE_BACK_MB (256) of long-idle mirrors a sweep at most, oldest first, always
  one; for room never held. 0: as before.
- The mirrors memory_begin_submission makes have their blocks bound together, up to RX_VK_SPARSE_BIND_BATCH_MB (64) a
  bind, sent before the submission's work (end of the loop, or before a mirror in the batch is given back). A refused
  batch is retried per mirror; a mirror still refused keeps its buffer with its blocks back in guest memory, wanted.
  0: each mirror's own bind, as before.
- The rules in gpu/vk/sparse_smooth.h, run by tests/test_sparse_smooth.py; counts on the existing sparse and give-back lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(vk): RX_VK_SPARSE_UNBIND_ASYNC=0 also drops the spare bind's own wait, so the kill switch is the old path whole
Some checks failed
PR description / Title and description (pull_request_target) Successful in 7s
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
cba8d844d4
With the switch off every submission already waits for the spare's unbinding, and the old code bound into a spare
with no wait in vkQueueBindSparse; the wait added with the unwaited unbinds now follows the switch. Checked by
tests/test_sparse_smooth.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Tested in game: Windows 11, AMD Radeon RX 6800, driver 26.10.1, i7-13700: a pack over main 28cb22b9 with the texture block reserve, shader parse cache and sparse bind PRs together, six teleport tours through Market, Hyrule Field (child and adult), Kakariko, Kokiri Forest and Lake Hylia with the player's settings; no visual differences, loads stutter about 20% less, frame rate in play unchanged. One run (the first after the build) froze with refused submits (VK_ERROR_OUT_OF_DEVICE_MEMORY), not seen again in nine runs: under investigation, see Risks.

Tested in game: Windows 11, AMD Radeon RX 6800, driver 26.10.1, i7-13700: a pack over main 28cb22b9 with the texture block reserve, shader parse cache and sparse bind PRs together, six teleport tours through Market, Hyrule Field (child and adult), Kakariko, Kokiri Forest and Lake Hylia with the player's settings; no visual differences, loads stutter about 20% less, frame rate in play unchanged. One run (the first after the build) froze with refused submits (VK_ERROR_OUT_OF_DEVICE_MEMORY), not seen again in nine runs: under investigation, see Risks.
Member

Possibly related, for context:

  • issue #585 Idea: sparse mirrors cost 1-4 ms of GPU a frame on an RX 5600 XT; whole mirrors are faster until their budget fills (runs wanted on other 6 GB cards) — Sparse-mirror performance context on a constrained AMD card
Possibly related, for context: - issue #585 Idea: sparse mirrors cost 1-4 ms of GPU a frame on an RX 5600 XT; whole mirrors are faster until their budget fills (runs wanted on other 6 GB cards) — Sparse-mirror performance context on a constrained AMD card
Author
Contributor

Measurements corrected: each change built alone over main (two runs each), against five runs of main. The earlier '-20% stutter in the loads' was mostly load length varying between runs. On its own this change does not move visible stutter at arrivals beyond the noise; with its two siblings the worst arrival frame is 15-25% shorter. Staying WIP while the next change (imports made ahead of their first binding) is measured on top.

Measurements corrected: each change built alone over main (two runs each), against five runs of main. The earlier '-20% stutter in the loads' was mostly load length varying between runs. On its own this change does not move visible stutter at arrivals beyond the noise; with its two siblings the worst arrival frame is 15-25% shorter. Staying WIP while the next change (imports made ahead of their first binding) is measured on top.
Author
Contributor

Closing as not significant: the stutter a player sees at an area change is not measurably lower with this change, alone or together with its siblings (#2890, #2891, #2892) and three further fixes. Each does the work it targets; the frames that stutter just move to other work.

Setup: Windows 11, RX 6800, driver 26.10.1, the player's settings, uncapped. A six-teleport tour, each run a separate launch in timing mode. Visible stutter is what frames of 25 ms or more spend over 16.7 ms, from 1 s before control returns to 2 s after, summed over the six arrivals.

Build Visible stutter at the arrivals Worst arrival frame
main @ 28cb22b9 (8 runs) 2527-3491 ms, median 2945 383-425 ms
this PR alone (2 runs each) within main's range 355-395 ms
#2890 + #2891 + #2892 together (6 runs) 2289-3091 ms, median 2626 287-348 ms
the three plus imports reused across loads, imports during loading screens, and mirrors idle by play time (3 runs) 2647-3223 ms, median 3046 372-485 ms

What did show:

  • #2890 cut texture creation in the loads from ~520 ms to ~170 ms.
  • Together, the three shortened the worst arrival frame by 15-25%.
  • With the further fixes, stutter inside the loads fell (2256-2711 ms against main's 2663-3195), but mostly behind the loading screen; and the 10 s after an arrival got worse (479-629 ms against 274-545).

What holds an arrival frame now, from symbolized samples: host-memory imports of the game's re-created area pools (39% of the replay thread), and mirrors rebuilt after the loading screen (~16%). The first frame of an area pays for whatever is first used in it; moving one cost earlier did not shorten that frame overall.

The branches stay on conongnang's fork in case someone picks this up.

🤖 Generated with Claude Code

Closing as not significant: the stutter a player sees at an area change is not measurably lower with this change, alone or together with its siblings (#2890, #2891, #2892) and three further fixes. Each does the work it targets; the frames that stutter just move to other work. **Setup:** Windows 11, RX 6800, driver 26.10.1, the player's settings, uncapped. A six-teleport tour, each run a separate launch in timing mode. Visible stutter is what frames of 25 ms or more spend over 16.7 ms, from 1 s before control returns to 2 s after, summed over the six arrivals. | Build | Visible stutter at the arrivals | Worst arrival frame | |---|---|---| | main @ 28cb22b9 (8 runs) | 2527-3491 ms, median 2945 | 383-425 ms | | this PR alone (2 runs each) | within main's range | 355-395 ms | | #2890 + #2891 + #2892 together (6 runs) | 2289-3091 ms, median 2626 | 287-348 ms | | the three plus imports reused across loads, imports during loading screens, and mirrors idle by play time (3 runs) | 2647-3223 ms, median 3046 | 372-485 ms | What did show: - **#2890** cut texture creation in the loads from ~520 ms to ~170 ms. - **Together**, the three shortened the worst arrival frame by 15-25%. - **With the further fixes**, stutter inside the loads fell (2256-2711 ms against main's 2663-3195), but mostly behind the loading screen; and the 10 s after an arrival got worse (479-629 ms against 274-545). **What holds an arrival frame now**, from symbolized samples: host-memory imports of the game's re-created area pools (39% of the replay thread), and mirrors rebuilt after the loading screen (~16%). The first frame of an area pays for whatever is first used in it; moving one cost earlier did not shorten that frame overall. The branches stay on conongnang's fork in case someone picks this up. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
conongnang closed this pull request 2026-11-30 10:56:00 +00:00
Some checks failed
PR description / Title and description (pull_request_target) Successful in 7s
CI / What the change needs (pull_request) Has been cancelled
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Has been cancelled
CI / Third-party parts and guide screenshots (pull_request) Has been cancelled
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Has been cancelled
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Has been cancelled
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Has been cancelled
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
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
MagicBeanDev/Longshot!2892
No description provided.