Skip to content

perf(agc): stage written-only buffer elements a dispatch sweeps on discrete GPUs - #7618

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
mrfelek:perf/stage-swept-write-only
Oct 25, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
mrfelek:perf/stage-swept-write-only

Conversation

@mrfelek

@mrfelek mrfelek commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

2688b76 stages a written V# element in a device-local buffer when a direct dispatch sweeps it: threads times the record size cover at least half of the element. main applies that up to 16 MiB, and only when the shader also reads the element. 6d92220 (#6486) extended it to raw elements through their access extent, still only for read and written elements. The message of 2688b76 explains why write-only elements keep the 2 MiB window: a kernel streaming once through a large buffer, like the engine's memcpy over 4–8 MiB video frames, would only gain the two copies.

Diablo II: Resurrected (PPSA03912) shows the other side of that. Once per frame it runs a compute dispatch of 144x144 groups of 8x8 threads (1,327,104 threads). Each thread writes into its own 160-byte record with buffer_store_dwordx4 (8 of them, so at most 128 of the 160 bytes), in a 212 MB write-only element (0x2a86e47f0+0xca80000, exactly threads x 160). One store instruction writes 16 bytes per lane, and neighbouring lanes are 160 bytes apart, so in place in the host import the stores are scattered. They take about 56–60 ms of GPU time per frame. Staged, the kernel takes about 3.5 ms, plus about 22 ms of bulk copies in and back.

Change, on discrete GPUs only (Context::discreteGpu, set from VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU):

  • ShaderResources::addGuestBuffer: a write-only element that a direct dispatch sweeps is staged as well when its V# stride is wider than the widest buffer store, 16 bytes (read || (context.discreteGpu && descriptor.Stride() > WidestBufferStoreBytes)). With records that wide, the lanes of every store land a record apart, which is what staging speeds up.
  • The swept cap goes from 16 MiB to 256 MiB (DiscreteSweptShadowMax).

Integrated GPUs keep today's rule. Their device-local memory is the same system RAM, so staging there gains little and pays for two copies.

What stays as it is:

  • a write-only element whose records are 16 bytes or less: one store can fill a whole record, so the lanes of a store write it contiguously, and it stays in place;
  • write-only raw elements (V# stride 0) are not swept, so they keep the 2 MiB window;
  • read and written elements, raw or strided, follow main's rule unchanged, apart from the larger cap on discrete GPUs;
  • indirect dispatches (unknown thread count) are not swept;
  • dispatches that touch only part of an element are not swept;
  • the copy-in is kept for write-only elements, because the copy-back covers the whole element and the stores may leave part of each record unwritten (in D2R at most 128 of each 160-byte record). The debt entry "Swept write-only staging" says when it could go.

Trade-off: a kernel with records wider than 16 bytes that it writes completely and once now pays two bulk copies instead of one pass of scattered in-place stores. If the memcpy kernel from 2688b76 binds its frames with a stride wider than 16 bytes, it falls under this; I couldn't find that title in the commit, so I haven't measured it. Records of 16 bytes or less stay in place even when their stores are narrower, because store widths are not recorded per instruction; that is the safe side.

Cost:

  • Memory: from the code, not measured: every shadow is its own device-local buffer, and a cached resource build keeps it, so D2R's 212 MB shadow stays allocated while that build is cached and a reused build records its copies again. When the device refuses the allocation (device memory or the allocation count exhausted, for example on a card close to its VRAM budget), the element is bound in place as before, [buffers] staging shadow of N bytes refused is printed and stagingRefused counts it; nothing aborts.
  • PCIe: on discrete GPUs the copies cross the bus. D2R moves 236 MiB in and 236 MiB back per present, about 4 GiB/s at 8.5 presents per second. That is a small share of an x16 link; on an x4 or x8 link (laptop eGPU, bifurcated slot) it is a visible share. I have no such machine to measure it. APS5_NO_WRITTEN_SHADOW=1 binds these elements in place again.

Tested

Linux: Ubuntu 26.10 (kernel 7.3), Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09 open), GCC 15.3.0, Release.

Windows: Windows 11 Pro 26200, i7-11850H, RTX A2000 Laptop GPU (NVIDIA 610.60), WinLibs GCC 15.2.0, Release.

