Skip to content

fix(agc): take depth fast clears from HTILE written by any kernel - #5627

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
AlxcBTW:fix/agc-htile-written-by-any-kernel
Oct 15, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
AlxcBTW:fix/agc-htile-written-by-any-kernel

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. fix(agc): take depth fast clears from HTILE written by any kernel is @esatturan's fix(agc): take depth fast clears from HTILE written by any kernel #2053 commit, rebased: the store-back fits main's DepthSurfaceView, the debt entry moved to docs/dev/debt/libSceAgcDriver.md, a duplicate GuestMemory.hpp include is gone, and agc_driver_depth_htile_write counts its tiles itself because main's harness has no HtileBytes.
  2. fix(agc): read and store back the whole pipe-aligned HTILE outside the surfaces lock
  3. fix(agc): take a slice of a depth array from its own HTILE only when its words are uniform
  4. fix(agc): route native-owned depth binds before the HTILE read and store-back

What changed against #2053:

  • Whole metadata. The read covered tiles * 4 bytes from DB_HTILE_DATA_BASE. main models the HTILE as the pipe-aligned SW_64KB_Z_X metadata (HtileMetadataBytes, the extent HtileFillCovers takes). 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 first tiles * 4 bytes 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 before DepthSurfaceView takes 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_submit gets a case where an unrecognized kernel (the fill kernel behind an s_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 fix(agc): give each slice of a depth array its own HTILE #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 no GFX10_HTILE_SW_PATTERN entry 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 uniform 0xfffffff0). Otherwise it is refused and its words are left alone. DepthTarget::firstSlice carries DB_DEPTH_VIEW SLICE_START.

  • Tests. @esatturan's agc_driver_depth_htile_write is kept; its HTILE arrays now hold the whole metadata. New in agc_driver_depth_fast_clear_submit:

    • a draw after a DMA fast clear (depth only, then depth and stencil) leaves every HTILE word expanded (0xfffc000f / 0xfffff3ff); this fails on main, where the words stay 0;
    • two consecutive unrecognized-kernel clears are both taken;
    • slice 1 with HTILE words that are not all equal is refused (no clear, words not stored back, slice 0 untouched);
    • slice 1 with equal words takes the clear and stores it back as expanded.

    agc_driver_graphics checks 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, and DepthSurfaceView takes GuestMemory::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):

  • DepthSurfaceView calls RoutesToNativeDepthOwner first 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 the samples != 1 early return in the store-back.
  • Lock order: the HTILE read and store-back still run before 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.
  • NativeMultisampleDepth binds 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.

  • It fast-clears its depth HTILE every frame with the memory-value fill (fix(agc): take an HTILE fill from a value in memory as a depth fast clear #3816's MatchMemoryValueFill), so the clear itself is already taken on main. The guest HTILE then keeps ZMask 0 for the whole frame, because our depth draws never update it.
  • After the depth prepass, the Nanite depth export runs as compute, one wave per 8x8 tile. It loads the tile's HTILE word and branches on ZMask: 0 skips the depth read and takes the scene depth as the clear value, 15 reads the depth per pixel, and other values are decoded as planes. SMem selects the same paths for the stencil.
  • So every tile reads as cleared, and Nanite depth wins wherever Nanite has geometry, including behind closer non-Nanite meshes. The shading mask sends those pixels to the Nanite material pass, which paints the background material over what the base pass wrote. On screen, the far half of the player's sword and parts of the cloak turn black.
  • Frame journal replay of one such frame on NVIDIA. As things are, the depth along the blade at the export's output is 0.119 / 0.132 on the near half, then 0.0016 / 0.0020 / 0.0021 (the Nanite background) on the far half. The shading mask marks the far pixels as Nanite, and every Nanite sample in the viewport (159313 px) replaced the scene depth. With expanded HTILE words after the depth draws, the depth is 0.119 / 0.132 / 0.135 / 0.144 / 0.154 along the whole blade, the shading mask is 0, and the base pass survives the Nanite material pass.
  • The sword is slice 0, so it does not depend on the slice rule.

Tested

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 (fix(agc): give each slice of a depth array its own HTILE #5111 is merged; supersedes fix(agc): take depth fast clears from HTILE written by any kernel #2053)
  • Unblocks: PPSA25344, PPSA29141 (no Game bring-up discussion for either yet)
  • GPUs tested: none on this head (lavapipe in the PR CI); NVIDIA RTX 5070 Laptop on the earlier rebase, see Tested
  • AI-assisted: yes

@Amoenus

Amoenus commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@adriadam10 adriadam10 self-assigned this Oct 15, 2026

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

This no longer rebases onto current main, and the part that conflicts needs a decision from you:

  • #5111 is merged, so its commit drops out. The next commit then conflicts in DepthSurface.cpp, State.cpp, Dispatch.cpp and tests/DepthSurfaceReuse.cpp, plus the debt entry.
  • main now has the native multisampled depth owner (#5496). DepthSurfaceView takes GuestMemory::GpuMutex() before surfacesMutex(), and RequireNoResidentDepthRead_nid_no_patch throws on a guest write over an owner's range (DepthSurface.cpp on main, around line 917). storeBackClearedHtile writes 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.
  • The read and store-back were placed outside the surfaces lock to wait on recorded work. With the new lock order, check that they still run before GpuMutex is taken, or say why not.

After the rebase, rerun agc_driver_depth_htile_write, agc_driver_depth_fast_clear_submit, agc_driver_graphics and the native-depth tests. Happy to re-review then.

esatturan and others added 4 commits October 15, 2026 13:53
…e surfaces lock

The bind-time HTILE read covered one word per 8x8 tile from DB_HTILE_DATA_BASE, but main models the
HTILE as the pipe-aligned SW_64KB_Z_X metadata (HtileMetadataBytes, the extent HtileFillCovers takes):
at 1920x1080, 1856 of the 32400 tile words lie past the first tiles*4 bytes, so they kept ZMask 0
after a store-back. The read and the store-back now cover the whole metadata and run before the
surfaces mutex is taken, since both wait for recorded work over the range. The tests' HTILE arrays
hold the whole metadata.

PPSA25344 (Unreal Engine 5) decodes its depth through HTILE in its Nanite depth export after the
depth prepass and takes ZMask 0 as a tile holding the clear value; the new
agc_driver_depth_fast_clear_submit case checks that a draw after a fast clear leaves every word
expanded.
…its words are uniform

A fill or clear the driver recognizes over an HTILE drops the range's last-read generation, so the
next bind reads the words and stores them back even when the write tracker did not see the fill's
stores. A depth view of slice 1 or later reads the HTILE at its own address
(DB_HTILE_DATA_BASE + slice * HtileMetadataBytes); the HTILE word layout is only modelled for slice 0,
so such a slice takes the clear and the store-back only when all its words are equal and keeps its
fill-time fast clears otherwise. DepthTarget::slice carries DB_DEPTH_VIEW SLICE_START.

agc_driver_depth_fast_clear_submit: an unrecognized kernel clears the HTILE on two frames in a row and
both clears are taken; slice 1 with words that are not all equal is refused and stored back untouched;
slice 1 with equal words takes the clear. agc_driver_graphics checks the decoded slice.
…ore-back

A multisampled depth target is created as, or already is, the native depth owner, and a target whose HTILE overlaps a resident depth guard aliases one. Neither may have its HTILE read or rewritten by the generic store-back: the expanded words would be a guest write over the owner's range. DepthSurfaceView decides this before the store-back, which still runs before GpuMutex() and the surfaces mutex are taken. NativeMultisampleDepth binds the owner and a single-sample alias of its HTILE and checks the words stay untouched.
@AlxcBTW
AlxcBTW force-pushed the fix/agc-htile-written-by-any-kernel branch from 7f8db66 to 139e3b8 Compare October 15, 2026 11:57
@AlxcBTW

AlxcBTW commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @adriadam10. The branch is rebased onto current main (fdea36b83, after #5524) and the routing is in. Head: 139e3b86e.

  • fix(agc): give each slice of a depth array its own HTILE #5111 and the conflicts. The fix(agc): give each slice of a depth array its own HTILE #5111 commit is gone. Of the four files and the debt entry you listed, only the include block of DepthSurface.cpp conflicted: main now includes GuestAllocations.hpp next to GuestMemory.hpp, and @esatturan's commit drops the duplicate GuestMemory.hpp include, so both of main's lines stay and the duplicate goes. State.cpp, Dispatch.cpp, tests/DepthSurfaceReuse.cpp and the debt file merged without conflict. The slice field is firstSlice now.
  • Routing a native-owned bind. New commit fix(agc): route native-owned depth binds before the HTILE read and store-back. DepthSurfaceView (DepthSurface.cpp:1050) asks RoutesToNativeDepthOwner (:914) first and skips storeBackClearedHtile when 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 blunt samples != 1u early return inside storeBackClearedHtile; 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.
  • Test. CheckHtileRouting in tests/execution/NativeMultisampleDepth.cpp (run for every format and sample count of agc_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.
  • Lock order. The read and the store-back still run before GpuMutex() is taken: DepthSurfaceView calls 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 the GpuMutex -> surfacesMutex order; 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 other agc_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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#5372 docs/dev/debt/libSceAgcDriver.md L97-98
#6146 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp L10-11
#6292 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp L11
#6300 core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp L816

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
#1368 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
#1395 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
#4040 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
#4448 docs/dev/debt/libSceAgcDriver.md
#4650 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#5390 docs/dev/debt/libSceAgcDriver.md
#5499 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#5500 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
#5609 docs/dev/debt/libSceAgcDriver.md
#5761 docs/dev/debt/libSceAgcDriver.md
#5781 core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
#5804 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
#5875 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#5884 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#5905 docs/dev/debt/libSceAgcDriver.md
#5925 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#6188 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
#6222 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/libSceAgcDriver.md
#6235 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp

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

Re-checked against the new head:

  • Rebase is clean, #5111 is gone.
  • RoutesToNativeDepthOwner sends multisampled targets and any target whose HTILE overlaps a resident depth guard past the generic read and store-back. NativeMultisampleDepth fails if that routing is disabled.
  • The read and store-back still run before 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_depth and agc_driver_native_depth_retirement on lavapipe: all pass. The new submit and htile_write cases fail without the change. Merging once the full Build is green.

This was referenced Oct 15, 2026
@adriadam10
adriadam10 merged commit 3ee5371 into boykopovar:main Oct 15, 2026
9 checks passed
@AlxcBTW

AlxcBTW commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Rebased again onto main after #5554 landed (head 69b413ed8). No conflicts: #5554's strict native clear branch runs first in NoteHtileDepthClear, our routing in DepthSurfaceView keeps native-owned binds off the generic HTILE read/store-back, and CheckHtileRouting is unchanged against the merged owner API.

TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 25, 2026
Main brings the pipelined-draw stack (boykopovar#1193/boykopovar#1201/boykopovar#1202), boykopovar#6316 after boykopovar#6060, boykopovar#5893, boykopovar#5910 and depth/HTILE work (boykopovar#454, boykopovar#5496, boykopovar#5554, boykopovar#5524, boykopovar#5627, 626cff7). Our SharedSpirv / cold-SPIR-V storage is replaced by main's mapped cache entries (CreateFileMappingW on Windows), so cached variants' words are views into the cache files instead of heap copies; APS5_COLD_SPIRV, APS5_COLD_SPIRV_FILE and APS5_CACHE_SPIRV_BACKING are gone. Kept local: lane hang fixes, boykopovar#5815, boykopovar#6227, table stores and storage tables, option 1 substitutes (.slots), stencil remap and alias path (a written storage view our alias path serves seeds from the depth surface instead of retiring it), 10_11_11 blending next to main's reversed-order blending, VRAM share/spill/fallbacks, diagnostics. Versions: FormatVersion 43, RuntimeAbi 17, request serializer v31. Notes in work/sync-20261013/*-4.md.

AI-assisted: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants