fix(agc): keep stencil planes coherent with guest memory #12210
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12210"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 bySyncFromMemory). 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_xcopy kernel: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
FlushDepthPlanesalready stores for depth) now follows the depth plane's rules, as a whole plane:FlushDepthPlanes: buffer uploads,GuestBuffersV#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;SyncFromMemorydoes 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
main3ca715ee15:agc_driver_stencil_plane_memory: draws stencil 0x18 over a D32S8 surface, reads the plane throughFlushDepthPlanesand 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). WithDepthSurface.cppfrommainit fails:a read of the stencil plane after a draw wrote stencil stored nothing.ctest -j8(this branch together with my five other branches of today, on the samemain): 2619 of 2620 pass;agc_driver_mesh_barycentric_emulationfails the same way onmainon this GPU (the pixel shader did not emulate barycentrics).tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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()callsretireStencil(), which clearsstencilStaleand removes its range without storing the pending plane.RetireDepthSurfacesretires 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 throughretireStoringStalewhen 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 currentmain; 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.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) clearsstencilStaleand 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 toretire). Store the stale stencil plane before retiring unless the overlapping write covers the stencil range itself.main; after the fix, rebase and rerunagc_driver_stencil_plane_memoryand the depth-plane tests with validation on.