fix(agc): release the stencil shadow probe image once when its allocation fails #26178

Merged
Plyxen merged 1 commit from upstream-pr/26178 into main 2026-11-27 21:35:50 +00:00
Plyxen commented 2026-11-23 11:45:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-23T11:45:47Z.


What

ProbeStencilShadowCopy (Graphics/src/DepthSurface.cpp) destroys a probe image twice when its memory allocation fails. The make lambda hands the image to an OwnedImage and then, if Allocate throws, destroys the image by hand and rethrows; unwinding then runs OwnedImage::~OwnedImage(), which calls vkDestroyImage on the same handle. The probe's outer catch only runs after that. The image also leaks if make_shared throws after vkCreateImage succeeded.

  • The OwnedImage is now created empty, takes the handle once vkCreateImage has succeeded, and is the only owner. The manual vkDestroyImage and its try/catch are gone.
  • The probe's result and its success path are unchanged.

mihiii1337 found this shape in the resolve probe of #25911, which I had copied from here; #25911 fixes its own copy and this is the original. Of the other places in this file that hand an image to an OwnedImage, the depth upload image, the native depth samples, the sampled depth shadow and DepthPlaneCopy release it through one owner; Texture.cpp already creates the owner empty first, as this change does. I did not trace the failure paths of the depth surface's own image.

tools/pr_overlap.py --branch confirms no overlap, and could not compare 20 open pull requests that conflict with main and change DepthSurface.cpp (#1368, #1395, #6808, #7256, #7642, #9508, #9895, #9950, #10193, #11444, #11551, #13863, #13920, #13928, #13929, #15621, #18185, #20315, #22062, #22647). I read each one's DepthSurface.cpp patch: none changes a line of ProbeStencilShadowCopy.

Tested

Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, main 318cc76b1e and the change on it, same machine. ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (1.4.363), VK_LOADER_DEBUG=layer, GPU selected with ANYPS5_GPU, ctest --test-dir build -R '^agc_.*(depth|stencil)' -V -j 2 (116 tests).

Radeon iGPU, AMD 24.30.36 RTX 5080, NVIDIA 616.56
main 116 pass (1 skipped); 0 VUID- 116 pass (2 skipped); 0 VUID-
change 116 pass (1 skipped); 0 VUID- 116 pass (2 skipped); 0 VUID-

The loader logs Insert instance layer "VK_LAYER_KHRONOS_validation" 158 times in each of the four logs and never was not found but was requested by env var VK_INSTANCE_LAYERS.

A separate build with a temporary print at the top of the probe (not in this change) shows that 14 of the 116 tests run it, 187 times on each GPU, for D32_SFLOAT_S8_UINT at 2, 4 and 8 samples: agc_driver_stencil_copy_draw, agc_driver_stencil_replay, _no_gpl and _unsuppressed, and ten agc_driver_native_depth_* tests.

The failing path itself is not tested: no test makes the probe's allocation fail, so the double destroy is shown by reading the code, and these runs only show that the success path is unchanged. tools/check_conventions.py passes. Not run on Linux or macOS. No title tested.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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(TM) Graphics iGPU (1002:13c0), driver 24.30.36 (32.0.13036.4), Windows 11; NVIDIA GeForce RTX 5080 (10de:2c02), driver 616.56, Windows 11
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5452445058 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26178). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-23T11:45:47Z. --- ### What `ProbeStencilShadowCopy` (`Graphics/src/DepthSurface.cpp`) destroys a probe image twice when its memory allocation fails. The `make` lambda hands the image to an `OwnedImage` and then, if `Allocate` throws, destroys the image by hand and rethrows; unwinding then runs `OwnedImage::~OwnedImage()`, which calls `vkDestroyImage` on the same handle. The probe's outer `catch` only runs after that. The image also leaks if `make_shared` throws after `vkCreateImage` succeeded. - The `OwnedImage` is now created empty, takes the handle once `vkCreateImage` has succeeded, and is the only owner. The manual `vkDestroyImage` and its `try`/`catch` are gone. - The probe's result and its success path are unchanged. mihiii1337 found this shape in the resolve probe of #25911, which I had copied from here; #25911 fixes its own copy and this is the original. Of the other places in this file that hand an image to an `OwnedImage`, the depth upload image, the native depth samples, the sampled depth shadow and `DepthPlaneCopy` release it through one owner; `Texture.cpp` already creates the owner empty first, as this change does. I did not trace the failure paths of the depth surface's own image. `tools/pr_overlap.py --branch` confirms no overlap, and could not compare 20 open pull requests that conflict with `main` and change `DepthSurface.cpp` (#1368, #1395, #6808, #7256, #7642, #9508, #9895, #9950, #10193, #11444, #11551, #13863, #13920, #13928, #13929, #15621, #18185, #20315, #22062, #22647). I read each one's `DepthSurface.cpp` patch: none changes a line of `ProbeStencilShadowCopy`. ### Tested Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, `main` `318cc76b1e` and the change on it, same machine. `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (1.4.363), `VK_LOADER_DEBUG=layer`, GPU selected with `ANYPS5_GPU`, `ctest --test-dir build -R '^agc_.*(depth|stencil)' -V -j 2` (116 tests). | | Radeon iGPU, AMD 24.30.36 | RTX 5080, NVIDIA 616.56 | |---|---|---| | `main` | 116 pass (1 skipped); 0 `VUID-` | 116 pass (2 skipped); 0 `VUID-` | | change | 116 pass (1 skipped); 0 `VUID-` | 116 pass (2 skipped); 0 `VUID-` | The loader logs `Insert instance layer "VK_LAYER_KHRONOS_validation"` 158 times in each of the four logs and never `was not found but was requested by env var VK_INSTANCE_LAYERS`. A separate build with a temporary print at the top of the probe (not in this change) shows that 14 of the 116 tests run it, 187 times on each GPU, for `D32_SFLOAT_S8_UINT` at 2, 4 and 8 samples: `agc_driver_stencil_copy_draw`, `agc_driver_stencil_replay`, `_no_gpl` and `_unsuppressed`, and ten `agc_driver_native_depth_*` tests. The failing path itself is not tested: no test makes the probe's allocation fail, so the double destroy is shown by reading the code, and these runs only show that the success path is unchanged. `tools/check_conventions.py` passes. Not run on Linux or macOS. No title tested. ### Checklist - [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) and the licence notice of code adapted from upstream (see AGENTS.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(TM) Graphics iGPU (`1002:13c0`), driver 24.30.36 (32.0.13036.4), Windows 11; NVIDIA GeForce RTX 5080 (`10de:2c02`), driver 616.56, Windows 11 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Superseabas commented 2026-11-23 12:38:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T12:38:04Z.

Review: APPROVED


Windows check of head 2c96bd7c5b with GCC 16 on an NVIDIA and an AMD GPU, merged onto main 071799dc88 (clean merge), and a read of the ownership change.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. GPUs, each picked with ANYPS5_GPU: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (1002:164e, Adrenalin 26.10.2). This was built together with #26096, #26174 and #26179, which touch other files.

  • ^(agc_driver_stencil.*|agc_driver_native_depth.*)$ (the 14 tests that reach the probe, and the rest of those two groups): main 44 of 44, and with this PR 44 of 44 in 2 of 2 runs, on both GPUs. The skips are the same on both sides: agc_driver_native_depth_driver_queued_fill (Windows by design) on both GPUs, and agc_driver_stencil_ref_export on the RTX. No VUID- lines.

The code: the double destroy is real on main. ~OwnedImage (Texture.cpp:66) destroys image when it is set, and the catch destroyed it first. With the owner created empty, every exit destroys the image once:

  • vkCreateImage fails: the owner holds no image, and ReleaseImageMemory returns at once for null memory.
  • Allocate throws: the owner holds the image but no allocation.
  • vkBindImageMemory fails: the owner holds both.

Texture.cpp creates empty owners the same way (:379, :638, :653, :1017). As the description says, no test makes the allocation fail, so the runs only show the success path is unchanged.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6021821794 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26178#pullrequestreview-6021821794). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-23T12:38:04Z. **Review:** APPROVED --- Windows check of head `2c96bd7c5b` with GCC 16 on an NVIDIA and an AMD GPU, merged onto `main` `071799dc88` (clean merge), and a read of the ownership change. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release full build, run from PowerShell with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. GPUs, each picked with `ANYPS5_GPU`: NVIDIA GeForce RTX 5070 Ti (621.01) and the Ryzen 7 7700X's integrated AMD Radeon (`1002:164e`, Adrenalin 26.10.2). This was built together with #26096, #26174 and #26179, which touch other files. - `^(agc_driver_stencil.*|agc_driver_native_depth.*)$` (the 14 tests that reach the probe, and the rest of those two groups): `main` 44 of 44, and with this PR 44 of 44 in 2 of 2 runs, on both GPUs. The skips are the same on both sides: `agc_driver_native_depth_driver_queued_fill` (Windows by design) on both GPUs, and `agc_driver_stencil_ref_export` on the RTX. No `VUID-` lines. **The code:** the double destroy is real on `main`. `~OwnedImage` (`Texture.cpp:66`) destroys `image` when it is set, and the `catch` destroyed it first. With the owner created empty, every exit destroys the image once: - `vkCreateImage` fails: the owner holds no image, and `ReleaseImageMemory` returns at once for null memory. - `Allocate` throws: the owner holds the image but no allocation. - `vkBindImageMemory` fails: the owner holds both. `Texture.cpp` creates empty owners the same way (`:379`, `:638`, `:653`, `:1017`). As the description says, no test makes the allocation fail, so the runs only show the success path is unchanged. AI-assisted: yes (Claude Code)
mihiii1337 commented 2026-11-23 12:44:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T12:44:11Z.

Review: APPROVED


Reviewed and built exact head 2c96bd7c5b1462ca8ec134244abaa51f8234db6f.

The ownership change closes both failure windows: the owner exists before vkCreateImage, receives the handle through a non-throwing assignment immediately after successful creation, and is then the sole image destroyer through allocation and bind failures. A focused native lifetime A/B with the same ownership sequence produced two destroys on the old allocation-failure path and exactly one on the PR path; the PR success control also destroyed exactly once. The independently reported Vulkan fault injection confirms the same result through the real validation layer.

I also configured the full recursive checkout and built agc_driver_stencil_copy_draw_tests with GCC 15.2. The exact-head test passed with VK_LAYER_KHRONOS_validation, ANYPS5_REQUIRE_VULKAN=1, and shader caching disabled on both physical notebook adapters:

  • Intel RaptorLake-S Mobile Graphics (8086:a78b), driver 101.6127, Vulkan 1.3.295: 1/1 passed.
  • NVIDIA GeForce RTX 4070 Laptop (10de:2860), driver 621.01, Vulkan 1.4.357: 1/1 passed.

Both runs selected and reported the intended adapter, executed the actual Vulkan stencil-copy draw, and printed that both stencil values were written at the rasterized locations. No validation error appeared. This verifies the unchanged success path on these two GPUs; it is not a console/title test. Hosted Windows and project checks are green.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6021900284 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26178#pullrequestreview-6021900284). Originally posted by [mihiii1337](https://github.com/mihiii1337) on 2026-11-23T12:44:11Z. **Review:** APPROVED --- Reviewed and built exact head `2c96bd7c5b1462ca8ec134244abaa51f8234db6f`. The ownership change closes both failure windows: the owner exists before `vkCreateImage`, receives the handle through a non-throwing assignment immediately after successful creation, and is then the sole image destroyer through allocation and bind failures. A focused native lifetime A/B with the same ownership sequence produced two destroys on the old allocation-failure path and exactly one on the PR path; the PR success control also destroyed exactly once. The independently reported Vulkan fault injection confirms the same result through the real validation layer. I also configured the full recursive checkout and built `agc_driver_stencil_copy_draw_tests` with GCC 15.2. The exact-head test passed with `VK_LAYER_KHRONOS_validation`, `ANYPS5_REQUIRE_VULKAN=1`, and shader caching disabled on both physical notebook adapters: - Intel RaptorLake-S Mobile Graphics (`8086:a78b`), driver 101.6127, Vulkan 1.3.295: 1/1 passed. - NVIDIA GeForce RTX 4070 Laptop (`10de:2860`), driver 621.01, Vulkan 1.4.357: 1/1 passed. Both runs selected and reported the intended adapter, executed the actual Vulkan stencil-copy draw, and printed that both stencil values were written at the rasterized locations. No validation error appeared. This verifies the unchanged success path on these two GPUs; it is not a console/title test. Hosted Windows and project checks are green. AI-assisted: yes.
MarcZX10 commented 2026-11-25 08:26:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T08:26:30Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 2c96bd7c5b1462ca8ec134244abaa51f8234db6f. I did not run commands or tests. I read the changed lines in core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp and the OwnedImage definition and destructor in Texture.hpp and Texture.cpp. The manifest lists one changed file, included in the review bundle; no changed files were excluded.

Results

Check Result Note
Diff and ownership path passed Single-owner cleanup is consistent with OwnedImage destruction.
Tests not run The description reports Windows GPU test runs; I did not run them.
Allocation failure control not run No failure-injection test was run.

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not read the full 5,406-line changed file or run tests. The allocation-failure path itself was not exercised. The author reports success-path test runs on Windows with Radeon and NVIDIA GPUs; those results were not independently verified. No Linux or macOS run was reported.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6054108172 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26178#pullrequestreview-6054108172). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T08:26:30Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `2c96bd7c5b1462ca8ec134244abaa51f8234db6f`. I did not run commands or tests. I read the changed lines in `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp` and the `OwnedImage` definition and destructor in `Texture.hpp` and `Texture.cpp`. The manifest lists one changed file, included in the review bundle; no changed files were excluded. ## Results | Check | Result | Note | |---|---|---| | Diff and ownership path | passed | Single-owner cleanup is consistent with `OwnedImage` destruction. | | Tests | not run | The description reports Windows GPU test runs; I did not run them. | | Allocation failure control | not run | No failure-injection test was run. | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not read the full 5,406-line changed file or run tests. The allocation-failure path itself was not exercised. The author reports success-path test runs on Windows with Radeon and NVIDIA GPUs; those results were not independently verified. No Linux or macOS run was reported. ## To get this to a good state Nothing is required.
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!26178
No description provided.