perf(agc): draw cache rebuilds shader stages when only per-draw buffer bases in user SGPRs move (Vampire Survivors late game) #9755

Open
opened 2026-10-22 12:47:16 +00:00 by krishalivaibhav · 0 comments
krishalivaibhav commented 2026-10-22 12:47:16 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-22T12:47:16Z.


Summary

In Vampire Survivors (PPSA21703), the late game makes about 10,500 draws per second. Most of them differ from a cached draw only in buffer descriptor base addresses held directly in user SGPRs: Unity's per-draw constants advance through a ring buffer. The draw cache misses these draws, and the data-only path cannot reuse their stages, so every draw rebuilds its shader stages. Each draw costs about 39 us of driver CPU, and the draw submission thread runs at 98% busy. FPS falls from 60 to 38-53 once the screen fills with enemies, while the GPU stays about half idle at 6-12 ms per frame.

Environment

macOS, Apple M2 (MacBook Air, fanless), MoltenVK 1.4.2, x86_64 build under Rosetta. The driver code paths cited below are the same on current main.

Measurements

These are from the last 10 s of a 10-minute play run, with APS5_TRACE_DRAW_CACHE=1 and APS5_PROFILE_DRAW=1.

  • [draw-cache]: 104,846 lookups and 21,872 hits (20.9%). 78,427 lookups found no entry: 69,602 were user words/pointers only and 8,238 were evictions (4096-entry table). Data-only hits: 78,409; 153,326 stages compiled and 3,492 reused.
  • [draw-cache] register-key miss component changes: 78,427 in user data, 1,938 in stable registers.
  • [draw-cache] stage data hits: 0. Stage inserts with data positions: 0.
  • No [draw-relocate] output.
  • [draw] driver phases (microseconds per packet): decode 9.7, key/lookup/validate 7.3, capture 5.6, recompile 5.6, lock wait 5.5; total 39.3. Graphics::Draw recording adds about 31 us per draw.
  • [rescache]: 100% hits, so resource validation is not the cost.

A sampled diagnostic of the user words that change between a data-only hit and its entry is a local print, not proposed for merging. It shows the same 6 programs each time. Only the first dword of 4-dword buffer descriptors in the vertex stage's user SGPRs changes (words 0/4/8/12/16, for example 43e56ce8 -> 40ec7b00), along with pointer words in the second stage (0x0222xxxx, 0x40ecxxxx).

Why the cache cannot reuse these stages

  1. The register key misses. The register key includes the user words, so these draws have no cache entry.
  2. The data-only fallback rejects them. DrawLookup.cpp accepts a candidate only when every changed user word is outside variant->userDataMask. The mask includes userDataUse.asBase words (DrawCapture.cpp, where stageCapture.userDataMask is set), so a moved base always rejects the candidate.
  3. The captured runs stay at the old base. Even if the mask allowed the change, the variant's runs were captured by walking memory from the old base, and validateVariant compares them at the old addresses.
  4. Patchable data words are recorded only for flattened SRTs. recordStageDataPositions records words only from a FlattenedSrt binding's memory runs, so constants reached through user-SGPR descriptors never become data positions.
  5. Relocation needs identical content. DrawRelocate.cpp handles moved asBase & ~asValue words. But relocatedContentKey hashes the content behind the pointer, which changes on every draw here, so relocation never finds a donor.

Possible direction

  • Combine relocation with data positions. When only asBase & ~asValue user words differ from a shape-matched entry, rebase the variant's runs by each base's delta. Validate the rebased runs at the new addresses. Reuse the stage if the remaining differences fall only on words the compiled shader reads as data, and refresh those words as stageDataHits already does.
  • Extend the recompiler. It would need to report data-word positions for buffers reached through user-SGPR descriptors, not only for flattened SRTs.
  • Guard against wrong output. Misclassifying a word would silently draw wrong constants. APS5_VERIFY_DRAW_DATA_HITS should cover the new path, with a test where a V# base moves between draws and its contents change only in data words.

The expected gain is up to about 20 us of decode, capture and recompile per draw. At about 200 draws per frame, that is roughly 4 ms per frame, enough to keep this title near 60 FPS in the late game on M2.

The cheaper partial fix is to raise APS5_DRAW_CACHE_ENTRIES, which covers only the evicted 8%.

AI-assisted: yes (Claude). The measurements are from local runs on the reporter's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5979591754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/9755). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-22T12:47:16Z. --- ## Summary In Vampire Survivors (PPSA21703), the late game makes about 10,500 draws per second. Most of them differ from a cached draw only in buffer descriptor base addresses held directly in user SGPRs: Unity's per-draw constants advance through a ring buffer. The draw cache misses these draws, and the data-only path cannot reuse their stages, so every draw rebuilds its shader stages. Each draw costs about 39 us of driver CPU, and the draw submission thread runs at 98% busy. FPS falls from 60 to 38-53 once the screen fills with enemies, while the GPU stays about half idle at 6-12 ms per frame. ## Environment macOS, Apple M2 (MacBook Air, fanless), MoltenVK 1.4.2, x86_64 build under Rosetta. The driver code paths cited below are the same on current `main`. ## Measurements These are from the last 10 s of a 10-minute play run, with `APS5_TRACE_DRAW_CACHE=1` and `APS5_PROFILE_DRAW=1`. - `[draw-cache]`: 104,846 lookups and 21,872 hits (20.9%). 78,427 lookups found no entry: 69,602 were user words/pointers only and 8,238 were evictions (4096-entry table). Data-only hits: 78,409; 153,326 stages compiled and 3,492 reused. - `[draw-cache] register-key miss component changes`: 78,427 in user data, 1,938 in stable registers. - `[draw-cache] stage data hits`: 0. Stage inserts with data positions: 0. - No `[draw-relocate]` output. - `[draw] driver phases` (microseconds per packet): decode 9.7, key/lookup/validate 7.3, capture 5.6, recompile 5.6, lock wait 5.5; total 39.3. Graphics::Draw recording adds about 31 us per draw. - `[rescache]`: 100% hits, so resource validation is not the cost. A sampled diagnostic of the user words that change between a data-only hit and its entry is a local print, not proposed for merging. It shows the same 6 programs each time. Only the first dword of 4-dword buffer descriptors in the vertex stage's user SGPRs changes (words 0/4/8/12/16, for example `43e56ce8 -> 40ec7b00`), along with pointer words in the second stage (`0x0222xxxx`, `0x40ecxxxx`). ## Why the cache cannot reuse these stages 1. **The register key misses.** The register key includes the user words, so these draws have no cache entry. 2. **The data-only fallback rejects them.** `DrawLookup.cpp` accepts a candidate only when every changed user word is outside `variant->userDataMask`. The mask includes `userDataUse.asBase` words (`DrawCapture.cpp`, where `stageCapture.userDataMask` is set), so a moved base always rejects the candidate. 3. **The captured runs stay at the old base.** Even if the mask allowed the change, the variant's runs were captured by walking memory from the old base, and `validateVariant` compares them at the old addresses. 4. **Patchable data words are recorded only for flattened SRTs.** `recordStageDataPositions` records words only from a `FlattenedSrt` binding's memory runs, so constants reached through user-SGPR descriptors never become data positions. 5. **Relocation needs identical content.** `DrawRelocate.cpp` handles moved `asBase & ~asValue` words. But `relocatedContentKey` hashes the content behind the pointer, which changes on every draw here, so relocation never finds a donor. ## Possible direction - **Combine relocation with data positions.** When only `asBase & ~asValue` user words differ from a shape-matched entry, rebase the variant's runs by each base's delta. Validate the rebased runs at the new addresses. Reuse the stage if the remaining differences fall only on words the compiled shader reads as data, and refresh those words as `stageDataHits` already does. - **Extend the recompiler.** It would need to report data-word positions for buffers reached through user-SGPR descriptors, not only for flattened SRTs. - **Guard against wrong output.** Misclassifying a word would silently draw wrong constants. `APS5_VERIFY_DRAW_DATA_HITS` should cover the new path, with a test where a V# base moves between draws and its contents change only in data words. The expected gain is up to about 20 us of decode, capture and recompile per draw. At about 200 draws per frame, that is roughly 4 ms per frame, enough to keep this title near 60 FPS in the late game on M2. The cheaper partial fix is to raise `APS5_DRAW_CACHE_ENTRIES`, which covers only the evicted 8%. AI-assisted: yes (Claude). The measurements are from local runs on the reporter's machine.
Sign in to join this conversation.
No labels
No milestone
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.

Reference
boykopovar/AnyPS5#9755
No description provided.