CSCI441 OpenGL Library 5.23.0.0
CS@Mines CSCI441 Computer Graphics Course Library
Loading...
Searching...
No Matches
ComputeShaderProgram.hpp
Go to the documentation of this file.
1
12#ifndef CSCI441_COMPUTE_SHADER_PROGRAM_HPP
13#define CSCI441_COMPUTE_SHADER_PROGRAM_HPP
14
15#include "ShaderProgram.hpp"
16
18
19namespace CSCI441 {
20
24 class ComputeShaderProgram final : public ShaderProgram {
25 public:
30 explicit ComputeShaderProgram( const char *computeShaderFilename );
31
35 ~ComputeShaderProgram() override = default;
36
45
56 ComputeShaderProgram& operator=(ComputeShaderProgram&&) noexcept = default;
57
65 [[maybe_unused]] void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) const;
66
71 [[maybe_unused]] [[nodiscard]] GLbitfield getProgramStages() const override;
72
73 protected:
78 };
79
80}
81
83
84inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) : mComputeShaderHandle(0) {
85 GLint major, minor;
86 glGetIntegerv(GL_MAJOR_VERSION, &major);
87 glGetIntegerv(GL_MINOR_VERSION, &minor);
88
89 if(major < 4 || (major == 4 && minor < 3)) {
90 fprintf(stderr, "[ERROR]: Compute Shaders only supported in OpenGL 4.3+\n");
91 return;
92 }
93
94 if( sDEBUG ) printf( "\n[INFO]: /--------------------------------------------------------\\\n");
95
96 // compile each one of our shaders
97 if( strcmp( computeShaderFilename, "" ) != 0 ) {
98 if( sDEBUG ) printf( "[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
99 mComputeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
100 } else {
102 }
103
104 // get a handle to a shader program
105 mShaderProgramHandle = glCreateProgram();
106
107 // attach the computer fragment shader to the shader program
108 if(mComputeShaderHandle != 0 ) {
110 }
111
112 // link all the programs together on the GPU
113 glLinkProgram(mShaderProgramHandle );
114
115 if( sDEBUG ) printf( "[INFO]: | Shader Program: %41s", "|\n" );
116
117 // check the program log
118 CSCI441_INTERNAL::ShaderUtils::printProgramLog(mShaderProgramHandle );
119
120 // detach & delete the vertex and fragment shaders to the shader program
121 if(mComputeShaderHandle != 0 ) {
123 glDeleteShader(mComputeShaderHandle );
124 }
125
126 // map uniforms
127 mpUniformLocationsMap = new std::map<std::string, GLint>();
128 GLint numUniforms;
129 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
130 GLint max_uniform_name_size;
131 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);
132 if( numUniforms > 0 ) {
133 for(GLuint i = 0; i < numUniforms; i++) {
134 char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
135 int actual_length = 0;
136 int size = 0;
137 GLenum type;
138 glGetActiveUniform(mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
139 GLint location = -1;
140 if(size > 1) {
141 for(int j = 0; j < size; j++) {
142 int max_array_size = actual_length + 4 + 2 + 1;
143 char* array_name = (char*) malloc(max_array_size * sizeof(char));
144 snprintf(array_name, max_array_size, "%s[%i]", name, j);
145 location = glGetUniformLocation(mShaderProgramHandle, array_name);
146 mpUniformLocationsMap->emplace(array_name, location);
147 free(array_name);
148 }
149 } else {
150 location = glGetUniformLocation(mShaderProgramHandle, name);
151 mpUniformLocationsMap->emplace(name, location);
152 }
153 free(name);
154 }
155 }
156 GLint linkStatus;
157 glGetProgramiv(mShaderProgramHandle, GL_LINK_STATUS, &linkStatus );
158
159 /* print shader info for uniforms & attributes */
160 if(linkStatus == 1) {
161 // print shader info for uniforms & attributes
162 CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(mShaderProgramHandle, false, false, false, false, false,
163 mComputeShaderHandle != 0, true);
164 }
165}
166
167[[maybe_unused]]
168inline void CSCI441::ComputeShaderProgram::dispatchWork(const GLuint numGroupsX = 1, const GLuint numGroupsY = 1, const GLuint numGroupsZ = 1) const {
169 glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
170}
171
172[[maybe_unused]]
174 GLbitfield shaderBits = 0;
175 if( mComputeShaderHandle != 0 ) shaderBits |= GL_COMPUTE_SHADER_BIT;
176 return shaderBits;
177}
178
179#endif // CSCI441_COMPUTE_SHADER_PROGRAM_HPP
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Compute Shaders.
Definition: ComputeShaderProgram.hpp:24
ComputeShaderProgram & operator=(const ComputeShaderProgram &)=delete
do not allow shader programs to be copied
GLuint mComputeShaderHandle
compute shader handle
Definition: ComputeShaderProgram.hpp:77
ComputeShaderProgram(ComputeShaderProgram &&) noexcept=default
construct a new compute shader program by moving an existing computer shader program
void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) const
dispatches work to the Compute Shader on the GPU
Definition: ComputeShaderProgram.hpp:168
ComputeShaderProgram(const ComputeShaderProgram &)=delete
do not allow shader programs to be copied
GLbitfield getProgramStages() const override
returns a single value corresponding to which shader stages are present in this shader program
Definition: ComputeShaderProgram.hpp:173
~ComputeShaderProgram() override=default
Clean up memory associated with the Compute Shader Program.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:36
GLuint mShaderProgramHandle
handle to the shader program
Definition: ShaderProgram.hpp:896
std::map< std::string, GLint > * mpUniformLocationsMap
caches locations of uniform names within shader program
Definition: ShaderProgram.hpp:901
static bool sDEBUG
if DEBUG information should be printed or not
Definition: ShaderProgram.hpp:870
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17