fix(agc): keep stencil planes coherent with guest memory #12210

Merged
XairRrR merged 3 commits from upstream-pr/12210 into main 2026-10-29 19:35:20 +00:00
XairRrR commented 2026-10-28 03:43:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-28T03:43:16Z.


What

Only the depth plane of a depth surface reaches guest memory (stale units stored by FlushDepthPlanes, guest writes reloaded by SyncFromMemory). The stencil plane lived in the host image alone, so a title that moves stencil with buffer copies read stale bytes from the source plane, and the destination surface never saw what the copy wrote.

PPSA05684 (Uncharted: Legacy of Thieves Collection) does exactly this every frame in Uncharted 4's prison and orphanage chapters, through a buffer_load_format_x / buffer_store_format_x copy kernel:

  1. a buffer fill clears the G-buffer stencil plane,
  2. the G-buffer draws write stencil (characters and other dynamic objects),
  3. the depth plane and the stencil plane are copied into the lighting depth target,
  4. that target's planes are copied into the two planes kept for the next frame.

The lighting and TAA passes sample those stencil planes. Since step 3 never reached the lighting target, its stencil kept the marks of earlier frames: characters left a trail of silhouettes, fog showed their old outlines, and the whole image smeared and doubled while the camera moved.

The stencil plane of a single-sample surface with the 64 KiB Z swizzle (the planes FlushDepthPlanes already stores for depth) now follows the depth plane's rules, as a whole plane:

  • binding the surface for a draw marks the plane drawn and registers its range with the stale planes;
  • a read that overlaps the range (FlushDepthPlanes: buffer uploads, GuestBuffers V#s, image lookups, pending-store flushes) stores it through an R8_UINT mirror and notes the write generation, so the store does not count as a guest write;
  • a collected guest write over the range reloads the plane when the surface is next bound or its stencil is sampled, unless a stencil clear is pending (as SyncFromMemory does for depth).

The stencil plane is taken to use the depth plane's swizzle at one byte per texel; the debt entry for depth planes in guest memory says so.

Tested

Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, on main 3ca715ee15:

  • New agc_driver_stencil_plane_memory: draws stencil 0x18 over a D32S8 surface, reads the plane through FlushDepthPlanes and expects 0x18 in guest memory, then writes 0x05 from the CPU and draws with stencil EQUAL 0x05 (every pixel passes) and EQUAL 0x18 (none passes). With DepthSurface.cpp from main it fails: a read of the stencil plane after a draw wrote stencil stored nothing.
  • Full ctest -j8 (this branch together with my five other branches of today, on the same main): 2619 of 2620 pass; agc_driver_mesh_barycentric_emulation fails the same way on main on this GPU (the pixel shader did not emulate barycentrics).
  • tools/check_conventions.py --base origin/main passes.
  • PPSA05684, Uncharted 4 prison yard fight (dev menu task): before, the frame is a brown smear with repeated fading copies of every character; after, the frame is sharp and stable. The frame trace shows the four stencil planes stored and reloaded every frame.

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: PPSA05684 #4001 (trails and image doubling in Uncharted 4's prison and orphanage)
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER, driver 615.78.08, Arch Linux 7.2.11-zen
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5044856113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12210). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-28T03:43:16Z. --- ### What Only the depth plane of a depth surface reaches guest memory (stale units stored by `FlushDepthPlanes`, guest writes reloaded by `SyncFromMemory`). The stencil plane lived in the host image alone, so a title that moves stencil with buffer copies read stale bytes from the source plane, and the destination surface never saw what the copy wrote. PPSA05684 (Uncharted: Legacy of Thieves Collection) does exactly this every frame in Uncharted 4's prison and orphanage chapters, through a `buffer_load_format_x` / `buffer_store_format_x` copy kernel: 1. a buffer fill clears the G-buffer stencil plane, 2. the G-buffer draws write stencil (characters and other dynamic objects), 3. the depth plane and the stencil plane are copied into the lighting depth target, 4. that target's planes are copied into the two planes kept for the next frame. The lighting and TAA passes sample those stencil planes. Since step 3 never reached the lighting target, its stencil kept the marks of earlier frames: characters left a trail of silhouettes, fog showed their old outlines, and the whole image smeared and doubled while the camera moved. The stencil plane of a single-sample surface with the 64 KiB Z swizzle (the planes `FlushDepthPlanes` already stores for depth) now follows the depth plane's rules, as a whole plane: - binding the surface for a draw marks the plane drawn and registers its range with the stale planes; - a read that overlaps the range (`FlushDepthPlanes`: buffer uploads, `GuestBuffers` V#s, image lookups, pending-store flushes) stores it through an R8_UINT mirror and notes the write generation, so the store does not count as a guest write; - a collected guest write over the range reloads the plane when the surface is next bound or its stencil is sampled, unless a stencil clear is pending (as `SyncFromMemory` does for depth). The stencil plane is taken to use the depth plane's swizzle at one byte per texel; the debt entry for depth planes in guest memory says so. ### Tested Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, on `main` 3ca715ee15: - New `agc_driver_stencil_plane_memory`: draws stencil 0x18 over a D32S8 surface, reads the plane through `FlushDepthPlanes` and expects 0x18 in guest memory, then writes 0x05 from the CPU and draws with stencil EQUAL 0x05 (every pixel passes) and EQUAL 0x18 (none passes). With `DepthSurface.cpp` from `main` it fails: `a read of the stencil plane after a draw wrote stencil stored nothing`. - Full `ctest -j8` (this branch together with my five other branches of today, on the same `main`): 2619 of 2620 pass; `agc_driver_mesh_barycentric_emulation` fails the same way on `main` on this GPU (`the pixel shader did not emulate barycentrics`). - `tools/check_conventions.py --base origin/main` passes. - PPSA05684, Uncharted 4 prison yard fight (dev menu task): before, the frame is a brown smear with repeated fading copies of every character; after, the frame is sharp and stable. The frame trace shows the four stencil planes stored and reloaded every frame. ### 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: PPSA05684 #4001 (trails and image doubling in Uncharted 4's prison and orphanage) - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER, driver 615.78.08, Arch Linux 7.2.11-zen - [x] AI-assisted: yes
MarcZX10 commented 2026-10-28 04:23:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:23:13Z.

Review: CHANGES_REQUESTED


I found a data-loss path in the new stencil tracking. retire() calls retireStencil(), which clears stencilStale and removes its range without storing the pending plane. RetireDepthSurfaces retires a surface when either its depth or stencil address overlaps; a write that overlaps only the depth plane therefore retires the surface and drops pending stencil results even though the write did not replace the stencil bytes. The same drop can occur through retireStoringStale when the depth side is clean. Please preserve/flush the stale stencil plane when retiring for an unrelated depth-range write, and add a regression that writes the depth plane after drawing stencil but before reading the stencil plane. This branch is also 60 commits behind current main; please rebase and rerun the focused stencil/depth-plane tests and validation checks on the rebased head. I did not run the GPU test locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677764758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12210#pullrequestreview-5677764758). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T04:23:13Z. **Review:** CHANGES_REQUESTED --- I found a data-loss path in the new stencil tracking. `retire()` calls `retireStencil()`, which clears `stencilStale` and removes its range without storing the pending plane. `RetireDepthSurfaces` retires a surface when *either* its depth or stencil address overlaps; a write that overlaps only the depth plane therefore retires the surface and drops pending stencil results even though the write did not replace the stencil bytes. The same drop can occur through `retireStoringStale` when the depth side is clean. Please preserve/flush the stale stencil plane when retiring for an unrelated depth-range write, and add a regression that writes the depth plane after drawing stencil but before reading the stencil plane. This branch is also 60 commits behind current `main`; please rebase and rerun the focused stencil/depth-plane tests and validation checks on the rebased head. I did not run the GPU test locally.
adriadam10 commented 2026-10-28 11:16:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T11:16:02Z.

Review: CHANGES_REQUESTED


Thanks for the change; the stencil store/reload reads correctly and the new test pins the draw → read → CPU write → draw sequence. One bug remains (same as MarcZX10's review):

  • DepthSurface.cpp:1984 (retire → retireStencil) clears stencilStale and removes the range without storing the pending stencil plane. RetireDepthSurfaces (:2835) retires a surface when either its depth or its stencil range overlaps, so a write that touches only the depth plane drops stencil results that were never stored. retireStoringStale (:1995) does the same when the depth side is clean (!AnyStale() goes straight to retire). Store the stale stencil plane before retiring unless the overlapping write covers the stencil range itself.
  • Add a regression: draw stencil, write the depth plane from the CPU, then read the stencil plane and expect the drawn value in guest memory.
  • The branch is well behind main; after the fix, rebase and rerun agc_driver_stencil_plane_memory and the depth-plane tests with validation on.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681739312 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12210#pullrequestreview-5681739312). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T11:16:02Z. **Review:** CHANGES_REQUESTED --- Thanks for the change; the stencil store/reload reads correctly and the new test pins the draw → read → CPU write → draw sequence. One bug remains (same as MarcZX10's review): - `DepthSurface.cpp:1984` (`retire` → `retireStencil`) clears `stencilStale` and removes the range without storing the pending stencil plane. `RetireDepthSurfaces` (`:2835`) retires a surface when either its depth or its stencil range overlaps, so a write that touches only the depth plane drops stencil results that were never stored. `retireStoringStale` (`:1995`) does the same when the depth side is clean (`!AnyStale()` goes straight to `retire`). Store the stale stencil plane before retiring unless the overlapping write covers the stencil range itself. - Add a regression: draw stencil, write the depth plane from the CPU, then read the stencil plane and expect the drawn value in guest memory. - The branch is well behind `main`; after the fix, rebase and rerun `agc_driver_stencil_plane_memory` and the depth-plane tests with validation on.
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!12210
No description provided.