perf(agc): allow shader preparation on first use #5872
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5872"
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 Sam-mon7 on 2026-10-14T14:22:21Z.
What
Adds opt-in
APS5_LAZY_SHADER_PREPARE=1: shaders are still validated and decoded at registration, but prepared on their first draw or dispatch instead of all at boot. The default is unchanged.Why: Demon's Souls (PPSA01341) registers thousands of shaders during boot. Preparing all of them up front pushes private memory to 17 GB and runs a 32 GB Windows machine out of RAM before the first real frame. #5806 reports the same on
mainunder an 18 GiB cap.main+ #5870main+ #5870 + this PR, opt-in on(#5870 fixes an earlier abort on
main; without it the game stops before this point. This PR doesn't depend on it.)Tested
Windows 11 (26200), WinLibs GCC 15.2.0, Release, RTX 4070.
agc_driver_prepared_shaders*: 12 passed, including the newlazyandlazy_syncmodes. These cover dispatch, metadata and failure reporting on first use.^(agc_|windows_backtrace_): 534 passed, 0 failed, 3 skipped (two udmabuf tests are Linux only; the RTX 4070 has no stencil export).PR overlap: #5115 edits a nearby line of
tests/execution/PreparedShaders.cpp. Either can merge first; the rebase is trivial.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)Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:16:55Z.
Review: COMMENTED
The memory numbers are a real problem for Demon's Souls. Before an opt-in mode, though, it is worth knowing what the extra 5 GB is.
In lazy mode, the first draw or dispatch of each shader goes through
SourceHandleFor→PrepareShaderWithDiagnosticson the thread recording it, while it holdssnapshot.prepared->mutex. With a cold disk cache that is a full recompile (SPIR-V generation and optimization) on the queue worker. With a warm one it is a disk-cache load and parse. Both stall the queue in the frame where the shader first appears, which is the stutter the background registration pool was added to avoid (#2583). The PR measures memory only, so the cost on the other side isn't visible. The summary lines of one scene with the opt-in off and on, warm cache, withframe_msp99/max, would show it, for Demon's Souls or a title that doesn't run out of memory.On the memory itself:
RegisteredPreparations, #5910 removes the decoded program each one holds, which was 638 MiB in Borderlands 4.A heap profile at the 12 GB / 17 GB points would show which of these it is. As it stands, Demon's Souls still runs out of memory by default, and users would need to know about the variable.
Imported from boykopovar/AnyPS5. Originally posted by AnthonyClemens on 2026-10-14T23:53:17Z.
Tried this on Resident Evil 2 (PPSA04288), Windows 11, RX 9070 XT, 32 GB. Local integration tree on main
93d69edcc; the only difference between the runs is APS5_LAZY_SHADER_PREPARE. Same build, warm shader cache (~8,000 shaders), 300 s boot through the first-boot setup screens, private bytes sampled every 10 s:So about 1 GB less here, with the same frame rate and the setup screens reached a bit sooner (161 s vs 170 s). Smaller than on Demon's Souls, because most of RE2's footprint at that point is its own direct memory rather than prepared shaders, but it's a clear win with no downside I could see. I didn't reach gameplay in these runs.
Imported from boykopovar/AnyPS5. Originally posted by Sam-mon7 on 2026-10-15T00:57:23Z.
Thanks, this was the right question. I measured it, and the answer points at your first option.
Short version: with the opt-in off, registration loads the prepared artifact of every registered shader at boot, about 6,300 shaders, while the game has drawn only ~170 by the intro. The extra memory is those undrawn artifacts, and it doesn't show up as private heap.
Demon's Souls (PPSA01341) boot, Windows 11, RTX 4070, 32 GB RAM,
main2559e9623+ #5870 + #5910:APS5_SYNC_SHADER_PREPARE=1What this rules in and out:
So a cap, or evicting prepared entries by last use, should fix this for everyone with no switch, as you suggested. I'll look at that as a follow-up and close this PR once it lands, unless you'd rather it closed now.
On the stutter cost: I can't give
frame_mswith the opt-in off for Demon's Souls, because it doesn't get past boot on this machine. You're right that lazy preparation moves the work onto the queue worker. I'd rather fix the retention than trade it for first-use hitches.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:10:49Z.
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.
main)core/libs/prx/libSceAgcDriver/CMakeLists.txtL1789core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cppL6-7, L29, L675core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppL77core/libs/prx/libSceAgcDriver/CMakeLists.txtL1788-1789Some 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.
core/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T13:55:21Z.
Review: CHANGES_REQUESTED
Checked the diff, built it merged onto main and ran the AgcDriver ctest scope (only
agc_driver_tessellationfails, it segfaults on main too).tests/execution/PreparedShaders.cpp:623(LazyRegistration, modelazy): it passes with theShaderRegistry.cppand docs change reverted, so it doesn't test the opt-in. It only checks dispatch output and error reporting, which eager preparation also gives. Please assert something only lazy mode does, e.g. that registration leaves the snapshot deferred (no prepared artifact / no disk-cache load until the first dispatch), and show it fail without the change.lazy_syncdoes fail without it.frame_msp99/max with the opt-in off and on, warm cache, on a scene that doesn't run out of memory.The memory numbers (Demon's Souls 17.1 → 12.2 GB, RE2 15.2 → 14.3 GB by @AnthonyClemens) are clear. Happy to re-review once the test is fixed.
Imported from boykopovar/AnyPS5. Originally posted by Sam-mon7 on 2026-10-15T18:26:08Z.
Thanks for the review. All three points are addressed below.
1. The
lazytest now checks the opt-in (f515ed29f)Driver::RegisteredShaders()returns the registry under the driver's mutex, like the existing registry copies inShaderRegistry.cpp.ShaderRegistry.cppchange reverted, bothagc_driver_prepared_shaders_lazyandlazy_syncfail with "lazy registration prepared the shader before its first dispatch". With it, all 11agc_driver_prepared_shaders*cases pass.2. First-use stall: Demon's Souls' intro, warm cache, same build (
main69e7138ac+ #5870 + this PR),APS5_HOST_IMPORT_MIB=8192andAPS5_TRACE_SUMMARY_EVERY=10on both sides, no other switches, RTX 4070, Windows 11.frame_msp50 (median of intervals)frame_msmax > 1 sOn this scene the opt-in added no visible hitches. The caveat: with the opt-in off this machine pages (27.8 GB private in a 10.2 GB working set), so some of the off-side spikes are paging. I don't have a scene where the opt-in off fits in RAM here, so this doesn't fully isolate first-use preparation cost on a machine with headroom.
3. Stopgap or close: I'd like this merged as an opt-in stopgap. It's off by default, and for Demon's Souls and RE2 it's the difference between booting and running out of memory. Meanwhile I'll work on the general fix you described (bounding or evicting prepared entries by last use) as a separate PR, and close this one if that change covers the same case without a switch.
Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-16T06:45:13Z.
Demon's Souls (PPSA01342), measured into the level rather than boot only. Windows Server 2025 (26100), RTX 3060 (driver 591.86), Ryzen 9 5900X, 32 GB RAM, 64 GiB pagefile (commit limit 95.9 GiB). Each run is 40 min: boot, the debug walk-through menu, Tutorial (Fort Entrance) picked by scripted pad input, the level load, then idle in Outpost Passage. Memory is sampled every 10 s, in GiB, one run per row.
The build is not
main: it is upstream0214069e3plus a private stack of about 90 commits. The stack carries this PR's runtime change (cherry-picked; itsShaderRegistry.cpphunk is identical to96070188b, andc8bedff05only adds the test accessor), #5910, and the pre-merge opt-in revision of #4806 (APS5_FILE_BACKED_SPIRV=1: SPIR-V modules of 64 KiB or more go to temporary file-backed views, and the rest stays on the heap). It also has a byte cap on those views,APS5_FILE_BACKED_SPIRV_MAX_BYTES, from a local version of the bounded-storage correction we offered on #4806, which was not merged and is not in any PR; above the cap, SPIR-V stays on the heap. A and C are one binary; B, D and E are that binary plus #5484. B to E setAPS5_JOB_AFFINITY_MASK/APS5_WORKER_AFFINITY_MASK; A does not.APS5_NO_LIBRARY_PREBUILD=1APS5_HEAP_CACHE_MIB=512¹ Shader disk-cache hits plus misses over the run; "cold" is the misses. ² At the final Cross on the walk-through menu. ³ Mean from the first frame dump showing the level HUD to the end of the run. ⁴ System commit / system commit minus other processes' private bytes (other processes held 15–17 GiB at each run's commit peak). ⁵ Set by short spikes at 1,320–1,660 s, about 15 min after B's last cache miss was compiled (all 4,642 were done by about 450 s). We haven't identified the cause, and A has no such spikes. Five single 10 s samples sit 4–9 GiB above their neighbours, and one of them sets the commit peak. Peak private is the process's peak-commit counter, which also caught larger transients between samples; no 10 s sample of B's private bytes is above 48.7 GiB. Without the five spike samples, B's highest private sample is 43.5 GiB and its peak commit is 71.3 / 56.3 GiB.
APS5_NO_LIBRARY_PREBUILD=1, which adds nothing, see below): private memory in the level is about 18 GiB lower (40.8 → 23.2), and peak commit minus other processes is 18 GiB lower (56.3 → 38.3, without B's spikes). B→C changes only the opt-in in the environment, and the binaries differ only by #5484. It gives the same result: 17.9 GiB in the level and 18.4 GiB of commit. A→C (one binary) shows 21–22 GiB, but C also raises the cap and adds the affinity masks. A→B puts the cap's effect at about 3.5 GiB in the level, which matches the 4.3 GiB of extra SPIR-V that went into file-backed views.APS5_NO_LIBRARY_PREBUILD=1, so we can't say how much of it is small SPIR-V or the registration-time library prebuild.APS5_NO_LIBRARY_PREBUILD=1(#5551) adds nothing on top of the opt-in (C→D: +0.5 GiB peak, +0.2 GiB in the level, same peak commit; C, D and E differ by up to 0.7 GiB in the level). With the opt-in, registration discards the plan (ShaderRegistry.cpp:1522-1525atc8bedff05), so the registration-time prebuild inPrepareRegistered(:900-902) never runs. The knob then only gates prebuilds reached from explicit ABI resolution (ResolveShaderAbiat:1308,ResolveGraphicsStagesAbiat:1262).APS5_HEAP_CACHE_MIB=512(E) made no measurable difference either.frame_msdata above.On a 32 GB machine, the opt-in is the difference between paging about 22 GiB and about 1 GiB. That supports keeping it as the stopgap until the cap/eviction change lands.
Nit (optional):
APS5_LAZY_SHADER_PREPARE=0also turns the mode on.ShaderRegistry.cpp:1522testsstd::getenv(...) != nullptr, the same asAPS5_SYNC_SHADER_PREPAREat:1073.docs/dev/ARCHITECTURE.md:103documents=1, andAPS5_NO_LIBRARY_PREBUILD(:874) compares against"1".GPUs tested: NVIDIA GeForce RTX 3060, driver 591.86, Windows Server 2025.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T10:07:40Z.
Review: APPROVED
Re-checked.
LazyRegistrationnow asserts the snapshot is deferred with no entries after registration and prepared after the first dispatch; with the lazy branch inShaderRegistry.cppdisabled,agc_driver_prepared_shaders_lazyand_lazy_syncfail. Built merged onto main: all 11agc_driver_prepared_shaders*pass; the only other failures (agc_driver_tessellation,agc_driver_preparation_scope_settle) also fail on main.Memory numbers are backed by @AnthonyClemens (RE2) and @fdfawef324 (Demon's Souls), and the frame_ms data answers the first-use question. Non-blocking:
APS5_LAZY_SHADER_PREPARE=0also enables the mode, same asAPS5_SYNC_SHADER_PREPARE. Merging as the opt-in stopgap.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T10:52:39Z.
Re-checked after main changed
libSceAgcDriver/CMakeLists.txtandDriver.hpp: same head, merges cleanly. Built merged onto current main and ran the AgcDriver ctest scope: all 11agc_driver_prepared_shaders*pass, including_lazyand_lazy_sync; the only failure isagc_driver_tessellation, which segfaults on main too.Memory numbers are backed by @AnthonyClemens (RE2) and @fdfawef324 (Demon's Souls). Non-blocking:
APS5_LAZY_SHADER_PREPARE=0also enables the mode, same asAPS5_SYNC_SHADER_PREPARE. Merging as the opt-in stopgap.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T11:18:10Z.
Re-checked after main moved again: same head, merges cleanly. Built merged onto current main and ran the AgcDriver ctest scope: all 11
agc_driver_prepared_shaders*pass, including_lazyand_lazy_sync; the only failure isagc_driver_tessellation, which segfaults on main too.Memory numbers are backed by @AnthonyClemens (RE2) and @fdfawef324 (Demon's Souls). Non-blocking:
APS5_LAZY_SHADER_PREPARE=0also enables the mode, same asAPS5_SYNC_SHADER_PREPARE. Merging as the opt-in stopgap.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T11:36:00Z.
Re-checked after main moved again: same head, merges cleanly. Built merged onto current main and ran the AgcDriver ctest scope: all 11
agc_driver_prepared_shaders*pass, including_lazyand_lazy_sync; the only failure isagc_driver_tessellation, which segfaults on main too.Memory numbers are backed by @AnthonyClemens (RE2) and @fdfawef324 (Demon's Souls). Non-blocking:
APS5_LAZY_SHADER_PREPARE=0also enables the mode, same asAPS5_SYNC_SHADER_PREPARE. Merging as the opt-in stopgap.