Skip to content
Snippets Groups Projects
Commit ca77a04e authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Merge branch '144-slang-compiler' into 'develop'

Resolve "Slang Compiler"

Closes #144

See merge request !115
parents ea60d494 c7b5f3c2
No related branches found
No related tags found
1 merge request!115Resolve "Slang Compiler"
Showing
with 442 additions and 97 deletions
...@@ -82,3 +82,16 @@ ...@@ -82,3 +82,16 @@
[submodule "modules/shader_compiler/lib/json-c"] [submodule "modules/shader_compiler/lib/json-c"]
path = modules/shader_compiler/lib/json-c path = modules/shader_compiler/lib/json-c
url = https://github.com/json-c/json-c.git url = https://github.com/json-c/json-c.git
[submodule "modules/shader_compiler/lib/slang"]
path = modules/shader_compiler/lib/slang
url = https://github.com/TheJackiMonster/slang.git
branch = master
[submodule "modules/shader_compiler/lib/unordered_dense"]
path = modules/shader_compiler/lib/unordered_dense
url = https://github.com/martinus/unordered_dense.git
[submodule "modules/shader_compiler/lib/lz4"]
path = modules/shader_compiler/lib/lz4
url = https://github.com/lz4/lz4.git
[submodule "modules/shader_compiler/lib/miniz"]
path = modules/shader_compiler/lib/miniz
url = https://github.com/richgel999/miniz.git
...@@ -30,6 +30,9 @@ set(vkcv_shader_compiler_sources ...@@ -30,6 +30,9 @@ set(vkcv_shader_compiler_sources
${vkcv_shader_compiler_include}/vkcv/shader/LLVMCompiler.hpp ${vkcv_shader_compiler_include}/vkcv/shader/LLVMCompiler.hpp
${vkcv_shader_compiler_source}/vkcv/shader/LLVMCompiler.cpp ${vkcv_shader_compiler_source}/vkcv/shader/LLVMCompiler.cpp
${vkcv_shader_compiler_include}/vkcv/shader/SlangCompiler.hpp
${vkcv_shader_compiler_source}/vkcv/shader/SlangCompiler.cpp
) )
filter_headers(vkcv_shader_compiler_sources ${vkcv_shader_compiler_include} vkcv_shader_compiler_headers) filter_headers(vkcv_shader_compiler_sources ${vkcv_shader_compiler_include} vkcv_shader_compiler_headers)
...@@ -53,6 +56,12 @@ include(config/GLSLANG.cmake) ...@@ -53,6 +56,12 @@ include(config/GLSLANG.cmake)
include(config/Murmur3.cmake) include(config/Murmur3.cmake)
include(config/Shady.cmake) include(config/Shady.cmake)
# Check and load SLANG
include(config/MINIZ.cmake)
include(config/LZ4.cmake)
include(config/UNORDERED_DENSE.cmake)
include(config/SLANG.cmake)
# link the required libraries to the module # link the required libraries to the module
target_link_libraries(vkcv_shader_compiler ${vkcv_shader_compiler_libraries} vkcv) target_link_libraries(vkcv_shader_compiler ${vkcv_shader_compiler_libraries} vkcv)
......
use_git_submodule("${vkcv_shader_compiler_lib_path}/lz4" lz4_status)
if (${lz4_status})
set(LZ4_BUNDLED_MODE ON)
add_subdirectory(${vkcv_shader_compiler_lib}/lz4/build/cmake SYSTEM)
if(MSVC)
target_compile_options(
lz4_static
PRIVATE /wd5045 /wd4820 /wd4711 /wd6385 /wd6262
)
endif()
set(lz4_include ${vkcv_shader_compiler_lib}/lz4/lib)
endif ()
use_git_submodule("${vkcv_shader_compiler_lib_path}/miniz" miniz_status)
if (${miniz_status})
add_subdirectory(${vkcv_shader_compiler_lib}/miniz SYSTEM)
set_property(TARGET miniz PROPERTY POSITION_INDEPENDENT_CODE ON)
# Work around https://github.com/richgel999/miniz/pull/292
get_target_property(miniz_c_launcher miniz C_COMPILER_LAUNCHER)
if(MSVC AND miniz_c_launcher MATCHES "ccache")
set_property(TARGET miniz PROPERTY C_COMPILER_LAUNCHER)
set_property(TARGET miniz PROPERTY MSVC_DEBUG_INFORMATION_FORMAT "")
endif()
endif ()
use_git_submodule("${vkcv_shader_compiler_lib_path}/slang" slang_status)
if (${slang_status})
set(EXTERNAL_MINIZ ON CACHE INTERNAL "")
set(EXTERNAL_LZ4 ON CACHE INTERNAL "")
set(EXTERNAL_VULKAN_HEADERS ON CACHE INTERNAL "")
set(EXTERNAL_SPIRV_HEADERS ON CACHE INTERNAL "")
set(EXTERNAL_UNORDERED_DENSE ON CACHE INTERNAL "")
set(SLANG_ENABLE_CUDA OFF CACHE INTERNAL "")
set(SLANG_ENABLE_OPTIX OFF CACHE INTERNAL "")
set(SLANG_ENABLE_NVAPI OFF CACHE INTERNAL "")
set(SLANG_ENABLE_XLIB OFF CACHE INTERNAL "")
set(SLANG_ENABLE_AFTERMATH OFF CACHE INTERNAL "")
set(SLANG_ENABLE_DX_ON_VK OFF CACHE INTERNAL "")
set(SLANG_EMBED_STDLIB_SOURCE OFF CACHE INTERNAL "")
set(SLANG_EMBED_STDLIB ON CACHE INTERNAL "")
set(SLANG_ENABLE_FULL_IR_VALIDATION OFF CACHE INTERNAL "")
set(SLANG_ENABLE_IR_BREAK_ALLOC OFF CACHE INTERNAL "")
set(SLANG_ENABLE_ASAN OFF CACHE INTERNAL "")
set(SLANG_ENABLE_PREBUILT_BINARIES OFF CACHE INTERNAL "")
set(SLANG_ENABLE_GFX OFF CACHE INTERNAL "")
set(SLANG_ENABLE_SLANGD OFF CACHE INTERNAL "")
set(SLANG_ENABLE_SLANGC OFF CACHE INTERNAL "")
set(SLANG_ENABLE_SLANGRT OFF CACHE INTERNAL "")
set(SLANG_ENABLE_SLANG_GLSLANG OFF CACHE INTERNAL "")
set(SLANG_ENABLE_TESTS OFF CACHE INTERNAL "")
set(SLANG_ENABLE_EXAMPLES OFF CACHE INTERNAL "")
set(SLANG_LIB_TYPE ${vkcv_build_attribute} CACHE INTERNAL "")
set(SLANG_SPIRV_HEADERS_INCLUDE_DIR ${spirv_headers_include} CACHE INTERNAL "")
add_subdirectory(${vkcv_shader_compiler_lib}/slang)
set(slang_system_includes "")
list(APPEND slang_system_includes ${spirv_headers_include})
list(APPEND slang_system_includes ${unordered_dense_include})
list(APPEND slang_system_includes ${lz4_include})
target_include_directories(core SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(compiler-core SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-cpp-extractor SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-embed SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-generate SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-lookup-generator SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-capability-generator SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-spirv-embed-generator SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-bootstrap SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(prelude SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-capability-defs SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-capability-lookup SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-lookup-tables SYSTEM BEFORE PRIVATE ${slang_system_includes})
target_include_directories(slang-no-embedded-stdlib SYSTEM BEFORE PRIVATE ${slang_system_includes})
set(SLANG_STDLIB_META_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${vkcv_shader_compiler_lib}/slang/source/slang/stdlib-meta)
target_include_directories(slang BEFORE PUBLIC ${SLANG_STDLIB_META_OUTPUT_DIR})
message(WARNING ${SLANG_STDLIB_META_OUTPUT_DIR})
list(APPEND vkcv_shader_compiler_libraries slang lz4_static miniz unordered_dense::unordered_dense)
list(APPEND vkcv_shader_compiler_includes ${vkcv_shader_compiler_lib}/slang/include ${slang_system_includes})
endif ()
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
use_git_submodule("${vkcv_shader_compiler_lib_path}/SPIRV-Headers" spriv_headers_status) use_git_submodule("${vkcv_shader_compiler_lib_path}/SPIRV-Headers" spriv_headers_status)
if (${spriv_headers_status}) if (${spriv_headers_status})
add_subdirectory(${vkcv_shader_compiler_lib}/SPIRV-Headers) add_subdirectory(${vkcv_shader_compiler_lib}/SPIRV-Headers SYSTEM)
set(spirv_headers_include ${vkcv_shader_compiler_lib_path}/SPIRV-Headers/include)
list(APPEND vkcv_shader_compiler_libraries SPIRV-Headers) list(APPEND vkcv_shader_compiler_libraries SPIRV-Headers)
list(APPEND vkcv_shader_compiler_includes ${vkcv_shader_compiler_lib}/SPIRV-Headers/include) list(APPEND vkcv_shader_compiler_includes ${spirv_headers_include})
endif () endif ()
...@@ -3,8 +3,8 @@ use_git_submodule("${vkcv_shader_compiler_lib_path}/shady" shady_status) ...@@ -3,8 +3,8 @@ use_git_submodule("${vkcv_shader_compiler_lib_path}/shady" shady_status)
if (${shady_status}) if (${shady_status})
set(EXTERNAL_JSON_C ON CACHE INTERNAL "") set(EXTERNAL_JSON_C ON CACHE INTERNAL "")
set(EXTERNAL_SPIRV_HEADERS ON CACHE INTERNAL "") set(EXTERNAL_SPIRV_HEADERS ON CACHE INTERNAL "")
set(EXTERNAL_MURMUR3 ON CACHE INTERNAL "") set(EXTERNAL_MURMUR3 ON CACHE INTERNAL "")
set(EXTERNAL_JSON_C_INCLUDE ${JSON_C_INCLUDE_DIR} CACHE INTERNAL "") set(EXTERNAL_JSON_C_INCLUDE ${JSON_C_INCLUDE_DIR} CACHE INTERNAL "")
......
use_git_submodule("${vkcv_shader_compiler_lib_path}/unordered_dense" unordered_dense_status)
if (${unordered_dense_status})
add_subdirectory(${vkcv_shader_compiler_lib}/unordered_dense SYSTEM)
set(unordered_dense_include ${vkcv_shader_compiler_lib_path}/unordered_dense/include)
endif ()
...@@ -84,11 +84,11 @@ namespace vkcv::shader { ...@@ -84,11 +84,11 @@ namespace vkcv::shader {
* @param[in] includePath Include path for shaders * @param[in] includePath Include path for shaders
* @param[in] update Flag to update shaders during runtime * @param[in] update Flag to update shaders during runtime
*/ */
virtual void compile(ShaderStage shaderStage, void compile(ShaderStage shaderStage,
const std::filesystem::path& shaderPath, const std::filesystem::path& shaderPath,
const ShaderCompiledFunction& compiled, const ShaderCompiledFunction& compiled,
const std::filesystem::path& includePath, const std::filesystem::path& includePath = "",
bool update) = 0; bool update = false);
/** /**
* Compile a shader program from a specific map of given file paths for * Compile a shader program from a specific map of given file paths for
......
#pragma once #pragma once
#include <filesystem>
#include <vkcv/ShaderStage.hpp> #include <vkcv/ShaderStage.hpp>
#include "Compiler.hpp" #include "Compiler.hpp"
...@@ -57,23 +55,6 @@ namespace vkcv::shader { ...@@ -57,23 +55,6 @@ namespace vkcv::shader {
*/ */
GlslangCompiler& operator=(GlslangCompiler&& other) = default; GlslangCompiler& operator=(GlslangCompiler&& other) = default;
/**
* Compile a shader from a specific file path for a target stage with
* a custom shader include path and an event function called if the
* compilation completes.
*
* @param[in] shaderStage Shader pipeline stage
* @param[in] shaderPath Filepath of shader
* @param[in] compiled Shader compilation event
* @param[in] includePath Include path for shaders
* @param[in] update Flag to update shaders during runtime
*/
void compile(ShaderStage shaderStage,
const std::filesystem::path& shaderPath,
const ShaderCompiledFunction& compiled,
const std::filesystem::path& includePath = "",
bool update = false) override;
}; };
/** @} */ /** @} */
......
...@@ -57,23 +57,6 @@ namespace vkcv::shader { ...@@ -57,23 +57,6 @@ namespace vkcv::shader {
*/ */
ShadyCompiler& operator=(ShadyCompiler&& other) = default; ShadyCompiler& operator=(ShadyCompiler&& other) = default;
/**
* Compile a shader from a specific file path for a target stage with
* a custom shader include path and an event function called if the
* compilation completes.
*
* @param[in] shaderStage Shader pipeline stage
* @param[in] shaderPath Filepath of shader
* @param[in] compiled Shader compilation event
* @param[in] includePath Include path for shaders
* @param[in] update Flag to update shaders during runtime
*/
void compile(ShaderStage shaderStage,
const std::filesystem::path& shaderPath,
const ShaderCompiledFunction& compiled,
const std::filesystem::path& includePath = "",
bool update = false) override;
}; };
/** @} */ /** @} */
......
#pragma once
#include <filesystem>
#include <vkcv/ShaderStage.hpp>
#include "Compiler.hpp"
namespace vkcv::shader {
/**
* @addtogroup vkcv_shader
* @{
*/
enum class SlangCompileProfile {
GLSL,
HLSL,
SPIRV,
UNKNOWN
};
/**
* An abstract class to handle Slang runtime shader compilation.
*/
class SlangCompiler : public Compiler {
private:
SlangCompileProfile m_profile;
public:
/**
* The constructor of a runtime Slang shader compiler instance.
*
* @param[in] profile Compile profile (optional)
*/
SlangCompiler(SlangCompileProfile profile = SlangCompileProfile::UNKNOWN);
/**
* The copy-constructor of a runtime Slang shader compiler instance.
*
* @param[in] other Other instance of a Slang shader compiler instance
*/
SlangCompiler(const SlangCompiler& other);
/**
* The move-constructor of a runtime Slang shader compiler instance.
*
* @param[out] other Other instance of a Slang shader compiler instance
*/
SlangCompiler(SlangCompiler&& other) = default;
/**
* The destructor of a runtime Slang shader compiler instance.
*/
~SlangCompiler();
/**
* The copy-operator of a runtime Slang shader compiler instance.
*
* @param[in] other Other instance of a Slang shader compiler instance
* @return Reference to this instance
*/
SlangCompiler& operator=(const SlangCompiler& other);
/**
* The copy-operator of a runtime Slang shader compiler instance.
*
* @param[out] other Other instance of a Slang shader compiler instance
* @return Reference to this instance
*/
SlangCompiler& operator=(SlangCompiler&& other) = default;
/**
* Compile a shader from source for a target stage with a custom shader
* include path and an event function called if the compilation completes.
*
* @param[in] shaderStage Shader pipeline stage
* @param[in] shaderSource Source of shader
* @param[in] compiled Shader compilation event
* @param[in] includePath Include path for shaders
* @return Result if the compilation succeeds
*/
bool compileSource(ShaderStage shaderStage,
const std::string& shaderSource,
const ShaderCompiledFunction& compiled,
const std::filesystem::path& includePath) override;
};
/** @} */
}
Subproject commit 68959d27c3ec37b339b3b8ecfea155faf0ef94f2
Subproject commit bf7a1f0a5aa1deae9cab2d73b5ef9edec41b877c
Subproject commit e1734897a3681f7685b4768fd8d40f446134d483
Subproject commit d911053e390816ecc5dedd5a9d6b4bb5ed92b4c9
...@@ -41,6 +41,33 @@ namespace vkcv::shader { ...@@ -41,6 +41,33 @@ namespace vkcv::shader {
}, directory }, directory
); );
} }
void Compiler::compile(ShaderStage shaderStage,
const std::filesystem::path &shaderPath,
const ShaderCompiledFunction &compiled,
const std::filesystem::path &includePath,
bool update) {
std::string shaderCode;
bool result = readTextFromFile(shaderPath, shaderCode);
if (!result) {
vkcv_log(LogLevel::ERROR, "Loading shader failed: (%s)", shaderPath.string().c_str());
}
if (!includePath.empty()) {
result = compileSource(shaderStage, shaderCode, compiled, includePath);
} else {
result = compileSource(shaderStage, shaderCode, compiled, shaderPath.parent_path());
}
if (!result) {
vkcv_log(LogLevel::ERROR, "Shader compilation failed: (%s)", shaderPath.string().c_str());
}
if (update) {
// TODO: Shader hot compilation during runtime
}
}
void Compiler::compileProgram(ShaderProgram& program, void Compiler::compileProgram(ShaderProgram& program,
const Dictionary<ShaderStage, const std::filesystem::path>& stages, const Dictionary<ShaderStage, const std::filesystem::path>& stages,
......
...@@ -36,31 +36,4 @@ namespace vkcv::shader { ...@@ -36,31 +36,4 @@ namespace vkcv::shader {
return *this; return *this;
} }
void GlslangCompiler::compile(ShaderStage shaderStage,
const std::filesystem::path &shaderPath,
const ShaderCompiledFunction &compiled,
const std::filesystem::path &includePath,
bool update) {
std::string shaderCode;
bool result = readTextFromFile(shaderPath, shaderCode);
if (!result) {
vkcv_log(LogLevel::ERROR, "Loading shader failed: (%s)", shaderPath.string().c_str());
}
if (!includePath.empty()) {
result = compileSource(shaderStage, shaderCode, compiled, includePath);
} else {
result = compileSource(shaderStage, shaderCode, compiled, shaderPath.parent_path());
}
if (!result) {
vkcv_log(LogLevel::ERROR, "Shader compilation failed: (%s)", shaderPath.string().c_str());
}
if (update) {
// TODO: Shader hot compilation during runtime
}
}
} }
...@@ -9,31 +9,4 @@ namespace vkcv::shader { ...@@ -9,31 +9,4 @@ namespace vkcv::shader {
ShadyCompiler::ShadyCompiler() ShadyCompiler::ShadyCompiler()
: Compiler() {} : Compiler() {}
void ShadyCompiler::compile(ShaderStage shaderStage,
const std::filesystem::path &shaderPath,
const ShaderCompiledFunction &compiled,
const std::filesystem::path &includePath,
bool update) {
std::string shaderCode;
bool result = readTextFromFile(shaderPath, shaderCode);
if (!result) {
vkcv_log(LogLevel::ERROR, "Loading shader failed: (%s)", shaderPath.string().c_str());
}
if (!includePath.empty()) {
result = compileSource(shaderStage, shaderCode, compiled, includePath);
} else {
result = compileSource(shaderStage, shaderCode, compiled, shaderPath.parent_path());
}
if (!result) {
vkcv_log(LogLevel::ERROR, "Shader compilation failed: (%s)", shaderPath.string().c_str());
}
if (update) {
// TODO: Shader hot compilation during runtime
}
}
} }
#include "vkcv/shader/SlangCompiler.hpp"
#include <cstdint>
#include <vkcv/File.hpp>
#include <vkcv/Logger.hpp>
#include <slang.h>
#include <slang-com-ptr.h>
#include <slang-com-helper.h>
#include <vkcv/ShaderStage.hpp>
namespace vkcv::shader {
static uint32_t s_CompilerCount = 0;
static Slang::ComPtr<slang::IGlobalSession> s_GlobalSession;
SlangCompiler::SlangCompiler(SlangCompileProfile profile)
: Compiler(), m_profile(profile) {
if (s_CompilerCount == 0) {
slang::createGlobalSession(s_GlobalSession.writeRef());
}
s_CompilerCount++;
}
SlangCompiler::SlangCompiler(const SlangCompiler &other)
: Compiler(other), m_profile(other.m_profile) {
s_CompilerCount++;
}
SlangCompiler::~SlangCompiler() {
s_CompilerCount--;
}
SlangCompiler &SlangCompiler::operator=(const SlangCompiler &other) {
m_profile = other.m_profile;
s_CompilerCount++;
return *this;
}
constexpr SlangStage findShaderLanguage(ShaderStage shaderStage) {
switch (shaderStage) {
case ShaderStage::VERTEX:
return SlangStage::SLANG_STAGE_VERTEX;
case ShaderStage::TESS_CONTROL:
return SlangStage::SLANG_STAGE_HULL;
case ShaderStage::TESS_EVAL:
return SlangStage::SLANG_STAGE_DOMAIN;
case ShaderStage::GEOMETRY:
return SlangStage::SLANG_STAGE_GEOMETRY;
case ShaderStage::FRAGMENT:
return SlangStage::SLANG_STAGE_FRAGMENT;
case ShaderStage::COMPUTE:
return SlangStage::SLANG_STAGE_COMPUTE;
case ShaderStage::TASK:
return SlangStage::SLANG_STAGE_AMPLIFICATION;
case ShaderStage::MESH:
return SlangStage::SLANG_STAGE_MESH;
case ShaderStage::RAY_GEN:
return SlangStage::SLANG_STAGE_RAY_GENERATION;
case ShaderStage::RAY_CLOSEST_HIT:
return SlangStage::SLANG_STAGE_CLOSEST_HIT;
case ShaderStage::RAY_MISS:
return SlangStage::SLANG_STAGE_MISS;
case ShaderStage::RAY_INTERSECTION:
return SlangStage::SLANG_STAGE_INTERSECTION;
case ShaderStage::RAY_ANY_HIT:
return SlangStage::SLANG_STAGE_ANY_HIT;
case ShaderStage::RAY_CALLABLE:
return SlangStage::SLANG_STAGE_CALLABLE;
default:
return SlangStage::SLANG_STAGE_NONE;
}
}
bool SlangCompiler::compileSource(ShaderStage shaderStage,
const std::string& shaderSource,
const ShaderCompiledFunction& compiled,
const std::filesystem::path& includePath) {
slang::SessionDesc sessionDesc = {};
slang::TargetDesc targetDesc = {};
SlangSourceLanguage lang;
targetDesc.format = SLANG_SPIRV;
switch (m_profile) {
case SlangCompileProfile::GLSL:
targetDesc.profile = s_GlobalSession->findProfile("glsl_460");
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
sessionDesc.allowGLSLSyntax = true;
lang = SLANG_SOURCE_LANGUAGE_GLSL;
break;
case SlangCompileProfile::HLSL:
targetDesc.profile = s_GlobalSession->findProfile("sm_5_0");
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_ROW_MAJOR;
lang = SLANG_SOURCE_LANGUAGE_HLSL;
break;
case SlangCompileProfile::SPIRV:
targetDesc.profile = s_GlobalSession->findProfile("spirv_1_5");
targetDesc.flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
lang = SLANG_SOURCE_LANGUAGE_SPIRV;
break;
default:
lang = SLANG_SOURCE_LANGUAGE_UNKNOWN;
break;
}
sessionDesc.targets = &targetDesc;
sessionDesc.targetCount = 1;
const char *searchPath = includePath.c_str();
sessionDesc.searchPaths = &searchPath;
sessionDesc.searchPathCount = 1;
std::vector<slang::PreprocessorMacroDesc> macros;
macros.reserve(m_defines.size());
for (const auto& define : m_defines) {
const slang::PreprocessorMacroDesc macro = {
define.first.c_str(),
define.second.c_str()
};
macros.push_back(macro);
}
sessionDesc.preprocessorMacros = macros.data();
sessionDesc.preprocessorMacroCount = macros.size();
Slang::ComPtr<slang::ISession> session;
if (SLANG_FAILED(s_GlobalSession->createSession(sessionDesc, session.writeRef()))) {
vkcv_log(LogLevel::ERROR, "Compiler session could not be created");
return false;
}
Slang::ComPtr<slang::ICompileRequest> request;
if (SLANG_FAILED(session->createCompileRequest(request.writeRef()))) {
vkcv_log(LogLevel::ERROR, "Compilation request could not be created");
return false;
}
const int translationUnit = request->addTranslationUnit(lang, nullptr);
request->addTranslationUnitSourceString(translationUnit, nullptr, shaderSource.c_str());
const int entryPoint = request->addEntryPoint(
translationUnit, "main", findShaderLanguage(shaderStage)
);
if (SLANG_FAILED(request->compile())) {
vkcv_log(LogLevel::ERROR, "Compilation process failed");
return false;
}
size_t size;
const void *code = request->getEntryPointCode(entryPoint, &size);
if (0 == size) {
code = request->getCompileRequestCode(&size);
}
if ((0 == size) || (!code)) {
vkcv_log(LogLevel::ERROR, "Entry point could not be found\n%s",
request->getDiagnosticOutput());
return false;
}
std::vector<uint32_t> spirv;
spirv.resize(size / sizeof(uint32_t));
memcpy(spirv.data(), code, spirv.size() * sizeof(uint32_t));
const std::filesystem::path tmp_path = generateTemporaryFilePath();
if (!writeBinaryToFile(tmp_path, spirv)) {
vkcv_log(LogLevel::ERROR, "Spir-V could not be written to disk");
return false;
}
if (compiled) {
compiled(shaderStage, tmp_path);
}
std::filesystem::remove(tmp_path);
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment