perf(agc): stage written buffer elements up to 8 MiB on discrete GPUs #8336
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8336"
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 saarthakpant on 2026-10-20T10:20:31Z.
What
A written buffer element larger than the 2 MiB written-shadow window is bound in place in its host import, so the shader's stores go to system memory over the bus. On NVIDIA's Windows driver those stores are slow enough to dominate a frame: Ghostrunner 2's GPU skin cache writes its outputs into two pool-sized elements of 2.5 and 3.3 MiB with 16-bit tangent stores, and one 67k-thread dispatch of a 380-instruction kernel took 4.2 ms, 55 ms per frame over the frame's 13 dispatches. In isolation the same dispatch takes 0.30 ms once its outputs are device-local, and 43.9 ms when they are written in place (an execution-test benchmark built from the kernel's dumped request; numbers below).
The swept rule does not catch these elements: the dispatch writes a third of the pool, not half, and the resource template that decides the binding is keyed by the shader's content (
DispatchContentKey), not by the thread count, so a rule on the dispatch's extent would bind by whichever dispatch built the template first. The change is the window itself: on a discrete GPU (Context::discreteGpu, fromVK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) a written element that is not swept is staged device-local up to 8 MiB instead of 2 MiB (DiscreteWrittenShadowMax). Integrated GPUs keep 2 MiB: their device-local memory is the same RAM and staging there only pays the two copies.APS5_WRITTEN_SHADOW_MAX_KIBis used as given on both device types when it is set (its default is 8192 KiB on a discrete GPU and 2048 otherwise), soAPS5_WRITTEN_SHADOW_MAX_KIB=2048restores the previous window for an A/B run, andAPS5_NO_WRITTEN_SHADOW=1turns staging off as before.docs/dev/ENVIRONMENT.mdand the comment aboveSweptShadowMaxdescribe the new defaults.The cost is the existing staging cost applied to 2 to 8 MiB elements: a copy-in and a copy-back of the element per dispatch. On the title below that is about 1.8 ms more staging per frame against 55 ms of kernel time removed.
Tested
Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 UCRT, Release, NVIDIA GeForce RTX 2060 SUPER (617.42).
agc_driver_recorder(boundInPlace): a written-only 4 MiB element is staged on a discrete GPU and bound in place on an integrated one; a written-only 12 MiB element is bound in place on both. The test device now setsContext::discreteGpufrom the physical device type (the harness had left it false, so the test exercised the integrated branch on every GPU), and the swept-rule cases use a 12 MiB element, above the discrete window and below the swept cap. The override was checked by running the test with the variable set: it passes unset and with8192; with2048it fails at "a written-only 4 MiB element was bound in place on a discrete GPU", with16384at "a written-only 12 MiB element was staged past the discrete written window" (a set value cannot be covered by aRequire, since it is read once into a static).agc_read_written_stagingwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1(ctest -V) onmain3fd1cc393and on this branch (35ac3402c, rebased ontomain35ac3402c): passes on both, the layer was found, noVUID-lines in either log. The test's guest block grows from 8 to 16 MiB so the 12 MiB cases fit inside the import.agc_driver_skin_cache_bench, local, not part of this PR), 1047 groups of 64, median of 10: outputs staged 0.30 ms; outputs in place with the game's descriptor sizes 43.9 ms;APS5_WRITTEN_SHADOW_MAX_KIB=4096on the same 0.29 ms. In the game with that override the kernel went from 4.18 to 0.09 ms per dispatch.APS5_TRACE_SECONDS=120:60, no other switches. The base ismain4b462ef60plus #7687 (this title stops before gameplay onmainalone, #7017) plus #5372 (without it 35 GB per 10 s of CPU copies of another buffer bound the frame); this change on top.base:
this change:
Frame p50 308.2 → 171.1 ms, presents 196 → 261 in the window, GPU busy per present 87.0 → 75.2 ms. Scripted runs die and respawn at different moments (the p99 of both windows is a respawn reload), so the per-program GPU times of the profiled runs are the steadier number: the kernel at 0x200fa10000 fell from 4.2 to 0.1 ms per dispatch in every window and left the top GPU programs; staging-in and staging-out rose from 1.97 and 1.73 to 3.72 and 3.55 ms per present. Both traces and the
tools/trace_digest.pyoutput of each are attached.tools/pr_overlap.py: no open pull request changes the same lines; 65 pull requests that conflict withmainshareGuestBufferMemory.cpp,VulkanDevice.cpp,Context.hpportests/Recorder.cppand could not be compared. #7618 raises the swept cap on discrete GPUs and extends the swept rule to written-only elements; it touches the samestagingEligibleline and adds the samediscreteGpufield, so whichever lands second rebases onto the other, and the two conditions compose (an element either sweeps or falls under the written window).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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T11:09:48Z.
AMD data point: RTX 3080 (NVIDIA 617.42) and AMD Radeon RDNA 2 iGPU (
1002:164e, 32.0.21045.5002), Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, head2bb7a2e6d:agc_read_written_stagingpasses 3 of 3 on both GPUs. (The Radeon is an integrated GPU, so it does not exercise the discrete-GPU staging size this PR changes.)AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T12:35:14Z.
Thanks for the measurements. One change before this goes further:
The description says
APS5_WRITTEN_SHADOW_MAX_KIB"still overrides both", but on a discrete GPU the new bound isstd::max(writtenShadowMax(), DiscreteWrittenShadowMax)(GuestBufferMemory.cpp,stagingEligible). A value below 8192 is therefore ignored there.APS5_WRITTEN_SHADOW_MAX_KIB=2048no longer restores the previous window, and the only way left to go back isAPS5_NO_WRITTEN_SHADOW=1, which also turns off the 16 KiB to 2 MiB staging. That knob is what an A/B run against this change needs, so please make an explicit value win on both device types, for example by making 8192 the default ofwrittenShadowMax()on a discrete GPU and 2048 otherwise, with the environment value used as given when it is set. AboundInPlacecase inreadWrittenStagingTeststhat sets the variable can't work because the value is read once into a static, so a sentence in the description saying how you checked it is enough.Please also update the two places that still describe the old window:
docs/dev/ENVIRONMENT.md,APS5_WRITTEN_SHADOW_MAX_KIB: the default is now 8192 on a discrete GPU and 2048 otherwise.SweptShadowMaxgives "16 / 2048" as the defaults and names 4-8 MiB memcpy kernels over video frames as the reason for the 2 MiB cap. Those elements are now staged on discrete GPUs, so the comment should say that the trade-off was revisited for discrete GPUs, or what still keeps such a kernel out.Imported from boykopovar/AnyPS5. Originally posted by saarthakpant on 2026-10-21T08:17:31Z.
Done in 569ce7c2d, rebased onto
mainf894ba69d:writtenShadowMax(bool discreteGpu): the environment value is used as given on both device types; without it the window is 8192 KiB on a discrete GPU and 2048 otherwise.stagingEligibletakes that bound directly for a written element that is not swept (the swept case keepsmax(bound, 16 MiB)as before), soAPS5_WRITTEN_SHADOW_MAX_KIB=2048restores the previous window on a discrete GPU too.docs/dev/ENVIRONMENT.mdand the comment aboveSweptShadowMaxdescribe the new defaults and why the discrete window takes the elements the 2 MiB cap kept out; the trade-off and what it was measured on are recorded under Functional indocs/dev/debt/libSceAgcDriver.md, which is also what the conventions check requires for a changed comment.Context::discreteGpufrom the physical device type (it was always false in the test harness, soreadWrittenStagingTestshad been exercising the integrated branch on a discrete GPU). With the discrete branch live, the swept-rule cases use a 12 MiB element, above the discrete window and below the swept cap, so they test the sweep and not the window.How the override was checked (RTX 2060 SUPER, 617.42):
agc_read_written_stagingpasses with the variable unset and withAPS5_WRITTEN_SHADOW_MAX_KIB=8192; with2048it fails at "a written-only 4 MiB element was bound in place on a discrete GPU", with16384at "a written-only 12 MiB element was staged past the discrete written window", so a set value wins on a discrete GPU in both directions. UnderVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1the test passes with noVUID-line and the layer found.AI-assisted: yes.