fix(agc): keep small storage images current outside the write watch #23790

Merged
birdmarty merged 2 commits from upstream-pr/23790 into main 2026-11-20 04:58:35 +00:00
birdmarty commented 2026-11-19 16:54:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by birdmarty on 2026-11-19T16:54:45Z.


What

A storage image of up to 1 MiB keeps an upload copy (generationBaseline) to prove, outside the write watch, that its memory still holds what the image was made from. Two defects let that copy go stale, so results stored over a unit outside the watch were refused although nothing went wrong:

AGC graphics: storing storage image results pending over memory outside the write watch without the guest bytes saved before them is not implemented (unit N of 8: its bytes differ from the copy taken at its upload)

Lies of P (PPSA10621) aborts with this after 3.5 to 10 minutes on Windows, always on a 512x512 storage image (format 7, tile mode 4, 0x80000 bytes, 8 units) whose memory sits in host imports under 1 MiB, which are compared rather than watched.

1. The unwatch hook renewed the copy before the unit's results landed (StorageTexture::rebaseBeforeUnwatch). It read the unit at once unless PendingWriteOverlaps named a batch writing it, and results that reached guest memory without such a note were read before they landed: a diagnostic build that also performed the old read counted 2-13 renewals per run where it differed from the bytes after all recorded work completed. It also skipped units whose results were still in their unit shadow, and a skipped unit cleared anyBaselineDue for the whole image, dropping it from later hooks.

  • A shadowed unit is published first (PublishShadow, PublishScope::Whole); a unit still shadowed afterwards stays due.
  • While the recorder has work open or in flight, or the hook runs inside a completion, the renewal waits for all recorded work (Recorder::OnComplete; batches complete in order); an idle recorder reads at once. awaitRebase already syncs a renewal still due when results become pending.
  • anyBaselineDue is derived from baselineDue.

2. A dispatch was recorded with a storage image that changed after its bindings were made. VulkanDevice::dispatch builds the resources (cachedStorageTexture → Refresh); VulkanDevice::recordDispatch records the dispatch later and ends in MarkGpuWrites. A CPU store into watched memory traps and the flush hook drains the queue first; a CPU store into memory outside the watch does not, so the dispatch was recorded with the image's old content and the copy no longer matched at its store.

  • StorageTexture::ChangedOutsideWatch: a unit outside the watch with no results pending and no save or renewal in flight changed when its bytes differ from original (when held) or from its upload copy, or when its copy was not renewed as its range left the watch.
  • recordDispatch calls ShaderResources::RefreshStorageChangedOutsideWatch before recording anything for the dispatch; each of the dispatch's storage images (and storage sources of its sampled views) with such a unit is refreshed, so the upload lands in the batch before the dispatch. refresh re-uploads such units and does not take its epoch proof while one exists.
  • The upload copy is kept per unit. upload dropped the whole copy when any part of the image had a live unit shadow, pending results or a recorded write in flight (guestBytesSettled); a refresh of one unit outside the watch while other units were shadowed would have left every unit without a copy. Each uploaded unit now reads its own slice when its own range is settled (unitSettled); an unsettled unit is marked due, so its copy is renewed when its range leaves the watch or the unit is uploaded again at its next refresh.

Not covered (debt fragment): draws (an upload cannot be recorded inside a render pass), and images above 1 MiB that hold no original (no upload copy to detect against).

What stays the same:

  • Images inside the write watch: the new check returns at once for a unit the watch covers. The same goes for images above 1 MiB with no original.
  • Units with pending results, a save or a renewal in flight are left alone, so a refresh never uploads over results that have not landed.
  • Storage-image results that #17796 publishes into a resident heap mirror do not count as a change while guest memory still holds the old bytes. Once they are stored to guest memory, the most the check can do is re-upload those same bytes.
  • The cost is one compare of each unit outside the watch against its copy (at most 1 MiB per image), per storage image of each recorded dispatch and in refresh before taking its epoch proof.

How the causes were found

Diagnostic builds kept a per-image event history (uploads, write-backs, unwatch decisions, renewals, refreshes, the caller that made results pending) and printed it at the refusal. What was tried and ruled out:

