ShaderProgram.hpp 1.95 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_cross.hpp>
14
#include "vkcv/VertexLayout.hpp"
15
16
17

namespace vkcv {

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

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

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

        /**
        * 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
47
        bool addShader(ShaderStage shaderStage, const std::filesystem::path &shaderPath);
48
49

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

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

58
59
        void reflectShader(ShaderStage shaderStage);

60
        const VertexLayout &getVertexLayout() const;
61

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

65
        VertexLayout m_VertexLayout;
66
67
	};
}