fix(agc): take depth fast clears from HTILE written by any kernel #5627
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5627"
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 AlxcBTW on 2026-10-14T02:08:34Z.
What
Supersedes #2053, as @oneandonlydean asked there. @esatturan's commit is kept with his authorship, rebased onto current
main, with three commits of ours on top: two answer the review on #2053 and one routes native-owned depth binds around the generic path (the review on this PR).#2053 reads a depth target's HTILE at bind when it was written since it was last read. If every word marks the depth (ZMask 0) or the stencil (SMem 0) cleared, the surface takes the pending fast clear and the words are stored back as expanded. That covers clears made by kernels the fill hooks do not recognize (PPSA29141's
InvokeHTileClearComputeShader), and the store-back also matters for titles that read their HTILE after drawing (Sword of the Sea, below).Commits, bottom to top:
fix(agc): take depth fast clears from HTILE written by any kernelis @esatturan's #2053 commit, rebased: the store-back fitsmain'sDepthSurfaceView, the debt entry moved todocs/dev/debt/libSceAgcDriver.md, a duplicateGuestMemory.hppinclude is gone, andagc_driver_depth_htile_writecounts its tiles itself becausemain's harness has noHtileBytes.fix(agc): read and store back the whole pipe-aligned HTILE outside the surfaces lockfix(agc): take a slice of a depth array from its own HTILE only when its words are uniformfix(agc): route native-owned depth binds before the HTILE read and store-backWhat changed against #2053:
Whole metadata. The read covered
tiles * 4bytes fromDB_HTILE_DATA_BASE.mainmodels the HTILE as the pipe-aligned SW_64KB_Z_X metadata (HtileMetadataBytes, the extentHtileFillCoverstakes). With the HTILE equation of Sword of the Sea's Nanite export shader, 1856 of the 32400 tile words of a 1920x1080 surface lie past the firsttiles * 4bytes and would keep ZMask 0 after a store-back. The read and the store-back now cover the whole metadata. Both wait for recorded work over the range, so they run beforeDepthSurfaceViewtakes the surfaces mutex.GPU-written HTILE, two frames in a row (@oneandonlydean's and @adriadam10's request, PPSA01802). A fill or clear the driver recognizes over an HTILE drops the range's last-read generation, so the next bind reads and stores back again even when the write tracker did not see the stores. A bind also reads the words whenever the recorder has a pending write over the HTILE.
agc_driver_depth_fast_clear_submitgets a case where an unrecognized kernel (the fill kernel behind ans_nop, so no matcher takes it) clears the HTILE on two consecutive frames and both clears are taken.Slices of a depth array (@XairRrR's data). Since #5111 a slice's HTILE is at
DB_HTILE_DATA_BASE + slice × HtileMetadataBytes(extent). The HTILE word layout is only modelled for slice 0: XairRrR's fit on PPSA05684 shows slices 1 to 3 need extra Z terms (X8 and Y8 also take Z0 and Z1) that noGFX10_HTILE_SW_PATTERNentry has. So a slice of 1 or more takes the clear and the store-back only when all its HTILE words are equal, which decodes the same under any layout (PPSA05684's boat level binds slices 4 and 5 of arrays whose HTILE is a uniform0xfffffff0). Otherwise it is refused and its words are left alone.DepthTarget::firstSlicecarriesDB_DEPTH_VIEWSLICE_START.Tests. @esatturan's
agc_driver_depth_htile_writeis kept; its HTILE arrays now hold the whole metadata. New inagc_driver_depth_fast_clear_submit:0xfffc000f/0xfffff3ff); this fails onmain, where the words stay 0;agc_driver_graphicschecks the decoded slice.The debt entry "HTILE written by other kernels" records what is still open: the slice layout above, and per-tile clear values of a depth-only HTILE (ZMask 0 everywhere but different Min/Max Z, as in PPSA05684's cascades), which are still taken as a whole-surface clear to
DB_DEPTH_CLEAR.Not in this PR: the store-back is a CPU write after the bind-time read. Recording it as a GPU fill in stream order, as @XairRrR does locally, would also remove the wait on pending HTILE writes.
Composition with #5496, #5524 and #5554. #5496 and #5524 are on
main: the native multisampled depth owner guards its depth and HTILE ranges, andDepthSurfaceViewtakesGuestMemory::GpuMutex()before the surfaces mutex. The expanded store-back is a guest write over an owner's HTILE, which the owner refuses, so commit 4 decides before the store-back whether the bind belongs to the owner (@adriadam10's request, and the routing @Amoenus and I agreed on here):DepthSurfaceViewcallsRoutesToNativeDepthOwnerfirst and skips the HTILE read and store-back when it is true. It is true for every multisampled target (it is the native owner or is about to be created as one, and the owner's import validates the HTILE as the guest left it) and for any target whose HTILE overlaps a resident depth guard (a single-sample alias of an owner's metadata). Those binds go on to the existing owner and creation path with its own refusals. Single-sample targets that do not alias an owner are unchanged. This replaces thesamples != 1early return in the store-back.GpuMutex()is taken, because the read waits for recorded work; the routing check takes only the shared depth guard mutex with no other lock held.NativeMultisampleDepthbinds a native-owned target twice and a single-sample alias of its HTILE, with HTILE words of 0, and checks the words are untouched and the alias is refused by the owner path.#5554 builds on #5524 and does not need the generic store-back.
Evidence: Sword of the Sea (PPSA25344, Unreal Engine 5)
The title needs the store-back for a different reason than the clear detection: it reads its HTILE after drawing.
MatchMemoryValueFill), so the clear itself is already taken onmain. The guest HTILE then keeps ZMask 0 for the whole frame, because our depth draws never update it.Tested
ci/lab/htile2053, based onmain5a81f6085, run 38497597289): Linux (lavapipe, GCC) 1092 of 1092 tests pass, includingagc_driver_graphicsand everyagc_driver_depth_*test; Windows (WinLibs GCC 15.2) builds and 1112 of 1112 pass, theagc_driver_depth_*execution tests skipped for lack of a Vulkan device. Not rerun on this head (rebased ontomainfdea36b83, with the routing commit); a new fork CI run will be linked here.agc_driver_depth_*execution tests passed on the earlier rebase of this stack (fix/htile-store-back-rebase, without the slice commit, on themainof 2026-10-13; driver not recorded). Not rerun on this head.main. The menu gate (no dark pixels, top luma 88 at frame 585) passed on 2026-10-12 on our lab buildsots/menu-boot-m1(21d699737), which has no store-back. That run is not evidence for this PR.main(57f96fd2), the levels rendered correctly (my comment on #2053). Not rerun on this head.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 Amoenus on 2026-10-14T03:16:13Z.
@AlxcBTW I agree with the composition contract you described: native-owned binds must reach the guarded owner before the generic HTILE read/store-back. If #5627 lands first, I will carry that routing and a regression in our native-depth stack; if our stack lands first, your stated follow-up here handles it. #5554 still depends on #5524/#5111, transitively #5496/#4820; it does not require the generic store-back implementation. Our refreshed dependency composition passes repeated native-depth tests on NVIDIA and AMD, but I have not tested it together with #5627 and make no combined-head compatibility claim.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:28:51Z.
Review: CHANGES_REQUESTED
This no longer rebases onto current
main, and the part that conflicts needs a decision from you:DepthSurface.cpp,State.cpp,Dispatch.cppandtests/DepthSurfaceReuse.cpp, plus the debt entry.mainnow has the native multisampled depth owner (#5496).DepthSurfaceViewtakesGuestMemory::GpuMutex()beforesurfacesMutex(), andRequireNoResidentDepthRead_nid_no_patchthrows on a guest write over an owner's range (DepthSurface.cpponmain, around line 917).storeBackClearedHtilewrites expanded words over the HTILE of every depth target, so a bind of a native-owned target would hit that guard. Your description says whichever PR lands second carries the routing: that is this one now. Please route a native-owned bind to the owner's path before the read and store-back, with a test that binds one.GpuMutexis taken, or say why not.After the rebase, rerun
agc_driver_depth_htile_write,agc_driver_depth_fast_clear_submit,agc_driver_graphicsand the native-depth tests. Happy to re-review then.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T11:57:34Z.
Thanks @adriadam10. The branch is rebased onto current
main(fdea36b83, after #5524) and the routing is in. Head:139e3b86e.DepthSurface.cppconflicted:mainnow includesGuestAllocations.hppnext toGuestMemory.hpp, and @esatturan's commit drops the duplicateGuestMemory.hppinclude, so both ofmain's lines stay and the duplicate goes.State.cpp,Dispatch.cpp,tests/DepthSurfaceReuse.cppand the debt file merged without conflict. The slice field isfirstSlicenow.fix(agc): route native-owned depth binds before the HTILE read and store-back.DepthSurfaceView(DepthSurface.cpp:1050) asksRoutesToNativeDepthOwner(:914) first and skipsstoreBackClearedHtilewhen it says yes. It says yes for every multisampled target, because that target either is the native owner or is about to be created as one, and the owner's import checks the HTILE as the guest left it (htileProvesClear,UnchangedSinceAll), so an expanded store-back before the import would change what it validates. It also says yes for any target whose HTILE overlaps a resident depth guard, which is a single-sample alias of an owner's metadata. Both go on to the existing creation and owner path, which keeps its own refusals ("layout or metadata changed", "fast-clear transition is not implemented"). This replaces the bluntsamples != 1uearly return insidestoreBackClearedHtile; the multisampled half is kept, as the first rule above, because the not-yet-created owner has no guard to detect. Single-sample targets that do not alias an owner are unchanged.CheckHtileRoutingintests/execution/NativeMultisampleDepth.cpp(run for every format and sample count ofagc_driver_native_multisample_depth): HTILE words of 0 (what the generic path would take as a clear), then bind the native target, rebind it, and bind a single-sample alias over its HTILE. The words in guest memory must stay 0 after each, the alias must be refused by the owner path ("layout or metadata changed"), and nothing may throw from the guard on the generic path.GpuMutex()is taken:DepthSurfaceViewcalls them at:1050,GpuMutex()is taken at:1051, and the surfaces mutex after it. They have to stay outside because the read waits for recorded work. The routing check takes only the shared depth guard mutex, with no other lock held, so it cannot invert theGpuMutex->surfacesMutexorder; the guard mutex is otherwise taken inside those two.To rerun:
agc_driver_depth_htile_write,agc_driver_depth_fast_clear_submit,agc_driver_graphics,agc_driver_native_multisample_depth,agc_driver_native_depth_retirement, and the otheragc_driver_depth_*tests. I have not built or run this head locally; the PR's CI runs on this push and I'll follow up on its result.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:00:30Z.
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)docs/dev/debt/libSceAgcDriver.mdL97-98core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppL10-11core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppL11core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cppL816Some 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/DepthSurface.cppcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mddocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppdocs/dev/debt/libSceAgcDriver.mdcore/libs/prx/libSceAgcDriver/tests/Graphics.cppImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:02:53Z.
Review: APPROVED
Re-checked against the new head:
RoutesToNativeDepthOwnersends multisampled targets and any target whose HTILE overlaps a resident depth guard past the generic read and store-back.NativeMultisampleDepthfails if that routing is disabled.GpuMutex(); the routing check only takes the shared guard mutex.Ran
agc_driver_graphics,agc_driver_depth_fast_clear,agc_driver_depth_fast_clear_submit,agc_driver_depth_htile_write,agc_driver_native_multisample_depthandagc_driver_native_depth_retirementon lavapipe: all pass. The new submit and htile_write cases fail without the change. Merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T12:52:59Z.
Rebased again onto
mainafter #5554 landed (head69b413ed8). No conflicts: #5554's strict native clear branch runs first inNoteHtileDepthClear, our routing inDepthSurfaceViewkeeps native-owned binds off the generic HTILE read/store-back, andCheckHtileRoutingis unchanged against the merged owner API.