Linux / AMD integrated: Ubuntu 26.04.1 (kernel 7.0), Ryzen 9 PRO 8945HS, Radeon 780M (RADV, Mesa 26.0.8, 2 GB carve-out), GCC 15.2.0, Release.

  • agc_driver_recorder readWrittenStagingTests:

    • The test device sets discreteGpu from the selected device's type.
    • A dispatch sweeping a written-only 4 MiB element of 32-byte records expects staging on a discrete GPU and in place otherwise.
    • New: a dispatch sweeping a written-only 4 MiB element of 16-byte records, which its stores write contiguously, binds it in place on every GPU.
    • A dispatch touching 32 KiB of a written-only element binds it in place.
    • A written-only raw (stride 0) element binds it in place.
    • A dispatch sweeping a written-only 31.25 MiB element of 160-byte records, and one sweeping a read and written element of that size, are staged on a discrete GPU and bound in place otherwise. The test block is 40 MiB for these cases.

    I ran the group on its own, through a local --tmp-staging-only switch that isn't part of this PR. It passes on the RTX 3080 Ti, on the Radeon 780M (integrated) and on the RTX A2000 under Windows at ab4c1fe, and on the RTX 3080 Ti again after the rebases onto main eaa5c92 and d14a186 (this head). On the RTX 3080 Ti I also ran it on main f71a311, on ab4c1fe and on this head with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (layer 1.4.357, loaded): all three pass with no VUID- message, so the change adds none. The full agc_driver_recorder_tests binary stops earlier, at (9) a scan under a pending write was kept, the same way as on main, so it's unrelated.

  • D2R (PPSA03912) on Linux, RTX 3080 Ti: in game with a saved character, the game's default settings, no input. main f71a311 and this head on top of it, each relinked with its own relinker. APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_SECONDS=170:10, 120 s from 60 s after the character loads, run alternately (main, this, this, main). Steady intervals from 100 s on:

main f71a311 this change (27876656b, same code as this head)
Presents per 10 s 61.8, 61.0 86.6, 85.0
Frame p50 162, 164 ms 116, 117 ms
GPU busy per present 150, 152 ms 106 ms
GPU time of the dispatch, per frame ~55 ms ~3.4 ms
staging-in / staging-out per present 0.16 / 0.20 ms, 2.4 MiB 9.8 / 11.8 ms, 236 MiB

The first run of this change reported GPU time only for its first two intervals, so the GPU busy row has the second run only. One more main run gave no numbers: it started while the monitors were asleep (SDL_GetWindowDisplayIndex failed: Couldn't find any displays). None of the runs of this change failed. The previous head (f959abc, Stride() != 0) gave 79.1 and 81.5 presents per 10 s against 59.5 and 56.7 on main in earlier runs; this rule stages the same D2R element.

trace_digest.py rows (steady part, second run of each):

main f71a31193   | 103-113 | 61 | 163.9 | 171.1 | 175.4 | 175.4 | 151.8 | 98 % | Driver.Packet 251.8 |
                 | 113-123 | 61 | 164.7 | 171.3 | 175.8 | 175.8 | 152.1 | 98 % | Driver.Packet 253.5 |
this 27876656b   | 104-114 | 85 | 118.3 | 122.6 | 126.8 | 126.8 | 106.3 | 98 % | Driver.Packet 170.1 |
                 | 114-124 | 86 | 117.1 | 120.9 | 125.1 | 125.1 | 107.3 | 98 % | Driver.Packet 169.1 |
performance_compare.py on the two 10 s trace windows (second run of each)

Performance comparison

Capture Commit Trace Presents Window (ms)
baseline f71a311 aps5-trace-f71a311935f3-50227.json.gz 62 10135.718
candidate 27876656bdf9 aps5-trace-27876656bdf9-48243.json.gz 85 10047.981
  • Compare captures from the same machine, title and scene; summaries do not record the CPU, title or scene.
  • Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval.
  • A missing top phase is unreported, not zero; its change is unavailable.
  • Recorded configuration differs; these measurements do not isolate the code change.
  • baseline: trace events were dropped; the event trace omits some of the work included in phase totals.
  • candidate: trace events were dropped; the event trace omits some of the work included in phase totals.

Configuration differences

Setting Baseline Candidate
window.first_present 814 1085

Frame and GPU times (ms)

Metric Baseline Candidate Change Change (%)
frame_ms.p50 162.924 117.445 -45.479 -27.914
frame_ms.p90 172.193 123.167 -49.026 -28.472
frame_ms.p99 178.736 131.254 -47.482 -26.565
frame_ms.max 178.736 131.254 -47.482 -26.565
mean_frame_ms 163.479 118.212 -45.268 -27.690
gpu_busy_ms_per_present 153.323 105.947 -47.376 -30.899

Top phases (ms per frame)

Metric Baseline Candidate Change Change (%)
Driver.Draw 142.854 96.904 -45.950 -32.166
Driver.Draw.draw_and_resource_release 66.593 n/a n/a n/a
Driver.Draw.shader_compile_and_link 55.959 56.238 +0.279 +0.499
Driver.Packet 250.643 169.643 -81.000 -32.317
Driver.Packet.draw 143.541 97.621 -45.920 -31.991
Driver.Packet.memory_wait 90.114 54.566 -35.548 -39.448
Graphics.Draw 65.706 66.725 +1.019 +1.551
Graphics.Draw.draw_and_resource_release 55.070 56.054 +0.984 +1.787
Shader.DrawStage n/a 48.709 n/a n/a
aps5-summary.jsonl lines of the two trace windows above

main f71a311:

{"commit":"f71a311935f3","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":814,"ms":10135.718},"presents":62,"frame_ms":{"p50":162.924,"p90":172.193,"p99":178.736,"max":178.736},"gpu_busy_ms_per_present":153.323,"worker_busy_pct":{"queue worker 0x23":55.278,"queue worker 0x0":98.207},"top_phases_ms_per_frame":{"Driver.Packet":250.643,"Driver.Packet.draw":143.541,"Driver.Draw":142.854,"Driver.Packet.memory_wait":90.114,"Driver.Draw.draw_and_resource_release":66.593,"Graphics.Draw":65.706,"Driver.Draw.shader_compile_and_link":55.959,"Graphics.Draw.draw_and_resource_release":55.070},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.886,"events_lost":186347,"totals_full":false}},"trace":"aps5-trace-f71a311935f3-50227.json.gz"}

this change (27876656b, same code as this head):

{"commit":"27876656bdf9","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":1085,"ms":10047.981},"presents":85,"frame_ms":{"p50":117.445,"p90":123.167,"p99":131.254,"max":131.254},"gpu_busy_ms_per_present":105.947,"worker_busy_pct":{"queue worker 0x23":46.384,"queue worker 0x0":97.366},"top_phases_ms_per_frame":{"Driver.Packet":169.643,"Driver.Packet.draw":97.621,"Driver.Draw":96.904,"Graphics.Draw":66.725,"Driver.Draw.shader_compile_and_link":56.238,"Graphics.Draw.draw_and_resource_release":56.054,"Driver.Packet.memory_wait":54.566,"Shader.DrawStage":48.709},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.265,"events_lost":356234,"totals_full":false}},"trace":"aps5-trace-27876656bdf9-48243.json.gz"}
  • Astro Bot (PPSA21564) on Linux, RTX 3080 Ti: Sky Garden (underwater_aerial_garden, actual gameplay), from a progress save, default settings, hands off from the level start (the level flies on its own), APS5_TRACE_STAGING=1 to list every distinct staged element. One run each, on main f71a311:

    main f71a311 this change (27876656b) previous head f959abc (Stride() != 0)
    Distinct staged elements 91, largest 10 MiB 91, largest 10 MiB 95, four more of 16, 26, 26 and 49 MiB
    staging-in per present about 3.0 ms, 64 MiB about 3.0 ms, 66 MiB about 4.5 ms, 97 MiB
    Presents per 10 s from the level start 29–31 for about 2 min, then 10–13 29–31 for about 2 min, then 9–10 29–30 for about 20 s, then 12–21

    This change stages the same elements in Astro Bot as main; the previous condition staged four large elements whose records are 16 bytes or less, and the frame rate fell much earlier. The drop later in the level is the texture cache thrashing (Astro Bot (PPSA21564) #357), on every build; the automatic texture budget there follows free VRAM, which the previous head's extra shadows reduced. One run each, so the numbers after the drop are noise-level.

  • Hollow Knight: Silksong (PPSA12544) on Linux, RTX 3080 Ti: a fixed spot in game from a save, standing still, APS5_TRACE_STAGING=1, 12 intervals of 10 s. main bf2c63f (Silksong needs fix(libc): give the scripting heap hooks blocks from libc's own heap #6842) and the same main with this PR's three commits: 1,199 presents per 10 s in every interval on both, and the same 4 staged elements (largest 64 KiB). Nothing there qualifies.

  • D2R on the Radeon 780M (integrated): with the discrete-only rule the change does nothing there. The integrated GPU's FPS numbers I had were taken with a non-default graphics mode, so I've left them out.

  • D2R on Windows: the laptop GPU has 4 GB, and D2R thrashes on it with or without staging, so I have no Windows FPS numbers.

  • I did not compare frames image by image.

Shadow memory bound

  • Each staged element gets its own device-local shadow (one VkDeviceMemory), sized to the element. This PR lets a swept write-only element with records wider than 16 bytes take one, up to 256 MiB, and only on a discrete GPU (context.discreteGpu); other GPUs keep main's caps.
  • A shadow is held by the cached build whose element it serves and is released with that build when it leaves the build cache. So at most (cached builds with such an element) × (element size, ≤ 256 MiB) is held at once; there is no separate total cap.
  • When the device refuses a shadow (VRAM or allocation count exhausted), the element binds in place as on main (stagingBuffer returns nullptr, Region::unstaged) and the [buffers] staging line counts it under "shadows refused". No failure.
  • In Diablo II: Resurrected the only element this adds is ~31.25 MiB (160-byte records).

Device memory, Diablo II: Resurrected

RTX 3080 Ti 12 GB, NVIDIA 615.71.09, Ubuntu 26.10, main a19e7f6 vs this change on it (same code as the current head), Rogue Encampment, default settings, two alternating pairs, 10 s windows at 180 s and 240 s in game, nvidia-smi peak memory.used:

main this PR
peak VRAM in window (total) 8077-8140 MiB 8275-8293 MiB
peak VRAM in window (app.elf) 6471-6509 MiB 6671-6680 MiB
presents / 10 s 55-57 77-85
staged copy-in / copy-back per 10 s ~0.47 GB ~18-21 GB
shadows refused 0 0

So about +150-200 MiB of VRAM for about +40% presents. Whole-run peaks are in the same range on both (main 8486-9035 MiB, this PR 8732-9173 MiB). The copy volume is the full-element copy-in and copy-back on every use; the debt entry describes skipping the copy-in when a sweep is proven to write every byte. No failure lines in any run.

Checklist

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot added the agc libSceAgc* label Oct 18, 2026
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8930 docs/dev/debt/libSceAgcDriver.md L268
PR 10064 docs/dev/debt/libSceAgcDriver.md L268
PR 10800 docs/dev/debt/libSceAgcDriver.md L268

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1191 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 1368 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 1395 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 2434 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 4448 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 4449 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 5021 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 5058 docs/dev/debt/libSceAgcDriver.md
PR 5372 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 5925 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6187 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6188 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6192 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6194 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6197 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6202 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 6209 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6223 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6273 docs/dev/debt/libSceAgcDriver.md
PR 6280 docs/dev/debt/libSceAgcDriver.md
PR 6300 docs/dev/debt/libSceAgcDriver.md
PR 6412 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6481 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6548 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6549 docs/dev/debt/libSceAgcDriver.md
PR 6808 docs/dev/debt/libSceAgcDriver.md
PR 6867 docs/dev/debt/libSceAgcDriver.md
PR 6918 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6959 docs/dev/debt/libSceAgcDriver.md
PR 6971 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 6977 docs/dev/debt/libSceAgcDriver.md
PR 7048 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 7201 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 7256 docs/dev/debt/libSceAgcDriver.md
PR 7449 docs/dev/debt/libSceAgcDriver.md
PR 7457 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 7505 docs/dev/debt/libSceAgcDriver.md
PR 7514 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 7604 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7689 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 7844 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7910 docs/dev/debt/libSceAgcDriver.md
PR 7978 docs/dev/debt/libSceAgcDriver.md
PR 8014 docs/dev/debt/libSceAgcDriver.md
PR 8223 docs/dev/debt/libSceAgcDriver.md
PR 8319 docs/dev/debt/libSceAgcDriver.md
PR 8484 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 8520 docs/dev/debt/libSceAgcDriver.md
PR 8592 docs/dev/debt/libSceAgcDriver.md
PR 8874 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 8896 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 8998 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 9020 docs/dev/debt/libSceAgcDriver.md
PR 9335 docs/dev/debt/libSceAgcDriver.md
PR 9356 docs/dev/debt/libSceAgcDriver.md
PR 9387 docs/dev/debt/libSceAgcDriver.md
PR 9393 docs/dev/debt/libSceAgcDriver.md
PR 9400 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 9431 docs/dev/debt/libSceAgcDriver.md
PR 9432 docs/dev/debt/libSceAgcDriver.md
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9533 docs/dev/debt/libSceAgcDriver.md
PR 9592 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9672 docs/dev/debt/libSceAgcDriver.md
PR 9673 docs/dev/debt/libSceAgcDriver.md
PR 9674 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9675 docs/dev/debt/libSceAgcDriver.md
PR 9676 docs/dev/debt/libSceAgcDriver.md
PR 9700 docs/dev/debt/libSceAgcDriver.md
PR 9717 docs/dev/debt/libSceAgcDriver.md
PR 9742 docs/dev/debt/libSceAgcDriver.md
PR 9748 docs/dev/debt/libSceAgcDriver.md
PR 9842 docs/dev/debt/libSceAgcDriver.md
PR 9891 docs/dev/debt/libSceAgcDriver.md
PR 9892 docs/dev/debt/libSceAgcDriver.md
PR 9893 docs/dev/debt/libSceAgcDriver.md
PR 9895 docs/dev/debt/libSceAgcDriver.md
PR 9897 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 9898 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 9904 docs/dev/debt/libSceAgcDriver.md
PR 9905 docs/dev/debt/libSceAgcDriver.md
PR 9906 docs/dev/debt/libSceAgcDriver.md
PR 9950 docs/dev/debt/libSceAgcDriver.md
PR 10000 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 10026 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 10049 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 10159 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
docs/dev/debt/libSceAgcDriver.md
PR 10368 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 10435 docs/dev/debt/libSceAgcDriver.md
PR 10442 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10443 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
PR 10469 docs/dev/debt/libSceAgcDriver.md
PR 10489 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
PR 10492 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md
PR 10497 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
docs/dev/debt/libSceAgcDriver.md

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line unblocks title Names a title ID on the Unblocks line labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@mrfelek mrfelek changed the title perf(agc): stage written-only buffer elements a dispatch sweeps perf(agc): stage written-only buffer elements a dispatch sweeps on discrete GPUs Oct 18, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@blawesom

Copy link
Copy Markdown
Contributor

Rebase handoff, with a feature-vs-feature verdict to review — rebased onto current main (all three commits, authorship unchanged). The conflicts are the same shape on two commits: main has since landed its own read/extent-aware addGuestBuffer (the read flag, bufferExtent, the recordBytes/swept computation and the 4-argument test form) — which is the same feature this PR implements, from a different angle. I kept main's forms throughout; your discrete-GPU gating (commit 3) and any behavior main's version lacks are what remains distinct.

So the practical question for you and the maintainers: main's landed version covers the read-and-written and raw-element staging paths this PR adds — worth checking whether your discrete-GPU-only gate (and anything else main's landed form misses) justifies a delta on top, or whether this closes as superseded.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the four known main regressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7618
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 7d8e4c7b1.

@mrfelek
mrfelek force-pushed the perf/stage-swept-write-only branch from 272b4c7 to f959abc Compare October 19, 2026 10:04
@mrfelek

mrfelek commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@blawesom thanks for the rebase and for running the suite on the RX 9070 XT. I didn't take it as is, because it dropped the part this PR is about. It isn't superseded by main.

main's addGuestBuffer still requires read:

const bool swept = read && dispatchThreads != 0 && recordBytes != 0 && dispatchThreads * recordBytes * 2u >= byteSize;

Your rebase kept that line, so a write-only element is still never swept. The 256 MiB cap from commit 3 then has nothing to apply to. D2R's 212 MB write-only element stays bound in place, and the dispatch still takes about 56–60 ms per frame. #6486 (now in main) covers raw elements that are read and written. This PR covers strided elements that are only written, on discrete GPUs. The two don't overlap.

I've rebased it myself onto main f71a311 (head f959abc). It keeps main's recordBytes form and changes the condition to (read || (context.discreteGpu && descriptor.Stride() != 0)), so write-only raw elements still keep the 2 MiB window. readWrittenStagingTests uses main's four-argument form plus an element size.

At the game's default settings on the RTX 3080 Ti, D2R goes from 59.5 / 56.7 to 79.1 / 81.5 presents per 10 s. The description has the full numbers, the summary lines and the traces.

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot removed the perf numbers missing perf pull request without a before and after summary line label Oct 19, 2026
@mrfelek
mrfelek marked this pull request as ready for review October 19, 2026 10:04
@mrfelek

mrfelek commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

The two traces of the 10 s windows in the description, for the summary lines and the performance_compare.py output there (open in ui.perfetto.dev):

Both are also in https://gist.github.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2.

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026

@CnRJay CnRJay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good PR: the cause is isolated (one strided 212 MB write-only element, 56 to 60 ms of GPU time in place against 3.5 ms staged), the result is measured twice on D2R (presents per 10 s 59.5/56.7 to 79.1/81.5, GPU busy 155/162 to 112/109 ms), and the fallbacks (integrated GPUs, raw elements, partial sweeps, indirect dispatches) are tested. The cost side needs more than the description gives it.

  • The staged path costs 11.3 ms in and 13.9 ms out per present, 267 MiB of copies, against 0.2 ms in place. On this card that is a clear win; the PR's own trade-off paragraph says it is a loss for a kernel that writes a large element once and contiguously. The condition descriptor.Stride() != 0 doesn't distinguish those two. The narrower condition the description proposes (the record stride larger than the store width per thread, so the in-place stores are scattered) is the real discriminator; D2R has 160-byte records with 16-byte stores. Please use it now rather than after a regression report, and add the contiguous case (stride equal to the store width) to the test as "bound in place".
  • The copies run over PCIe on a discrete card. 267 MiB per present at about 8 presents per second is about 2 GiB/s in total, trivial on x16 but a visible share of an x4 or x8 link (laptop eGPU, bifurcated slot). The test cards are all x16-class; say what the cap DiscreteSweptShadowMax (256 MiB, up from 16) does on a card that is already near its VRAM budget, since the shadow buffer is allocated for every such dispatch. Is it allocated from the pool and released each frame, or kept?
  • The description keeps the copy-in for write-only elements, because the copy-back covers the whole element. For D2R the records are 160 bytes and stores are 16 bytes, so gaps hold old data and the copy-in is needed. If a later check can prove the sweep writes every byte, the copy-in (11.3 ms here) could go; note that in debt rather than leaving it implicit.
  • A second title that is not D2R and does stage would help (Astro Bot has nothing it stages). If nobody can name the memcpy title, the stride-versus-store-width condition is the safe choice.

@mrfelek

mrfelek commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@CnRJay thanks for the review. All four points are in the new head (ab4c1fe), and the description is updated.

1. The condition. A write-only element is now staged only when its V# stride is wider than 16 bytes, the widest buffer store (descriptor.Stride() > WidestBufferStoreBytes). With records that wide, the lanes of each store land a record apart, so in place the stores are scattered. With 16 bytes or less, one store can fill a whole record and the element stays in place. The recompiler doesn't record store widths per instruction, so this uses the widest possible store instead of the real one. That errs on the side of keeping an element in place. New test case: a written-only element of 16-byte records, swept, "bound in place" on every GPU. The swept-and-staged case now uses 32-byte records, and the partial-sweep case too, so it doesn't pass for the wrong reason.

D2R is unchanged by the narrower rule, since its records are 160 bytes:

D2R, default settings, alternating runs main f71a311 this head (run on 27876656b, same code)
Presents per 10 s 61.8, 61.0 86.6, 85.0
GPU busy per present 150, 152 ms 106 ms
staging-in / staging-out per present 0.16 / 0.20 ms 9.8 / 11.8 ms (236 MiB each way)

2. Memory and PCIe.

  • From the code: each shadow is its own device-local buffer, kept by the cached resource build that uses it. So it isn't pooled and released each frame; it stays allocated while that build is cached, and each reuse records its copies again. If the allocation is refused, for example near the VRAM budget, the element is bound in place as before: [buffers] staging shadow of N bytes refused and the stagingRefused counter, no abort. I haven't run D2R on a card that is close to its VRAM limit.
  • Bus traffic: about 472 MiB per present (236 MiB each way), about 4 GiB/s at 8.5 presents per second. I have no x4 or x8 machine to measure it on. Both points are in the description's new Cost paragraph, with APS5_NO_WRITTEN_SHADOW=1 as the way back to in place.

3. The copy-in. New debt entry "Swept write-only staging" in docs/dev/debt/libSceAgcDriver.md. It's needed because the stores can leave part of each record unwritten (D2R's thread has 8 buffer_store_dwordx4, at most 128 of its 160 bytes). A sweep proven to cover every byte could drop it, about 10 ms per frame here.

4. A second title. I couldn't name one that stages, so I took the narrower condition, as you suggested.

Tests at this head: the staging group passes on the RTX 3080 Ti (Linux), the Radeon 780M (integrated, Linux) and the RTX A2000 (Windows). Under the Khronos validation layer on the 3080 Ti, it passes on both main and this head with no VUID- message.

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed perf numbers missing perf pull request without a before and after summary line docs labels Oct 19, 2026
@mrfelek
mrfelek force-pushed the perf/stage-swept-write-only branch from ab4c1fe to 9d8a96b Compare October 19, 2026 21:55
@mrfelek

mrfelek commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@CnRJay a follow-up on point 4: Astro Bot turned out to be a second title the rule matters for, in gameplay this time instead of the galaxy map.

Setup: Linux, RTX 3080 Ti, default settings. Sky Garden (underwater_aerial_garden) from a progress save, hands off from the level start (the level flies on its own). APS5_TRACE_STAGING=1 lists every distinct staged element. One run each, on main f71a311.

Astro Bot, Sky Garden main this change previous head (Stride() != 0)
Distinct staged elements 91, largest 10 MiB 91, largest 10 MiB 95, four more of 16, 26, 26 and 49 MiB
staging-in per present ~3.0 ms, 64 MiB ~3.0 ms, 66 MiB ~4.5 ms, 97 MiB
Presents per 10 s from the level start 29–31 for ~2 min, then 10–13 29–31 for ~2 min, then 9–10 29–30 for ~20 s, then 12–21
  • The previous condition staged four large elements whose records are 16 bytes or less, and the level slowed down much earlier. The narrower condition stages exactly what main stages.
  • The drop later in the level is the texture cache thrashing (Astro Bot (PPSA21564) #357) and happens on every build. The automatic texture budget follows free VRAM, which the previous head's extra shadows reduced.
  • Silksong (a fixed spot, standing still): 1,199 presents per 10 s on main bf2c63f and with this PR, the same 4 small staged elements. Nothing there qualifies.

I've also rebased onto main eaa5c92 (a debt-file conflict only, both sides appended entries) and replaced the galaxy-map table in the description with these results.

AI-assisted; the runs are from my machine(s).

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed unblocks title Names a title ID on the Unblocks line labels Oct 19, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked against current main (513ca7e): the PR applies cleanly, context is in scope in addGuestBuffer, and the head's patch is the one already reviewed, so the code is unchanged. The measurements on D2R and Astro Bot look sound.

One point before merge: the swept cap goes from 16 MiB to 256 MiB per written element on discrete GPUs, and the shadow stays allocated while its cached resource build is kept. The description says the memory is "from the code, not measured". Please add:

  • the bound on how many such shadows the cache can hold at once (cached builds × 256 MiB, or whatever limit the cache applies), and what releases a shadow when its build leaves the cache;
  • device-memory use before and after on D2R (peak allocated VRAM over the same 10 s window, from the same machine), so the cost of the larger cap is a number and not an estimate.

Once that is in the description, the rest of the review stands.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
On a discrete GPU a write-only V# element whose stride is wider than the
widest buffer store (16 bytes) and that a direct dispatch sweeps is staged
in a device-local shadow of up to 256 MiB, as read and written swept
elements are. Its stores no longer cross the bus into the host import.

AI-assisted. Tested by me on my own machine.
@mrfelek
mrfelek force-pushed the perf/stage-swept-write-only branch from c12e8e8 to a293f79 Compare October 24, 2026 10:22
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@mrfelek

mrfelek commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@adriadam10 both points are in the description now, under "Shadow memory bound" and "Device memory, Diablo II: Resurrected":

  • Bound: one device-local shadow per staged element, sized to the element (at most 256 MiB, discrete GPUs only). It is held by the cached build that uses it and released with that build when it leaves the build cache. There is no separate total cap; a refused allocation binds the element in place as on main and is counted as "shadows refused". In D2R the only element this adds is ~31.25 MiB.
  • D2R on the RTX 3080 Ti, two alternating pairs, same 10 s windows: peak VRAM 8077-8140 MiB on main vs 8275-8293 MiB with this PR (+150-200 MiB), presents 55-57 vs 77-85 per 10 s, 0 shadows refused in all runs.

I also rebased onto main 2dc8e45. #8336 had landed in the same lines, so the change now sits on its writtenShadowMax(context.discreteGpu); the logic is unchanged, and the staging tests are adjusted to main's 12 MiB element.

AI-assisted. Tested by me on my own machine.

@adriadam10
adriadam10 merged commit a8b199d into boykopovar:main Oct 25, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Rechecked the new head: the logic is the one already reviewed, now on top of #8336's writtenShadowMax(context.discreteGpu), and the staging test expectations still depend on the change (32-byte, 16-byte, partial-sweep, raw and 160-byte cases). The shadow bound and the D2R VRAM numbers (+150-200 MiB peak, 0 refused) are in the description; merged.

mrfelek added a commit to mrfelek/AnyPS5 that referenced this pull request Oct 28, 2026
…revert A/B

AI-assisted. Tested by me on my own machine.
mrfelek added a commit to mrfelek/AnyPS5 that referenced this pull request Oct 28, 2026
…revert A/B

AI-assisted. Tested by me on my own machine.
mrfelek added a commit to mrfelek/AnyPS5 that referenced this pull request Oct 28, 2026
…revert A/B

AI-assisted. Tested by me on my own machine.
mrfelek added a commit to mrfelek/AnyPS5 that referenced this pull request Oct 28, 2026
…scussion

AI-assisted. Tested by me on my own machine.
mrfelek added a commit to mrfelek/AnyPS5 that referenced this pull request Oct 28, 2026
…scussion

AI-assisted. Tested by me on my own machine.
adriadam10 pushed a commit to mrfelek/AnyPS5 that referenced this pull request Oct 30, 2026
Staging a swept write-only element (boykopovar#7618) costs a copy-in and a
copy-back per use. In PPSA03912's town that saves GPU time, but in its
main menu the 202 MiB element is faster in place (73 vs 91 ms of GPU
time per frame), so the menu lost about a fifth of its frames.

Decide per element by measurement. The busiest element is probed in
its current mode, the other mode and its current mode again; the other
mode is kept when its frames are faster by more than twice the
standard error. A repeated answer doubles the wait for the next probe
from 20 s up to 160 s, and a jump of the frame GPU time by more than a
quarter (a scene change) probes again at once. The frame GPU time comes
from the whole-batch timestamps, which the first probe turns on.
Resource builds made under the other decision are not reused.
APS5_NO_ADAPTIVE_STAGING=1 stages every eligible element, as before.

The policy is in AdaptiveStaging.hpp, fed by the caller with frame
counts and time; agc_adaptive_staging covers keeping the faster mode,
frame noise, the back-off cap, a scene change and the share threshold.

AI-assisted. Tested by me on my own machine.
adriadam10 pushed a commit that referenced this pull request Nov 11, 2026
Main's #7618 adds its swept write-only staging entry at the end of the
debt file, where this branch adds its partially committed 3D textures
entry; the merge keeps both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants