diff options
| author | 2019-02-07 18:28:06 +0100 | |
|---|---|---|
| committer | 2019-03-29 16:42:19 +0100 | |
| commit | 798d76f4c7018174e58702fb06a042dc8c84f0be (patch) | |
| tree | acfd88bacaf40e3c0895ab5503aac20ec0feb470 /src | |
| parent | Merge pull request #2266 from FernandoS27/arbitration (diff) | |
| download | yuzu-798d76f4c7018174e58702fb06a042dc8c84f0be.tar.gz yuzu-798d76f4c7018174e58702fb06a042dc8c84f0be.tar.xz yuzu-798d76f4c7018174e58702fb06a042dc8c84f0be.zip | |
data_compression: Move LZ4 compression from video_core/gl_shader_disk_cache to common/data_compression
Diffstat (limited to '')
| -rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/common/data_compression.cpp | 46 | ||||
| -rw-r--r-- | src/common/data_compression.h | 17 | ||||
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_disk_cache.cpp | 46 |
5 files changed, 75 insertions, 39 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 850ce8006..43834eb85 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -79,6 +79,8 @@ add_library(common STATIC | |||
| 79 | common_funcs.h | 79 | common_funcs.h |
| 80 | common_paths.h | 80 | common_paths.h |
| 81 | common_types.h | 81 | common_types.h |
| 82 | data_compression.cpp | ||
| 83 | data_compression.h | ||
| 82 | file_util.cpp | 84 | file_util.cpp |
| 83 | file_util.h | 85 | file_util.h |
| 84 | hash.h | 86 | hash.h |
| @@ -136,3 +138,4 @@ endif() | |||
| 136 | create_target_directory_groups(common) | 138 | create_target_directory_groups(common) |
| 137 | 139 | ||
| 138 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) | 140 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) |
| 141 | target_link_libraries(common PRIVATE lz4_static) | ||
diff --git a/src/common/data_compression.cpp b/src/common/data_compression.cpp new file mode 100644 index 000000000..624b8ac47 --- /dev/null +++ b/src/common/data_compression.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <lz4.h> | ||
| 8 | |||
| 9 | #include "data_compression.h" | ||
| 10 | |||
| 11 | namespace Compression { | ||
| 12 | |||
| 13 | |||
| 14 | std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size) { | ||
| 15 | if (source_size > LZ4_MAX_INPUT_SIZE) { | ||
| 16 | // Source size exceeds LZ4 maximum input size | ||
| 17 | return {}; | ||
| 18 | } | ||
| 19 | const auto source_size_int = static_cast<int>(source_size); | ||
| 20 | const int max_compressed_size = LZ4_compressBound(source_size_int); | ||
| 21 | std::vector<u8> compressed(max_compressed_size); | ||
| 22 | const int compressed_size = LZ4_compress_default(reinterpret_cast<const char*>(source), | ||
| 23 | reinterpret_cast<char*>(compressed.data()), | ||
| 24 | source_size_int, max_compressed_size); | ||
| 25 | if (compressed_size <= 0) { | ||
| 26 | // Compression failed | ||
| 27 | return {}; | ||
| 28 | } | ||
| 29 | compressed.resize(compressed_size); | ||
| 30 | return compressed; | ||
| 31 | } | ||
| 32 | |||
| 33 | std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size) { | ||
| 34 | std::vector<u8> uncompressed(uncompressed_size); | ||
| 35 | const int size_check = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed.data()), | ||
| 36 | reinterpret_cast<char*>(uncompressed.data()), | ||
| 37 | static_cast<int>(compressed.size()), | ||
| 38 | static_cast<int>(uncompressed.size())); | ||
| 39 | if (static_cast<int>(uncompressed_size) != size_check) { | ||
| 40 | // Decompression failed | ||
| 41 | return {}; | ||
| 42 | } | ||
| 43 | return uncompressed; | ||
| 44 | } | ||
| 45 | |||
| 46 | } // namespace Compression | ||
diff --git a/src/common/data_compression.h b/src/common/data_compression.h new file mode 100644 index 000000000..70878b8d9 --- /dev/null +++ b/src/common/data_compression.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Compression { | ||
| 12 | |||
| 13 | std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size); | ||
| 14 | |||
| 15 | std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size); | ||
| 16 | |||
| 17 | } // namespace Compression \ No newline at end of file | ||
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 14b76680f..d5695638b 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -137,4 +137,4 @@ endif() | |||
| 137 | create_target_directory_groups(video_core) | 137 | create_target_directory_groups(video_core) |
| 138 | 138 | ||
| 139 | target_link_libraries(video_core PUBLIC common core) | 139 | target_link_libraries(video_core PUBLIC common core) |
| 140 | target_link_libraries(video_core PRIVATE glad lz4_static) | 140 | target_link_libraries(video_core PRIVATE glad) |
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 82fc4d44b..a74b66a90 100644 --- a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp | |||
| @@ -4,11 +4,11 @@ | |||
| 4 | 4 | ||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | #include <fmt/format.h> | 6 | #include <fmt/format.h> |
| 7 | #include <lz4.h> | ||
| 8 | 7 | ||
| 9 | #include "common/assert.h" | 8 | #include "common/assert.h" |
| 10 | #include "common/common_paths.h" | 9 | #include "common/common_paths.h" |
| 11 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 11 | #include "common/data_compression.h" | ||
| 12 | #include "common/file_util.h" | 12 | #include "common/file_util.h" |
| 13 | #include "common/logging/log.h" | 13 | #include "common/logging/log.h" |
| 14 | #include "common/scm_rev.h" | 14 | #include "common/scm_rev.h" |
| @@ -49,39 +49,6 @@ ShaderCacheVersionHash GetShaderCacheVersionHash() { | |||
| 49 | return hash; | 49 | return hash; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | template <typename T> | ||
| 53 | std::vector<u8> CompressData(const T* source, std::size_t source_size) { | ||
| 54 | if (source_size > LZ4_MAX_INPUT_SIZE) { | ||
| 55 | // Source size exceeds LZ4 maximum input size | ||
| 56 | return {}; | ||
| 57 | } | ||
| 58 | const auto source_size_int = static_cast<int>(source_size); | ||
| 59 | const int max_compressed_size = LZ4_compressBound(source_size_int); | ||
| 60 | std::vector<u8> compressed(max_compressed_size); | ||
| 61 | const int compressed_size = LZ4_compress_default(reinterpret_cast<const char*>(source), | ||
| 62 | reinterpret_cast<char*>(compressed.data()), | ||
| 63 | source_size_int, max_compressed_size); | ||
| 64 | if (compressed_size <= 0) { | ||
| 65 | // Compression failed | ||
| 66 | return {}; | ||
| 67 | } | ||
| 68 | compressed.resize(compressed_size); | ||
| 69 | return compressed; | ||
| 70 | } | ||
| 71 | |||
| 72 | std::vector<u8> DecompressData(const std::vector<u8>& compressed, std::size_t uncompressed_size) { | ||
| 73 | std::vector<u8> uncompressed(uncompressed_size); | ||
| 74 | const int size_check = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed.data()), | ||
| 75 | reinterpret_cast<char*>(uncompressed.data()), | ||
| 76 | static_cast<int>(compressed.size()), | ||
| 77 | static_cast<int>(uncompressed.size())); | ||
| 78 | if (static_cast<int>(uncompressed_size) != size_check) { | ||
| 79 | // Decompression failed | ||
| 80 | return {}; | ||
| 81 | } | ||
| 82 | return uncompressed; | ||
| 83 | } | ||
| 84 | |||
| 85 | } // namespace | 52 | } // namespace |
| 86 | 53 | ||
| 87 | ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type, | 54 | ShaderDiskCacheRaw::ShaderDiskCacheRaw(u64 unique_identifier, Maxwell::ShaderProgram program_type, |
| @@ -292,7 +259,7 @@ ShaderDiskCacheOpenGL::LoadPrecompiledFile(FileUtil::IOFile& file) { | |||
| 292 | return {}; | 259 | return {}; |
| 293 | } | 260 | } |
| 294 | 261 | ||
| 295 | dump.binary = DecompressData(compressed_binary, binary_length); | 262 | dump.binary = Compression::DecompressDataLZ4(compressed_binary, binary_length); |
| 296 | if (dump.binary.empty()) { | 263 | if (dump.binary.empty()) { |
| 297 | return {}; | 264 | return {}; |
| 298 | } | 265 | } |
| @@ -321,7 +288,7 @@ std::optional<ShaderDiskCacheDecompiled> ShaderDiskCacheOpenGL::LoadDecompiledEn | |||
| 321 | return {}; | 288 | return {}; |
| 322 | } | 289 | } |
| 323 | 290 | ||
| 324 | const std::vector<u8> code = DecompressData(compressed_code, code_size); | 291 | const std::vector<u8> code = Compression::DecompressDataLZ4(compressed_code, code_size); |
| 325 | if (code.empty()) { | 292 | if (code.empty()) { |
| 326 | return {}; | 293 | return {}; |
| 327 | } | 294 | } |
| @@ -507,7 +474,8 @@ void ShaderDiskCacheOpenGL::SaveDecompiled(u64 unique_identifier, const std::str | |||
| 507 | if (!IsUsable()) | 474 | if (!IsUsable()) |
| 508 | return; | 475 | return; |
| 509 | 476 | ||
| 510 | const std::vector<u8> compressed_code{CompressData(code.data(), code.size())}; | 477 | const std::vector<u8> compressed_code{ |
| 478 | Compression::CompressDataLZ4(reinterpret_cast<const u8*>(code.data()), code.size())}; | ||
| 511 | if (compressed_code.empty()) { | 479 | if (compressed_code.empty()) { |
| 512 | LOG_ERROR(Render_OpenGL, "Failed to compress GLSL code - skipping shader {:016x}", | 480 | LOG_ERROR(Render_OpenGL, "Failed to compress GLSL code - skipping shader {:016x}", |
| 513 | unique_identifier); | 481 | unique_identifier); |
| @@ -537,7 +505,9 @@ void ShaderDiskCacheOpenGL::SaveDump(const ShaderDiskCacheUsage& usage, GLuint p | |||
| 537 | std::vector<u8> binary(binary_length); | 505 | std::vector<u8> binary(binary_length); |
| 538 | glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data()); | 506 | glGetProgramBinary(program, binary_length, nullptr, &binary_format, binary.data()); |
| 539 | 507 | ||
| 540 | const std::vector<u8> compressed_binary = CompressData(binary.data(), binary.size()); | 508 | const std::vector<u8> compressed_binary = |
| 509 | Compression::CompressDataLZ4(binary.data(), binary.size()); | ||
| 510 | |||
| 541 | if (compressed_binary.empty()) { | 511 | if (compressed_binary.empty()) { |
| 542 | LOG_ERROR(Render_OpenGL, "Failed to compress binary program in shader={:016x}", | 512 | LOG_ERROR(Render_OpenGL, "Failed to compress binary program in shader={:016x}", |
| 543 | usage.unique_identifier); | 513 | usage.unique_identifier); |