Archived
1
Fork 0

Add better documentation for ShaderCompiler

This commit is contained in:
redstrate 2020-08-15 20:42:14 -04:00
parent f50852abeb
commit dbe23f4aa4

View file

@ -6,28 +6,34 @@
#include <string_view> #include <string_view>
#include <optional> #include <optional>
/// The shader stage that the shader is written in.
enum class ShaderStage { enum class ShaderStage {
Vertex, Vertex,
Fragment Fragment
}; };
/// The shader language that the shader is written in.
enum class ShaderLanguage { enum class ShaderLanguage {
GLSL, GLSL,
MSL, MSL,
SPIRV SPIRV
}; };
/// Compilation options when compiling shaders.
class CompileOptions { class CompileOptions {
public: public:
/// Adds a GLSL define.
void add_definition(const std::string_view name) { void add_definition(const std::string_view name) {
definitions.emplace_back(name); definitions.emplace_back(name);
} }
std::vector<std::string> definitions; std::vector<std::string> definitions;
/// When compiling MSL, the result may differ whether or not we're targetting non-Mac Metal platforms.
bool is_apple_mobile = false; bool is_apple_mobile = false;
}; };
/// Represents the source code of a shader either in plaintext (GLSL, MSL) or bytecode (SPIR-V).
class ShaderSource { class ShaderSource {
public: public:
ShaderSource() {} ShaderSource() {}
@ -37,23 +43,33 @@ public:
std::variant<std::string, std::vector<uint32_t>> source; std::variant<std::string, std::vector<uint32_t>> source;
/// Returns a view of the shader source as plaintext.
std::string_view as_string() const { std::string_view as_string() const {
return std::get<std::string>(source); return std::get<std::string>(source);
} }
/// Returns a copy of the shader source as bytecode.
std::vector<uint32_t> as_bytecode() const { std::vector<uint32_t> as_bytecode() const {
return std::get<std::vector<uint32_t>>(source); return std::get<std::vector<uint32_t>>(source);
} }
}; };
/// Compiles GLSL shaders to a specified shader language offline or at runtime.
class ShaderCompiler { class ShaderCompiler {
public: public:
ShaderCompiler(); ShaderCompiler();
/// Sets the include directory used to search for files inside of #include directives.
void set_include_path(const std::string_view path); void set_include_path(const std::string_view path);
/** /**
Compiles from one shader language to another shader language. Compiles from one shader language to another shader language.
@param from_language The language the shader passed by shader_source is written in.
@param shader_stage The shader stage of the shader.
@param shader_source The shader code.
@param to_language The shader language to compile to.
@param options Optional compilation parameters.
@note Right now, only GLSL is supported as a source shader language.
*/ */
std::optional<ShaderSource> compile(const ShaderLanguage from_language, const ShaderStage shader_stage, const ShaderSource& shader_source, const ShaderLanguage to_language, const CompileOptions& options = CompileOptions()); std::optional<ShaderSource> compile(const ShaderLanguage from_language, const ShaderStage shader_stage, const ShaderSource& shader_source, const ShaderLanguage to_language, const CompileOptions& options = CompileOptions());
}; };