ShaderProgram.hpp 3.55 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
17
18
19
20
#include <vulkan/vulkan.hpp>
#include "Context.hpp"

namespace vkcv {

	class ShaderProgram final {
	private:

		vkcv::Context& m_context;
21
		std::vector<vk::PipelineShaderStageCreateInfo> m_shaderStagesList;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
		/**
		* 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 
		*/
43
		vk::ShaderModule createShaderModule(const std::vector<char>& shaderCode);
44
45
46
47
48
49
50
51

		/**
		* 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 
		*/ 
52
		vk::PipelineShaderStageCreateInfo createShaderStage(vk::ShaderModule& shaderModule, vk::ShaderStageFlagBits shaderStage);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


	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")
		*/
76
		void addShader(vk::ShaderStageFlagBits shaderStage, const std::string& filepath);
77
78
79
80
81
82

		/**
		* 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
		*/
83
		bool containsShaderStage(vk::ShaderStageFlagBits shaderStage);
84
85
86
87
88
89

		/**
		* 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
		*/
90
		bool deleteShaderStage(vk::ShaderStageFlagBits shaderStage);
91
92
93
94
95
96

		/**
		* 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
		*/
97
		std::vector<vk::PipelineShaderStageCreateInfo> getShaderStages();
98
99
100
101
102
103
104
105
106
107
108

		/**
		* 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();


	};
}