perf(agc): stage recorded draws' written buffers in device memory #15887

Merged
TripyFox merged 3 commits from upstream-pr/15887 into main 2026-11-04 23:43:16 +00:00
TripyFox commented 2026-11-04 22:12:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TripyFox on 2026-11-04T22:12:59Z.


What

Recorded draws now get the device-local staging that dispatches already use. If a recorded draw writes a buffer element inside a host import, or does atomics on one, and the element fits the staging window, the element is copied to a device-local buffer. The draw is bound to that copy, and RecordCopyBacks copies the changed bytes back. Recorded draws already reach RecordCopyBacks through MarkGpuWrites.

Staging is not used in three cases:

  • A stage uses BDA page tables, because the address tables point at the host addresses.
  • The draw is not recordable.
  • A draw that turns out synchronous after its build would have staged regions (HasStagedCopies). It is rebuilt unstaged, the same way a build with moved ranges is today, because a CPU-side store cannot write a device-local copy.

APS5_NO_DRAW_STAGING=1 keeps draws' elements in place. The new row is in docs/dev/ENVIRONMENT.md, the Device-local staging debt entry names recorded draws, and a new Draw device-local staging debt entry records the per-use whole-element copy.

Cause, from #15560: ASTRO BOT's clustered light assignment is a pixel shader that ds_appends on GDS. It scatters 8 B and 12 B node records into 8 MiB and 12 MiB buffers. Those buffers are host imports in system RAM, so every store crosses the bus. The draw took about 16 ms of GPU time per frame on an RTX 5090. The atomic is not the cost. In a RenderDoc replay of the same frame, three variants show this:

  • The original takes 16.2 ms.
  • Keeping the GDS atomic but folding the node index into 256 records takes 0.49 ms.
  • Removing the atomic but scattering the stores the same way still takes 11.7–12.8 ms.

With this change the draw takes 8.6 ms (APS5_PROFILE_GPU=1, attribution only). The 12 MiB buffer stays in host memory because it exceeds the 8 MiB written-staging window on a discrete GPU.

Tested

Windows 11, NVIDIA GeForce RTX 5090, driver 621.01, Ryzen 7 9800X3D, ASTRO BOT (PPSA21567) 01.019.000, Crash Site, 3840x2160, no input.

The A/B/A/B runs were 240 s each with APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_BENCH=1 after a warm-up run, in the order main, change, change, main. The figures are means over the intervals from 40 to 240 s.

presents / 10 s frame p50 ms frame p99 ms GPU busy ms / present
main r1 105.6 95.6 104.6 75.3
change r1 108.2 91.9 104.0 71.5
change r2 110.1 90.2 110.5 70.5
main r2 105.0 96.2 107.2 75.8
mean change − main +3.6 % −4.9 (−5.1 %) +1.3 (noise) −4.6 (−6.0 %)

tools/performance_compare.py on matched 10 s intervals at about 150 s:

pair 1  frame_ms.p50 95.196 -> 92.455 (-2.879 %)   gpu_busy_ms_per_present 75.316 -> 72.207 (-4.128 %)
pair 2  frame_ms.p50 97.177 -> 93.960 (-3.310 %)   gpu_busy_ms_per_present 75.884 -> 72.668 (-4.238 %)

On a local stack of open PRs (#15569, #9712, #15456, #10678, #15682) with APS5_NO_LABEL_READBACK_STORE=1, the same change gave p50 −5.7 ms (−7.6 %) and GPU busy −5.1 ms (−11.8 %).

GPU tests: Windows 11, RTX 5090, 621.01, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation.

  • New agc_driver_recorded_draw_staging. A recorded pixel draw appends to the GDS and stores one record per fragment, by the returned index, into a 256 KiB guest buffer. The test checks four things:

    • the counter equals the 256 covered fragments;
    • records 0..255 are exactly the covered fragments;
    • the rest of the buffer still holds its sentinel;
    • the target is drawn.

    It runs a fresh build, a reused build, and a draw whose device-local heap reports full. That last run must refuse a staging shadow (StagingShadowsRefused). On main, that check fails because draws never try to stage; the functional checks pass there. APS5_TRACE_STAGING=1 shows the node buffer staged on the change and not with APS5_NO_DRAW_STAGING=1 or APS5_SYNC_DRAWS=1. It passes with APS5_NO_DRAW_STAGING=1, APS5_SYNC_DRAWS=1 and APS5_DRAW_TRANSITIONS=1, which takes the rebuild-unstaged branch. It also passed 5 of 5 repeated runs.

  • 36 related execution tests, the same on main and on the change, also with APS5_NO_DRAW_STAGING=1: 35 pass, and agc_driver_vertex_elected_atomic_max skips because the device runs vertex programs in subgroups. The tests are:

    • GdsAppendConsume, DsAppendConsume, GdsDmaFill
    • OverlapStagedCopies, StagedClearSampledRead, AddressDrawSpaceReuse
    • CopiedWriterCopy, CopiedWriterLabelOrder
    • PixelInterlock, PixelLdsReadModifyWrite
    • RecordedKeptBytes, DrawRecaptureOnChange, StencilReplay, PassInputHazard
    • LinearTargetDrawBindings, LinearTargetCpuWrites, LinearTargetDirectWriteCompletion
    • DrawOffsetIndices, DrawRelocateUserValues, DrawWriteRanges
    • DrawCacheReuse, DrawCacheRelocation, ManyBuffers
    • PipelinedIndirectDraw, QueuedIndexReadOrder, PipelineJournal, PreparedShaders
    • Tessellation, VertexElectedAtomicMax
    • DepthStorageDispatch, DispatchMove, DispatchOverlap
    • StorageWriteBackNoWindow, StorageWriteBackOverhang, StorageFlushOrdering
    • AdaptiveStaging
  • Validation: no VUID- messages on main or on the change.

On the rebased branch (main b611e634f7): all 37 tests pass with validation, also with APS5_NO_DRAW_STAGING=1, and no VUID- messages appear. tools/check_conventions.py --base origin/main reports "All conventions checks passed."

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 (#15560 stays open; see the follow-up below)
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5090, driver 621.01, Windows 11 (10.0.26200)
  • AI-assisted: yes

Follow-up, not in this PR: the frame has six draws that bind the same two node buffers. Each staged use copies the whole element in and back. A 16 MiB draw window removes the GDS draw's cost entirely (<0.13 ms), but the extra copies cost 6.1 ms in and 8.8 ms out per frame. One device-local copy shared across consecutive GPU users, with CPU-write and foreign-user hazard checks, would keep most of the remaining ~8 ms.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5162457087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15887). Originally posted by [TripyFox](https://github.com/TripyFox) on 2026-11-04T22:12:59Z. --- ### What Recorded draws now get the device-local staging that dispatches already use. If a recorded draw writes a buffer element inside a host import, or does atomics on one, and the element fits the staging window, the element is copied to a device-local buffer. The draw is bound to that copy, and `RecordCopyBacks` copies the changed bytes back. Recorded draws already reach `RecordCopyBacks` through `MarkGpuWrites`. Staging is not used in three cases: - A stage uses BDA page tables, because the address tables point at the host addresses. - The draw is not recordable. - A draw that turns out synchronous after its build would have staged regions (`HasStagedCopies`). It is rebuilt unstaged, the same way a build with moved ranges is today, because a CPU-side store cannot write a device-local copy. `APS5_NO_DRAW_STAGING=1` keeps draws' elements in place. The new row is in docs/dev/ENVIRONMENT.md, the Device-local staging debt entry names recorded draws, and a new Draw device-local staging debt entry records the per-use whole-element copy. Cause, from #15560: ASTRO BOT's clustered light assignment is a pixel shader that `ds_append`s on GDS. It scatters 8 B and 12 B node records into 8 MiB and 12 MiB buffers. Those buffers are host imports in system RAM, so every store crosses the bus. The draw took about 16 ms of GPU time per frame on an RTX 5090. The atomic is not the cost. In a RenderDoc replay of the same frame, three variants show this: - The original takes 16.2 ms. - Keeping the GDS atomic but folding the node index into 256 records takes 0.49 ms. - Removing the atomic but scattering the stores the same way still takes 11.7–12.8 ms. With this change the draw takes 8.6 ms (`APS5_PROFILE_GPU=1`, attribution only). The 12 MiB buffer stays in host memory because it exceeds the 8 MiB written-staging window on a discrete GPU. ### Tested Windows 11, NVIDIA GeForce RTX 5090, driver 621.01, Ryzen 7 9800X3D, ASTRO BOT (PPSA21567) 01.019.000, Crash Site, 3840x2160, no input. The A/B/A/B runs were 240 s each with `APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_BENCH=1` after a warm-up run, in the order main, change, change, main. The figures are means over the intervals from 40 to 240 s. | | presents / 10 s | frame p50 ms | frame p99 ms | GPU busy ms / present | |---|---|---|---|---| | main r1 | 105.6 | 95.6 | 104.6 | 75.3 | | change r1 | 108.2 | 91.9 | 104.0 | 71.5 | | change r2 | 110.1 | 90.2 | 110.5 | 70.5 | | main r2 | 105.0 | 96.2 | 107.2 | 75.8 | | **mean change − main** | **+3.6 %** | **−4.9 (−5.1 %)** | +1.3 (noise) | **−4.6 (−6.0 %)** | `tools/performance_compare.py` on matched 10 s intervals at about 150 s: ``` pair 1 frame_ms.p50 95.196 -> 92.455 (-2.879 %) gpu_busy_ms_per_present 75.316 -> 72.207 (-4.128 %) pair 2 frame_ms.p50 97.177 -> 93.960 (-3.310 %) gpu_busy_ms_per_present 75.884 -> 72.668 (-4.238 %) ``` On a local stack of open PRs (#15569, #9712, #15456, #10678, #15682) with `APS5_NO_LABEL_READBACK_STORE=1`, the same change gave p50 −5.7 ms (−7.6 %) and GPU busy −5.1 ms (−11.8 %). GPU tests: Windows 11, RTX 5090, 621.01, with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. - **New `agc_driver_recorded_draw_staging`.** A recorded pixel draw appends to the GDS and stores one record per fragment, by the returned index, into a 256 KiB guest buffer. The test checks four things: - the counter equals the 256 covered fragments; - records 0..255 are exactly the covered fragments; - the rest of the buffer still holds its sentinel; - the target is drawn. It runs a fresh build, a reused build, and a draw whose device-local heap reports full. That last run must refuse a staging shadow (`StagingShadowsRefused`). On main, that check fails because draws never try to stage; the functional checks pass there. `APS5_TRACE_STAGING=1` shows the node buffer staged on the change and not with `APS5_NO_DRAW_STAGING=1` or `APS5_SYNC_DRAWS=1`. It passes with `APS5_NO_DRAW_STAGING=1`, `APS5_SYNC_DRAWS=1` and `APS5_DRAW_TRANSITIONS=1`, which takes the rebuild-unstaged branch. It also passed 5 of 5 repeated runs. - **36 related execution tests,** the same on main and on the change, also with `APS5_NO_DRAW_STAGING=1`: 35 pass, and `agc_driver_vertex_elected_atomic_max` skips because the device runs vertex programs in subgroups. The tests are: - GdsAppendConsume, DsAppendConsume, GdsDmaFill - OverlapStagedCopies, StagedClearSampledRead, AddressDrawSpaceReuse - CopiedWriterCopy, CopiedWriterLabelOrder - PixelInterlock, PixelLdsReadModifyWrite - RecordedKeptBytes, DrawRecaptureOnChange, StencilReplay, PassInputHazard - LinearTargetDrawBindings, LinearTargetCpuWrites, LinearTargetDirectWriteCompletion - DrawOffsetIndices, DrawRelocateUserValues, DrawWriteRanges - DrawCacheReuse, DrawCacheRelocation, ManyBuffers - PipelinedIndirectDraw, QueuedIndexReadOrder, PipelineJournal, PreparedShaders - Tessellation, VertexElectedAtomicMax - DepthStorageDispatch, DispatchMove, DispatchOverlap - StorageWriteBackNoWindow, StorageWriteBackOverhang, StorageFlushOrdering - AdaptiveStaging - **Validation:** no `VUID-` messages on main or on the change. On the rebased branch (main b611e634f7): all 37 tests pass with validation, also with `APS5_NO_DRAW_STAGING=1`, and no `VUID-` messages appear. `tools/check_conventions.py --base origin/main` reports "All conventions checks passed." ### 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](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] Fixes: none (#15560 stays open; see the follow-up below) - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 5090, driver 621.01, Windows 11 (10.0.26200) - [x] AI-assisted: yes Follow-up, not in this PR: the frame has six draws that bind the same two node buffers. Each staged use copies the whole element in and back. A 16 MiB draw window removes the GDS draw's cost entirely (<0.13 ms), but the extra copies cost 6.1 ms in and 8.8 ms out per frame. One device-local copy shared across consecutive GPU users, with CPU-write and foreign-user hazard checks, would keep most of the remaining ~8 ms. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!15887
No description provided.