fix(agc): reload a sampled depth plane the guest wrote since its surface was bound #12238

Merged
XairRrR merged 2 commits from upstream-pr/12238 into main 2026-10-30 05:32:55 +00:00
XairRrR commented 2026-10-28 04:17:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-28T04:17:11Z.


What

A depth plane with the 64 KiB Z swizzle reloads the units the guest wrote (SyncFromMemory, #7154) when its surface is bound as a depth target. Sampling the plane through the surface (DepthSurfaceTexture, the single-sample Sampled path) skipped that check and read the host image. A title that writes the plane with a buffer copy and samples it before it draws into it again therefore read the depth from before the copy.

PPSA05684 (Uncharted: Legacy of Thieves Collection) does this every frame in Uncharted 4: it copies its depth pre-pass into a second depth target with a buffer_load_format_x / buffer_store_format_x kernel, samples that copy right away to build the half-resolution depth its volumetric fog and dust read, and only later binds the target. The fog used the previous frame's depth, so at the frame rates reached here a dark or light silhouette followed every moving character through fog and dust (the orphanage's closing scene and the prison yard).

Sampling a single-sample plane through its surface now runs the same reload first, so the sampled depth includes the guest's writes collected by then. The debt entry for depth planes in guest memory lists sampling among the points where guest writes are collected.

Tested

Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, on main 3a2b919816, NVIDIA GeForce RTX 4070 Ti SUPER (615.78.08):

  • agc_driver_depth_plane_sampling gains GuestWriteAfterDrawCase. It draws depth 0.25 into a 128x128 D32 plane with the 64 KiB Z swizzle and samples it (0.25), then writes 0.75 into the guest memory under it and samples it again without binding the surface. With DepthSurface.cpp from main it fails: D32 plane the guest wrote after a draw, sampled before its surface is bound again: texel 0 read 0x3e800000, expected 0x3f400000. DrawDepth takes an optional depth swizzle for it.
  • Where guest memory is not write-watched (APS5_NO_WRITE_WATCH=1, or a host without a write watch), the driver cannot see the guest's write. The case then prints guest memory has no write watch: the guest write after a draw is not checked and returns, like the CPU-write cases of agc_driver_depth_plane_partial_store.
  • VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer ctest -V -R '^agc_driver_depth_plane_sampling' on main 3a2b919816 and on this branch: 2/2 pass on both, and the layer is inserted in every test. No log reports was not found but was requested by env var VK_INSTANCE_LAYERS, and neither log has a VUID- line. With APS5_NO_WRITE_WATCH=1, the branch also passes 2/2 with no VUID- line.
  • Full ctest -j8 on the first base (main 3ca715ee15): 2620 of 2621 pass. agc_driver_mesh_barycentric_emulation fails the same way on main on this GPU (the pixel shader did not emulate barycentrics).
  • tools/check_conventions.py --base origin/main passes.
  • Cost: I timed the added SyncFromMemory call in the prison yard fight of PPSA05684, where the fog and dust sample the copied depth plane every frame. It ran 6 to 17 thousand times per 10 s at about 1.1 us each, 7 to 16 ms per 10 s in total, so 0.2 to 0.4 ms of a ~260 ms frame.
  • PPSA05684: the silhouettes in the fog of the orphanage's closing scene and in the dust of the prison yard are gone (checked in game by the user, on a local stack with #12210).

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: #12210 (both edit the debt entry for depth planes in guest memory; this PR goes second and keeps #12210's wording for stencil planes, with sampling added to the points where guest writes are collected)
  • Fixes: none
  • Unblocks: PPSA05684 #4001 (silhouettes following characters through fog and dust)
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER, driver 615.78.08, Arch Linux 7.2.11-zen
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5045131673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-28T04:17:11Z. --- ### What A depth plane with the 64 KiB Z swizzle reloads the units the guest wrote (`SyncFromMemory`, #7154) when its surface is bound as a depth target. Sampling the plane through the surface (`DepthSurfaceTexture`, the single-sample `Sampled` path) skipped that check and read the host image. A title that writes the plane with a buffer copy and samples it before it draws into it again therefore read the depth from before the copy. PPSA05684 (Uncharted: Legacy of Thieves Collection) does this every frame in Uncharted 4: it copies its depth pre-pass into a second depth target with a `buffer_load_format_x` / `buffer_store_format_x` kernel, samples that copy right away to build the half-resolution depth its volumetric fog and dust read, and only later binds the target. The fog used the previous frame's depth, so at the frame rates reached here a dark or light silhouette followed every moving character through fog and dust (the orphanage's closing scene and the prison yard). Sampling a single-sample plane through its surface now runs the same reload first, so the sampled depth includes the guest's writes collected by then. The debt entry for depth planes in guest memory lists sampling among the points where guest writes are collected. ### Tested Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, on `main` 3a2b919816, NVIDIA GeForce RTX 4070 Ti SUPER (615.78.08): - `agc_driver_depth_plane_sampling` gains `GuestWriteAfterDrawCase`. It draws depth 0.25 into a 128x128 D32 plane with the 64 KiB Z swizzle and samples it (0.25), then writes 0.75 into the guest memory under it and samples it again without binding the surface. With `DepthSurface.cpp` from `main` it fails: `D32 plane the guest wrote after a draw, sampled before its surface is bound again: texel 0 read 0x3e800000, expected 0x3f400000`. `DrawDepth` takes an optional depth swizzle for it. - Where guest memory is not write-watched (`APS5_NO_WRITE_WATCH=1`, or a host without a write watch), the driver cannot see the guest's write. The case then prints `guest memory has no write watch: the guest write after a draw is not checked` and returns, like the CPU-write cases of `agc_driver_depth_plane_partial_store`. - `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 VK_LOADER_DEBUG=error,warn,layer ctest -V -R '^agc_driver_depth_plane_sampling'` on `main` 3a2b919816 and on this branch: 2/2 pass on both, and the layer is inserted in every test. No log reports `was not found but was requested by env var VK_INSTANCE_LAYERS`, and neither log has a `VUID-` line. With `APS5_NO_WRITE_WATCH=1`, the branch also passes 2/2 with no `VUID-` line. - Full `ctest -j8` on the first base (`main` 3ca715ee15): 2620 of 2621 pass. `agc_driver_mesh_barycentric_emulation` fails the same way on `main` on this GPU (`the pixel shader did not emulate barycentrics`). - `tools/check_conventions.py --base origin/main` passes. - Cost: I timed the added `SyncFromMemory` call in the prison yard fight of PPSA05684, where the fog and dust sample the copied depth plane every frame. It ran 6 to 17 thousand times per 10 s at about 1.1 us each, 7 to 16 ms per 10 s in total, so 0.2 to 0.4 ms of a ~260 ms frame. - PPSA05684: the silhouettes in the fog of the orphanage's closing scene and in the dust of the prison yard are gone (checked in game by the user, on a local stack with #12210). ### 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) - [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: #12210 (both edit the debt entry for depth planes in guest memory; this PR goes second and keeps #12210's wording for stencil planes, with sampling added to the points where guest writes are collected) - [x] Fixes: none - [x] Unblocks: PPSA05684 #4001 (silhouettes following characters through fog and dust) - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER, driver 615.78.08, Arch Linux 7.2.11-zen - [x] AI-assisted: yes
MarcZX10 commented 2026-10-28 04:20:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:20:31Z.

Review: CHANGES_REQUESTED


The targeted test and negative-control evidence are strong, and the one-line reload is placed before the sampled image is consumed. I did not find a separate functional defect in the changed path. This branch is 38 commits behind current main, though; the repository contribution rules require PR branches to stay up to date. Please rebase/update on current main (including resolving the debt-file overlap with #12210 if it is still open), rerun the focused depth-plane test and validation checks on the rebased head, then I can reassess.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677749635 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238#pullrequestreview-5677749635). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T04:20:31Z. **Review:** CHANGES_REQUESTED --- The targeted test and negative-control evidence are strong, and the one-line reload is placed before the sampled image is consumed. I did not find a separate functional defect in the changed path. This branch is 38 commits behind current `main`, though; the repository contribution rules require PR branches to stay up to date. Please rebase/update on current `main` (including resolving the debt-file overlap with #12210 if it is still open), rerun the focused depth-plane test and validation checks on the rebased head, then I can reassess.
MarcZX10 commented 2026-10-28 04:52:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:52:31Z.

Review: CHANGES_REQUESTED


I found one additional ordering case to address alongside the requested rebase and focused retest.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677935379 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238#pullrequestreview-5677935379). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T04:52:31Z. **Review:** CHANGES_REQUESTED --- I found one additional ordering case to address alongside the requested rebase and focused retest.
adriadam10 commented 2026-10-28 11:23:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T11:23:09Z.

Review: CHANGES_REQUESTED


The change is the right one: DepthSurface.cpp:2491 runs the same SyncFromMemory behind the same mirrorable gate as the bind path, and the new test pins 0x3f400000 after a guest write, which main would read as 0x3e800000. Two things before it can land:

  • Cost: as @oneandonlydean asked, the check now runs on every sample of a single-sample D32 plane (a CollectWrites plus a generation compare when nothing was written, a unit reload when something was). Please add a profile line or a before/after frame time (APS5_PROFILE_DRAW or APS5_TRACE_SUMMARY_EVERY) from a scene that samples such a plane many times per frame, or say how you measured it as negligible.
  • The ordering case @MarcZX10 raised (memoized CollectWrites hiding a CPU write before FlushDepthPlanes stores a stale plane) is in code this PR does not change: FlushDepthPlanes already calls SyncFromMemory (DepthSurface.cpp:2221) and the new line only adds a second caller. If you can show it is a real problem, it belongs in its own PR; please say in the thread whether the sample path makes it reachable in a way the bind path does not.

The branch also conflicts with main (the Depth planes in guest memory debt entry, which #12210 edits too); we will do that rebase once the cost line is in.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681823593 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238#pullrequestreview-5681823593). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T11:23:09Z. **Review:** CHANGES_REQUESTED --- The change is the right one: `DepthSurface.cpp:2491` runs the same `SyncFromMemory` behind the same `mirrorable` gate as the bind path, and the new test pins `0x3f400000` after a guest write, which `main` would read as `0x3e800000`. Two things before it can land: - Cost: as @oneandonlydean asked, the check now runs on every sample of a single-sample D32 plane (a `CollectWrites` plus a generation compare when nothing was written, a unit reload when something was). Please add a profile line or a before/after frame time (`APS5_PROFILE_DRAW` or `APS5_TRACE_SUMMARY_EVERY`) from a scene that samples such a plane many times per frame, or say how you measured it as negligible. - The ordering case @MarcZX10 raised (memoized `CollectWrites` hiding a CPU write before `FlushDepthPlanes` stores a stale plane) is in code this PR does not change: `FlushDepthPlanes` already calls `SyncFromMemory` (`DepthSurface.cpp:2221`) and the new line only adds a second caller. If you can show it is a real problem, it belongs in its own PR; please say in the thread whether the sample path makes it reachable in a way the bind path does not. The branch also conflicts with `main` (the `Depth planes in guest memory` debt entry, which #12210 edits too); we will do that rebase once the cost line is in.
MarcZX10 commented 2026-10-29 00:41:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T00:41:29Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The new write-watch test and its no-write-watch skip address the reported test-environment issue, and the reported GPU run includes a main-code negative control; before this branch is ready, update it against current main and resolve its declared ordering with #12210.

What I ran

  • Inspected PR head 78c1cb46e4017ab6865fa09df261148882ecfc38, its diff, current main (3a2b91981651569163768854a1fcdc7193b6667b), and the repository branch and AGC testing rules.
  • Checked the current GitHub status checks and ran git diff --check against the merge base.
  • Compared the branch with current main using Git's merge-tree preview. No conflicted merge was reported, but the branch is 233 commits behind and both branches edit the AGC debt file.

Results

Check Result
Windows CI
Raw statuswindows pass
Conventions CI
Raw statusconventions pass
Other hosted jobs
Raw statuslinux skipping; register-context skipping; relinker skipping; relinker-asan skipping; write-watch skipping
Local whitespace check
Raw outputgit diff --check upstream/main...refs/review/pr-12238 — no output; exit status 0
Merge preview
Raw outputmerge-tree-exit=0; preview has edits from both branches in docs/dev/debt/libSceAgcDriver.md

Negative control

The author reports that agc_driver_depth_plane_sampling and _evicting passed with validation enabled on the candidate, and that the added sampling case failed with DepthSurface.cpp from main (0x3e800000, expected 0x3f400000). I have not independently run those GPU tests in this review.

Findings

Blocking

  • The branch is 233 commits behind current main. CONTRIBUTING.md requires branches to be kept up to date. Please update it and rerun the focused AGC tests and checks on the resulting head.
  • The checklist says Depends on: none, but #12210 is still open and both PRs edit docs/dev/debt/libSceAgcDriver.md. Your comment says the combined result requires one branch's debt wording to be retained and “sample” added. The contribution rules require agreement on ordering and a matching Depends on: #12210 (or the agreed reverse order). Please coordinate and declare the agreed order.

Non-blocking

  • A reviewer asked for a cost measurement because this adds a write collection/generation check on sampled depth planes. The latest test report does not include a profile or frame-time comparison. Please add one, or explain with measured evidence why the added work is negligible.

Sources

  • PR diff and checks reviewed at 78c1cb46e4017ab6865fa09df261148882ecfc38.
  • Current base: main at 3a2b91981651569163768854a1fcdc7193b6667b.
  • Contribution rules: CONTRIBUTING.md at 3a2b91981651569163768854a1fcdc7193b6667b.

Not verified

I did not run a local build, focused GPU tests, or console tests. Linux and specialized hosted checks were skipped. The reported GPU results are author-provided and are not a substitute for rerunning them after updating this branch.

To get this to a good state

Agree with #12210 on merge order, set the corresponding dependency on the checklist, update this branch to current main, then rerun and report the required focused tests and validation checks on the new head. Add the requested cost measurement if available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5691501887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238#pullrequestreview-5691501887). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T00:41:29Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The new write-watch test and its no-write-watch skip address the reported test-environment issue, and the reported GPU run includes a main-code negative control; before this branch is ready, update it against current `main` and resolve its declared ordering with #12210. ## What I ran - Inspected PR head `78c1cb46e4017ab6865fa09df261148882ecfc38`, its diff, current `main` (`3a2b91981651569163768854a1fcdc7193b6667b`), and the repository branch and AGC testing rules. - Checked the current GitHub status checks and ran `git diff --check` against the merge base. - Compared the branch with current `main` using Git's merge-tree preview. No conflicted merge was reported, but the branch is 233 commits behind and both branches edit the AGC debt file. ## Results | Check | Result | |---|---| | Windows CI | <details><summary>Raw status</summary><code>windows pass</code></details> | | Conventions CI | <details><summary>Raw status</summary><code>conventions pass</code></details> | | Other hosted jobs | <details><summary>Raw status</summary><code>linux skipping; register-context skipping; relinker skipping; relinker-asan skipping; write-watch skipping</code></details> | | Local whitespace check | <details><summary>Raw output</summary><code>git diff --check upstream/main...refs/review/pr-12238</code> — no output; exit status 0</details> | | Merge preview | <details><summary>Raw output</summary><code>merge-tree-exit=0</code>; preview has edits from both branches in <code>docs/dev/debt/libSceAgcDriver.md</code></details> | ## Negative control The author reports that `agc_driver_depth_plane_sampling` and `_evicting` passed with validation enabled on the candidate, and that the added sampling case failed with `DepthSurface.cpp` from `main` (`0x3e800000`, expected `0x3f400000`). I have not independently run those GPU tests in this review. ## Findings ### Blocking - The branch is 233 commits behind current `main`. `CONTRIBUTING.md` requires branches to be kept up to date. Please update it and rerun the focused AGC tests and checks on the resulting head. - The checklist says `Depends on: none`, but #12210 is still open and both PRs edit `docs/dev/debt/libSceAgcDriver.md`. Your comment says the combined result requires one branch's debt wording to be retained and “sample” added. The contribution rules require agreement on ordering and a matching `Depends on: #12210` (or the agreed reverse order). Please coordinate and declare the agreed order. ### Non-blocking - A reviewer asked for a cost measurement because this adds a write collection/generation check on sampled depth planes. The latest test report does not include a profile or frame-time comparison. Please add one, or explain with measured evidence why the added work is negligible. ## Sources - PR diff and checks reviewed at `78c1cb46e4017ab6865fa09df261148882ecfc38`. - Current base: `main` at `3a2b91981651569163768854a1fcdc7193b6667b`. - Contribution rules: `CONTRIBUTING.md` at `3a2b91981651569163768854a1fcdc7193b6667b`. ## Not verified I did not run a local build, focused GPU tests, or console tests. Linux and specialized hosted checks were skipped. The reported GPU results are author-provided and are not a substitute for rerunning them after updating this branch. ## To get this to a good state Agree with #12210 on merge order, set the corresponding dependency on the checklist, update this branch to current `main`, then rerun and report the required focused tests and validation checks on the new head. Add the requested cost measurement if available.
MarcZX10 commented 2026-10-29 05:49:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:49:31Z.

Review: CHANGES_REQUESTED


Re-checked — changes requested

The new sampled-depth reload and write-watch-gated regression address the previous test-scope concern, but this branch is still 36 commits behind current main 52c09d567d2fa453494dd720fcf53a98149468e4. The repository checklist requires a current-main base, and the reported tests are not on the refreshed integration.

What I ran

  • Reviewed exact head 29a96bfc5d1fa648096d68d6a2e4535d20b23bbc against current main.
  • Ran git merge-tree --write-tree and git diff --check.
  • Inspected the production/test diff and checked the author’s reports.

Results

Check Result
Current-main merge preview Clean; current main is not in the branch history
git diff --check Passed
Focused GPU tests Existing reports are against the earlier 3a2b919816 base
Current-main validation run Not reported

Please update onto current main and rerun the focused sampled-depth tests and required validation-layer comparison on that head. I did not run the C++ or GPU tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693412360 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12238#pullrequestreview-5693412360). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T05:49:31Z. **Review:** CHANGES_REQUESTED --- # Re-checked — changes requested The new sampled-depth reload and write-watch-gated regression address the previous test-scope concern, but this branch is still 36 commits behind current `main` `52c09d567d2fa453494dd720fcf53a98149468e4`. The repository checklist requires a current-main base, and the reported tests are not on the refreshed integration. ## What I ran - Reviewed exact head `29a96bfc5d1fa648096d68d6a2e4535d20b23bbc` against current `main`. - Ran `git merge-tree --write-tree` and `git diff --check`. - Inspected the production/test diff and checked the author’s reports. ## Results | Check | Result | |---|---| | Current-main merge preview | Clean; current `main` is not in the branch history | | `git diff --check` | Passed | | Focused GPU tests | Existing reports are against the earlier `3a2b919816` base | | Current-main validation run | Not reported | Please update onto current `main` and rerun the focused sampled-depth tests and required validation-layer comparison on that head. I did not run the C++ or GPU tests locally.
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!12238
No description provided.