Skip to content

perf(agc): draws that only update a buffer atomically continue one render pass - #122

Open
Thimo188 wants to merge 15 commits into
mainfrom
upstream-pr/9852
Open

Thimo188 wants to merge 15 commits into
mainfrom
upstream-pr/9852

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#9852, originally opened by EdouardSence.

Source commit: 1d0308e22ab570d32f774a92f0d89c51a49242d1. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

In ASTRO BOT's Sky Garden, one vertex shader that most draws use updates a 32-record counter buffer with buffer atomics. The recompiler counts an atomic element as both read and written, so every draw after the first saw a read-after-write hazard on the open render pass and began a new pass. Since boykopovar#15887, main also stages that counter through a device-local buffer for draws; the staged copy-in and copy-back each end the open pass (PassBreak::Command). On main f42d8b4a44, about 45K of the 265K to 275K draws per 10 s begin a render pass, 37K of them for a command; with this change about 10.7K of 290K to 300K draws, 2.1K for a command.

This is the atomic counterpart of boykopovar#7340, which left atomic buffers out on purpose.

  • Recompiler: BufferResource::plain records any non-atomic access of a buffer (Merge). DescriptorBinding::bufferAtomicOnly marks an element whose every access is an atomic: atomic, not plain, and not aliased by an image (imageAlias). The shader disk cache stores both.
  • Binding: a draw binds its bufferAtomicOnly elements of host imports in place (Region::inPlace, checked by GuestBufferMemory::stagingWanted) instead of staging them. Dispatches keep staging them (the histogram kernels atomic staging was added for have no render pass), and an element a draw also loads or stores stays staged. APS5_DRAW_ATOMIC_STAGING=1 restores staging for draws.
  • ShaderResources::AtomicOnlyRanges(): the guest ranges of atomic-only elements that no other allocation of the draw overlaps, built like WriteOnlyRanges().
  • At record time, recordDraw keeps only the ranges whose every write by the draw lands in a host import bound in place (GuestBufferMemory::WrittenInPlace). A CPU copy already makes the draw unknownMemory, and a GPU copy or a staged copy keeps the previous behaviour. recordDraw takes the kept ranges out of the draw's hazard reads and writes and passes them as PassAccess::atomics. The recorder's write tracking (NoteAccess, pending writes) still sees them as written.
  • PassHazards:
    • Atomic ranges conflict with reads (read after write) and with read-write and write-only uses (write after write), in both directions, and with unknown reads or writes.
    • Atomic ranges of different draws over the same bytes do not conflict. The guest GPU leaves atomics of draws that no barrier separates unordered as well.
    • SealWriteOnly() also moves the open pass's atomic ranges into its writes. A flush packet between two draws still orders their atomics.
  • recordDraw also keeps the device read count up to date after the write-only erase, so the atomic erase stays within the draw's own device reads.
  • APS5_NO_ATOMIC_PASSES=1 restores the previous hazard rule.

Overlaps (textual only; whichever lands second adapts):

  • Shader disk cache format: main f42d8b4a44 and current main e917850d23 are at 68; this PR writes 69. An open PR that bumps the same FormatVersion line takes the next free version after whichever lands first.
  • git merge-tree of this head against current main e917850d23 is clean. tools/pr_overlap.py lists open PRs that touch the same files (mostly the docs/dev/ENVIRONMENT.md table); none implements the same change.

Tested

Exact revisions: this PR at a4e218f8cd. The main side is f42d8b4a44, the main that this head last merged (its merge-base with main), so the two builds differ only by this PR.

  • Linux (CachyOS, kernel 7.3.1), GCC, Release, RX 9070 XT (RADV Mesa 26.2.5), Ryzen 7 7800X3D, 31 GiB.
  • Focused tests on both revisions, same machine and GPU, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R <pattern> -V:
    • The set: agc_driver_buffer_atomics, agc_driver_global_atomics, agc_driver_recorded_draw_staging, agc_driver_graphics, agc_driver_recorder, agc_pass_hazards, agc_shader_disk_cache. This branch adds agc_driver_atomic_passes and agc_driver_atomic_passes_split, which do not exist on main.
    • main passes 6 and this branch 8; agc_driver_recorded_draw_staging skips on both (this device does not keep host imports write-watched). Neither log has a VUID- line or was not found but was requested by env var VK_INSTANCE_LAYERS, and in both the loader loads VkLayer_khronos_validation.json. Logs: main, this PR.
  • New tests:
    • agc_driver_atomic_passes (tests/execution/AtomicPasses.cpp): a pixel shader adds 1 into one counter with buffer_atomic_add. One draw adds Width×Height. Two consecutive draws into a host-imported counter add twice that, and the second continues the first's pass. Two draws into a copied counter (not a guest allocation) add twice that, and the second begins a new pass. agc_driver_atomic_passes_split runs the same with APS5_NO_ATOMIC_PASSES=1, where no draw continues and both sums still match.
    • agc_driver_recorder: a 64-byte atomic counter added as a draw's atomic-only element is bound in place and WrittenInPlace reports it; added as a plain atomic element it is staged.
    • testAtomics in agc_pass_hazards covers each hazard pair and the seal.
    • agc_shader_disk_cache round-trips bufferAtomicOnly and checks it in the binding plan.
  • Speed setup:
    • ASTRO BOT PPSA21567 01.019.000, Sky Garden (-lvl underwater_aerial_garden), headless gamescope 3840×2160, title defaults, no input, 180 s per run.
    • APS5_NO_SNAPSHOT_CHECK=1 APS5_HOST_IMPORT_MIB=11815 APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_EVENTS=4194304 APS5_TRACE_SECONDS=40:25 on both sides.
    • Each revision has its own relinked run directory and shader cache. One discarded warm-up run per side, then main, PR, main, PR.
    • Every run of both revisions drops to 1 to 8 presents per 10 s at some point after the fly-in (between 60 and 145 s, the shape perf: ASTRO BOT (PPSA21567) first level drops from 3 to 0.7 FPS as the device-local heap fills; with the staging switches off it holds 3.3 FPS boykopovar/AnyPS5#18020 describes on Windows), and some recover. The table uses the 10 s intervals of the fly-in before that drop, without the level's first interval.
Build intervals presents / 10 s frame p50 ms GPU busy ms/present render passes begun / 10 s of them command
main f42d8b4a44, round 1 30-60 s 45 to 46 218 to 223 138.3 to 143.8 44807 to 45226 36965 to 37414
this PR a4e218f8cd, round 1 40-60 s 51 to 52 196 101.5 to 101.8 10865 to 10893 2088 to 2106
main, round 2 30-91 s 46 to 47 217 to 221 138.1 to 144.4 44593 to 45577 36859 to 37805
this PR, round 2 57-118 s 50 to 53 190 to 202 102.2 to 103.3 10467 to 10993 1992 to 2225

The 25 s trace windows (trace_digest.py and performance_compare.py):

Round main p50 / p90 ms, GPU this PR p50 / p90 ms, GPU
1 219.0 / 223.9, 143.9 195.8 / 199.7, 101.6
2 219.4 / 223.1, 142.3 203.7 / 224.7, 120.7

The PR's round 2 run took about 15 s longer to load, so its window starts with the level's first slow interval (155 ms GPU) and ends at 65 s; the 10 s intervals after it stay at 102 to 103 ms. Round 1's main window ends as that run's drop begins (one 3.4 s frame) and the PR's has one 5.2 s frame; p99 and max in those windows come from those single frames. No trace dropped events.

Frame p50 drops by about 10 % and GPU time per present by about 29 %; render passes begun drop by about 76 %. The queue workers stay at ~99 % on both sides.

Artifacts for all four runs (release): traces, aps5-summary.jsonl, trace_digest.py output, and the performance_compare.py output of each round. Round 1 summary lines:

{"commit":"f42d8b4a447f","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"11815","APS5_NO_SNAPSHOT_CHECK":"1","APS5_PROFILE_DRAW":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"4194304","APS5_TRACE_SECONDS":"40:25","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 9070 XT (RADV GFX1201)","driver":"radv Mesa 26.2.5-arch3.1","os":"Linux 7.3.1-1-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31227,"title_id":"PPSA21567","title":"ASTRO BOT","content_version":"01.019.000","resolution":"3840x2160","window":{"spec":"seconds 40:25","first_present":502,"ms":25391.530},"presents":97,"frame_ms":{"p50":218.978,"p90":223.867,"p99":3449.562,"max":3449.562},"gpu_busy_ms_per_present":143.887,"worker_busy_pct":{"queue worker 0x0":98.617,"queue worker 0x48":87.305,"queue worker 0x40":98.323,"queue worker 0x46":0.352},"top_phases_ms_per_frame":{"Driver.Packet":743.569,"Driver.Packet.memory_wait":436.728,"Driver.Packet.draw":212.982,"Driver.Draw":211.468,"Graphics.Draw":165.893,"Graphics.Draw.draw_and_resource_release":144.631,"Driver.Draw.shader_compile_and_link":97.187,"Driver.Draw.draw_enqueued":94.765},"owned_draw_counts":{},"trace_dropped":false,"trace":"aps5-trace-f42d8b4a447f-339619.json.gz"}
{"commit":"a4e218f8cdb4","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"11815","APS5_NO_SNAPSHOT_CHECK":"1","APS5_PROFILE_DRAW":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"4194304","APS5_TRACE_SECONDS":"40:25","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 9070 XT (RADV GFX1201)","driver":"radv Mesa 26.2.5-arch3.1","os":"Linux 7.3.1-1-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31227,"title_id":"PPSA21567","title":"ASTRO BOT","content_version":"01.019.000","resolution":"3840x2160","window":{"spec":"seconds 40:25","first_present":466,"ms":26013.739},"presents":107,"frame_ms":{"p50":195.816,"p90":199.718,"p99":205.315,"max":5212.140},"gpu_busy_ms_per_present":101.648,"worker_busy_pct":{"queue worker 0x0":98.592,"queue worker 0x48":80.709,"queue worker 0x40":99.905,"queue worker 0x46":0.316},"top_phases_ms_per_frame":{"Driver.Packet":678.196,"Driver.Packet.memory_wait":425.483,"Driver.Packet.draw":195.238,"Driver.Draw":193.727,"Graphics.Draw":164.756,"Graphics.Draw.draw_and_resource_release":143.956,"Driver.Draw.shader_compile_and_link":95.814,"Shader.DrawStage":80.336},"owned_draw_counts":{},"trace_dropped":false,"trace":"aps5-trace-a4e218f8cdb4-343655.json.gz"}
  • Windows, same GPU: BunnyFl's run of this head with and without APS5_DRAW_ATOMIC_STAGING=1 (in the conversation) shows the same direction: about a quarter of the render passes and ~100 against ~177 ms GPU per present.
  • Not tested: NVIDIA, Intel, other titles.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9070 XT, RADV Mesa 26.2.5, Linux 7.3.1 (CachyOS)
  • AI-assisted: yes

EdouardSence and others added 15 commits October 22, 2026 22:27
…nder pass

A guest buffer element the recompiler proves is only accessed through
atomics (BufferResource::plain is false) and that no other allocation of
the draw overlaps is passed to PassHazards as an atomic range. Atomic
ranges of different draws over the same bytes do not conflict; they
conflict with reads, read-write uses and write-only stores in both
directions, and a flush packet seals them like write-only stores.

APS5_NO_ATOMIC_PASSES=1 restores the previous behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ting draws

An atomic-only range joins the pass's atomics only when every write of
the draw over it lands in a host import bound in place
(GuestBufferMemory::WrittenInPlace). A copied region (a CPU copy, a GPU
copy before its copy-back) keeps the previous behaviour.

agc_driver_atomic_passes records two draws whose pixel shader adds 1
into one counter, once in a host import and once in a copied buffer, and
checks the counter and how many draws continued a pass;
agc_driver_atomic_passes_split runs it with APS5_NO_ATOMIC_PASSES=1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ing fields

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve Draw.cpp (keep AtomicPasses, take main's recordDraw trace
argument and subdraw counter) and ShaderDiskCache.cpp (BufferResource
13 fields from this branch, ImageResource 43 fields from main). Main
took format 47 for the resinfo change, so the combined layout is 48.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	core/libs/prx/libSceAgcDriver/CMakeLists.txt
# Conflicts:
#	core/shader/recompiler/ShaderDiskCache.cpp
#	core/shader/recompiler/ShaderDiskCache.hpp
Resolve the conflicts with main f42d8b4:
- bufferAtomicOnly becomes ElementFlags like main's other per-element
  flags, and descriptorBindingBytes counts it.
- The split test moves to tests/cmake/agc_driver_atomic_passes_tests.cmake.
- Shader cache format 69 (main is at 68); DescriptorBinding 920 bytes,
  BufferResource 13 fields, ImageResource 50.
- GuestBufferMemory keeps both WrittenInPlace and main's RunAccesses /
  MarkStagedWrites.

Co-Authored-By: Claude <noreply@anthropic.com>
Main stages atomic elements of host imports in device memory. For a
draw, the staged copy-in and copy-back each end the open render pass,
so draws that only add into a small counter still break the pass, now
as PassBreak::Command, and the atomic-pass rule has nothing to remove.

Draws now bind their bufferAtomicOnly elements in place; dispatches
keep staging them (the histogram kernels it was added for have no
render pass), and elements a draw also loads or stores stay staged.
APS5_DRAW_ATOMIC_STAGING=1 restores staging for draws.

ASTRO BOT (PPSA21567 01.019.000), Sky Garden, RX 9070 XT / RADV,
APS5_HOST_IMPORT_MIB=11815, 60 s of steady fly-in: about 11.1K render
passes begun per 10 s against 47.3K with APS5_DRAW_ATOMIC_STAGING=1,
GPU busy about 95 against 137 ms per present.

Co-Authored-By: Claude <noreply@anthropic.com>
…p rule

addGuestBuffer: main sweeps write-only elements of any record stride on discrete GPUs (the stride test is now Stride() != 0 and the 16-byte constant is gone). This change's in-place argument to AddWritable and its APS5_DRAW_ATOMIC_STAGING switch follow that line unchanged. Sweeping applies to compute dispatches and the in-place flag to draw builds, so the two do not meet.
ShaderDiskCache: 13 BufferResource fields from this change and 51 ImageResource fields from main.
Main added imageUintStorage to DescriptorBinding and moved the shader
disk cache format to 88; this branch adds bufferAtomicOnly. Keep both
flags in descriptorBindingBytes, set the DescriptorBinding size check to
976 (both sides had moved it from 864 to 920 for one flag each), and move
the format to 89.
@github-actions github-actions Bot added perf Performance change needs rebase Conflicts with main, rebase needed labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants