From 12b38144b9ff5d131e0d30af22bd38607d07d9fd Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:54:10 +0300 Subject: [PATCH] Downgrade to C++17 & switch to fmtlib. --- CMakeLists.txt | 6 +++--- ShaderRecomp/CMakeLists.txt | 4 +++- ShaderRecomp/main.cpp | 8 ++++---- ShaderRecomp/pch.h | 25 ++++++++++++++++++++----- ShaderRecomp/shader_recompiler.cpp | 12 ++++++------ ShaderRecomp/shader_recompiler.h | 8 ++++---- vcpkg.json | 1 + 7 files changed, 41 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4077bde..0caffec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 3.29) +cmake_minimum_required(VERSION 3.20) include($ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake) -set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD 17) project(ShaderRecomp-ALL) -add_subdirectory(ShaderRecomp) \ No newline at end of file +add_subdirectory(ShaderRecomp) diff --git a/ShaderRecomp/CMakeLists.txt b/ShaderRecomp/CMakeLists.txt index a7a72b0..aeabbd5 100644 --- a/ShaderRecomp/CMakeLists.txt +++ b/ShaderRecomp/CMakeLists.txt @@ -17,11 +17,13 @@ add_executable(ShaderRecomp find_package(directx-dxc CONFIG REQUIRED) find_package(xxhash CONFIG REQUIRED) find_package(zstd CONFIG REQUIRED) +find_package(fmt CONFIG REQUIRED) target_link_libraries(ShaderRecomp PRIVATE Microsoft::DirectXShaderCompiler xxHash::xxhash - $,zstd::libzstd_shared,zstd::libzstd_static>) + $,zstd::libzstd_shared,zstd::libzstd_static> + fmt::fmt) target_include_directories(ShaderRecomp PRIVATE ${SMOLV_SOURCE_DIR}) diff --git a/ShaderRecomp/main.cpp b/ShaderRecomp/main.cpp index dce0323..10535ce 100644 --- a/ShaderRecomp/main.cpp +++ b/ShaderRecomp/main.cpp @@ -126,10 +126,10 @@ int main(int argc, char** argv) size_t currentProgress = ++progress; if ((currentProgress % 10) == 0 || (currentProgress == shaders.size() - 1)) - std::println("Recompiling shaders... {}%", currentProgress / float(shaders.size()) * 100.0f); + fmt::println("Recompiling shaders... {}%", currentProgress / float(shaders.size()) * 100.0f); }); - std::println("Creating shader cache..."); + fmt::println("Creating shader cache..."); StringBuffer f; f.println("#include \"shader_cache.h\""); @@ -151,7 +151,7 @@ int main(int argc, char** argv) f.println("}};"); - std::println("Compressing DXIL cache..."); + fmt::println("Compressing DXIL cache..."); int level = ZSTD_maxCLevel(); //level = ZSTD_defaultCLevel(); @@ -165,7 +165,7 @@ int main(int argc, char** argv) f.println("}};"); - std::println("Compressing SPIRV cache..."); + fmt::println("Compressing SPIRV cache..."); std::vector spirvCompressed(ZSTD_compressBound(spirv.size())); spirvCompressed.resize(ZSTD_compress(spirvCompressed.data(), spirvCompressed.size(), spirv.data(), spirv.size(), level)); diff --git a/ShaderRecomp/pch.h b/ShaderRecomp/pch.h index 7619813..eca8d39 100644 --- a/ShaderRecomp/pch.h +++ b/ShaderRecomp/pch.h @@ -8,14 +8,29 @@ #include #include #include -#include -#include #include +#include +#include #include #include #include #include -#include + +template +static T byteSwap(T value) +{ + if constexpr (sizeof(T) == 1) + return value; + else if constexpr (sizeof(T) == 2) + return static_cast(__builtin_bswap16(static_cast(value))); + else if constexpr (sizeof(T) == 4) + return static_cast(__builtin_bswap32(static_cast(value))); + else if constexpr (sizeof(T) == 8) + return static_cast(__builtin_bswap64(static_cast(value))); + + assert(false && "Unexpected byte size."); + return value; +} template struct be @@ -25,9 +40,9 @@ struct be T get() const { if constexpr (std::is_enum_v) - return T(std::byteswap(std::underlying_type_t(value))); + return T(byteSwap(std::underlying_type_t(value))); else - return std::byteswap(value); + return byteSwap(value); } operator T() const diff --git a/ShaderRecomp/shader_recompiler.cpp b/ShaderRecomp/shader_recompiler.cpp index 779b5f0..96f8e72 100644 --- a/ShaderRecomp/shader_recompiler.cpp +++ b/ShaderRecomp/shader_recompiler.cpp @@ -257,7 +257,7 @@ void ShaderRecompiler::recompile(const TextureFetchInstruction& instr, bool bicu } else { - constName = std::format("s{}", instr.constIndex); + constName = fmt::format("s{}", instr.constIndex); constNamePtr = constName.c_str(); } @@ -437,7 +437,7 @@ void ShaderRecompiler::recompile(const AluInstruction& instr) if (select) { - regFormatted = std::format("r{}", reg); + regFormatted = fmt::format("r{}", reg); } else { @@ -449,12 +449,12 @@ void ShaderRecompiler::recompile(const AluInstruction& instr) { if (hasMtxProjection && strcmp(constantName, "g_MtxProjection") == 0) { - regFormatted = std::format("(iterationIndex == 0 ? mtxProjectionReverseZ[{0}] : mtxProjection[{0}])", + regFormatted = fmt::format("(iterationIndex == 0 ? mtxProjectionReverseZ[{0}] : mtxProjection[{0}])", reg - findResult->second->registerIndex); } else { - regFormatted = std::format("{}({}{})", constantName, + regFormatted = fmt::format("{}({}{})", constantName, reg - findResult->second->registerIndex, instr.const0Relative ? (instr.constAddressRegisterRelative ? " + a0" : " + aL") : ""); } } @@ -467,7 +467,7 @@ void ShaderRecompiler::recompile(const AluInstruction& instr) else { assert(!instr.const0Relative && !instr.const1Relative); - regFormatted = std::format("c{}", reg); + regFormatted = fmt::format("c{}", reg); } } @@ -1408,7 +1408,7 @@ void ShaderRecompiler::recompile(const uint8_t* shaderData, const std::string_vi { auto vertexShader = reinterpret_cast(shader); value = vertexShader->vertexElementsAndInterpolators[vertexShader->field18 + vertexShader->vertexElementCount + i]; - interpolators.emplace(i, std::format("o{}{}", USAGE_VARIABLES[uint32_t(interpolator.usage)], uint32_t(interpolator.usageIndex))); + interpolators.emplace(i, fmt::format("o{}{}", USAGE_VARIABLES[uint32_t(interpolator.usage)], uint32_t(interpolator.usageIndex))); } } diff --git a/ShaderRecomp/shader_recompiler.h b/ShaderRecomp/shader_recompiler.h index 4603fcf..2cc93a3 100644 --- a/ShaderRecomp/shader_recompiler.h +++ b/ShaderRecomp/shader_recompiler.h @@ -8,15 +8,15 @@ struct StringBuffer std::string out; template - void print(std::format_string fmt, Args&&... args) + void print(fmt::format_string fmt, Args&&... args) { - std::vformat_to(std::back_inserter(out), fmt.get(), std::make_format_args(args...)); + fmt::vformat_to(std::back_inserter(out), fmt.get(), fmt::make_format_args(args...)); } template - void println(std::format_string fmt, Args&&... args) + void println(fmt::format_string fmt, Args&&... args) { - std::vformat_to(std::back_inserter(out), fmt.get(), std::make_format_args(args...)); + fmt::vformat_to(std::back_inserter(out), fmt.get(), fmt::make_format_args(args...)); out += '\n'; } }; diff --git a/vcpkg.json b/vcpkg.json index 170cc32..5e2a2e0 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,6 +2,7 @@ "builtin-baseline": "e63bd09dc0b7204467705c1c7c71d0e2a3f8860b", "dependencies": [ "directx-dxc", + "fmt", "xxhash", "zstd" ]