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

Add generic SlangCompiler class

parent b05076ca
Branches
Tags
1 merge request!115Resolve "Slang Compiler"
This commit is part of merge request !115. Comments created here will be created in the context of that merge request.
...@@ -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)
......
#pragma once
#include <filesystem>
#include <vkcv/ShaderStage.hpp>
#include "Compiler.hpp"
namespace vkcv::shader {
/**
* @addtogroup vkcv_shader
* @{
*/
/**
* An abstract class to handle Slang runtime shader compilation.
*/
class SlangCompiler : public Compiler {
public:
/**
* The constructor of a runtime Slang shader compiler instance.
*/
SlangCompiler();
/**
* 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 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;
};
/** @} */
}
#include "vkcv/shader/SlangCompiler.hpp"
#include <vkcv/File.hpp>
#include <vkcv/Logger.hpp>
#include <slang.h>
namespace vkcv::shader {
static uint32_t s_CompilerCount = 0;
static slang::IGlobalSession* s_GlobalSession = nullptr;
SlangCompiler::SlangCompiler() : Compiler() {
if (s_CompilerCount == 0) {
slang::createGlobalSession(&s_GlobalSession);
}
s_CompilerCount++;
}
SlangCompiler::SlangCompiler(const SlangCompiler &other) : Compiler(other) {
s_CompilerCount++;
}
SlangCompiler::~SlangCompiler() {
s_CompilerCount--;
if ((s_CompilerCount == 0) && (s_GlobalSession != nullptr)) {
spDestroySession(s_GlobalSession);
s_GlobalSession = nullptr;
}
}
SlangCompiler &SlangCompiler::operator=(const SlangCompiler &other) {
s_CompilerCount++;
return *this;
}
void SlangCompiler::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
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment