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

#define GLFW_INCLUDE_VULKAN
#include <vector>
10
11
#include <fstream>
#include <iostream>
12
13
14
15
16
#include <vulkan/vulkan.hpp>

namespace vkcv {

	class ShaderProgram final {
17
18
    public:

19
20
21
22
23
        enum class ShaderStage {
            VERTEX,
            FRAGMENT,
            COMPUTE
        };
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44


        /**
        * destructor of ShaderProgram, does nothing so far
        */
        ~ShaderProgram();

        /**
        * Creates a shader program.
        * So far it only calls the constructor.
        * @param[in] context of the app
        */
        static ShaderProgram create();

        /**
        * 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")
        */
45
        void addShader(ShaderProgram::ShaderStage shaderStage, const std::string& filepath);
46
47
48
49
50
51

        /**
        * Tests if the shader program contains a certain shader stage.
        * @param[in] flag that signals the respective shader stage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
        * @return boolean that is true if the shader program contains the shader stage
        */
52
        bool containsShaderStage(ShaderProgram::ShaderStage shaderStage) const;
53
54
55
56
57
58

        /**
        * Deletes the given shader stage in the shader program.
        * @param[in] flag that signals the respective shader stage (e.g. VK_SHADER_STAGE_VERTEX_BIT)
        * @return boolean that is false if the shader stage was not found in the shader program
        */
59
        bool deleteShaderStage(ShaderProgram::ShaderStage shaderStage);
60
61
62
63
64
65

        /**
        * Returns a list with all the shader stages in the shader program.
        * Needed for the transfer to the pipeline.
        * @return vector list with all shader stage info structs
        */
66
        std::vector<vk::ShaderStageFlagBits> getShaderStages() const;
67
68
69
70
71
72

        /**
        * Returns a list with all the shader code in the shader program.
        * Needed for the transfer to the pipeline.
        * @return vector list with all shader code char vecs
        */
73
        std::vector<std::vector<char>> getShaderCode() const;
74
75
76
77
78
79

        /**
        * Returns the number of shader stages in the shader program.
        * Needed for the transfer to the pipeline.
        * @return integer with the number of stages
        */
80
        int getShaderStagesCount() const;
81
82
83



84
85
	private:

86
87
88
89
90
91
92
	    struct ShaderStages {
            std::vector<std::vector<char>> shaderCode;
            std::vector<vk::ShaderStageFlagBits> shaderStageFlag;
	    };

		ShaderStages m_shaderStages;

93
94
95
96
		/**
		* Constructor of ShaderProgram requires a context for the logical device. 
		* @param context of the app
		*/
97
		ShaderProgram();
98
99
100
101
102
103
104
105
106
		
		/**
		* Reads the file of a given shader code. 
		* Only used within the class. 
		* @param[in] relative path to the shader code 
		* @return vector of chars as a buffer for the code 
		*/
		std::vector<char> readFile(const std::string& filepath);

107
108
109
110
111
        /**
		* Converts ShaderStage Enum into vk::ShaderStageFlagBits
		* @param[in] ShaderStage enum
		* @return vk::ShaderStageFlagBits
		*/
112
        vk::ShaderStageFlagBits convertToShaderStageFlagBits(ShaderProgram::ShaderStage shaderStage) const;
113

114
115
116
117
118
119
120
		/**
		* Creates a shader module that encapsulates the read shader code. 
		* Only used within the class. 
		* Shader modules are destroyed after respective shader stages are created. 
		* @param[in] a vector of chars as a buffer for the code 
		* @return shader module 
		*/
121
		//vk::ShaderModule createShaderModule(const std::vector<char>& shaderCode); -> Core
122
123
124
125
126
127
128
129

		/**
		* Creates a shader stage (info struct) for the to be added shader. 
		* Only used within the class. 
		* @param[in] Shader module that encapsulates the shader code 
		* @param[in] flag that signals the respective shaderStage 
		* @return pipeline shader stage info struct 
		*/ 
130
		//vk::PipelineShaderStageCreateInfo createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage); -> Core
131
132
133

	};
}