-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComputeShaderProgram.hpp
More file actions
executable file
·179 lines (151 loc) · 7.09 KB
/
ComputeShaderProgram.hpp
File metadata and controls
executable file
·179 lines (151 loc) · 7.09 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file ComputeShaderProgram.hpp
* @brief Class to work with OpenGL Compute Shaders
* @author Dr. Jeffrey Paone
*
* @copyright MIT License Copyright (c) 2021 Dr. Jeffrey Paone
*
* These functions, classes, and constants help minimize common
* code that needs to be written.
*/
#ifndef CSCI441_COMPUTE_SHADER_PROGRAM_HPP
#define CSCI441_COMPUTE_SHADER_PROGRAM_HPP
#include "ShaderProgram.hpp"
////////////////////////////////////////////////////////////////////////////////
namespace CSCI441 {
/**
* @brief Handles registration and compilation of Compute Shaders
*/
class ComputeShaderProgram final : public ShaderProgram {
public:
/**
* @brief Creates a Compute Shader Program
* @param computeShaderFilename name of the file corresponding to the compute shader
*/
explicit ComputeShaderProgram( const char *computeShaderFilename );
/**
* @brief Clean up memory associated with the Compute Shader Program
*/
~ComputeShaderProgram() override = default;
/**
* @brief do not allow shader programs to be copied
*/
ComputeShaderProgram(const ComputeShaderProgram&) = delete;
/**
* @brief do not allow shader programs to be copied
*/
ComputeShaderProgram& operator=(const ComputeShaderProgram&) = delete;
/**
* @brief construct a new compute shader program by moving an existing computer shader program
* @note uses default implementation which is to call the parent CSCI441::ShaderProgram implementation
*/
ComputeShaderProgram(ComputeShaderProgram&&) noexcept = default;
/**
* @brief assign this compute shader program by moving an existing computer shader program
* @return our newly assigned computer shader program
* @note uses default implementation which is to call the parent CSCI441::ShaderProgram implementation
*/
ComputeShaderProgram& operator=(ComputeShaderProgram&&) noexcept = default;
/**
* @brief dispatches work to the Compute Shader on the GPU
* @param numGroupsX number of work groups in X dimension (defaults to 1)
* @param numGroupsY number of work groups in Y dimension (defaults to 1)
* @param numGroupsZ number of work groups in Z dimension (defaults to 1)
* @note call after calling ShaderProgram::useProgram()
*/
[[maybe_unused]] void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) const;
/**
* @brief returns a single value corresponding to which shader stages are present in this shader program
* @return bitfield of shader stages
*/
[[maybe_unused]] [[nodiscard]] GLbitfield getProgramStages() const override;
protected:
/**
* @brief compute shader handle
*/
GLuint mComputeShaderHandle;
};
}
////////////////////////////////////////////////////////////////////////////////
inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) : mComputeShaderHandle(0) {
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
if(major < 4 || (major == 4 && minor < 3)) {
fprintf(stderr, "[ERROR]: Compute Shaders only supported in OpenGL 4.3+\n");
return;
}
if( sDEBUG ) printf( "\n[INFO]: /--------------------------------------------------------\\\n");
// compile each one of our shaders
if( strcmp( computeShaderFilename, "" ) != 0 ) {
if( sDEBUG ) printf( "[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
mComputeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
} else {
mComputeShaderHandle = 0;
}
// get a handle to a shader program
mShaderProgramHandle = glCreateProgram();
// attach the computer fragment shader to the shader program
if(mComputeShaderHandle != 0 ) {
glAttachShader(mShaderProgramHandle, mComputeShaderHandle );
}
// link all the programs together on the GPU
glLinkProgram(mShaderProgramHandle );
if( sDEBUG ) printf( "[INFO]: | Shader Program: %41s", "|\n" );
// check the program log
CSCI441_INTERNAL::ShaderUtils::printProgramLog(mShaderProgramHandle );
// detach & delete the vertex and fragment shaders to the shader program
if(mComputeShaderHandle != 0 ) {
glDetachShader(mShaderProgramHandle, mComputeShaderHandle );
glDeleteShader(mComputeShaderHandle );
}
// map uniforms
mpUniformLocationsMap = new std::map<std::string, GLint>();
GLint numUniforms;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
GLint max_uniform_name_size;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);
if( numUniforms > 0 ) {
for(GLuint i = 0; i < numUniforms; i++) {
char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform(mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
GLint location = -1;
if(size > 1) {
for(int j = 0; j < size; j++) {
int max_array_size = actual_length + 4 + 2 + 1;
char* array_name = (char*) malloc(max_array_size * sizeof(char));
snprintf(array_name, max_array_size, "%s[%i]", name, j);
location = glGetUniformLocation(mShaderProgramHandle, array_name);
mpUniformLocationsMap->emplace(array_name, location);
free(array_name);
}
} else {
location = glGetUniformLocation(mShaderProgramHandle, name);
mpUniformLocationsMap->emplace(name, location);
}
free(name);
}
}
GLint linkStatus;
glGetProgramiv(mShaderProgramHandle, GL_LINK_STATUS, &linkStatus );
/* print shader info for uniforms & attributes */
if(linkStatus == 1) {
// print shader info for uniforms & attributes
CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(mShaderProgramHandle, false, false, false, false, false,
mComputeShaderHandle != 0, true);
}
}
[[maybe_unused]]
inline void CSCI441::ComputeShaderProgram::dispatchWork(const GLuint numGroupsX = 1, const GLuint numGroupsY = 1, const GLuint numGroupsZ = 1) const {
glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
}
[[maybe_unused]]
inline GLbitfield CSCI441::ComputeShaderProgram::getProgramStages() const {
GLbitfield shaderBits = 0;
if( mComputeShaderHandle != 0 ) shaderBits |= GL_COMPUTE_SHADER_BIT;
return shaderBits;
}
#endif // CSCI441_COMPUTE_SHADER_PROGRAM_HPP