perf(agc): reuse unchanged draw input snapshots across draws #276

Merged
oneandonlydean merged 1 commit from upstream-pr/276 into main 2026-10-02 19:33:18 +00:00
oneandonlydean commented 2026-10-01 21:36:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-01T21:36:38Z.


What

A recorded draw binds a CPU copy of each read-only guest buffer it reads in place (ShaderResources::PrepareDrawBindings, from #76), and the copy is made for every draw. When the draws of a frame bind the same large buffers many times, most of the draw's time goes into those copies.

The recorder now keeps the snapshots in a cache (Recorder::ReusableDrawSnapshot / KeepDrawSnapshot), keyed by guest address and size, least recently used first, under a 256 MiB budget and 1024 entries. A later draw of the same range binds the kept copy, which is immutable once filled, only while both of these hold:

  • GuestMemory::UnchangedSince holds for the write generation collected right before the copy. A CPU store (stamped by a later collect) or a driver or GPU store (MarkWritten) fails it.
  • The guest allocation registry generation is unchanged, since a range unmapped and mapped again holds bytes no store stamped.

Anything else makes the next draw copy again. Unwatched memory has no generation and is copied for every draw, as before. The draw collects its range before the lookup, so every store made so far is stamped before the check. A recency list, in which each entry holds its own position, gives the eviction victim and moves a used entry to the back in O(1).

The proof is the write tracker from #121 plus the registry generation from the recorder in #5. The misaligned-snapshot range from #202 (item.address - item.adjustment) is the key, so the reused copy covers the same bytes the shader offset points into.

Measured in PPSA21564 on my integration branch:

  • The title-stage draws bind the same light-list buffers, which compute passes write (up to 16 MiB each), hundreds of times per frame. About 4 ms of every draw went into copying about 30 MiB into fresh host buffers, about 250 GB per 10 s once the title stage runs at speed. With the cache, those draws bind the kept copies.
  • A first version found the eviction victim with a scan of the map. At the 1024-entry cap (about 85k inserts per 10 s at the title), std::_Rb_tree_increment was 8% of the graphics queue worker's samples. The O(1) recency list removes that.

Tested

  • Linux (CachyOS, RTX 4090). New tests in agc_driver_recorder_tests. This is a GPU test that is built but not part of ctest, like the other recorder tests.
    • drawSnapshotReuseTests makes a write-watched, host-imported range and binds it through PrepareDrawBindings twice. It requires the same snapshot both times. It then requires a new copy holding the new bytes after a CPU store, a MarkWritten store and a registry mutation, and requires the recopied input to be kept. On current main it fails with "an unchanged draw input was copied again"; with this branch it passes.
    • drawSnapshotEvictionTests checks the least recently used order at the entry cap and that replacing an entry evicts nothing else. It also checks that a budget-sized snapshot evicts the rest and that a CPU store drops a kept snapshot. It uses the new API, so it has no main counterpart.
  • ctest --test-dir build --timeout 120: 96/96 pass on current main with this branch. agc_driver_recorder_tests, agc_driver_graphics_tests and agc_structurizer_tests pass when run directly.
  • Exercised in PPSA21564 on my integration branch (numbers above), where the same change has run through my visual regression check.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4709924473 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/276). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T21:36:38Z. --- ### What A recorded draw binds a CPU copy of each read-only guest buffer it reads in place (`ShaderResources::PrepareDrawBindings`, from #76), and the copy is made for every draw. When the draws of a frame bind the same large buffers many times, most of the draw's time goes into those copies. The recorder now keeps the snapshots in a cache (`Recorder::ReusableDrawSnapshot` / `KeepDrawSnapshot`), keyed by guest address and size, least recently used first, under a 256 MiB budget and 1024 entries. A later draw of the same range binds the kept copy, which is immutable once filled, only while both of these hold: - `GuestMemory::UnchangedSince` holds for the write generation collected right before the copy. A CPU store (stamped by a later collect) or a driver or GPU store (`MarkWritten`) fails it. - The guest allocation registry generation is unchanged, since a range unmapped and mapped again holds bytes no store stamped. Anything else makes the next draw copy again. Unwatched memory has no generation and is copied for every draw, as before. The draw collects its range before the lookup, so every store made so far is stamped before the check. A recency list, in which each entry holds its own position, gives the eviction victim and moves a used entry to the back in O(1). The proof is the write tracker from #121 plus the registry generation from the recorder in #5. The misaligned-snapshot range from #202 (`item.address - item.adjustment`) is the key, so the reused copy covers the same bytes the shader offset points into. Measured in PPSA21564 on my integration branch: - The title-stage draws bind the same light-list buffers, which compute passes write (up to 16 MiB each), hundreds of times per frame. About 4 ms of every draw went into copying about 30 MiB into fresh host buffers, about 250 GB per 10 s once the title stage runs at speed. With the cache, those draws bind the kept copies. - A first version found the eviction victim with a scan of the map. At the 1024-entry cap (about 85k inserts per 10 s at the title), `std::_Rb_tree_increment` was 8% of the graphics queue worker's samples. The O(1) recency list removes that. ### Tested - Linux (CachyOS, RTX 4090). New tests in `agc_driver_recorder_tests`. This is a GPU test that is built but not part of ctest, like the other recorder tests. - `drawSnapshotReuseTests` makes a write-watched, host-imported range and binds it through `PrepareDrawBindings` twice. It requires the same snapshot both times. It then requires a new copy holding the new bytes after a CPU store, a `MarkWritten` store and a registry mutation, and requires the recopied input to be kept. On current main it fails with "an unchanged draw input was copied again"; with this branch it passes. - `drawSnapshotEvictionTests` checks the least recently used order at the entry cap and that replacing an entry evicts nothing else. It also checks that a budget-sized snapshot evicts the rest and that a CPU store drops a kept snapshot. It uses the new API, so it has no main counterpart. - `ctest --test-dir build --timeout 120`: 96/96 pass on current main with this branch. `agc_driver_recorder_tests`, `agc_driver_graphics_tests` and `agc_structurizer_tests` pass when run directly. - Exercised in PPSA21564 on my integration branch (numbers above), where the same change has run through my visual regression check. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!276
No description provided.