fix(agc): take a storage image's writes to the stencil plane when a depth target binds #12617

Merged
milescoviello merged 6 commits from upstream-pr/12617 into main 2026-11-04 05:34:35 +00:00
milescoviello commented 2026-10-28 23:02:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-28T23:02:49Z.


What

A title can write a depth target's stencil through an 8-bit storage image over the target's stencil plane: a compute or pixel shader stores R8_UINT texels at stencilAddress, and later draws stencil-test against that depth target. DepthSurface takes a pending or live storage image's writes into its depth plane when it is created (TakeStorage), but not into its stencil plane. So the stencil test read whatever the stencil aspect held before: the clear, or an earlier frame's stencil.

Ghost of Tsushima Director's Cut (PPSA02225 02.024.000) does this in its opening. I counted it with a local diagnostic: in a 910 s run, a one-sample D32S8 depth surface bound over a pending R8_UINT storage image of its whole stencil plane 400 times (265 at 3840x2160 and 133 at 1920x2160 planes, plus one other).

This is a different path from #12210, which keeps a stencil plane coherent with guest memory written by buffer copies. Here the writes are a storage image's and are still on the GPU. The two merge cleanly together.

How

DepthSurface::TakeStencilStorage runs when a one-sample depth target with a stencil plane binds. It finds a pending (or live) storage image at stencilAddress of the plane's size, with the plane's extent and an R8_UINT or R8_UNORM format, and copies it into the stencil aspect with the existing copyPlane. It does this only when that image's content version is newer than the one it last took, so a draw with no new storage write between binds doesn't copy again. Multisampled targets and other storage shapes are left as before.

Tested

The lines below name the head they ran on, or were run before the 11-02 rebase onto main d2ce337078. For the rebased head e056b23d28, the area tests and the paired validation-layer comparison are in this comment.

Head f6355144d9: rebased onto main 673fa865f4 after #10854 merged. Both PRs add cases to DepthSurfaceFromStorage.cpp. The block is now 15 planes; #10854's cases keep planes 8–10 and this PR's move from 9–12 to 11–14. StoreRow keeps main's level argument, through this PR's shared Store helper.

  • The 50 tests matching ^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage pass 3 of 3.
  • Validation layer, same command as below, on main 673fa865f4 and this head: both pass 2 of 2, with no missing-layer message, the 1 device checked on each side and no VUID- messages.

Earlier, at 123b2741d9 (after oneandonlydean's review: only pending storage images are taken, and the scan waits for a write to an 8-bit storage image):

  • The 49 tests matching ^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage pass 3 of 3.
  • Validation layer, same command as below, on main 319621ba2e and this head: both pass 2 of 2, with no missing-layer message, the 1 device checked on each side and no VUID- messages.

Earlier, at bd1c1ffd90:

Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti (NVIDIA 615.71.09), on main 319621ba2e:

  • New cases in agc_driver_depth_surface_from_storage. An R8_UINT storage write of 1 over a D32S8 target's stencil plane, then a draw with stencil EQUAL 1:
    • a new surface created over the pending write: all 32 pixels pass;
    • a surface drawn before the write and drawn again after it: all 32 pass, and the draw before the write still passes EQUAL 0 on the cleared plane;
    • a third draw with no storage write in between: all 32 still pass.
    • The test alone on main (dde951ef46) fails the first case: ... tested EQUAL to the stored 1: 0 pixels passed, expected 32. At this head (bd1c1ffd90) the cases pass.
  • Area: ^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil (40 tests) pass 3 of 3.
  • Validation layer (Khronos VK_LAYER_KHRONOS_validation 1.4.363, LunarG SDK), on main 319621ba2e and at this head:
    VK_LAYER_PATH=<sdk>/layers VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_driver_depth_surface_from_storage$|^agc_driver_depth_surface' -V
    • Both pass 2 of 2, and neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS.
    • The layer checked the device of agc_driver_depth_surface_from_storage on each side (agc_driver_depth_surface_last_writer creates none), with no VUID- messages.
  • Ghost of Tsushima (PPSA02225 02.024.000), a local stack with #12561. I compared the same 480 s opening with and without this change, with frames dumped every 100 presents.
    • Of the 11 dumped frames, 7 match pixel for pixel.
    • The 4 that differ show the logo animation or a menu at a different moment, not different stencil-tested content.
    • So the opening shows no visible change. I haven't found a scene where this stencil decides what's drawn.

Windows 11 VM (GeForce GT 1030, WinLibs GCC 15.2, Release): agc_driver_depth_surface_from_storage and agc_driver_depth_surface_last_writer pass 3 of 3 at this head and on main 319621ba2e.

AMD RDNA 2 on RADV, by @crastinator (comment): RX 6700 XT, Mesa 26.2.5, head e056b23d28 merged onto main 98346b748c. tools/vulkan_validation_gate.py over ^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage (57 tests, 3 iterations per side) passes on both sides with no VUID- messages, and main with only this PR's test fails the new case 3 of 3.

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: NVIDIA GeForce RTX 4070 Ti (Linux)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5059074619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12617). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-28T23:02:49Z. --- ### What A title can write a depth target's stencil through an 8-bit storage image over the target's stencil plane: a compute or pixel shader stores `R8_UINT` texels at `stencilAddress`, and later draws stencil-test against that depth target. `DepthSurface` takes a pending or live storage image's writes into its depth plane when it is created (`TakeStorage`), but not into its stencil plane. So the stencil test read whatever the stencil aspect held before: the clear, or an earlier frame's stencil. Ghost of Tsushima Director's Cut (PPSA02225 02.024.000) does this in its opening. I counted it with a local diagnostic: in a 910 s run, a one-sample D32S8 depth surface bound over a pending `R8_UINT` storage image of its whole stencil plane 400 times (265 at 3840x2160 and 133 at 1920x2160 planes, plus one other). This is a different path from #12210, which keeps a stencil plane coherent with guest memory written by buffer copies. Here the writes are a storage image's and are still on the GPU. The two merge cleanly together. ### How `DepthSurface::TakeStencilStorage` runs when a one-sample depth target with a stencil plane binds. It finds a pending (or live) storage image at `stencilAddress` of the plane's size, with the plane's extent and an `R8_UINT` or `R8_UNORM` format, and copies it into the stencil aspect with the existing `copyPlane`. It does this only when that image's content version is newer than the one it last took, so a draw with no new storage write between binds doesn't copy again. Multisampled targets and other storage shapes are left as before. ### Tested The lines below name the head they ran on, or were run before the 11-02 rebase onto `main` `d2ce337078`. For the rebased head `e056b23d28`, the area tests and the paired validation-layer comparison are in [this comment](https://github.com/boykopovar/AnyPS5/pull/12617#issuecomment-6575131376). **Head `f6355144d9`**: rebased onto `main` `673fa865f4` after #10854 merged. Both PRs add cases to `DepthSurfaceFromStorage.cpp`. The block is now 15 planes; #10854's cases keep planes 8–10 and this PR's move from 9–12 to 11–14. `StoreRow` keeps `main`'s `level` argument, through this PR's shared `Store` helper. - The 50 tests matching `^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage` pass 3 of 3. - Validation layer, same command as below, on `main` `673fa865f4` and this head: both pass 2 of 2, with no missing-layer message, the 1 device checked on each side and no `VUID-` messages. **Earlier, at `123b2741d9`** (after oneandonlydean's review: only pending storage images are taken, and the scan waits for a write to an 8-bit storage image): - The 49 tests matching `^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage` pass 3 of 3. - Validation layer, same command as below, on `main` `319621ba2e` and this head: both pass 2 of 2, with no missing-layer message, the 1 device checked on each side and no `VUID-` messages. **Earlier, at `bd1c1ffd90`:** Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti (NVIDIA 615.71.09), on `main` `319621ba2e`: - **New cases in `agc_driver_depth_surface_from_storage`.** An `R8_UINT` storage write of 1 over a D32S8 target's stencil plane, then a draw with stencil `EQUAL 1`: - a new surface created over the pending write: all 32 pixels pass; - a surface drawn before the write and drawn again after it: all 32 pass, and the draw before the write still passes `EQUAL 0` on the cleared plane; - a third draw with no storage write in between: all 32 still pass. - The test alone on `main` (`dde951ef46`) fails the first case: `... tested EQUAL to the stored 1: 0 pixels passed, expected 32`. At this head (`bd1c1ffd90`) the cases pass. - **Area:** `^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil` (40 tests) pass 3 of 3. - **Validation layer** (Khronos `VK_LAYER_KHRONOS_validation` 1.4.363, LunarG SDK), on `main` `319621ba2e` and at this head: `VK_LAYER_PATH=<sdk>/layers VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_driver_depth_surface_from_storage$|^agc_driver_depth_surface' -V` - Both pass 2 of 2, and neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. - The layer checked the device of `agc_driver_depth_surface_from_storage` on each side (`agc_driver_depth_surface_last_writer` creates none), with no `VUID-` messages. - **Ghost of Tsushima (PPSA02225 02.024.000)**, a local stack with #12561. I compared the same 480 s opening with and without this change, with frames dumped every 100 presents. - Of the 11 dumped frames, 7 match pixel for pixel. - The 4 that differ show the logo animation or a menu at a different moment, not different stencil-tested content. - So the opening shows no visible change. I haven't found a scene where this stencil decides what's drawn. Windows 11 VM (GeForce GT 1030, WinLibs GCC 15.2, Release): `agc_driver_depth_surface_from_storage` and `agc_driver_depth_surface_last_writer` pass 3 of 3 at this head and on `main` `319621ba2e`. AMD RDNA 2 on RADV, by @crastinator ([comment](https://github.com/boykopovar/AnyPS5/pull/12617#issuecomment-6577431762)): RX 6700 XT, Mesa 26.2.5, head `e056b23d28` merged onto `main` `98346b748c`. `tools/vulkan_validation_gate.py` over `^agc_driver_depth|^agc_driver_native_depth|^agc_driver_stencil|^agc_driver_storage` (57 tests, 3 iterations per side) passes on both sides with no `VUID-` messages, and `main` with only this PR's test fails the new case 3 of 3. ### 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: NVIDIA GeForce RTX 4070 Ti (Linux) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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!12617
No description provided.