fix(agc): return a synchronous draw's render target memory to the image pool #9188

Merged
thuanquang merged 2 commits from upstream-pr/9188 into main 2026-11-02 07:57:20 +00:00
thuanquang commented 2026-10-21 14:43:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T14:43:45Z.


What

A synchronous draw (a draw into a color target that has no resident image) renders into a RenderTarget, a per-draw image of the target's size. RenderTarget takes its memory from the image pool (AllocateImageMemory) but freed it with vkFreeMemory, so the pool never got the memory back. Every synchronous draw therefore created and freed device memory of the whole target's size.

It now keeps the allocation's size and memory type and returns the memory with ReleaseImageMemory, as OwnedImage does. The next synchronous draw of the same size takes it from the pool. The image is still destroyed before its memory is released. Today the only production RenderTarget is the synchronous draw's, and it is destroyed after SubmitAndWait.

Tested

Rechecked merged onto main 14c99b52ac (2026-10-26, AMD Windows driver 32.0.21045.16010):

  • RenderTargetMemoryReuse fails with main's Resources.cpp (the two FAIL: lines below) in 3 of 3 rounds; with this change it passes 3 of 3.
  • 15 tests pass 3 of 3: the 12 listed below and three more that make synchronous draws, agc_driver_cmask_fast_clear, agc_driver_srgb8_color_target and agc_driver_packed_unorm4444_color_target.
  • Under the validation layer (inserted in all 14 GPU test processes, no "was not found" line), the only VUID- errors are 3 of VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 in agc_driver_srgb8_color_target. Plain main 14c99b52ac reports the same 3 there (#6782 enables independentBlend).

Rebased onto main 513ca7e558 (head fc74d9349f) for the needs-rebase conflict: #7919 added the agc_snapshot_copy_failure test block next to agc_buffer_pool_tests, whose target_sources line this PR extends with Resources.cpp. Both are kept and the PR's change is unchanged (git range-diff). On this head, built clean: the 12 tests below, 3 rounds, all pass; under the Khronos validation layer 12 of 12 pass with 0 VUID- lines, the layer inserted 11 times, no "was not found" line.

New test case: RenderTargetMemoryReuse in agc_buffer_pool (mock device, CPU only). It checks that a released render target's memory stays in the image pool and that the next render target of the same size takes it.

  • To build it, agc_buffer_pool_tests now also compiles Resources.cpp.
  • main f90a3f7297 with only the test fails:
    FAIL: a released render target freed its image memory instead of returning it to the image pool
    FAIL: a second render target of the same size allocated new image memory instead of the pooled one
    
  • With this change it passes 3 of 3.

RX 6700 XT (gfx1031), AMD Windows driver 32.0.21045.5002, Windows 11 26200, MinGW GCC 15.2 Release:

  • 12 tests, 3 rounds, all pass: agc_buffer_pool, 10 tests that draw through non-resident color targets and agc_driver_linear_target_resident, whose target is imported and resident:
    • agc_driver_uint8x4_color_target_no_resident
    • agc_driver_color1555_target, agc_driver_color565_target
    • agc_driver_packed_float_color_target, agc_driver_packed_unorm_color_target, agc_driver_packed_mip_tail_target
    • agc_driver_pixel32_ar_export, agc_driver_pixel_uint16_export
    • agc_driver_pixel_ancillary_word, agc_driver_pixel_fixed_point_position
    • agc_driver_linear_target_resident

Khronos validation layer A/B (VK_LOADER_DEBUG=layer shows the layer inserted in all 11 GPU test processes on both sides, with no "was not found" line), same 12 tests:

  • main with only the new test: 11 of 12 pass (the new case fails as above), 0 VUID- lines.
  • This PR: 12 of 12 pass, 0 VUID- lines.

PPSA14363 (METAL GEAR SOLID Master Collection Vol. 1, own dump), RX 6700 XT:

  • Workload. The title draws its PS1 games into three linear 3072x2048 R8G8B8A8 color targets that have no host import, so all those draws are synchronous. The PS1 intro makes up to about 80 of them per frame.
  • Runs. One run each, 150 s, APS5_PROFILE_DRAW=1, warm shader cache, the same scripted input (title screen, Game Selection, Start Game).
  • Local change on both sides: stop 1, sceNpTrophy2GetTrophyInfo: trophy 19 is not in .../trophy00.ucp, was bypassed locally; #9115 fixes it.
  • Results. From the [draws] and [bufferpool] lines, per 10 s window in the PS1 section:
main f90a3f7297 this PR
synchronous draws per window 80 to 162 261 to 596
average synchronous draw 27.3 to 49.5 ms 13.6 to 15.8 ms
prepare phase per synchronous draw (allocation and image creation) 5.5 to 12.1 ms 0.03 to 0.05 ms
writeBack phase per synchronous draw (includes the free) 12.6 to 17.3 ms 7.0 to 7.3 ms
image pool over the run 667 hits, 1287 misses 6502 hits, 37 misses

The game reached its 3000th guest flip (flip counts of the [packets] lines, PS1 logos) after 107.9 s on main and 69.4 s with this change. On both sides the host stopped presenting about 60 s into the run, while the guest kept flipping. That happened with APS5_PROFILE_DRAW=1 on this unattended desktop, so treat the flip times as rough. The per-draw rows above don't depend on it.

Gameplay, 2026-10-22: main 6ef5a1c4c9 + #9115 + #9373 on both sides. libSceAgcDriver.prx was built with and without #9188 and #9202; nothing else differs. Two 600 s runs per side, alternating, with the same scripted input and APS5_PROFILE_DRAW=1:

without #9188 and #9202 with both
MGS1 opening and codec, presents per 10 s 38 to 74 168 to 190
average synchronous draw 17.7 to 27.1 ms (one window 76 ms) 4.6 to 7.4 ms in the opening and codec, 1.3 to 2.1 ms at the Dock
codec line "The Codec's receiver directly stimulates the small bones of your ear." at 440 s and 538 s at 183 s and 204 s
Dock gameplay, radar on screen not reached in 600 s (both runs) at 237 s and 268 s

No run stopped. The second run with both changes was slower at the Dock (107 to 147 presents per 10 s, against 124 to 196 in the first). These runs held only this PC's GPU lock, so other work on the PC may have slowed it. The title's progress is tracked in its bring-up discussion, #9615.

With #9879: #9879 creates the host import of these three targets. In our runs with its branch (2026-10-23), the PS1 part made no synchronous draws on the RX 6700 XT, so once #9879 is merged this change no longer affects PPSA14363 on that GPU. It still applies to every target #9879 keeps synchronous (listed in #9879). The two PRs don't depend on each other and merge cleanly in either order.

Other GPUs, from reviewers: @MaxLikesCode, NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, head c9a9831f7 merged onto main 38d3c0858:

  • The 12 tests pass 3 of 3 on main and with this PR, without and with the validation layer, with no VUID- lines.
  • tools/select_tests.py on the merged tree: 1166 of 1169 pass. The 3 that fail (agc_driver_sample_order, _grid_reference, _recipe_depth_locations) fail the same way on main on that GPU (#9313).

Not tested: AMD GPUs other than the RX 6700 XT, Linux. The PS1 section is still far from 60 fps: each synchronous draw also copies the whole 24 MiB target in and out. That is a separate change.

Overlap: #10194 (@harry-z-kang) adds allocation accounting on the same render-target release path in Resources.cpp; agreed order: this PR first, then #10194 rebases around its ReleaseImageMemory call.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6700 XT (gfx1031), AMD Windows drivers 32.0.21045.5002 and 32.0.21045.16010, Windows 11 26200; NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro (MaxLikesCode)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4953461214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9188). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-21T14:43:45Z. --- ### What A synchronous draw (a draw into a color target that has no resident image) renders into a `RenderTarget`, a per-draw image of the target's size. `RenderTarget` takes its memory from the image pool (`AllocateImageMemory`) but freed it with `vkFreeMemory`, so the pool never got the memory back. Every synchronous draw therefore created and freed device memory of the whole target's size. It now keeps the allocation's size and memory type and returns the memory with `ReleaseImageMemory`, as `OwnedImage` does. The next synchronous draw of the same size takes it from the pool. The image is still destroyed before its memory is released. Today the only production `RenderTarget` is the synchronous draw's, and it is destroyed after `SubmitAndWait`. ### Tested **Rechecked merged onto `main` `14c99b52ac`** (2026-10-26, AMD Windows driver 32.0.21045.16010): - `RenderTargetMemoryReuse` fails with `main`'s `Resources.cpp` (the two `FAIL:` lines below) in 3 of 3 rounds; with this change it passes 3 of 3. - 15 tests pass 3 of 3: the 12 listed below and three more that make synchronous draws, `agc_driver_cmask_fast_clear`, `agc_driver_srgb8_color_target` and `agc_driver_packed_unorm4444_color_target`. - Under the validation layer (inserted in all 14 GPU test processes, no "was not found" line), the only `VUID-` errors are 3 of `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605` in `agc_driver_srgb8_color_target`. Plain `main` `14c99b52ac` reports the same 3 there (#6782 enables `independentBlend`). **Rebased onto `main` `513ca7e558`** (head `fc74d9349f`) for the needs-rebase conflict: #7919 added the `agc_snapshot_copy_failure` test block next to `agc_buffer_pool_tests`, whose `target_sources` line this PR extends with `Resources.cpp`. Both are kept and the PR's change is unchanged (`git range-diff`). On this head, built clean: the 12 tests below, 3 rounds, all pass; under the Khronos validation layer 12 of 12 pass with 0 `VUID-` lines, the layer inserted 11 times, no "was not found" line. **New test case:** `RenderTargetMemoryReuse` in `agc_buffer_pool` (mock device, CPU only). It checks that a released render target's memory stays in the image pool and that the next render target of the same size takes it. - To build it, `agc_buffer_pool_tests` now also compiles `Resources.cpp`. - `main` `f90a3f7297` with only the test fails: ``` FAIL: a released render target freed its image memory instead of returning it to the image pool FAIL: a second render target of the same size allocated new image memory instead of the pooled one ``` - With this change it passes 3 of 3. **RX 6700 XT** (gfx1031), AMD Windows driver 32.0.21045.5002, Windows 11 26200, MinGW GCC 15.2 Release: - 12 tests, 3 rounds, all pass: `agc_buffer_pool`, 10 tests that draw through non-resident color targets and `agc_driver_linear_target_resident`, whose target is imported and resident: - `agc_driver_uint8x4_color_target_no_resident` - `agc_driver_color1555_target`, `agc_driver_color565_target` - `agc_driver_packed_float_color_target`, `agc_driver_packed_unorm_color_target`, `agc_driver_packed_mip_tail_target` - `agc_driver_pixel32_ar_export`, `agc_driver_pixel_uint16_export` - `agc_driver_pixel_ancillary_word`, `agc_driver_pixel_fixed_point_position` - `agc_driver_linear_target_resident` **Khronos validation layer A/B** (`VK_LOADER_DEBUG=layer` shows the layer inserted in all 11 GPU test processes on both sides, with no "was not found" line), same 12 tests: - `main` with only the new test: 11 of 12 pass (the new case fails as above), 0 `VUID-` lines. - This PR: 12 of 12 pass, 0 `VUID-` lines. **PPSA14363** (METAL GEAR SOLID Master Collection Vol. 1, own dump), RX 6700 XT: - **Workload.** The title draws its PS1 games into three linear 3072x2048 R8G8B8A8 color targets that have no host import, so all those draws are synchronous. The PS1 intro makes up to about 80 of them per frame. - **Runs.** One run each, 150 s, `APS5_PROFILE_DRAW=1`, warm shader cache, the same scripted input (title screen, Game Selection, Start Game). - **Local change on both sides:** stop 1, `sceNpTrophy2GetTrophyInfo: trophy 19 is not in .../trophy00.ucp`, was bypassed locally; #9115 fixes it. - **Results.** From the `[draws]` and `[bufferpool]` lines, per 10 s window in the PS1 section: | | `main` `f90a3f7297` | this PR | |---|---|---| | synchronous draws per window | 80 to 162 | 261 to 596 | | average synchronous draw | 27.3 to 49.5 ms | 13.6 to 15.8 ms | | `prepare` phase per synchronous draw (allocation and image creation) | 5.5 to 12.1 ms | 0.03 to 0.05 ms | | `writeBack` phase per synchronous draw (includes the free) | 12.6 to 17.3 ms | 7.0 to 7.3 ms | | image pool over the run | 667 hits, 1287 misses | 6502 hits, 37 misses | The game reached its 3000th guest flip (`flip` counts of the `[packets]` lines, PS1 logos) after 107.9 s on `main` and 69.4 s with this change. On both sides the host stopped presenting about 60 s into the run, while the guest kept flipping. That happened with `APS5_PROFILE_DRAW=1` on this unattended desktop, so treat the flip times as rough. The per-draw rows above don't depend on it. **Gameplay, 2026-10-22:** `main` `6ef5a1c4c9` + #9115 + #9373 on both sides. `libSceAgcDriver.prx` was built with and without #9188 and #9202; nothing else differs. Two 600 s runs per side, alternating, with the same scripted input and `APS5_PROFILE_DRAW=1`: | | without #9188 and #9202 | with both | |---|---|---| | MGS1 opening and codec, presents per 10 s | 38 to 74 | 168 to 190 | | average synchronous draw | 17.7 to 27.1 ms (one window 76 ms) | 4.6 to 7.4 ms in the opening and codec, 1.3 to 2.1 ms at the Dock | | codec line "The Codec's receiver directly stimulates the small bones of your ear." | at 440 s and 538 s | at 183 s and 204 s | | Dock gameplay, radar on screen | not reached in 600 s (both runs) | at 237 s and 268 s | No run stopped. The second run with both changes was slower at the Dock (107 to 147 presents per 10 s, against 124 to 196 in the first). These runs held only this PC's GPU lock, so other work on the PC may have slowed it. The title's progress is tracked in its bring-up discussion, #9615. **With #9879:** #9879 creates the host import of these three targets. In our runs with its branch (2026-10-23), the PS1 part made no synchronous draws on the RX 6700 XT, so once #9879 is merged this change no longer affects PPSA14363 on that GPU. It still applies to every target #9879 keeps synchronous (listed in #9879). The two PRs don't depend on each other and merge cleanly in either order. **Other GPUs, from reviewers:** @MaxLikesCode, NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, head `c9a9831f7` merged onto `main` `38d3c0858`: - The 12 tests pass 3 of 3 on `main` and with this PR, without and with the validation layer, with no `VUID-` lines. - `tools/select_tests.py` on the merged tree: 1166 of 1169 pass. The 3 that fail (`agc_driver_sample_order`, `_grid_reference`, `_recipe_depth_locations`) fail the same way on `main` on that GPU (#9313). **Not tested:** AMD GPUs other than the RX 6700 XT, Linux. The PS1 section is still far from 60 fps: each synchronous draw also copies the whole 24 MiB target in and out. That is a separate change. Overlap: #10194 (@harry-z-kang) adds allocation accounting on the same render-target release path in `Resources.cpp`; agreed order: this PR first, then #10194 rebases around its `ReleaseImageMemory` call. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6700 XT (gfx1031), AMD Windows drivers 32.0.21045.5002 and 32.0.21045.16010, Windows 11 26200; NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro (MaxLikesCode) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MaxLikesCode commented 2026-10-21 19:53:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T19:53:22Z.


RTX 4090 (Ada) on Windows, head c9a9831f7 merged onto main 38d3c0858 (clean).

NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. The 12 tests from the description (agc_buffer_pool and the 11 that draw through non-resident color targets) with ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, 3 runs without and 3 with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan-ValidationLayers vulkan-sdk-1.4.363.0, implicit layers disabled). The loader log shows the layer inserted in all 11 GPU test processes, and no log has was not found but was requested by env var VK_INSTANCE_LAYERS.

Build Without the layer With the layer VUID-
main 12 of 12 pass, 3 of 3 runs 12 of 12 pass, 3 of 3 runs none
main + #9188 12 of 12 pass, 3 of 3 runs 12 of 12 pass, 3 of 3 runs none

tools/select_tests.py on the merged tree: 1166 of the 1169 selected tests pass. The 3 failures are agc_driver_sample_order, _grid_reference and _recipe_depth_locations, which fail the same way on main on this GPU (#9313, fixed by #9056). agc_driver_image_array_window_past_layer_limit is left out: it does not compile with MinGW (#8310).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6325149542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9188#issuecomment-6325149542). Originally posted by [MaxLikesCode](https://github.com/MaxLikesCode) on 2026-10-21T19:53:22Z. --- RTX 4090 (Ada) on Windows, head `c9a9831f7` merged onto `main` `38d3c0858` (clean). NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. The 12 tests from the description (`agc_buffer_pool` and the 11 that draw through non-resident color targets) with `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, 3 runs without and 3 with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Vulkan-ValidationLayers `vulkan-sdk-1.4.363.0`, implicit layers disabled). The loader log shows the layer inserted in all 11 GPU test processes, and no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`. | Build | Without the layer | With the layer | `VUID-` | |---|---|---|---| | `main` | 12 of 12 pass, 3 of 3 runs | 12 of 12 pass, 3 of 3 runs | none | | `main` + #9188 | 12 of 12 pass, 3 of 3 runs | 12 of 12 pass, 3 of 3 runs | none | `tools/select_tests.py` on the merged tree: 1166 of the 1169 selected tests pass. The 3 failures are `agc_driver_sample_order`, `_grid_reference` and `_recipe_depth_locations`, which fail the same way on `main` on this GPU (#9313, fixed by #9056). `agc_driver_image_array_window_past_layer_limit` is left out: it does not compile with MinGW (#8310). AI-assisted: yes (Claude Code)
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!9188
No description provided.