CSCI441 OpenGL Library 5.9.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() final = default;
36
44 ComputeShaderProgram& operator=(const ComputeShaderProgram&) = delete;
45
53 [[maybe_unused]] virtual void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) final;
54 };
55}
56
58
59inline CSCI441::ComputeShaderProgram::ComputeShaderProgram( const char *computeShaderFilename ) {
60 GLint major, minor;
61 glGetIntegerv(GL_MAJOR_VERSION, &major);
62 glGetIntegerv(GL_MINOR_VERSION, &minor);
63
64 if(major < 4 || (major == 4 && minor < 3)) {
65 fprintf(stderr, "[ERROR]: Compute Shaders only supported in OpenGL 4.3+\n");
66 return;
67 }
68
69 if( sDEBUG ) printf( "\n[INFO]: /--------------------------------------------------------\\\n");
70
71 // stores shader program handle
72 GLuint computeShaderHandle;
73
74 // compile each one of our shaders
75 if( strcmp( computeShaderFilename, "" ) != 0 ) {
76 if( sDEBUG ) printf( "[INFO]: | Compute Shader: %38s |\n", computeShaderFilename );
77 computeShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(computeShaderFilename, GL_COMPUTE_SHADER );
78 } else {
79 computeShaderHandle = 0;
80 }
81
82 // get a handle to a shader program
83 mShaderProgramHandle = glCreateProgram();
84
85 // attach the computer fragment shader to the shader program
86 if(computeShaderHandle != 0 ) {
87 glAttachShader(mShaderProgramHandle, computeShaderHandle );
88 }
89
90 // link all the programs together on the GPU
91 glLinkProgram(mShaderProgramHandle );
92
93 if( sDEBUG ) printf( "[INFO]: | Shader Program: %41s", "|\n" );
94
95 // check the program log
96 CSCI441_INTERNAL::ShaderUtils::printProgramLog(mShaderProgramHandle );
97
98 // detach & delete the vertex and fragment shaders to the shader program
99 if(computeShaderHandle != 0 ) {
100 glDetachShader(mShaderProgramHandle, computeShaderHandle );
101 glDeleteShader(computeShaderHandle );
102 }
103
104 // map uniforms
105 mpUniformLocationsMap = new std::map<std::string, GLint>();
106 GLint numUniforms;
107 glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
108 if( numUniforms > 0 ) {
109 for(GLuint i = 0; i < numUniforms; i++) {
110 char name[64];
111 int max_length = 64;
112 int actual_length = 0;
113 int size = 0;
114 GLenum type;
115 glGetActiveUniform(mShaderProgramHandle, i, max_length, &actual_length, &size, &type, name );
116 GLint location = -1;
117 if(size > 1) {
118 for(int j = 0; j < size; j++) {
119 char long_name[64];
120 sprintf(long_name, "%s[%i]", name, j);
121 location = glGetUniformLocation(mShaderProgramHandle, long_name);
122 }
123 } else {
124 location = glGetUniformLocation(mShaderProgramHandle, name);
125 }
126 mpUniformLocationsMap->emplace(name, location );
127 }
128 }
129 GLint linkStatus;
130 glGetProgramiv(mShaderProgramHandle, GL_LINK_STATUS, &linkStatus );
131
132 /* print shader info for uniforms & attributes */
133 if(linkStatus == 1) {
134 // print shader info for uniforms & attributes
135 CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(mShaderProgramHandle, false, false, false, false, false,
136 computeShaderHandle != 0, true);
137 }
138}
139
140[[maybe_unused]]
141inline void CSCI441::ComputeShaderProgram::dispatchWork(GLuint numGroupsX = 1, GLuint numGroupsY = 1, GLuint numGroupsZ = 1) {
142 glDispatchCompute(numGroupsX, numGroupsY, numGroupsZ);
143}
144
145#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
virtual void dispatchWork(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) final
dispatches work to the Compute Shader on the GPU
Definition: ComputeShaderProgram.hpp:141
~ComputeShaderProgram() final=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:845
std::map< std::string, GLint > * mpUniformLocationsMap
caches locations of uniform names within shader program
Definition: ShaderProgram.hpp:850
static bool sDEBUG
if DEBUG information should be printed or not
Definition: ShaderProgram.hpp:819
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17