fix(agc): take a depth plane a shader wrote through its own view from that image, not from stale guest bytes #23739

Open
yinx080 wants to merge 3 commits from upstream-pr/23739 into main
yinx080 commented 2026-11-19 15:08:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by yinx080 on 2026-11-19T15:08:39Z.


What

The Exit 8 (PPSA23276, UE5 with Nanite) renders most walls and floors black on main: the G-buffer stays empty there, so lighting has nothing to shade.

The draws that fail are UE5's classic Nanite material pass:

  • Each material draws at depth materialId / 16383.
  • The draw tests EQUAL against the Nanite material depth, a separate D32 target.

The material depth is written by Nanite's emit pass, a compute shader, through the title's own R32F storage view of the depth memory. That memory is Z64KBX, the same surface as the depth surface's mirror.

Right after the emit, that view holds the correct material ids on every wall and floor pixel. Then this happens:

  1. The view's write-back is empty: its layerGeneration is still the one from its first upload, every unit has been stamped since, so every unit counts as written by someone else and the results are dropped without a store.
  2. At the next bind, the depth surface sees every unit changed and refreshes its own mirror from guest memory. The mirror is a second StorageTexture with the same surface.
  3. The mirror gets the bytes of whatever transient resource used that memory before, in whole 64 KiB units.
  4. The EQUAL test fails on most of the screen.

This change takes each changed unit from the image that wrote it last, and only while it is the last writer:

  • A write-back records the stamp it makes over each tracked unit (StorageTexture::stampStored replaces the three MarkWritten loops of writeBack), and markLayersPending records the tracker generation of each unit's latest results (layerResultsSince).
  • StorageTexture::DepthPlaneWriters looks at the live images with exactly the mirror's surface at the same address. A changed unit goes to the image whose latest results or own write-back is newer than every stamp on the unit's block; when several qualify, the newest wins.
  • DepthSurface::SyncFromMemory copies those units on the device from their images, and every other changed unit from the mirror, as before. A CPU store, a DMA copy or another image's write-back made after the shader write stamps the block newer, so the guest bytes win there.
  • APS5_NO_DEPTH_FROM_ALIAS restores the old path; its row in docs/dev/ENVIRONMENT.md describes the rule.
  • New tests agc_driver_depth_plane_alias_* (IvanFogel's fixture from the review): a shader write through an R32F view, then a CPU store or a PM4 DMA_DATA copy into one unit, with no draw first, a draw in between (_split) or a bind before the write (_prebind). They skip when the plane is not write-watched or its block generations are unavailable.

Both references are needed. The emit view in The Exit 8 never stores (step 1), so only its results time shows it wrote last; with the write-back stamp alone the corridor stays 54-100 % black. The write-back stamp covers an image whose results did reach memory, since its own stamp is newer than its results.

Overlap with #15621. #15621 changes SyncFromMemory and Texture.hpp too, so one of the two will need a rebase after the other. It doesn't fix this case on its own: main + #15621 rendered 31-100 % of the corridor near-black.

Root cause. The empty write-back in step 1 is why guest memory never receives the results. This change works around it for depth planes; if the write-back's generation check is the place to fix it, I can look at that in a separate PR.

Tested

Windows 11 Pro (26200), Ryzen 7 5800H, RTX 3060 Laptop (NVIDIA 610.91), WinLibs GCC 15.2.0 r7, Release, main 07336cc200 + this branch (5d622cc26c).

  • The Exit 8, 1080p, opening corridor. A 200 s scripted walk with a window shot every 30 s; the share of near-black pixels (max channel < 12) in the lower part of the screen, where the walls and floor are:

    30 s 60 s 90 s 120 s 150 s
    APS5_NO_DEPTH_FROM_ALIAS=1 54.1 % 70.3 % 100 % 100 % 100 %
    this PR 0.6 % 0.5 % 0.0 % 0.0 % 0.0 %

    The 180 s shot of the last run failed to capture; the run went the full 200 s. Walls, floor, tiles, posters and the guide line render with correct occlusion, and the frame rate matches the path-off run: about 58-74 presents per 10 s after warm-up in both, with an occasional dip in each.

  • The new cases, 10 runs each:

    Case pass / skip / fail
    _split (control, CPU, DMA) 10 / 0 / 0 each
    control_prebind 4 / 6 / 0
    cpu_prebind 5 / 5 / 0
    dma_prebind 4 / 6 / 0
    no draw first (control, CPU, DMA) 0 / 10 / 0 each

    On Windows the write watch stops covering the plane after the storage write-back in every run without a draw first and in about half the _prebind runs, with or without this PR; those runs skip. Before the rework, cpu_split, dma_split, cpu_prebind and dma_prebind failed on the draw assertion and passed with APS5_NO_DEPTH_FROM_ALIAS=1, matching IvanFogel's Linux run.

  • Unit tests. ctest -R "^agc_driver_depth": 54 of 54 pass (45 existing + 9 new), and the same with APS5_NO_DEPTH_FROM_ALIAS=1.

  • Stray (PPSA02100), regression check, on the previous head dbb10a3638: 5.68 fps with APS5_NO_DEPTH_FROM_ALIAS=1, 5.69 fps with it on. Not rerun on this head.

  • tools/check_conventions.py --base origin/main and tools/check_test_registration.py build pass.

  • Not run: Linux, the validation layer.

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 (overlaps #15621, see above)
  • Fixes: none
  • Unblocks: PPSA23276 #8587
  • GPUs tested: NVIDIA GeForce RTX 3060 Laptop, driver 610.91, Windows 11
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5397470092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23739). Originally posted by [yinx080](https://github.com/yinx080) on 2026-11-19T15:08:39Z. --- ### What The Exit 8 (PPSA23276, UE5 with Nanite) renders most walls and floors black on `main`: the G-buffer stays empty there, so lighting has nothing to shade. The draws that fail are UE5's classic Nanite material pass: - Each material draws at depth `materialId / 16383`. - The draw tests EQUAL against the Nanite *material depth*, a separate D32 target. The material depth is written by Nanite's emit pass, a compute shader, through the title's own R32F storage view of the depth memory. That memory is Z64KBX, the same surface as the depth surface's mirror. Right after the emit, that view holds the correct material ids on every wall and floor pixel. Then this happens: 1. The view's write-back is empty: its `layerGeneration` is still the one from its first upload, every unit has been stamped since, so every unit counts as written by someone else and the results are dropped without a store. 2. At the next bind, the depth surface sees every unit changed and refreshes its own mirror from guest memory. The mirror is a second `StorageTexture` with the same surface. 3. The mirror gets the bytes of whatever transient resource used that memory before, in whole 64 KiB units. 4. The EQUAL test fails on most of the screen. This change takes each changed unit from the image that wrote it last, and only while it is the last writer: - A write-back records the stamp it makes over each tracked unit (`StorageTexture::stampStored` replaces the three `MarkWritten` loops of `writeBack`), and `markLayersPending` records the tracker generation of each unit's latest results (`layerResultsSince`). - `StorageTexture::DepthPlaneWriters` looks at the live images with exactly the mirror's surface at the same address. A changed unit goes to the image whose latest results or own write-back is newer than every stamp on the unit's block; when several qualify, the newest wins. - `DepthSurface::SyncFromMemory` copies those units on the device from their images, and every other changed unit from the mirror, as before. A CPU store, a DMA copy or another image's write-back made after the shader write stamps the block newer, so the guest bytes win there. - `APS5_NO_DEPTH_FROM_ALIAS` restores the old path; its row in `docs/dev/ENVIRONMENT.md` describes the rule. - New tests `agc_driver_depth_plane_alias_*` (IvanFogel's fixture from the review): a shader write through an R32F view, then a CPU store or a PM4 `DMA_DATA` copy into one unit, with no draw first, a draw in between (`_split`) or a bind before the write (`_prebind`). They skip when the plane is not write-watched or its block generations are unavailable. Both references are needed. The emit view in The Exit 8 never stores (step 1), so only its results time shows it wrote last; with the write-back stamp alone the corridor stays 54-100 % black. The write-back stamp covers an image whose results did reach memory, since its own stamp is newer than its results. **Overlap with #15621.** #15621 changes `SyncFromMemory` and `Texture.hpp` too, so one of the two will need a rebase after the other. It doesn't fix this case on its own: `main` + #15621 rendered 31-100 % of the corridor near-black. **Root cause.** The empty write-back in step 1 is why guest memory never receives the results. This change works around it for depth planes; if the write-back's generation check is the place to fix it, I can look at that in a separate PR. ### Tested Windows 11 Pro (26200), Ryzen 7 5800H, RTX 3060 Laptop (NVIDIA 610.91), WinLibs GCC 15.2.0 r7, Release, `main` `07336cc200` + this branch (`5d622cc26c`). - **The Exit 8, 1080p, opening corridor.** A 200 s scripted walk with a window shot every 30 s; the share of near-black pixels (max channel < 12) in the lower part of the screen, where the walls and floor are: | | 30 s | 60 s | 90 s | 120 s | 150 s | |---|---|---|---|---|---| | `APS5_NO_DEPTH_FROM_ALIAS=1` | 54.1 % | 70.3 % | 100 % | 100 % | 100 % | | this PR | 0.6 % | 0.5 % | 0.0 % | 0.0 % | 0.0 % | The 180 s shot of the last run failed to capture; the run went the full 200 s. Walls, floor, tiles, posters and the guide line render with correct occlusion, and the frame rate matches the path-off run: about 58-74 presents per 10 s after warm-up in both, with an occasional dip in each. - **The new cases**, 10 runs each: | Case | pass / skip / fail | |---|---| | `_split` (control, CPU, DMA) | 10 / 0 / 0 each | | `control_prebind` | 4 / 6 / 0 | | `cpu_prebind` | 5 / 5 / 0 | | `dma_prebind` | 4 / 6 / 0 | | no draw first (control, CPU, DMA) | 0 / 10 / 0 each | On Windows the write watch stops covering the plane after the storage write-back in every run without a draw first and in about half the `_prebind` runs, with or without this PR; those runs skip. Before the rework, `cpu_split`, `dma_split`, `cpu_prebind` and `dma_prebind` failed on the draw assertion and passed with `APS5_NO_DEPTH_FROM_ALIAS=1`, matching IvanFogel's Linux run. - **Unit tests.** `ctest -R "^agc_driver_depth"`: 54 of 54 pass (45 existing + 9 new), and the same with `APS5_NO_DEPTH_FROM_ALIAS=1`. - **Stray (PPSA02100)**, regression check, on the previous head `dbb10a3638`: 5.68 fps with `APS5_NO_DEPTH_FROM_ALIAS=1`, 5.69 fps with it on. Not rerun on this head. - `tools/check_conventions.py --base origin/main` and `tools/check_test_registration.py build` pass. - Not run: Linux, the validation layer. ### 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 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 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 (overlaps #15621, see above) - [x] Fixes: none - [x] Unblocks: PPSA23276 #8587 - [x] GPUs tested: NVIDIA GeForce RTX 3060 Laptop, driver 610.91, Windows 11 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-20 05:07:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T05:07:58Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change based on the source inspected; the reported runtime and test results were not independently run.

What I ran

Reviewed head dbb10a363881dc91d6b4aaa2326233881534fdf7. No commands or tests were run. Read description.md, commits.md, manifest.json, diff.patch, and the complete changed files Texture.hpp, DepthSurface.cpp, and docs/dev/ENVIRONMENT.md; read the changed function and surrounding context in Texture.cpp through the pinned reader. No changed files were excluded by the prepared manifest.

Results

Check Result Note
Diff and changed code review passed Four changed files inspected
Tests and build not run Results in the PR description are author-reported
Runtime behavior not run No console run performed

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of Texture.cpp was not read because it was excluded from the review folder for size; only the changed implementation and relevant context were read through repo_read.py. Tests, build, and reported console behavior were not independently verified. No code-level test coverage for this changed behavior was confirmed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5987801383 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23739#pullrequestreview-5987801383). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T05:07:58Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change based on the source inspected; the reported runtime and test results were not independently run. ## What I ran Reviewed head `dbb10a363881dc91d6b4aaa2326233881534fdf7`. No commands or tests were run. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and the complete changed files `Texture.hpp`, `DepthSurface.cpp`, and `docs/dev/ENVIRONMENT.md`; read the changed function and surrounding context in `Texture.cpp` through the pinned reader. No changed files were excluded by the prepared manifest. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code review | passed | Four changed files inspected | | Tests and build | not run | Results in the PR description are author-reported | | Runtime behavior | not run | No console run performed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full text of `Texture.cpp` was not read because it was excluded from the review folder for size; only the changed implementation and relevant context were read through `repo_read.py`. Tests, build, and reported console behavior were not independently verified. No code-level test coverage for this changed behavior was confirmed. ## To get this to a good state Nothing is required.
IvanFogel commented 2026-11-20 08:44:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IvanFogel on 2026-11-20T08:44:58Z.

Review: CHANGES_REQUESTED


I reproduced the later-writer regression discussed above. With an R32F storage write followed by an ordinary CPU store or a PM4 DMA_DATA copy into one depth tile, guest memory contains the new value but the next depth draw uses the older image. This happens when the depth surface was already bound. The cold first-bind cases pass.

Same nine cases on an RTX 3070 Ti Result
Main e825fa1492 9/9 passed
PR dbb10a3638 merged into that main 5/9 passed
Same PR build with APS5_NO_DEPTH_FROM_ALIAS=1 9/9 passed

In each failing case, 16,399 pixels should pass the depth test. The PR draws 15, losing exactly the 16,384 pixels in the overwritten tile. CPU and DMA stores both advance the tile’s guest generation. Only the CPU store sets the CPU-write flag. A CPU-only gate would still accept stale texels after the DMA copy. I traced all four failures to the new alias-copy branch. Each time it copied one rectangle for tile 2 from alias version 2.

Four existing depth tests passed on both trees. All runs required real Vulkan with the shader cache disabled, logged the Khronos validation layer, and produced no VUID or synchronization diagnostics. Tested on CachyOS Linux, NVIDIA GeForce RTX 3070 Ti, driver 620.70.06. This was a synthetic draw and did not run The Exit 8.

Regression fixture and run instructions

The patch below applies to main e825fa1492 and its merge with this PR. After applying it, configure the usual test build, build agc_driver_depth_plane_partial_store_tests, and run:

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_LOADER_DEBUG=layer VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R '^peer_23739_' -V
--- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp
+++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp
@@ -1,3 +1,4 @@
+#include "prx/libSceAgcDriver/Execution/include/Pm4.hpp"
 #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
 #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
 #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp"
@@ -126,6 +127,62 @@
 
 }
 
+constexpr std::uint32_t Threads = 32;
+constexpr std::uint32_t Format8Uint = 5;
+constexpr std::uint32_t Format16Unorm = 7;
+constexpr std::uint32_t Format32Float = 22;
+constexpr std::uint32_t TileDepth64KB = 0x18;
+constexpr std::uint32_t Type2D = 9;
+constexpr std::uint32_t Type2DArray = 13;
+
+
+constexpr std::uint32_t RowPasses = 15;
+
+alignas(256) constexpr std::array<std::uint32_t, 9> StoreRowCode{
+    0x7e280d00, 0x102828ff, 0x3cf80000, 0x7e3c0300, 0x7e3e0280, 0xf0201108, 0x0001141e, 0xbf810000, 0xbf810000,
+};
+
+alignas(256) constexpr std::array<std::uint32_t, 7> StoreStencilRowCode{
+    0x7e280281, 0x7e3c0300, 0x7e3e0280, 0xf0201108, 0x0001141e, 0xbf810000, 0xbf810000,
+};
+
+alignas(256) constexpr std::array<std::uint32_t, 10> StoreRowLayerCode{
+    0x7e280d00, 0x102828ff, 0x3cf80000, 0x7e3c0300, 0x7e3e0280, 0x7e400280, 0xf0201128, 0x0001141e, 0xbf810000, 0xbf810000,
+};
+
+std::array<std::uint32_t, 8> StorageView(std::uint64_t address, std::uint32_t format, std::uint32_t layers, std::uint32_t baseLevel = 0, std::uint32_t lastLevel = 0) {
+    return {
+        static_cast<std::uint32_t>(address >> 8u),
+        static_cast<std::uint32_t>((address >> 40u) & 0xffu) | (format << 20u) | (((PlaneWidth - 1u) & 3u) << 30u),
+        ((PlaneWidth - 1u) >> 2u) | ((PlaneHeight - 1u) << 14u),
+        0xfacu | (baseLevel << 12u) | (lastLevel << 16u) | (TileDepth64KB << 20u) | ((layers > 1u ? Type2DArray : Type2D) << 28u),
+        layers - 1u, lastLevel << 4u, 0u, 0u,
+    };
+}
+
+void Store(AgcDriver::VulkanDevice& device, const std::array<std::uint32_t, 8>& view, std::span<const std::uint32_t> code) {
+    std::vector<std::uint32_t> userData(16, 0u);
+    std::copy(view.begin(), view.end(), userData.begin() + 4);
+    const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
+    const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1};
+    ShaderRecompiler::RecompileRequest request{
+        {ShaderRecompiler::ShaderStage::Compute, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}},
+        {32, 0, userData, compute, std::nullopt, std::nullopt, memory},
+        device.Target(),
+        {0, 0, 0, 128}
+    };
+    request.useCache = false;
+    const auto program = ShaderRecompiler::Recompile(request);
+    device.Dispatch(program, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
+    device.WaitIdle();
+}
+
+void StoreRow(AgcDriver::VulkanDevice& device, std::uint8_t* plane, VkFormat format, std::uint32_t layers, std::uint32_t level = 0) {
+    const std::span<const std::uint32_t> code = layers > 1u ? std::span<const std::uint32_t>(StoreRowLayerCode) : std::span<const std::uint32_t>(StoreRowCode);
+    Store(device, StorageView(reinterpret_cast<std::uintptr_t>(plane), format == VK_FORMAT_D16_UNORM ? Format16Unorm : Format32Float, layers, level, level), code);
+}
+
+
 int main(int argc, char** argv) {
     try {
         const bool uploadImage = argc > 1 && std::string(argv[1]) == "--upload-image";
@@ -148,6 +205,54 @@
         const auto draw = [&](const Surface& surface, const DrawOptions& options) { return Draw(*device, shaders, surface, options, PlaneExtent, Colors); };
         const DrawOptions writeHalf{.depthWrite = true, .depthCompare = VK_COMPARE_OP_ALWAYS};
 
+        if (argc==2 && std::string_view(argv[1]).starts_with("--peer-")) {
+            const std::string mode(argv[1]);
+            const auto watched=AgcDriver::GuestMemory::Watched(base, PlaneBytes);
+            Require(watched, "six-unit depth plane requires write watch");
+            Fill(plane, 0.25f);
+            if (mode.ends_with("-prebind")) std::printf("first bind covered %u pixels\n",draw(depth,writeHalf));
+            StoreRow(*device, reinterpret_cast<std::uint8_t*>(plane), VK_FORMAT_D32_SFLOAT, 1);
+            AgcDriver::Graphics::StorageTexture::FlushPending(base, PlaneBytes, nullptr, "test");
+            device->WaitIdle();
+            std::printf("published row guest > half %zu, registered cached writer %d\n",
+                static_cast<std::size_t>(std::count_if(plane,plane+PlaneBytes/4,[](float x){return x>0.5f;})),
+                AgcDriver::Graphics::StorageTexture::FindLive(base,PlaneBytes)!=nullptr);
+            if (mode.ends_with("-split")) {
+                const auto first=draw(depth,{});
+                std::printf("before unit replacement draw passed %u texels\n",first);
+                Require(first==RowPasses,"guest shader row was not readable through depth");
+            }
+            AgcDriver::GuestMemory::CollectWritesUncached(base,PlaneBytes);
+            const auto before=AgcDriver::GuestMemory::TrackerGeneration();
+            auto expected=RowPasses;
+            if (mode.find("-cpu")!=std::string::npos) {
+                FillUnit(plane,2,0.75f);
+                expected+=UnitTexels;
+            } else if (mode.find("-dma")!=std::string::npos) {
+                const auto source=reinterpret_cast<std::uintptr_t>(blocks);
+                const auto destination=base+2*Block;
+                std::fill_n(reinterpret_cast<float*>(blocks),UnitTexels,0.75f);
+                const std::array<std::uint32_t,7> packet{0xc0055000,0x60000000,
+                    static_cast<std::uint32_t>(source),static_cast<std::uint32_t>(source>>32),
+                    static_cast<std::uint32_t>(destination),static_cast<std::uint32_t>(destination>>32),Block};
+                AgcDriver::QueueState queue;
+                AgcDriver::Pm4::Validate(packet,0);
+                AgcDriver::Pm4::Execute(packet,queue);
+                expected+=UnitTexels;
+            }
+            const auto inMemory=static_cast<std::size_t>(std::count_if(plane,plane+PlaneBytes/4,[](float x){return x>0.5f;}));
+            AgcDriver::GuestMemory::CollectWritesUncached(base,PlaneBytes);
+            std::array<std::uint64_t,Units> generation{};
+            generation.fill(before);
+            std::array<std::uint8_t,Units> changed{},cpu{};
+            Require(AgcDriver::GuestMemory::ChangedBlocks(base,PlaneBytes,generation,changed,cpu),"watched block generations must be available");
+            std::printf("unit 2 guest stamp %u CPU stamp %u\n",changed[2],cpu[2]);
+            const auto got=draw(depth,{});
+            std::printf("mode %s guest %zu GPU %u expected %u\n",mode.c_str(),inMemory,got,expected);
+            Require(inMemory==expected,"guest bytes before draw must contain both ordered writes");
+            Require(got==expected,"depth test must observe both ordered writes");
+            return 0;
+        }
         const bool pageWatch = argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-page-unwatch";
         const bool partialWatch = pageWatch || (argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-partial-unwatch");
         if (partialWatch || (argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-memo")) {
--- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_depth_plane_partial_store_tests.cmake
+++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_depth_plane_partial_store_tests.cmake
@@ -60,3 +60,30 @@
     add_test(NAME agc_driver_depth_plane_partial_store_buffer_copy COMMAND agc_driver_depth_plane_partial_store_tests --buffer-copy)
     add_test(NAME agc_driver_depth_plane_partial_store_upload_image COMMAND agc_driver_depth_plane_partial_store_tests --upload-image)
 endif()
+
+add_test(NAME peer_23739_control COMMAND agc_driver_depth_plane_partial_store_tests --peer-control)
+set_tests_properties(peer_23739_control PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_cpu COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu)
+set_tests_properties(peer_23739_cpu PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_dma COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma)
+set_tests_properties(peer_23739_dma PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_control_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-control-split)
+set_tests_properties(peer_23739_control_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_cpu_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu-split)
+set_tests_properties(peer_23739_cpu_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_dma_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma-split)
+set_tests_properties(peer_23739_dma_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_control_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-control-prebind)
+set_tests_properties(peer_23739_control_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_cpu_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu-prebind)
+set_tests_properties(peer_23739_cpu_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
+
+add_test(NAME peer_23739_dma_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma-prebind)
+set_tests_properties(peer_23739_dma_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")

AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5989786216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23739#pullrequestreview-5989786216). Originally posted by [IvanFogel](https://github.com/IvanFogel) on 2026-11-20T08:44:58Z. **Review:** CHANGES_REQUESTED --- I reproduced the later-writer regression discussed above. With an R32F storage write followed by an ordinary CPU store or a PM4 `DMA_DATA` copy into one depth tile, guest memory contains the new value but the next depth draw uses the older image. This happens when the depth surface was already bound. The cold first-bind cases pass. | Same nine cases on an RTX 3070 Ti | Result | |---|---| | Main `e825fa1492` | 9/9 passed | | PR `dbb10a3638` merged into that main | 5/9 passed | | Same PR build with `APS5_NO_DEPTH_FROM_ALIAS=1` | 9/9 passed | In each failing case, 16,399 pixels should pass the depth test. The PR draws 15, losing exactly the 16,384 pixels in the overwritten tile. CPU and DMA stores both advance the tile’s guest generation. Only the CPU store sets the CPU-write flag. A CPU-only gate would still accept stale texels after the DMA copy. I traced all four failures to the new alias-copy branch. Each time it copied one rectangle for tile 2 from alias version 2. Four existing depth tests passed on both trees. All runs required real Vulkan with the shader cache disabled, logged the Khronos validation layer, and produced no VUID or synchronization diagnostics. Tested on CachyOS Linux, NVIDIA GeForce RTX 3070 Ti, driver 620.70.06. This was a synthetic draw and did not run *The Exit 8*. <details> <summary>Regression fixture and run instructions</summary> The patch below applies to main `e825fa1492` and its merge with this PR. After applying it, configure the usual test build, build `agc_driver_depth_plane_partial_store_tests`, and run: ```sh ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_LOADER_DEBUG=layer VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R '^peer_23739_' -V ``` ```diff --- a/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DepthPlanePartialStore.cpp @@ -1,3 +1,4 @@ +#include "prx/libSceAgcDriver/Execution/include/Pm4.hpp" #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" #include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" @@ -126,6 +127,62 @@ } +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Format8Uint = 5; +constexpr std::uint32_t Format16Unorm = 7; +constexpr std::uint32_t Format32Float = 22; +constexpr std::uint32_t TileDepth64KB = 0x18; +constexpr std::uint32_t Type2D = 9; +constexpr std::uint32_t Type2DArray = 13; + + +constexpr std::uint32_t RowPasses = 15; + +alignas(256) constexpr std::array<std::uint32_t, 9> StoreRowCode{ + 0x7e280d00, 0x102828ff, 0x3cf80000, 0x7e3c0300, 0x7e3e0280, 0xf0201108, 0x0001141e, 0xbf810000, 0xbf810000, +}; + +alignas(256) constexpr std::array<std::uint32_t, 7> StoreStencilRowCode{ + 0x7e280281, 0x7e3c0300, 0x7e3e0280, 0xf0201108, 0x0001141e, 0xbf810000, 0xbf810000, +}; + +alignas(256) constexpr std::array<std::uint32_t, 10> StoreRowLayerCode{ + 0x7e280d00, 0x102828ff, 0x3cf80000, 0x7e3c0300, 0x7e3e0280, 0x7e400280, 0xf0201128, 0x0001141e, 0xbf810000, 0xbf810000, +}; + +std::array<std::uint32_t, 8> StorageView(std::uint64_t address, std::uint32_t format, std::uint32_t layers, std::uint32_t baseLevel = 0, std::uint32_t lastLevel = 0) { + return { + static_cast<std::uint32_t>(address >> 8u), + static_cast<std::uint32_t>((address >> 40u) & 0xffu) | (format << 20u) | (((PlaneWidth - 1u) & 3u) << 30u), + ((PlaneWidth - 1u) >> 2u) | ((PlaneHeight - 1u) << 14u), + 0xfacu | (baseLevel << 12u) | (lastLevel << 16u) | (TileDepth64KB << 20u) | ((layers > 1u ? Type2DArray : Type2D) << 28u), + layers - 1u, lastLevel << 4u, 0u, 0u, + }; +} + +void Store(AgcDriver::VulkanDevice& device, const std::array<std::uint32_t, 8>& view, std::span<const std::uint32_t> code) { + std::vector<std::uint32_t> userData(16, 0u); + std::copy(view.begin(), view.end(), userData.begin() + 4); + const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderRecompiler::ShaderStage::Compute, reinterpret_cast<std::uintptr_t>(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto program = ShaderRecompiler::Recompile(request); + device.Dispatch(program, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data())); + device.WaitIdle(); +} + +void StoreRow(AgcDriver::VulkanDevice& device, std::uint8_t* plane, VkFormat format, std::uint32_t layers, std::uint32_t level = 0) { + const std::span<const std::uint32_t> code = layers > 1u ? std::span<const std::uint32_t>(StoreRowLayerCode) : std::span<const std::uint32_t>(StoreRowCode); + Store(device, StorageView(reinterpret_cast<std::uintptr_t>(plane), format == VK_FORMAT_D16_UNORM ? Format16Unorm : Format32Float, layers, level, level), code); +} + + int main(int argc, char** argv) { try { const bool uploadImage = argc > 1 && std::string(argv[1]) == "--upload-image"; @@ -148,6 +205,54 @@ const auto draw = [&](const Surface& surface, const DrawOptions& options) { return Draw(*device, shaders, surface, options, PlaneExtent, Colors); }; const DrawOptions writeHalf{.depthWrite = true, .depthCompare = VK_COMPARE_OP_ALWAYS}; + if (argc==2 && std::string_view(argv[1]).starts_with("--peer-")) { + const std::string mode(argv[1]); + const auto watched=AgcDriver::GuestMemory::Watched(base, PlaneBytes); + Require(watched, "six-unit depth plane requires write watch"); + Fill(plane, 0.25f); + if (mode.ends_with("-prebind")) std::printf("first bind covered %u pixels\n",draw(depth,writeHalf)); + StoreRow(*device, reinterpret_cast<std::uint8_t*>(plane), VK_FORMAT_D32_SFLOAT, 1); + AgcDriver::Graphics::StorageTexture::FlushPending(base, PlaneBytes, nullptr, "test"); + device->WaitIdle(); + std::printf("published row guest > half %zu, registered cached writer %d\n", + static_cast<std::size_t>(std::count_if(plane,plane+PlaneBytes/4,[](float x){return x>0.5f;})), + AgcDriver::Graphics::StorageTexture::FindLive(base,PlaneBytes)!=nullptr); + if (mode.ends_with("-split")) { + const auto first=draw(depth,{}); + std::printf("before unit replacement draw passed %u texels\n",first); + Require(first==RowPasses,"guest shader row was not readable through depth"); + } + AgcDriver::GuestMemory::CollectWritesUncached(base,PlaneBytes); + const auto before=AgcDriver::GuestMemory::TrackerGeneration(); + auto expected=RowPasses; + if (mode.find("-cpu")!=std::string::npos) { + FillUnit(plane,2,0.75f); + expected+=UnitTexels; + } else if (mode.find("-dma")!=std::string::npos) { + const auto source=reinterpret_cast<std::uintptr_t>(blocks); + const auto destination=base+2*Block; + std::fill_n(reinterpret_cast<float*>(blocks),UnitTexels,0.75f); + const std::array<std::uint32_t,7> packet{0xc0055000,0x60000000, + static_cast<std::uint32_t>(source),static_cast<std::uint32_t>(source>>32), + static_cast<std::uint32_t>(destination),static_cast<std::uint32_t>(destination>>32),Block}; + AgcDriver::QueueState queue; + AgcDriver::Pm4::Validate(packet,0); + AgcDriver::Pm4::Execute(packet,queue); + expected+=UnitTexels; + } + const auto inMemory=static_cast<std::size_t>(std::count_if(plane,plane+PlaneBytes/4,[](float x){return x>0.5f;})); + AgcDriver::GuestMemory::CollectWritesUncached(base,PlaneBytes); + std::array<std::uint64_t,Units> generation{}; + generation.fill(before); + std::array<std::uint8_t,Units> changed{},cpu{}; + Require(AgcDriver::GuestMemory::ChangedBlocks(base,PlaneBytes,generation,changed,cpu),"watched block generations must be available"); + std::printf("unit 2 guest stamp %u CPU stamp %u\n",changed[2],cpu[2]); + const auto got=draw(depth,{}); + std::printf("mode %s guest %zu GPU %u expected %u\n",mode.c_str(),inMemory,got,expected); + Require(inMemory==expected,"guest bytes before draw must contain both ordered writes"); + Require(got==expected,"depth test must observe both ordered writes"); + return 0; + } const bool pageWatch = argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-page-unwatch"; const bool partialWatch = pageWatch || (argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-partial-unwatch"); if (partialWatch || (argc == 2 && std::string_view(argv[1]) == "--cpu-write-after-memo")) { --- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_depth_plane_partial_store_tests.cmake +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_depth_plane_partial_store_tests.cmake @@ -60,3 +60,30 @@ add_test(NAME agc_driver_depth_plane_partial_store_buffer_copy COMMAND agc_driver_depth_plane_partial_store_tests --buffer-copy) add_test(NAME agc_driver_depth_plane_partial_store_upload_image COMMAND agc_driver_depth_plane_partial_store_tests --upload-image) endif() + +add_test(NAME peer_23739_control COMMAND agc_driver_depth_plane_partial_store_tests --peer-control) +set_tests_properties(peer_23739_control PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_cpu COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu) +set_tests_properties(peer_23739_cpu PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_dma COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma) +set_tests_properties(peer_23739_dma PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_control_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-control-split) +set_tests_properties(peer_23739_control_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_cpu_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu-split) +set_tests_properties(peer_23739_cpu_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_dma_split COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma-split) +set_tests_properties(peer_23739_dma_split PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_control_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-control-prebind) +set_tests_properties(peer_23739_control_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_cpu_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-cpu-prebind) +set_tests_properties(peer_23739_cpu_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + +add_test(NAME peer_23739_dma_prebind COMMAND agc_driver_depth_plane_partial_store_tests --peer-dma-prebind) +set_tests_properties(peer_23739_dma_prebind PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") ``` </details> AI-assisted review.
MarcZX10 commented 2026-11-22 22:49:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T22:49:08Z.

Review: COMMENTED


Reviewed — comments

The alias refresh path and its test changes were reviewed at head 5d622cc26c81a33c184e70d1a905b807c688f9c5; Texture.cpp was excluded from the review bundle, so the complete changed implementation was not verified.

What I ran

No build or tests were run. I used repo_read.py to inspect the prepared description, commits, manifest, diff, changed-file ranges, and tracker declarations/behavior at the pinned head.

Results

Check Result Note
Prepared diff review passed Core changed paths and test logic inspected
Full changed-file review not run Texture.cpp full text was excluded
Build and tests not run No commands run

Findings

Blocking

None verified in the inspected code.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp was excluded as too large; only its changed regions were read. The manifest lists six changed files, and all six were reviewed; this excluded file was only partially read. No build, tests, Linux run, validation-layer run, or physical-console behavior was verified. The author-reported Windows test results were not run by this review.

To get this to a good state

Nothing is required. The implementation in the excluded portion of Texture.cpp remains unverified, so this review does not make a formal approval decision.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6014945090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23739#pullrequestreview-6014945090). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T22:49:08Z. **Review:** COMMENTED --- # Reviewed — comments The alias refresh path and its test changes were reviewed at head `5d622cc26c81a33c184e70d1a905b807c688f9c5`; `Texture.cpp` was excluded from the review bundle, so the complete changed implementation was not verified. ## What I ran No build or tests were run. I used `repo_read.py` to inspect the prepared description, commits, manifest, diff, changed-file ranges, and tracker declarations/behavior at the pinned head. ## Results | Check | Result | Note | |---|---|---| | Prepared diff review | passed | Core changed paths and test logic inspected | | Full changed-file review | not run | `Texture.cpp` full text was excluded | | Build and tests | not run | No commands run | ## Findings ### Blocking None verified in the inspected code. ### Non-blocking None. ## Not verified The full text of `core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp` was excluded as too large; only its changed regions were read. The manifest lists six changed files, and all six were reviewed; this excluded file was only partially read. No build, tests, Linux run, validation-layer run, or physical-console behavior was verified. The author-reported Windows test results were not run by this review. ## To get this to a good state Nothing is required. The implementation in the excluded portion of `Texture.cpp` remains unverified, so this review does not make a formal approval decision.
ligmaSec commented 2026-12-03 21:57:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T21:57:02Z.

Review: COMMENTED


The current head checks store ownership per depth-plane unit before taking it from the alias image. The CPU and DMA prebind cases verify that a later writer stays visible even when the image still contains an earlier row. Please keep the guest-byte and final depth-test assertions together in the paired run. The ordinary depth-plane tests and new alias cases are queued on lavapipe.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6179306074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23739#pullrequestreview-6179306074). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T21:57:02Z. **Review:** COMMENTED --- The current head checks store ownership per depth-plane unit before taking it from the alias image. The CPU and DMA prebind cases verify that a later writer stays visible even when the image still contains an earlier row. Please keep the guest-byte and final depth-test assertions together in the paired run. The ordinary depth-plane tests and new alias cases are queued on lavapipe. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/23739:upstream-pr/23739'
git switch 'upstream-pr/23739'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/23739'
git switch 'upstream-pr/23739'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/23739'
git switch 'upstream-pr/23739'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/23739'
git switch 'main'
git merge --squash 'upstream-pr/23739'
git switch 'main'
git merge --ff-only 'upstream-pr/23739'
git switch 'main'
git merge 'upstream-pr/23739'
git push origin 'main'
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!23739
No description provided.