fix(shader): map retained SPIR-V from shader cache entries #4806
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4806"
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 eljackjoestar-cmd on 2026-10-12T22:26:44Z.
What
Replace this PR's opt-in temporary-file backing with read-only mappings of the shader disk-cache entries themselves.
APS5_FILE_BACKED_SPIRVand the extra temporary files are removed from the implementation.This adopts ThatsLeo's
perf/shader-spirv-on-diskimplementation (0bc9120bb), preserving its authorship, and adds the Windows mapping path. Cache-loaded variants retain a shared mapping owner; newly compiled variants are persisted to the normal cache and mapped back before retention. Reading or copying a module shares that storage; mutation detaches into a private vector.Words()returns a const span, and explicit vector consumers useCopy().Windows mappings share read, write and delete access, so atomic replacement/removal of a cache file does not invalidate its existing readers. Failed cache persistence/mapping and disabled caches retain the existing vector path. No dedicated backing-enable switch, temporary-volume dependency, memory-budget knob or cache-version bypass is introduced. This does not add cache eviction; normal cache files still use disk space.
The cache payload is aligned and its format advances from 30 to 31. Length, key and payload validation remain in force.
Tested
Windows 11 Pro 10.0.26300, GCC 15.2, Release, SPIRV-Tools OFF, current main
f87799aa3plus this PR alone.The cache regression verifies actual Windows
MEM_MAPPEDstorage, round-trip words/metadata, copy-on-write isolation, and retained word access after the original cache pathname is atomically replaced and removed. Existing cross-process, corruption, cache-invalidation, binding, vertex specialization and invocation-isolation cases also pass.Full Windows build passed on the final rebased branch. Four focused cache/shader/rendering checks passed on NVIDIA RTX 5070 / driver 617.14 in 6.81 s; graphics and prepared-shader checks passed on AMD Radeon integrated gfx1036 / driver 32.0.21036.18 in 5.26 s. The cache regression also exercises loading while a published writer retains write/rename access. Conventions and whitespace checks pass. Linux/macOS execution and full-suite CI are not claimed for this revised head.
The previous opt-in implementation's memory measurements are superseded and are not evidence for this implementation. The independent Demon's Souls report in https://github.com/boykopovar/AnyPS5/pull/4806#issuecomment-6160926186 applies to ThatsLeo's original POSIX branch: it reached the tutorial where its comparison run exhausted the memory cap. That report used a warm cache and test-only format/source-version overrides, which are not included here. It is external Linux title evidence, not a Windows title validation of this revised head. No new Windows title-memory or speed improvement is claimed yet.
Checklist
main; no equivalent open cache-mapping PR foundRelated: #5552 changes the fallback file reader. Its head f2b86928 merges cleanly with this branch in a pairwise merge simulation; that simulation is not a combined build/test claim.
Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T06:48:01Z.
In a plain build the two new tests don't run.
cmake --build buildfollowed by--target libsleavesagc_shared_spirv_testsunbuilt, soctestreportsagc_shared_spirv_heapandagc_shared_spirv_fileas Not Run.Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head 501f19776 merged into
main495bf91a0. It now conflicts withmainea2bbefb2indocs/dev/debt/shader.md.add_test_executable(agc_shared_spirv_tests EXCLUDE_FROM_ALL ...)has noset_target_properties(agc_shared_spirv_tests PROPERTIES EXCLUDE_FROM_ALL OFF). The other registered tests in this file set that insideif(BUILD_TESTING), and the twoadd_testlines here are outside that block.ctest --timeout 120 --parallel 4) then passes 1023 of 1025 on both GPUs, and the two failures are these Not Run entries.tools/check_test_registration.pystill passes, because it only checks executables that were built.--target agc_shared_spirv_tests), both tests pass on both GPUs with--repeat until-fail:3. WithAPS5_FILE_BACKED_SPIRV=1it printssharing, write isolation, lifetime and reads passed, with a private-bytes delta of 163840.Also, the new block sits between
add_test_executable(agc_shader_disk_cache_tests ...)and that target's owntarget_include_directoriesandtarget_link_librarieslines.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by eljackjoestar-cmd on 2026-10-13T08:17:01Z.
Rebased onto main
fc1f063f5, preserving the newer shader-debt entries. Also corrected the reported registration defect: the shared-SPIR-V test target is included in the normal build whenBUILD_TESTING=ON, its twoadd_testcalls are guarded by that option, and its block is separate from the disk-cache target setup.The default full Windows Release build now builds the executable without naming its target. Heap/file storage, shader memory and shader disk cache all pass without skips (four tests, 0.78 s). The 64 MiB private-byte deltas are 67,244,032 bytes for heap storage and 159,744 bytes for file backing. Separate cache-disabled heap/file replay processes emit identical 134,780-word SPIR-V for the captured title shader. Conventions and whitespace checks pass. No new game-runtime claim is made for this rebase.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:47:10Z.
Rebased onto current
main; the only conflict was the adjacent line indocs/dev/debt/shader.md(kept both, SharedSpirv entry before the BDA span reads one). Checked the shared-SPIR-V heap/file tests on Linux (pass, and they don't build without the change), the rest of the libSceAgc/libSceAgcDriver/libc/libkernel ctest scope (onlyagc_driver_tessellationsegfaults inside RADV, unrelated to this code, default storage unchanged). Merging once the Windows build is green.Imported from boykopovar/AnyPS5. Originally posted by ThatsLeo on 2026-10-13T16:16:23Z.
Data from Linux, and a follow-up for after this merges.
Stray (PPSA02101) registers about 3,400 shaders, with a median of 520 KB of SPIR-V each and 3.1 GB on disk.
heaptrackputs 3.3 GB of the driver's heap inShaderDiskCache::decodeArtifact, kept for the whole run. On an Intel Meteor Lake laptop with 14 GB shared between CPU and GPU, Stray goes out of memory onmain5a76fd71with a cold cache, 50 s in.I tried the same idea a different way, on
ThatsLeo/AnyPS5:perf/shader-spirv-on-disk(f320698e):SharedSpirvholds a view into the mapping, with the mapping as its owner.ShaderDiskCache::Persist). A cold first run, the one after every update, is covered too.agc_shader_disk_cachechecks that.Persistreturns null and loads read the file as before, since I can't test whether a mapped entry can be replaced there.Stray,
main5a76fd71against that branch, 150 s on the brightness calibration screen: anonymous memory 5.3 GB plus 4.4 GB swapped with 0.2 GB available, against 2.6 GB plus 4.2 GB with 3.3 GB available. The coldmainrun went out of memory; the branch didn't. One caveat:main's cache was still partly cold in that run, so the frame rates (0.1 to 0.7 fps against 1.1 to 1.6) aren't comparable.Full
cteston Intel Arc and lavapipe: 1053 of 1054 pass.guest_ampr_cpp_errordoesn't link with Fedora's toolchain, onmainas well.It touches the same
SharedSpirvas this PR, so I won't open it alongside. Once this merges, I can rebase it on top: the cache's own entries become one more backing for your storage, and the temporary-file path stays for when the disk cache is disabled. If you'd rather fold the cache-file mapping in here, the branch is yours to take.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-13T18:07:09Z.
Rebase handoff — rebased onto current
main(staleness only, applied cleanly), authorship unchanged.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesmainregression (agc_driver_tessellationsegfault, #4727)To pick it up:
Head is
ee727ce4a.Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T19:34:05Z.
I encountered the documented backing-write failure policy while testing Demon's Souls with
APS5_FILE_BACKED_SPIRV=1: retained modules filled the temporary volume after about three minutes. I prepared a bounded-storage and heap-fallback correction atfdfawef324:codex/shared-spirv-bounded-offer, directly on this PR's current headcebe922c17. It contains only the correction above your original implementation, preserving authorship.The default live backing limit is 1 GiB per recompiler library, configurable with
APS5_FILE_BACKED_SPIRV_MAX_BYTES. Concurrent reservations follow the shared mapping's lifetime, so copies and moves consume no extra capacity, and detaching or releasing the last owner returns it. Budget exhaustion or a backing I/O/allocation failure clears partial resources and moves the original vector to heap storage. Failed-open names and failed POSIX unlink names are retained for cleanup retry; successful Windows delete-on-close handles immediately discard their pathname, avoiding a later pathname-deletion race. Unrecoverable temporary-file cleanup disables further backing in that library.This trades additional private memory for bounded temporary disk use and continuity on storage failure. Retained module count remains unbounded, and this does not replace cache eviction. Shader words and cache semantics are unchanged. The correction has not been tested in another game run.
CPU-tested platform: Windows Server 2025 10.0.26100 x64, WinLibs GCC 15.2, Release. Both added regressions fail on the prior implementation: it ignores a 64 KiB limit, and a real invalid temporary-directory resource path throws. All four heap/file/budget/I/O tests pass with the correction, without skips, in 1.92 s. Coverage includes shared and moved owners, last-owner release, detached writes, concurrent reservations, preserved vector storage, copy-on-write after fallback and preservation of the existing blocked path. Conventions and diff checks pass; Linux/macOS execution remains unverified.
GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100 (original failure run; the correction received CPU storage tests with no GPU execution).
AI-assisted: yes.
Co-Authored-By: Codex [email protected]
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T03:06:00Z.
APS5_FILE_BACKED_SPIRVis an opt-in environment switch; a change like this goes in as the one path, not behind a knob. With it on, the temporary volume filled after about three minutes in the run reported above. Mapping the disk-cache entry itself, as in ThatsLeo's branch, needs neither the switch nor extra disk space; could you take that route instead?Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-14T04:30:46Z.
A Demon's Souls (PPSA01341 v1.07) data point for the route @oneandonlydean suggested. @ThatsLeo, your
perf/shader-spirv-on-disk(0bc9120bb) gets the title into its tutorial level on Linux. Without it, the title runs out of memory while the level loads.Setup: Linux, RTX 5070 Ti, NVIDIA 595.91, 30 GiB RAM,
ANYPS5_ENABLE_SPIRV_TOOLS=OFF. Basemain7a0c838cawith local merges of #758, #5181 (ceda63b9c, b130903c8) and a local stencil-export workaround for #4276. The run is capped at 22 GiB RAM plus 12 GiB swap. Warm shader cache, which needed two test-only tweaks to stay valid across the builds (I kept the cache format at 30 and overrode the source version). Neither is part of your change.A heap profile of the run without it puts the growth in SPIR-V decoded from the disk cache and kept in
SourceEntry::variants: 3.1 GiB live at t+40 and 10.0 GiB at t+340, about 1.2 MiB per cache hit.This PR's file-backed storage doesn't help on this machine, because
/tmpis tmpfs and the spilled modules stay in RAM.If you open the branch as a PR, I can rerun it there.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by eljackjoestar-cmd on 2026-10-14T07:51:45Z.
Replaced the opt-in temporary-file implementation with the suggested cache-entry mapping route. The branch now carries ThatsLeo's original change with its authorship preserved, plus Windows mapping support at
4a0489a7a. There is noAPS5_FILE_BACKED_SPIRVswitch or separate temporary backing file. Cache-loaded and freshly persisted variants retain their cache mapping; mutation detaches a private copy.Rebased onto
f87799aa3. Full Windows Release build passed. Four cache/shader/rendering checks passed on RTX 5070 (617.14), and the two rendering/prepared-shader checks passed on AMD integrated gfx1036 (32.0.21036.18). The regression verifies mapped storage, copy isolation, a retained writer's write/rename access, and reader survival after replacement/removal of the pathname. Conventions pass. The body records the Demon's Souls result above as external evidence for the original POSIX branch; its test-only format/source-version overrides are not included, and no revised Windows title-memory result is claimed.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T07:54:51Z.
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/tests/ShaderMemory.cppL726-792, L812-862core/shader/recompiler/ShaderDiskCache.cppL80-83core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppL726-792, L817-862core/shader/recompiler/ShaderDiskCache.cppL80-83core/shader/recompiler/ShaderDiskCache.hppL16core/shader/recompiler/ShaderDiskCache.cppL16-17Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T08:56:50Z.
The Linux build with SPIRV-Tools failed in
SpirvNarrowConstantStore.cpp:SpirvTools::Validatehas no overload for a span, so I pushed a one-line fix that passes the word pointer and count. It builds withANYPS5_ENABLE_SPIRV_TOOLS=ONand the shader tests pass; running the full build again.Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-14T09:46:53Z.
Demon's Souls (PPSA01341 v1.07) on Linux with this head (
4a0489a7a), as offered above. With it, the title loads the tutorial level and stays in it, where it was OOM-killed without it.Setup: Linux, RTX 5070 Ti, NVIDIA 595.91,
ANYPS5_ENABLE_SPIRV_TOOLS=OFF. Basemain6546a3394with local merges of #758, #5181 (three commits) and #5747, plus a local stencil-export workaround for #4276. The run is capped at 22 GiB RAM and 12 GiB swap. Warm shader cache: 9067 hits, 757 misses.One caveat: the run without the mapping was on an older base (
main7a0c838cawith #5181). The newer base also includes #290's larger import budget, so the base differs a little besides this PR.The Linux side works like ThatsLeo's original branch did here (numbers in my earlier comment).
AI-assisted: yes