diff --git a/.gitmodules b/.gitmodules
index b8d1544e4..c5d05edd3 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -117,3 +117,6 @@
path = externals/sdl3_mixer
url = https://github.com/libsdl-org/SDL_mixer
shallow = true
+[submodule "externals/miniz"]
+ path = externals/miniz
+ url = https://github.com/richgel999/miniz
diff --git a/CMakeLists.txt b/CMakeLists.txt
index efa5016b9..b4328886f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -512,7 +512,7 @@ set(PAD_LIB src/core/libraries/pad/pad.cpp
src/core/libraries/pad/pad_errors.h
)
-set(SYSTEM_GESTURE_LIB
+set(SYSTEM_GESTURE_LIB
src/core/libraries/system_gesture/system_gesture.cpp
src/core/libraries/system_gesture/system_gesture.h
)
@@ -693,7 +693,6 @@ set(COMMON src/common/logging/backend.cpp
src/common/lru_cache.h
src/common/error.cpp
src/common/error.h
- src/common/scope_exit.h
src/common/fixed_value.h
src/common/func_traits.h
src/common/native_clock.cpp
@@ -707,6 +706,8 @@ set(COMMON src/common/logging/backend.cpp
src/common/rdtsc.h
src/common/recursive_lock.cpp
src/common/recursive_lock.h
+ src/common/scope_exit.h
+ src/common/serdes.h
src/common/sha1.h
src/common/shared_first_mutex.h
src/common/signal_context.h
@@ -990,6 +991,8 @@ set(VIDEO_CORE src/video_core/amdgpu/cb_db_extent.h
src/video_core/renderer_vulkan/vk_pipeline_cache.h
src/video_core/renderer_vulkan/vk_pipeline_common.cpp
src/video_core/renderer_vulkan/vk_pipeline_common.h
+ src/video_core/renderer_vulkan/vk_pipeline_serialization.cpp
+ src/video_core/renderer_vulkan/vk_pipeline_serialization.h
src/video_core/renderer_vulkan/vk_platform.cpp
src/video_core/renderer_vulkan/vk_platform.h
src/video_core/renderer_vulkan/vk_presenter.cpp
@@ -1027,6 +1030,8 @@ set(VIDEO_CORE src/video_core/amdgpu/cb_db_extent.h
src/video_core/texture_cache/tile_manager.cpp
src/video_core/texture_cache/tile_manager.h
src/video_core/texture_cache/types.h
+ src/video_core/cache_storage.cpp
+ src/video_core/cache_storage.h
src/video_core/page_manager.cpp
src/video_core/page_manager.h
src/video_core/multi_level_page_table.h
@@ -1081,7 +1086,8 @@ add_executable(shadps4
create_target_directory_groups(shadps4)
target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg Dear_ImGui gcn half::half ZLIB::ZLIB PNG::PNG)
-target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator LibAtrac9 sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::glslang SDL3::SDL3 SDL3_mixer::SDL3_mixer pugixml::pugixml stb::headers libusb::usb lfreist-hwinfo::hwinfo nlohmann_json::nlohmann_json)
+target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator LibAtrac9 sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::glslang SDL3::SDL3 SDL3_mixer::SDL3_mixer pugixml::pugixml)
+target_link_libraries(shadps4 PRIVATE stb::headers libusb::usb lfreist-hwinfo::hwinfo nlohmann_json::nlohmann_json miniz)
target_compile_definitions(shadps4 PRIVATE IMGUI_USER_CONFIG="imgui/imgui_config.h")
target_compile_definitions(Dear_ImGui PRIVATE IMGUI_USER_CONFIG="${PROJECT_SOURCE_DIR}/src/imgui/imgui_config.h")
diff --git a/dist/net.shadps4.shadPS4.metainfo.xml b/dist/net.shadps4.shadPS4.metainfo.xml
index 5798876f6..c85fcf003 100644
--- a/dist/net.shadps4.shadPS4.metainfo.xml
+++ b/dist/net.shadps4.shadPS4.metainfo.xml
@@ -18,19 +18,19 @@
https://cdn.jsdelivr.net/gh/shadps4-emu/shadps4@main/documents/Screenshots/1.png
- Bloodborne
+ Bloodborne by From Software
https://cdn.jsdelivr.net/gh/shadps4-emu/shadps4@main/documents/Screenshots/2.png
- Hatsune Miku: Project DIVA Future Tone
+ Hatsune Miku Project DIVA Future Tone by SEGA
https://cdn.jsdelivr.net/gh/shadps4-emu/shadps4@main/documents/Screenshots/3.png
- Yakuza 0
+ Yakuza 0 by SEGA
https://cdn.jsdelivr.net/gh/shadps4-emu/shadps4@main/documents/Screenshots/4.png
- Persona 4 Golden
+ DRIVECLUBâ„¢ by Evolution Studios
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index b6c7c746e..eb3723f2c 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -261,3 +261,6 @@ endif()
#nlohmann json
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(json)
+
+# miniz
+add_subdirectory(miniz)
diff --git a/externals/miniz b/externals/miniz
new file mode 160000
index 000000000..174573d60
--- /dev/null
+++ b/externals/miniz
@@ -0,0 +1 @@
+Subproject commit 174573d60290f447c13a2b1b3405de2b96e27d6c
diff --git a/src/common/config.cpp b/src/common/config.cpp
index d1b75be32..826117f48 100644
--- a/src/common/config.cpp
+++ b/src/common/config.cpp
@@ -185,6 +185,8 @@ static ConfigEntry vkCrashDiagnostic(false);
static ConfigEntry vkHostMarkers(false);
static ConfigEntry vkGuestMarkers(false);
static ConfigEntry rdocEnable(false);
+static ConfigEntry pipelineCacheEnable(false);
+static ConfigEntry pipelineCacheArchive(false);
// Debug
static ConfigEntry isFpsColor(true);
@@ -362,6 +364,14 @@ bool isRdocEnabled() {
return rdocEnable.get();
}
+bool isPipelineCacheEnabled() {
+ return pipelineCacheEnable.get();
+}
+
+bool isPipelineCacheArchived() {
+ return pipelineCacheArchive.get();
+}
+
bool fpsColor() {
return isFpsColor.get();
}
@@ -473,6 +483,14 @@ void setRdocEnabled(bool enable, bool is_game_specific) {
rdocEnable.set(enable, is_game_specific);
}
+void setPipelineCacheEnabled(bool enable, bool is_game_specific) {
+ pipelineCacheEnable.set(enable, is_game_specific);
+}
+
+void setPipelineCacheArchived(bool enable, bool is_game_specific) {
+ pipelineCacheArchive.set(enable, is_game_specific);
+}
+
void setVblankFreq(u32 value, bool is_game_specific) {
vblankFrequency.set(value, is_game_specific);
}
@@ -658,6 +676,8 @@ void load(const std::filesystem::path& path, bool is_game_specific) {
vkHostMarkers.setFromToml(vk, "hostMarkers", is_game_specific);
vkGuestMarkers.setFromToml(vk, "guestMarkers", is_game_specific);
rdocEnable.setFromToml(vk, "rdocEnable", is_game_specific);
+ pipelineCacheEnable.setFromToml(vk, "pipelineCacheEnable", is_game_specific);
+ pipelineCacheArchive.setFromToml(vk, "pipelineCacheArchive", is_game_specific);
}
string current_version = {};
@@ -799,6 +819,8 @@ void save(const std::filesystem::path& path, bool is_game_specific) {
vkHostMarkers.setTomlValue(data, "Vulkan", "hostMarkers", is_game_specific);
vkGuestMarkers.setTomlValue(data, "Vulkan", "guestMarkers", is_game_specific);
rdocEnable.setTomlValue(data, "Vulkan", "rdocEnable", is_game_specific);
+ pipelineCacheEnable.setTomlValue(data, "Vulkan", "pipelineCacheEnable", is_game_specific);
+ pipelineCacheArchive.setTomlValue(data, "Vulkan", "pipelineCacheArchive", is_game_specific);
m_language.setTomlValue(data, "Settings", "consoleLanguage", is_game_specific);
@@ -903,6 +925,8 @@ void setDefaultValues(bool is_game_specific) {
vkHostMarkers.set(false, is_game_specific);
vkGuestMarkers.set(false, is_game_specific);
rdocEnable.set(false, is_game_specific);
+ pipelineCacheEnable.set(false, is_game_specific);
+ pipelineCacheArchive.set(false, is_game_specific);
// GS - Settings
m_language.setDefault(is_game_specific);
diff --git a/src/common/config.h b/src/common/config.h
index fa3cd5407..423708443 100644
--- a/src/common/config.h
+++ b/src/common/config.h
@@ -72,7 +72,11 @@ void setVkHostMarkersEnabled(bool enable, bool is_game_specific = false);
bool getVkGuestMarkersEnabled();
void setVkGuestMarkersEnabled(bool enable, bool is_game_specific = false);
bool isRdocEnabled();
+bool isPipelineCacheEnabled();
+bool isPipelineCacheArchived();
void setRdocEnabled(bool enable, bool is_game_specific = false);
+void setPipelineCacheEnabled(bool enable, bool is_game_specific = false);
+void setPipelineCacheArchived(bool enable, bool is_game_specific = false);
int getCursorHideTimeout();
std::string getMainOutputDevice();
void setMainOutputDevice(std::string device, bool is_game_specific = false);
diff --git a/src/common/path_util.cpp b/src/common/path_util.cpp
index 8ec1b38b0..c74aed0a5 100644
--- a/src/common/path_util.cpp
+++ b/src/common/path_util.cpp
@@ -128,6 +128,7 @@ static auto UserPaths = [] {
create_path(PathType::CustomTrophy, user_dir / CUSTOM_TROPHY);
create_path(PathType::CustomConfigs, user_dir / CUSTOM_CONFIGS);
create_path(PathType::HomeDir, user_dir / HOME_DIR);
+ create_path(PathType::CacheDir, user_dir / CACHE_DIR);
std::ofstream notice_file(user_dir / CUSTOM_TROPHY / "Notice.txt");
if (notice_file.is_open()) {
diff --git a/src/common/path_util.h b/src/common/path_util.h
index dff44866b..883ccd4e0 100644
--- a/src/common/path_util.h
+++ b/src/common/path_util.h
@@ -25,6 +25,7 @@ enum class PathType {
CustomTrophy, // Where custom files for trophies are stored.
CustomConfigs, // Where custom files for different games are stored.
HomeDir, // PS4 home directory
+ CacheDir, // Where pipeline and shader cache is stored.
};
constexpr auto PORTABLE_DIR = "user";
@@ -44,6 +45,7 @@ constexpr auto METADATA_DIR = "game_data";
constexpr auto CUSTOM_TROPHY = "custom_trophy";
constexpr auto CUSTOM_CONFIGS = "custom_configs";
constexpr auto HOME_DIR = "home";
+constexpr auto CACHE_DIR = "cache";
// Filenames
constexpr auto LOG_FILE = "shad_log.txt";
diff --git a/src/common/serdes.h b/src/common/serdes.h
new file mode 100644
index 000000000..a36fed4d3
--- /dev/null
+++ b/src/common/serdes.h
@@ -0,0 +1,140 @@
+// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "common/assert.h"
+#include "common/types.h"
+
+#include
+
+namespace Serialization {
+
+template
+concept Container = requires(T t) {
+ typename T::iterator;
+ { t.begin() } -> std::same_as;
+ { t.end() } -> std::same_as;
+ { t.size() } -> std::convertible_to;
+};
+
+struct Archive {
+ void Alloc(size_t size) {
+ container.resize(size);
+ }
+
+ void Grow(size_t size) {
+ container.resize(container.size() + size);
+ }
+
+ void Merge(const Archive& ar) {
+ container.insert(container.end(), ar.container.cbegin(), ar.container.cend());
+ offset = container.size();
+ }
+
+ [[nodiscard]] size_t SizeBytes() const {
+ return container.size();
+ }
+
+ u8* CurrPtr() {
+ return container.data() + offset;
+ }
+
+ void Advance(size_t size) {
+ ASSERT(offset + size <= container.size());
+ offset += size;
+ }
+
+ std::vector&& TakeOff() {
+ offset = 0;
+ return std::move(container);
+ }
+
+ [[nodiscard]] bool IsEoS() const {
+ return offset >= container.size();
+ }
+
+ Archive() = default;
+ explicit Archive(std::vector&& v) : container{v} {}
+
+private:
+ u32 offset{};
+ std::vector container{};
+
+ friend struct Writer;
+ friend struct Reader;
+};
+
+struct Writer {
+ template
+ void Write(const T* ptr, size_t size) {
+ if (ar.offset + size >= ar.container.size()) {
+ ar.Grow(size);
+ }
+ std::memcpy(ar.CurrPtr(), reinterpret_cast(ptr), size);
+ ar.Advance(size);
+ }
+
+ template
+ requires(!Container)
+ void Write(const T& value) {
+ const auto size = sizeof(value);
+ Write(&value, size);
+ }
+
+ void Write(const auto& v) {
+ Write(v.size());
+ for (const auto& elem : v) {
+ Write(elem);
+ }
+ }
+
+ void Write(const std::string& s) {
+ Write(s.size());
+ Write(s.c_str(), s.size());
+ }
+
+ Writer() = delete;
+ explicit Writer(Archive& ar_) : ar{ar_} {}
+
+ Archive& ar;
+};
+
+struct Reader {
+ template
+ void Read(T* ptr, size_t size) {
+ ASSERT(ar.offset + size <= ar.container.size());
+ std::memcpy(reinterpret_cast(ptr), ar.CurrPtr(), size);
+ ar.Advance(size);
+ }
+
+ template
+ requires(!Container)
+ void Read(T& value) {
+ const auto size = sizeof(value);
+ Read(&value, size);
+ }
+
+ void Read(auto& v) {
+ size_t num_elements{};
+ Read(num_elements);
+ for (int i = 0; i < num_elements; ++i) {
+ v.emplace_back();
+ Read(v.back());
+ }
+ }
+
+ void Read(std::string& s) {
+ size_t length{};
+ Read(length);
+ s.resize(length);
+ Read(s.data(), length);
+ }
+
+ Reader() = delete;
+ explicit Reader(Archive& ar_) : ar{ar_} {}
+
+ Archive& ar;
+};
+
+} // namespace Serialization
diff --git a/src/emulator.cpp b/src/emulator.cpp
index 21730cbec..31631bfee 100644
--- a/src/emulator.cpp
+++ b/src/emulator.cpp
@@ -43,6 +43,7 @@
#include "core/linker.h"
#include "core/memory.h"
#include "emulator.h"
+#include "video_core/cache_storage.h"
#include "video_core/renderdoc.h"
#ifdef _WIN32
@@ -389,6 +390,7 @@ void Emulator::Run(std::filesystem::path file, std::vector args,
}
UpdatePlayTime(id);
+ Storage::DataBase::Instance().Close();
std::quick_exit(0);
}
diff --git a/src/shader_recompiler/frontend/fetch_shader.cpp b/src/shader_recompiler/frontend/fetch_shader.cpp
index 35bea1c1b..ba0635546 100644
--- a/src/shader_recompiler/frontend/fetch_shader.cpp
+++ b/src/shader_recompiler/frontend/fetch_shader.cpp
@@ -51,7 +51,7 @@ std::optional ParseFetchShader(const Shader::Info& info) {
}
const auto* code = GetFetchShaderCode(info, info.fetch_shader_sgpr_base);
- FetchShaderData data{.code = code};
+ FetchShaderData data{};
GcnCodeSlice code_slice(code, code + std::numeric_limits::max());
GcnDecodeContext decoder;
diff --git a/src/shader_recompiler/frontend/fetch_shader.h b/src/shader_recompiler/frontend/fetch_shader.h
index 442a9af2f..a57bbb0c6 100644
--- a/src/shader_recompiler/frontend/fetch_shader.h
+++ b/src/shader_recompiler/frontend/fetch_shader.h
@@ -8,6 +8,10 @@
#include "common/types.h"
#include "shader_recompiler/info.h"
+namespace Serialization {
+struct Archive;
+}
+
namespace Shader::Gcn {
struct VertexAttribute {
@@ -50,7 +54,6 @@ struct VertexAttribute {
};
struct FetchShaderData {
- const u32* code;
u32 size = 0;
std::vector attributes;
s8 vertex_offset_sgpr = -1; ///< SGPR of vertex offset from VADDR
@@ -60,6 +63,9 @@ struct FetchShaderData {
return attributes == other.attributes && vertex_offset_sgpr == other.vertex_offset_sgpr &&
instance_offset_sgpr == other.instance_offset_sgpr;
}
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& buffer);
};
const u32* GetFetchShaderCode(const Info& info, u32 sgpr_base);
diff --git a/src/shader_recompiler/frontend/structured_control_flow.cpp b/src/shader_recompiler/frontend/structured_control_flow.cpp
index 963b2c0d5..1dc186c64 100644
--- a/src/shader_recompiler/frontend/structured_control_flow.cpp
+++ b/src/shader_recompiler/frontend/structured_control_flow.cpp
@@ -596,9 +596,8 @@ public:
IR::AbstractSyntaxList& syntax_list_, std::span inst_list_,
Info& info_, const RuntimeInfo& runtime_info_, const Profile& profile_)
: stmt_pool{stmt_pool_}, inst_pool{inst_pool_}, block_pool{block_pool_},
- syntax_list{syntax_list_}, inst_list{inst_list_}, info{info_},
- runtime_info{runtime_info_}, profile{profile_},
- translator{info_, runtime_info_, profile_} {
+ syntax_list{syntax_list_}, inst_list{inst_list_}, runtime_info{runtime_info_},
+ profile{profile_}, translator{info_, runtime_info_, profile_} {
Visit(root_stmt, nullptr, nullptr);
IR::Block* first_block = syntax_list.front().data.block;
@@ -782,7 +781,7 @@ private:
}
}
- IR::Block* MergeBlock(Statement& parent, Statement& stmt) {
+ IR::Block* MergeBlock(Statement& parent, Statement& stmt) const {
Statement* merge_stmt{TryFindForwardBlock(stmt)};
if (!merge_stmt) {
// Create a merge block we can visit later
@@ -798,7 +797,6 @@ private:
IR::AbstractSyntaxList& syntax_list;
const Block dummy_flow_block{.is_dummy = true};
std::span inst_list;
- Info& info;
const RuntimeInfo& runtime_info;
const Profile& profile;
Translator translator;
diff --git a/src/shader_recompiler/frontend/translate/translate.cpp b/src/shader_recompiler/frontend/translate/translate.cpp
index 57b50a3e1..3aa70e2ec 100644
--- a/src/shader_recompiler/frontend/translate/translate.cpp
+++ b/src/shader_recompiler/frontend/translate/translate.cpp
@@ -560,7 +560,8 @@ void Translator::EmitFetch(const GcnInst& inst) {
}
const auto filename = fmt::format("vs_{:#018x}.fetch.bin", info.pgm_hash);
const auto file = IOFile{dump_dir / filename, FileAccessMode::Create};
- file.WriteRaw(fetch_data->code, fetch_data->size);
+ const auto* code = GetFetchShaderCode(info, code_sgpr_base);
+ file.WriteRaw(code, fetch_data->size);
}
for (const auto& attrib : fetch_data->attributes) {
diff --git a/src/shader_recompiler/info.h b/src/shader_recompiler/info.h
index a3be34390..8d89537cb 100644
--- a/src/shader_recompiler/info.h
+++ b/src/shader_recompiler/info.h
@@ -19,6 +19,10 @@
#include "shader_recompiler/resource.h"
#include "shader_recompiler/runtime_info.h"
+namespace Serialization {
+struct Archive;
+}
+
namespace Shader {
enum class Qualifier : u8 {
@@ -34,7 +38,49 @@ enum class Qualifier : u8 {
/**
* Contains general information generated by the shader recompiler for an input program.
*/
-struct Info {
+struct InfoPersistent {
+ BufferResourceList buffers;
+ ImageResourceList images;
+ SamplerResourceList samplers;
+ FMaskResourceList fmasks;
+
+ struct UserDataMask {
+ void Set(IR::ScalarReg reg) noexcept {
+ mask |= 1 << static_cast(reg);
+ }
+
+ u32 Index(IR::ScalarReg reg) const noexcept {
+ const u32 reg_mask = (1 << static_cast(reg)) - 1;
+ return std::popcount(mask & reg_mask);
+ }
+
+ u32 NumRegs() const noexcept {
+ return std::popcount(mask);
+ }
+
+ u32 mask;
+ };
+ UserDataMask ud_mask{};
+ u32 fetch_shader_sgpr_base{};
+
+ u64 pgm_hash{};
+
+ s32 tess_consts_dword_offset = -1;
+ IR::ScalarReg tess_consts_ptr_base = IR::ScalarReg::Max;
+ Stage stage;
+ LogicalStage l_stage;
+
+ u8 mrt_mask{};
+ bool has_fetch_shader{};
+ bool has_bitwise_xor{};
+ bool uses_dma{};
+
+ InfoPersistent() = default;
+ InfoPersistent(Stage stage_, LogicalStage l_stage_, u64 pgm_hash_)
+ : stage{stage_}, l_stage{l_stage_}, pgm_hash{pgm_hash_} {}
+};
+
+struct Info : InfoPersistent {
struct AttributeFlags {
bool Get(IR::Attribute attrib, u32 comp = 0) const {
return flags[Index(attrib)] & (1 << comp);
@@ -58,56 +104,32 @@ struct Info {
std::array flags;
};
- AttributeFlags loads{};
- AttributeFlags stores{};
- struct UserDataMask {
- void Set(IR::ScalarReg reg) noexcept {
- mask |= 1 << static_cast(reg);
- }
-
- u32 Index(IR::ScalarReg reg) const noexcept {
- const u32 reg_mask = (1 << static_cast(reg)) - 1;
- return std::popcount(mask & reg_mask);
- }
-
- u32 NumRegs() const noexcept {
- return std::popcount(mask);
- }
-
- u32 mask;
+ enum class ReadConstType {
+ None = 0,
+ Immediate = 1 << 0,
+ Dynamic = 1 << 1,
};
- UserDataMask ud_mask{};
-
- CopyShaderData gs_copy_data;
- u32 uses_patches{};
-
- BufferResourceList buffers;
- ImageResourceList images;
- SamplerResourceList samplers;
- FMaskResourceList fmasks;
-
- PersistentSrtInfo srt_info;
- std::vector flattened_ud_buf;
struct Interpolation {
Qualifier primary;
Qualifier auxiliary;
};
- std::array fs_interpolation{};
-
- IR::ScalarReg tess_consts_ptr_base = IR::ScalarReg::Max;
- s32 tess_consts_dword_offset = -1;
std::span user_data;
- Stage stage;
- LogicalStage l_stage;
+ std::vector flattened_ud_buf;
+ PersistentSrtInfo srt_info;
+
+ AttributeFlags loads{};
+ AttributeFlags stores{};
+
+ ReadConstType readconst_types{};
+ CopyShaderData gs_copy_data;
+ u32 uses_patches{};
- u64 pgm_hash{};
VAddr pgm_base;
bool has_storage_images{};
bool has_discard{};
- bool has_bitwise_xor{};
bool has_image_gather{};
bool has_image_query{};
bool uses_buffer_atomic_float_min_max{};
@@ -125,20 +147,12 @@ struct Info {
bool stores_tess_level_outer{};
bool stores_tess_level_inner{};
bool translation_failed{};
- u8 mrt_mask{0u};
- bool has_fetch_shader{false};
- u32 fetch_shader_sgpr_base{0u};
- enum class ReadConstType {
- None = 0,
- Immediate = 1 << 0,
- Dynamic = 1 << 1,
- };
- ReadConstType readconst_types{};
- bool uses_dma{};
+ std::array fs_interpolation{};
- explicit Info(Stage stage_, LogicalStage l_stage_, ShaderParams params)
- : stage{stage_}, l_stage{l_stage_}, pgm_hash{params.hash}, pgm_base{params.Base()},
+ Info() = default;
+ Info(Stage stage_, LogicalStage l_stage_, ShaderParams params)
+ : InfoPersistent(stage_, l_stage_, params.hash), pgm_base{params.Base()},
user_data{params.user_data} {}
template
@@ -192,6 +206,9 @@ struct Info {
reinterpret_cast(tess_constants_addr),
sizeof(tess_constants));
}
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
};
DECLARE_ENUM_FLAG_OPERATORS(Info::ReadConstType);
diff --git a/src/shader_recompiler/ir/passes/flatten_extended_userdata_pass.cpp b/src/shader_recompiler/ir/passes/flatten_extended_userdata_pass.cpp
index 7626b9c9f..e1f9f2c5a 100644
--- a/src/shader_recompiler/ir/passes/flatten_extended_userdata_pass.cpp
+++ b/src/shader_recompiler/ir/passes/flatten_extended_userdata_pass.cpp
@@ -28,6 +28,17 @@ using namespace Xbyak::util;
static Xbyak::CodeGenerator g_srt_codegen(32_MB);
static const u8* g_srt_codegen_start = nullptr;
+namespace Shader {
+
+PFN_SrtWalker RegisterWalkerCode(const u8* ptr, size_t size) {
+ const auto func_addr = (PFN_SrtWalker)g_srt_codegen.getCurr();
+ g_srt_codegen.db(ptr, size);
+ g_srt_codegen.ready();
+ return func_addr;
+}
+
+} // namespace Shader
+
namespace {
static void DumpSrtProgram(const Shader::Info& info, const u8* code, size_t codesize) {
@@ -215,9 +226,12 @@ static void GenerateSrtProgram(Info& info, PassInfo& pass_info) {
c.ret();
c.ready();
+ info.srt_info.walker_func_size =
+ c.getCurr() - reinterpret_cast(info.srt_info.walker_func);
+
if (Config::dumpShaders()) {
- size_t codesize = c.getCurr() - reinterpret_cast(info.srt_info.walker_func);
- DumpSrtProgram(info, reinterpret_cast(info.srt_info.walker_func), codesize);
+ DumpSrtProgram(info, reinterpret_cast(info.srt_info.walker_func),
+ info.srt_info.walker_func_size);
}
info.srt_info.flattened_bufsize_dw = pass_info.dst_off_dw;
diff --git a/src/shader_recompiler/ir/passes/hull_shader_transform.cpp b/src/shader_recompiler/ir/passes/hull_shader_transform.cpp
index 2f8e1d7b1..48b496727 100644
--- a/src/shader_recompiler/ir/passes/hull_shader_transform.cpp
+++ b/src/shader_recompiler/ir/passes/hull_shader_transform.cpp
@@ -363,7 +363,7 @@ static IR::F32 ReadTessControlPointAttribute(IR::U32 addr, const u32 stride, IR:
} // namespace
-void HullShaderTransform(IR::Program& program, RuntimeInfo& runtime_info) {
+void HullShaderTransform(IR::Program& program, const RuntimeInfo& runtime_info) {
const Info& info = program.info;
for (IR::Block* block : program.blocks) {
@@ -561,8 +561,8 @@ void HullShaderTransform(IR::Program& program, RuntimeInfo& runtime_info) {
}
}
-void DomainShaderTransform(IR::Program& program, RuntimeInfo& runtime_info) {
- Info& info = program.info;
+void DomainShaderTransform(const IR::Program& program, const RuntimeInfo& runtime_info) {
+ const Info& info = program.info;
for (IR::Block* block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) {
diff --git a/src/shader_recompiler/ir/passes/ir_passes.h b/src/shader_recompiler/ir/passes/ir_passes.h
index fdae9d3cf..5bf362284 100644
--- a/src/shader_recompiler/ir/passes/ir_passes.h
+++ b/src/shader_recompiler/ir/passes/ir_passes.h
@@ -24,8 +24,8 @@ void LowerBufferFormatToRaw(IR::Program& program);
void LowerFp64ToFp32(IR::Program& program);
void RingAccessElimination(const IR::Program& program, const RuntimeInfo& runtime_info);
void TessellationPreprocess(IR::Program& program, RuntimeInfo& runtime_info);
-void HullShaderTransform(IR::Program& program, RuntimeInfo& runtime_info);
-void DomainShaderTransform(IR::Program& program, RuntimeInfo& runtime_info);
+void HullShaderTransform(IR::Program& program, const RuntimeInfo& runtime_info);
+void DomainShaderTransform(const IR::Program& program, const RuntimeInfo& runtime_info);
void SharedMemoryBarrierPass(IR::Program& program, const RuntimeInfo& runtime_info,
const Profile& profile);
void SharedMemorySimplifyPass(IR::Program& program, const Profile& profile);
diff --git a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp
index b9b4e9726..53b161149 100644
--- a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp
+++ b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp
@@ -498,7 +498,8 @@ void PatchBufferSharp(IR::Block& block, IR::Inst& inst, Info& info, Descriptors&
// buffer_load_format_xyz v[8:10], v1, s[32:35], 0 ...
// is used to define an inline buffer resource
std::array raw;
- raw[0] = info.pgm_base + (handle->Arg(0).U32() | u64(handle->Arg(1).U32()) << 32);
+ // Keep relative address, we'll do fixup of the address at buffer fetch later
+ raw[0] = (handle->Arg(0).U32() | u64(handle->Arg(1).U32()) << 32);
raw[1] = handle->Arg(2).U32() | u64(handle->Arg(3).U32()) << 32;
const auto buffer = std::bit_cast(raw);
buffer_binding = descriptors.Add(BufferResource{
diff --git a/src/shader_recompiler/ir/passes/srt.h b/src/shader_recompiler/ir/passes/srt.h
index 4dce38674..918b832e0 100644
--- a/src/shader_recompiler/ir/passes/srt.h
+++ b/src/shader_recompiler/ir/passes/srt.h
@@ -7,9 +7,14 @@
#include
#include "common/types.h"
+namespace Serialization {
+struct Archive;
+}
+
namespace Shader {
using PFN_SrtWalker = void PS4_SYSV_ABI (*)(const u32* /*user_data*/, u32* /*flat_dst*/);
+PFN_SrtWalker RegisterWalkerCode(const u8* ptr, size_t size);
struct PersistentSrtInfo {
// Special case when fetch shader uses step rates.
@@ -20,7 +25,11 @@ struct PersistentSrtInfo {
};
PFN_SrtWalker walker_func{};
+ size_t walker_func_size{};
u32 flattened_bufsize_dw = 16; // NumUserDataRegs
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
};
} // namespace Shader
diff --git a/src/shader_recompiler/profile.h b/src/shader_recompiler/profile.h
index c51e00088..52e37bbf0 100644
--- a/src/shader_recompiler/profile.h
+++ b/src/shader_recompiler/profile.h
@@ -8,6 +8,10 @@
namespace Shader {
struct Profile {
+ u64 max_ubo_size{};
+ u32 max_viewport_width{};
+ u32 max_viewport_height{};
+ u32 max_shared_memory_size{};
u32 supported_spirv{0x00010000};
u32 subgroup_size{};
bool support_int8{};
@@ -37,10 +41,7 @@ struct Profile {
bool needs_lds_barriers{};
bool needs_buffer_offsets{};
bool needs_unorm_fixup{};
- u64 max_ubo_size{};
- u32 max_viewport_width{};
- u32 max_viewport_height{};
- u32 max_shared_memory_size{};
+ bool _pad0{};
};
} // namespace Shader
diff --git a/src/shader_recompiler/recompiler.cpp b/src/shader_recompiler/recompiler.cpp
index 547d4524f..4764ddbec 100644
--- a/src/shader_recompiler/recompiler.cpp
+++ b/src/shader_recompiler/recompiler.cpp
@@ -29,7 +29,7 @@ IR::BlockList GenerateBlocks(const IR::AbstractSyntaxList& syntax_list) {
return blocks;
}
-IR::Program TranslateProgram(std::span code, Pools& pools, Info& info,
+IR::Program TranslateProgram(const std::span& code, Pools& pools, Info& info,
RuntimeInfo& runtime_info, const Profile& profile) {
// Ensure first instruction is expected.
constexpr u32 token_mov_vcchi = 0xBEEB03FF;
@@ -55,8 +55,8 @@ IR::Program TranslateProgram(std::span code, Pools& pools, Info& info
Gcn::CFG cfg{gcn_block_pool, program.ins_list};
// Structurize control flow graph and create program.
- program.syntax_list = Shader::Gcn::BuildASL(pools.inst_pool, pools.block_pool, cfg,
- program.info, runtime_info, profile);
+ program.syntax_list =
+ Shader::Gcn::BuildASL(pools.inst_pool, pools.block_pool, cfg, info, runtime_info, profile);
program.blocks = GenerateBlocks(program.syntax_list);
program.post_order_blocks = Shader::IR::PostOrder(program.syntax_list.front());
diff --git a/src/shader_recompiler/recompiler.h b/src/shader_recompiler/recompiler.h
index 8180c29b3..80c63447a 100644
--- a/src/shader_recompiler/recompiler.h
+++ b/src/shader_recompiler/recompiler.h
@@ -27,7 +27,8 @@ struct Pools {
}
};
-[[nodiscard]] IR::Program TranslateProgram(std::span code, Pools& pools, Info& info,
- RuntimeInfo& runtime_info, const Profile& profile);
+[[nodiscard]] IR::Program TranslateProgram(const std::span& code, Pools& pools,
+ Info& info, RuntimeInfo& runtime_info,
+ const Profile& profile);
} // namespace Shader
diff --git a/src/shader_recompiler/resource.h b/src/shader_recompiler/resource.h
index 29545d0bb..5d9965105 100644
--- a/src/shader_recompiler/resource.h
+++ b/src/shader_recompiler/resource.h
@@ -53,8 +53,15 @@ struct BufferResource {
}
constexpr AmdGpu::Buffer GetSharp(const auto& info) const noexcept {
- const auto buffer =
- inline_cbuf ? inline_cbuf : info.template ReadUdSharp(sharp_idx);
+ AmdGpu::Buffer buffer{};
+ if (inline_cbuf) {
+ buffer = inline_cbuf;
+ if (inline_cbuf.base_address > 1) {
+ buffer.base_address += info.pgm_base; // address fixup
+ }
+ } else {
+ buffer = info.template ReadUdSharp(sharp_idx);
+ }
if (!buffer.Valid()) {
LOG_DEBUG(Render, "Encountered invalid buffer sharp");
return AmdGpu::Buffer::Null();
diff --git a/src/shader_recompiler/runtime_info.h b/src/shader_recompiler/runtime_info.h
index 9624c465f..8620ab970 100644
--- a/src/shader_recompiler/runtime_info.h
+++ b/src/shader_recompiler/runtime_info.h
@@ -159,7 +159,8 @@ struct GeometryRuntimeInfo {
return num_outputs == other.num_outputs && outputs == other.outputs && num_invocations &&
other.num_invocations && output_vertices == other.output_vertices &&
in_primitive == other.in_primitive &&
- std::ranges::equal(out_primitive, other.out_primitive);
+ std::ranges::equal(out_primitive, other.out_primitive) &&
+ vs_copy_hash == other.vs_copy_hash;
}
};
diff --git a/src/shader_recompiler/specialization.h b/src/shader_recompiler/specialization.h
index a7215e29e..4f6bb44bf 100644
--- a/src/shader_recompiler/specialization.h
+++ b/src/shader_recompiler/specialization.h
@@ -79,8 +79,8 @@ struct SamplerSpecialization {
struct StageSpecialization {
static constexpr size_t MaxStageResources = 128;
- const Shader::Info* info;
- RuntimeInfo runtime_info;
+ const Info* info{};
+ RuntimeInfo runtime_info{};
std::bitset bitset{};
std::optional fetch_shader_data{};
boost::container::small_vector vs_attribs;
@@ -90,6 +90,7 @@ struct StageSpecialization {
boost::container::small_vector samplers;
Backend::Bindings start{};
+ StageSpecialization() = default;
StageSpecialization(const Info& info_, RuntimeInfo runtime_info_, const Profile& profile_,
Backend::Bindings start_)
: info{&info_}, runtime_info{runtime_info_}, start{start_} {
@@ -158,7 +159,7 @@ struct StageSpecialization {
// Initialize runtime_info fields that rely on analysis in tessellation passes
if (info->l_stage == LogicalStage::TessellationControl ||
info->l_stage == LogicalStage::TessellationEval) {
- Shader::TessellationDataConstantBuffer tess_constants;
+ TessellationDataConstantBuffer tess_constants{};
info->ReadTessConstantBuffer(tess_constants);
if (info->l_stage == LogicalStage::TessellationControl) {
runtime_info.hs_info.InitFromTessConstants(tess_constants);
@@ -192,21 +193,43 @@ struct StageSpecialization {
}
}
+ [[nodiscard]] bool Valid() const {
+ return info != nullptr;
+ }
+
bool operator==(const StageSpecialization& other) const {
- if (start != other.start) {
+ if (!Valid()) {
return false;
}
+
+ if (vs_attribs != other.vs_attribs) {
+ return false;
+ }
+
if (runtime_info != other.runtime_info) {
return false;
}
+
if (fetch_shader_data != other.fetch_shader_data) {
return false;
}
- for (u32 i = 0; i < vs_attribs.size(); i++) {
- if (vs_attribs[i] != other.vs_attribs[i]) {
- return false;
- }
+
+ if (fmasks != other.fmasks) {
+ return false;
}
+
+ // For VS which only generates geometry and doesn't have any inputs, its start
+ // bindings still may change as they depend on previously processed FS. The check below
+ // handles this case and prevents generation of redundant permutations. This is also safe
+ // for other types of shaders with no bindings.
+ if (bitset.none() && other.bitset.none()) {
+ return true;
+ }
+
+ if (start != other.start) {
+ return false;
+ }
+
u32 binding{};
for (u32 i = 0; i < buffers.size(); i++) {
if (other.bitset[binding++] && buffers[i] != other.buffers[i]) {
@@ -218,11 +241,7 @@ struct StageSpecialization {
return false;
}
}
- for (u32 i = 0; i < fmasks.size(); i++) {
- if (other.bitset[binding++] && fmasks[i] != other.fmasks[i]) {
- return false;
- }
- }
+
for (u32 i = 0; i < samplers.size(); i++) {
if (samplers[i] != other.samplers[i]) {
return false;
@@ -230,6 +249,9 @@ struct StageSpecialization {
}
return true;
}
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
};
} // namespace Shader
diff --git a/src/video_core/amdgpu/pixel_format.h b/src/video_core/amdgpu/pixel_format.h
index 21c2eee2a..69e082edb 100644
--- a/src/video_core/amdgpu/pixel_format.h
+++ b/src/video_core/amdgpu/pixel_format.h
@@ -79,10 +79,10 @@ enum class NumberFormat : u32 {
Ubscaled = 13,
};
-enum class NumberClass {
- Float,
- Sint,
- Uint,
+enum class NumberClass : u8 {
+ Float = 0,
+ Sint = 1,
+ Uint = 2,
};
enum class CompSwizzle : u8 {
diff --git a/src/video_core/cache_storage.cpp b/src/video_core/cache_storage.cpp
new file mode 100644
index 000000000..1c46a4cf5
--- /dev/null
+++ b/src/video_core/cache_storage.cpp
@@ -0,0 +1,264 @@
+// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "common/config.h"
+#include "common/elf_info.h"
+#include "common/io_file.h"
+#include "common/polyfill_thread.h"
+#include "common/thread.h"
+
+#include "video_core/cache_storage.h"
+#include "video_core/renderer_vulkan/vk_instance.h"
+#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
+
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+namespace {
+
+std::mutex submit_mutex{};
+u32 num_requests{};
+std::condition_variable_any request_cv{};
+std::queue> req_queue{};
+std::mutex m_request{};
+
+mz_zip_archive zip_ar{};
+bool ar_is_read_only{true};
+
+} // namespace
+
+namespace Storage {
+
+void ProcessIO(const std::stop_token& stoken) {
+ Common::SetCurrentThreadName("shadPS4:PipelineCacheIO");
+
+ while (!stoken.stop_requested()) {
+ {
+ std::unique_lock lk{submit_mutex};
+ Common::CondvarWait(request_cv, lk, stoken, [&] { return num_requests; });
+ }
+
+ if (stoken.stop_requested()) {
+ break;
+ }
+
+ while (num_requests) {
+ std::packaged_task request{};
+ {
+ std::scoped_lock lock{m_request};
+ if (req_queue.empty()) {
+ continue;
+ }
+ request = std::move(req_queue.front());
+ req_queue.pop();
+ }
+
+ if (request.valid()) {
+ request();
+ request.get_future().wait();
+ }
+
+ --num_requests;
+ }
+ }
+}
+
+constexpr std::string GetBlobFileExtension(BlobType type) {
+ switch (type) {
+ case BlobType::ShaderMeta: {
+ return "meta";
+ }
+ case BlobType::ShaderBinary: {
+ return "spv";
+ }
+ case BlobType::PipelineKey: {
+ return "key";
+ }
+ case BlobType::ShaderProfile: {
+ return "bin";
+ }
+ default:
+ UNREACHABLE();
+ }
+}
+
+void DataBase::Open() {
+ if (opened) {
+ return;
+ }
+
+ const auto& game_info = Common::ElfInfo::Instance();
+
+ using namespace Common::FS;
+ if (Config::isPipelineCacheArchived()) {
+ mz_zip_zero_struct(&zip_ar);
+
+ cache_path = GetUserPath(PathType::CacheDir) /
+ std::filesystem::path{game_info.GameSerial()}.replace_extension(".zip");
+
+ if (!mz_zip_reader_init_file(&zip_ar, cache_path.string().c_str(),
+ MZ_ZIP_FLAG_READ_ALLOW_WRITING) ||
+ !mz_zip_validate_archive(&zip_ar, 0)) {
+ LOG_INFO(Render, "Cache archive {} is not found or archive is corrupted",
+ cache_path.string().c_str());
+ mz_zip_reader_end(&zip_ar);
+ mz_zip_writer_init_file(&zip_ar, cache_path.string().c_str(), 0);
+ }
+ } else {
+ cache_path = GetUserPath(PathType::CacheDir) / game_info.GameSerial();
+ if (!std::filesystem::exists(cache_path)) {
+ std::filesystem::create_directories(cache_path);
+ }
+ }
+
+ io_worker = std::jthread{ProcessIO};
+ opened = true;
+}
+
+void DataBase::Close() {
+ if (!IsOpened()) {
+ return;
+ }
+
+ io_worker.request_stop();
+ io_worker.join();
+
+ if (Config::isPipelineCacheArchived()) {
+ mz_zip_writer_finalize_archive(&zip_ar);
+ mz_zip_writer_end(&zip_ar);
+ }
+
+ LOG_INFO(Render, "Cache dumped");
+}
+
+template
+bool WriteVector(const BlobType type, std::filesystem::path&& path_, std::vector&& v) {
+ {
+ auto request = std::packaged_task{[=]() {
+ auto path{path_};
+ path.replace_extension(GetBlobFileExtension(type));
+ if (Config::isPipelineCacheArchived()) {
+ ASSERT_MSG(!ar_is_read_only,
+ "The archive is read-only. Did you forget to call `FinishPreload`?");
+ if (!mz_zip_writer_add_mem(&zip_ar, path.string().c_str(), v.data(),
+ v.size() * sizeof(T), MZ_BEST_COMPRESSION)) {
+ LOG_ERROR(Render, "Failed to add {} to the archive", path.string().c_str());
+ }
+ } else {
+ using namespace Common::FS;
+ const auto file = IOFile{path, FileAccessMode::Create};
+ file.Write(v);
+ }
+ }};
+ std::scoped_lock lock{m_request};
+ req_queue.emplace(std::move(request));
+ }
+
+ std::scoped_lock lk{submit_mutex};
+ ++num_requests;
+ request_cv.notify_one();
+ return true;
+}
+
+template
+void LoadVector(BlobType type, std::filesystem::path& path, std::vector& v) {
+ using namespace Common::FS;
+ path.replace_extension(GetBlobFileExtension(type));
+ if (Config::isPipelineCacheArchived()) {
+ int index{-1};
+ index = mz_zip_reader_locate_file(&zip_ar, path.string().c_str(), nullptr, 0);
+ if (index < 0) {
+ LOG_WARNING(Render, "File {} is not found in the archive", path.string().c_str());
+ return;
+ }
+ mz_zip_archive_file_stat stat{};
+ mz_zip_reader_file_stat(&zip_ar, index, &stat);
+ v.resize(stat.m_uncomp_size / sizeof(T));
+ mz_zip_reader_extract_to_mem(&zip_ar, index, v.data(), stat.m_uncomp_size, 0);
+ } else {
+ const auto file = IOFile{path, FileAccessMode::Read};
+ v.resize(file.GetSize() / sizeof(T));
+ file.Read(v);
+ }
+}
+
+bool DataBase::Save(BlobType type, const std::string& name, std::vector&& data) {
+ if (!opened) {
+ return false;
+ }
+
+ auto path = Config::isPipelineCacheArchived() ? std::filesystem::path{name} : cache_path / name;
+ return WriteVector(type, std::move(path), std::move(data));
+}
+
+bool DataBase::Save(BlobType type, const std::string& name, std::vector&& data) {
+ if (!opened) {
+ return false;
+ }
+
+ auto path = Config::isPipelineCacheArchived() ? std::filesystem::path{name} : cache_path / name;
+ return WriteVector(type, std::move(path), std::move(data));
+}
+
+void DataBase::Load(BlobType type, const std::string& name, std::vector& data) {
+ if (!opened) {
+ return;
+ }
+
+ auto path = Config::isPipelineCacheArchived() ? std::filesystem::path{name} : cache_path / name;
+ return LoadVector(type, path, data);
+}
+
+void DataBase::Load(BlobType type, const std::string& name, std::vector& data) {
+ if (!opened) {
+ return;
+ }
+
+ auto path = Config::isPipelineCacheArchived() ? std::filesystem::path{name} : cache_path / name;
+ return LoadVector(type, path, data);
+}
+
+void DataBase::ForEachBlob(BlobType type, const std::function&& data)>& func) {
+ const auto& ext = GetBlobFileExtension(type);
+ if (Config::isPipelineCacheArchived()) {
+ const auto num_files = mz_zip_reader_get_num_files(&zip_ar);
+ for (int index = 0; index < num_files; ++index) {
+ std::array file_name{};
+ file_name.fill(0);
+ mz_zip_reader_get_filename(&zip_ar, index, file_name.data(), file_name.size());
+ if (std::string{file_name.data()}.ends_with(ext)) {
+ mz_zip_archive_file_stat stat{};
+ mz_zip_reader_file_stat(&zip_ar, index, &stat);
+ std::vector data(stat.m_uncomp_size);
+ mz_zip_reader_extract_to_mem(&zip_ar, index, data.data(), data.size(), 0);
+ func(std::move(data));
+ }
+ }
+ } else {
+ for (const auto& file_name : std::filesystem::directory_iterator{cache_path}) {
+ if (file_name.path().extension().string().ends_with(ext)) {
+ using namespace Common::FS;
+ const auto& file = IOFile{file_name, FileAccessMode::Read};
+ if (file.IsOpen()) {
+ std::vector data(file.GetSize());
+ file.Read(data);
+ func(std::move(data));
+ }
+ }
+ }
+ }
+}
+
+void DataBase::FinishPreload() {
+ if (Config::isPipelineCacheArchived()) {
+ mz_zip_writer_init_from_reader(&zip_ar, cache_path.string().c_str());
+ ar_is_read_only = false;
+ }
+}
+
+} // namespace Storage
diff --git a/src/video_core/cache_storage.h b/src/video_core/cache_storage.h
new file mode 100644
index 000000000..91f2136e9
--- /dev/null
+++ b/src/video_core/cache_storage.h
@@ -0,0 +1,50 @@
+// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "common/path_util.h"
+#include "common/singleton.h"
+#include "common/types.h"
+
+#include
+#include
+#include
+
+namespace Storage {
+
+enum class BlobType : u32 {
+ ShaderMeta,
+ ShaderBinary,
+ PipelineKey,
+ ShaderProfile,
+};
+
+class DataBase {
+public:
+ static DataBase& Instance() {
+ return *Common::Singleton::Instance();
+ }
+
+ void Open();
+ void Close();
+ [[nodiscard]] bool IsOpened() const {
+ return opened;
+ }
+ void FinishPreload();
+
+ bool Save(BlobType type, const std::string& name, std::vector&& data);
+ bool Save(BlobType type, const std::string& name, std::vector&& data);
+
+ void Load(BlobType type, const std::string& name, std::vector& data);
+ void Load(BlobType type, const std::string& name, std::vector& data);
+
+ void ForEachBlob(BlobType type, const std::function&& data)>& func);
+
+private:
+ std::jthread io_worker{};
+ std::filesystem::path cache_path{};
+ bool opened{};
+};
+
+} // namespace Storage
diff --git a/src/video_core/renderer_vulkan/vk_compute_pipeline.cpp b/src/video_core/renderer_vulkan/vk_compute_pipeline.cpp
index 2b93eb7f3..35eda86da 100644
--- a/src/video_core/renderer_vulkan/vk_compute_pipeline.cpp
+++ b/src/video_core/renderer_vulkan/vk_compute_pipeline.cpp
@@ -13,7 +13,8 @@ namespace Vulkan {
ComputePipeline::ComputePipeline(const Instance& instance, Scheduler& scheduler,
DescriptorHeap& desc_heap, const Shader::Profile& profile,
vk::PipelineCache pipeline_cache, ComputePipelineKey compute_key_,
- const Shader::Info& info_, vk::ShaderModule module)
+ const Shader::Info& info_, vk::ShaderModule module,
+ SerializationSupport& sdata, bool preloading /*=false*/)
: Pipeline{instance, scheduler, desc_heap, profile, pipeline_cache, true},
compute_key{compute_key_} {
auto& info = stages[int(Shader::LogicalStage::Compute)];
@@ -29,7 +30,11 @@ ComputePipeline::ComputePipeline(const Instance& instance, Scheduler& scheduler,
u32 binding{};
boost::container::small_vector bindings;
for (const auto& buffer : info->buffers) {
- const auto sharp = buffer.GetSharp(*info);
+ // During deserialization, we don't have access to the UD to fetch sharp data. To address
+ // this properly we need to track shaprs or portion of them in `sdata`, but since we're
+ // interested only in "is storage" flag (which is not even effective atm), we can take a
+ // shortcut there.
+ const auto sharp = preloading ? AmdGpu::Buffer{} : buffer.GetSharp(*info);
bindings.push_back({
.binding = binding++,
.descriptorType = buffer.IsStorage(sharp) ? vk::DescriptorType::eStorageBuffer
diff --git a/src/video_core/renderer_vulkan/vk_compute_pipeline.h b/src/video_core/renderer_vulkan/vk_compute_pipeline.h
index 79059b509..1cac7204c 100644
--- a/src/video_core/renderer_vulkan/vk_compute_pipeline.h
+++ b/src/video_core/renderer_vulkan/vk_compute_pipeline.h
@@ -11,6 +11,10 @@ class BufferCache;
class TextureCache;
} // namespace VideoCore
+namespace Serialization {
+struct Archive;
+}
+
namespace Vulkan {
class Instance;
@@ -26,14 +30,24 @@ struct ComputePipelineKey {
friend bool operator!=(const ComputePipelineKey& lhs, const ComputePipelineKey& rhs) {
return !(lhs == rhs);
}
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
};
class ComputePipeline : public Pipeline {
public:
+ struct SerializationSupport {
+ u32 dummy{};
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
+ };
+
ComputePipeline(const Instance& instance, Scheduler& scheduler, DescriptorHeap& desc_heap,
const Shader::Profile& profile, vk::PipelineCache pipeline_cache,
ComputePipelineKey compute_key, const Shader::Info& info,
- vk::ShaderModule module);
+ vk::ShaderModule module, SerializationSupport& sdata, bool preloading);
~ComputePipeline();
private:
diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
index e2531456c..242c9b6f2 100644
--- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
+++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
@@ -41,12 +41,12 @@ GraphicsPipeline::GraphicsPipeline(
vk::PipelineCache pipeline_cache, std::span infos,
std::span runtime_infos,
std::optional fetch_shader_,
- std::span modules)
+ std::span modules, SerializationSupport& sdata, bool preloading)
: Pipeline{instance, scheduler, desc_heap, profile, pipeline_cache}, key{key_},
fetch_shader{std::move(fetch_shader_)} {
const vk::Device device = instance.GetDevice();
std::ranges::copy(infos, stages.begin());
- BuildDescSetLayout();
+ BuildDescSetLayout(preloading);
const auto debug_str = GetDebugString();
const vk::PushConstantRange push_constants = {
@@ -68,27 +68,26 @@ GraphicsPipeline::GraphicsPipeline(
pipeline_layout = std::move(layout);
SetObjectName(device, *pipeline_layout, "Graphics PipelineLayout {}", debug_str);
- VertexInputs vertex_attributes;
- VertexInputs vertex_bindings;
- VertexInputs divisors;
- VertexInputs guest_buffers;
- if (!instance.IsVertexInputDynamicState()) {
- const auto& vs_info = runtime_infos[u32(Shader::LogicalStage::Vertex)].vs_info;
- GetVertexInputs(vertex_attributes, vertex_bindings, divisors, guest_buffers,
- vs_info.step_rate_0, vs_info.step_rate_1);
+ if (!preloading) {
+ VertexInputs guest_buffers;
+ if (!instance.IsVertexInputDynamicState()) {
+ const auto& vs_info = runtime_infos[u32(Shader::LogicalStage::Vertex)].vs_info;
+ GetVertexInputs(sdata.vertex_attributes, sdata.vertex_bindings, sdata.divisors,
+ guest_buffers, vs_info.step_rate_0, vs_info.step_rate_1);
+ }
}
const vk::PipelineVertexInputDivisorStateCreateInfo divisor_state = {
- .vertexBindingDivisorCount = static_cast(divisors.size()),
- .pVertexBindingDivisors = divisors.data(),
+ .vertexBindingDivisorCount = static_cast(sdata.divisors.size()),
+ .pVertexBindingDivisors = sdata.divisors.data(),
};
const vk::PipelineVertexInputStateCreateInfo vertex_input_info = {
- .pNext = divisors.empty() ? nullptr : &divisor_state,
- .vertexBindingDescriptionCount = static_cast(vertex_bindings.size()),
- .pVertexBindingDescriptions = vertex_bindings.data(),
- .vertexAttributeDescriptionCount = static_cast(vertex_attributes.size()),
- .pVertexAttributeDescriptions = vertex_attributes.data(),
+ .pNext = sdata.divisors.empty() ? nullptr : &divisor_state,
+ .vertexBindingDescriptionCount = static_cast(sdata.vertex_bindings.size()),
+ .pVertexBindingDescriptions = sdata.vertex_bindings.data(),
+ .vertexAttributeDescriptionCount = static_cast(sdata.vertex_attributes.size()),
+ .pVertexAttributeDescriptions = sdata.vertex_attributes.data(),
};
const auto topology = LiverpoolToVK::PrimitiveType(key.prim_type);
@@ -98,7 +97,6 @@ GraphicsPipeline::GraphicsPipeline(
const bool is_rect_list = key.prim_type == AmdGpu::PrimitiveType::RectList;
const bool is_quad_list = key.prim_type == AmdGpu::PrimitiveType::QuadList;
- const auto& fs_info = runtime_infos[u32(Shader::LogicalStage::Fragment)].fs_info;
const vk::PipelineTessellationStateCreateInfo tessellation_state = {
.patchControlPoints = is_rect_list ? 3U : (is_quad_list ? 4U : key.patch_control_points),
};
@@ -128,12 +126,15 @@ GraphicsPipeline::GraphicsPipeline(
raster_chain.unlink();
}
- const vk::PipelineMultisampleStateCreateInfo multisampling = {
- .rasterizationSamples = LiverpoolToVK::NumSamples(
- key.num_samples, instance.GetColorSampleCounts() & instance.GetDepthSampleCounts()),
- .sampleShadingEnable =
- fs_info.addr_flags.persp_sample_ena || fs_info.addr_flags.linear_sample_ena,
- };
+ if (!preloading) {
+ const auto& fs_info = runtime_infos[u32(Shader::LogicalStage::Fragment)].fs_info;
+ sdata.multisampling = {
+ .rasterizationSamples = LiverpoolToVK::NumSamples(
+ key.num_samples, instance.GetColorSampleCounts() & instance.GetDepthSampleCounts()),
+ .sampleShadingEnable =
+ fs_info.addr_flags.persp_sample_ena || fs_info.addr_flags.linear_sample_ena,
+ };
+ }
const vk::PipelineViewportDepthClipControlCreateInfoEXT clip_control = {
.negativeOneToOne = key.clip_space == AmdGpu::ClipSpace::MinusWToW,
@@ -164,7 +165,7 @@ GraphicsPipeline::GraphicsPipeline(
}
if (instance.IsVertexInputDynamicState()) {
dynamic_states.push_back(vk::DynamicState::eVertexInputEXT);
- } else if (!vertex_bindings.empty()) {
+ } else if (!sdata.vertex_bindings.empty()) {
dynamic_states.push_back(vk::DynamicState::eVertexInputBindingStride);
}
@@ -200,10 +201,13 @@ GraphicsPipeline::GraphicsPipeline(
});
} else if (is_rect_list || is_quad_list) {
const auto type = is_quad_list ? AuxShaderType::QuadListTCS : AuxShaderType::RectListTCS;
- auto tcs = Shader::Backend::SPIRV::EmitAuxilaryTessShader(type, fs_info);
+ if (!preloading) {
+ const auto& fs_info = runtime_infos[u32(Shader::LogicalStage::Fragment)].fs_info;
+ sdata.tcs = Shader::Backend::SPIRV::EmitAuxilaryTessShader(type, fs_info);
+ }
shader_stages.emplace_back(vk::PipelineShaderStageCreateInfo{
.stage = vk::ShaderStageFlagBits::eTessellationControl,
- .module = CompileSPV(tcs, instance.GetDevice()),
+ .module = CompileSPV(sdata.tcs, instance.GetDevice()),
.pName = "main",
});
}
@@ -215,11 +219,14 @@ GraphicsPipeline::GraphicsPipeline(
.pName = "main",
});
} else if (is_rect_list || is_quad_list) {
- auto tes =
- Shader::Backend::SPIRV::EmitAuxilaryTessShader(AuxShaderType::PassthroughTES, fs_info);
+ if (!preloading) {
+ const auto& fs_info = runtime_infos[u32(Shader::LogicalStage::Fragment)].fs_info;
+ sdata.tes = Shader::Backend::SPIRV::EmitAuxilaryTessShader(
+ AuxShaderType::PassthroughTES, fs_info);
+ }
shader_stages.emplace_back(vk::PipelineShaderStageCreateInfo{
.stage = vk::ShaderStageFlagBits::eTessellationEvaluation,
- .module = CompileSPV(tes, instance.GetDevice()),
+ .module = CompileSPV(sdata.tes, instance.GetDevice()),
.pName = "main",
});
}
@@ -360,7 +367,7 @@ GraphicsPipeline::GraphicsPipeline(
.pTessellationState = &tessellation_state,
.pViewportState = &viewport_info,
.pRasterizationState = &raster_chain.get(),
- .pMultisampleState = &multisampling,
+ .pMultisampleState = &sdata.multisampling,
.pColorBlendState = &color_blending,
.pDynamicState = &dynamic_info,
.layout = *pipeline_layout,
@@ -428,7 +435,7 @@ template void GraphicsPipeline::GetVertexInputs(
VertexInputs& divisors,
VertexInputs& guest_buffers, u32 step_rate_0, u32 step_rate_1) const;
-void GraphicsPipeline::BuildDescSetLayout() {
+void GraphicsPipeline::BuildDescSetLayout(bool preloading) {
boost::container::small_vector bindings;
u32 binding{};
@@ -438,7 +445,9 @@ void GraphicsPipeline::BuildDescSetLayout() {
}
const auto stage_bit = LogicalStageToStageBit[u32(stage->l_stage)];
for (const auto& buffer : stage->buffers) {
- const auto sharp = buffer.GetSharp(*stage);
+ const auto sharp =
+ preloading ? AmdGpu::Buffer{}
+ : buffer.GetSharp(*stage); // See for the comment in compute PL creation
bindings.push_back({
.binding = binding++,
.descriptorType = buffer.IsStorage(sharp) ? vk::DescriptorType::eStorageBuffer
diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
index 8254605cb..0dea92864 100644
--- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
+++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h
@@ -63,17 +63,33 @@ struct GraphicsPipelineKey {
bool operator==(const GraphicsPipelineKey& key) const noexcept {
return std::memcmp(this, &key, sizeof(key)) == 0;
}
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
};
class GraphicsPipeline : public Pipeline {
public:
+ struct SerializationSupport {
+ VertexInputs vertex_attributes{};
+ VertexInputs vertex_bindings{};
+ VertexInputs divisors{};
+ vk::PipelineMultisampleStateCreateInfo multisampling{};
+ std::vector tcs{};
+ std::vector tes{};
+
+ void Serialize(Serialization::Archive& ar) const;
+ bool Deserialize(Serialization::Archive& ar);
+ };
+
GraphicsPipeline(const Instance& instance, Scheduler& scheduler, DescriptorHeap& desc_heap,
const Shader::Profile& profile, const GraphicsPipelineKey& key,
vk::PipelineCache pipeline_cache,
std::span stages,
std::span runtime_infos,
std::optional fetch_shader,
- std::span modules);
+ std::span modules, SerializationSupport& sdata,
+ bool preloading);
~GraphicsPipeline();
const std::optional& GetFetchShader() const noexcept {
@@ -92,7 +108,7 @@ public:
u32 step_rate_1) const;
private:
- void BuildDescSetLayout();
+ void BuildDescSetLayout(bool preloading);
private:
GraphicsPipelineKey key;
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
index 69b06479c..8e16885b8 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
@@ -14,9 +14,10 @@
#include "shader_recompiler/recompiler.h"
#include "shader_recompiler/runtime_info.h"
#include "video_core/amdgpu/liverpool.h"
+#include "video_core/cache_storage.h"
#include "video_core/renderer_vulkan/liverpool_to_vk.h"
#include "video_core/renderer_vulkan/vk_instance.h"
-#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
+#include "video_core/renderer_vulkan/vk_pipeline_serialization.h"
#include "video_core/renderer_vulkan/vk_scheduler.h"
#include "video_core/renderer_vulkan/vk_shader_util.h"
@@ -224,6 +225,13 @@ PipelineCache::PipelineCache(const Instance& instance_, Scheduler& scheduler_,
desc_heap{instance, scheduler.GetMasterSemaphore(), DescriptorHeapSizes} {
const auto& vk12_props = instance.GetVk12Properties();
profile = Shader::Profile{
+ // When binding a UBO, we calculate its size considering the offset in the larger buffer
+ // cache underlying resource. In some cases, it may produce sizes exceeding the system
+ // maximum allowed UBO range, so we need to reduce the threshold to prevent issues.
+ .max_ubo_size = instance.UniformMaxSize() - instance.UniformMinAlignment(),
+ .max_viewport_width = instance.GetMaxViewportWidth(),
+ .max_viewport_height = instance.GetMaxViewportHeight(),
+ .max_shared_memory_size = instance.MaxComputeSharedMemorySize(),
.supported_spirv = SpirvVersion1_6,
.subgroup_size = instance.SubgroupSize(),
.support_int8 = instance.IsShaderInt8Supported(),
@@ -259,14 +267,10 @@ PipelineCache::PipelineCache(const Instance& instance_, Scheduler& scheduler_,
instance.GetDriverID() == vk::DriverId::eMoltenvk,
.needs_buffer_offsets = instance.StorageMinAlignment() > 4,
.needs_unorm_fixup = instance.GetDriverID() == vk::DriverId::eMoltenvk,
- // When binding a UBO, we calculate its size considering the offset in the larger buffer
- // cache underlying resource. In some cases, it may produce sizes exceeding the system
- // maximum allowed UBO range, so we need to reduce the threshold to prevent issues.
- .max_ubo_size = instance.UniformMaxSize() - instance.UniformMinAlignment(),
- .max_viewport_width = instance.GetMaxViewportWidth(),
- .max_viewport_height = instance.GetMaxViewportHeight(),
- .max_shared_memory_size = instance.MaxComputeSharedMemorySize(),
};
+
+ WarmUp();
+
auto [cache_result, cache] = instance.GetDevice().createPipelineCacheUnique({});
ASSERT_MSG(cache_result == vk::Result::eSuccess, "Failed to create pipeline cache: {}",
vk::to_string(cache_result));
@@ -284,9 +288,14 @@ const GraphicsPipeline* PipelineCache::GetGraphicsPipeline() {
const auto pipeline_hash = std::hash{}(graphics_key);
LOG_INFO(Render_Vulkan, "Compiling graphics pipeline {:#x}", pipeline_hash);
- it.value() = std::make_unique(instance, scheduler, desc_heap, profile,
- graphics_key, *pipeline_cache, infos,
- runtime_infos, fetch_shader, modules);
+ GraphicsPipeline::SerializationSupport sdata{};
+ it.value() = std::make_unique(
+ instance, scheduler, desc_heap, profile, graphics_key, *pipeline_cache, infos,
+ runtime_infos, fetch_shader, modules, sdata, false);
+
+ RegisterPipelineData(graphics_key, pipeline_hash, sdata);
+ ++num_new_pipelines;
+
if (EmulatorSettings::GetInstance()->IsShaderDump()) {
for (auto stage = 0; stage < MaxShaderStages; ++stage) {
if (infos[stage]) {
@@ -295,6 +304,7 @@ const GraphicsPipeline* PipelineCache::GetGraphicsPipeline() {
}
}
}
+ fetch_shader.reset();
}
return it->second.get();
}
@@ -308,9 +318,13 @@ const ComputePipeline* PipelineCache::GetComputePipeline() {
const auto pipeline_hash = std::hash{}(compute_key);
LOG_INFO(Render_Vulkan, "Compiling compute pipeline {:#x}", pipeline_hash);
- it.value() =
- std::make_unique(instance, scheduler, desc_heap, profile,
- *pipeline_cache, compute_key, *infos[0], modules[0]);
+ ComputePipeline::SerializationSupport sdata{};
+ it.value() = std::make_unique(instance, scheduler, desc_heap, profile,
+ *pipeline_cache, compute_key, *infos[0],
+ modules[0], sdata, false);
+ RegisterPipelineData(compute_key, sdata);
+ ++num_new_pipelines;
+
if (EmulatorSettings::GetInstance()->IsShaderDump()) {
auto& m = modules[0];
module_related_pipelines[m].emplace_back(compute_key);
@@ -446,6 +460,7 @@ bool PipelineCache::RefreshGraphicsStages() {
};
infos.fill(nullptr);
+ modules.fill(nullptr);
bind_stage(Stage::Fragment, LogicalStage::Fragment);
const auto* fs_info = infos[static_cast(LogicalStage::Fragment)];
@@ -516,7 +531,7 @@ bool PipelineCache::RefreshComputeKey() {
}
vk::ShaderModule PipelineCache::CompileModule(Shader::Info& info, Shader::RuntimeInfo& runtime_info,
- std::span code, size_t perm_idx,
+ const std::span& code, size_t perm_idx,
Shader::Backend::Bindings& binding) {
LOG_INFO(Render_Vulkan, "Compiling {} shader {:#x} {}", info.stage, info.pgm_hash,
perm_idx != 0 ? "(permutation)" : "");
@@ -537,6 +552,8 @@ vk::ShaderModule PipelineCache::CompileModule(Shader::Info& info, Shader::Runtim
module = CompileSPV(spv, instance.GetDevice());
}
+ RegisterShaderBinary(std::move(spv), info.pgm_hash, perm_idx);
+
const auto name = GetShaderName(info.stage, info.pgm_hash, perm_idx);
Vulkan::SetObjectName(instance.GetDevice(), module, name);
if (EmulatorSettings::GetInstance()->IsShaderDump()) {
@@ -547,7 +564,7 @@ vk::ShaderModule PipelineCache::CompileModule(Shader::Info& info, Shader::Runtim
}
PipelineCache::Result PipelineCache::GetProgram(Stage stage, LogicalStage l_stage,
- Shader::ShaderParams params,
+ const Shader::ShaderParams& params,
Shader::Backend::Bindings& binding) {
auto runtime_info = BuildRuntimeInfo(stage, l_stage);
auto [it_pgm, new_program] = program_cache.try_emplace(params.hash);
@@ -556,32 +573,42 @@ PipelineCache::Result PipelineCache::GetProgram(Stage stage, LogicalStage l_stag
auto& program = it_pgm.value();
auto start = binding;
const auto module = CompileModule(program->info, runtime_info, params.code, 0, binding);
- const auto spec = Shader::StageSpecialization(program->info, runtime_info, profile, start);
+ auto spec = Shader::StageSpecialization(program->info, runtime_info, profile, start);
+ const auto perm_hash = HashCombine(params.hash, 0);
+
+ RegisterShaderMeta(program->info, spec.fetch_shader_data, spec, perm_hash, 0);
program->AddPermut(module, std::move(spec));
- return std::make_tuple(&program->info, module, spec.fetch_shader_data,
- HashCombine(params.hash, 0));
+ return std::make_tuple(&program->info, module, program->modules[0].spec.fetch_shader_data,
+ perm_hash);
}
- it_pgm.value()->info.user_data = params.user_data;
auto& program = it_pgm.value();
auto& info = program->info;
+ info.pgm_base = params.Base(); // Needs to be actualized for inline cbuffer address fixup
+ info.user_data = params.user_data;
info.RefreshFlatBuf();
- const auto spec = Shader::StageSpecialization(info, runtime_info, profile, binding);
+ auto spec = Shader::StageSpecialization(info, runtime_info, profile, binding);
+
size_t perm_idx = program->modules.size();
+ u64 perm_hash = HashCombine(params.hash, perm_idx);
+
vk::ShaderModule module{};
const auto it = std::ranges::find(program->modules, spec, &Program::Module::spec);
if (it == program->modules.end()) {
auto new_info = Shader::Info(stage, l_stage, params);
module = CompileModule(new_info, runtime_info, params.code, perm_idx, binding);
+
+ RegisterShaderMeta(info, spec.fetch_shader_data, spec, perm_hash, perm_idx);
program->AddPermut(module, std::move(spec));
} else {
info.AddBindings(binding);
module = it->module;
perm_idx = std::distance(program->modules.begin(), it);
+ perm_hash = HashCombine(params.hash, perm_idx);
}
- return std::make_tuple(&info, module, spec.fetch_shader_data,
- HashCombine(params.hash, perm_idx));
+ return std::make_tuple(&program->info, module,
+ program->modules[perm_idx].spec.fetch_shader_data, perm_hash);
}
std::optional PipelineCache::ReplaceShader(vk::ShaderModule module,
@@ -655,5 +682,4 @@ std::optional> PipelineCache::GetShaderPatch(u64 hash, Shader::
file.Read(code);
return code;
}
-
} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.h b/src/video_core/renderer_vulkan/vk_pipeline_cache.h
index 706b99536..754397214 100644
--- a/src/video_core/renderer_vulkan/vk_pipeline_cache.h
+++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.h
@@ -23,6 +23,10 @@ namespace AmdGpu {
class Liverpool;
}
+namespace Serialization {
+struct Archive;
+}
+
namespace Shader {
struct Info;
}
@@ -38,17 +42,25 @@ struct Program {
vk::ShaderModule module;
Shader::StageSpecialization spec;
};
- using ModuleList = boost::container::small_vector;
+ static constexpr size_t MaxPermutations = 8;
+ using ModuleList = boost::container::small_vector;
Shader::Info info;
- ModuleList modules;
+ ModuleList modules{};
- explicit Program(Shader::Stage stage, Shader::LogicalStage l_stage, Shader::ShaderParams params)
+ Program() = default;
+ Program(Shader::Stage stage, Shader::LogicalStage l_stage, Shader::ShaderParams params)
: info{stage, l_stage, params} {}
- void AddPermut(vk::ShaderModule module, const Shader::StageSpecialization&& spec) {
+ void AddPermut(vk::ShaderModule module, Shader::StageSpecialization&& spec) {
modules.emplace_back(module, std::move(spec));
}
+
+ void InsertPermut(vk::ShaderModule module, Shader::StageSpecialization&& spec,
+ size_t perm_idx) {
+ modules.resize(std::max(modules.size(), perm_idx + 1)); // <-- beware of realloc
+ modules[perm_idx] = {module, std::move(spec)};
+ }
};
class PipelineCache {
@@ -57,6 +69,13 @@ public:
AmdGpu::Liverpool* liverpool);
~PipelineCache();
+ void WarmUp();
+ void Sync();
+
+ bool LoadComputePipeline(Serialization::Archive& ar);
+ bool LoadGraphicsPipeline(Serialization::Archive& ar);
+ bool LoadPipelineStage(Serialization::Archive& ar, size_t stage);
+
const GraphicsPipeline* GetGraphicsPipeline();
const ComputePipeline* GetComputePipeline();
@@ -64,7 +83,7 @@ public:
using Result = std::tuple, u64>;
Result GetProgram(Shader::Stage stage, Shader::LogicalStage l_stage,
- Shader::ShaderParams params, Shader::Backend::Bindings& binding);
+ const Shader::ShaderParams& params, Shader::Backend::Bindings& binding);
std::optional ReplaceShader(vk::ShaderModule module,
std::span spv_code);
@@ -86,10 +105,14 @@ private:
std::optional> GetShaderPatch(u64 hash, Shader::Stage stage, size_t perm_idx,
std::string_view ext);
vk::ShaderModule CompileModule(Shader::Info& info, Shader::RuntimeInfo& runtime_info,
- std::span code, size_t perm_idx,
+ const std::span& code, size_t perm_idx,
Shader::Backend::Bindings& binding);
const Shader::RuntimeInfo& BuildRuntimeInfo(Shader::Stage stage, Shader::LogicalStage l_stage);
+ [[nodiscard]] bool IsPipelineCacheDirty() const {
+ return num_new_pipelines > 0;
+ }
+
private:
const Instance& instance;
Scheduler& scheduler;
@@ -108,6 +131,7 @@ private:
std::optional fetch_shader{};
GraphicsPipelineKey graphics_key{};
ComputePipelineKey compute_key{};
+ u32 num_new_pipelines{}; // new pipelines added to the cache since the game start
// Only if Config::collectShadersForDebug()
tsl::robin_map& fetch_shader_data,
+ const Shader::StageSpecialization& spec, size_t perm_hash,
+ size_t perm_idx) {
+ if (!Storage::DataBase::Instance().IsOpened()) {
+ return;
+ }
+
+ Serialization::Archive ar;
+ Serialization::Writer meta{ar};
+
+ meta.Write(Serialization::ShaderMetaVersion);
+ meta.Write(Serialization::ShaderBinaryVersion);
+
+ meta.Write(perm_hash);
+ meta.Write(perm_idx);
+
+ spec.Serialize(ar);
+ info.Serialize(ar);
+
+ Storage::DataBase::Instance().Save(Storage::BlobType::ShaderMeta,
+ fmt::format("{:#018x}", perm_hash), ar.TakeOff());
+}
+
+void RegisterShaderBinary(std::vector&& spv, u64 pgm_hash, size_t perm_idx) {
+ if (!Storage::DataBase::Instance().IsOpened()) {
+ return;
+ }
+
+ Storage::DataBase::Instance().Save(Storage::BlobType::ShaderBinary,
+ fmt::format("{:#018x}_{}", pgm_hash, perm_idx),
+ std::move(spv));
+}
+
+bool LoadShaderMeta(Serialization::Archive& ar, Shader::Info& info,
+ std::optional& fetch_shader_data,
+ Shader::StageSpecialization& spec, size_t& perm_idx) {
+ Serialization::Reader meta{ar};
+
+ u32 meta_version{};
+ meta.Read(meta_version);
+ if (meta_version != Serialization::ShaderMetaVersion) {
+ return false;
+ }
+
+ u32 binary_version{};
+ meta.Read(binary_version);
+ if (binary_version != Serialization::ShaderBinaryVersion) {
+ return false;
+ }
+
+ u64 perm_hash_ar{};
+ meta.Read(perm_hash_ar);
+ meta.Read(perm_idx);
+
+ spec.Deserialize(ar);
+ info.Deserialize(ar);
+
+ fetch_shader_data = spec.fetch_shader_data;
+ return true;
+}
+
+void ComputePipelineKey::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer key{ar};
+ key.Write(value);
+}
+
+bool ComputePipelineKey::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader key{ar};
+ key.Read(value);
+ return true;
+}
+
+void ComputePipeline::SerializationSupport::Serialize(Serialization::Archive& ar) const {
+ // Nothing here yet
+ return;
+}
+
+bool ComputePipeline::SerializationSupport::Deserialize(Serialization::Archive& ar) {
+ // Nothing here yet
+ return true;
+}
+
+bool PipelineCache::LoadComputePipeline(Serialization::Archive& ar) {
+ compute_key.Deserialize(ar);
+
+ ComputePipeline::SerializationSupport sdata{};
+ sdata.Deserialize(ar);
+
+ std::vector meta_blob;
+ Storage::DataBase::Instance().Load(Storage::BlobType::ShaderMeta,
+ fmt::format("{:#018x}", compute_key.value), meta_blob);
+ if (meta_blob.empty()) {
+ return false;
+ }
+
+ Serialization::Archive meta_ar{std::move(meta_blob)};
+
+ if (!LoadPipelineStage(meta_ar, 0)) {
+ return false;
+ }
+
+ const auto [it, is_new] = compute_pipelines.try_emplace(compute_key);
+ ASSERT(is_new);
+
+ it.value() =
+ std::make_unique(instance, scheduler, desc_heap, profile, *pipeline_cache,
+ compute_key, *infos[0], modules[0], sdata, true);
+
+ infos.fill(nullptr);
+ modules.fill(nullptr);
+
+ return true;
+}
+
+void GraphicsPipelineKey::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer key{ar};
+
+ key.Write(this, sizeof(*this));
+}
+
+bool GraphicsPipelineKey::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader key{ar};
+
+ key.Read(this, sizeof(*this));
+ return true;
+}
+
+void GraphicsPipeline::SerializationSupport::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer sdata{ar};
+
+ sdata.Write(&vertex_attributes, sizeof(vertex_attributes));
+ sdata.Write(&vertex_bindings, sizeof(vertex_bindings));
+ sdata.Write(&divisors, sizeof(divisors));
+ sdata.Write(multisampling);
+ sdata.Write(tcs);
+ sdata.Write(tes);
+}
+
+bool GraphicsPipeline::SerializationSupport::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader sdata{ar};
+
+ sdata.Read(&vertex_attributes, sizeof(vertex_attributes));
+ sdata.Read(&vertex_bindings, sizeof(vertex_bindings));
+ sdata.Read(&divisors, sizeof(divisors));
+ sdata.Read(multisampling);
+ sdata.Read(tcs);
+ sdata.Read(tes);
+ return true;
+}
+
+bool PipelineCache::LoadGraphicsPipeline(Serialization::Archive& ar) {
+ graphics_key.Deserialize(ar);
+
+ GraphicsPipeline::SerializationSupport sdata{};
+ sdata.Deserialize(ar);
+
+ for (int stage_idx = 0; stage_idx < MaxShaderStages; ++stage_idx) {
+ const auto& hash = graphics_key.stage_hashes[stage_idx];
+ if (!hash) {
+ continue;
+ }
+
+ std::vector meta_blob;
+ Storage::DataBase::Instance().Load(Storage::BlobType::ShaderMeta,
+ fmt::format("{:#018x}", hash), meta_blob);
+ if (meta_blob.empty()) {
+ return false;
+ }
+
+ Serialization::Archive meta_ar{std::move(meta_blob)};
+
+ if (!LoadPipelineStage(meta_ar, stage_idx)) {
+ return false;
+ }
+ }
+
+ const auto [it, is_new] = graphics_pipelines.try_emplace(graphics_key);
+ ASSERT(is_new);
+
+ it.value() = std::make_unique(
+ instance, scheduler, desc_heap, profile, graphics_key, *pipeline_cache, infos,
+ runtime_infos, fetch_shader, modules, sdata, true);
+
+ infos.fill(nullptr);
+ modules.fill(nullptr);
+ fetch_shader.reset();
+
+ return true;
+}
+
+bool PipelineCache::LoadPipelineStage(Serialization::Archive& ar, size_t stage) {
+ auto program = std::make_unique();
+ Shader::StageSpecialization spec{};
+ spec.info = &program->info;
+ size_t perm_idx{};
+ if (!LoadShaderMeta(ar, program->info, fetch_shader, spec, perm_idx)) {
+ return false;
+ }
+
+ std::vector spv{};
+ Storage::DataBase::Instance().Load(Storage::BlobType::ShaderBinary,
+ fmt::format("{:#018x}_{}", program->info.pgm_hash, perm_idx),
+ spv);
+ if (spv.empty()) {
+ return false;
+ }
+
+ // Permutation hash depends on shader variation index. To prevent collisions, we need insert it
+ // at the exact position rather than append
+
+ vk::ShaderModule module{};
+
+ auto [it_pgm, new_program] = program_cache.try_emplace(program->info.pgm_hash);
+ if (new_program) {
+ module = CompileSPV(spv, instance.GetDevice());
+ it_pgm.value() = std::move(program);
+ } else {
+ const auto& it = std::ranges::find(it_pgm.value()->modules, spec, &Program::Module::spec);
+ if (it != it_pgm.value()->modules.end()) {
+ // If the permutation is already preloaded, make sure it has the same permutation index
+ const auto idx = std::distance(it_pgm.value()->modules.begin(), it);
+ ASSERT_MSG(perm_idx == idx, "Permutation {} is already inserted at {}! ({}_{:x})",
+ perm_idx, idx, program->info.stage, program->info.pgm_hash);
+ module = it->module;
+ } else {
+ module = CompileSPV(spv, instance.GetDevice());
+ }
+ }
+ it_pgm.value()->InsertPermut(module, std::move(spec), perm_idx);
+
+ infos[stage] = &it_pgm.value()->info;
+ modules[stage] = module;
+
+ return true;
+}
+
+void PipelineCache::WarmUp() {
+ if (!Config::isPipelineCacheEnabled()) {
+ return;
+ }
+
+ Storage::DataBase::Instance().Open();
+
+ // Check if cache is compatible
+ std::vector profile_data{};
+ Storage::DataBase::Instance().Load(Storage::BlobType::ShaderProfile, "profile", profile_data);
+ if (profile_data.empty()) {
+ Storage::DataBase::Instance().FinishPreload();
+
+ profile_data.resize(sizeof(profile));
+ std::memcpy(profile_data.data(), &profile, sizeof(profile));
+ Storage::DataBase::Instance().Save(Storage::BlobType::ShaderProfile, "profile",
+ std::move(profile_data));
+ return;
+ }
+ if (std::memcmp(profile_data.data(), &profile, sizeof(profile)) != 0) {
+ LOG_WARNING(Render,
+ "Pipeline cache isn't compatible with current system. Ignoring the cache");
+ return;
+ }
+
+ u32 num_pipelines{};
+ u32 num_total_pipelines{};
+
+ Storage::DataBase::Instance().ForEachBlob(
+ Storage::BlobType::PipelineKey, [&](std::vector&& data) {
+ ++num_total_pipelines;
+
+ Serialization::Archive ar{std::move(data)};
+ Serialization::Reader pldata{ar};
+
+ u32 version{};
+ pldata.Read(version);
+ if (version != Serialization::PipelineKeyVersion) {
+ return;
+ }
+
+ u32 is_compute{};
+ pldata.Read(is_compute);
+
+ bool result{};
+ if (is_compute) {
+ result = LoadComputePipeline(ar);
+ } else {
+ result = LoadGraphicsPipeline(ar);
+ }
+
+ if (result) {
+ ++num_pipelines;
+ }
+ });
+
+ LOG_INFO(Render, "Preloaded {} pipelines", num_pipelines);
+ if (num_total_pipelines > num_pipelines) {
+ LOG_WARNING(Render, "{} stale pipelines were found. Consider re-generating the cache",
+ num_total_pipelines - num_pipelines);
+ }
+
+ Storage::DataBase::Instance().FinishPreload();
+}
+
+void PipelineCache::Sync() {
+ Storage::DataBase::Instance().Close();
+}
+
+} // namespace Vulkan
+
+namespace Shader {
+
+void Info::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer info{ar};
+
+ info.Write(this, sizeof(InfoPersistent));
+ info.Write(flattened_ud_buf);
+ srt_info.Serialize(ar);
+}
+
+bool Info::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader info{ar};
+
+ info.Read(this, sizeof(Shader::InfoPersistent));
+ info.Read(flattened_ud_buf);
+
+ return srt_info.Deserialize(ar);
+}
+
+void Gcn::FetchShaderData::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer fetch{ar};
+ ar.Grow(6 + attributes.size() * sizeof(VertexAttribute));
+
+ fetch.Write(size);
+ fetch.Write(vertex_offset_sgpr);
+ fetch.Write(instance_offset_sgpr);
+ fetch.Write(attributes);
+}
+
+bool Gcn::FetchShaderData::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader fetch{ar};
+
+ fetch.Read(size);
+ fetch.Read(vertex_offset_sgpr);
+ fetch.Read(instance_offset_sgpr);
+ fetch.Read(attributes);
+
+ return true;
+}
+
+void PersistentSrtInfo::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer srt{ar};
+
+ srt.Write(this, sizeof(*this));
+ if (walker_func_size) {
+ srt.Write(reinterpret_cast(walker_func), walker_func_size);
+ }
+}
+
+bool PersistentSrtInfo::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader srt{ar};
+
+ srt.Read(this, sizeof(*this));
+
+ if (walker_func_size) {
+ walker_func = RegisterWalkerCode(ar.CurrPtr(), walker_func_size);
+ ar.Advance(walker_func_size);
+ }
+
+ return true;
+}
+
+void StageSpecialization::Serialize(Serialization::Archive& ar) const {
+ Serialization::Writer spec{ar};
+
+ spec.Write(start);
+ spec.Write(runtime_info);
+
+ spec.Write(bitset.to_string());
+
+ if (fetch_shader_data) {
+ spec.Write(sizeof(*fetch_shader_data));
+ fetch_shader_data->Serialize(ar);
+ } else {
+ spec.Write(size_t{0});
+ }
+
+ spec.Write(vs_attribs);
+ spec.Write(buffers);
+ spec.Write(images);
+ spec.Write(fmasks);
+ spec.Write(samplers);
+}
+
+bool StageSpecialization::Deserialize(Serialization::Archive& ar) {
+ Serialization::Reader spec{ar};
+
+ spec.Read(start);
+ spec.Read(runtime_info);
+
+ std::string bits{};
+ spec.Read(bits);
+ bitset = std::bitset(bits);
+
+ u64 fetch_data_size{};
+ spec.Read(fetch_data_size);
+
+ if (fetch_data_size) {
+ Gcn::FetchShaderData fetch_data;
+ fetch_data.Deserialize(ar);
+ fetch_shader_data = fetch_data;
+ }
+
+ spec.Read(vs_attribs);
+ spec.Read(buffers);
+ spec.Read(images);
+ spec.Read(fmasks);
+ spec.Read(samplers);
+
+ return true;
+}
+
+} // namespace Shader
diff --git a/src/video_core/renderer_vulkan/vk_pipeline_serialization.h b/src/video_core/renderer_vulkan/vk_pipeline_serialization.h
new file mode 100644
index 000000000..31ea4e357
--- /dev/null
+++ b/src/video_core/renderer_vulkan/vk_pipeline_serialization.h
@@ -0,0 +1,21 @@
+// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "shader_recompiler/frontend/fetch_shader.h"
+#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
+#include "video_core/renderer_vulkan/vk_shader_util.h"
+
+namespace Vulkan {
+
+void RegisterPipelineData(const ComputePipelineKey& key,
+ ComputePipeline::SerializationSupport& sdata);
+void RegisterPipelineData(const GraphicsPipelineKey& key, u64 hash,
+ GraphicsPipeline::SerializationSupport& sdata);
+void RegisterShaderMeta(const Shader::Info& info,
+ const std::optional& fetch_shader_data,
+ const Shader::StageSpecialization& spec, size_t perm_hash, size_t perm_idx);
+void RegisterShaderBinary(std::vector&& spv, u64 pgm_hash, size_t perm_idx);
+
+} // namespace Vulkan