WIP: perf(vk): keep lowered shader stages by their bytes, so a new program of known code is not parsed again #2891

Closed
conongnang wants to merge 1 commit from (deleted):perf/shader-parse-cache into main
Contributor

Summary

After an area loads, the game makes new programs of shader code it has used before. On main the same code was then parsed and lowered again on the replay thread: about 85 ms per arrival in symbolized samples (program_pick > codes_of > read_live_code > parse_code > LowerToMaxwell). Two caches miss it:

  • codes_of keeps stages by program serial, and drops them when the game frees the program.
  • read_live_code's only other cache is a 32-slot ring of weak references.

This keeps each lowered stage by its bytes:

  • Key: bytes.key plus dev.native_mma; a hit needs the 216-byte control section and the whole code area to match byte for byte.
  • Size: least recently used goes first past 96 MB.
  • Locking: the cache has its own lock, never held during a parse.
  • Switch: RX_VK_PARSE_CACHE=0 puts back the ring exactly as before.

Read first: gpu/vk/parse_cache.h, and read_live_code in gpu/vk/vk_shader.cpp.

Note: vk_shader.cpp is in the shader translator's stamp, so players prepare the shaders once after updating.

Testing

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

  • In the runs with all three PRs the loads looked shorter (3337-3743 frames against 4790-4822), but two later runs of main took 3691-3755: load length varies too much between runs to credit this change with it.
  • The saving it targets (about 85 ms of shader parsing per arrival, in samples) is not visible above the noise in the frame trace.

Checks:

  • tests/test_parse_cache.py (new): matching, collisions, eviction, the cap, 8 threads.
  • tests/test_live_shader_stage.py: runs the real read_live_code with the cache on and off; its "all owners released" step fails on main.
  • The other checks that read the changed files pass, apart from failures shared with main.
  • clang++ -fsyntax-only -Wall -Wextra on the changed files.

Still to test:

  • Steam Deck: memory with the cache (up to 96 MB) over a long session (by hand).
  • NVIDIA and Intel: the tour, expecting shorter loads and no visual change (@Beanstalk teleport through Market, Hyrule Field and Kakariko; compare load lengths with main).

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

  • Memory: up to 96 MB more RAM.
  • The translator stamp moves: every player prepares the shaders once after updating.
  • 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_PARSE_CACHE=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 After an area loads, the game makes new programs of shader code it has used before. On main the same code was then parsed and lowered again on the replay thread: about 85 ms per arrival in symbolized samples (`program_pick > codes_of > read_live_code > parse_code > LowerToMaxwell`). Two caches miss it: - `codes_of` keeps stages by program serial, and drops them when the game frees the program. - `read_live_code`'s only other cache is a 32-slot ring of weak references. This keeps each lowered stage by its bytes: - **Key:** `bytes.key` plus `dev.native_mma`; a hit needs the 216-byte control section and the whole code area to match byte for byte. - **Size:** least recently used goes first past 96 MB. - **Locking:** the cache has its own lock, never held during a parse. - **Switch:** `RX_VK_PARSE_CACHE=0` puts back the ring exactly as before. Read first: `gpu/vk/parse_cache.h`, and `read_live_code` in `gpu/vk/vk_shader.cpp`. Note: `vk_shader.cpp` is in the shader translator's stamp, so players prepare the shaders once after updating. ## Testing **Tested:** Windows 11, AMD Radeon RX 6800, driver 26.10.1, with the other two PRs (Measurements). No visual differences seen. - In the runs with all three PRs the loads looked shorter (3337-3743 frames against 4790-4822), but two later runs of main took 3691-3755: load length varies too much between runs to credit this change with it. - The saving it targets (about 85 ms of shader parsing per arrival, in samples) is not visible above the noise in the frame trace. **Checks:** - `tests/test_parse_cache.py` (new): matching, collisions, eviction, the cap, 8 threads. - `tests/test_live_shader_stage.py`: runs the real `read_live_code` with the cache on and off; its "all owners released" step fails on main. - The other checks that read the changed files pass, apart from failures shared with main. - `clang++ -fsyntax-only -Wall -Wextra` on the changed files. **Still to test:** - Steam Deck: memory with the cache (up to 96 MB) over a long session (by hand). - NVIDIA and Intel: the tour, expecting shorter loads and no visual change (@Beanstalk teleport through Market, Hyrule Field and Kakariko; compare load lengths with main). ## 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 - **Memory:** up to 96 MB more RAM. - **The translator stamp moves:** every player prepares the shaders once after updating. - **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_PARSE_CACHE=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)
perf(vk): keep lowered shader stages by their bytes, so a new program of known code is not parsed again
Some checks failed
PR description / Title and description (pull_request_target) Successful in 5s
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
4b28f9df62
After a load the game frees its programs and makes new ones (new serials) of code it had before. codes_of keeps the
lowered stages by serial and read_live_code's 32-slot ring holds only weak references, so a stage whose programs were all
freed was parsed and lowered again: ~85 ms of the replay thread an arrival on an RX 6800 (program_pick > codes_of >
read_live_code > parse_code > LowerToMaxwell).

gpu/vk/parse_cache.h keeps them by the stage's code key and dev.native_mma (all else parse_code reads is the bytes or
read once a run), comparing the stored control section and code area before it answers, least recently used out past
96 MB of what the entries hold. Its own mutex, never held across a parse. RX_VK_PARSE_CACHE=0 (kill) goes back to the
ring alone; RX_VK_DUMP_NATIVE still parses each one. A 600-present line counts the stages taken and lowered when they
moved.

Tests: tests/test_parse_cache.py (key, compare, variant, eviction, size cap, threads), tests/test_live_shader_stage.py
runs read_live_code with the cache and with RX_VK_PARSE_CACHE=0 (its "all owners released" step fails on main).

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.
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:55:56 +00:00
Some checks failed
PR description / Title and description (pull_request_target) Successful in 5s
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
1 participant
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!2891
No description provided.