Hypothesis Result
The hook ran without an active recorder, so in-flight writes were missed (fall back to context.recorder) Recorder::Active() is a global, not per thread; no effect, the next run still renewed a unit with stale bytes
Flush GPU writes in the hook (GuestMemory::FlushGpuWrites) Deadlock: refreshImports re-unwatches an import under the host imports mutex and the flush takes it again (ExistingHostImportFor); replaced by waiting with OnComplete
Another storage image over the same memory writes back over the unit Every write-back by another live image over the unit was logged: none
The resource cache reuses the image without a refresh fastRevalidate rejects ranges outside the watch (UnchangedSince), and its other paths call Refresh; every exit of refresh was logged: none ran between the unwatch and the refused dispatch
Resolve-ahead adopts a stale dispatch ResolvedDispatch carries no storage images
Keep the CPU's block instead of refusing Contradicts agc_storage_pending_after_unwatch, which requires the results to land in a unit they cover
A refresh would miss the change No: refresh already re-uploads a unit outside the watch when it runs (its blocks are untracked); it does not run between the build and the record

The history then showed the remaining case directly: units 0-1 carried CPU stores the watch had seen, unit 2 changed after its copy was renewed correctly, no refresh ran, and the refusing dispatch came through MarkGpuWrites from recordDispatch.

settleFloorBytes lowered to 512 KiB also avoids the abort in the title, by keeping these imports watched; this PR leaves it at 1 MiB.

Tested

Windows 11 Pro build 26300, Intel Core i9-9900K, AMD Radeon RX 6800 (AMD proprietary driver 26.10.1, Vulkan 1.4.315), WinLibs GCC 15.3.0, Release.

Tests, main (d7be67833e) and this change, same machine and GPU. The set has 75 test programs:

  • agc_driver_recorder_tests (the storage-image, unit shadow and unwatch tests, and the new one);
  • agc_driver_store_over_untracked_tests and agc_driver_host_write_shadowed_unit_tests;
  • agc_driver_own_refresh_view_alias_tests (#21699) and agc_driver_resident_heap_mirror_tests (#17796);
  • every other guest memory, host import, mirror, storage image, write tracking and BDA test program.
main this change
Tests 411: 404 passed, 5 skipped, 2 failed 412 (the new one included): 405 passed, 5 skipped, 2 failed
Again with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (1.4.363), VK_LOADER_DEBUG=layer, ctest -V 405 passed, 5 skipped, 1 failed 406 passed, 5 skipped, 1 failed
Insert instance layer "VK_LAYER_KHRONOS_validation" 239 240
was not found but was requested by env var VK_INSTANCE_LAYERS 0 0
VUID- lines 0 0
public_headers 65/65 65/65

The same tests fail on both sides:

  • agc_driver_store_over_untracked: the known Windows failure #22670. Run alone 10 times it failed 9 of 10 on main and 8 of 10 with this change, both with a pending storage result was stored over bytes the CPU wrote to untracked memory.
  • agc_storage_write_back_host_retile: failed in the parallel run on both sides ("the linear write-back went through its scratch instead of retiling into the import") and passed in the validation run on both. Run alone 10 times with this change it passed 10 of 10.

The five skips are the same on both sides: agc_driver_host_write_shadowed_unit and agc_host_import_sparse (Linux imports only), agc_driver_copied_writer_label_order, agc_driver_copied_writer_label_order_drain and agc_driver_partially_committed_volume_page_samples. agc_shader_memory_tests does not link with this toolchain on either side (ld: .rsrc merge failure: multiple non-default manifests), so its tests are not counted.

tools/check_conventions.py --base origin/main and tools/debt_fold.py --check: passed.

tools/pr_overlap.py: three open pull requests change lines next to this change's, not the same logic. Whichever lands second resolves the adjacent lines.

  • #7002 changes the RequireGuestBuffers call in recordDispatch, after which this change adds its refresh.
  • #9897 changes the constructor right after the new RefreshStorageChangedOutsideWatch.
  • #22018 adds test switches at the same place in Recorder.cpp's main. It drops pending results over unmapped memory and does not touch the upload copy.

86 pull requests that conflict with main share files and could not be compared.

New test agc_storage_refresh_after_unwatched_cpu_write:

  1. A 256x256 kR64KBX image is written back while watched, and its last unit leaves the watch through the hook.
  2. The image is refreshed, then the CPU writes that unit.
  3. ChangedOutsideWatch must report it, and the refresh must clear it.
  4. Results stored over the whole image afterwards must land.

With the detection switched off (unitChangedOutsideWatch returning false) it fails with (c) a CPU write into the unit outside the watch is not seen; with it, it passes. The recordDispatch call needs the full dispatch pipeline and is covered by the title runs below.

Lies of P (PPSA10621, 01.012.000), #22009, on an earlier main (d31916ed37) with #23789 and with #16053 reverted (#20827 does this on main now), plus this change and its diagnostics. The title only gets past its first loading screen and into gameplay with #23789, so that one goes first; this change does not need its code. A cold shader cache (shaders compiled at first use) reproduced the abort within minutes before the fix.

Build Cache Route Seconds Exit
without the fix not recorded / warm train → Krat Central Station 601 / 403 abort (UploadCopyDiffers)
part 1 only cold new game 416 / 215 / 297 abort (UploadCopyDiffers)
this change cold new game → first Stargazer 956 clean
this change, record-time refresh switched off (diagnostic switch) cold new game 531 abort (UploadCopyDiffers) on the image whose refresh was skipped just before
this change cold new game → first boss 1123 clean
this change warm first boss → Hotel Krat → second Stargazer 1062 clean

The before-fix route crashed reliably at three places (leaving the train, the way to the first miniboss, the door to the first Stargazer); every clean run crossed all three. In the clean runs the record-time refresh fired 5, 26 and 8 times, every time on a 0x80000 storage image, and the hook's renewal waited for in-flight writes that the old read would have missed 5, 5 and 2 times. With the record-time refresh switched off, the six refreshes it would have made were logged as skipped, and the store was refused on the image of the last one.

Lies of P with this change: first boss down, Hotel Krat, the second area

Lies of P (PPSA10261, 01.005.001, another release of the game; see #5983), same build and stack: a cold run from no save data through a New Game and about 16 minutes of gameplay ran 1154 s and exited cleanly. The record-time refresh fired 12 times, again every time on a 0x80000 storage image, and the hook's renewal waited for in-flight writes 5 times.

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
  • Fixes: none
  • Unblocks: PPSA10621 #22009
  • GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer on main and this change, no new VUIDs
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5399262121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23790). Originally posted by [birdmarty](https://github.com/birdmarty) on 2026-11-19T16:54:45Z. --- ### What A storage image of up to 1 MiB keeps an upload copy (`generationBaseline`) to prove, outside the write watch, that its memory still holds what the image was made from. Two defects let that copy go stale, so results stored over a unit outside the watch were refused although nothing went wrong: ``` AGC graphics: storing storage image results pending over memory outside the write watch without the guest bytes saved before them is not implemented (unit N of 8: its bytes differ from the copy taken at its upload) ``` *Lies of P* (PPSA10621) aborts with this after 3.5 to 10 minutes on Windows, always on a 512x512 storage image (format 7, tile mode 4, 0x80000 bytes, 8 units) whose memory sits in host imports under 1 MiB, which are compared rather than watched. **1. The unwatch hook renewed the copy before the unit's results landed** (`StorageTexture::rebaseBeforeUnwatch`). It read the unit at once unless `PendingWriteOverlaps` named a batch writing it, and results that reached guest memory without such a note were read before they landed: a diagnostic build that also performed the old read counted 2-13 renewals per run where it differed from the bytes after all recorded work completed. It also skipped units whose results were still in their unit shadow, and a skipped unit cleared `anyBaselineDue` for the whole image, dropping it from later hooks. - A shadowed unit is published first (`PublishShadow`, `PublishScope::Whole`); a unit still shadowed afterwards stays due. - While the recorder has work open or in flight, or the hook runs inside a completion, the renewal waits for all recorded work (`Recorder::OnComplete`; batches complete in order); an idle recorder reads at once. `awaitRebase` already syncs a renewal still due when results become pending. - `anyBaselineDue` is derived from `baselineDue`. **2. A dispatch was recorded with a storage image that changed after its bindings were made.** `VulkanDevice::dispatch` builds the resources (`cachedStorageTexture` → `Refresh`); `VulkanDevice::recordDispatch` records the dispatch later and ends in `MarkGpuWrites`. A CPU store into watched memory traps and the flush hook drains the queue first; a CPU store into memory outside the watch does not, so the dispatch was recorded with the image's old content and the copy no longer matched at its store. - `StorageTexture::ChangedOutsideWatch`: a unit outside the watch with no results pending and no save or renewal in flight changed when its bytes differ from `original` (when held) or from its upload copy, or when its copy was not renewed as its range left the watch. - `recordDispatch` calls `ShaderResources::RefreshStorageChangedOutsideWatch` before recording anything for the dispatch; each of the dispatch's storage images (and storage sources of its sampled views) with such a unit is refreshed, so the upload lands in the batch before the dispatch. `refresh` re-uploads such units and does not take its epoch proof while one exists. - The upload copy is kept per unit. `upload` dropped the whole copy when any part of the image had a live unit shadow, pending results or a recorded write in flight (`guestBytesSettled`); a refresh of one unit outside the watch while other units were shadowed would have left every unit without a copy. Each uploaded unit now reads its own slice when its own range is settled (`unitSettled`); an unsettled unit is marked due, so its copy is renewed when its range leaves the watch or the unit is uploaded again at its next refresh. Not covered (debt fragment): draws (an upload cannot be recorded inside a render pass), and images above 1 MiB that hold no `original` (no upload copy to detect against). What stays the same: - Images inside the write watch: the new check returns at once for a unit the watch covers. The same goes for images above 1 MiB with no `original`. - Units with pending results, a save or a renewal in flight are left alone, so a refresh never uploads over results that have not landed. - Storage-image results that #17796 publishes into a resident heap mirror do not count as a change while guest memory still holds the old bytes. Once they are stored to guest memory, the most the check can do is re-upload those same bytes. - The cost is one compare of each unit outside the watch against its copy (at most 1 MiB per image), per storage image of each recorded dispatch and in `refresh` before taking its epoch proof. #### How the causes were found Diagnostic builds kept a per-image event history (uploads, write-backs, unwatch decisions, renewals, refreshes, the caller that made results pending) and printed it at the refusal. What was tried and ruled out: | Hypothesis | Result | |---|---| | The hook ran without an active recorder, so in-flight writes were missed (fall back to `context.recorder`) | `Recorder::Active()` is a global, not per thread; no effect, the next run still renewed a unit with stale bytes | | Flush GPU writes in the hook (`GuestMemory::FlushGpuWrites`) | Deadlock: `refreshImports` re-unwatches an import under the host imports mutex and the flush takes it again (`ExistingHostImportFor`); replaced by waiting with `OnComplete` | | Another storage image over the same memory writes back over the unit | Every write-back by another live image over the unit was logged: none | | The resource cache reuses the image without a refresh | `fastRevalidate` rejects ranges outside the watch (`UnchangedSince`), and its other paths call `Refresh`; every exit of `refresh` was logged: none ran between the unwatch and the refused dispatch | | Resolve-ahead adopts a stale dispatch | `ResolvedDispatch` carries no storage images | | Keep the CPU's block instead of refusing | Contradicts `agc_storage_pending_after_unwatch`, which requires the results to land in a unit they cover | | A refresh would miss the change | No: `refresh` already re-uploads a unit outside the watch when it runs (its blocks are untracked); it does not run between the build and the record | The history then showed the remaining case directly: units 0-1 carried CPU stores the watch had seen, unit 2 changed after its copy was renewed correctly, no `refresh` ran, and the refusing dispatch came through `MarkGpuWrites` from `recordDispatch`. `settleFloorBytes` lowered to 512 KiB also avoids the abort in the title, by keeping these imports watched; this PR leaves it at 1 MiB. ### Tested Windows 11 Pro build 26300, Intel Core i9-9900K, AMD Radeon RX 6800 (AMD proprietary driver 26.10.1, Vulkan 1.4.315), WinLibs GCC 15.3.0, Release. **Tests**, `main` (`d7be67833e`) and this change, same machine and GPU. The set has 75 test programs: - `agc_driver_recorder_tests` (the storage-image, unit shadow and unwatch tests, and the new one); - `agc_driver_store_over_untracked_tests` and `agc_driver_host_write_shadowed_unit_tests`; - `agc_driver_own_refresh_view_alias_tests` (#21699) and `agc_driver_resident_heap_mirror_tests` (#17796); - every other guest memory, host import, mirror, storage image, write tracking and BDA test program. | | `main` | this change | |---|---|---| | Tests | 411: 404 passed, 5 skipped, 2 failed | 412 (the new one included): 405 passed, 5 skipped, 2 failed | | Again with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (1.4.363), `VK_LOADER_DEBUG=layer`, `ctest -V` | 405 passed, 5 skipped, 1 failed | 406 passed, 5 skipped, 1 failed | | `Insert instance layer "VK_LAYER_KHRONOS_validation"` | 239 | 240 | | `was not found but was requested by env var VK_INSTANCE_LAYERS` | 0 | 0 | | `VUID-` lines | 0 | 0 | | `public_headers` | 65/65 | 65/65 | The same tests fail on both sides: - `agc_driver_store_over_untracked`: the known Windows failure #22670. Run alone 10 times it failed 9 of 10 on `main` and 8 of 10 with this change, both with `a pending storage result was stored over bytes the CPU wrote to untracked memory`. - `agc_storage_write_back_host_retile`: failed in the parallel run on both sides ("the linear write-back went through its scratch instead of retiling into the import") and passed in the validation run on both. Run alone 10 times with this change it passed 10 of 10. The five skips are the same on both sides: `agc_driver_host_write_shadowed_unit` and `agc_host_import_sparse` (Linux imports only), `agc_driver_copied_writer_label_order`, `agc_driver_copied_writer_label_order_drain` and `agc_driver_partially_committed_volume_page_samples`. `agc_shader_memory_tests` does not link with this toolchain on either side (`ld: .rsrc merge failure: multiple non-default manifests`), so its tests are not counted. `tools/check_conventions.py --base origin/main` and `tools/debt_fold.py --check`: passed. `tools/pr_overlap.py`: three open pull requests change lines next to this change's, not the same logic. Whichever lands second resolves the adjacent lines. - #7002 changes the `RequireGuestBuffers` call in `recordDispatch`, after which this change adds its refresh. - #9897 changes the constructor right after the new `RefreshStorageChangedOutsideWatch`. - #22018 adds test switches at the same place in `Recorder.cpp`'s `main`. It drops pending results over unmapped memory and does not touch the upload copy. 86 pull requests that conflict with `main` share files and could not be compared. **New test** `agc_storage_refresh_after_unwatched_cpu_write`: 1. A 256x256 `kR64KBX` image is written back while watched, and its last unit leaves the watch through the hook. 2. The image is refreshed, then the CPU writes that unit. 3. `ChangedOutsideWatch` must report it, and the refresh must clear it. 4. Results stored over the whole image afterwards must land. With the detection switched off (`unitChangedOutsideWatch` returning false) it fails with `(c) a CPU write into the unit outside the watch is not seen`; with it, it passes. The `recordDispatch` call needs the full dispatch pipeline and is covered by the title runs below. **Lies of P** (PPSA10621, 01.012.000), [#22009](https://github.com/boykopovar/AnyPS5/discussions/22009), on an earlier `main` (`d31916ed37`) with #23789 and with #16053 reverted (#20827 does this on `main` now), plus this change and its diagnostics. The title only gets past its first loading screen and into gameplay with #23789, so that one goes first; this change does not need its code. A cold shader cache (shaders compiled at first use) reproduced the abort within minutes before the fix. | Build | Cache | Route | Seconds | Exit | |---|---|---|---|---| | without the fix | not recorded / warm | train → Krat Central Station | 601 / 403 | abort (`UploadCopyDiffers`) | | part 1 only | cold | new game | 416 / 215 / 297 | abort (`UploadCopyDiffers`) | | this change | cold | new game → first Stargazer | 956 | clean | | this change, record-time refresh switched off (diagnostic switch) | cold | new game | 531 | abort (`UploadCopyDiffers`) on the image whose refresh was skipped just before | | this change | cold | new game → first boss | 1123 | clean | | this change | warm | first boss → Hotel Krat → second Stargazer | 1062 | clean | The before-fix route crashed reliably at three places (leaving the train, the way to the first miniboss, the door to the first Stargazer); every clean run crossed all three. In the clean runs the record-time refresh fired 5, 26 and 8 times, every time on a 0x80000 storage image, and the hook's renewal waited for in-flight writes that the old read would have missed 5, 5 and 2 times. With the record-time refresh switched off, the six refreshes it would have made were logged as skipped, and the store was refused on the image of the last one. ![Lies of P with this change: first boss down, Hotel Krat, the second area](https://raw.githubusercontent.com/birdmarty/AnyPS5/pr-assets/fix-agc-storage-baseline-outside-watch/1-lies-of-p-past-the-crash-points.png) **Lies of P** (PPSA10261, 01.005.001, another release of the game; see #5983), same build and stack: a cold run from no save data through a New Game and about 16 minutes of gameplay ran 1154 s and exited cleanly. The record-time refresh fired 12 times, again every time on a 0x80000 storage image, and the hook's renewal waited for in-flight writes 5 times. ### 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) 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](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: PPSA10621 #22009 - [x] GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer on `main` and this change, no new VUIDs - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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!23790
No description provided.