diff options
author | ReinUsesLisp <reinuseslisp@airmail.cc> | 2019-01-15 05:48:29 +0100 |
---|---|---|
committer | ReinUsesLisp <reinuseslisp@airmail.cc> | 2019-02-07 02:23:39 +0100 |
commit | f087639e4a0b7ff5fbe06fe84f78302f89fae1a6 (patch) | |
tree | 65fadc896c08a20706211b7a4f7dfc1a795e961e | |
parent | gl_shader_disk_cache: Save GLSL and entries into the precompiled file (diff) | |
download | yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar.gz yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar.bz2 yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar.lz yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar.xz yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.tar.zst yuzu-f087639e4a0b7ff5fbe06fe84f78302f89fae1a6.zip |
-rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_disk_cache.cpp | 61 |
2 files changed, 57 insertions, 6 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 48be37082..33e507e69 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -104,4 +104,4 @@ add_library(video_core STATIC create_target_directory_groups(video_core) target_link_libraries(video_core PUBLIC common core) -target_link_libraries(video_core PRIVATE glad) +target_link_libraries(video_core PRIVATE glad lz4_static) diff --git a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp index 7628a74c2..bea4b29d1 100644 --- a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp @@ -5,8 +5,8 @@ #pragma once #include <cstring> - #include <fmt/format.h> +#include <lz4.h> #include "common/assert.h" #include "common/common_paths.h" @@ -51,6 +51,35 @@ std::string GetShaderHash() { std::strncpy(hash.data(), Common::g_shader_cache_version, ShaderHashSize); return std::string(hash.data()); } + +template <typename T> +std::vector<u8> CompressData(const T* source, std::size_t source_size) { + const auto source_size_int = static_cast<int>(source_size); + const int max_compressed_size = LZ4_compressBound(source_size_int); + std::vector<u8> compressed(max_compressed_size); + const int compressed_size = LZ4_compress_default(reinterpret_cast<const char*>(source), + reinterpret_cast<char*>(compressed.data()), + source_size_int, max_compressed_size); + if (compressed_size < 0) { + // Compression failed + return {}; + } + compressed.resize(compressed_size); + return compressed; +} + +std::vector<u8> DecompressData(const std::vector<u8>& compressed, std::size_t uncompressed_size) { + std::vector<u8> uncompressed(uncompressed_size); + const int size_check = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed.data()), + reinterpret_cast<char*>(uncompressed.data()), + static_cast<int>(compressed.size()), + static_cast<int>(uncompressed.size())); + if (static_cast<int>(uncompressed_size) != size_check) { + // Decompression failed + return {}; + } + return uncompressed; +} } // namespace ShaderDiskCacheRaw::ShaderDiskCacheRaw(FileUtil::IOFile& file) { @@ -175,10 +204,24 @@ bool ShaderDiskCacheOpenGL::LoadPrecompiled( file.ReadBytes(&unique_identifier, sizeof(u64)); u32 code_size{}; + u32 compressed_code_size{}; file.ReadBytes(&code_size, sizeof(u32)); - std::vector<u8> code(code_size); - file.ReadArray(code.data(), code.size()); - entry.code = std::string(reinterpret_cast<char*>(code.data()), code_size); + file.ReadBytes(&compressed_code_size, sizeof(u32)); + + std::vector<u8> compressed_code(compressed_code_size); + file.ReadArray(compressed_code.data(), compressed_code.size()); + + const std::vector<u8> code = DecompressData(compressed_code, code_size); + if (code.empty()) { + LOG_ERROR(Render_OpenGL, + "Failed to decompress GLSL code in precompiled shader={:016x} - removing", + unique_identifier); + InvalidatePrecompiled(); + dumps.clear(); + decompiled.clear(); + return false; + } + entry.code = std::string(reinterpret_cast<const char*>(code.data()), code_size); u32 const_buffers_count{}; file.ReadBytes(&const_buffers_count, sizeof(u32)); @@ -321,12 +364,20 @@ void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::str return; } + const std::vector<u8> compressed_code{CompressData(code.data(), code.size())}; + if (compressed_code.empty()) { + LOG_ERROR(Render_OpenGL, "Failed to compress GLSL code - skipping shader {:016x}", + unique_identifier); + return; + } + file.WriteObject(static_cast<u32>(PrecompiledEntryKind::Decompiled)); file.WriteObject(unique_identifier); file.WriteObject(static_cast<u32>(code.size())); - file.WriteArray(code.data(), code.size()); + file.WriteObject(static_cast<u32>(compressed_code.size())); + file.WriteArray(compressed_code.data(), compressed_code.size()); file.WriteObject(static_cast<u32>(entries.const_buffers.size())); for (const auto& cbuf : entries.const_buffers) { |