ShaderProgram.hpp 2.12 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>
11
#include <algorithm>
Artur Wasmut's avatar
Artur Wasmut committed
12
#include <filesystem>
13
#include <vulkan/vulkan.hpp>
14
#include <spirv_cross.hpp>
15
#include "vkcv/VertexLayout.hpp"
16
#include "vkcv/ShaderStage.hpp"
17
#include "vkcv/DescriptorConfig.hpp"
18
19
20

namespace vkcv {

Artur Wasmut's avatar
Artur Wasmut committed
21
22
23
24
25
26
27
28
    struct Shader
    {
        std::vector<char> shaderCode;
        ShaderStage shaderStage;
    };

	class ShaderProgram
	{
29
    public:
Artur Wasmut's avatar
Artur Wasmut committed
30
31
        ShaderProgram() noexcept; // ctor
        ~ShaderProgram() = default; // dtor
32
33
34
35
36
37
38
39

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

        /**
Artur Wasmut's avatar
Artur Wasmut committed
43
        * Returns the shader program's shader of the specified shader.
44
        * Needed for the transfer to the pipeline.
Artur Wasmut's avatar
Artur Wasmut committed
45
        * @return Shader object consisting of buffer with shader code and shader stage enum
46
        */
Artur Wasmut's avatar
Artur Wasmut committed
47
        const Shader &getShader(ShaderStage shaderStage) const;
48

Artur Wasmut's avatar
Artur Wasmut committed
49
        bool existsShader(ShaderStage shaderStage) const;
50

51
52
        void reflectShader(ShaderStage shaderStage);

53
        const VertexLayout &getVertexLayout() const;
54
		size_t getPushConstantSize() const;
55

56
        const std::vector<std::vector<DescriptorBinding>> getReflectedDescriptors() const;
57

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

61
        VertexLayout m_VertexLayout;
62
        std::vector<std::vector<DescriptorBinding>> m_DescriptorSets;
63
		size_t m_pushConstantSize = 0;
64
65
	};
}