Skip to content

feat(agc): persistent shader disk cache and Vulkan pipeline cache - #129

Merged
boykopovar merged 2 commits into
boykopovar:mainfrom
oneandonlydean:feat/shader-disk-cache
Sep 30, 2026
Merged

boykopovar merged 2 commits into
boykopovar:mainfrom
oneandonlydean:feat/shader-disk-cache

Conversation

@oneandonlydean

@oneandonlydean oneandonlydean commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

What

The recompiler's variant cache and the device's VkPipelineCache only lived in memory, so every run recompiled every shader. A title's first frames spent hundreds of milliseconds per large program in the front end, emitter and optimizer, and titles that time their queues against each other could miss their deadlines.

Shader disk cache (core/shader/recompiler/ShaderDiskCache.*):

  • What is stored: each compiled variant, meaning the SPIR-V plus the compiled shader info and binding allocation that per-snapshot binding population needs. There is one file per key, written atomically by a background thread. A later run loads the file instead of compiling.
  • Key: everything the in-memory variant cache already matches on:
    • the in-memory recompile key (stage, code hash, stage registers, user data count, target)
    • every code word
    • the host subgroup size
    • the binding layout
    • the resource specialization
    • the values of the APS5_* switches that the recompiler sources read. These are collected at build time; switches that only report are excluded.
  • Validation: entries live under one directory per source version. The source version is a build-time hash of the recompiler sources, the AGC driver sources and the build configuration, so any source change starts a fresh directory. Each entry carries its key and checksums. An entry whose format, version, key, size or checksum doesn't match, or whose payload doesn't decode exactly, is ignored and recompiled.
  • Cleanup: directories that no build has used for two weeks are removed, and so are stale temporary files.
  • Bypass: requests under the debug probe skip the disk cache.
  • Encoder guard: static_asserts on the stored structures' sizes (Linux/x86-64/libstdc++) force the encoder to be updated when a field is added. Other open PRs that add fields to RecompileResult, CompiledShaderInfo or the binding structures will have to extend the encoder. Fields added only to the request are covered by the in-memory key and the source version. The disk cache has its own FormatVersion (1) and doesn't use RequestSerializer, so no serializer version change is needed here.

Pipeline cache: the device's VkPipelineCache is loaded at creation if the file's checksum and Vulkan header match the device. Vendor, device and pipelineCacheUUID are checked, and the driver version is part of the file name. The cache is saved every 10 s while it grew, and again at teardown.

Settings:

  • Location: a shader_cache folder beside the running executable (found through /proc/self/exe or GetModuleFileNameW, not the working directory), so the relinked game stays a self-contained folder that changes nothing outside itself. ANYPS5_SHADER_CACHE_DIR overrides it.
  • ANYPS5_NO_SHADER_CACHE=1 turns both caches off. The AGC ctest entries run with it set, so tests never write to the user's cache.

Tested

  • Linux (CachyOS, GCC). New agc_shader_disk_cache test (in ctest) covers:
    • the result and entry encoding round trips
    • that the key changes with every input
    • that damaged, truncated and other-version entries are rejected
    • a variant stored by one process and loaded by a second one
  • agc_driver_graphics_tests passes with test(agc): link agc_driver_graphics_tests again, run it under ctest and bring its stale expectations up to date #113 merged in on a throwaway branch. That merge has a one-line conflict in the foreach(agcTest ...) list of libSceAgcDriver/CMakeLists.txt: keep both test names.
  • Exercised in Astro Bot on our integration branch, where the cache has been on since it was written.
  • The test also checks that the default directory is shader_cache beside the test executable, and that ANYPS5_NO_SHADER_CACHE=1 disables it.
  • Rebased on current main. ctest: 62/62 pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@github-actions github-actions Bot added build shader core/shader agc libSceAgc* labels Sep 29, 2026
@boykopovar

Copy link
Copy Markdown
Owner

A useful feature, especially since there is no recompilation of shaders at the relinker stage. The only thing I don't like is the location of the cache in the system paths. After all, with the current architecture of the project, before your changes, the final working result is an independent folder that does not depend on anything from the outside, and does not change anything outside, and this is important. Can you make the cache folder be created next to the executable file (not in the working directory, because the file can be launched from anywhere, namely next to the executable file)?

@oneandonlydean

Copy link
Copy Markdown
Collaborator Author

Good point, done. The cache now lives in a shader_cache folder beside the running executable (found through /proc/self/exe on Linux and GetModuleFileNameW on Windows, so the working directory doesn't matter), and nothing is written outside the game folder anymore. ANYPS5_SHADER_CACHE_DIR still overrides it and ANYPS5_NO_SHADER_CACHE=1 turns it off. The test checks the default location. Also rebased on current main.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Sep 30, 2026
oneandonlydean and others added 2 commits September 30, 2026 12:01
Every run recompiled every shader: the recompiler's variant cache and
the device's VkPipelineCache only lived in memory, and the first frames
of a title spent hundreds of milliseconds per large program in the
front end, the emitter and the optimizer.

Compiled variants (the SPIR-V plus the compiled shader info and binding
allocation that per-snapshot binding population needs) are now stored
on disk, one file per key, written atomically by a background thread,
and a later run loads them instead of compiling. The key holds every
input the in-memory variant cache matches on: the in-memory recompile
key (stage, code hash, stage registers, user data count, target), every
code word, the host subgroup size, the binding layout, the resource
specialization, and the values of the APS5_* switches the recompiler
sources read (found at build time; switches that only report are left
out). Entries live under a directory per source version, a build-time
hash of the recompiler and AGC driver sources and the build
configuration, and carry their key and checksums: an entry whose
format, version, key, size or checksum does not match, or whose payload
does not decode exactly, is ignored and recompiled. Directories no
build used for two weeks and stale temporary files are removed. Requests
under the debug probe bypass the disk cache.

The device's VkPipelineCache is loaded at creation when the file's
checksum and Vulkan header match the device (vendor, device and
pipelineCacheUUID; the driver version is in the file name), saved every
10 s while it grew, and saved at teardown.

Location: a shader_cache folder beside the running executable (not the
working directory), so the relinked game stays a self-contained folder;
ANYPS5_SHADER_CACHE_DIR overrides it. ANYPS5_NO_SHADER_CACHE=1 turns both caches off; the AGC
tests run with it set so they never write to the user's cache.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ
@boykopovar
boykopovar force-pushed the feat/shader-disk-cache branch from 7bab67d to bb60336 Compare September 30, 2026 09:04
@boykopovar
boykopovar merged commit 819391d into boykopovar:main Sep 30, 2026
3 checks passed
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Sep 30, 2026
…, boykopovar#129) into astrobot

Conflicts resolved:
- Shader disk cache (boykopovar#129 as merged upstream): upstream's version, plus
  astrobot's imageDepthCompare and waveLayout fields (format 3).
- Shader fusion: upstream's fd4700b replaces astrobot's boykopovar#124 version.
- -KRzWekV120: upstream's export replaces astrobot's; its test now calls
  sceAgcUnknown__MKRzWekV120.
- Misaligned buffer views: upstream's push-constant adjustment (3dec6ce)
  replaces astrobot's view aliases; GuestBufferMemory is the rebased boykopovar#121
  version (write watch on shared direct memory).
- DirectMemory: rebased boykopovar#121 (re-register after the shared-backing remap,
  aliased pages leave the watch) plus boykopovar#164's per-piece unmap.
- Depth: astrobot's DepthTarget model is kept for attachments; upstream's
  DepthSurface module is dropped (it would duplicate it). Upstream's
  mip-chain colour targets, CB_COLOR_VIEW MIP_LEVEL at bits 26-29 and
  export formats are taken into DecodeColorBuffer.
- GPU-selected V#s: upstream's gpuDescriptor replaces astrobot's opt-in
  runtimeDescriptor; image_bvh_intersect_ray is upstream's.
- Occlusion counter dumps: upstream's Vulkan occlusion queries replace
  astrobot's always-visible PIXEL_PIPE_STAT dump.
- AudioOut2 DualSense ports: rebased boykopovar#180 on upstream's 16-bit ports.
- Pixel stage decode: rebased boykopovar#136; mesh push constants: rebased boykopovar#133.
- Label waits (boykopovar#178): noteHeldAtSubmit moved into reserveOutputs.
@oneandonlydean
oneandonlydean deleted the feat/shader-disk-cache branch October 1, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build needs rebase Conflicts with main, rebase needed shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants