ShaderProgram.hpp 2.18 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
#include "vkcv/DescriptorSetLayout.hpp"
16
17
18

namespace vkcv {

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

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

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

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

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

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

59
60
        void reflectShader(ShaderStage shaderStage);

61
        const VertexLayout &getVertexLayout() const;
62
		size_t getPushConstantSize() const;
63

64
65
        const DescriptorSetLayout &getDescriptorSetLayout() const;

66
	private:
Artur Wasmut's avatar
Artur Wasmut committed
67
        std::unordered_map<ShaderStage, Shader> m_Shaders;
68

69
        VertexLayout m_VertexLayout;
70
        DescriptorSetLayout m_DescriptorSetLayout;
71
		size_t m_pushConstantSize = 0;
72
73
	};
}