fix(agc): retire an unwritten native multisample depth surface on guest access #6727

Merged
simonbronk merged 1 commit from upstream-pr/6727 into main 2026-10-22 18:55:56 +00:00
simonbronk commented 2026-10-16 07:48:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-16T07:48:28Z.


What

Part of #6570. A guest read over the depth or HTILE range of a resident native multisample depth surface throws sample-preserving guest publication of native multisample depth is not implemented. That includes a surface the host never wrote after its import. In that case its samples are still the clear value the import took from the guest HTILE, so the guest bytes already describe the surface and nothing needs publishing.

  • Each native depth surface now records whether the host wrote it after the import. These count as host writes:

    • draws with depth writes, or with stencil tests and a non-zero stencil write mask, on the full draw path and on the cached recipe path (NoteDepthSurfaceWrite, called from Draw.cpp);
    • depth/stencil copies into the surface (CopyAspectFrom);
    • storage write-back.

    Host clears that follow a proven guest HTILE clear do not count: they keep the host equal to what the guest memory describes.

  • RequireNoResidentDepthRead_nid_no_patch retires an unwritten surface instead of throwing: it clears the surface's active flag and removes its guards. A later bind imports it again from the guest memory.

  • A surface the host wrote still throws the same refusal. Publishing it needs the layout and readback foundations in #6588 and #6601 and stays the follow-up in #6570.

  • The debt entry says what counts as a host write, and that a draw without depth or stencil writes leaving the console's Z planes and HTILE unchanged is inferred, not measured.

This depends on #6557 (its commit is the first one here). Both edit the same debt entry, and the title needs both.

The title's case, measured with local prints:

  1. Onimusha imports a 128x64, 4-sample D32 target (HTILE without ITERATE_256).
  2. It draws into it once with GREATER_OR_EQUAL depth tests, no depth or stencil writes and no color target.
  3. A compute dispatch then reads the same memory as a structured buffer (buffer_load_dwordx4 with idxen, 96-byte records) and stores into it.

Details are in #6570.

