Skip to content

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

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
XairRrR:fix/agc-stencil-plane-buffer-coherence
Oct 29, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
XairRrR:fix/agc-stencil-plane-buffer-coherence

Conversation

@XairRrR

@XairRrR XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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 3ca715e:

  • 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 Uncharted: Legacy of Thieves Collection (PPSA05684) on Linux / NVIDIA #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

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 28, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@XairRrR thanks, the stencil plane store and reload read correctly to me. One change before merge: the new debt text in docs/dev/debt/libSceAgcDriver.md names the game next to PPSA05684. Debt docs can carry the title ID but not game names, so please keep PPSA05684 and drop the name.

@XairRrR

XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Uncharted 4 prison yard fight, same spot, 640x360 frame dumps:

Before (without this change):

stencil-before

After (this PR):

stencil-after

@github-actions

github-actions Bot commented Oct 28, 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 2434 docs/dev/debt/libSceAgcDriver.md
PR 4448 docs/dev/debt/libSceAgcDriver.md
PR 4449 docs/dev/debt/libSceAgcDriver.md
PR 5372 docs/dev/debt/libSceAgcDriver.md
PR 5925 docs/dev/debt/libSceAgcDriver.md
PR 6185 docs/dev/debt/libSceAgcDriver.md
PR 6186 docs/dev/debt/libSceAgcDriver.md
PR 6194 docs/dev/debt/libSceAgcDriver.md
PR 6223 docs/dev/debt/libSceAgcDriver.md
PR 6280 docs/dev/debt/libSceAgcDriver.md
PR 6300 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6412 docs/dev/debt/libSceAgcDriver.md
PR 6481 docs/dev/debt/libSceAgcDriver.md
PR 6548 docs/dev/debt/libSceAgcDriver.md
PR 6549 docs/dev/debt/libSceAgcDriver.md
PR 6804 docs/dev/debt/libSceAgcDriver.md
PR 6808 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6977 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7048 docs/dev/debt/libSceAgcDriver.md
PR 7256 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7449 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7604 docs/dev/debt/libSceAgcDriver.md
PR 7642 docs/dev/debt/libSceAgcDriver.md
PR 7978 docs/dev/debt/libSceAgcDriver.md
PR 8014 docs/dev/debt/libSceAgcDriver.md
PR 8319 docs/dev/debt/libSceAgcDriver.md
PR 8542 docs/dev/debt/libSceAgcDriver.md
PR 8592 docs/dev/debt/libSceAgcDriver.md
PR 9356 docs/dev/debt/libSceAgcDriver.md
PR 9387 docs/dev/debt/libSceAgcDriver.md
PR 9432 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9508 docs/dev/debt/libSceAgcDriver.md
PR 9592 docs/dev/debt/libSceAgcDriver.md
PR 9748 docs/dev/debt/libSceAgcDriver.md
PR 9891 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9893 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9895 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9898 docs/dev/debt/libSceAgcDriver.md
PR 9915 docs/dev/debt/libSceAgcDriver.md
PR 10026 docs/dev/debt/libSceAgcDriver.md
PR 10193 docs/dev/debt/libSceAgcDriver.md
PR 10233 docs/dev/debt/libSceAgcDriver.md
PR 10249 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
PR 10472 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 11551 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
docs/dev/debt/libSceAgcDriver.md
PR 11557 docs/dev/debt/libSceAgcDriver.md
PR 11825 docs/dev/debt/libSceAgcDriver.md

@XairRrR

XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. Dropped the game name in 366ddec: the debt text names PPSA05684 only. The stencil part had also taken the depth-plane entry past the 1000-character limit, so it is now an entry of its own, "Stencil planes in guest memory", right after it.

AI-assisted: yes

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

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.

@maskym

