perf(agc): grow the resource cache while the keys it evicts come back #7189
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7189"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-17T15:38:26Z.
What
The resource cache (
ResourceCache, the builtShaderResourcesthat a repeated dispatch or draw binds again) has a fixed bound of 1024 entries. A title that binds more distinct resource sets per frame than that evicts each set before the next frame needs it again, so every frame rebuilds every set.The bound now grows when that happens:
[rescache] capacity A -> B entriesline is printed each time.The bound also comes back down. Once grown, the cache counts the distinct keys over a window of
bound × shrink windowuses. When at most a quarter of the bound served that window, the bound halves, down to 1024 at least, and the extra entries leave at the next insert along with the other evictions. The shrink window starts at 8 and doubles, up to 1024, each time growth undoes the last shrink, so the bound stops swinging when keys come back over a longer span.Clear()resets the bound to 1024.APS5_RESOURCE_CACHE_ENTRIESstill sets a fixed bound, with no growth. The table-element budget (APS5_RESOURCE_CACHE_TABLE_ELEMENTS) is unchanged, and so are the evictions it makes.In GTA V (PPSA04264) in the open world, about 2,400 draws per frame bind about 9,700 distinct sets. On
main, the draw path missed the cache 47,236 times per 10 s; the misses repeat keys the cache had evicted ([rescache] miss churn: "same key" 98,736). The draw committer spent about 10 % of its samples building these sets again (ShaderResources::ShaderResources).Test:
agc_driver_resource_cache_growth(tests/execution/ResourceCacheGrowth.cpp). It sets a base bound of 32 entries and a first shrink window of 2 (ResourceCache::SetBase, so the test stays near 1.2k dispatches) and turns dispatch recipes off (APS5_NO_DISPATCH_RECIPE), so every dispatch goes through the cache. It runs 48 compute dispatches, each with its own buffer, four times in turn, and checks that each dispatch stored its value and that the cache then holds all 48 sets. It then checks that two hot sets bring the bound back down, that the next insert trims the cache to it, that the large set grows it again, that the same hot pass then leaves it there (the backoff), and thatClear()resets it to the base. Without the growth it fails: "the resource cache kept only 32 entries (initial bound 32), so every pass rebuilt every set". Without the shrink, the backoff doubling or theClear()reset, it fails at the matching check.Related: #6972 (index the resource cache by a four-lane key hash) rewrites the same class's private members for a different purpose, so the two conflict textually. Whichever merges second needs a short rebase; the growth only needs the evicted keys' hashes, which #6972 already stores in each entry. #6492 (drop entries that hold released sampled textures) adds a member next to the same lines; only those lines need merging by hand.
Tested
Windows 11 Pro (26200), RTX 4090 (NVIDIA 616.92), i9-13900K, MinGW GCC 15.2.0:
agc_driver_resource_cache_growth: passes in about 0.5 s; with the growth, the shrink, the backoff doubling or theClear()reset removed, it fails (messages above).ctest -R "agc_driver|agc_masked|agc_shader"on this branch (f27ad681e): 524/524 pass, 2 skipped. The GTA build below uses the same change on a base that also carries #6488 and #6848, which GTA needs to render.APS5_PROFILE_DRAW=1, last 12 windows of 10 s:[rescache] drawsmisses / 10 sThe bound grew 1024 -> 2048 -> 4096 -> 8192 -> 16384, and the cache settled at 9,708 entries. Process RAM was the same in both runs (25 GB).
APS5_TRACE_SECONDS=240:60, same scene and session, no otherAPS5_TRACE_*/PROFILE_*variables:Traces: base, this PR. Both commits are the base above plus two local bench hooks that do nothing unless their variable is set. Read these numbers with care: the frame rate in this scene depends on where the save respawns the player, both runs were still compiling some shaders (
Driver.Draw.shader_compile_and_link106 and 55 ms per frame), and queue worker 0's trace buffer filled after about 4 s in both runs (trace_dropped). The committer figures above are the steadier comparison.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01XBXM4pgkMHYEtqN1EQni2P
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T15:57:16Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppL661-668, L671-672core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppL3304-3305, L3309-3310, L3438-3440, L3445-3447, L3491-3493Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppImported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-17T16:37:44Z.
AMD data point, since the description lists NVIDIA: head
c652880f4as-is, Release, Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, AMD Radeon RDNA 2 iGPU (1002:164e, driver 32.0.21045.5002) chosen withANYPS5_GPU.agc_driver_resource_cache_growth: 5 of 5 pass (not skipped).1536 dispatches with distinct resources repeated 4 times in turn, but the resource cache kept only 1024 entries (initial bound 1024).Not checked: other tests, titles, timings.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T22:23:34Z.
Thanks for this. Review and full CI pass on
c652880f4, and our games gate shows no regressions, but the cache growth needs one change before we merge.In Astro Bot the cache grows from 1024 to 8192 entries, and peak VRAM goes from 13.2-13.7 GB on main to 14.6 GB with this PR (about +1.0-1.4 GB). The bound only ever grows, not even
Clear()resets it, so one heavy scene keeps the larger cache for the rest of the process. Neither of our gate titles shows an FPS gain, so on those titles it's extra VRAM for nothing.Could you let the bound come back down? For example, reset it to the base size on
Clear()and shrink it again when the evicted keys stop coming back, or cap the growth lower than 8192. Either way, please include a test that the bound shrinks.If you can also post the miss/return counters from GTA V before and after (the 47k -> 120 per 10 s numbers), with the VRAM figure from the same run, that would help us weigh the trade-off.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-17T22:30:30Z.
Thanks for the Astro Bot numbers, that's a fair point. I'll let the bound come back down: reset to the base size on
Clear(), and shrink it once evicted keys stop coming back. I'll add a test that the bound shrinks, and post the GTA V miss/return counters with peak VRAM from the same run, before and after.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T00:01:35Z.
Done in
bea4be8f9: the bound now comes back down.bound × shrink windowuses (Find hits, Touch hits, inserts). If at most a quarter of the bound served that window, the bound halves, but never below 1024. The entries over the new bound leave at the next insert, along with the other evictions, so a caller under the GPU mutex still hands them to the recorder. I didn't trim insideFind: it can run without the GPU mutex (PrepareDispatch), and freeing a set returns it todescriptorCache.Clear()resets the bound to 1024, and the shrink window to 8.Test (
agc_driver_resource_cache_growth, 1536 dispatches with distinct sets, then 64 of them repeated 640 times):Clear()resets it to 1024.Run on
c652880f4, the test fails at step 2 ("64 sets served 40960 dispatches, but the bound stayed at 2048 entries"). Without the doubling, it fails at step 5.ctest -R "agc_driver|agc_masked|agc_shader": 524/524 pass, 2 skipped, RTX 4090.GTA V, open world. Build: main + #6488 + #6848, same save, one run each, last 12 windows of 10 s. VRAM is the whole GPU's
memory.usedfromnvidia-smi, sampled every 2 s over the run; the idle desktop accounts for about 1.7 GB of it.[rescache] drawsmisses / 10 sbea4be8f9)The base misses are lower than the 47k in the PR body because the respawn point changes the scene, and misses vary a lot between runs. The committer figures carry the same noise. With the backoff, GTA settles after two shrinks, at 8192 entries (it used to reach about 9.7k at the 16384 cap). In this title the larger cache costs no measurable VRAM: the peak is within run-to-run noise of main.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T00:57:17Z.
Thanks for adding the shrink. The bound now comes back down as asked, and the review of
bea4be8f95found no problems in the cache code.One thing blocks CI: the new
agc_driver_resource_cache_growthtest times out on the Linux runner (llvmpipe). In full CI run 39081985777 onbea4be8f95it was killed at the 120 s ctest limit, and it was the only failure (Linux 1 of 1406; it skips on Windows, which has no Vulkan device). On the earlier headc652880f4it passed but already took 48.8 s, and CONTRIBUTING.md asks for tests to finish within 30 s. The extra shrink and regrow passes push it to roughly 100k dispatches.Could you make the test cheaper rather than raising the timeout? One option is a test-only way to set a smaller base bound and window, so the grow, shrink and regrow steps take a few thousand dispatches instead of ~100k. It should still fail without the doubling and without the
Clear()reset, as it does now. We'll rerun full CI on the new head.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T01:10:17Z.
Thanks.
f27ad681emakes the test short without touching the timeout.ResourceCache::SetBase(entries, shrinkWindow)sets the bound a cleared cache starts from and shrinks back to (default 1024) and the first shrink window (default 8). It takes effect at the nextClear(). The test sets 32 and 2, so its working sets are 48 sets and 2 hot sets.APS5_NO_DISPATCH_RECIPE=1, the wayImageTableCacheBoundssets its budgets. With the small sizes, a dispatch recipe hit onlyTouches its template and never re-inserts it, so after an eviction or aClear()some sets bypassed the cache and the bound saw no misses. With recipes off, every dispatch goes throughFind/Insert.Every step runs in about 1.2k dispatches in total (it was about 100k): grow, shrink, trim at the next insert, regrow, the backoff, then
Clear()and grow again. Here it takes 0.5 s on the RTX 4090. I have no llvmpipe setup, so the CI run will show the Linux time.Each control still fails at its own check:
Clear()reset: "Clear() left the bound at 64 entries instead of 32".ctest -R "agc_driver|agc_masked|agc_shader": 524/524 pass, 2 skipped. Conventions pass. The PR body is updated.AI-assisted (Claude Code).