Skip to content

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

Merged
boykopovar merged 2 commits into
boykopovar:mainfrom
oneandonlydean:perf/agc-draw-input-reuse
Oct 2, 2026
Merged

boykopovar merged 2 commits into
boykopovar:mainfrom
oneandonlydean:perf/agc-draw-input-reuse

Conversation

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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: perf(agc): reuse unchanged draw input snapshots across draws #276 (this branch contains its commit; review only the second commit)
  • AI-assisted: yes

A recorded draw binds a CPU copy of each read-only guest buffer it reads
in place, made for every draw. PPSA21564's title-stage draws bind the
same large light-list buffers written by compute passes (up to 16 MiB
each) hundreds of times per frame, so about 4 ms of every draw went into
copying about 30 MiB into freshly allocated host buffers.

The recorder now keeps the snapshots, least recently used first, under a
256 MiB budget and 1024 entries. A later draw of the same range binds the
kept, immutable copy while GuestMemory::UnchangedSince holds for the
write generation collected right before the copy and the guest allocation
registry generation is unchanged. A CPU store (stamped by a collect), a
driver or GPU store (MarkWritten) or a registry mutation makes the next
draw copy again. Unwatched memory has no generation and is copied for
every draw, as before.

A recency list (each entry holding its own position) gives the eviction
victim and moves a used entry to the back in O(1).

Tests: agc_driver_recorder_tests binds a write-watched, host-imported
range twice and requires the same snapshot, then a new copy with the new
bytes after a CPU store, a MarkWritten store and a registry mutation; and
checks the least recently used eviction order at the entry cap, the byte
budget and a CPU store dropping a kept snapshot.
…es are unchanged

Every draw copied its index and vertex ranges from guest memory into
fresh buffers and scanned the indices, although the draws of a frame read
the same meshes again: about 10 GiB of vertex copies per 10 s at
PPSA21564's title.

CopyDrawInput now keeps the copy in the recorder's draw snapshot cache
and binds it again while the write tracker proves the range unchanged
since the copy was taken, with the same rules as the storage-buffer
snapshots: UnchangedSince on the collect made right before the copy and
an unchanged allocation registry. As GuestMemory::Read does, the flush
hook runs first, so pending GPU results over the range are stored and
stamped before the check. Unwatched memory is copied every time. An index
copy keeps its highest index; the device limit is still checked for every
draw.

The cache key includes the snapshot's use (storage, vertex, 16-bit or
32-bit index), so each use keeps its own buffer with its own usage. A
vertex copy runs from the buffer's start to its highest fetched element,
so draws over one shared vertex pool ask for many sizes: a vertex
snapshot also serves a shorter read at the same address while that
read's bytes are unchanged, and a new one replaces the shorter ones.
Vertex and index snapshots have their own least recently used pool
(1 GiB, 16384 entries), so storage copies, most of which change every
frame, do not push them out before their next use.

Tests: agc_driver_recorder_tests copies index and vertex ranges of
write-watched memory and requires a reuse (with the stored highest index)
while the bytes are unchanged, a new copy with the current bytes after a
CPU store, a MarkWritten store and a registry mutation, separate copies
per use, no reuse without a recorder, prefix reuse for vertex reads only,
and storage snapshots not evicting vertex snapshots.
@github-actions github-actions Bot added the agc libSceAgc* label Oct 2, 2026
@boykopovar
boykopovar merged commit 7831d57 into boykopovar:main Oct 2, 2026
3 checks passed
@oneandonlydean
oneandonlydean deleted the perf/agc-draw-input-reuse branch October 2, 2026 19:49
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 2, 2026
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 3, 2026
…oykopovar#304 boykopovar#305 boykopovar#306 boykopovar#322 boykopovar#409 boykopovar#416, boykopovar#276 boykopovar#277 boykopovar#275 boykopovar#307, shader ops boykopovar#226 boykopovar#255 boykopovar#260 boykopovar#280 boykopovar#308 boykopovar#309 boykopovar#311 boykopovar#318 boykopovar#320 boykopovar#324 boykopovar#326 boykopovar#379 boykopovar#384 boykopovar#399, boykopovar#261, kernel boykopovar#335 boykopovar#341 boykopovar#349 boykopovar#355, libs boykopovar#227 boykopovar#244 boykopovar#245 boykopovar#273 boykopovar#274 boykopovar#279 boykopovar#286 boykopovar#287 boykopovar#289 boykopovar#296 boykopovar#300 boykopovar#328 boykopovar#358 boykopovar#402) into astrobot

Where our PR merged in a revised form, main's version is taken; where
astrobot carries the same work plus later local commits, astrobot's code
stays and main's additions appear once.

Conflicts:
- AvPlayer (Export.cpp both libs, include/AvPlayer.hpp, src/Player.cpp,
  src/Source.cpp, AvPlayerLibrary.cmake, CMakeLists.txt, SceTypes.hpp,
  tests/GuestAvPlayer.cpp): main's boykopovar#307 (our reviewed squash, which carries
  9c8e7fd e9ea5c8 adcede3 fb4eeb9 953057c ac456ee). It links the
  shared ffmpeg-core target, so astrobot's second FFmpeg build goes:
  3rdparty/FFmpeg submodule, core/libs/FFmpeg.cmake and src/Exports.cpp.
- libScePngEnc: main's boykopovar#227 (supersedes OverkillLabs' encoder).
- libScePad Export.cpp: main's boykopovar#287 tilt correction (supersedes 49d45f5).
- libSceAudioPropagation Export.cpp: astrobot's object model, which
  defines every function boykopovar#289 declares as a stub.
- libSceSaveData.native Export.cpp: both (SaveDataHoldWrites and boykopovar#289's
  unknown exports).
- VideoOutDriver.cpp: astrobot's (2546bd1 counters around boykopovar#416's
  lastFlipVblank store, same value). tests/Flip.cpp: boykopovar#416's pacing test and
  boykopovar#417's after-GPU test both kept.
- boykopovar#278 files (DisplayFormat, VulkanDevice, GpuColorTransfer,
  ResidentPresent, agc CMakeLists): main's comment-free boykopovar#278 plus
  astrobot's P5 present-direct (b29728d), which removed DetileImage,
  ResidentPresent.hpp and the recorder-test hook; the resident present test
  stays its own executable.
- VulkanDevice.cpp/.hpp: astrobot's GDS, local memory probe, sample dumps
  and P5 code; boykopovar#303/boykopovar#409 comments as main has them; main's shader clock
  (boykopovar#324) and DepthSurface-free includes.
- RequestSerializer.cpp: version stays 8 (6 mesh passthrough, 7
  nonConstantImageOffsets, 8 astrobot's later field). boykopovar#303's auto-merged
  second write and read of nonConstantImageOffsets removed.
- SpirvEmitter.hpp, SpirvEmitterState.hpp, Recompiler.hpp/.cpp, StageIO.hpp,
  SpirvOptimizer.cpp, InstructionTranslator.cpp, ShaderReplay.cpp: local
  fields kept, boykopovar#303/boykopovar#305 comments as main has them.
- SpirvAluEmitterMath.cpp: v_readlane keeps the split-wave path and takes
  boykopovar#322's wrap at the wave size for the others.
- SpirvEmitter.cpp: astrobot's single-lane layout check.
- SpirvFlowEmitter.cpp: boykopovar#304 as merged (the second lane emits a shared
  value itself when the first lane defined nothing).
- SpirvImageEmitter.cpp: main's Offset operand placed before MinLod.
- boykopovar#304: main's WaveUniformValues (SpirvAnalysis.cpp) replaces astrobot's
  copy in SpirvWaveExchange.cpp.
- Recorder.hpp/.cpp, Draw.hpp, Graphics Draw.cpp, ShaderResources.cpp:
  astrobot's boykopovar#276/boykopovar#277 with its later snapshot work (per-use budgets,
  stale-snapshot refresh, profile counters, boykopovar#410, boykopovar#424, fan GS); boykopovar#409/boykopovar#306
  comments as main has them.
- PacketMemory.cpp: boykopovar#409 as merged plus astrobot's two opt-outs.
- tests: ShaderMemory.cpp takes boykopovar#303's and boykopovar#304's new tests; Recorder.cpp
  takes boykopovar#409's afterRecordedWorkTests and drops main's copies of the
  boykopovar#276/boykopovar#277 tests astrobot already has; Structurizer.cpp keeps boykopovar#413's case.

boykopovar#143 (NGG passthrough) stays as on astrobot: upstream did not touch its
files since the boykopovar#258 revert.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants