From d25c392cec57e8c561899bf75668da79c4e67aed Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Wed, 9 Oct 2024 22:11:24 -0700 Subject: add shader compile infra --- src/render_pass.h | 23 +++++++++++++++++++ src/shader.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shader.h | 33 +++++++++++++++++++++++++++ xmake.lua | 3 ++- 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/render_pass.h create mode 100644 src/shader.cpp create mode 100644 src/shader.h diff --git a/src/render_pass.h b/src/render_pass.h new file mode 100644 index 0000000..b01bcae --- /dev/null +++ b/src/render_pass.h @@ -0,0 +1,23 @@ +#pragma once + +enum struct RenderPassType { + RayTracing, + Compute, + Rasterization // Not intended to implement, want to make a pure ray tracer +}; + +struct RayTracingPass { + +}; + +struct ComputePass { + +}; + +struct RenderPass { + RenderPassType type; + union { + RayTracingPass ray_tracing; + ComputePass compute; + } inner; +}; diff --git a/src/shader.cpp b/src/shader.cpp new file mode 100644 index 0000000..95a7904 --- /dev/null +++ b/src/shader.cpp @@ -0,0 +1,67 @@ +#include "shader.h" +#include +#include +#include +#include + +namespace iris { + +ShaderDesc::ShaderDesc(const std::string_view path) { + // Load the shader from file + std::ifstream file("example.txt"); + if (!file.is_open()) { + spdlog::error("Failed to open shader file: {}", path); + abort(); + } + std::ostringstream file_stream; + file_stream << file.rdbuf(); + this->source = file_stream.str(); + file.close(); + + // Determine the shader type + size_t last_dot = path.find_last_of('.'); + if (last_dot == std::string::npos) { + spdlog::error("Invalid shader file type: {}", path); + abort(); + } + size_t last_slash = path.find_last_of('/'); + if (last_slash == std::string::npos) { + last_slash = 0; + } + this->name = path.substr(last_slash, last_dot - last_slash - 1); + this->type = shader_type_from_string(path.substr(last_dot + 1)); + + // Compile the shader to SPIR-V + shaderc::Compiler compiler; + shaderc::CompileOptions options; + options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_3); + + shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv( + this->source, shaderc_glsl_compute_shader, path.data(), options); + if (result.GetCompilationStatus() != shaderc_compilation_status_success) { + spdlog::error("Failed to compile shader: {}", result.GetErrorMessage()); + abort(); + } + this->compiled_binary = std::vector(result.cbegin(), result.cend()); +} + +ShaderDesc::Type ShaderDesc::shader_type_from_string(std::string_view type) { + if (type == "rgen") { + return ShaderDesc::Type::eRayGen; + } else if (type == "rmiss") { + return ShaderDesc::Type::eMiss; + } else if (type == "rchit") { + return ShaderDesc::Type::eClosestHit; + } else if (type == "rahit") { + return ShaderDesc::Type::eAnyHit; + } else if (type == "rint") { + return ShaderDesc::Type::eIntersection; + } else if (type == "comp") { + return ShaderDesc::Type::eCompute; + } else { + spdlog::error("Unknown shader type: {}", type); + abort(); + } +} + +} // namespace iris \ No newline at end of file diff --git a/src/shader.h b/src/shader.h new file mode 100644 index 0000000..b1be8a8 --- /dev/null +++ b/src/shader.h @@ -0,0 +1,33 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace iris { + +struct ShaderDesc { + // Name of the shader, filename if loaded from file + std::string name; + // Source code of the shader + std::string source; + + // Shader type + enum struct Type { + eRayGen, + eMiss, + eClosestHit, + eAnyHit, + eIntersection, + eCompute, + } type; + + // Compiled binary + std::vector compiled_binary; + + static Type shader_type_from_string(std::string_view type); + ShaderDesc(const std::string_view path); +}; + +} // namespace iris \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index eb1d750..41b1d0e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,5 +1,6 @@ add_rules("mode.debug", "mode.release") -- add_requires("vulkansdk", "glfw", "fmt", "vulkan-memory-allocator", "spdlog", "glm") +add_requires("shaderc") -- Project settings set_project("iris_renderer") @@ -70,7 +71,7 @@ target("iris_renderer") add_deps("argparse", "imgui", "tinygltf", "tinyobjloader", "stb", "glm") -- Add libraries - add_packages("vulkansdk", "glfw", "fmt", "vulkan-memory-allocator", "spdlog", "glm") + add_packages("vulkansdk", "glfw", "fmt", "vulkan-memory-allocator", "spdlog", "glm", "shaderc") -- OS-specific libraries (dl, pthread, X11, etc.) if is_plat("linux") then -- cgit v1.2.3-70-g09d2