feat(agc): place textures and storage images in host memory when device memory runs out #4108
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4108"
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-11T23:29:04Z.
What
Before a sampled texture or storage image is allocated, the device-local heap's
VK_EXT_memory_budgetbudget and usage are read; when the allocation does not fit in what is left, it is placed in a memory type the image allows whose heap is not device-local (system memory on a discrete GPU).vkAllocateMemoryis called once with the chosen type and still throws when it fails. WithoutVK_EXT_memory_budget, or when the heap reports no budget, the device-local type is used as before. Host placements are logged on the first 5 allocations and then at powers of two, with the running count and size.The texture and storage image caches each have a 2048 MiB floor (
ShaderResources.cpp), so on a 4 GB GPU the storage image cache alone reaches its limit while the driver'sVK_EXT_memory_budgetfor heap 0 drops from 3735 to 2952 MiB. Beast of Reincarnation (PPSA29343) then failedvkAllocateMemory storage textureat the title screen after about 200 s. Lowering the budget (APS5_TEXTURE_CACHE_MIB=1024) made it worse: the cache evicted and recreated images constantly (4200 creations, 23 GB in total).Device buffers are left as they are:
BufferPool::AllocateMemoryalready trims the pool when an allocation fails.Tested
Linux (CachyOS, kernel 7.2), Ryzen 5 7535HS, RTX 3050 Laptop 4 GB (NVIDIA 615.78.08), GCC, Release.
main;tools/check_conventions.pypasses.cteston the RTX 3050: 951 of 951 pass (6 of them skipped: 5video_out_flip/frame_dumptests andagc_driver_stencil_ref_export),agc_buffer_poolandagc_device_memory_fallbackincluded.agc_device_memory_fallbackpasses, and fails in 2 of its 7 cases with the host branch removed.vkAllocateMemory texture: Vulkan result -2at about 210 s in 1 of 2 runs, with device memory at 3806 MiB. With this change: no failure in 5 runs. In one of them the driver lowered the device-local heap budget to 2999 MiB and one 2.7 MiB texture was placed in system memory at about the point where the run without the change failed; in the others nothing was placed in system memory. With the change the title ran at about 1.6 presents/s.agc_device_memory_fallbackcovers the memory type choice with a mockedvkGetPhysicalDeviceMemoryProperties2; whether a run reaches the budget depends on the GPU, the driver and the timing.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T00:50:04Z.
Both PRs touch
Texture.cppin the storage/texture allocation path, so reviewing together — only #4108 also touchesContext.hpp:AllocateDeviceMemoryis additive: it always makes the same first device-local attempt and returns itsVkResultunless it is exactlyVK_ERROR_OUT_OF_DEVICE_MEMORY, so every other result (success or any other error) passes through unchanged. That's a transparent substitution, not a new regression surface on the existing success path.FlushDumpSelected) runs only whenAPS5_DUMP_STORAGEis set ANDAPS5_STORE_AT_FLIPis NOT set — the two debug knobs are mutually exclusive by construction (theelseinPresentDisplayBuffer), so there's no double-flush.TextureandStorageTextureconstructors, where #3956'swriteBackLayersand the newFlushDumpSelected/dump-selector code sit roughly 2000 lines away in different functions.parsed.anyFormat = std::strchr(text, ':') == nullptr;means a shape string with a trailing colon and no format digits (e.g.512x512x1:) is read as "match format 0" instead of "match any format" — the colon is present, soanyFormatis set false even though nothing was parsed intoformat. Checking the actual field countsscanfreturns (>= 4means a format was given) rather than the presence of a colon would avoid that.BufferPool.cpptest already mocks aContextwith aVK_ERROR_OUT_OF_DEVICE_MEMORY-returning allocator, and the same mock would exerciseAllocateDeviceMemorydirectly. For #3956 it's a debug aid gated behind an env var, genuinely hard to test meaningfully in CI.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-12T03:01:03Z.
Thanks. Added
agc_device_memory_fallback(23c985f): a mocked Context with a device-local heap and a host heap covers success on the first try, the host fallback, a mask without host types, other results passing through, and a failing fallback. It fails with the fallback removed.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T08:25:25Z.
AllocateDeviceMemoryallocates again in a host heap after the device refuses the first allocation (Context.hpp:205-215). Retrying after an error isn't allowed here; choose the memory type before allocating (theVK_EXT_memory_budgetfigures you quote would do) and keep the throw when that allocation fails.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-12T12:33:20Z.
Changed in
b35117c.Context::DeviceMemoryTypereads the device-local heap's VK_EXT_memory_budget budget and usage before the texture and storage image allocations and picks a host memory type the image allows when the allocation does not fit.vkAllocateMemoryis called once with that type andCheckstill throws when it fails. Without VK_EXT_memory_budget, or when the heap reports no budget, the device-local type is used as before.agc_device_memory_fallbacknow checks the choice against a mockedvkGetPhysicalDeviceMemoryProperties2(7 cases) and fails with the host branch removed. In game the check fired in 1 of 5 runs (budget lowered to 2999 MiB) and none failed; without the change 1 of 2 failed.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-12T12:34:21Z.
@oneandonlydean, on why the first version retried after the error: in this title the shortage is not steady. Device memory sits a few MiB under the 4 GB heap (3787 to 3806 MiB in every run) and the driver lowers the heap budget while the game runs (3784 MiB at start, 2999 MiB in one run), so whether an allocation fits depends on the moment, and it failed in 1 of 2 runs. Acting on
VK_ERROR_OUT_OF_DEVICE_MEMORYlooked like the narrowest change: it only touched the result that otherwise aborted the game, kept the first device-local attempt and every other result as they were, and needed no budget query per allocation. Choosing from the budget avoids relying on a failed allocation and gave the same outcome in the runs above, so the PR now does that.