From f087639e4a0b7ff5fbe06fe84f78302f89fae1a6 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 15 Jan 2019 01:48:29 -0300 Subject: [PATCH] gl_shader_disk_cache: Compress GLSL code using LZ4 --- src/video_core/CMakeLists.txt | 2 +- .../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 - #include +#include #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 +std::vector CompressData(const T* source, std::size_t source_size) { + const auto source_size_int = static_cast(source_size); + const int max_compressed_size = LZ4_compressBound(source_size_int); + std::vector compressed(max_compressed_size); + const int compressed_size = LZ4_compress_default(reinterpret_cast(source), + reinterpret_cast(compressed.data()), + source_size_int, max_compressed_size); + if (compressed_size < 0) { + // Compression failed + return {}; + } + compressed.resize(compressed_size); + return compressed; +} + +std::vector DecompressData(const std::vector& compressed, std::size_t uncompressed_size) { + std::vector uncompressed(uncompressed_size); + const int size_check = LZ4_decompress_safe(reinterpret_cast(compressed.data()), + reinterpret_cast(uncompressed.data()), + static_cast(compressed.size()), + static_cast(uncompressed.size())); + if (static_cast(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 code(code_size); - file.ReadArray(code.data(), code.size()); - entry.code = std::string(reinterpret_cast(code.data()), code_size); + file.ReadBytes(&compressed_code_size, sizeof(u32)); + + std::vector compressed_code(compressed_code_size); + file.ReadArray(compressed_code.data(), compressed_code.size()); + + const std::vector 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(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 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(PrecompiledEntryKind::Decompiled)); file.WriteObject(unique_identifier); file.WriteObject(static_cast(code.size())); - file.WriteArray(code.data(), code.size()); + file.WriteObject(static_cast(compressed_code.size())); + file.WriteArray(compressed_code.data(), compressed_code.size()); file.WriteObject(static_cast(entries.const_buffers.size())); for (const auto& cbuf : entries.const_buffers) {