From 32830d45ffaa9001713a93500d97a6d6471aa11b Mon Sep 17 00:00:00 2001 From: DH Date: Mon, 27 Jun 2016 21:53:56 +0300 Subject: [PATCH] Improved shaders cache --- Utilities/File.cpp | 7 ++++-- Utilities/File.h | 2 +- bin/data/.gitignore | 4 --- bin/data/cache/.gitignore | 4 --- rpcs3/Emu/RSX/rsx_cache.cpp | 50 +++++++++++++++++++++++++++++++++---- rpcs3/Emu/RSX/rsx_cache.h | 6 ++++- 6 files changed, 56 insertions(+), 17 deletions(-) delete mode 100644 bin/data/.gitignore delete mode 100644 bin/data/cache/.gitignore diff --git a/Utilities/File.cpp b/Utilities/File.cpp index bea4bc905c..896fda922b 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -1207,7 +1207,7 @@ const std::string& fs::get_executable_dir() return s_dir; } -void fs::remove_all(const std::string& path) +void fs::remove_all(const std::string& path, bool remove_root) { for (const auto& entry : dir(path)) { @@ -1227,7 +1227,10 @@ void fs::remove_all(const std::string& path) } } - remove_dir(path); + if (remove_root) + { + remove_dir(path); + } } u64 fs::get_dir_size(const std::string& path) diff --git a/Utilities/File.h b/Utilities/File.h index 5fd7645fd4..aaa2c9ee41 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -440,7 +440,7 @@ namespace fs const std::string& get_executable_dir(); // Delete directory and all its contents recursively - void remove_all(const std::string& path); + void remove_all(const std::string& path, bool remove_root = true); // Get size of all files recursively u64 get_dir_size(const std::string& path); diff --git a/bin/data/.gitignore b/bin/data/.gitignore deleted file mode 100644 index 5e7d2734cf..0000000000 --- a/bin/data/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore diff --git a/bin/data/cache/.gitignore b/bin/data/cache/.gitignore deleted file mode 100644 index 5e7d2734cf..0000000000 --- a/bin/data/cache/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore diff --git a/rpcs3/Emu/RSX/rsx_cache.cpp b/rpcs3/Emu/RSX/rsx_cache.cpp index 61a5434684..6a391d9251 100644 --- a/rpcs3/Emu/RSX/rsx_cache.cpp +++ b/rpcs3/Emu/RSX/rsx_cache.cpp @@ -4,6 +4,11 @@ namespace rsx { + void shaders_cache::path(const std::string &path_) + { + m_path = path_; + } + shader_info shaders_cache::get(const program_cache_context &ctxt, raw_shader &raw_shader, const program_state& state) { auto found_entry = m_entries.find(raw_shader); @@ -11,8 +16,6 @@ namespace rsx shader_info info; entry_t *entry; - static const std::string &path = fs::get_executable_dir() + "data/cache/"; - if (found_entry != m_entries.end()) { entry = &found_entry->second; @@ -21,10 +24,18 @@ namespace rsx { //analyze_raw_shader(raw_shader); - fs::file{ path + fmt::format("%016llx.", raw_shader.hash()) + (raw_shader.type == rsx::program_type::fragment ? "fp" : "vp") + ".ucode", fs::rewrite } - .write(raw_shader.ucode.data(), raw_shader.ucode.size()); + std::string shader_name_base = + fmt::format("%016llx", raw_shader.hash()) + + (raw_shader.type == rsx::program_type::fragment ? ".fp" : ".vp"); + + fs::file{ m_path + shader_name_base + ".ucode", fs::rewrite } + .write(raw_shader.ucode.data(), raw_shader.ucode.size()); rsx::decompiled_shader decompiled_shader = decompile(raw_shader, ctxt.lang); + + fs::file{ m_path + shader_name_base + (ctxt.lang == rsx::decompile_language::glsl ? ".glsl" : ".hlsl"), fs::rewrite } + .write(decompiled_shader.code); + auto inserted = m_entries.insert({ raw_shader, entry_t{ decompiled_shader } }).first; inserted->second.decompiled.raw = &inserted->first; entry = &inserted->second; @@ -45,7 +56,15 @@ namespace rsx info.complete = &entry->complete.insert({ state, complete_shader }).first->second; info.complete->user_data = nullptr; - fs::file{ path + fmt::format("%016llx.", raw_shader.hash()) + (raw_shader.type == rsx::program_type::fragment ? "fp" : "vp") + "." + (ctxt.lang == rsx::decompile_language::glsl ? "glsl" : "hlsl"), fs::rewrite }.write(info.complete->code); + const std::string hash_combination = fmt::format("%016llx.%016llx", raw_shader.hash(), state.hash()); + + std::string shader_name = + hash_combination + + (raw_shader.type == rsx::program_type::fragment ? ".fp" : ".vp") + + (ctxt.lang == rsx::decompile_language::glsl ? ".glsl" : ".hlsl"); + + fs::file{ m_path + shader_name, fs::rewrite }.write(info.complete->code); + fs::file{ m_path + hash_combination + ".state", fs::rewrite }.write(state); } if (info.complete->user_data == nullptr) @@ -69,6 +88,27 @@ namespace rsx m_entries.clear(); } + programs_cache::programs_cache() + { + std::string path{ fs::get_executable_dir() + "data/cache/" }; + std::string title = Emu.GetTitleID(); + + if (title.empty()) + { + path += "temporary/"; + fs::remove_all(path, false); + } + else + { + path += title + "/"; + } + + fs::create_path(path); + + m_vertex_shaders_cache.path(path); + m_fragment_shader_cache.path(path); + } + programs_cache::~programs_cache() { clear(); diff --git a/rpcs3/Emu/RSX/rsx_cache.h b/rpcs3/Emu/RSX/rsx_cache.h index 306130b384..8b69f15e30 100644 --- a/rpcs3/Emu/RSX/rsx_cache.h +++ b/rpcs3/Emu/RSX/rsx_cache.h @@ -28,7 +28,7 @@ namespace rsx void(*remove_shader)(void *ptr); }; - struct shaders_cache + class shaders_cache { struct entry_t { @@ -37,8 +37,11 @@ namespace rsx }; std::unordered_map m_entries; + std::string m_path; public: + void path(const std::string &path_); + shader_info get(const program_cache_context &ctxt, raw_shader &raw_shader, const program_state& state); void clear(const program_cache_context& context); }; @@ -53,6 +56,7 @@ namespace rsx public: program_cache_context context; + programs_cache(); ~programs_cache(); program_info get(raw_program raw_program_, decompile_language lang);