From f21f235980dbd312eb965b6be4ba8ab917b369d8 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Sat, 25 Mar 2023 15:58:06 +0800 Subject: [PATCH] Moved out all native shader compiler code Replaced call to internal classes with system calls to external executable --- SHADE_Engine/premake5.lua | 1 - .../Compilers/SHShaderSourceCompiler.cpp | 164 ------------------ .../Compilers/SHShaderSourceCompiler.h | 31 ---- SHADE_Engine/src/Assets/SHAssetMacros.h | 1 + SHADE_Engine/src/Assets/SHAssetManager.cpp | 15 +- 5 files changed, 8 insertions(+), 204 deletions(-) delete mode 100644 SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.cpp delete mode 100644 SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.h diff --git a/SHADE_Engine/premake5.lua b/SHADE_Engine/premake5.lua index 17ca5be8..e4bcbc09 100644 --- a/SHADE_Engine/premake5.lua +++ b/SHADE_Engine/premake5.lua @@ -67,7 +67,6 @@ project "SHADE_Engine" "vulkan-1.lib", "SDL2.lib", "SDL2main.lib", - "shaderc_shared.lib", "shlwapi.lib" } diff --git a/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.cpp b/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.cpp deleted file mode 100644 index 0bde59c7..00000000 --- a/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/*************************************************************************//** - * \file SHShaderSourceCompiler.cpp - * \author Loh Xiao Qi - * \date 23 10 2022 - * \brief - * - * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or - * disclosure of this file or its contents without the prior written consent - * of DigiPen Institute of Technology is prohibited. - *****************************************************************************/ -#include "SHpch.h" -#include "SHShaderSourceCompiler.h" -#include "shaderc/shaderc.hpp" -#include "Events/SHEventManager.hpp" - -#include -#include -#include -#include - -namespace SHADE -{ - std::string SHShaderSourceCompiler::CompileShaderSourceToBinary(AssetPath path, SHShaderAsset const& data) noexcept - { - std::string newPath{ path.string() }; - newPath = newPath.substr(0, newPath.find_last_of('.')); - newPath += SHADER_BUILT_IN_EXTENSION.data(); - - std::ofstream file{ newPath, std::ios::binary | std::ios::out | std::ios::trunc }; - - file.write( - reinterpret_cast(& data.shaderType), sizeof(uint8_t) - ); - - size_t const byteCount = sizeof(uint32_t) * data.spirvBinary.size(); - - file.write( - reinterpret_cast(&byteCount), sizeof(size_t) - ); - - file.write( - reinterpret_cast(data.spirvBinary.data()), byteCount - ); - - file.close(); - - return newPath; - } - - SHShaderAsset const* SHShaderSourceCompiler::CompileShaderSourceToMemory(std::string const& data, std::string const& name, SH_SHADER_TYPE type) noexcept - { - // shaderc compiler - shaderc::Compiler compiler; - shaderc::CompileOptions options; - - options.AddMacroDefinition("MY_DEFINE", "1"); - - //TODO: Check if we need optimisation levels when compiling into spirv - // Set optimization levels - //if (opLevel != shaderc_optimization_level_zero) - // options.SetOptimizationLevel(opLevel); - - // Attempt to get the shaderc equivalent shader stage - shaderc_shader_kind shaderKind; - switch (type) - { - case SH_SHADER_TYPE::VERTEX: - shaderKind = shaderc_shader_kind::shaderc_glsl_vertex_shader; - break; - case SH_SHADER_TYPE::FRAGMENT: - shaderKind = shaderc_shader_kind::shaderc_glsl_fragment_shader; - break; - case SH_SHADER_TYPE::COMPUTE: - shaderKind = shaderc_shader_kind::shaderc_glsl_compute_shader; - break; - default: - shaderKind = shaderc_shader_kind::shaderc_glsl_vertex_shader; - break; - } - - // Compile the shader and get the result - shaderc::SpvCompilationResult compileResult = compiler.CompileGlslToSpv(data, shaderKind, name.c_str(), options); - - if (compileResult.GetCompilationStatus() != shaderc_compilation_status_success) - { - SHLOG_ERROR("Shaderc failed to compile GLSL shader to binary | " + compileResult.GetErrorMessage()); - return nullptr; - } - - auto result = new SHShaderAsset(); - result->spirvBinary.resize(compileResult.end() - compileResult.begin()); - - std::ranges::copy(compileResult.begin(), compileResult.end(), result->spirvBinary.data()); - - result->name = name; - result->shaderType = type; - - return result; - } - - SH_SHADER_TYPE SHShaderSourceCompiler::GetShaderTypeFromFilename(std::string name) noexcept - { - for (auto i { 0 }; i < SHADER_TYPE_MAX_COUNT; ++i) - { - const auto& [SHADER_SUFFIX, SHADER_TYPE] = SHADER_IDENTIFIERS[i]; - if (name.find(SHADER_SUFFIX.data()) != std::string::npos) - { - return SHADER_TYPE; - } - } - - return SH_SHADER_TYPE::INAVLID_TYPE; - } - - std::optional SHShaderSourceCompiler::LoadAndCompileShader(AssetPath path) noexcept - { - auto type = GetShaderTypeFromFilename(path.filename().string()); - - if (type == SH_SHADER_TYPE::INAVLID_TYPE) - { - SHLOG_ERROR("Invalid filename for shaders, follow suffix in SHAssetMacros.h: {}", path.string()); - return {}; - } - - path.make_preferred(); - - std::ifstream file{ path.string(), std::ios::in }; - - if (file.is_open()) - { - std::stringstream stream; - - stream << file.rdbuf(); - - std::string const content = stream.str(); - - auto data = CompileShaderSourceToMemory(content, path.filename().string(), type); - - if (data == nullptr) - { - return{}; - } - - return CompileShaderSourceToBinary(path, *data); - } - - SHLOG_ERROR("Unable to open shader file: {}", path.string()); - - return {}; - } - - std::optional SHShaderSourceCompiler::CompileShaderFromString - (std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept - { - auto const data = CompileShaderSourceToMemory(string, path.filename().string(), type); - - if (data == nullptr) - { - return{}; - } - - return CompileShaderSourceToBinary(path, *data); - } -} diff --git a/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.h b/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.h deleted file mode 100644 index 4ba87050..00000000 --- a/SHADE_Engine/src/Assets/Libraries/Compilers/SHShaderSourceCompiler.h +++ /dev/null @@ -1,31 +0,0 @@ -/*************************************************************************//** - * \file SHShaderSourceCompiler.h - * \author Loh Xiao Qi - * \date 23 10 2022 - * \brief - * - * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or - * disclosure of this file or its contents without the prior written consent - * of DigiPen Institute of Technology is prohibited. - *****************************************************************************/ -#pragma once - -#include "Assets/SHAssetMacros.h" -#include "Assets/Asset Types/SHShaderAsset.h" - -namespace SHADE -{ - class SHShaderSourceCompiler - { - private: - static std::string CompileShaderSourceToBinary(AssetPath path, SHShaderAsset const& data) noexcept; - static SHShaderAsset const* CompileShaderSourceToMemory(std::string const& data, std::string const& name, SH_SHADER_TYPE type) noexcept; - - static SH_SHADER_TYPE GetShaderTypeFromFilename(std::string name) noexcept; - - public: - static std::optional LoadAndCompileShader(AssetPath path) noexcept; - static std::optional CompileShaderFromString - (std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept; - }; -} diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index 0616471c..0f037d07 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -78,6 +78,7 @@ constexpr std::string_view BUILT_IN_ASSET_ROOT{ "../../Built_In" }; // COMPILER EXE constexpr std::string_view MODEL_COMPILER_EXE{ "ModelCompiler.exe" }; constexpr std::string_view FONT_COMPILER_EXE{ "FontCompiler.exe" }; +constexpr std::string_view SHADER_COMPILER_EXE{ "ShaderCompiler.exe" }; // INTERNAL ASSET PATHS constexpr std::string_view SCENE_FOLDER{ "/Scenes/" }; diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index 14b5b0a7..1a141023 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -29,7 +29,6 @@ #include "Asset Types/SHAnimClipContainerAsset.h" #include "Libraries/Compilers/SHTextureCompiler.h" -#include "Libraries/Compilers/SHShaderSourceCompiler.h" #include "Filesystem/SHFileSystem.h" #include @@ -483,13 +482,13 @@ namespace SHADE auto const ext{ path.extension().string() }; if (ext == GLSL_EXTENSION.data()) { - auto value { SHShaderSourceCompiler::LoadAndCompileShader(path) }; - if (!value.has_value()) - { - SHLOG_ERROR("Shader compile failed: {}", path.string()); - return; - } - newPath = value.value(); + std::string command {SHADER_COMPILER_EXE.data()}; + command += " " + path.string(); + std::system(command.c_str()); + + std::string shaderPath = path.string().substr(0, path.string().find_last_of('.')); + shaderPath += SHADER_BUILT_IN_EXTENSION; + newPath = shaderPath; } else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data()) {