Tested

  • Windows 11 Pro 26200, Onimusha: Way of the Sword (PPSA27836), APS5_NO_HOST_IMPORT=1 (#6374). The build was main 5d9dcc42d + #5907 + #5884 + #5915 + #6557 + this PR.

    • Before: the first scene load stops on the refusal above in all 6 runs since 2026-10-15, on this stack or on the previous day's main 4eb91eacd.
    • After: it passes that point and stops later in the same load, at depth export, shader coverage or ordered fragment execution is unsupported (register 0x203 = 0x00000045) (DB_SHADER_CONTROL).
  • agc_driver_native_multisample_depth has a new case per format and sample count: D16 and D32 at 2, 4 and 8 samples, 6 cases on each GPU. Each case checks that:

    • a read of the depth range, and a DMA-copy check of the HTILE range, retire the unwritten surface and return the guest bytes;
    • an unrelated read, and write notes for another device or another target, retire nothing;
    • a rebind imports a new surface;
    • after a write note, both accesses still throw and the surface stays resident;
    • the guest depth and HTILE bytes are unchanged.

    The existing cases now note a host write before their refusal checks, since they rasterize into the surface directly. With retirement replaced by the old refusal, the new case fails.

  • ctest -R "agc|shader|video_out" -j 8 on both GPUs:

    • NVIDIA: all pass.
    • AMD: only #5271's three tests fail.
    • On both, agc_driver_gpu_indirect_arguments_misaligned_writer_ahead (from main) does not build with WinLibs GCC (#6452).
  • python tools/check_conventions.py --base upstream/main: passed.

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: #6557
  • Unblocks: PPSA27836 #5769
  • GPUs tested: NVIDIA GeForce RTX 5070, driver 617.42, Windows 11 Pro 26200; AMD Radeon gfx1036 iGPU, Windows 11 Pro 26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4885198816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-16T07:48:28Z. --- ### What Part of #6570. A guest read over the depth or HTILE range of a resident native multisample depth surface throws `sample-preserving guest publication of native multisample depth is not implemented`. That includes a surface the host never wrote after its import. In that case its samples are still the clear value the import took from the guest HTILE, so the guest bytes already describe the surface and nothing needs publishing. - Each native depth surface now records whether the host wrote it after the import. These count as host writes: - draws with depth writes, or with stencil tests and a non-zero stencil write mask, on the full draw path and on the cached recipe path (`NoteDepthSurfaceWrite`, called from `Draw.cpp`); - depth/stencil copies into the surface (`CopyAspectFrom`); - storage write-back. Host clears that follow a proven guest HTILE clear do not count: they keep the host equal to what the guest memory describes. - `RequireNoResidentDepthRead_nid_no_patch` retires an unwritten surface instead of throwing: it clears the surface's `active` flag and removes its guards. A later bind imports it again from the guest memory. - A surface the host wrote still throws the same refusal. Publishing it needs the layout and readback foundations in #6588 and #6601 and stays the follow-up in #6570. - The debt entry says what counts as a host write, and that a draw without depth or stencil writes leaving the console's Z planes and HTILE unchanged is inferred, not measured. This depends on #6557 (its commit is the first one here). Both edit the same debt entry, and the title needs both. **The title's case**, measured with local prints: 1. Onimusha imports a 128x64, 4-sample D32 target (HTILE without `ITERATE_256`). 2. It draws into it once with `GREATER_OR_EQUAL` depth tests, no depth or stencil writes and no color target. 3. A compute dispatch then reads the same memory as a structured buffer (`buffer_load_dwordx4` with `idxen`, 96-byte records) and stores into it. Details are in #6570. ### Tested - Windows 11 Pro 26200, Onimusha: Way of the Sword (PPSA27836), `APS5_NO_HOST_IMPORT=1` (#6374). The build was `main` 5d9dcc42d + #5907 + #5884 + #5915 + #6557 + this PR. - Before: the first scene load stops on the refusal above in all 6 runs since 2026-10-15, on this stack or on the previous day's `main` 4eb91eacd. - After: it passes that point and stops later in the same load, at `depth export, shader coverage or ordered fragment execution is unsupported (register 0x203 = 0x00000045)` (`DB_SHADER_CONTROL`). - `agc_driver_native_multisample_depth` has a new case per format and sample count: D16 and D32 at 2, 4 and 8 samples, 6 cases on each GPU. Each case checks that: - a read of the depth range, and a DMA-copy check of the HTILE range, retire the unwritten surface and return the guest bytes; - an unrelated read, and write notes for another device or another target, retire nothing; - a rebind imports a new surface; - after a write note, both accesses still throw and the surface stays resident; - the guest depth and HTILE bytes are unchanged. The existing cases now note a host write before their refusal checks, since they rasterize into the surface directly. With retirement replaced by the old refusal, the new case fails. - `ctest -R "agc|shader|video_out" -j 8` on both GPUs: - NVIDIA: all pass. - AMD: only #5271's three tests fail. - On both, `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead` (from `main`) does not build with WinLibs GCC (#6452). - `python tools/check_conventions.py --base upstream/main`: passed. ### Checklist - [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) - [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: #6557 - [x] Unblocks: PPSA27836 #5769 - [x] GPUs tested: NVIDIA GeForce RTX 5070, driver 617.42, Windows 11 Pro 26200; AMD Radeon gfx1036 iGPU, Windows 11 Pro 26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-20 12:51:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T12:51:12Z.


Thanks. The first commit on this branch (the ITERATE_256 HTILE import) is already on main, and the branch now conflicts with main in tests/execution/NativeMultisampleDepth.cpp and the libSceAgcDriver debt file. Could you rebase onto current main so the PR carries only "retire an unwritten native multisample depth surface on guest access"? I'll review it on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291256625 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727#issuecomment-6291256625). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T12:51:12Z. --- Thanks. The first commit on this branch (the ITERATE_256 HTILE import) is already on `main`, and the branch now conflicts with `main` in `tests/execution/NativeMultisampleDepth.cpp` and the libSceAgcDriver debt file. Could you rebase onto current `main` so the PR carries only "retire an unwritten native multisample depth surface on guest access"? I'll review it on the rebased head.
simonbronk commented 2026-10-20 22:02:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-20T22:02:04Z.


@oneandonlydean rebased onto current main (bcdb03375). The PR now carries only "retire an unwritten native multisample depth surface on guest access" (8328c2fd3).

  • Conflicts: main's 84e3b73f1 added a depthWritten parameter to DepthSurfaceView. I kept it at both Draw.cpp call sites and added this PR's NoteDepthSurfaceWrite next to them, so the plane mirroring and the host-write mark are independent.
  • Debt entry: I shortened this PR's addition to the native multisampled depth entry (1,922 → 1,778 characters; it was 1,403 on main, already over the 1,000 warning). The content is unchanged.

Tested on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, ctest -R "agc|shader|video_out":

  • RTX 5070 (617.42): 879/880 passed. agc_driver_storage_read_during_idle_store failed once under -j 8; it passes 5/5 alone on this head and 3/3 on main.
  • AMD gfx1036 iGPU: 879/880 passed. agc_driver_tessellation fails 3/3 on main as well.
  • agc_driver_native_multisample_depth passes on both.
  • agc_driver_image_array_window_past_layer_limit doesn't build with MinGW on main (alignas(65536), #8310 fixes it), so it was excluded.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6302718738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727#issuecomment-6302718738). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-20T22:02:04Z. --- @oneandonlydean rebased onto current `main` (`bcdb03375`). The PR now carries only "retire an unwritten native multisample depth surface on guest access" (8328c2fd3). - **Conflicts:** `main`'s 84e3b73f1 added a `depthWritten` parameter to `DepthSurfaceView`. I kept it at both `Draw.cpp` call sites and added this PR's `NoteDepthSurfaceWrite` next to them, so the plane mirroring and the host-write mark are independent. - **Debt entry:** I shortened this PR's addition to the native multisampled depth entry (1,922 → 1,778 characters; it was 1,403 on `main`, already over the 1,000 warning). The content is unchanged. **Tested** on Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release, `ctest -R "agc|shader|video_out"`: - RTX 5070 (617.42): 879/880 passed. `agc_driver_storage_read_during_idle_store` failed once under `-j 8`; it passes 5/5 alone on this head and 3/3 on `main`. - AMD gfx1036 iGPU: 879/880 passed. `agc_driver_tessellation` fails 3/3 on `main` as well. - `agc_driver_native_multisample_depth` passes on both. - `agc_driver_image_array_window_past_layer_limit` doesn't build with MinGW on `main` (`alignas(65536)`, #8310 fixes it), so it was excluded. AI-assisted: yes
oneandonlydean commented 2026-10-20 23:38:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T23:38:12Z.


@simonbronk Thanks for the rebase. The conflict resolution against 84e3b73f1 looks right (both call sites keep depthWritten), and the retire-on-read logic matches main's release retirement. Two small requests:

  1. NoteDepthSurfaceWrite takes surfacesMutex and walks every depth surface on every depth- or stencil-writing draw, including single-sample draws in titles that never create a resident depth guard. Could it return early when there are no guards? Guards are only created together with a fresh surface (whose hostWritten starts false), so this is exact:
    void NoteDepthSurfaceWrite(const Context& context, const DepthTarget& target) {
        if (target.samples == 1u || !anyDepthGuard().load(std::memory_order_acquire)) return;
        ...
    
  2. The Draw.cpp marking isn't covered yet: the new case calls NoteDepthSurfaceWrite directly. The fixture already does a real device->Draw(state, draw, initial) with depthWrite = true. Could you add a guest-read TargetRefuses(...) right after it, and if it's easy, a depth-test-only draw followed by a read that retires? A missed mark there would silently drop the host's depth instead of throwing, so it's the path most worth pinning.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6304251209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727#issuecomment-6304251209). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T23:38:12Z. --- @simonbronk Thanks for the rebase. The conflict resolution against 84e3b73f1 looks right (both call sites keep `depthWritten`), and the retire-on-read logic matches main's release retirement. Two small requests: 1. `NoteDepthSurfaceWrite` takes `surfacesMutex` and walks every depth surface on every depth- or stencil-writing draw, including single-sample draws in titles that never create a resident depth guard. Could it return early when there are no guards? Guards are only created together with a fresh surface (whose `hostWritten` starts false), so this is exact: ```cpp void NoteDepthSurfaceWrite(const Context& context, const DepthTarget& target) { if (target.samples == 1u || !anyDepthGuard().load(std::memory_order_acquire)) return; ... ``` 2. The Draw.cpp marking isn't covered yet: the new case calls `NoteDepthSurfaceWrite` directly. The fixture already does a real `device->Draw(state, draw, initial)` with `depthWrite = true`. Could you add a guest-read `TargetRefuses(...)` right after it, and if it's easy, a depth-test-only draw followed by a read that retires? A missed mark there would silently drop the host's depth instead of throwing, so it's the path most worth pinning.
simonbronk commented 2026-10-22 09:11:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-22T09:11:47Z.


@oneandonlydean both done, on a rebase onto main d6eafa54d (one conflict in DepthSurface.hpp, where main added DepthPlaneStoreCounts; I kept both).

  1. NoteDepthSurfaceWrite now returns at once for a single-sample target or when anyDepthGuard() is false, as you suggested.

  2. CheckDrawFeedback now covers the Draw.cpp marking through device->Draw:

    • right after the depth-writing draw, a guest read of the surface TargetRefuses(... "sample-preserving guest publication"), and the surface stays resident;
    • a second target gets a depth-test-only draw (depthWrite = false). A guest read of it then returns its guest bytes and retires it.

    The direct NoteDepthSurfaceWrite calls stay only in the fixtures that don't draw.

Tested: only partly, and I'd ask you to run the full CI on this head.

  • Builds: Linux, Ubuntu 24.04 x86-64 container (Colima/Rosetta), GCC 13, both RelWithDebInfo with ASan and Release, merged onto main d6eafa54d. check_conventions.py --base origin/main passes; its only warning is the debt entry's length, which was already over 1,000 characters on main.
  • agc_driver_native_multisample_depth could not be exercised there. That VM's kernel has no asynchronous userfaultfd write protection ("write watch unavailable"), so every native depth import is refused with "requires watched accessible guest memory". The test fails the same way on main d6eafa54d and on this head (lavapipe, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, Khronos validation), so neither run tells anything about the change. The only VUID- in both logs is the same VUID-VkDeviceCreateInfo-pNext-pNext, from Ubuntu's 1.3.275 layer not knowing a newer structure.
  • The Windows RTX 5070 / AMD iGPU runs from the previous head are not repeated on this one.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337113807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727#issuecomment-6337113807). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-22T09:11:47Z. --- @oneandonlydean both done, on a rebase onto `main` d6eafa54d (one conflict in `DepthSurface.hpp`, where `main` added `DepthPlaneStoreCounts`; I kept both). 1. `NoteDepthSurfaceWrite` now returns at once for a single-sample target or when `anyDepthGuard()` is false, as you suggested. 2. `CheckDrawFeedback` now covers the `Draw.cpp` marking through `device->Draw`: - right after the depth-writing draw, a guest read of the surface `TargetRefuses(... "sample-preserving guest publication")`, and the surface stays resident; - a second target gets a depth-test-only draw (`depthWrite = false`). A guest read of it then returns its guest bytes and retires it. The direct `NoteDepthSurfaceWrite` calls stay only in the fixtures that don't draw. **Tested:** only partly, and I'd ask you to run the full CI on this head. - Builds: Linux, Ubuntu 24.04 x86-64 container (Colima/Rosetta), GCC 13, both RelWithDebInfo with ASan and Release, merged onto `main` d6eafa54d. `check_conventions.py --base origin/main` passes; its only warning is the debt entry's length, which was already over 1,000 characters on `main`. - `agc_driver_native_multisample_depth` could not be exercised there. That VM's kernel has no asynchronous `userfaultfd` write protection ("write watch unavailable"), so every native depth import is refused with "requires watched accessible guest memory". The test fails the same way on `main` d6eafa54d and on this head (lavapipe, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, Khronos validation), so neither run tells anything about the change. The only `VUID-` in both logs is the same `VUID-VkDeviceCreateInfo-pNext-pNext`, from Ubuntu's 1.3.275 layer not knowing a newer structure. - The Windows RTX 5070 / AMD iGPU runs from the previous head are not repeated on this one. AI-assisted: yes
EagleBearerYS commented 2026-10-22 12:42:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T12:42:27Z.


@simonbronk a merge-order note: #9533, with #9717 and #9742 stacked on it, passes the draw's stencilTest to DepthSurfaceView at the two calls in Draw.cpp where this PR adds NoteDepthSurfaceWrite after the view. Both also reword the native multisampled depth sampling entry in the debt file. Composed, each call keeps the extra argument and your write note, and the entry keeps both changes. #9533 goes after this PR and rebases over it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6341018123 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6727#issuecomment-6341018123). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T12:42:27Z. --- @simonbronk a merge-order note: #9533, with #9717 and #9742 stacked on it, passes the draw's `stencilTest` to `DepthSurfaceView` at the two calls in `Draw.cpp` where this PR adds `NoteDepthSurfaceWrite` after the view. Both also reword the native multisampled depth sampling entry in the debt file. Composed, each call keeps the extra argument and your write note, and the entry keeps both changes. #9533 goes after this PR and rebases over it.
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!6727
No description provided.