Skip to content

perf(agc): draw cache keys on the user words a program reads, memoizes failures, serves data-only hits - #291

Closed
MichonGoddijn231849 wants to merge 5 commits into
boykopovar:mainfrom
MichonGoddijn231849:split/draw-cache-user-words
Closed

MichonGoddijn231849 wants to merge 5 commits into
boykopovar:mainfrom
MichonGoddijn231849:split/draw-cache-user-words

Conversation

@MichonGoddijn231849

Copy link
Copy Markdown
Contributor

What

Split out of #167. Three changes to the draw cache:

  • Keys on the user words a program reads. The draw key mixes only the user words each stage's RSRC2 counts, so words a program never reads no longer make every draw a miss. APS5_DRAW_KEY_ALL_USER_WORDS=1 mixes all 32.
  • A failure memo. A draw that failed is not decoded and compiled again while the memory it read is unchanged, for at most APS5_DRAW_FAILURE_RETRY_MS (500 ms).
  • Data-only hits. A draw that differs from its shape's newest entry only in user data reuses that entry's decode and the compiled stages whose reads still hold, re-reading only the user words. APS5_NO_DRAW_DATA_HITS=1 disables them, and APS5_VERIFY_DRAW_DATA_HITS=1 compiles and compares instead.

A new key of a shape replaces that shape's previous entry instead of filling the cache with keys that never repeat. The new entry starts without the previous entry's variants: those were compiled under other user words, and a variant validates only the memory it captured, not the user words that pointed there.

That inheritance caused the regression found on #167 (Dreaming Sarah's menu text invisible until clicked). Each text draw took over the previous one's variants and drew with its data.

Tested

Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: feat(agc): host import windows, least-recent eviction under the budget #290 (the failure memo records the GPU out-of-memory count it adds)
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

MichonGoddijn231849 and others added 5 commits October 3, 2026 10:23
…t, recoverable GPU out-of-memory

A guest allocation larger than 64 MiB (APS5_HOST_IMPORT_WINDOW_MIB) is imported in windows around
the range a draw or dispatch uses instead of whole, so one large heap no longer takes the import
budget (APS5_HOST_IMPORT_MIB, 6 GiB as before). Windows follow a grid from the range's start and the
last one takes the remainder, so no window is smaller than the window size. Past the budget the
least recently used imports no draw holds are retired first; only what still does not fit is
refused. A refused import of 16 MiB or more that failed for memory retires imports and is tried
once more; a refused import is never offered again and its range is copied, as before. A draw or
dispatch whose allocation fails with VK_ERROR_OUT_OF_DEVICE_MEMORY waits for the GPU, drops the
shared resource cache, retires a quarter of the live imports (at least 256 MiB), lowers the import
limit to the larger of what stays live and half the budget, trims the buffer pool, and runs once
more.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
A window made while a smaller import of the same range was still in use,
or a window over other cells than an existing one, left two live imports
over the same bytes. Lookups and the unit shadows take the one import
before an address, so a shadowed write-back in one import stayed hidden
from reads through the other. A new import now takes in every import it
would overlap and replaces them, and is not made while a build still
holds one of them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… of evicting; default budget from RAM

An address-based build imports every registered allocation. When that set is larger than the
budget, evicting least-recent imports during the pass retires imports the same pass took or will
take, moves the import epoch, and keeps the address space from being published, so every build
re-imported guest memory (about 20 ms each in GTA V). The pass now refuses past the budget (those
ranges use mirrors as before) and no longer evicts.

APS5_HOST_IMPORT_MIB now defaults to half of physical memory, at least 6 GiB; the recoverable
out-of-memory path still lowers the limit when the driver runs out.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Both returns of configuredBudget's lambda must deduce the same type; on Linux GCC
strtoull's unsigned long long and std::uint64_t differ.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…s failures, serves data-only hits

The draw key mixes only the user words each stage's RSRC2 counts (APS5_DRAW_KEY_ALL_USER_WORDS=1
mixes all 32). A draw that failed is not retried while the memory it read is unchanged, for at most
APS5_DRAW_FAILURE_RETRY_MS (500 ms). A draw that differs from its shape's newest entry only in user
data reuses that entry's decode and the compiled stages whose reads still hold, re-reading only the
user words (APS5_NO_DRAW_DATA_HITS=1 disables, APS5_VERIFY_DRAW_DATA_HITS=1 compiles and compares).
A new key of a shape replaces the shape's previous entry instead of keeping both; it starts without
that entry's variants, which were compiled under other user words.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@boykopovar this one needs your call before Michon rebases it: should a draw that failed be skipped on a timer? This PR keeps a failed draw rejected until the memory it read changes or 500 ms pass (APS5_DRAW_FAILURE_RETRY_MS). Failures in the decode step are stored without any memory runs (recordDrawFailure(failureKey, error, memoryFailures, {}, {}) in Draw.cpp), so for those only the clock ends the skip, and which draws get skipped then depends on timing. That is a project policy question more than a plain bug.

It also bundles two other mechanisms (draw keys on the user words a program reads, and data-only hits), and #422, merged today, already memoizes source failures one layer down. My read: I'd take the user-word keys and the data-only hits as separate PRs, and a failure memo only if it ends on memory changes alone. It needs a rebase after #290 and #422 either way.

@MichonGoddijn231849

MichonGoddijn231849 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author

That split makes sense to me, and I'd rather not have a time-based skip in the driver either. My plan, unless @boykopovar prefers otherwise:

  1. A separate PR: draw cache keys on the user words a program reads.
  2. A separate PR: data-only hits.
  3. The failure memo only if it can end on memory changes alone. Decode-step failures carry no memory runs, so for those I'd drop the memo rather than keep the 500 ms timer. With perf(agc): memoize source failures of draw stages as for compute #422 already memoizing source failures, it may not be worth a PR at all; I'll check what's left after rebasing on feat(agc): host import windows, least-recent eviction under the budget #290 and perf(agc): memoize source failures of draw stages as for compute #422.

I'll close this one when the split PRs are up.

@MichonGoddijn231849

Copy link
Copy Markdown
Contributor Author

Split as discussed: #493 keys draws on the user words each stage reads, and #494 (on top of it) adds the data-only hits. The time-based failure memo is dropped: #422 already memoizes source failures, and a skip that ends on a clock isn't exact. Both are rebased on current main and no longer stack on #290.

@MichonGoddijn231849
MichonGoddijn231849 deleted the split/draw-cache-user-words branch October 9, 2026 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants