ShaderProgram.hpp 1.84 KB
Newer Older
1
2
#pragma once
/**
3
 * @authors Simeon Hermann, Leonie Franken
4
5
6
7
 * @file src/vkcv/ShaderProgram.hpp
 * @brief ShaderProgram class to handle and prepare the shader stages for a graphics pipeline
 */

Artur Wasmut's avatar
Artur Wasmut committed
8
#include <unordered_map>
9
10
#include <fstream>
#include <iostream>
Artur Wasmut's avatar
Artur Wasmut committed
11
#include <filesystem>
12
#include <vulkan/vulkan.hpp>
13
#include <spirv_glsl.hpp>
14
15
16

namespace vkcv {

Artur Wasmut's avatar
Artur Wasmut committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    enum class ShaderStage
    {
        VERTEX,
        TESS_CONTROL,
        TESS_EVAL,
        GEOMETRY,
        FRAGMENT,
        COMPUTE
    };

    struct Shader
    {
        std::vector<char> shaderCode;
        ShaderStage shaderStage;
    };

	class ShaderProgram
	{
35
    public:
Artur Wasmut's avatar
Artur Wasmut committed
36
37
        ShaderProgram() noexcept; // ctor
        ~ShaderProgram() = default; // dtor
38
39
40
41
42
43
44
45

        /**
        * Adds a shader into the shader program.
        * The shader is only added if the shader program does not contain the particular shader stage already.
        * Contains: (1) reading of the code, (2) creation of a shader module, (3) creation of a shader stage, (4) adding to the shader stage list, (5) destroying of the shader module
        * @param[in] flag that signals the respective shaderStage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
        * @param[in] relative path to the shader code (e.g. "../../../../../shaders/vert.spv")
        */
Artur Wasmut's avatar
Artur Wasmut committed
46
        bool addShader(ShaderStage shaderStage, const std::filesystem::path &shaderPath);
47
48

        /**
Artur Wasmut's avatar
Artur Wasmut committed
49
        * Returns the shader program's shader of the specified shader.
50
        * Needed for the transfer to the pipeline.
Artur Wasmut's avatar
Artur Wasmut committed
51
        * @return Shader object consisting of buffer with shader code and shader stage enum
52
        */
Artur Wasmut's avatar
Artur Wasmut committed
53
        const Shader &getShader(ShaderStage shaderStage) const;
54

Artur Wasmut's avatar
Artur Wasmut committed
55
        bool existsShader(ShaderStage shaderStage) const;
56

57
58
        void reflectShader(ShaderStage shaderStage) const;

59
	private:
Artur Wasmut's avatar
Artur Wasmut committed
60
        std::unordered_map<ShaderStage, Shader> m_Shaders;
61
62
63

	};
}