fix(agc): store a written storage image over a depth plane into the surface #5113

Merged
XairRrR merged 2 commits from upstream-pr/5113 into main 2026-10-14 18:17:41 +00:00
XairRrR commented 2026-10-13 12:03:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-13T12:03:46Z.


What

DepthSurfaceStoreBack only took the storage image a depth surface had handed out for its plane (DepthSurfaceStorage). A storage image over the same plane that the surface did not hand out was written but never reached the depth image, so later depth tests and plane reads used the surface's old contents. For example:

  • an image created and cached before the depth surface existed;
  • an image a dispatch keeps because its resources revalidate from their records.

Changes in Graphics/src/DepthSurface.cpp:

  • A written storage image that exactly covers a depth surface's depth or stencil plane is now copied into that plane when it is stored back, the same way the surface's own view is. "Exactly" means the plane's address and extent, level 0, one 2D layer (or a one-layer 2D array at slice 0), and the plane's 32-bit float, 16-bit unorm or 8-bit uint format.
  • The surface's write serial moves, so plane copies refresh.
  • A view that the surface's own view replaced still throws, as before.
  • copyPlane takes the storage image to copy from or to, instead of always the plane's own one.

Test in tests/execution/DepthStorage.cpp:

  • a storage image created over the depth plane with StorageTexture::Create, filled with 0.125 and stored back, reaches the depth surface (read back through the surface's own storage view);
  • a storage image of another extent over the same memory is not taken.

The first check fails on main.

Uncharted: Legacy of Thieves Collection (PPSA05684) writes a 640x360 downsampled depth with a compute shader, then tests its mist particles against that memory as a depth target. The storage image was created before the depth surface, and the dispatch kept reusing it, so the depth target stayed at its clear value and the mist drew over the foreground cage in the main menu. On that title's branch, an equivalent local change (making such a written storage image the plane's writer, taken at the next bind) puts the mist behind the cage. That branch carries other depth work, so this PR's exact code was verified by the test, not in the title.

Related: #454 (retiring a surface when another writer covers it) and #5003 (2D array storage views of a depth plane) also touch storage access to depth planes. Neither stores an outside view back into the plane.

Tested

  • Arch Linux (kernel 7.2.9), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.78.08), Release with BUILD_TESTING=ON.
  • Branch built on main ff8fe4dbc. Full ctest: 1041/1041. One run hit an unrelated failure in guest_kernel_fcntl, which passed when rerun. Conventions check passed.
  • Title: Uncharted: Legacy of Thieves Collection (PPSA05684), main menu, on its own bring-up branch, because plain main does not run it yet.

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 in TechnicalDebt
  • 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
  • Unblocks: PPSA05684 #4001
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4842924811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5113). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-13T12:03:46Z. --- ### What `DepthSurfaceStoreBack` only took the storage image a depth surface had handed out for its plane (`DepthSurfaceStorage`). A storage image over the same plane that the surface did not hand out was written but never reached the depth image, so later depth tests and plane reads used the surface's old contents. For example: - an image created and cached before the depth surface existed; - an image a dispatch keeps because its resources revalidate from their records. Changes in `Graphics/src/DepthSurface.cpp`: - A written storage image that exactly covers a depth surface's depth or stencil plane is now copied into that plane when it is stored back, the same way the surface's own view is. "Exactly" means the plane's address and extent, level 0, one 2D layer (or a one-layer 2D array at slice 0), and the plane's 32-bit float, 16-bit unorm or 8-bit uint format. - The surface's write serial moves, so plane copies refresh. - A view that the surface's own view replaced still throws, as before. - `copyPlane` takes the storage image to copy from or to, instead of always the plane's own one. Test in `tests/execution/DepthStorage.cpp`: - a storage image created over the depth plane with `StorageTexture::Create`, filled with 0.125 and stored back, reaches the depth surface (read back through the surface's own storage view); - a storage image of another extent over the same memory is not taken. The first check fails on `main`. Uncharted: Legacy of Thieves Collection (PPSA05684) writes a 640x360 downsampled depth with a compute shader, then tests its mist particles against that memory as a depth target. The storage image was created before the depth surface, and the dispatch kept reusing it, so the depth target stayed at its clear value and the mist drew over the foreground cage in the main menu. On that title's branch, an equivalent local change (making such a written storage image the plane's writer, taken at the next bind) puts the mist behind the cage. That branch carries other depth work, so this PR's exact code was verified by the test, not in the title. Related: #454 (retiring a surface when another writer covers it) and #5003 (2D array storage views of a depth plane) also touch storage access to depth planes. Neither stores an outside view back into the plane. ### Tested - Arch Linux (kernel 7.2.9), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.78.08), Release with `BUILD_TESTING=ON`. - Branch built on main ff8fe4dbc. Full ctest: 1041/1041. One run hit an unrelated failure in `guest_kernel_fcntl`, which passed when rerun. Conventions check passed. - Title: Uncharted: Legacy of Thieves Collection (PPSA05684), main menu, on its own bring-up branch, because plain `main` does not run it yet. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: PPSA05684 #4001 - [x] AI-assisted: yes
kr4y4 commented 2026-10-13 14:58:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T14:58:54Z.


First Windows and Intel run.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790). The head dfc28bd55 merged into main 0f2683cb7, which passes 1069 of 1069 on both GPUs.

  • agc_driver_depth_storage, --repeat until-fail:3: passes on both GPUs.
  • Full suite: 1069 of 1069 on both GPUs.
  • With DepthSurface.cpp from main, agc_driver_depth_storage fails on both GPUs with AGC graphics: a storage image over the depth plane that the depth surface did not hand out was not stored back.
  • It merges cleanly with #5111.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148481173 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5113#issuecomment-6148481173). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-13T14:58:54Z. --- First Windows and Intel run. Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790). The head dfc28bd55 merged into `main` 0f2683cb7, which passes 1069 of 1069 on both GPUs. - `agc_driver_depth_storage`, `--repeat until-fail:3`: passes on both GPUs. - Full suite: 1069 of 1069 on both GPUs. - With `DepthSurface.cpp` from `main`, `agc_driver_depth_storage` fails on both GPUs with `AGC graphics: a storage image over the depth plane that the depth surface did not hand out was not stored back`. - It merges cleanly with #5111. Written with AI assistance.
nikowolf123 commented 2026-10-13 16:05:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T16:05:16Z.


DepthSurfaceStoreBack goes through surfaces() oldest first and stops at the first surface whose StoreBack returns true (DepthSurface.cpp L712-L718). The new CoversPlane loop (L204-L211) lets an older surface with the same depth address, extent and format take the copy before the loop reaches the surface that handed the view out.

Such surfaces exist when one depth plane is bound with different stencil planes, because sameSurface includes the stencil address in its key (L460-L462). #4683 reports that PPSA05684 binds one depth plane with three stencil planes. DepthSurfaceTexture (L609), DepthSurfaceAt (L690) and DepthSurfaceStorage (L706) all resolve a plane address to the newest surface (rbegin), so they never see a copy made into an older one.

Both of these work on main and break with this PR:

  1. A surface writes through its own storage view and stores it back. The copy goes into the older pairing, and the pairing that handed the view out keeps its old depth.
  2. Storing back through a replaced view no longer throws when an older pairing exists. The older pairing's CoversPlane accepts the view before the owning surface's replaced check runs. The PR description says this case still throws.

An outside image takes the same loop, so it also lands in the oldest pairing.

Repro: a copy of tests/execution/DepthStorage.cpp with this Run:

const DepthTarget first{address, address + BlockBytes / 2, {Side, Side}, VK_FORMAT_D32_SFLOAT_S8_UINT, 0.25f, 7};
const DepthTarget second{address, address + BlockBytes / 2 + 8192, {Side, Side}, VK_FORMAT_D32_SFLOAT_S8_UINT, 0.5f, 3};
DepthSurfaceView(context, first);
DepthSurfaceView(context, second);
const auto depth = DepthSurfaceStorage(context, {}, PlaneResource(address, Format32Float), 0); // second's view, reads 0.5
fill(*depth, 0.75f);
DepthSurfaceStoreBack(context, *depth);
// read back through DepthSurfaceStorage(...) again and count 0.75
auto layered = PlaneResource(address, Format32Float);
layered.dimension = TextureDimension::k2DArray;
DepthSurfaceStorage(context, {}, layered, 0); // replaces `depth` on the second pairing
DepthSurfaceStoreBack(context, *depth);       // should throw

Results (WSL2 Ubuntu, GCC 15.2, CI CMake configuration, Release, lavapipe):

  • main fdefd7660: the read back has 4096 texels of 0.75. The second store back throws ... through a view that another view of the plane ... replaced are not implemented.
  • This PR merged onto fdefd7660: the read back has 4096 texels of 0.5 (the second pairing's clear value). The second store back returns true and does not throw.
  • The PR's own agc_driver_depth_storage passes on the merge.

A fix I tried locally: first run the identity and replaced checks on every surface, then run the CoversPlane copy newest first, in the same order DepthSurfaceStorage uses:

for (const auto& surface : surfaces())
    if (surface->context.device == context.device && surface->StoreBack(storage)) return true; // own view, or throw for a replaced one
for (auto it = surfaces().rbegin(); it != surfaces().rend(); ++it)
    if ((*it)->context.device == context.device && (*it)->StoreBackCovering(storage)) return true; // the new CoversPlane loop, moved out of StoreBack

With that change (+8 lines), the PR's test and the repro both pass.

AI-assisted: written with Claude Code; I checked the code paths and ran what is described above.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149790642 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5113#issuecomment-6149790642). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-13T16:05:16Z. --- `DepthSurfaceStoreBack` goes through `surfaces()` oldest first and stops at the first surface whose `StoreBack` returns true ([DepthSurface.cpp L712-L718](https://github.com/boykopovar/AnyPS5/blob/dfc28bd552c2d6313633c322177907fed6f62ece/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp#L712-L718)). The new `CoversPlane` loop ([L204-L211](https://github.com/boykopovar/AnyPS5/blob/dfc28bd552c2d6313633c322177907fed6f62ece/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp#L204-L211)) lets an older surface with the same depth address, extent and format take the copy before the loop reaches the surface that handed the view out. Such surfaces exist when one depth plane is bound with different stencil planes, because `sameSurface` includes the stencil address in its key ([L460-L462](https://github.com/boykopovar/AnyPS5/blob/dfc28bd552c2d6313633c322177907fed6f62ece/core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp#L460-L462)). #4683 reports that PPSA05684 binds one depth plane with three stencil planes. `DepthSurfaceTexture` (L609), `DepthSurfaceAt` (L690) and `DepthSurfaceStorage` (L706) all resolve a plane address to the newest surface (`rbegin`), so they never see a copy made into an older one. Both of these work on `main` and break with this PR: 1. A surface writes through its own storage view and stores it back. The copy goes into the older pairing, and the pairing that handed the view out keeps its old depth. 2. Storing back through a replaced view no longer throws when an older pairing exists. The older pairing's `CoversPlane` accepts the view before the owning surface's replaced check runs. The PR description says this case still throws. An outside image takes the same loop, so it also lands in the oldest pairing. Repro: a copy of `tests/execution/DepthStorage.cpp` with this `Run`: ```cpp const DepthTarget first{address, address + BlockBytes / 2, {Side, Side}, VK_FORMAT_D32_SFLOAT_S8_UINT, 0.25f, 7}; const DepthTarget second{address, address + BlockBytes / 2 + 8192, {Side, Side}, VK_FORMAT_D32_SFLOAT_S8_UINT, 0.5f, 3}; DepthSurfaceView(context, first); DepthSurfaceView(context, second); const auto depth = DepthSurfaceStorage(context, {}, PlaneResource(address, Format32Float), 0); // second's view, reads 0.5 fill(*depth, 0.75f); DepthSurfaceStoreBack(context, *depth); // read back through DepthSurfaceStorage(...) again and count 0.75 auto layered = PlaneResource(address, Format32Float); layered.dimension = TextureDimension::k2DArray; DepthSurfaceStorage(context, {}, layered, 0); // replaces `depth` on the second pairing DepthSurfaceStoreBack(context, *depth); // should throw ``` Results (WSL2 Ubuntu, GCC 15.2, CI CMake configuration, Release, lavapipe): - `main` fdefd7660: the read back has 4096 texels of 0.75. The second store back throws `... through a view that another view of the plane ... replaced are not implemented`. - This PR merged onto fdefd7660: the read back has 4096 texels of 0.5 (the second pairing's clear value). The second store back returns true and does not throw. - The PR's own `agc_driver_depth_storage` passes on the merge. A fix I tried locally: first run the identity and replaced checks on every surface, then run the `CoversPlane` copy newest first, in the same order `DepthSurfaceStorage` uses: ```cpp for (const auto& surface : surfaces()) if (surface->context.device == context.device && surface->StoreBack(storage)) return true; // own view, or throw for a replaced one for (auto it = surfaces().rbegin(); it != surfaces().rend(); ++it) if ((*it)->context.device == context.device && (*it)->StoreBackCovering(storage)) return true; // the new CoversPlane loop, moved out of StoreBack ``` With that change (+8 lines), the PR's test and the repro both pass. AI-assisted: written with Claude Code; I checked the code paths and ran what is described above.
XairRrR commented 2026-10-13 16:52:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-13T16:52:56Z.


@nikowolf123 thanks, you're right on both points. I pushed 6d1893f40 with your fix:

  • StoreBack now only takes the surface's own view or throws for a view that another view replaced, and that check runs over every surface first.
  • The covering copy moved to StoreBackCovering, which DepthSurfaceStoreBack calls newest first, in the order DepthSurfaceStorage resolves a plane.

Your repro is now the RunPairings pass of tests/execution/DepthStorage.cpp: one depth plane bound with two stencil planes, a store back through the newest pairing's view, a read back through DepthSurfaceStorage, and a store back through the replaced view that must still throw.

  • With 6d1893f40, agc_driver_depth_storage passes.
  • With only DepthSurface.cpp reverted to dfc28bd55, it fails with a storage image stored back went into an older pairing instead of the pairing that handed it out.
  • On the branch merged into main 0214069e3 (Linux, RTX GPU, NVIDIA driver, Release), all 487 agc tests pass (agc_driver_stencil_ref_export is skipped), and check_conventions.py passes.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150674370 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5113#issuecomment-6150674370). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-13T16:52:56Z. --- @nikowolf123 thanks, you're right on both points. I pushed 6d1893f40 with your fix: - `StoreBack` now only takes the surface's own view or throws for a view that another view replaced, and that check runs over every surface first. - The covering copy moved to `StoreBackCovering`, which `DepthSurfaceStoreBack` calls newest first, in the order `DepthSurfaceStorage` resolves a plane. Your repro is now the `RunPairings` pass of `tests/execution/DepthStorage.cpp`: one depth plane bound with two stencil planes, a store back through the newest pairing's view, a read back through `DepthSurfaceStorage`, and a store back through the replaced view that must still throw. - With 6d1893f40, `agc_driver_depth_storage` passes. - With only `DepthSurface.cpp` reverted to dfc28bd55, it fails with `a storage image stored back went into an older pairing instead of the pairing that handed it out`. - On the branch merged into `main` 0214069e3 (Linux, RTX GPU, NVIDIA driver, Release), all 487 agc tests pass (`agc_driver_stencil_ref_export` is skipped), and `check_conventions.py` passes. AI-assisted: yes
adriadam10 commented 2026-10-14 17:57:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:57:37Z.


Rebased onto current main (one conflict in tests/execution/DepthStorage.cpp, next to the new layered-view checks; kept both). Checked: agc_driver_depth_storage* pass, the new check fails with DepthSurface.cpp from main, the pairing order fix from the earlier review is covered by RunPairings, and the rest of the scoped ctest passes except agc_driver_tessellation, which crashes inside RADV's shader compiler and doesn't touch this code. Windows/Intel run by @kr4y4 also green. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174846217 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5113#issuecomment-6174846217). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:57:37Z. --- Rebased onto current `main` (one conflict in `tests/execution/DepthStorage.cpp`, next to the new layered-view checks; kept both). Checked: `agc_driver_depth_storage*` pass, the new check fails with `DepthSurface.cpp` from `main`, the pairing order fix from the earlier review is covered by `RunPairings`, and the rest of the scoped ctest passes except `agc_driver_tessellation`, which crashes inside RADV's shader compiler and doesn't touch this code. Windows/Intel run by @kr4y4 also green. Merging once the full Build is green.
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!5113
No description provided.