perf(agc): reuse a draw's index and vertex copies while the guest bytes are unchanged #277

Merged
oneandonlydean merged 2 commits from upstream-pr/277 into main 2026-10-02 19:33:33 +00:00
oneandonlydean commented 2026-10-01 21:36:42 +00:00 (Migrated from github.com)

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


What

Every draw copies its index and vertex ranges from guest memory into fresh buffers and scans the indices for the highest one, although the draws of a frame read the same meshes again.

CopyDrawInput now keeps those copies in the recorder's draw snapshot cache from #276. It binds a kept copy again while the write tracker proves the range unchanged since the copy was taken, with the same rules as the storage-buffer snapshots:

  • UnchangedSince holds on the collect made right before the copy;
  • the allocation registry is unchanged.

As GuestMemory::Read does, FlushGpuWrites runs first, so pending GPU results over the range are stored and stamped before the check. Unwatched memory is copied every time, and a draw without a recorder copies as before. The caller still checks the range's access and alignment for every draw. An index copy keeps its highest index, and the device's maxDrawIndexedIndexValue is still checked for every draw.

  • Use in the key. The cache key includes the snapshot's use (storage, vertex, 16-bit index or 32-bit index). Each use keeps its own buffer with its own usage flags, and an index copy's highest index depends on the index size.
  • Shorter vertex reads. A vertex copy runs from the buffer's start to its highest fetched element, so draws over one shared vertex pool ask for as many sizes as they have highest indices. A vertex snapshot also serves a shorter read at the same address while that read's bytes are unchanged, and the draw fetches only that prefix. A new vertex snapshot replaces the shorter ones at its address. Index reads need their exact size, because the stored highest index covers the whole copy.
  • Separate pool. Vertex and index snapshots have their own least recently used pool, of 1 GiB and 16384 entries. Storage snapshots, most of which change every frame, keep the 256 MiB, 1024-entry pool and no longer push vertex and index snapshots out before their next use.

Measured in PPSA21564 on my integration branch:

  • Before the reuse, about 10 GiB of vertex copies per 10 s at the title.
  • In the level's landing cinematic, the shared pool still let storage copies push the vertex and index snapshots out, and draws copied about 20 GiB of vertex data per 10 s. Separate pools fixed that.

Tested

  • Linux (CachyOS, RTX 4090). New drawInputReuseTests in agc_driver_recorder_tests (a GPU test, built but not part of ctest) runs over write-watched memory. It requires:

    • a reuse, with the stored highest index, while the bytes are unchanged;
    • a new copy with the current bytes after a CPU store, after a MarkWritten store and after a registry mutation;
    • separate copies per use, and no reuse without a recorder;
    • prefix reuse for vertex reads only: not for a longer read, another address or an index read, and not past a store outside the prefix.

    drawSnapshotEvictionTests now also requires that filling the storage pool past its cap keeps a vertex snapshot.

    These tests call CopyDrawInput, which this PR adds, so they cannot run on the base branch. Instead I confirmed that each part fails without its change:

    • copying for every draw fails with "an unchanged draw input was copied again";
    • an exact-size vertex lookup fails with "a shorter vertex read did not reuse the longer snapshot";
    • one shared pool fails with "storage snapshots evicted a vertex snapshot".
  • ctest --test-dir build --timeout 120: 96/96 pass with this branch on current main. 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), which passes 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: #276 (this branch contains its commit; review only the second commit)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4709925062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/277). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T21:36:42Z. --- ### What Every draw copies its index and vertex ranges from guest memory into fresh buffers and scans the indices for the highest one, although the draws of a frame read the same meshes again. `CopyDrawInput` now keeps those copies in the recorder's draw snapshot cache from #276. It binds a kept copy again while the write tracker proves the range unchanged since the copy was taken, with the same rules as the storage-buffer snapshots: - `UnchangedSince` holds on the collect made right before the copy; - the allocation registry is unchanged. As `GuestMemory::Read` does, `FlushGpuWrites` runs first, so pending GPU results over the range are stored and stamped before the check. Unwatched memory is copied every time, and a draw without a recorder copies as before. The caller still checks the range's access and alignment for every draw. An index copy keeps its highest index, and the device's `maxDrawIndexedIndexValue` is still checked for every draw. - **Use in the key.** The cache key includes the snapshot's use (storage, vertex, 16-bit index or 32-bit index). Each use keeps its own buffer with its own usage flags, and an index copy's highest index depends on the index size. - **Shorter vertex reads.** A vertex copy runs from the buffer's start to its highest fetched element, so draws over one shared vertex pool ask for as many sizes as they have highest indices. A vertex snapshot also serves a shorter read at the same address while that read's bytes are unchanged, and the draw fetches only that prefix. A new vertex snapshot replaces the shorter ones at its address. Index reads need their exact size, because the stored highest index covers the whole copy. - **Separate pool.** Vertex and index snapshots have their own least recently used pool, of 1 GiB and 16384 entries. Storage snapshots, most of which change every frame, keep the 256 MiB, 1024-entry pool and no longer push vertex and index snapshots out before their next use. Measured in PPSA21564 on my integration branch: - Before the reuse, about 10 GiB of vertex copies per 10 s at the title. - In the level's landing cinematic, the shared pool still let storage copies push the vertex and index snapshots out, and draws copied about 20 GiB of vertex data per 10 s. Separate pools fixed that. ### Tested - Linux (CachyOS, RTX 4090). New `drawInputReuseTests` in `agc_driver_recorder_tests` (a GPU test, built but not part of ctest) runs over write-watched memory. It requires: - a reuse, with the stored highest index, while the bytes are unchanged; - a new copy with the current bytes after a CPU store, after a `MarkWritten` store and after a registry mutation; - separate copies per use, and no reuse without a recorder; - prefix reuse for vertex reads only: not for a longer read, another address or an index read, and not past a store outside the prefix. `drawSnapshotEvictionTests` now also requires that filling the storage pool past its cap keeps a vertex snapshot. These tests call `CopyDrawInput`, which this PR adds, so they cannot run on the base branch. Instead I confirmed that each part fails without its change: - copying for every draw fails with "an unchanged draw input was copied again"; - an exact-size vertex lookup fails with "a shorter vertex read did not reuse the longer snapshot"; - one shared pool fails with "storage snapshots evicted a vertex snapshot". - `ctest --test-dir build --timeout 120`: 96/96 pass with this branch on current main. `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), which passes 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: #276 (this branch contains its commit; review only the second commit) - [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!277
No description provided.