fix(agc): keep depth planes coherent with buffer reads and writes of their memory #7154
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7154"
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-17T14:14:12Z.
What
On
maina depth surface lives only in its Vulkan image. Two buffer accesses to its depth plane go wrong:Uncharted: Legacy of Thieves Collection (PPSA05684, #4001) hits both in its main menu. A buffer compute (61440x1x1,
buffer(ro)->buffer(rw), 0xf00000 bytes) copies the main depth plane as raw memory into two ping-pong planes. The title then draws the cage's depth-only meshes into those planes, downsamples them to linear depth and fogs the scene from the result. Onmainthe copy carries nothing (89 % of the half-res depth reads 1.0), so the fog covers the cliff and the whole skeleton cage.Changes
A single-sample
D32_SFLOAT/D32_SFLOAT_S8_UINTsurface with the 64 KiB Z swizzle (DB_Z_INFO.SW_MODE24,SW_64KB_Z_X) keeps a storage image in that swizzle over its depth plane (mirrorPlane, guest format 32_FLOAT).DepthSurfaceViewnow takesdepthWritten, and both bind sites inDraw.cpppassstate.depthWrite. A bind that writes depth, or applies a recognized HTILE fill clear, marks the plane stale.New function
FlushDepthPlanes(address, bytes). It copies each stale plane that overlaps the range into its storage image, then letsStorageTexture::FlushPendingstore the image into guest memory. It is called from:StorageTexture::FlushPendingGuestBufferMemory::UploadFinish, over the regions the build binds through its descriptors (not over the whole heaps an address-based shader maps, as for pending storage images)VulkanDevice::dispatch, for each V# of itsGuestBuffersbindings. This is needed because a cached dispatch reads its buffers without an upload.fastTexture,fastRevalidate,fastRevalidateEach): a stale plane's memory has not been written yet, so it looks unchanged to themStale planes are kept as a list of ranges under their own lock, with an atomic hull. A call whose range overlaps no stale plane returns without taking
GpuMutexor the surfaces lock.Every bind of such a surface, and every flush of its stale plane, collects the plane's writes (
GuestMemory::CollectWrites). If someone else wrote the plane since the surface's previous bind, store or reload (WrittenSinceagainst the generation taken then), the surface reloads its depth from the storage image instead of storing. So a guest write made after a depth draw is not overwritten by that draw's late store, also before the first store. A guest write is ordered by the bind or flush that collects it: it replaces the depth drawn before it, and depth drawn from a later bind is stored over it.A pending HTILE fast clear wins over the reload: a tile HTILE marks cleared reads the clear value whatever the plane's bytes hold.
A color target flushes the stale planes under it before it retires their surfaces (
Draw.cpp, as the storage-image path already did), so a target that loads or blends reads the stored depth.retire()clears the surface'smemoryStaleand drops its range from the stale list, so a later flush never stores a retired plane's old depth over what the new owner of the memory wrote.A bind of a mirrorable target drops the plane images, and the pending store, of older surfaces whose planes overlap its plane over another range (another extent or base, as when a title changes its output resolution over the same memory). Those images stayed live and lent their old results over the new plane, also to buffer copies served as image copies. A same-range surface (another stencil or HTILE address) keeps its image. The scan runs only when a surface or plane image was created since the surface's last scan.
The surfaces mutex is now a
std::recursive_mutex, because a store reached from a flush underDepthSurfaceTexturere-enters it.copyPlanecan run in its ownCommandBatchwhen no recorder is active.Debt: a new entry, "Depth planes in guest memory", lists what stays incoherent: D16 planes, other swizzles, stencil, MSAA, CPU reads, reads through the heaps an address-based shader maps. It also states the ordering above and its granularity: the tracker stamps 64 KiB blocks and a store or reload covers the whole plane, so a partial guest write over unstored depth also drops that depth outside the written bytes. It also notes that console memory under a compressed HTILE holds compressed data until a DB decompress (HTILE definitions in Mesa
src/amd/common/ac_descriptors.h). The #454 entry points to the new entry.No new GPU semantics are introduced. The bytes in guest memory come from the existing
kZ64KBXdetiler, the same equation a sampled view of the plane already uses.Tested
Arch Linux, kernel 7.2.9-zen, NVIDIA RTX 4070 Ti SUPER (driver 615.78.08), GCC 16.2.1, Release,
BUILD_TESTING=ON, onmain40315fcf8.agc_driver_depth_plane_buffer_memory(new,tests/execution/DepthPlaneBufferMemory.cpp, on theDepthFastClearharness, which gains a swizzle field and a vertex-buffer argument):Draws depth 0.5 into a 64x32 D32 Z_X plane, then fetches vertices from the plane. All 2048 texels must read 0.5.
Writes 0.75 into the plane's memory, then draws at 0.5 under LESS. The draw must pass.
Draws at 0.5 with depth write, then fetches again. Memory must hold that 0.5.
On a new plane: draws 0.5, writes 0.75 into memory before any store, then fetches the plane with a draw on another target. Memory must still hold 0.75, and a draw at 0.5 under LESS must pass. Then it writes 0.25 and draws 0.5 with depth write: the fetch must see 0.5.
On a plane with HTILE: stores 0.5, writes 0.25, then a whole-HTILE ZMask 0 fill. A draw at 0.5 under LESS must pass (the clear wins).
A linear R32F texture at an offset into a plane must read what memory holds after a flush, also when it is viewed through its cached storage image after another depth draw.
On a plane filled with 0.25: draws depth 0.5, then a color target at the same address that blends ZERO/ONE (keeps what it loaded), then fetches the plane. It must hold 0.5. Red without the color-target flush, with or without the
retire()change.Step 4 is red on the previous revision (064a08c6a): "the depth 0.5 a draw wrote before a guest write into the plane was stored over that write". Step 5 is red when the reload drops the pending clear, as 064a08c6a did. Step 6 is red on 53b20c456 at the cached storage image read.
Red on
mainat step 1 ("a vertex fetch from the depth plane did not see the depth 0.5 a draw wrote into it"). With step 1's check removed, it is red at step 2 ("a draw at depth 0.5 failed LESS against the 0.75 written into the depth plane's memory"). Green with the change. Steps 2 and 3 run only where guest memory is write-watched; the test prints when it is not.All 701
agc_tests pass (ctest -R ^agc_ -j8, on c8a8ff100);agc_driver_stencil_ref_exportskips on this device as onmain.Uncharted: Legacy of Thieves Collection (PPSA05684), on the local integration branch (main + open PRs): the main menu shows the cliff and the whole cage under the fog in all 15 menu frame dumps. Frame times against the earlier version are in the comments. The title does not run on plain
mainyet.Overlap with open PRs
DB_Z_WRITE_BASE. It touchesDepthSurface.cpp,GuestBufferMemory.cppandVulkanDevice.cppat other places.DepthSurfaceView(pendingClear |= htileClearedbecomesNoteHtileClear).DepthSurface.cpp.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-17T14:18:30Z.
I traced the documented unstored-depth limitation through the new buffer-write path. The current regression calls the buffer reader before writing 0.75, so that first access has already run
FlushDepthPlanesand setstoredGeneration. If a CPU/buffer write happens after the depth-writing bind but before the first store,UploadFinishcallsFlushDepthPlaneswhilememoryStaleis true; that path unconditionallyStoreDepthPlanes the old image, andSyncFromMemoryis not reached before the guest write can be overwritten. WithstoredGeneration == 0, there is no prior store generation to compare either. Could you add a regression for this ordering and decide whether the store should detect/reload the newer guest write, or make the supported buffer-write claim explicitly require a prior depth-plane flush? The current limitation is documented, but this ordering is close to the advertised buffer-write case.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T14:24:06Z.
Thanks for this. Review at 064a08c6a: changes requested before it can go further.
Confirmed from the code:
SyncFromMemoryreloads the plane in the same bind (DepthSurface.cpp, the reload path). The clear should win over the reload.VulkanDevice::dispatchdecodes everyStorageBuffer/UniformBufferguestDescriptoras a V#, including bindings that aren't buffer descriptors (ShaderData,FlattenedSrt,ImageTableMap, and so on). The scan should be limited toGuestBuffers.Plausible:
WrittenSincebefore the store.Performance:
anyMemoryStalelooks effectively always set, so everyFlushPending, dispatch binding andUploadFinishregion takesGpuMutexplussurfacesMutex, and stores canSubmitAndWaitsynchronously.Asks:
WrittenSinceon the plane since its depth write.GuestBuffers.anyMemoryStalefrom staying permanently set.Ordering note: this merges cleanly with #7131, #7116 and #7085, but #7131 handles part of the same case (a fill writing depth memory), so the order between them will need settling.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T14:35:07Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppL1298-1305docs/dev/debt/libSceAgcDriver.mdL71Some 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.
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/include/Texture.hppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Draw.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppcore/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppdocs/dev/debt/libSceAgcDriver.mdImported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-17T15:33:58Z.
Thanks @MarcZX100 and @oneandonlydean. Pushed 53b20c456 (amended), PR text updated.
Write after the depth draw, before the first store (MarcZX100, ask 2): every bind of a mirrored surface now collects the plane's writes and takes the tracker generation, created surfaces included, so there is always a baseline.
FlushDepthPlaneschecksWrittenSinceagainst it before storing; if the plane was written since the last bind, store or reload, it reloads the depth from memory instead of storing. The rule: a guest write is ordered by the bind or flush that collects it. It replaces the depth drawn before it, unstored depth included, and depth drawn from a later bind is stored over it. On the console both are memory writes that land per byte in submission order. Here the tracker stamps 64 KiB blocks and a store or reload covers the whole plane, so a partial guest write over unstored depth also drops that depth outside the written bytes. The debt entry says this.HTILE clear vs reload (ask 1): a pending depth fast clear now skips the reload, and the reload no longer clears
pendingClear. A tile HTILE marks cleared reads the clear value whatever the plane bytes hold, so the clear wins.Dispatch scan (ask 3): only
DescriptorRole::GuestBuffersbindings are decoded as V#s.Tests (ask 5), in
agc_driver_depth_plane_buffer_memory:The first check is red on 064a08c6a. The HTILE one is red when the reload drops the pending clear, as 064a08c6a did. All 646
agc_tests pass and so does the conventions check.Performance (ask 4): you're right that the flag stayed set. A depth target drawn every frame and never read through a buffer stays stale until a flush over its range, so one global flag is set almost all the time. I replaced it with the list of stale plane ranges, kept under its own small lock, plus an atomic hull.
FlushPending, dispatch bindings andUploadFinishregions that miss every stale plane return after the hull check, or one short lock inside the hull, withoutGpuMutexor the surfaces lock. A flush that covers the range rebuilds the list from the surfaces that are still stale. What's left: a range that does overlap a stale plane still stores it, and outside an active recorder (a CPU-side flush) that store submits and waits. I don't have frame times yet. I'm measuring this revision against the previous one in PPSA05684 (menu and a depth-heavy gameplay stretch) and will post the numbers here.#7131: with both PRs, a recognized whole-plane fill becomes a pending clear with the fill value, so the reload here is skipped and the fill wins exactly. Any other write to a D32 Z_X plane reloads. That holds in either merge order. The second one needs a small rebase in
DepthSurfaceView.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T16:00:53Z.
Thanks for 53b20c456. Asks 1 (HTILE clear wins over the reload), 2 (WrittenSince before the store), 3 (GuestBuffers only) and 5 (tests) are closed in the code. The new checks fail with the amend reverted, so they test the fix.
Still owed: the before/after numbers for ask 4 on a heavy title (frame time and
[lock]lines). Please make sure the A/B also covers the new per-bind cost:SyncFromMemory, and with itGuestMemory::CollectWritesover the whole plane, now runs on every bind of a mirrorable depth target, created surfaces included.Heads-up on order: #7131 is planned to merge first. After it, this PR will conflict on one line in
DepthSurfaceView(surface->NoteHtileClear(htileCleared);vssurface->pendingClear |= htileCleared;plus the newSyncFromMemoryline). The resolution keepsNoteHtileClear(htileCleared)followed by theSyncFromMemoryline. No need to rebase until #7131 lands.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-17T20:10:41Z.
Pushed f5f7fcb56, with a correction to my previous reply.
Correction. Restricting the dispatch scan to
GuestBuffers(ask 3) exposed a real missing flush. Before, the old scan decoded float constants inFlattenedSrtbindings as V#s spanning hundreds of GB, which stored every stale plane by accident. Without that, cached images over a plane read stale memory. The write-stamp checks that let a cached texture, a cached storage image or a reused build skip their lookup see a stale plane's memory as unchanged, because it has not been written yet. In PPSA05684 this left the main menu under a blue fog wall in 15 of 17 frames. Now every image lookup and each of those checks (fastTexture,fastRevalidate,fastRevalidateEach) stores an overlapping stale plane first, and a reused build flushes its in-place buffer regions. The dispatch scan stays restricted toGuestBuffers. New test step: a linear texture at an offset into a plane, read through its cached storage image after another depth draw. It is red on 53b20c456 and green now. All 646agc_tests pass.Frame times (ask 4). Arch Linux (kernel 7.2.9-zen), i9-14900KF, RTX 4070 Ti SUPER, NVIDIA 615.78.08. Local integration branch (main
864729dc0+ open PRs #6412 #6182 #6771 #6772 #6280 + local patches), with this PR's earlier version 63f1e9078 (A) or f5f7fcb56 (B). Uncharted: Legacy of Thieves Collection, warm caches. Two 10-minute runs per side into a depth-heavy scene (APS5_PROFILE_DRAW=1), plus 60-75 s main-menu runs.Perf (DWARF stacks, three busiest driver threads, heavy part):
FlushDepthPlanes0.30 / 2.77 / 0.04 % on A, 0.11 / 0.39 / 0.05 % on B.StoreDepthPlaneis about 0 on both.FlushPendingandUploadFinishare 0.1-0.6 % on both.What keeps the stale state from staying set: a plane leaves the list when it is stored or reloaded, and a flush only touches planes in its range. That is why B stores about 9 times fewer planes than A, whose global flag made every flush store every stale plane. A depth target drawn every frame is still stale between its draw and the first read over its memory; a read that overlaps it stores it, and outside an active recorder that store submits and waits.
No new depth errors, throws, crashes or device losses. All 15 menu frame dumps with B show the cliff and the whole cage.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T10:17:17Z.
Rebase handoff — rebased onto current
main(authorship unchanged). Two conflicts inShaderResources.cpp:main'sDepthSurfaceOverlapsfast-fail guard kept alongside yourFlushDepthPlanescoherence check, both applied in order.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the knownmainregression (agc_driver_tessellationsegfault — Mesa, #5444), not this PR — your depth-plane tests pass.To pick it up:
Head is
07a967b67.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-18T10:55:24Z.
Thanks @blawesom. I rebased it myself onto
main9caecfd2das 19fc7978e, with one difference from your resolution: infastRevalidateandfastRevalidateEachI keptmain'sDepthSurfaceOverlapsguard and dropped this PR'sFlushDepthPlanesline. The guard already sends any image over a depth surface to the full walk, which flushes stale planes, so with both lines the flush could never find anything. All 701agc_tests pass (Linux, RTX 4070 Ti SUPER), conventions OK.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T11:21:53Z.
Thanks for the rebase and the f5f7fcb fixes; the flushes before each write-stamp shortcut look right.
One gap, which was already in 53b20c456:
DepthSurfaceOverlapsonly counts active, non-retired surfaces, butFlushDepthPlanesstores everymemoryStalesurface, andretire()leavesmemoryStaleset. So a plane that is drawn and then retired by a color target at the same memory (Draw.cpp:2042, no flush before it) is skipped by the storage-image fast path, and a later flush over that range stores the old depth over the color target's write-back.Could you:
retire()clearmemoryStaleand drop the surface's range from the stale list, so a retired plane is never stored later;FlushDepthPlanes(color.address, ...)before Draw.cpp:2042), as ShaderResources.cpp:984 already does for storage images;With that, dropping the flush in the storage-image loops is fully justified. On performance, our main-vs-PR gate runs will cover the comparison against plain
main, so no extra A/B is needed from you.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-18T11:44:53Z.
@oneandonlydean thanks, pushed 154c405d8 with all three:
retire()clearsmemoryStaleand drops the plane's range from the stale list (unless another stale surface still covers the same range), so a retired plane is never stored later.FlushDepthPlanes(color.address, ...)beforeRetireDepthSurfaces, as the storage-image path does.agc_driver_depth_plane_buffer_memoryhas a new step: fill a plane with 0.25, draw depth 0.5 into it, then draw a color target at the same address that blends ZERO/ONE (keeps what it loaded), then fetch vertices from the plane. The plane must hold the 0.5.Negative controls on this test: without (2), with or without (1), the new step fails (
a color target blending over a depth plane did not load the depth drawn into the plane); with (2) alone it passes.I also tried a step for the late store itself (depth draw, plain color overwrite, buffer read). It passed even without both changes. In the test, the color write-back is a CPU write the tracker sees, so the flush's write-generation check reloads the plane instead of storing it. So (1) only matters for a write-back the tracker does not see, and the test cannot reach that case. I left that step out because it passes either way.
All 701
agc_tests pass (Linux, RTX 4070 Ti SUPER), conventions OK.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T18:52:10Z.
Thanks. On 154c405d the functional results are clean, but the gate measured a consistent cost against main on the same base: the level-phase median frame time is 7-9% higher (reproduced in 2 of 2 runs, both above all 3 controls), and menu FPS is 2-4% lower. Before merging we'd like to cut that cost first. From reading the head, it seems to come from three places:
GuestBufferMemory::UploadFinish. It callsFlushDepthPlanesover every upload region and over the whole cached address space (space->base). Every depth-writing draw on a mirrorable target setsmemoryStaleagain inDepthSurfaceView, so the next build'sUploadFinishmost likely overlaps the plane and stores it, even if that draw never reads the depth memory. If so, a depth pass stores the full plane once per draw rather than once per reader.StoreDepthPlane→copyPlanecopies the full plane image → staging buffer → storage image behind threeALL_COMMANDSbarriers, or withSubmitAndWaitwhen no recorder is active. ThenFlushPendingwrites it back to guest memory.FlushDepthPlanesholdsGuestMemory::GpuMutex()together withsurfacesMutex()while it runsSyncFromMemoryand records the copies for every overlapping stale surface. That serialises other threads' guest-memory work for the duration.Suggestions (your call on the details):
UploadFinishflushes, especially thespace->baseloop, and rely on the consumer sites that already flush (cachedTexture,cachedStorageTexture,FlushPending, theGuestBuffersscan inVulkanDevice::dispatch,Revalidate), narrowed to the ranges they bind.GpuMutexonly for the part that needs it, and keep the copy recorded into the active recorder (noSubmitAndWait) on the draw path.A per-frame count of
StoreDepthPlanecalls (you had a store counter in the f5f7fcb comparison) before and after, on the same scene, would show whether point 1 is the main cost. Frame time against plainmainfrom the same machine would let us re-gate quickly.Separately, could you name the case this PR fixes, as a test or a short scenario without a game title? For example: what reads the depth memory as a buffer or texture, what it sees on
mainand what it sees with the PR.DepthPlaneBufferMemorytests the mechanism, but the PR doesn't yet say which behaviour onmainis wrong. That helps us weigh the remaining cost against the fix.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-18T23:44:14Z.
@oneandonlydean thanks for the gate numbers. Pushed 9626d8eb0 (amended).
What changed. The
space->baseloop inUploadFinishis gone.space->baseholds every registered heap an address-based shader maps, so it overlapped every stale plane. Each build with such a shader therefore stored every plane drawn since the last store, once per draw instead of once per reader, which is your point 1.mainalready exempts those heaps from pending storage-image flushes ("such shaders do not read texture memory through pointers"), and depth planes now follow the same rule. The debt entry says a read through those heaps is not caught. The loop overregionsstays: those are the ranges the build binds through its descriptors, and it covers the CPU-copied regions that noFlushPendingreaches.A plane is now stored only when a reader's range overlaps it, and the store clears
memoryStale, so depth draws between two readers cost one store. On the locks: draws and dispatches already holdGpuMutexwhile they record, so on those paths the flush's lock is a recursive re-entry.copyPlanerecords into the active recorder whenever one exists, and it submits and waits only for a flush from a CPU-side access.Numbers. Arch Linux, RTX 4070 Ti SUPER, NVIDIA 615.78.08, 2560x1440. PPSA05684 on my integration build (the title does not run on plain
mainyet), same session,APS5_PROFILE_DRAW=1in every run:"Mirroring off" is 9626d8eb0 with
mirrorable()returning false: nothing is marked stale, synced or stored. It is the closest I can get tomainhere. The remaining stores all come from real readers. The dispatches that copy the depth plane as a buffer (about two per frame) take about 100 stores per 10 s. Image-table entries whose texture ranges lie over a stale plane take about 130.This title runs at 2-5 fps here, so per-draw costs hide in its frame time. Your gate title runs at 20-27 fps in 4K, where a plane is about 33 MB and a store costs a larger share of a frame. I could not reproduce the 7-9% here and cannot run that title, so a re-gate would tell whether the store fix closes it. If a cost remains, the likely source is the per-bind
SyncFromMemory(aCollectWritesover the whole plane on every bind of a mirrorable target). I can make that skip binds whose plane cannot have been written since the last check.The case it fixes, without a title. A draw writes depth into a single-sample D32 plane with the 64 KiB Z swizzle. A compute dispatch then reads the plane through a buffer V# and copies it as raw dwords into another buffer, which is later sampled as depth. On
main, the dispatch reads whatever guest memory held, because no draw writes depth into guest memory: zeros on first use, then stale bytes. With the PR, it reads the drawn depth. Step 1 ofagc_driver_depth_plane_buffer_memoryis this case with a vertex fetch as the reader, and it fails onmain("a vertex fetch from the depth plane did not see the depth 0.5 a draw wrote into it"). The reverse is step 2: a buffer write into the plane, then a depth-tested draw. Onmain, the draw tests against the old image.All 701
agc_tests pass, and so does the conventions check.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-19T01:47:59Z.
Pushed c8a8ff100 on top: a fix for a bug in this PR that a title hits when it changes its output resolution.
Case. The title switches its render mode at runtime (2560x1440 → 1920x1080 → 3840x2160 → back). Each mode binds new D32 Z_X depth targets over memory where the previous mode's planes were: the 3840x2160 plane covers both the 2560x1440 and the 1920x1080 planes, from another base. After the first switch, buffer reads of the new planes kept seeing old depth, and that stayed true after switching back to the first mode.
Cause. Each surface stores through a storage image of its own plane (
mirrorPlane), kept for the surface's life. The older surfaces' images stayed live over the new planes' memory. Storage images over the same memory lend each other pending results, and a buffer copy whose source range is exactly a live image becomes an image copy (AliasCopy). So the new plane's stores stopped reaching the readers.Counted per 10 s on the main plane (a store lands in guest memory, and the next bind sees it as written and syncs):
Fix. A bind of a mirrorable target drops the images, and the pending store, of older surfaces whose planes overlap its plane over another range (another extent or base). A same-range surface (another stencil or HTILE address over the same plane) keeps its image. The scan runs only when a surface or a plane image was created since the surface's last scan, so steady frames do not scan. The debt entry describes the rule.
Tested. All 701
agc_tests pass (Linux, RTX 4070 Ti SUPER), and so does the conventions check. In the title, every mode and the switch back render as at launch. Before the fix, the first switch broke them until restart.No regression test yet. I tried three shapes on the harness, and all pass without the fix:
VulkanDevice::CopyBufferwith the CPU path refused (alias path).The title also has several surfaces per plane (one per stencil address), HTILE clears on every frame, and the stream-copy kernel going through the driver's packet path. I am working on a test that reproduces it and will add it here.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-19T12:51:13Z.
Follow-up to my last comment: I found the root cause behind the resolution-switch case, and added a test that fails without the fix. Pushed
c505661eaon top of your merge ofmain(9e32729ab), thanks for that.Cause.
StorageTexture::writeBackLayerskeeps the 64 KiB blocks written since an image's layers were last in sync, since that memory may have been reused. Each store overwrites a depth plane's image whole, but the image kept the sync state of its creation or its last reload. A plane bound behind an HTILE fast clear never reloads, because the clear wins over the reload. So once anything had written the plane's memory (in the title, the other resolution's targets over the same memory), every later store kept those blocks and the drawn depth never reached guest memory. A store now marks the image as holding the whole plane, the way a whole-image copy already does.StorageTexture::MarkWholeWrittenis split out ofCopyFrom's tail, unchanged.Test.
agc_driver_depth_plane_buffer_memorygains the case:The fetch must see the new depth, over two frames. Without
c505661eait fails at frame 0: "after a guest write into a stored depth plane, the depth drawn behind an HTILE clear did not reach memory".c8a8ff100 stays. It covers older surfaces' images over another range of the same memory, and it is the change I verified in the title.
The "Depth planes in guest memory" debt entry is shortened under the new 1000-character limit (
3f0d75025). Allagc_tests pass exceptagc_driver_bda_device, which fails at thismainbase too (Linux, RTX 4070 Ti SUPER). The conventions check passes. The title's menu renders as before.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T10:21:22Z.
An interaction found by bisecting a catalog title. It isn't a bug in this PR's depth handling as such, and the layer-scoped fix is already open as #6202.
Title: Pulse PPSA99993 (homebrew catalog, built on ps5-opengl). Windows 11, AMD Radeon 680M, AMD driver 32.0.21030.2001. The RTX 3070 shows the same per-draw upload.
Bisect (first-parent,
92e50418f..1ff5c8ac5, countingStorageTexture upload submitamong the first 80 draws, 25 s runs):50e0624d7, the merge of this PR. 78 uploads in 80 draws, and the title exits after 3 s with ps5-opengl's[ps5-agc] submission retirement failed.26740077b. 1 upload in 80 draws, and the title runs.Mechanism:
CB_COLOR0_ATTRIB3array depth 1), draws only into slice 0, and puts its D32 depth buffer where slice 1 would be. Here: colour 0x255200000, 16.7 MB per slice; depth at 0x256200000.FlushDepthPlanesover the covered range stores the stale depth plane into guest memory ([flush] image 0x256200000+0x870000 for depth plane store), and the store stamps the range.Refreshthen reportsguest memory changed(uploadReasonstore) and uploads all 33 MB again through the CPU path. That is about 22 ms per draw on this iGPU, which pushes the first ~90 draws past the title's 2 s wait.Before this PR the depth plane was never stored there, so the overlap with the unused slice didn't matter.
Fix: with #6202 (refresh only the layers a use of an array colour target covers) merged locally into
main, the lookup covers slice 0 only. Pulse then makes 1 upload in 80 draws and runs 60 s, 3 of 3; without it, 0 of 3. Numbers and the conflict resolutions are on #6202: https://github.com/boykopovar/AnyPS5/pull/6202#issuecomment-6313629742AI-assisted: yes