CSCI441 OpenGL Library 5.13.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);
66 };
67}
68
70
71inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) {
72 GLint major, minor;
73 glGetIntegerv(GL_MAJOR_VERSION, &major);
74 glGetIntegerv(GL_MINOR_VERSION, &minor);
75
76 if(major < 4 || (major == 4 && minor < 3)) {
77 fprintf(stderr, "[ERROR]: Compute Shaders only supported in OpenGL 4.3+\n");
78 return;
79 }
80
81 if( sDEBUG ) printf( "\n[INFO]: /--------------------------------------------------------\\\n");
82
83 // stores shader program handle
84 GLuint computeShaderHandle;
85
86 // compile each one of our shaders
87 if( strcmp( computeShaderFilename, "" ) != 0 ) {
88 if( sDEBUG ) printf( "[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
89 computeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
90 } else {
91 computeShaderHandle = 0;
92 }
93
94 // get a handle to a shader program
95 mShaderProgramHandle = glCreateProgram();
96
97 // attach the computer fragment shader to the shader program
98 if(computeShaderHandle != 0 ) {
99 glAttachShader(mShaderProgramHandle, computeShaderHandle );
100 }
101
102 // link all the programs together on the GPU
103 glLinkProgram(mShaderProgramHandle );
104
105 if( sDEBUG ) printf( "[INFO]: | Shader Program: %41s", "|\n" );
106
107 // check the program log
108 CSCI441_INTERNAL::ShaderUtils::printProgramLog(mShaderProgramHandle );
109
110 // detach & delete the vertex and fragment shaders to the shader program
111 if(computeShaderHandle != 0 ) {
112 glDetachShader(mShaderProgramHandle, computeShaderHandle );
113 glDeleteShader(computeShaderHandle );
114 }
115
116 // map uniforms
117 mpUniformLocationsMap = new std::map<std::string, GLint>();
118 GLint numUniforms;
119 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
120 GLint max_uniform_name_size;
121 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);
122 if( numUniforms > 0 ) {
123 for(GLuint i = 0; i < numUniforms; i++) {
124 char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
125 int actual_length = 0;
126 int size = 0;
127 GLenum type;
128 glGetActiveUniform(mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
129 GLint location = -1;
130 if(size > 1) {
131 for(int j = 0; j < size; j++) {
132 int max_array_size = actual_length + 4 + 2 + 1;
133 char* array_name = (char*) malloc(max_array_size * sizeof(char));
134 snprintf(array_name, max_array_size, "%s[%i]", name, j);
135 location = glGetUniformLocation(mShaderProgramHandle, array_name);
136 mpUniformLocationsMap->emplace(array_name, location);
137 free(array_name);
138 }
139 } else {
140 location = glGetUniformLocation(mShaderProgramHandle, name);
141 mpUniformLocationsMap->emplace(name, location);
142 }
143 free(name);
144 }
145 }
146 GLint linkStatus;
147 glGetProgramiv(mShaderProgramHandle, GL_LINK_STATUS, &linkStatus );
148
149 /* print shader info for uniforms & attributes */
150 if(linkStatus == 1) {
151 // print shader info for uniforms & attributes
152 CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(mShaderProgramHandle, false, false, false, false, false,
153 computeShaderHandle != 0, true);
154 }
155}
156
157[[maybe_unused]]
158inline void CSCI441::ComputeShaderProgram::dispatchWork(const GLuint numGroupsX = 1, const GLuint numGroupsY = 1, const GLuint numGroupsZ = 1) {
159 glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
160}
161
162#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
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)
dispatches work to the Compute Shader on the GPU
Definition: ComputeShaderProgram.hpp:158
ComputeShaderProgram(const ComputeShaderProgram &)=delete
do not allow shader programs to be copied
~ComputeShaderProgram() override=default
Clean up memory associated with the Compute Shader Program.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:35
GLuint mShaderProgramHandle
handle to the shader program
Definition: ShaderProgram.hpp:875
std::map< std::string, GLint > * mpUniformLocationsMap
caches locations of uniform names within shader program
Definition: ShaderProgram.hpp:880
static bool sDEBUG
if DEBUG information should be printed or not
Definition: ShaderProgram.hpp:849
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17