fix(agc): store small pending storage results at each flip #4841

Merged
gjonn merged 1 commit from upstream-pr/4841 into main 2026-10-14 17:34:51 +00:00
gjonn commented 2026-10-12 23:27:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-12T23:27:41Z.


What

A storage image result stays on the GPU until something stores it to guest memory: a driver-observed use of its range (FlushPending through the flush hook, a packet that reads the range), the queue-idle store (#3777), or APS5_STORE_AT_FLIP=1 at PresentDisplayBuffer. A guest CPU that reads the result with plain loads gets none of the first, the second only runs when queue 0 drains, and the third is off by default. While a title keeps queue 0 busy, such a result never reaches guest memory.

PPSA01317 needs a guarantee here. Its CPU auto-exposure reads 16x16 R32F and RGBA16F images that the GPU writes in host-imported memory, a frame or more after they are written. On an older local tree without #3777, those images were marked written 50 to 80 times and stored 0 times, and a guard-page probe on them caught guest CPU loads. The exposure multiplier computed from them collapsed to 2^-32 and the garage car view went black within seconds of each camera cut.

On current main, #3777 covers this whenever queue 0 drains between frames. This change makes the store not depend on the queue going idle. That matters if the idle store is delayed, as #4410 proposes (50 ms): this title's gap between a flip and its next submit is about 5 to 13 ms. I have not run the title on current main; the test below shows the failure on main when the queue stays busy.

Change: when queue 0 reaches a flip packet, before the flip's batch is submitted, StorageTexture::StoreSmallPending stores every pending image of at most SmallStoreBytes (64 KiB) whose memory is host-imported. The stores are recorded on the GPU behind the frame's work and submitted in the flip's batch, so neither the queue worker nor the title waits for the GPU. A flip ends the title's frame, so a result it reads a frame later has been stored by then.

The bounds:

  • 64 KiB: one 64 KiB swizzle block (the largest tile block and the write-stamp granularity). It covers surfaces a CPU reads back as reductions (luminance, histogram and visibility results, up to 128x128 R32F). Full-size targets stay pending as before: storing those at every flip is what APS5_STORE_AT_FLIP=1 does, and its cost is why that switch is off by default (#1995).
  • Host imports only: a store into memory that is not host-imported goes through the CPU after a GPU wait, which would serialize the queue worker with the GPU at every flip. Such images keep today's behaviour.

APS5_STORE_AT_FLIP=1 is unchanged.

Related: #4794 (open) stores every image result at draw time instead; this PR stores only small imported results, once per frame.

Tested

  • Windows 11 Pro, MinGW GCC 15.2, Release, Ninja, NVIDIA GPU; rebased on main at 90c683946.
  • New agc_driver_flip_stores_small_results: on queue 0, a dispatch writes a 16x16 R32 storage image in imported guest memory, then a flip, then a WAIT_REG_MEM on a dword only the test writes, which keeps queue 0 busy so the queue-idle store cannot run. The test polls the image with plain CPU loads for 2 s. Without the change it fails ("a small image written before a flip did not reach guest memory while the queue kept working"); with it, it passes.
  • ctest -R agc_driver: 347 tests, all passed (1 skipped as on main: agc_driver_stencil_ref_export).
  • PPSA01317, on the older local tree (without #3777) with the same store done at the flip: the garage car view stayed lit in 3 of 3 runs (before: black within seconds of each camera cut). The stores cost about 0.37 ms per flip.
  • python tools/check_conventions.py --base origin/main passes.

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
  • Unblocks: PPSA01317 #4697
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4836190823 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-12T23:27:41Z. --- ### What A storage image result stays on the GPU until something stores it to guest memory: a driver-observed use of its range (`FlushPending` through the flush hook, a packet that reads the range), the queue-idle store (#3777), or `APS5_STORE_AT_FLIP=1` at `PresentDisplayBuffer`. A guest CPU that reads the result with plain loads gets none of the first, the second only runs when queue 0 drains, and the third is off by default. While a title keeps queue 0 busy, such a result never reaches guest memory. PPSA01317 needs a guarantee here. Its CPU auto-exposure reads 16x16 R32F and RGBA16F images that the GPU writes in host-imported memory, a frame or more after they are written. On an older local tree without #3777, those images were marked written 50 to 80 times and stored 0 times, and a guard-page probe on them caught guest CPU loads. The exposure multiplier computed from them collapsed to 2^-32 and the garage car view went black within seconds of each camera cut. On current `main`, #3777 covers this whenever queue 0 drains between frames. This change makes the store not depend on the queue going idle. That matters if the idle store is delayed, as #4410 proposes (50 ms): this title's gap between a flip and its next submit is about 5 to 13 ms. I have not run the title on current `main`; the test below shows the failure on `main` when the queue stays busy. Change: when queue 0 reaches a flip packet, before the flip's batch is submitted, `StorageTexture::StoreSmallPending` stores every pending image of at most `SmallStoreBytes` (64 KiB) whose memory is host-imported. The stores are recorded on the GPU behind the frame's work and submitted in the flip's batch, so neither the queue worker nor the title waits for the GPU. A flip ends the title's frame, so a result it reads a frame later has been stored by then. The bounds: - **64 KiB:** one 64 KiB swizzle block (the largest tile block and the write-stamp granularity). It covers surfaces a CPU reads back as reductions (luminance, histogram and visibility results, up to 128x128 R32F). Full-size targets stay pending as before: storing those at every flip is what `APS5_STORE_AT_FLIP=1` does, and its cost is why that switch is off by default (#1995). - **Host imports only:** a store into memory that is not host-imported goes through the CPU after a GPU wait, which would serialize the queue worker with the GPU at every flip. Such images keep today's behaviour. `APS5_STORE_AT_FLIP=1` is unchanged. Related: #4794 (open) stores every image result at draw time instead; this PR stores only small imported results, once per frame. ### Tested - Windows 11 Pro, MinGW GCC 15.2, Release, Ninja, NVIDIA GPU; rebased on `main` at 90c683946. - New `agc_driver_flip_stores_small_results`: on queue 0, a dispatch writes a 16x16 R32 storage image in imported guest memory, then a flip, then a `WAIT_REG_MEM` on a dword only the test writes, which keeps queue 0 busy so the queue-idle store cannot run. The test polls the image with plain CPU loads for 2 s. Without the change it fails ("a small image written before a flip did not reach guest memory while the queue kept working"); with it, it passes. - `ctest -R agc_driver`: 347 tests, all passed (1 skipped as on `main`: `agc_driver_stencil_ref_export`). - PPSA01317, on the older local tree (without #3777) with the same store done at the flip: the garage car view stayed lit in 3 of 3 runs (before: black within seconds of each camera cut). The stores cost about 0.37 ms per flip. - `python tools/check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA01317 #4697 - [x] AI-assisted: yes
kr4y4 commented 2026-10-13 04:07:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T04:07:34Z.


An Intel run, and the full suite.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head fb388af3f merged into main 495bf91a0, which passes 1023 of 1023 on both GPUs.

  • agc_driver_flip_stores_small_results passes on both GPUs with --repeat until-fail:3, and it ran on both without returning 77.
  • Full suite (ctest --timeout 120 --parallel 4): 1024 of 1024 on both GPUs.

No negative control here: with the production files as on main the test doesn't build, because StorageTexture::SmallStoreBytes is new.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6138015624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841#issuecomment-6138015624). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-13T04:07:34Z. --- An Intel run, and the full suite. Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head fb388af3f merged into `main` 495bf91a0, which passes 1023 of 1023 on both GPUs. - `agc_driver_flip_stores_small_results` passes on both GPUs with `--repeat until-fail:3`, and it ran on both without returning 77. - Full suite (`ctest --timeout 120 --parallel 4`): 1024 of 1024 on both GPUs. No negative control here: with the production files as on `main` the test doesn't build, because `StorageTexture::SmallStoreBytes` is new. Written with AI assistance.
xXPBSXx commented 2026-10-13 04:39:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T04:39:02Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main 3779597 together with four other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:

  • tests/execution/FlipStoresSmallResults.cpp (agc_driver_flip_stores_small_results): 3 of 3 runs passed.

Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6138356558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841#issuecomment-6138356558). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T04:39:02Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main `3779597` together with four other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it: - `tests/execution/FlipStoresSmallResults.cpp` (`agc_driver_flip_stores_small_results`): 3 of 3 runs passed. Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed. AI-assisted (Claude Code).
gjonn commented 2026-10-14 13:14:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-14T13:14:11Z.


Rebased onto main 63fb0cd92 (force-push, one commit, 0b5a4aae3). The only conflict was in Texture.hpp, where main added SaveBeforeUnwatch next to SmallStoreBytes and StoreSmallPending; both are kept. agc_driver_flip_stores_small_results passes on Windows (MinGW GCC, Release, RTX 5090 Laptop), and tools/check_conventions.py passes.

Thanks @kr4y4 and @xXPBSXx for the runs.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6169206504 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841#issuecomment-6169206504). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-14T13:14:11Z. --- Rebased onto `main` 63fb0cd92 (force-push, one commit, 0b5a4aae3). The only conflict was in `Texture.hpp`, where `main` added `SaveBeforeUnwatch` next to `SmallStoreBytes` and `StoreSmallPending`; both are kept. `agc_driver_flip_stores_small_results` passes on Windows (MinGW GCC, Release, RTX 5090 Laptop), and `tools/check_conventions.py` passes. Thanks @kr4y4 and @xXPBSXx for the runs. AI-assisted: yes
alfalfa-star commented 2026-10-14 14:02:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-14T14:02:05Z.


A game run: Kena: Bridge of Spirits (PPSA01802) shows this on its title screen, and this PR fixes it.

Setup: Linux (Arch, kernel 7.2.9), GCC, Release, RX 9070 XT (RADV, Mesa 26.2.4). Kena does not reach its title on plain main (it stops at buffer descriptor uses an unsupported type, which #5049 covers), so the runs are on a local stack: main f74ec639c + #5049 + #5156 + #4722 + #1201 (with APS5_PIPELINED_DRAWS=1) + local stand-ins, APS5_SYNC_FLIP=1. This PR's commit 0b5a4aae3 was cherry-picked on top. Each run starts from the same save and sits on the title; every presented frame from the same point on was dumped (APS5_DUMP_FRAMES) and its mean brightness measured.

  • Without this PR: the whole screen flickers in a 3-frame cycle, white, white, then a normal frame. 26 of 40 title frames were white (mean brightness above 0.9). [storage] write-backs by reason: memory access 284, no flip stores.
  • With this PR: 0 of 34 title frames white, the title is stable and correctly exposed. [storage] write-backs: flip 560/30.0 MiB over the run. No GPU reset.
  • agc_driver_flip_stores_small_results passes here (Linux, RADV).

The 3-frame cycle and the fix fit the exposure readback described above (small GPU-written results read by the CPU a frame later, here apparently rotating through three images). With queue 0 kept busy by the pipelined committer, the queue-idle store does not cover it.

Before finding this PR I had a narrower local version (images of at most 4x4 texels, stored through FlushPending at the flip); I am dropping it in favour of this one.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6170173016 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841#issuecomment-6170173016). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-14T14:02:05Z. --- A game run: Kena: Bridge of Spirits (PPSA01802) shows this on its title screen, and this PR fixes it. Setup: Linux (Arch, kernel 7.2.9), GCC, Release, RX 9070 XT (RADV, Mesa 26.2.4). Kena does not reach its title on plain `main` (it stops at `buffer descriptor uses an unsupported type`, which #5049 covers), so the runs are on a local stack: `main` f74ec639c + #5049 + #5156 + #4722 + #1201 (with `APS5_PIPELINED_DRAWS=1`) + local stand-ins, `APS5_SYNC_FLIP=1`. This PR's commit 0b5a4aae3 was cherry-picked on top. Each run starts from the same save and sits on the title; every presented frame from the same point on was dumped (`APS5_DUMP_FRAMES`) and its mean brightness measured. - Without this PR: the whole screen flickers in a 3-frame cycle, white, white, then a normal frame. 26 of 40 title frames were white (mean brightness above 0.9). `[storage]` write-backs by reason: `memory access 284`, no flip stores. - With this PR: 0 of 34 title frames white, the title is stable and correctly exposed. `[storage]` write-backs: `flip 560/30.0 MiB` over the run. No GPU reset. - `agc_driver_flip_stores_small_results` passes here (Linux, RADV). The 3-frame cycle and the fix fit the exposure readback described above (small GPU-written results read by the CPU a frame later, here apparently rotating through three images). With queue 0 kept busy by the pipelined committer, the queue-idle store does not cover it. Before finding this PR I had a narrower local version (images of at most 4x4 texels, stored through `FlushPending` at the flip); I am dropping it in favour of this one. AI-assisted (Claude Code).
adriadam10 commented 2026-10-14 17:17:56 +00:00 (Migrated from github.com)

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


Checked on Linux (merged onto current main): agc_driver_flip_stores_small_results passes, and it fails with the flip hook in PacketMemory.cpp removed. The rest of the agc scope is green except agc_driver_tessellation, which segfaults here with and without this change. Also confirmed by @kr4y4 (Windows, RTX 4070 + Intel iGPU), @xXPBSXx (Windows, RTX 4080 SUPER) and @alfalfa-star (Linux RADV, Kena title screen flicker gone). Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174093521 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4841#issuecomment-6174093521). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:17:56Z. --- Checked on Linux (merged onto current `main`): `agc_driver_flip_stores_small_results` passes, and it fails with the flip hook in `PacketMemory.cpp` removed. The rest of the agc scope is green except `agc_driver_tessellation`, which segfaults here with and without this change. Also confirmed by @kr4y4 (Windows, RTX 4070 + Intel iGPU), @xXPBSXx (Windows, RTX 4080 SUPER) and @alfalfa-star (Linux RADV, Kena title screen flicker gone). 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!4841
No description provided.