Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Plyxen:fix/agc-stencil-probe-image-owner
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Plyxen:fix/agc-stencil-probe-image-owner

Conversation

@Plyxen

@Plyxen Plyxen commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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

…tion fails

ProbeStencilShadowCopy handed the image to an OwnedImage and then destroyed
it by hand when Allocate threw, so unwinding destroyed the same handle a
second time. The OwnedImage is now created empty, takes the handle after
vkCreateImage succeeds and is the only owner, which also closes the leak if
make_shared throws after the image exists.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Helldiverr

Copy link
Copy Markdown
Contributor

Looks good on AMD RDNA4 with the Khronos validation layer. The depth and stencil tests I ran pass 3/3 on the head, with 0 VUID- and 0 synchronization hazards. I also forced the probe's allocation to fail. On main the layer then reports the double vkDestroyImage this PR describes. On the head the same failure is clean.

What this run adds.

  • Earlier runs:
    • the author's own runs, with the layer, on a Radeon iGPU (1002:13c0, 24.30.36) and an RTX 5080: 116 depth/stencil tests;
    • no other runs are posted.
  • The author writes that the failing path itself is not tested.
  • This run adds:
    • a discrete RDNA4 run of this head (Windows, driver 32.0.31044.16), with the layer and with synchronization validation;
    • a fault injection that reaches the failing path on main and on the head.

Code.

  • DepthSurface.cpp:797-803, in ProbeStencilShadowCopy's make lambda:
    • the OwnedImage is now created empty;
    • it takes the handle (owned->image = image;) only after vkCreateImage succeeds;
    • the manual vkDestroyImage and its try/catch are removed.
  • ~OwnedImage (Texture.cpp:66-67) is now the only place that destroys the image, so an Allocate failure destroys it once. This matches how Texture.cpp already builds its owner.
  • Conventions.
    • No comments are added.
    • No debt fragment is needed.
    • tools/check_conventions.py --base ddf76fcbe8 and tools/debt_fold.py --check pass on the merge.

Negative control (fault injection).

  • The PR adds no test, and no existing test makes the allocation fail. To reach that path, I added a temporary throw just before owned->Allocate(...):
    • on main, inside the existing try;
    • on the head, right after owned->image = image;.
  • The throw is gated on an environment variable and prints [inject]. It is not part of this PR.
  • [inject] printed once in each of agc_driver_stencil_replay_no_gpl and agc_driver_stencil_replay_unsuppressed. It did not print in the three native_depth tests of this pattern.
  • main with the injection:
    • All 5 tests still pass, because the probe's outer catch absorbs the error.
    • The layer reports VUID-vkDestroyImage-image-parameter (vkDestroyImage(): image Invalid VkImage Object) once in each of the two tests.
    • Each test also has two UNASSIGNED-Threading-Info messages (Couldn't find VkImage Object).
    • That is 6 Validation Error messages in total: the second destroy of an image that was already destroyed.
  • Head with the injection: all 5 tests pass, with 0 Validation Error, 0 VUID- and 0 Threading-Info.

GPU. Head 2c96bd7c5b, merged cleanly onto main ddf76fcbe8 as 2058740f04:

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R "^agc_driver_(stencil_replay_no_gpl|stencil_replay_unsuppressed|native_depth_stencil|native_depth_separate_stencil|native_depth_aliased_stencil)$" -V
run stencil_replay_no_gpl stencil_replay_unsuppressed native_depth_stencil native_depth_separate_stencil native_depth_aliased_stencil Validation Error
main Passed Passed Passed Passed Passed 0
main, allocation failure injected Passed Passed Passed Passed Passed 6 (2 VUID-vkDestroyImage-image-parameter)
head 1 Passed Passed Passed Passed Passed 0
head 2 Passed Passed Passed Passed Passed 0
head 3 Passed Passed Passed Passed Passed 0
head, allocation failure injected Passed Passed Passed Passed Passed 0
main, sync validation Passed Passed Passed Passed Passed 0
head, sync validation Passed Passed Passed Passed Passed 0

Validation.

  • Insert instance layer "VK_LAYER_KHRONOS_validation" appears 8 times in every log.
  • No log contains was not found but was requested by env var VK_INSTANCE_LAYERS.
  • VUID- lines are 0 in every log except main with the injection, as described above.
  • With VK_KHRONOS_VALIDATION_VALIDATE_SYNC=true added, both main and the head have 0 SYNC-HAZARD- lines and 0 VUID- lines.

Host:

  • GPU: AMD Radeon AI PRO R9700 (gfx1201, 1002:7551), driver 32.0.31044.16, Vulkan 1.4.349
  • Validation layer: Khronos validation layer 1.4.363 (LunarG SDK)
  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: main ddf76fcbe8, head 2c96bd7c5b, merged as 2058740f04

AI-assisted: yes

@github-actions github-actions Bot added the agc libSceAgc* label Nov 23, 2026

@Superseabas Superseabas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 mihiii1337 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Some 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.

Pull request Shared files not compared
PR 1368 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 1395 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 6808 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 7256 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 9895 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 9950 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 10469 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 10492 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 10497 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 11444 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 11551 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 13863 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 13920 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 15621 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 16880 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 18185 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 22062 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 22531 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 22647 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 24083 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 24327 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 25534 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 25557 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 25610 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 26819 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 27698 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean
oneandonlydean merged commit fa107dd into boykopovar:main Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants