-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComputeShaderProgram.hpp
executable file
·145 lines (121 loc) · 5.38 KB
/
ComputeShaderProgram.hpp
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
/**
* @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() final = 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 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]] virtual void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) final;
};
}
////////////////////////////////////////////////////////////////////////////////
inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) {
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");
// stores shader program handle
GLuint computeShaderHandle;
// compile each one of our shaders
if( strcmp( computeShaderFilename, "" ) != 0 ) {
if( sDEBUG ) printf( "[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
computeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
} else {
computeShaderHandle = 0;
}
// get a handle to a shader program
mShaderProgramHandle = glCreateProgram();
// attach the computer fragment shader to the shader program
if(computeShaderHandle != 0 ) {
glAttachShader(mShaderProgramHandle, computeShaderHandle );
}
// 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(computeShaderHandle != 0 ) {
glDetachShader(mShaderProgramHandle, computeShaderHandle );
glDeleteShader(computeShaderHandle );
}
// map uniforms
mpUniformLocationsMap = new std::map<std::string, GLint>();
GLint numUniforms;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
if( numUniforms > 0 ) {
for(GLuint i = 0; i < numUniforms; i++) {
char name[64];
int max_length = 64;
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform(mShaderProgramHandle, i, max_length, &actual_length, &size, &type, name );
GLint location = -1;
if(size > 1) {
for(int j = 0; j < size; j++) {
char long_name[64];
sprintf(long_name, "%s[%i]", name, j);
location = glGetUniformLocation(mShaderProgramHandle, long_name);
}
} else {
location = glGetUniformLocation(mShaderProgramHandle, name);
}
mpUniformLocationsMap->emplace(name, location );
}
}
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,
computeShaderHandle != 0, true);
}
}
[[maybe_unused]]
inline void CSCI441::ComputeShaderProgram::dispatchWork(GLuint numGroupsX = 1, GLuint numGroupsY = 1, GLuint numGroupsZ = 1) {
glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
}
#endif // CSCI441_COMPUTE_SHADER_PROGRAM_HPP