ShaderProgram.hpp 3.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
/**
 * @authors Simeon Hermann
 * @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>
#include <map>
#include <vulkan/vulkan.hpp>
#include "Context.hpp"

namespace vkcv {

	class ShaderProgram final {
	private:

		vkcv::Context& m_context;
		std::vector<VkPipelineShaderStageCreateInfo> m_shaderStagesList;
		/**
		* Constructor of ShaderProgram requires a context for the logical device. 
		* @param context of the app
		*/
		ShaderProgram(vkcv::Context& context);
		
		/**
		* 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);

		/**
		* 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 
		*/
		VkShaderModule createShaderModule(const std::vector<char>& shaderCode);

		/**
		* 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 
		*/ 
		VkPipelineShaderStageCreateInfo createShaderStage(VkShaderModule& shaderModule, VkShaderStageFlagBits shaderStage);


	public:

		/**
		* 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(vkcv::Context& context);

		/**
		* 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")
		*/
		void addShader(VkShaderStageFlagBits shaderStage, const std::string& filepath);

		/**
		* 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
		*/
		bool containsShaderStage(VkShaderStageFlagBits shaderStage);

		/**
		* 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
		*/
		bool deleteShaderStage(VkShaderStageFlagBits shaderStage);

		/**
		* 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
		*/
		std::vector<VkPipelineShaderStageCreateInfo> getShaderStages();

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


	};
}