Removed shader compiler to external executable #443
|
@ -67,7 +67,6 @@ project "SHADE_Engine"
|
||||||
"vulkan-1.lib",
|
"vulkan-1.lib",
|
||||||
"SDL2.lib",
|
"SDL2.lib",
|
||||||
"SDL2main.lib",
|
"SDL2main.lib",
|
||||||
"shaderc_shared.lib",
|
|
||||||
"shlwapi.lib"
|
"shlwapi.lib"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
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<char const*>(& data.shaderType), sizeof(uint8_t)
|
|
||||||
);
|
|
||||||
|
|
||||||
size_t const byteCount = sizeof(uint32_t) * data.spirvBinary.size();
|
|
||||||
|
|
||||||
file.write(
|
|
||||||
reinterpret_cast<char const*>(&byteCount), sizeof(size_t)
|
|
||||||
);
|
|
||||||
|
|
||||||
file.write(
|
|
||||||
reinterpret_cast<char const*>(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<AssetPath> 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<AssetPath> 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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<AssetPath> LoadAndCompileShader(AssetPath path) noexcept;
|
|
||||||
static std::optional<AssetPath> CompileShaderFromString
|
|
||||||
(std::string const& string, AssetPath path, SH_SHADER_TYPE type) noexcept;
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -78,6 +78,7 @@ constexpr std::string_view BUILT_IN_ASSET_ROOT{ "../../Built_In" };
|
||||||
// COMPILER EXE
|
// COMPILER EXE
|
||||||
constexpr std::string_view MODEL_COMPILER_EXE{ "ModelCompiler.exe" };
|
constexpr std::string_view MODEL_COMPILER_EXE{ "ModelCompiler.exe" };
|
||||||
constexpr std::string_view FONT_COMPILER_EXE{ "FontCompiler.exe" };
|
constexpr std::string_view FONT_COMPILER_EXE{ "FontCompiler.exe" };
|
||||||
|
constexpr std::string_view SHADER_COMPILER_EXE{ "ShaderCompiler.exe" };
|
||||||
|
|
||||||
// INTERNAL ASSET PATHS
|
// INTERNAL ASSET PATHS
|
||||||
constexpr std::string_view SCENE_FOLDER{ "/Scenes/" };
|
constexpr std::string_view SCENE_FOLDER{ "/Scenes/" };
|
||||||
|
|
|
@ -29,7 +29,6 @@
|
||||||
#include "Asset Types/SHAnimClipContainerAsset.h"
|
#include "Asset Types/SHAnimClipContainerAsset.h"
|
||||||
|
|
||||||
#include "Libraries/Compilers/SHTextureCompiler.h"
|
#include "Libraries/Compilers/SHTextureCompiler.h"
|
||||||
#include "Libraries/Compilers/SHShaderSourceCompiler.h"
|
|
||||||
|
|
||||||
#include "Filesystem/SHFileSystem.h"
|
#include "Filesystem/SHFileSystem.h"
|
||||||
#include <rttr/registration.h>
|
#include <rttr/registration.h>
|
||||||
|
@ -483,13 +482,13 @@ namespace SHADE
|
||||||
auto const ext{ path.extension().string() };
|
auto const ext{ path.extension().string() };
|
||||||
if (ext == GLSL_EXTENSION.data())
|
if (ext == GLSL_EXTENSION.data())
|
||||||
{
|
{
|
||||||
auto value { SHShaderSourceCompiler::LoadAndCompileShader(path) };
|
std::string command {SHADER_COMPILER_EXE.data()};
|
||||||
if (!value.has_value())
|
command += " " + path.string();
|
||||||
{
|
std::system(command.c_str());
|
||||||
SHLOG_ERROR("Shader compile failed: {}", path.string());
|
|
||||||
return;
|
std::string shaderPath = path.string().substr(0, path.string().find_last_of('.'));
|
||||||
}
|
shaderPath += SHADER_BUILT_IN_EXTENSION;
|
||||||
newPath = value.value();
|
newPath = shaderPath;
|
||||||
}
|
}
|
||||||
else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data())
|
else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data())
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue