feat(agc): place textures and storage images in host memory when device memory runs out #4108

Merged
GustavoGamarra95 merged 4 commits from upstream-pr/4108 into main 2026-10-12 14:41:45 +00:00
GustavoGamarra95 commented 2026-10-11 23:29:04 +00:00 (Migrated from github.com)

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_budget budget 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). vkAllocateMemory is called once with the chosen type and still throws when it fails. Without VK_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's VK_EXT_memory_budget for heap 0 drops from 3735 to 2952 MiB. Beast of Reincarnation (PPSA29343) then failed vkAllocateMemory storage texture at 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::AllocateMemory already 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.

  • Built on current main; tools/check_conventions.py passes.
  • Full ctest on the RTX 3050: 951 of 951 pass (6 of them skipped: 5 video_out_flip/frame_dump tests and agc_driver_stencil_ref_export), agc_buffer_pool and agc_device_memory_fallback included.
  • agc_device_memory_fallback passes, and fails in 2 of its 7 cases with the host branch removed.
  • PPSA29343 at the title screen in 4K, on a local branch that also carries the title's rendering fixes, 300 to 330 s per run. At the branch's commit before this change the failure is intermittent: vkAllocateMemory texture: Vulkan result -2 at 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_fallback covers the memory type choice with a mocked vkGetPhysicalDeviceMemoryProperties2; whether a run reaches the budget depends on the GPU, the driver and the timing.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823202825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108). Originally posted by [GustavoGamarra95](https://github.com/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_budget` budget 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). `vkAllocateMemory` is called once with the chosen type and still throws when it fails. Without `VK_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's `VK_EXT_memory_budget` for heap 0 drops from 3735 to 2952 MiB. Beast of Reincarnation (PPSA29343) then failed `vkAllocateMemory storage texture` at 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::AllocateMemory` already 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. - Built on current `main`; `tools/check_conventions.py` passes. - Full `ctest` on the RTX 3050: 951 of 951 pass (6 of them skipped: 5 `video_out_flip`/`frame_dump` tests and `agc_driver_stencil_ref_export`), `agc_buffer_pool` and `agc_device_memory_fallback` included. - `agc_device_memory_fallback` passes, and fails in 2 of its 7 cases with the host branch removed. - PPSA29343 at the title screen in 4K, on a local branch that also carries the title's rendering fixes, 300 to 330 s per run. At the branch's commit before this change the failure is intermittent: `vkAllocateMemory texture: Vulkan result -2` at 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_fallback` covers the memory type choice with a mocked `vkGetPhysicalDeviceMemoryProperties2`; whether a run reaches the budget depends on the GPU, the driver and the timing. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-12 00:50:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T00:50:04Z.


Both PRs touch Texture.cpp in the storage/texture allocation path, so reviewing together — only #4108 also touches Context.hpp:

  • #4108's AllocateDeviceMemory is additive: it always makes the same first device-local attempt and returns its VkResult unless it is exactly VK_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.
  • #3956's new debug code path (FlushDumpSelected) runs only when APS5_DUMP_STORAGE is set AND APS5_STORE_AT_FLIP is NOT set — the two debug knobs are mutually exclusive by construction (the else in PresentDisplayBuffer), so there's no double-flush.
  • The two PRs do not conflict: #4108 edits only the Texture and StorageTexture constructors, where #3956's writeBackLayers and the new FlushDumpSelected/dump-selector code sit roughly 2000 lines away in different functions.
  • One real defect in #3956, independent of the above: 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, so anyFormat is set false even though nothing was parsed into format. Checking the actual field count sscanf returns (>= 4 means a format was given) rather than the presence of a colon would avoid that.
  • Neither PR adds an automated test, which the bodies state plainly; for #4108 that's reasonable for the real-OOM part, though the memory-type selection loop itself doesn't need a real GPU — the existing BufferPool.cpp test already mocks a Context with a VK_ERROR_OUT_OF_DEVICE_MEMORY-returning allocator, and the same mock would exercise AllocateDeviceMemory directly. For #3956 it's a debug aid gated behind an env var, genuinely hard to test meaningfully in CI.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115796289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108#issuecomment-6115796289). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T00:50:04Z. --- Both PRs touch `Texture.cpp` in the storage/texture allocation path, so reviewing together — only #4108 also touches `Context.hpp`: - #4108's `AllocateDeviceMemory` is additive: it always makes the same first device-local attempt and returns its `VkResult` unless it is exactly `VK_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. - #3956's new debug code path (`FlushDumpSelected`) runs only when `APS5_DUMP_STORAGE` is set AND `APS5_STORE_AT_FLIP` is NOT set — the two debug knobs are mutually exclusive by construction (the `else` in `PresentDisplayBuffer`), so there's no double-flush. - The two PRs do not conflict: #4108 edits only the `Texture` and `StorageTexture` constructors, where #3956's `writeBackLayers` and the new `FlushDumpSelected`/dump-selector code sit roughly 2000 lines away in different functions. - One real defect in #3956, independent of the above: `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, so `anyFormat` is set false even though nothing was parsed into `format`. Checking the actual field count `sscanf` returns (`>= 4` means a format was given) rather than the presence of a colon would avoid that. - Neither PR adds an automated test, which the bodies state plainly; for #4108 that's reasonable for the real-OOM part, though the memory-type selection loop itself doesn't need a real GPU — the existing `BufferPool.cpp` test already mocks a `Context` with a `VK_ERROR_OUT_OF_DEVICE_MEMORY`-returning allocator, and the same mock would exercise `AllocateDeviceMemory` directly. For #3956 it's a debug aid gated behind an env var, genuinely hard to test meaningfully in CI. Written with AI assistance.
GustavoGamarra95 commented 2026-10-12 03:01:03 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116963273 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108#issuecomment-6116963273). Originally posted by [GustavoGamarra95](https://github.com/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.
oneandonlydean commented 2026-10-12 08:25:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T08:25:25Z.


AllocateDeviceMemory allocates 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 (the VK_EXT_memory_budget figures you quote would do) and keep the throw when that allocation fails.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6120538781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108#issuecomment-6120538781). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T08:25:25Z. --- `AllocateDeviceMemory` allocates 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 (the `VK_EXT_memory_budget` figures you quote would do) and keep the throw when that allocation fails.
GustavoGamarra95 commented 2026-10-12 12:33:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-12T12:33:20Z.


Changed in b35117c. Context::DeviceMemoryType reads 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. vkAllocateMemory is called once with that type and Check still 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_fallback now checks the choice against a mocked vkGetPhysicalDeviceMemoryProperties2 (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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124339912 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108#issuecomment-6124339912). Originally posted by [GustavoGamarra95](https://github.com/GustavoGamarra95) on 2026-10-12T12:33:20Z. --- Changed in b35117c. `Context::DeviceMemoryType` reads 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. `vkAllocateMemory` is called once with that type and `Check` still 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_fallback` now checks the choice against a mocked `vkGetPhysicalDeviceMemoryProperties2` (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.
GustavoGamarra95 commented 2026-10-12 12:34:21 +00:00 (Migrated from github.com)

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_MEMORY looked 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124355000 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4108#issuecomment-6124355000). Originally posted by [GustavoGamarra95](https://github.com/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_MEMORY` looked 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4108
No description provided.