CSCI441 OpenGL Library 6.1.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 "LogUtils.hpp"
16#include "ShaderProgram.hpp"
17
19
20namespace CSCI441 {
21
25 class ComputeShaderProgram final : public ShaderProgram {
26 public:
31 explicit ComputeShaderProgram( const char *computeShaderFilename );
32
36 ~ComputeShaderProgram() override = default;
37
46
57 ComputeShaderProgram& operator=(ComputeShaderProgram&&) noexcept = default;
58
66 [[maybe_unused]] void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) const;
67
72 [[maybe_unused]] [[nodiscard]] GLbitfield getProgramStages() const override;
73
74 protected:
79 };
80
81}
82
84
85inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) : mComputeShaderHandle(0) {
86 GLint major, minor;
87 glGetIntegerv(GL_MAJOR_VERSION, &major);
88 glGetIntegerv(GL_MINOR_VERSION, &minor);
89
90 if(major < 4 || (major == 4 && minor < 3)) {
91 CSCI441::LogUtils::logError("[ERROR]: Compute Shaders only supported in OpenGL 4.3+\n");
92 return;
93 }
94
95 if( sDEBUG ) CSCI441::LogUtils::log("\n[INFO]: /--------------------------------------------------------\\\n");
96
97 // compile each one of our shaders
98 if( strcmp( computeShaderFilename, "" ) != 0 ) {
99 if( sDEBUG ) CSCI441::LogUtils::log("[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
100 mComputeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
101 } else {
103 }
104
105 // get a handle to a shader program
106 mShaderProgramHandle = glCreateProgram();
107
108 // attach the computer fragment shader to the shader program
109 if(mComputeShaderHandle != 0 ) {
111 }
112
113 // link all the programs together on the GPU
114 glLinkProgram(mShaderProgramHandle );
115
116 if( sDEBUG ) CSCI441::LogUtils::log("[INFO]: | Shader Program: %41s", "|\n" );
117
118 // check the program log
119 CSCI441_INTERNAL::ShaderUtils::printProgramLog(mShaderProgramHandle );
120
121 // detach & delete the vertex and fragment shaders to the shader program
122 if(mComputeShaderHandle != 0 ) {
124 glDeleteShader(mComputeShaderHandle );
125 }
126
127 // map uniforms
128 mpUniformLocationsMap = new std::map<std::string, GLint>();
129 GLint numUniforms;
130 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
131 GLint max_uniform_name_size;
132 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);
133 if( numUniforms > 0 ) {
134 for(GLuint i = 0; i < numUniforms; i++) {
135 char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
136 int actual_length = 0;
137 int size = 0;
138 GLenum type;
139 glGetActiveUniform(mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
140 GLint location = -1;
141 if(size > 1) {
142 for(int j = 0; j < size; j++) {
143 int max_array_size = actual_length + 4 + 2 + 1;
144 char* array_name = (char*) malloc(max_array_size * sizeof(char));
145 snprintf(array_name, max_array_size, "%s[%i]", name, j);
146 location = glGetUniformLocation(mShaderProgramHandle, array_name);
147 mpUniformLocationsMap->emplace(array_name, location);
148 free(array_name);
149 }
150 } else {
151 location = glGetUniformLocation(mShaderProgramHandle, name);
152 mpUniformLocationsMap->emplace(name, location);
153 }
154 free(name);
155 }
156 }
157 GLint linkStatus;
158 glGetProgramiv(mShaderProgramHandle, GL_LINK_STATUS, &linkStatus );
159
160 /* print shader info for uniforms & attributes */
161 if(linkStatus == 1) {
162 // print shader info for uniforms & attributes
163 CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(mShaderProgramHandle, false, false, false, false, false,
164 mComputeShaderHandle != 0, true);
165 }
166}
167
168[[maybe_unused]]
169inline void CSCI441::ComputeShaderProgram::dispatchWork(const GLuint numGroupsX = 1, const GLuint numGroupsY = 1, const GLuint numGroupsZ = 1) const {
170 glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
171}
172
173[[maybe_unused]]
175 GLbitfield shaderBits = 0;
176 if( mComputeShaderHandle != 0 ) shaderBits |= GL_COMPUTE_SHADER_BIT;
177 return shaderBits;
178}
179
180#endif // CSCI441_COMPUTE_SHADER_PROGRAM_HPP
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Compute Shaders.
Definition: ComputeShaderProgram.hpp:25
ComputeShaderProgram & operator=(const ComputeShaderProgram &)=delete
do not allow shader programs to be copied
GLuint mComputeShaderHandle
compute shader handle
Definition: ComputeShaderProgram.hpp:78
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:169
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:174
~ComputeShaderProgram() override=default
Clean up memory associated with the Compute Shader Program.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:37
GLuint mShaderProgramHandle
handle to the shader program
Definition: ShaderProgram.hpp:897
std::map< std::string, GLint > * mpUniformLocationsMap
caches locations of uniform names within shader program
Definition: ShaderProgram.hpp:902
static bool sDEBUG
if DEBUG information should be printed or not
Definition: ShaderProgram.hpp:871
void log(const char *MSG,...)
log a message to both the standard output stream and file
Definition: LogUtils.hpp:116
void logError(const char *MSG,...)
log a message to both the standard error stream and file
Definition: LogUtils.hpp:128
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17