perf(agc): keep the current present's storage images in a recovery that can place the new allocation in host memory #12786
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12786"
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 milescoviello on 2026-10-29T09:00:43Z.
What
#12684 lets the sampled texture cache grow past the device's share while a frame's textures don't fit. While the budget has that room, an out-of-memory recovery for an allocation with a host-memory fallback stops at the first sampled entry used in the last two presents. The recovery then goes on to the storage image cache and evicts its least recently used images, whatever their age. In Ghost of Tsushima Director's Cut (PPSA02225 02.024.000), those are the frame's own storage images. They're made again at their next use: 45,000 to 117,000 of them over 210-600 s of the opening, which is 86 to 103 s of storage image creation.
How
Storage cache entries now record the present of their last use: a hit, an insert,
StorageImageCached,StorageImagesCachedandRepeatStorageSurface. Under the same condition as the sampled stop (room past the device's share, and an allocation with a host-memory fallback), the recovery also stops at the first storage image used in the last two presents. The room is mirrored in an atomic, so the storage half can read it while the sampled cache is busy. The[gpu] texture caches releasedline says when the storage half stopped.USAGE.md and the debt entry for the room mention the storage stop.
Tested
At this head
5bcf672fd1(oneandonlydean's review onContext::DeviceMemoryType):ReclaimFallbackScopemoves toContext.hpp, built on aSwapReclaimHostFallbacksetter thatBufferPool.cppdefines next toReclaimHasHostFallback.DeviceMemoryTypenow opens the scope around its reclaim before a budget-full allocation goes to host memory (#13064), so the storage stop also applies there. A new recovery mode,recover-storage-recent-type, reports the device-local heap full when the new texture picks its memory type. It requires the current present's storage image to stay cached, the texture to go to host memory, and no device allocation to fail.ctest -R 'agc_driver_recover_|agc_device_memory_fallback', 15 tests, passes 3 of 3 at this head, the new mode included.cmake --build buildof every target passes at8f9db9591e, which differs from this head only in the new test's request-count check.ctest -R 'agc_driver_recover_|agc_device_memory_fallback', 15 tests, passes 3 of 3 at this head.41391cc931is this head withContext.hppandBufferPool.cppreverted, so only the new test remains. It failsrecover-storage-recent-type3 of 3 on the Windows VM:while the sampled cache was past the device's share, an allocation that could go to host memory evicted a storage image of the current present. That is the reclaim throughDeviceMemoryTypeevicting the image.Earlier, at
08ae978210(oneandonlydean's review; details):d13440dd05takes the room the storage half uses from the sampled half's read under its lock.08ae978210adds four recovery modes:storage-hit,storage-repeatandstorage-proved, which keep an image used in the current present through a cache hit,RepeatStorageSurfaceor a fast proof (StorageImagesCached), andstorage-aged, which evicts an image left untouched for two presents. The 25 tests pass 3 of 3. A mutant dropping the present stamp at each of the three sites fails its mode. Khronos validation: 19 of 19 atba0eaedff6and 25 of 25 here, noVUID-messages.Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti (NVIDIA 615.71.09), at the first head
544cbf2044(on #12684's headba0eaedff6):ctest -R '^agc_driver_recover_|^agc_sampled_cache|^agc_driver_sampled_cache|^agc_driver_storage_cache|^agc_buffer_pool', 21 tests, passes in each of 3 runs. That includes the two new recovery modes:agc_driver_recover_storage-recent_allocation: with room past the device's share and a host fallback, the recovery keeps a storage image used in the current present.agc_driver_recover_storage-recent-noroom_allocation: with no room, it still evicts that image, as before.814a4a79e7(the storage stop removed) failsstorage-recent:while the sampled cache was past the device's share, an allocation that could go to host memory evicted a storage image of the current present.d48018c0d8(the stop on any host fallback, room or not) failsstorage-recent-noroom:with no room past the device's share, sampled allocation recovery did not evict the storage image of the current present.VK_LAYER_KHRONOS_validation, LunarG SDK) on the same tests, atba0eaedff6and at this head: 9 of 9 and 11 of 11 pass, the layer checked the Vulkan device each run creates, and there are noVUID-messages on either side.Windows 11 VM (GeForce GT 1030, WinLibs GCC, Release), the same regex: at this head 21 of 21 pass in each of 3 runs, and on the stack's base
mainc9207624bc14 of 14 (the tests there).Ghost of Tsushima Director's Cut (PPSA02225 02.024.000), the opening, 600 s runs with pad input and warm caches,
APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_SECONDS=300:60, alternating without and with the stop. As in #12684, plainmainstops early in this title onnative multisample stencil or depth format import is not implemented(#9717), so "without" is #12684's capture buildee55f90649(its local stack plus #12684's three commits), and "with" is229a6c1473, the same build plus this PR's commit (the same changed lines asba0eaedff6..544cbf2044). Each run's trace,aps5-summary.jsonllines,trace_digest.pyoutput on the trace and on the summary file, andperformance_compare.pyoutput for both pairs are in the release storage-recency-got-captures.Graphics.Draw.draw_and_resource_releaseat -70.6 %.Window summary lines (
aps5-summary.jsonl): B1 and B3 without, S2 and S4 withperformance_compare.py, B3 (baseline) against S4 (candidate)Performance comparison
Frame and GPU times (ms)
Top phases (ms per frame)
An earlier, env-gated version of this stop (
71f251195e) on the same stack gave 22 and 23 presents in 210-600 s with it, against 12 without.AMD RDNA 4, run by @bunbeans2-arch (comment): Windows 11, RX 9070 XT,
c81fda17d1merged ontomainf7f03daf52, 3 runs, Khronos validation layer loaded. 12 of the 13 recovery modes and theagc_driver_sampled_cache*tests pass 3 of 3, with noVUID-line.recover-storage-provedis skipped there as on the Windows VM, becauseRevalidatetakes the full walk on Windows. ItsStorageImagesCachedstamp is covered by the Linux run.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)ba0eaedff6). #6194 (Thunderhawkk) grows the storage cache's budget while it evicts under pressure, a different mechanism; it conflicts withmainitself, so its textual overlap with this PR can't be checked yet. Whichever lands second rebases.🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T11:10:47Z.
Review: APPROVED
Reviewed — approved
The storage stop matches the sampled stop's condition and records the present at every place that moves a storage entry to the front of the cache. The 25 focused tests pass with Khronos validation and no
VUID-messages, and the four new "keep" modes fail when this PR's production change is reverted.What I ran
c81fda17d1bb6aad9fc008f7f1848b6d874a7078(stacked on #12684's headba0eaedff633434f93e03d39a19913ee4b799edd)main:ba5b5330b37724bca4b99f30faab504653635a04, merged with the head intobc0f5bb2a1. Themainbaseline below ran at the same commit.08ae978210toc81fda17d1. The amend changes onlySampledCacheBudget.cpp:recover-storage-provednow skips instead of failing when the descriptor set does not take the fast proof. I repeated the merge, the checks, the full build, the focused tests with validation, themainbaseline and the negative control atc81fda17d1. The two extra mutants further down ran at08ae978210, whose production code is identical.Results
mainCMakeLists.txt, the debt file andUSAGE.md)git diff --checktools/check_conventions.py --base origin/mainAll conventions checks passed.libs(merged)recover-storage-provedran (it took the fast proof here, it was not skipped)main, with validationVUID-linesmainwas not found but was requested by env var VK_INSTANCE_LAYERSagc_driver_recover_storage-*) and onmaintrace_digest.pyoutputs and bothperformance_compare.pyoutputsMerged tree, new tests
Negative control
The tests depend on #12684's API (
SetSampledSpillForTest, the room), so restoring production files frommainwould only break the build. Instead I reverse-applied this PR's ownShaderResources.cppdiff (ba0eaedff6..c81fda17d1) on the merged tree, rebuilt and ran the six new tests. The modes that check old behaviour still pass (storage-recent-noroom,storage-aged). The four modes that check the stop fail with their messages:Output
I also ran two mutants that the description does not cover. The first replaces the atomic read at the top of
ReleaseTextureCaches(ShaderResources.cpp:1478) withfalse. Onlyagc_driver_recover_storage-proved_allocationfails, and its recovery logs0 sampled entries (cache busy, skipped). So the fallback tosampledRoomwhen the sampled half is skipped is covered, but only through the proved fixture (see the second non-blocking finding). The second, which narrows the window, is described in the third non-blocking finding.Findings
Blocking
None.
I checked these points in the code:
splicesites and the insert stamppresentunder the storage cache mutex:cachedStorageTexturehit and insert,StorageImageCached,StorageImagesCachedandRepeatStorageSurface. No other site moves a storage entry to the front, so the LRU-orderbreakat ShaderResources.cpp:1532-1535 cannot skip past an older entry that sits behind a recent one.present != 0 && it->present + 1u >= presentwindow and the samehostFallback && roomcondition as the sampled stop.keepRecentis recomputed fromcache.spillunder the sampled lock (ShaderResources.cpp:1487). Point 1 is confirmed by the negative control above.Non-blocking
recover-storage-provedpasses only because the sampled half is skipped there and the storage half readssampledRoom. If a later change made the sampled half run in that fixture, the atomic path would lose its only test without any test failing. Suggest a dedicated mode that holds the sampled cache busy (or uses the inherited, non-newImagepath) with room set, and asserts that the storage image is kept and that the log line sayscache busy, skipped.storage-agedages the entry by two presents and expects it evicted. The "keep" modes touch it in the current present. Nothing checks an entry last used exactly one present earlier, whichit->present + 1u >= presentkeeps. I narrowed the check at ShaderResources.cpp:1532 toit->present >= present(current present only), and all 13agc_driver_recover_*tests still passed. A variant ofstorage-agedthat ages the entry by one present and expects the image to stay would pin the window.recover-storage-provedcan hide theStorageImagesCachedstamp. At SampledCacheBudget.cpp:1291-1295, the mode returnsVulkanTestSkippedwhen the secondRevalidatetakes the full walk. On a host where that happens, nothing tests the stamp inStorageImagesCached, and ctest reports a skip, not a failure. Here it took the fast proof and ran. If the fast proof depends only on the fixture (the write-watched surface) and not on the device, aRequirewould keep the stamp tested everywhere. If it does depend on the device, the skip is fine, but the description should say on which devices the mode skips.NVIDIA GeForce RTX 4070 Ti (Linux)but no driver, and it omits the GeForce GT 1030 (Windows 11 VM) named in Tested. The details are in the Tested section, so this only affects the checklist line.main. AGENTS.md and the bot's comment ask for amaincapture. The description explains thatmainstops early in PPSA02225 on an unimplemented native multisample stencil or depth import, and the baselineee55f90649differs from the candidate229a6c1473only by this PR's commit, so the comparison isolates the change. The baseline also contains an unpublished local stack, though, so nobody else can rebuild it. Naming the commits in that stack, or pushing the baseline branch next to the release, would make the captures reproducible.Not verified
APS5_DRAW_PIPELINEgap the author describes (an epoch-shortcut proof that does not stamp storage images) was not exercised.Pull request closed