From 8f67d6a4440bf23e882db1a2424aaf814a3cf7d9 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Sat, 7 Sep 2019 15:15:04 -0600 Subject: [PATCH] Add ZSTD compression for precompiled cache --- .gitmodules | 3 ++ externals/CMakeLists.txt | 4 +++ externals/zstd | 1 + src/common/zstd_compression.cpp | 51 +++++++++++++++++++++++++++++++++ src/common/zstd_compression.h | 44 ++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 160000 externals/zstd create mode 100644 src/common/zstd_compression.cpp create mode 100644 src/common/zstd_compression.h diff --git a/.gitmodules b/.gitmodules index 0dc4538c1..b247ccdbe 100644 --- a/.gitmodules +++ b/.gitmodules @@ -46,3 +46,6 @@ [submodule "lodepng"] path = externals/lodepng/lodepng url = https://github.com/lvandeve/lodepng.git +[submodule "zstd"] + path = externals/zstd + url = https://github.com/facebook/zstd.git diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 2ae4c33f9..63159a52b 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -62,6 +62,10 @@ if (ARCHITECTURE_x86_64) target_compile_definitions(xbyak INTERFACE XBYAK_NO_OP_NAMES) endif() +# Zstandard +add_subdirectory(zstd/build/cmake EXCLUDE_FROM_ALL) +target_include_directories(libzstd_static INTERFACE ./zstd/lib) + # ENet add_subdirectory(enet) target_include_directories(enet INTERFACE ./enet/include) diff --git a/externals/zstd b/externals/zstd new file mode 160000 index 000000000..470344d33 --- /dev/null +++ b/externals/zstd @@ -0,0 +1 @@ +Subproject commit 470344d33e1d52a2ada75d278466da8d4ee2faf6 diff --git a/src/common/zstd_compression.cpp b/src/common/zstd_compression.cpp new file mode 100644 index 000000000..23263ce30 --- /dev/null +++ b/src/common/zstd_compression.cpp @@ -0,0 +1,51 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include + +#include "common/assert.h" +#include "common/zstd_compression.h" + +namespace Common::Compression { + +std::vector CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level) { + compression_level = std::clamp(compression_level, 1, ZSTD_maxCLevel()); + + const std::size_t max_compressed_size = ZSTD_compressBound(source_size); + std::vector compressed(max_compressed_size); + + const std::size_t compressed_size = + ZSTD_compress(compressed.data(), compressed.size(), source, source_size, compression_level); + + if (ZSTD_isError(compressed_size)) { + // Compression failed + return {}; + } + + compressed.resize(compressed_size); + + return compressed; +} + +std::vector CompressDataZSTDDefault(const u8* source, std::size_t source_size) { + return CompressDataZSTD(source, source_size, ZSTD_CLEVEL_DEFAULT); +} + +std::vector DecompressDataZSTD(const std::vector& compressed) { + const std::size_t decompressed_size = + ZSTD_getDecompressedSize(compressed.data(), compressed.size()); + std::vector decompressed(decompressed_size); + + const std::size_t uncompressed_result_size = ZSTD_decompress( + decompressed.data(), decompressed.size(), compressed.data(), compressed.size()); + + if (decompressed_size != uncompressed_result_size || ZSTD_isError(uncompressed_result_size)) { + // Decompression failed + return {}; + } + return decompressed; +} + +} // namespace Common::Compression \ No newline at end of file diff --git a/src/common/zstd_compression.h b/src/common/zstd_compression.h new file mode 100644 index 000000000..e9de941c8 --- /dev/null +++ b/src/common/zstd_compression.h @@ -0,0 +1,44 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/common_types.h" + +namespace Common::Compression { + +/** + * Compresses a source memory region with Zstandard and returns the compressed data in a vector. + * + * @param source the uncompressed source memory region. + * @param source_size the size in bytes of the uncompressed source memory region. + * @param compression_level the used compression level. Should be between 1 and 22. + * + * @return the compressed data. + */ +std::vector CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level); + +/** + * Compresses a source memory region with Zstandard with the default compression level and returns + * the compressed data in a vector. + * + * @param source the uncompressed source memory region. + * @param source_size the size in bytes of the uncompressed source memory region. + * + * @return the compressed data. + */ +std::vector CompressDataZSTDDefault(const u8* source, std::size_t source_size); + +/** + * Decompresses a source memory region with Zstandard and returns the uncompressed data in a vector. + * + * @param compressed the compressed source memory region. + * + * @return the decompressed data. + */ +std::vector DecompressDataZSTD(const std::vector& compressed); + +} // namespace Common::Compression \ No newline at end of file