maskym commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Windows + NVIDIA result for head 366ddec6cd, merged into main 3b979d42a9 (the PR's GPUs tested line names no NVIDIA run on Windows).

This PR's own tests: the new agc_driver_stencil_plane_memory (0.77 s), all passed.

Host: Windows 11 26300, Intel i7-12700K, NVIDIA GeForce RTX 2070 SUPER (vendor 0x10de, device 0x1e84; Vulkan loader 1.4.357 from the NVIDIA driver 621.01), Khronos Validation Layers nightly 1.4.366 (b2b44c0d37) through VK_ADD_LAYER_PATH, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

Same focused run on main 3b979d42a9 and on this tree, same machine and GPU: VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, VK_LOADER_DEBUG=error,warn,layer, ctest --test-dir build -R '^(agc|guest_video_out|video_out)' --parallel 4 -V.

The tree also carried the open PRs #12298, #12272, #12262, #12256, #12250, #12234, #12228, #12211, #12209, #12208, #12207, all merged cleanly, so the run is a joint one. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Only the depth plane of a depth surface reached guest memory: stencil
lived in the host image alone. 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. Uncharted 4 clears its G-buffer
stencil plane with a fill, then copies it into the lighting depth
target and on into last frame's planes with a buffer copy kernel every
frame; its lighting kept the stencil marks of earlier frames, so
characters left trails and the image doubled while the camera moved.

The stencil plane of a single-sample surface with the 64 KiB Z swizzle
is now marked drawn when the surface is bound, stored whole when a read
overlaps it (FlushDepthPlanes, like the depth plane) and reloaded from
guest memory when a collected guest write changed it before the surface
is bound or its stencil sampled, unless a stencil clear is pending.

agc_driver_stencil_plane_memory draws stencil, reads the plane through
FlushDepthPlanes, then writes it from the CPU and tests it with stencil
EQUAL; it fails without the change.

AI-assisted: yes
The stencil plane text named the game; debt entries carry title IDs
only. It also took the depth plane entry past the 1000-character limit,
so it now has an entry of its own.

AI-assisted: yes
… another write

A surface retires when a write overlaps either of its planes. The stencil
plane's pending GPU results were dropped with it even when the write only
covered the depth plane. Retiring for a color target, a storage write or a
guest write now stores a stale stencil plane first unless that write
replaces the stencil plane itself.

AI-assisted: yes
@XairRrR
XairRrR force-pushed the fix/agc-stencil-plane-buffer-coherence branch from 366ddec to 1dabe79 Compare October 28, 2026 19:01
@XairRrR

XairRrR commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both of you, the retire path was a real hole. Fixed in 1dabe79: RetireDepthSurfaces, RetireCoveredDepthSurfaces and retireStoringStale now store a stale stencil plane before they retire the surface, unless the write replaces the stencil plane itself: a range covering the whole stencil plane, a storage write covering it (PlaneCoveredBy), or a guest write over it since it was last noted. The store goes through the same StoreStencilPlane + FlushPending("stencil plane store") as FlushDepthPlanes, under the GPU lock and the surfaces lock in that order.

agc_driver_stencil_plane_memory gains the regression: draw stencil 0x2a, RetireDepthSurfaces over the depth plane only, then expect 0x2a in the guest stencil plane and nothing left to store. With DepthSurface.cpp from the previous head it fails: retiring the surface for a write over its depth plane only dropped the stencil 0x2a a draw wrote.

Rebased on main e4da19a (head 1dabe79), no conflicts. The depth-plane and stencil tests (agc_driver_stencil_plane_memory, agc_driver_depth_plane_*, agc_driver_depth_surface_last_writer) pass on the rebased head.

Rebased on main e4da19a. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, RTX 4070 Ti SUPER (NVIDIA 615.78.08), Khronos validation layers 1.4.363. VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer ctest -R '^(agc|guest_video_out|video_out|guest_process_time)' on main plus #12206, #12207, #12208, #12210 and #12238 rebased: 1145/1145 pass. The loader inserted the layer 1000 times, no log line says it was not found, and the only VUID is VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (18 times), the one maskym's run reports on main (#6782). I did not run main alone on Linux; maskym's paired Windows run above covers that side.

AI-assisted: yes

@adriadam10
adriadam10 merged commit 1578380 into boykopovar:main Oct 29, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the new head against the previous points: stale stencil planes are now stored before RetireDepthSurfaces, RetireCoveredDepthSurfaces and retireStoringStale retire the surface, unless the write covers the stencil plane itself; the lock order matches FlushDepthPlanes. The new case in StencilPlaneMemory.cpp (draw stencil 0x2a, retire through a depth-plane write, expect 0x2a in guest memory) fails without the change; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants