[GH-ISSUE #2787] [Feature Request]: Shader & Pipeline Cache Persistence (SPIR-V & Shader Replacement Support) #903

Closed
opened 2026-02-27 21:08:47 +03:00 by kerem · 5 comments
Owner

Originally created by @ydzat on GitHub (Apr 15, 2025).
Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/2787

Checklist

  • I have searched for a similar issue in this repository and did not find one.

Description

Disclaimer: This implementation was generated with the help of AI. While it works in local testing (improves long-session stutter issues in games like Bloodborne), I cannot guarantee its correctness or alignment with the project's long-term architecture. This issue is meant to document and share the design idea and patch structure in case any of it proves helpful for future development.

I attempted to introduce a persistent shader cache system for the project with the following features:

  • Save and load Vulkan Pipeline and SPIR-V caches.
  • Load cache in the constructor, save in the destructor.
  • Avoid redundant compilation using cached results.
  • Dynamically load and modify SPIR-V (including patch files).
  • Replace specified shader modules at runtime.
  • Track cache hit ratio and provide debug logs.
  • Add a config option (shouldUseShaderCache) to toggle the feature.

General Goals:

  • Save/load Vulkan Pipeline and SPIR-V shader cache.
  • Enable dynamic SPIR-V patching at runtime.
  • Support shader module replacement.

1. Implementation Summary

Modified Files:

vk_pipeline_cache.h
vk_pipeline_cache.cpp
config.h
config.cpp

Main Features:

1. Vulkan Pipeline Cache Management

Files: vk_pipeline_cache.h, vk_pipeline_cache.cpp

  • New Methods:

    • SavePipelineCache(), LoadPipelineCache()
    • SaveSpirvCache(), LoadSpirvCache()
    • GetShaderCachePath(), GetPipelineCachePath(), GetSpirvCachePath()
  • New Variables:

    • spirv_cache (stores compiled SPIR-V binaries)
    • spirv_cache_dirty (tracks cache updates)
  • Logic:

    • Constructor loads caches; destructor saves them
    • CompileModule() tries SPIR-V cache before recompilation

2. Config Support

File: config.cpp, config.h

  • New Option: shouldUseShaderCache
  • Behavior:
    • Enables/disables cache feature
    • Persistent through config file (default: true)

3. SPIR-V Compilation Optimization

File: vk_pipeline_cache.cpp

  • Logic in CompileModule():
    • Checks SPIR-V cache first
    • Falls back to compilation if needed
    • Applies optional patch via GetShaderPatch()

4. Shader Replacement Feature

File: vk_pipeline_cache.cpp

  • New Method: ReplaceShader()
  • Behavior:
    • Replaces a shader at runtime
    • Clears affected pipeline entries

5. Shader Module Naming

File: vk_pipeline_cache.cpp

  • New Method: GetShaderName()
  • Logic:
    • Generates unique names for shaders
    • Uses SetObjectName() for debugging clarity

6. Cache Path Management

File: vk_pipeline_cache.cpp

  • Creates standard cache directories
  • Uses std::filesystem

7. Statistics Tracking

Files: vk_pipeline_cache.h, vk_pipeline_cache.cpp

  • New Variables: cache_hits, total_requests
  • Logs: Cache hit ratio per session

8. Debug Logging

File: vk_pipeline_cache.cpp

  • Logs key steps: loading, saving, hit/miss
  • Helpful for profiling/debugging

2. File Relationships

vk_pipeline_cache.h: Declares interfaces for cache and compilation.

vk_pipeline_cache.cpp: Implements logic including caching, shader patching, replacement.

config.cpp: Adds shouldUseShaderCache, allowing runtime toggle and persistence.

Cache paths in vk_pipeline_cache.cpp respect user configuration from config.cpp.

Reason

  • Reduces shader/pipeline recompilation.
  • Improves runtime performance (validated in long-play sessions).
  • Introduces features helpful for debugging and modding.
  • Offers an idea for long-term shader persistence even if not officially planned

Examples

No response

Originally created by @ydzat on GitHub (Apr 15, 2025). Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/2787 ### Checklist - [x] I have searched for a similar issue in this repository and did not find one. ### Description > Disclaimer: This implementation was generated with the help of AI. While it works in local testing (improves long-session stutter issues in games like Bloodborne), I cannot guarantee its correctness or alignment with the project's long-term architecture. This issue is meant to document and share the design idea and patch structure in case any of it proves helpful for future development. I attempted to introduce a persistent shader cache system for the project with the following features: - Save and load Vulkan Pipeline and SPIR-V caches. - Load cache in the constructor, save in the destructor. - Avoid redundant compilation using cached results. - Dynamically load and modify SPIR-V (including patch files). - Replace specified shader modules at runtime. - Track cache hit ratio and provide debug logs. - Add a config option (shouldUseShaderCache) to toggle the feature. # General Goals: - Save/load Vulkan Pipeline and SPIR-V shader cache. - Enable dynamic SPIR-V patching at runtime. - Support shader module replacement. ## 1. Implementation Summary ### Modified Files: `vk_pipeline_cache.h` `vk_pipeline_cache.cpp` `config.h` `config.cpp` ### Main Features: #### 1. Vulkan Pipeline Cache Management Files: `vk_pipeline_cache.h`, `vk_pipeline_cache.cpp` - New Methods: - SavePipelineCache(), LoadPipelineCache() - SaveSpirvCache(), LoadSpirvCache() - GetShaderCachePath(), GetPipelineCachePath(), GetSpirvCachePath() - New Variables: - spirv_cache (stores compiled SPIR-V binaries) - spirv_cache_dirty (tracks cache updates) - Logic: - Constructor loads caches; destructor saves them - CompileModule() tries SPIR-V cache before recompilation #### 2. Config Support File: `config.cpp`, `config.h` - New Option: shouldUseShaderCache - Behavior: - Enables/disables cache feature - Persistent through config file (default: true) #### 3. SPIR-V Compilation Optimization File: `vk_pipeline_cache.cpp` - Logic in CompileModule(): - Checks SPIR-V cache first - Falls back to compilation if needed - Applies optional patch via GetShaderPatch() #### 4. Shader Replacement Feature File: `vk_pipeline_cache.cpp` - New Method: ReplaceShader() - Behavior: - Replaces a shader at runtime - Clears affected pipeline entries #### 5. Shader Module Naming File: `vk_pipeline_cache.cpp` - New Method: GetShaderName() - Logic: - Generates unique names for shaders - Uses SetObjectName() for debugging clarity #### 6. Cache Path Management File: `vk_pipeline_cache.cpp` - Creates standard cache directories - Uses std::filesystem #### 7. Statistics Tracking Files: `vk_pipeline_cache.h`, `vk_pipeline_cache.cpp` - New Variables: cache_hits, total_requests - Logs: Cache hit ratio per session #### 8. Debug Logging File: `vk_pipeline_cache.cpp` - Logs key steps: loading, saving, hit/miss - Helpful for profiling/debugging ### 2. File Relationships `vk_pipeline_cache.h`: Declares interfaces for cache and compilation. `vk_pipeline_cache.cpp`: Implements logic including caching, shader patching, replacement. `config.cpp`: Adds `shouldUseShaderCache`, allowing runtime toggle and persistence. Cache paths in `vk_pipeline_cache.cpp` respect user configuration from config.cpp. ### Reason - Reduces shader/pipeline recompilation. - Improves runtime performance (validated in long-play sessions). - Introduces features helpful for debugging and modding. - Offers an idea for long-term shader persistence even if not officially planned ### Examples _No response_
kerem closed this issue 2026-02-27 21:08:48 +03:00
Author
Owner

@thecatontheceiling commented on GitHub (Apr 15, 2025):

What is the point of sharting out AI slop in a feature request issue

<!-- gh-comment-id:2804119064 --> @thecatontheceiling commented on GitHub (Apr 15, 2025): What is the point of sharting out AI slop in a feature request issue
Author
Owner

@ydzat commented on GitHub (Apr 15, 2025):

What is the point of sharting out AI slop in a feature request issue

As I asked in shadps4's discord, AI-generated code is generally not guaranteed to be accurate, so pull requests should not be made.
Therefore, I can only provide ideas in as much detail as possible, hoping to be helpful to interested developers ;)

<!-- gh-comment-id:2804167205 --> @ydzat commented on GitHub (Apr 15, 2025): > What is the point of sharting out AI slop in a feature request issue As I asked in shadps4's discord, AI-generated code is generally not guaranteed to be accurate, so pull requests should not be made. Therefore, I can only provide ideas in as much detail as possible, hoping to be helpful to interested developers ;)
Author
Owner

@thecatontheceiling commented on GitHub (Apr 15, 2025):

It's not helping much because the only thing it did is list what files it changed and what variables it added with a one sentence explanation of what was changed (which in almost every case put emphasis on something extremely trivial).

It's just not very useful as it doesn't even function as an implementation plan. AI is not capable of participating in the development of complex emulator code.

<!-- gh-comment-id:2804547996 --> @thecatontheceiling commented on GitHub (Apr 15, 2025): It's not helping much because the only thing it did is list what files it changed and what variables it added with a one sentence explanation of what was changed (which in almost every case put emphasis on something extremely trivial). It's just not very useful as it doesn't even function as an implementation plan. AI is not capable of participating in the development of complex emulator code.
Author
Owner

@ydzat commented on GitHub (Apr 15, 2025):

It's not helping much because the only thing it did is list what files it changed and what variables it added with a one sentence explanation of what was changed (which in almost every case put emphasis on something extremely trivial).

It's just not very useful as it doesn't even function as an implementation plan. AI is not capable of participating in the development of complex emulator code.

Ok, sorry :( I thought this would help because it does reduce the lag to some extent when I run it locally. Looks like I'll just have to close this thread. Also, what should I put forward to be considered helpful?

<!-- gh-comment-id:2804610443 --> @ydzat commented on GitHub (Apr 15, 2025): > It's not helping much because the only thing it did is list what files it changed and what variables it added with a one sentence explanation of what was changed (which in almost every case put emphasis on something extremely trivial). > > It's just not very useful as it doesn't even function as an implementation plan. AI is not capable of participating in the development of complex emulator code. Ok, sorry :( I thought this would help because it does reduce the lag to some extent when I run it locally. Looks like I'll just have to close this thread. Also, what should I put forward to be considered helpful?
Author
Owner

@thecatontheceiling commented on GitHub (Apr 15, 2025):

I didn't mean to bully you into closing this issue lol

The feature itself that you're suggesting is a good idea (on paper), I meant to say that AI can't really help with something as complex to implement as this

Many other complex emulators like RPCS3 and Ryujinx also have some form of shader caching implemented, and it would be nice if ShadPS4 had something similar too

When you make a feature request you're not expected to provide an implementation of said feature, that is what pull requests are for :)

<!-- gh-comment-id:2804702545 --> @thecatontheceiling commented on GitHub (Apr 15, 2025): I didn't mean to bully you into closing this issue lol The feature itself that you're suggesting is a good idea (on paper), I meant to say that AI can't really help with something as complex to implement as this Many other complex emulators like RPCS3 and Ryujinx also have some form of shader caching implemented, and it would be nice if ShadPS4 had something similar too When you make a feature request you're not expected to provide an implementation of said feature, that is what pull requests are for :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#903
No description provided.