fix(agc): keep guest bytes when a stale depth plane is flushed #16373

Merged
NFSS10 merged 2 commits from upstream-pr/16373 into main 2026-11-09 20:17:07 +00:00
NFSS10 commented 2026-11-06 13:13:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NFSS10 on 2026-11-06T13:13:11Z.


What

A depth surface keeps its mirror registered after the guest releases the memory it covers, so a flush driven by later GPU work over that range stores pending results into live guest data. A pending fast clear made it silent: the storing synchronisation returned before the guest-write check, so the clear value (zero) was written over whatever the guest now keeps there. On PPSA27616 the fault read through a TLSF bin whose node sat inside a 64 KiB depth plane (plane+0x7ff8) while TLSF control pointed at the guest heap.

  • Each plane unit records the backingSerial of the allocation registered over it at every synchronisation, and a flush drops a unit whose allocation changed instead of storing into it. Per unit, because the rest of the plane can still be the guest's.
  • A storing synchronisation (SyncFromMemory(resource, storing)) no longer lets a pending clear skip the guest-write check: it drops the cleared units and forgets the clear for the surface.
  • retireStoringStale, which retires a depth plane after it was sampled and overwritten, applies the same unit rule.
  • The draw path is unchanged: storing=false keeps the previous early return.
  • The technical debt entry records the residual hole (reuse inside one unchanged registration with no write to the range since the last synchronisation) and that the stencil plane keeps its own rule, which #14969 changes.

Related: #14969 covers the stencil half of the same bug and #15621 touches SyncFromMemory and the plane member block; this change does not include the stencil plane on purpose.

Tested

  • Windows 11 Pro 22631, MinGW build.
  • PPSA27616 (Bendy and the Ink Machine), a recorded pad session replayed through APS5_PAD_INPUT_FILE.
  • Result: the crash no longer reproduces. On main all 11 runs crashed; with this change none of the 17 did.
  • main (e3a14a86e4 and d23df92b38): 11 runs, all fatal, 0xc0000005 on UnityGfxDeviceWorker, rip 0x1417f2bfb, reading through a TLSF bin whose node is inside a depth plane.
  • This commit: 5 runs, no fatal, the last one after the rebase onto e8b1bd3d7f. Counting the two intermediate builds that carry the same depth rule: 17 runs, no fatal.
  • In 9 of those 17 runs the new guard logs the condition it exists for ([gpu] depth plane 0x257810000+0x40000 was re-registered with GPU results pending; they are dropped) and the session carries on.
  • ctest -R 'depth|storage|write_tracking|guest_memory': 124/124 on this branch. The same suite passes on main and on this branch with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (that comparison ran on the immediately preceding main, d23df92b38, whose DepthSurface.cpp is identical); neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS and neither contains a VUID- line.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 7900 GRE, driver 32.0.21013.1000, Windows 11 Pro 22631
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5188359430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16373). Originally posted by [NFSS10](https://github.com/NFSS10) on 2026-11-06T13:13:11Z. --- ### What A depth surface keeps its mirror registered after the guest releases the memory it covers, so a flush driven by later GPU work over that range stores pending results into live guest data. A pending fast clear made it silent: the storing synchronisation returned before the guest-write check, so the clear value (zero) was written over whatever the guest now keeps there. On PPSA27616 the fault read through a TLSF bin whose node sat inside a 64 KiB depth plane (`plane+0x7ff8`) while TLSF control pointed at the guest heap. - Each plane unit records the `backingSerial` of the allocation registered over it at every synchronisation, and a flush drops a unit whose allocation changed instead of storing into it. Per unit, because the rest of the plane can still be the guest's. - A storing synchronisation (`SyncFromMemory(resource, storing)`) no longer lets a pending clear skip the guest-write check: it drops the cleared units and forgets the clear for the surface. - `retireStoringStale`, which retires a depth plane after it was sampled and overwritten, applies the same unit rule. - The draw path is unchanged: `storing=false` keeps the previous early return. - The technical debt entry records the residual hole (reuse inside one unchanged registration with no write to the range since the last synchronisation) and that the stencil plane keeps its own rule, which #14969 changes. Related: #14969 covers the stencil half of the same bug and #15621 touches `SyncFromMemory` and the plane member block; this change does not include the stencil plane on purpose. ### Tested - Windows 11 Pro 22631, MinGW build. - PPSA27616 (Bendy and the Ink Machine), a recorded pad session replayed through `APS5_PAD_INPUT_FILE`. - Result: the crash no longer reproduces. On `main` all 11 runs crashed; with this change none of the 17 did. - `main` (`e3a14a86e4` and `d23df92b38`): 11 runs, all fatal, `0xc0000005` on `UnityGfxDeviceWorker`, `rip 0x1417f2bfb`, reading through a TLSF bin whose node is inside a depth plane. - This commit: 5 runs, no fatal, the last one after the rebase onto `e8b1bd3d7f`. Counting the two intermediate builds that carry the same depth rule: 17 runs, no fatal. - In 9 of those 17 runs the new guard logs the condition it exists for (`[gpu] depth plane 0x257810000+0x40000 was re-registered with GPU results pending; they are dropped`) and the session carries on. - `ctest -R 'depth|storage|write_tracking|guest_memory'`: 124/124 on this branch. The same suite passes on `main` and on this branch with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (that comparison ran on the immediately preceding `main`, `d23df92b38`, whose `DepthSurface.cpp` is identical); neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS` and neither contains a `VUID-` line. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 7900 GRE, driver 32.0.21013.1000, Windows 11 Pro 22631 - [x] AI-assisted: yes
milescoviello commented 2026-11-06 16:46:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-06T16:46:05Z.

Review: COMMENTED


Read it against DepthSurface.cpp on main. The unit rule and the storing/non-storing split look right to me. One cost point and one coverage note.

  1. The refresh runs on every sync, including the draw path. SyncFromMemory now refreshes unitBacking with one GuestAllocationsLookup_nid_postfix per 64 KiB unit, before the early return. That's one registry lock and one shared_ptr copy per unit. It's reached from the depth-target bind (if (mirrorable(target)) surface->SyncFromMemory(...)) and the sampled path, not only from flushes. A 3840x2160 D32 plane is about 500 units, so about 500 locked lookups per bind. Two existing APIs cover it:
    • GuestAllocationsLookupEach_nid_postfix does the whole plane under one lock.
    • GuestAllocationsGeneration_nid_postfix changes on every mapping mutation, so a surface that stores the generation it filled unitBacking at can skip the refresh when nothing was mapped or unmapped since.
      On a draw-heavy title the second one makes this free.
  2. Reuse inside one registration. The debt entry already says it isn't covered. For what it's worth, STEINS;GATE RE:BOOT (PPSA36591, #15350) hits that case with storage images: the game's GPU Heap mspace reuses blocks inside one mapped direct-memory range, so backingSerial stays the same there. That's a separate fix, not something this PR should take on.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5803698666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16373#pullrequestreview-5803698666). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-06T16:46:05Z. **Review:** COMMENTED --- Read it against `DepthSurface.cpp` on `main`. The unit rule and the storing/non-storing split look right to me. One cost point and one coverage note. 1. **The refresh runs on every sync, including the draw path.** `SyncFromMemory` now refreshes `unitBacking` with one `GuestAllocationsLookup_nid_postfix` per 64 KiB unit, before the early return. That's one registry lock and one `shared_ptr` copy per unit. It's reached from the depth-target bind (`if (mirrorable(target)) surface->SyncFromMemory(...)`) and the sampled path, not only from flushes. A 3840x2160 D32 plane is about 500 units, so about 500 locked lookups per bind. Two existing APIs cover it: - `GuestAllocationsLookupEach_nid_postfix` does the whole plane under one lock. - `GuestAllocationsGeneration_nid_postfix` changes on every mapping mutation, so a surface that stores the generation it filled `unitBacking` at can skip the refresh when nothing was mapped or unmapped since. On a draw-heavy title the second one makes this free. 2. **Reuse inside one registration.** The debt entry already says it isn't covered. For what it's worth, STEINS;GATE RE:BOOT (PPSA36591, #15350) hits that case with storage images: the game's GPU Heap mspace reuses blocks inside one mapped direct-memory range, so `backingSerial` stays the same there. That's a separate fix, not something this PR should take on. AI-assisted: yes (Claude Code)
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!16373
No description provided.