fix(agc): place device memory in host memory when the driver refuses a device-local allocation #5987
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5987"
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 GustavoGamarra95 on 2026-10-14T18:29:32Z.
What
DeviceMemoryTypeplaces a device-local allocation in host memory only whenVK_EXT_memory_budgetreports the heap as full. The report lags the driver. On a 4 GB RTX 3050 Laptop the heap was at 3684 of a 3685 MiB budget and the budget still showed room. The NVIDIA driver refused the allocation withVK_ERROR_OUT_OF_DEVICE_MEMORY, and the title stopped:With device buffers covered, the next run stopped the same way at
vkAllocateMemory depth target.This is mostly a Linux/NVIDIA problem. On Windows, WDDM demotes allocations to system memory itself instead of refusing them, so these paths should rarely run there. I have not tested Windows.
Host placement is the last step of
BufferPool::AllocateMemory, after the recovery that is already onmain:A refusal the reclaim can satisfy never reaches host placement.
Changes:
fix: a device buffer still refused after steps 1 and 2 is allocated in host memory. It is marked host-placed, so the device pool does not keep it after release.
fix: every image allocation goes through the same path (
AllocateImageMemory). This covers textures, storage textures, block, repacked, depth, depth plane and stencil copies, render, depth and multisampled targets, and attachment proxies. Several of these sites calledvkAllocateMemorydirectly and stopped the title on the first refusal: the render, depth and multisampled targets, the attachment proxies, and the block, repacked, depth plane and stencil copies. The 1x1 denormal clear probe is left as it is.Only
VK_ERROR_OUT_OF_DEVICE_MEMORYfrom a device-local type falls back. Other results, and requirements that allow no host type, fail as before.Each placement is logged with the bytes trimmed and released and the cumulative MiB, rate-limited to powers of two like the existing budget placement.
docs:
docs/dev/debt/libSceAgcDriver.mdrecords that a placed image keeps its placement until it is destroyed and is never moved back, so it is slower (PCIe) for its lifetime.test:
agc_buffer_pool_testscovers these cases:RefusedDevicePlacement: a device buffer refused within the reported budget must land in host memory and leave the pool on release; a context with no host type must still fail.AttachmentReclaim: an attachment refused once must reclaim the pool and stay device-local; refused twice, it must land in host memory.ImageCacheReclaim: a texture refused once must release the texture caches and stay device-local; refused again, it must land in host memory.Each fails without its fix.
agc_device_memory_fallback_testscovers the choice of fallback type: a refused device-local type, a refused host type, the type mask, and an unknown type.Overlaps #4472 in
Context.hppandtests/BufferPool.cpp. That PR stops pooling released device buffers over the budget; this one handles a refusal the budget did not predict. Whichever merges second needs a manual merge there.pr_overlapalso reports line overlaps with #7119 (tests/BufferPool.cpp) and #6764 and #7256 (DepthSurface.cpp). #6553 is stacked on this PR.Tested
Rebased onto
main9f25d0bf5(head 9f98c7c9b). Linux (CachyOS, kernel 7.2.9), Release build:agc_buffer_pool,_small_device_shared,_small_device_no_stagingagc_device_memory_fallbackagc_driver_graphicsagc_driver_covered_centroid_stateagc_driver_tessellationagc_driver_depth_storage_reuseagc_*ctestsBeast of Reincarnation (PPSA29343), RTX 3050 Laptop 4 GB, 900 s per run with the same scripted pad input. These runs used head 8bb3e07e0, which lacks only the repacked/stencil copy routing, the MiB counter and the
DeviceBuffercleanup:main(bd5856170), two warm runs: FPS / p50Both runs reached 900 s with no stop, no fault and no device lost. Peak VRAM was 3480 MiB, the same as
main. The driver refused no allocation in these runs, so the host placement path did not run there: they show the rebase costs nothing, not what a placement costs. The run with a cold shader cache (2617 misses) is left out of the table.Earlier data, before #6174 merged (head 4afec5739 and older):
mainstopped at the device buffer refusal after about 15 minutes. With only the buffer fallback, the next run stopped at the depth target refusal. With this PR the runs stayed up for the full 15–24 minutes measured.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)Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:16:52Z.
Review: COMMENTED
Falling back on
VK_ERROR_OUT_OF_DEVICE_MEMORYinstead of stopping is reasonable for buffers. They are pooled and short-lived, and a host-placed one leaves the pool on release, so the cost goes away with it.For images it is different, because a host-placed image keeps its placement until it is destroyed. If a refusal hits during a short spike, a long-lived render target or depth target lands in system memory and stays there for the rest of the session. Every draw that writes or tests against it then goes over PCIe, at a fraction of VRAM bandwidth. On a 4 GB laptop part that is a large, permanent frame-time loss that no log line ties to the spike. Render targets, depth targets, multisampled targets and attachment proxies are the worst candidates for host memory. Sampled textures and block/depth copies are much more tolerable.
Some suggestions:
maindoesn't hit the refusal) would show what the title pays.This is mostly a Linux/NVIDIA problem: on Windows, WDDM demotes allocations itself instead of refusing them. A sentence in the description saying so would help reviewers on the other platform.
Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-15T01:01:00Z.
#6174 touches the same allocation sites (it releases the texture caches before the pool's retry). I will put it behind this one and route its texture and storage image allocations through
Context::AllocateDeviceMemoryonce this is merged, so no change is needed here.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-15T11:24:27Z.
Thanks @CnRJay and @ee6aba54 for the feedback! I really appreciate you both taking the time to look into this.
I'll spend some time today going through the points you raised and figuring out the best way to address them.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:12:47Z.
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/Context.hppL308-313core/libs/prx/libSceAgcDriver/tests/BufferPool.cppL169-179, L209, L790-791core/libs/prx/libSceAgcDriver/tests/BufferPool.cppL177-179, L695core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppL253-261Some 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/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Context.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Resources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/BufferPool.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Context.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/BufferPool.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Context.hppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Resources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdImported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-15T12:32:49Z.
@CnRJay Thanks for the review. Updated:
AttachmentReclaimtest.main's budget placement of device buffers, not this PR. I corrected it.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-15T18:20:59Z.
Thanks. Attachments now go through reclaim and a device-local retry first, and only a second refusal places them in host memory, with the bytes logged. That addresses the permanent-placement concern. The correction to the "about 2 GB" figure and the WDDM note help too. Leaving the texture-cache release to #6174 is fine as long as both land. Nothing more from me.
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-16T02:04:28Z.
Data from 007 First Light (PPSA11386) on Linux 7.2.10, RTX 3080 10 GB, driver 615.78.08, a second title and GPU for the attachment path. Shortly after its level entry the title stops on the refusal this PR handles. The stack is main
4186f06a9plus open PRs and unpublished local fixes the title needs to get that far, plus local logging and scripted input, and the title's two feedback-wait limits widened in its executable as described in #6172 (local only). Three of the four runs below also carry #6174, and two of those also carry #6402 and #6364. 13 runs on these stacks stopped at a depth or render target; four are shown in detail.vkAllocateMemory depth target: Vulkan result -2and one atvkAllocateMemory render target: Vulkan result -2. They came after presents 1186, 1236, 1245 and 1268, which is 21-31 presents after each run's level entry (presents 1155-1241).Context::AllocateDeviceMemory. That counter reached 2048 allocations and 16,818 MiB placed by present 1403; 11 of its 14 printed lines were storage textures and 3 textures. Device buffers placed in host memory after the pool's trim (released with their use) reached 8192 by present 1391. All 16 printed lines of that counter are device buffers; none is an attachment. Both counts come from the last rate-limited line, are cumulative and are lower bounds, not resident memory. The frame rate fell over the same stretch: 0.12-0.13 at presents 1280-1296, 0.08 at 1330, 0.05 at 1360 and 0.03 from 1396 on. In that stretch the emulator created about 130 storage images (about 400 MiB) per present. This PR does not create these allocations; it decides where the refused ones go, and the run slows down instead of stopping.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-16T10:28:24Z.
Thanks @SleglJan for putting together these detailed results.
The comparison is really useful, especially the fact that the attachment path successfully recovered from 43 depth-target allocation failures and allowed the game to continue without a fault or stop.
The performance degradation is also worth keeping in mind. While this PR addresses the immediate allocation failure, sustained memory pressure and allocation overhead can still make the game effectively unplayable.
I'll investigate how I can further improve the recovery path and reduce the performance overhead under sustained memory pressure. These results provide a valuable starting point for identifying potential improvements.
Thanks again for the detailed testing and analysis!
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-16T15:39:00Z.
Windows data, since the description says it wasn't tested there: RTX 4060 8 GB (NVIDIA 617.42), 16 GB RAM, Windows 10, Minecraft Dungeons II world loads with an oversized cache budget. With this PR the load got past the point where
maindied, but the next abort was againVK_ERROR_OUT_OF_DEVICE_MEMORYon memory type 0, which on this driver is the host heap:vkAllocateMemory device buffer: allocation=134217728 memoryType=0 reclaimed=980363776 initialResult=-2 attempts=2. The buffer had already been placed in host memory byDeviceMemoryTypeand heap 1 was full, so the device-local retry here doesn't apply; in these rows no device-local allocation was ever refused, which fits WDDM demoting rather than refusing. Details and the heap 1 figures are on #6553: https://github.com/boykopovar/AnyPS5/pull/6553#issuecomment-6219031322AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T20:33:17Z.
Review: CHANGES_REQUESTED
Thanks for the work and for the per-run numbers. The approach looks fine to me, but it can't merge as it is:
main:MultisampleTarget.cppnow allocates insideMultisampleTarget::allocate(), so theAllocateAttachmentMemorycall has to move there.DepthSurfaceReuse.cppalso definesmapMemory/unmapMemory/destroyBuffermocks thatmainalready has, which breaks the build after the merge.agc_driver_graphics,agc_driver_covered_centroid_state(DCC over a multisampled color target is unsupported) andagc_driver_tessellation(segfault) fail. Please rebase, run them with and without the PR, and say which also fail onmain.Context.hpp(DeviceMemoryType/HostMemoryType); please agree the order with it and rebase on whichever lands first.Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-16T20:54:36Z.
@adriadam10 On the last point, a correction to my earlier comment. I listed the 0.12 → 0.03 FPS fall under what this PR costs, but that run did not carry #6465. The data I have since does not separate the cost of the host placements from the cost of the depth-surface list.
main4186f06a9with open PRs including #5987 and #6553, and the non-default switches listed there.Revalidategrew from 3.94 s to 18.2 s while its work per present stayed the same.DepthSurfaceTexture, which scans the depth-surface list on sampled-texture lookups, took 17.8% of the title process's samples.The storage-texture path still falls back on its first refusal without a reclaim, as you say.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ee6aba54 on 2026-10-16T23:40:30Z.
Point 4 (textures and storage textures going through the pool reclaim and a device-local retry first) is what #6174 does:
texture,depth copyandstorage textureallocate throughBufferPool::AllocateMemory, which trims the pool's idle tiers, releases sampled textures and clean storage images through a reclaim hook, and retries before it reports the failure. Both PRs change the same threevkAllocateMemorylines inTexture.cpp, so one of them will have to rebase on the other.A possible order: #6174 first (it merges cleanly with current
mainand does not need this PR), then this PR places memory in host memory only when the pool's reclaim and retry still fail, e.g. as the last step insideAllocateMemoryor in anAllocateDeviceMemorycalled by it. That covers the 0.12 to 0.03 FPS case, because a refusal that the reclaim can satisfy never reaches host placement. Happy to rebase #6174 onto whatever order you and #6553 agree on.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-17T20:50:29Z.
Thanks @adriadam10 for the review, and @CnRJay, @ee6aba54, @SleglJan and @parl0v for the runs, data and corrections on this PR; they made the remaining work much clearer. Sorry for the delay in getting back to this.
Rebased onto
main922dc8999:MultisampleTarget.cpp: the attachment call now lives inMultisampleTarget::allocate(), so both the plain and the storable target go throughAllocateAttachmentMemory.DepthSurfaceReuse.cpp: dropped this PR'smapMemory/unmapMemory/destroyBuffermocks;mainalready defines them. The PR no longer touches that file.Tests:
agc_driver_graphics,agc_driver_covered_centroid_stateandagc_driver_tessellationonmain922dc8999and on this branch 4afec5739, same Release build, Linux 7.2.9:main, lavapipemain, RTX 3050 Laptop 615.78.08agc_driver_graphicsagc_driver_covered_centroid_stateagc_driver_tessellationOn the PR,
agc_buffer_pool*,agc_device_memory_fallbackandagc_driver_depth_storage_reusealso pass on lavapipe. I couldn't reproduce the three failures. They may come from the conflicted merge (the duplicate mocks, or the oldMultisampleTargetallocation path). If they still fail for you on this head, which GPU and driver did you use?Beast of Reincarnation (PPSA29343): a 300 s run on this head (RTX 3050 Laptop 4 GB) had no stop, no fault and no device-memory refusal. Peak VRAM was 3169 MiB.
Order with #6553: #6553 is stacked on this PR (it carries these three commits and says "Depends on #5987"), so it would rebase after this one lands. @fdfawef324, does that work for you?
Textures and storage textures: I'd follow @ee6aba54's order. #6174 lands first and routes
texture,depth copyandstorage texturethroughBufferPool::AllocateMemory(trim the idle tiers, release sampled and clean storage images, retry device-local). This PR then rebases on it, and its host placement becomes the last step after that retry is also refused. A refusal that the reclaim can satisfy never reaches host placement, which covers the case you raised. Until #6174 lands, those three lines still fall back on the first refusal here. I'd rather not duplicate #6174's reclaim hook in this PR, since both change the same lines inTexture.cpp.On the 0.12 → 0.03 FPS run: per @SleglJan's correction, that run did not carry #6465, and its slowdown matches
Revalidate/DepthSurfaceTexturegrowth more than the host placements.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-18T04:12:48Z.
Rebased onto main
2626e5e, which carries #6174 (head b3187ec).Host placement is now the last step of
BufferPool::AllocateMemory, after the pool's own recovery and #6174's cache release:cacheReclaimand retry device-local memory;A refusal that the reclaim can satisfy never reaches host placement, which covers the textures and storage textures point from @adriadam10's review.
The new commit also routes every image allocation through one helper,
AllocateImageMemory(renamed fromAllocateAttachmentMemory). That covers textures, storage textures, block, depth and depth plane copies, render, depth and multisampled targets, and attachment proxies. Block copies and depth plane copies were not covered by #6174 and still fell back on their first refusal. They now take the same path.Context::AllocateDeviceMemoryis gone.Tests, Release build, Linux 7.2.9:
New
ImageCacheReclaimcase in agc_buffer_pool_tests:It fails when the cache release is skipped before host placement.
Correction to my earlier reply: the 007 First Light data came from @SleglJan, not @CnRJay. Sorry about the mix-up.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-18T11:19:39Z.
Rebased onto main
9f25d0bf5(head 9f98c7c9b). A commit merged since the last rebase added a repacked copy inTexture.cppthat calledvkAllocateMemorydirectly. It now goes throughAllocateImageMemory, and so does the depth stencil copy inDepthSurface.cpp. The host placement log line now carries the cumulative MiB. The debt file records that a placed image stays in host memory for its lifetime. The description is updated: it no longer refers toContext::AllocateDeviceMemoryor to #6174 as pending.All 700
agc_*ctests pass on the RTX 3050 Laptop (615.78.08). On lavapipe, the buffer pool, fallback, graphics and covered-centroid tests pass, and tessellation and depth-storage-reuse are skipped.Beast of Reincarnation ran 900 s on the previous head 8bb3e07e0 with no stop and no fault, at the same frame times as
main. The driver refused no allocation there, so those runs show the rebase costs nothing; the numbers are in the description.@adriadam10, the four points from your review should be covered now; could you take another look?
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-18T11:37:16Z.
@adriadam10 when you have a moment, could you start the full build for this head (9f98c7c9b)? Only collaborators can run it:
Locally all 700
agc_*ctests pass on the RTX 3050 Laptop. I have not built it on Windows.Thanks again to @SleglJan, @parl0v, @CnRJay and @ee6aba54 for the runs, data and reviews that shaped this PR.