fix(agc): read the stencil plane from memory for an unwritten native target #25874

Merged
birdmarty merged 1 commit from upstream-pr/25874 into main 2026-11-23 09:43:40 +00:00
birdmarty commented 2026-11-22 19:38:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by birdmarty on 2026-11-22T19:38:43Z.


What

A native multisampled D32S8 target that keeps its stencil in HTILE refused every stencil read through a descriptor without metadata unless an in-place expand was seen (native multisample depth sampled metadata or XOR mismatch). The texture unit ignores HTILE: such a read returns the plane bytes in memory, and an HTILE-only clear doesn't write them. When the host hasn't written the target since its last publication or import, those bytes are known: what was published or imported.

The read now goes to the guest-memory path (MultisampleTargetTexture) in that case, when:

  • the descriptor has no metadata;
  • it matches the plane (R8_UINT, the target's extent, samples, Z_X tiling, XOR, one slice and mip);
  • the target is not expanded.

After a host write it still refuses, since the plane the console would read then depends on stencil compression that is not modeled. The depth plane's policy is unchanged. Together with #25871 the rule for such a read is: an unwritten target reads the plane in memory (this PR), a written one needs an in-place expand of depth and stencil (#25871), and otherwise it refuses.

Changes

  • core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp: StencilPlaneReadFromMemory, checked in DepthSurfaceTexture after CheckCurrent.
  • core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp, tests/cmake/agc_driver_native_multisample_depth_tests.cmake: agc_driver_native_depth_stencil_plane_memory_read, for 2, 4 and 8 samples. The plane bytes in memory are 0x33 and the host stencil is cleared to 0x5a through HTILE; a stencil read without metadata returns 0x33 for every sample. A target the host has written since import still refuses.
  • docs/dev/debt/changes/fix-agc-read-unwritten-htile-stencil-from-memory.md: a new entry.

Tested

Windows 11 Pro (build 26300), Intel Core i9-9900K, AMD Radeon RX 6800 (AMD driver 26.10.1, Windows driver 32.0.21045.16010, Vulkan 1.4.315), MinGW-w64 GCC 15.3.0, Release, main 66668ebb8b.

Focused set (agc_driver_graphics, agc_shader_memory, agc_driver_sample_multisample_target, agc_driver_recorder, agc_driver_cached_image_multisample_guard, agc_driver_cmask_fast_clear, agc_driver_pipelined_metadata_pass*, agc_driver_native_*, agc_driver_fmask_*, agc_driver_msaa_*, agc_driver_multisample*, agc_driver_sampled_cache*, image_msaa*, agc_driver_depth*, agc_driver_htile*, agc_driver_stencil*) with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (1.4.363) and VK_LOADER_DEBUG=layer, on a1f22c3d2a (this commit before its test entry moved to another line):

main 66668ebb8b this change
Passed / failed 149 / 0 150 / 0 (adds agc_driver_native_depth_stencil_plane_memory_read)
Validation layer inserted 189 191
VUID- lines 0 0

On this commit, --stencil-plane-memory-read, --stencil-view, --stencil and --expand-gate passed again with the validation layer and 0 VUIDs. Without the fix (main's DepthSurface.cpp with this branch's test), the new test fails with AGC graphics: native multisample depth sampled metadata or XOR mismatch.

Forza Horizon 5 (PPSA22327 01.685.672), local stack of open PRs:

  • What the game does, every loading frame: it fills its native 4x D32S8 target's whole HTILE with 0, renders a single-sample depth target into the same memory, and reads the 4x stencil plane through a descriptor without metadata. The 4x target itself was drawn once and published before; no host write followed.
  • Without this change: the refusal stopped 5 of 5 runs.
  • With this change: 8 runs, none with it. Two stopped at one later stop and two at another; the other four did not stop: three hung (two on the loading screen, one after the start of the intro) and one ran 300 s until stopped, without screenshots.

py -3 tools/check_conventions.py --base origin/main and py -3 tools/debt_fold.py --check pass. py -3 tools/pr_overlap.py --branch fix/agc-read-unwritten-htile-stencil-from-memory (2,438 open PRs) found only a test-file line shared with #20186; it was moved, and the branch now merges cleanly with it. Twenty-four PRs that conflict with main were searched by hand; #6808, #9950, #1368 and #1395 touch other stencil code, and none reads the plane from memory.

Not tested: what the console returns for a plane written while compressed (still refused), other GPUs, Linux.

Rebased onto main f220c22162 (394 commits, none touching the files this PR changes, no conflicts). On the rebased commit check_conventions and debt_fold --check pass. All eight PRs merged together on f220c22162, the focused tests of all eight with the same environment: main 182 passed / 0 failed, validation layer inserted 242 times, 0 VUID- lines; the eight together 188 passed / 0 failed (the six new tests added), inserted 250 times, 0 VUID- lines.

Part of the Forza Horizon 5 (PPSA22327) bring-up, #25152. The game hits these stops one after another, so these PRs were found and tested together on one stack: the other open PRs the game needs (#24960, #24994, #25151, #23688, #23545, #21479, #11825, #23778, #21155, #22306, #21199) plus the PRs below. Each one stands alone: it applies to main by itself, needs none of the others to build or pass its tests, and merges cleanly with every other one.

  • #25867 fix/agc-materialize-cold-native-fmask: cold native FMASK pages read as stored after a first touch
  • #25868 fix/agc-multisample-read-single-sample-depth: multisampled read over a single-sample depth surface
  • #25869 fix/agc-claim-retires-memory-backed-msaa-copy: memory-backed multisampled copy treated as a host-only alias
  • #25870 fix/agc-skip-no-op-htile-hiz-patch: HTILE Hi-Z patch dispatch that writes nothing
  • #25871 fix/agc-sample-htile-stencil-after-expand: stencil kept in HTILE read without metadata after an expand
  • #25872 fix/agc-expanded-after-whole-depth-publication: a whole native depth publication taken as an expand
  • #25873 fix/agc-htile-clear-supersedes-plane-writes: plane writes before a whole HTILE clear

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: PPSA22327 #25152
  • GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer 1.4.363.0 on main and this change, 0 VUIDs
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5442605428 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25874). Originally posted by [birdmarty](https://github.com/birdmarty) on 2026-11-22T19:38:43Z. --- ### What A native multisampled D32S8 target that keeps its stencil in HTILE refused every stencil read through a descriptor without metadata unless an in-place expand was seen (`native multisample depth sampled metadata or XOR mismatch`). The texture unit ignores HTILE: such a read returns the plane bytes in memory, and an HTILE-only clear doesn't write them. When the host hasn't written the target since its last publication or import, those bytes are known: what was published or imported. The read now goes to the guest-memory path (`MultisampleTargetTexture`) in that case, when: - the descriptor has no metadata; - it matches the plane (R8_UINT, the target's extent, samples, Z_X tiling, XOR, one slice and mip); - the target is not expanded. After a host write it still refuses, since the plane the console would read then depends on stencil compression that is not modeled. The depth plane's policy is unchanged. Together with #25871 the rule for such a read is: an unwritten target reads the plane in memory (this PR), a written one needs an in-place expand of depth and stencil (#25871), and otherwise it refuses. #### Changes - `core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp`: `StencilPlaneReadFromMemory`, checked in `DepthSurfaceTexture` after `CheckCurrent`. - `core/libs/prx/libSceAgcDriver/tests/execution/NativeMultisampleDepth.cpp`, `tests/cmake/agc_driver_native_multisample_depth_tests.cmake`: `agc_driver_native_depth_stencil_plane_memory_read`, for 2, 4 and 8 samples. The plane bytes in memory are `0x33` and the host stencil is cleared to `0x5a` through HTILE; a stencil read without metadata returns `0x33` for every sample. A target the host has written since import still refuses. - `docs/dev/debt/changes/fix-agc-read-unwritten-htile-stencil-from-memory.md`: a new entry. ### Tested Windows 11 Pro (build 26300), Intel Core i9-9900K, AMD Radeon RX 6800 (AMD driver 26.10.1, Windows driver 32.0.21045.16010, Vulkan 1.4.315), MinGW-w64 GCC 15.3.0, Release, `main` 66668ebb8b. Focused set (`agc_driver_graphics`, `agc_shader_memory`, `agc_driver_sample_multisample_target`, `agc_driver_recorder`, `agc_driver_cached_image_multisample_guard`, `agc_driver_cmask_fast_clear`, `agc_driver_pipelined_metadata_pass*`, `agc_driver_native_*`, `agc_driver_fmask_*`, `agc_driver_msaa_*`, `agc_driver_multisample*`, `agc_driver_sampled_cache*`, `image_msaa*`, `agc_driver_depth*`, `agc_driver_htile*`, `agc_driver_stencil*`) with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (1.4.363) and `VK_LOADER_DEBUG=layer`, on a1f22c3d2a (this commit before its test entry moved to another line): | | `main` 66668ebb8b | this change | |---|---|---| | Passed / failed | 149 / 0 | 150 / 0 (adds `agc_driver_native_depth_stencil_plane_memory_read`) | | Validation layer inserted | 189 | 191 | | `VUID-` lines | 0 | 0 | On this commit, `--stencil-plane-memory-read`, `--stencil-view`, `--stencil` and `--expand-gate` passed again with the validation layer and 0 VUIDs. Without the fix (`main`'s `DepthSurface.cpp` with this branch's test), the new test fails with `AGC graphics: native multisample depth sampled metadata or XOR mismatch`. Forza Horizon 5 (PPSA22327 01.685.672), local stack of open PRs: - **What the game does, every loading frame:** it fills its native 4x D32S8 target's whole HTILE with 0, renders a single-sample depth target into the same memory, and reads the 4x stencil plane through a descriptor without metadata. The 4x target itself was drawn once and published before; no host write followed. - **Without this change:** the refusal stopped 5 of 5 runs. - **With this change:** 8 runs, none with it. Two stopped at one later stop and two at another; the other four did not stop: three hung (two on the loading screen, one after the start of the intro) and one ran 300 s until stopped, without screenshots. `py -3 tools/check_conventions.py --base origin/main` and `py -3 tools/debt_fold.py --check` pass. `py -3 tools/pr_overlap.py --branch fix/agc-read-unwritten-htile-stencil-from-memory` (2,438 open PRs) found only a test-file line shared with #20186; it was moved, and the branch now merges cleanly with it. Twenty-four PRs that conflict with `main` were searched by hand; #6808, #9950, #1368 and #1395 touch other stencil code, and none reads the plane from memory. Not tested: what the console returns for a plane written while compressed (still refused), other GPUs, Linux. Rebased onto `main` f220c22162 (394 commits, none touching the files this PR changes, no conflicts). On the rebased commit `check_conventions` and `debt_fold --check` pass. All eight PRs merged together on f220c22162, the focused tests of all eight with the same environment: `main` 182 passed / 0 failed, validation layer inserted 242 times, 0 `VUID-` lines; the eight together 188 passed / 0 failed (the six new tests added), inserted 250 times, 0 `VUID-` lines. ### Related Part of the Forza Horizon 5 (PPSA22327) bring-up, #25152. The game hits these stops one after another, so these PRs were found and tested together on one stack: the other open PRs the game needs (#24960, #24994, #25151, #23688, #23545, #21479, #11825, #23778, #21155, #22306, #21199) plus the PRs below. Each one stands alone: it applies to `main` by itself, needs none of the others to build or pass its tests, and merges cleanly with every other one. - #25867 `fix/agc-materialize-cold-native-fmask`: cold native FMASK pages read as stored after a first touch - #25868 `fix/agc-multisample-read-single-sample-depth`: multisampled read over a single-sample depth surface - #25869 `fix/agc-claim-retires-memory-backed-msaa-copy`: memory-backed multisampled copy treated as a host-only alias - #25870 `fix/agc-skip-no-op-htile-hiz-patch`: HTILE Hi-Z patch dispatch that writes nothing - #25871 `fix/agc-sample-htile-stencil-after-expand`: stencil kept in HTILE read without metadata after an expand - #25872 `fix/agc-expanded-after-whole-depth-publication`: a whole native depth publication taken as an expand - #25873 `fix/agc-htile-clear-supersedes-plane-writes`: plane writes before a whole HTILE clear ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA22327 #25152 - [x] GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer 1.4.363.0 on `main` and this change, 0 VUIDs - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-22 19:54:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T19:54:29Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code supports the documented behavior, and the reported focused tests pass.

What I ran

Reviewed head f809aef7450d2d0542e8ef529f32ec506b0b6496 using repo_read.py. I did not run tests or other commands; test results below are author-reported.

Results

Check Result Note
Code review passed Four changed files read
Focused tests not run Reported passing by author
Convention and debt checks not run Reported passing by author

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the tests, convention checks, or debt check. Console behavior, Linux, and GPUs other than the reported RX 6800 were not verified. All four changed files listed in the manifest were read; no excluded files were listed in the supplied manifest. The review bundle did not provide files.json.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6014150273 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25874#pullrequestreview-6014150273). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T19:54:29Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code supports the documented behavior, and the reported focused tests pass. ## What I ran Reviewed head `f809aef7450d2d0542e8ef529f32ec506b0b6496` using `repo_read.py`. I did not run tests or other commands; test results below are author-reported. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Four changed files read | | Focused tests | not run | Reported passing by author | | Convention and debt checks | not run | Reported passing by author | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the tests, convention checks, or debt check. Console behavior, Linux, and GPUs other than the reported RX 6800 were not verified. All four changed files listed in the manifest were read; no excluded files were listed in the supplied manifest. The review bundle did not provide `files.json`. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!25874
No description provided.