summaryrefslogtreecommitdiff
path: root/src/shader.cpp
blob: 95a79041570cf09b4ed77203d36c27874e0c0d5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "shader.h"
#include <spdlog/spdlog.h>
#include <fstream>
#include <sstream>
#include <shaderc/shaderc.hpp>

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<uint32_t>(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