perf(agc): stage written-only buffer elements a dispatch sweeps on discrete GPUs #7618
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7618"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-18T17:30:58Z.
What
2688b7646stages 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.mainapplies that up to 16 MiB, and only when the shader also reads the element.6d9222082(#6486) extended it to raw elements through their access extent, still only for read and written elements. The message of2688b7646explains 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 fromVK_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.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:
main's rule unchanged, apart from the larger cap on discrete GPUs;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
2688b7646binds 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:
[buffers] staging shadow of N bytes refusedis printed andstagingRefusedcounts it; nothing aborts.APS5_NO_WRITTEN_SHADOW=1binds 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_recorderreadWrittenStagingTests:discreteGpufrom the selected device's type.I ran the group on its own, through a local
--tmp-staging-onlyswitch 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 ab4c1febe, and on the RTX 3080 Ti again after the rebases ontomaineaa5c9238andd14a186f4(this head). On the RTX 3080 Ti I also ran it onmainf71a31193, on ab4c1febe and on this head withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(layer 1.4.357, loaded): all three pass with noVUID-message, so the change adds none. The fullagc_driver_recorder_testsbinary stops earlier, at(9) a scan under a pending write was kept, the same way as onmain, so it's unrelated.D2R (PPSA03912) on Linux, RTX 3080 Ti: in game with a saved character, the game's default settings, no input.
mainf71a31193and 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:mainf71a31193staging-in/staging-outper presentThe 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
mainrun 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 (f959abc09,Stride() != 0) gave 79.1 and 81.5 presents per 10 s against 59.5 and 56.7 onmainin earlier runs; this rule stages the same D2R element.trace_digest.pyrows (steady part, second run of each):performance_compare.py on the two 10 s trace windows (second run of each)
Performance comparison
f71a311935Configuration differences
Frame and GPU times (ms)
Top phases (ms per frame)
aps5-summary.jsonl lines of the two trace windows above
main
f71a31193:this change (27876656b, same code as this head):
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=1to list every distinct staged element. One run each, onmainf71a31193:mainf71a31193Stride() != 0)staging-inper presentThis 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 (#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.mainbf2c63f85(Silksong needs #6842) and the samemainwith 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
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.stagingBufferreturns nullptr,Region::unstaged) and the[buffers] stagingline counts it under "shadows refused". No failure.Device memory, Diablo II: Resurrected
RTX 3080 Ti 12 GB, NVIDIA 615.71.09, Ubuntu 26.10, main
a19e7f6fdvs 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-smipeakmemory.used: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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)maind14a186f4(a conflict in the debt file only: both sides appended entries); the measurements were taken onmainf71a31193with the same code. Thatmainhas #6486 (read and written raw elements); this PR keeps its form of the sweep test.pr_overlap.pylisted #6636, #6947 and #7340 for neighbouring lines inShaderResources.cpp.AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T22:54:34Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-19T07:35:49Z.
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:mainhas since landed its own read/extent-awareaddGuestBuffer(thereadflag,bufferExtent, therecordBytes/swept computation and the 4-argument test form) — which is the same feature this PR implements, from a different angle. I keptmain's forms throughout; your discrete-GPU gating (commit 3) and any behaviormain'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 elsemain'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 knownmainregressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR.To pick it up:
Head is
7d8e4c7b1.Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T10:04:49Z.
@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'saddGuestBufferstill requiresread: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
mainf71a31193(head f959abc09). It keepsmain'srecordBytesform and changes the condition to(read || (context.discreteGpu && descriptor.Stride() != 0)), so write-only raw elements still keep the 2 MiB window.readWrittenStagingTestsusesmain'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).
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T10:11:53Z.
The two traces of the 10 s windows in the description, for the summary lines and the
performance_compare.pyoutput there (open in ui.perfetto.dev):mainf71a31193, run 3: aps5-trace-f71a311935f3-3059676.json.gzBoth are also in https://gist.github.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2.
AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-19T17:49:12Z.
Review: COMMENTED
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.
descriptor.Stride() != 0doesn'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".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?Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T20:55:36Z.
@CnRJay thanks for the review. All four points are in the new head (ab4c1febe), 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:
mainf71a31193staging-in/staging-outper present2. Memory and PCIe.
[buffers] staging shadow of N bytes refusedand thestagingRefusedcounter, no abort. I haven't run D2R on a card that is close to its VRAM limit.APS5_NO_WRITTEN_SHADOW=1as 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 8buffer_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
mainand this head with noVUID-message.AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T21:55:34Z.
@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=1lists every distinct staged element. One run each, onmainf71a31193.mainStride() != 0)staging-inper presentmainstages.mainbf2c63f85and with this PR, the same 4 small staged elements. Nothing there qualifies.I've also rebased onto
maineaa5c9238(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).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T10:29:48Z.
Review: CHANGES_REQUESTED
Rechecked against current
main(513ca7e55): the PR applies cleanly,contextis in scope inaddGuestBuffer, 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:
Once that is in the description, the rest of the review stands.