13#ifndef CSCI441_SHADER_PROGRAM_HPP
14#define CSCI441_SHADER_PROGRAM_HPP
19#include <glm/gtc/type_ptr.hpp>
54 const char *fragmentShaderFilename );
64 const char *fragmentShaderFilename,
76 const char *tessellationControlShaderFilename,
77 const char *tessellationEvaluationShaderFilename,
78 const char *geometryShaderFilename,
79 const char *fragmentShaderFilename );
92 const char *tessellationControlShaderFilename,
93 const char *tessellationEvaluationShaderFilename,
94 const char *geometryShaderFilename,
95 const char *fragmentShaderFilename,
107 const char *tessellationControlShaderFilename,
108 const char *tessellationEvaluationShaderFilename,
109 const char *fragmentShaderFilename );
121 const char *tessellationControlShaderFilename,
122 const char *tessellationEvaluationShaderFilename,
123 const char *fragmentShaderFilename,
133 const char *geometryShaderFilename,
134 const char *fragmentShaderFilename );
145 const char *geometryShaderFilename,
146 const char *fragmentShaderFilename,
161 bool vertexPresent,
bool tessellationPresent,
bool geometryPresent,
bool fragmentPresent,
236 [[maybe_unused]]
virtual GLint*
getUniformBlockOffsets(
const char *uniformBlockName,
const char *names[] )
const final;
251 [[nodiscard]]
virtual GLint*
getUniformBlockOffsets(GLint uniformBlockIndex,
const char *names[] )
const final;
276 [[maybe_unused]]
virtual GLuint
getSubroutineIndex( GLenum shaderStage,
const char *subroutineName )
const final;
284 [[maybe_unused]]
virtual GLint
getImageBinding(
const char* imageName)
const final;
320 [[maybe_unused]] [[nodiscard]]
virtual GLuint
getNumUniforms() const final;
330 [[maybe_unused]] [[nodiscard]] virtual GLuint
getNumAttributes() const final;
341 [[maybe_unused]] virtual
void useProgram() const final;
348 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLfloat v0) const final;
354 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLint v0) const final;
360 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint v0) const final;
366 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat2 mtx) const final;
372 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat3 mtx) const final;
378 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat4 mtx) const final;
384 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat2x3 mtx) const final;
390 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat3x2 mtx) const final;
396 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat2x4 mtx) const final;
402 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat4x2 mtx) const final;
408 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat3x4 mtx) const final;
414 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::mat4x3 mtx) const final;
422 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLfloat v0, GLfloat v1) const final;
429 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLint v0, GLint v1) const final;
436 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint v0, GLuint v1) const final;
445 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLfloat v0, GLfloat v1, GLfloat v2) const final;
453 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLint v0, GLint v1, GLint v2) const final;
461 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint v0, GLuint v1, GLuint v2) const final;
471 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) const final;
480 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLint v0, GLint v1, GLint v2, GLint v3) const final;
489 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint v0, GLuint v1, GLuint v2, GLuint v3) const final;
496 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::vec2 value) const final;
502 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::ivec2 value) const final;
508 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::uvec2 value) const final;
515 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::vec3 value) const final;
521 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::ivec3 value) const final;
527 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::uvec3 value) const final;
534 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::vec4 value) const final;
540 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::ivec4 value) const final;
546 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, glm::uvec4 value) const final;
555 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint dim, GLsizei count, const GLfloat *value) const final;
563 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint dim, GLsizei count, const GLint *value) const final;
571 [[maybe_unused]] virtual
void setProgramUniform(const
char* uniformName, GLuint dim, GLsizei count, const GLuint *value) const final;
578 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLfloat v0) const final;
584 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLint v0) const final;
590 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLuint v0) const final;
596 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat2 mtx) const final;
602 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat3 mtx) const final;
608 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat4 mtx) const final;
614 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat2x3 mtx) const final;
620 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat3x2 mtx) const final;
626 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat2x4 mtx) const final;
632 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat4x2 mtx) const final;
638 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat3x4 mtx) const final;
644 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::mat4x3 mtx) const final;
652 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLfloat v0, GLfloat v1) const final;
659 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLint v0, GLint v1) const final;
666 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLuint v0, GLuint v1) const final;
675 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLfloat v0, GLfloat v1, GLfloat v2) const final;
683 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLint v0, GLint v1, GLint v2) const final;
691 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLuint v0, GLuint v1, GLuint v2) const final;
701 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) const final;
710 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLint v0, GLint v1, GLint v2, GLint v3) const final;
719 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, GLuint v0, GLuint v1, GLuint v2, GLuint v3) const final;
726 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::vec2 value) const final;
732 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::ivec2 value) const final;
738 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::uvec2 value) const final;
745 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::vec3 value) const final;
751 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::ivec3 value) const final;
757 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::uvec3 value) const final;
764 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::vec4 value) const final;
770 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::ivec4 value) const final;
776 [[maybe_unused]] virtual
void setProgramUniform(GLint uniformLocation, glm::uvec4 value) const final;
785 virtual
void setProgramUniform(GLint uniformLocation, GLuint dim, GLsizei count, const GLfloat *value) const final;
793 virtual
void setProgramUniform(GLint uniformLocation, GLuint dim, GLsizei count, const GLint *value) const final;
801 virtual
void setProgramUniform(GLint uniformLocation, GLuint dim, GLsizei count, const GLuint *value) const final;
807 [[maybe_unused]] [[nodiscard]] virtual GLbitfield
getProgramStages() const final;
867 const
char *tessellationControlShaderFilename,
868 const
char *tessellationEvaluationShaderFilename,
869 const
char *geometryShaderFilename,
870 const
char *fragmentShaderFilename,
871 bool isSeparable ) final;
905inline CSCI441::ShaderProgram::ShaderProgram(
const char *vertexShaderFilename,
const char *tessellationControlShaderFilename,
const char *tessellationEvaluationShaderFilename,
const char *geometryShaderFilename,
const char *fragmentShaderFilename ) {
907 mRegisterShaderProgram(vertexShaderFilename, tessellationControlShaderFilename, tessellationEvaluationShaderFilename,
908 geometryShaderFilename, fragmentShaderFilename,
false);
912inline CSCI441::ShaderProgram::ShaderProgram(
const char *vertexShaderFilename,
const char *tessellationControlShaderFilename,
const char *tessellationEvaluationShaderFilename,
const char *geometryShaderFilename,
const char *fragmentShaderFilename,
bool isSeparable ) {
914 mRegisterShaderProgram(vertexShaderFilename, tessellationControlShaderFilename, tessellationEvaluationShaderFilename,
915 geometryShaderFilename, fragmentShaderFilename, isSeparable);
919inline CSCI441::ShaderProgram::ShaderProgram(
const char *vertexShaderFilename,
const char *tessellationControlShaderFilename,
const char *tessellationEvaluationShaderFilename,
const char *fragmentShaderFilename ) {
921 mRegisterShaderProgram(vertexShaderFilename, tessellationControlShaderFilename, tessellationEvaluationShaderFilename,
922 "", fragmentShaderFilename,
false);
926inline CSCI441::ShaderProgram::ShaderProgram(
const char *vertexShaderFilename,
const char *tessellationControlShaderFilename,
const char *tessellationEvaluationShaderFilename,
const char *fragmentShaderFilename,
bool isSeparable ) {
928 mRegisterShaderProgram(vertexShaderFilename, tessellationControlShaderFilename, tessellationEvaluationShaderFilename,
929 "", fragmentShaderFilename, isSeparable);
934 mRegisterShaderProgram(vertexShaderFilename,
"",
"", geometryShaderFilename, fragmentShaderFilename,
false);
940 mRegisterShaderProgram(vertexShaderFilename,
"",
"", geometryShaderFilename, fragmentShaderFilename, isSeparable);
945 const bool vertexPresent,
const bool tessellationPresent,
const bool geometryPresent,
const bool fragmentPresent,
946 const bool isSeparable ) {
948 if( vertexPresent && !tessellationPresent && !geometryPresent && !fragmentPresent ) {
950 fprintf(stderr,
"[ERROR]: Fragment Shader not present. Program must be separable.\n");
954 }
else if( vertexPresent && tessellationPresent && !geometryPresent && !fragmentPresent ) {
956 fprintf(stderr,
"[ERROR]: Fragment Shader not present. Program must be separable.\n");
960 }
else if( vertexPresent && tessellationPresent && geometryPresent && !fragmentPresent ) {
962 fprintf(stderr,
"[ERROR]: Fragment Shader not present. Program must be separable.\n");
964 mRegisterShaderProgram(shaderFilenames[0], shaderFilenames[1], shaderFilenames[2], shaderFilenames[3],
"",
967 }
else if( vertexPresent && tessellationPresent && geometryPresent && fragmentPresent ) {
969 shaderFilenames[4], isSeparable);
970 }
else if( vertexPresent && tessellationPresent && !geometryPresent && fragmentPresent ) {
971 mRegisterShaderProgram(shaderFilenames[0], shaderFilenames[1], shaderFilenames[2],
"", shaderFilenames[3],
973 }
else if( vertexPresent && !tessellationPresent && geometryPresent && !fragmentPresent ) {
975 fprintf(stderr,
"[ERROR]: Fragment Shader not present. Program must be separable.\n");
979 }
else if( vertexPresent && !tessellationPresent && geometryPresent && fragmentPresent ) {
981 }
else if( vertexPresent && !tessellationPresent && !geometryPresent && fragmentPresent ) {
983 }
else if( !vertexPresent && tessellationPresent && !geometryPresent && !fragmentPresent ) {
985 fprintf(stderr,
"[ERROR]: Vertex & Fragment Shaders not present. Program must be separable.\n");
989 }
else if( !vertexPresent && tessellationPresent && geometryPresent && !fragmentPresent ) {
991 fprintf(stderr,
"[ERROR]: Vertex & Fragment Shaders not present. Program must be separable.\n");
995 }
else if( !vertexPresent && tessellationPresent && geometryPresent && fragmentPresent ) {
997 fprintf(stderr,
"[ERROR]: Vertex Shader not present. Program must be separable.\n");
999 mRegisterShaderProgram(
"", shaderFilenames[0], shaderFilenames[1], shaderFilenames[2], shaderFilenames[3],
1002 }
else if( !vertexPresent && tessellationPresent && !geometryPresent && fragmentPresent ) {
1003 if( !isSeparable ) {
1004 fprintf(stderr,
"[ERROR]: Vertex Shader not present. Program must be separable.\n");
1008 }
else if( !vertexPresent && !tessellationPresent && geometryPresent && !fragmentPresent ) {
1009 if( !isSeparable ) {
1010 fprintf(stderr,
"[ERROR]: Vertex & Fragment Shaders not present. Program must be separable.\n");
1014 }
else if( !vertexPresent && !tessellationPresent && geometryPresent && fragmentPresent ) {
1015 if( !isSeparable ) {
1016 fprintf(stderr,
"[ERROR]: Vertex Shader not present. Program must be separable.\n");
1020 }
else if( !vertexPresent && !tessellationPresent && !geometryPresent && fragmentPresent ) {
1021 if( !isSeparable ) {
1022 fprintf(stderr,
"[ERROR]: Vertex Shader not present. Program must be separable.\n");
1026 }
else if( !vertexPresent && !tessellationPresent && !geometryPresent && !fragmentPresent ) {
1027 fprintf(stderr,
"[ERROR]: At least one shader must be present.\n");
1029 fprintf(stderr,
"[ERROR]: Unknown state.\n");
1033inline bool CSCI441::ShaderProgram::mRegisterShaderProgram(
const char *vertexShaderFilename,
const char *tessellationControlShaderFilename,
const char *tessellationEvaluationShaderFilename,
const char *geometryShaderFilename,
const char *fragmentShaderFilename,
const bool isSeparable ) {
1035 glGetIntegerv(GL_MAJOR_VERSION, &major);
1036 glGetIntegerv(GL_MINOR_VERSION, &minor);
1038 if(
sDEBUG ) printf(
"\n[INFO]: /--------------------------------------------------------\\\n");
1041 if( strcmp( vertexShaderFilename,
"" ) != 0 ) {
1042 if(
sDEBUG ) printf(
"[INFO]: | Vertex Shader: %39s |\n", vertexShaderFilename );
1043 mVertexShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(vertexShaderFilename, GL_VERTEX_SHADER );
1048 if(strcmp(tessellationControlShaderFilename,
"" ) != 0 ) {
1049 if(
sDEBUG ) printf(
"[INFO]: | Tess Control Shader: %33s |\n", tessellationControlShaderFilename );
1051 printf(
"[ERROR]:| TESSELLATION SHADER NOT SUPPORTED!! UPGRADE TO v4.0+ |\n" );
1060 if(strcmp(tessellationEvaluationShaderFilename,
"" ) != 0 ) {
1061 if(
sDEBUG ) printf(
"[INFO]: | Tess Evaluation Shader: %30s |\n", tessellationEvaluationShaderFilename );
1063 printf(
"[ERROR]:| TESSELLATION SHADER NOT SUPPORTED!! UPGRADE TO v4.0+ |\n" );
1072 if( strcmp( geometryShaderFilename,
"" ) != 0 ) {
1073 if(
sDEBUG ) printf(
"[INFO]: | Geometry Shader: %37s |\n", geometryShaderFilename );
1074 if( major < 3 || (major == 3 && minor < 2) ) {
1075 printf(
"[ERROR]:| GEOMETRY SHADER NOT SUPPORTED!!! UPGRADE TO v3.2+ |\n" );
1078 mGeometryShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(geometryShaderFilename, GL_GEOMETRY_SHADER );
1084 if( strcmp( fragmentShaderFilename,
"" ) != 0 ) {
1085 if(
sDEBUG ) printf(
"[INFO]: | Fragment Shader: %37s |\n", fragmentShaderFilename );
1086 mFragmentShaderHandle = CSCI441_INTERNAL::ShaderUtils::compileShader(fragmentShaderFilename, GL_FRAGMENT_SHADER );
1118 if(
sDEBUG ) printf(
"[INFO]: | Shader Program: %41s",
"|\n" );
1150 GLint max_uniform_name_size;
1152 if( numUniforms > 0 ) {
1153 for(GLint i = 0; i < numUniforms; i++) {
1154 char* name = (
char*) malloc(max_uniform_name_size *
sizeof(
char));
1155 int actual_length = 0;
1158 glGetActiveUniform(
mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
1159 GLint location = -1;
1161 for(
int j = 0; j < size; j++) {
1162 int max_array_size = actual_length + 4 + 2 + 1;
1163 char* array_name = (
char*) malloc(max_array_size *
sizeof(
char));
1164 snprintf(array_name, max_array_size,
"%s[%i]", name, j);
1179 GLint numAttributes;
1181 GLint max_attr_name_size;
1183 if( numAttributes > 0 ) {
1184 for(GLint i = 0; i < numAttributes; i++) {
1185 char* name = (
char*) malloc(max_attr_name_size *
sizeof(
char));
1186 int actual_length = 0;
1189 glGetActiveAttrib(
mShaderProgramHandle, i, max_attr_name_size, &actual_length, &size, &type, name );
1190 GLint location = -1;
1192 for(
int j = 0; j < size; j++ ) {
1193 int max_array_size = actual_length + 4 + 2 + 1;
1194 char* array_name = (
char*) malloc(max_array_size *
sizeof(
char));
1195 snprintf( array_name, max_array_size,
"%s[%i]", name, j );
1208 GLint separable = GL_FALSE;
1211 if(
sDEBUG ) printf(
"[INFO]: | Program Separable: %35s |\n", (separable ?
"Yes" :
"No"));
1217 if(linkStatus == 1) {
1230 if( uniformLoc == -1 )
1231 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle );
1237 if( uniformBlockLoc == -1 )
1238 fprintf(stderr,
"[ERROR]: Could not find uniform block \"%s\" for Shader Program %u\n", uniformBlockName,
mShaderProgramHandle );
1239 return uniformBlockLoc;
1250 GLubyte *blockBuffer;
1254 blockBuffer = (GLubyte*)malloc(blockSize);
1271 glGetActiveUniformBlockiv(
mShaderProgramHandle, uniformBlockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &numUniforms );
1273 auto indices = (GLuint*)malloc(numUniforms*
sizeof(GLuint));
1274 glGetActiveUniformBlockiv(
mShaderProgramHandle, uniformBlockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, (GLint*)indices);
1276 auto offsets = (GLint*)malloc(numUniforms*
sizeof(GLint));
1277 glGetActiveUniformsiv(
mShaderProgramHandle, numUniforms, indices, GL_UNIFORM_OFFSET, offsets );
1283 glGetActiveUniformBlockiv(
mShaderProgramHandle, uniformBlockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &numUniforms );
1285 auto indices = (GLuint*)malloc(numUniforms*
sizeof(GLuint));
1288 auto offsets = (GLint*)malloc(numUniforms*
sizeof(GLint));
1289 glGetActiveUniformsiv(
mShaderProgramHandle, numUniforms, indices, GL_UNIFORM_OFFSET, offsets );
1301 fprintf(stderr,
"[ERROR]: Could not find attribute \"%s\" for Shader Program %u\n", attributeName,
mShaderProgramHandle );
1304 return attribIter->second;
1309 GLuint subroutineIndex = glGetSubroutineIndex(
mShaderProgramHandle, shaderStage, subroutineName );
1310 if( subroutineIndex == GL_INVALID_INDEX )
1311 fprintf(stderr,
"[ERROR]: Could not find subroutine \"%s\" in %s for Shader Program %u\n", subroutineName, CSCI441_INTERNAL::ShaderUtils::GL_shader_type_to_string(shaderStage),
mShaderProgramHandle );
1312 return subroutineIndex;
1320 if(imageLoc == -1) {
1321 fprintf(stderr,
"[ERROR]: Could not find image \"%s\" for Shader Program %u\n", imageName,
mShaderProgramHandle);
1328 return bindingPoint;
1333 GLuint ssboIndex = glGetProgramResourceIndex(
mShaderProgramHandle, GL_SHADER_STORAGE_BLOCK, ssboName);
1335 if(ssboIndex == GL_INVALID_INDEX) {
1336 fprintf(stderr,
"[ERROR]: Could not find shader storage block \"%s\" for Shader Program %u\n", ssboName,
mShaderProgramHandle);
1340 const int NUM_PROPS = 1;
1341 GLenum props[NUM_PROPS] = {GL_BUFFER_BINDING};
1342 GLint results[NUM_PROPS];
1343 glGetProgramResourceiv(
mShaderProgramHandle, GL_SHADER_STORAGE_BLOCK, ssboIndex, NUM_PROPS, props, NUM_PROPS,
nullptr, results);
1352 if(uniformIndex == GL_INVALID_INDEX) {
1353 fprintf(stderr,
"[ERROR]: Could not find atomic counter \"%s\" for Shader Program %u\n", atomicName,
mShaderProgramHandle);
1357 GLenum props = GL_ATOMIC_COUNTER_BUFFER_INDEX;
1358 GLint atomicIndex, binding;
1359 glGetProgramResourceiv(
mShaderProgramHandle, GL_UNIFORM, uniformIndex, 1, &props, 1,
nullptr, &atomicIndex);
1360 glGetActiveAtomicCounterBufferiv(
mShaderProgramHandle, atomicIndex, GL_ATOMIC_COUNTER_BUFFER_BINDING, &binding);
1369 if(uniformIndex == GL_INVALID_INDEX) {
1370 fprintf(stderr,
"[ERROR]: Could not find atomic counter \"%s\" for Shader Program %u\n", atomicName,
mShaderProgramHandle);
1383 if(uniformIndex == GL_INVALID_INDEX) {
1384 fprintf(stderr,
"[ERROR]: Could not find atomic counter \"%s\" for Shader Program %u\n", atomicName,
mShaderProgramHandle);
1388 GLenum props = GL_ATOMIC_COUNTER_BUFFER_INDEX;
1389 GLint atomicIndex, bufferSize;
1390 glGetProgramResourceiv(
mShaderProgramHandle, GL_UNIFORM, uniformIndex, 1, &props, 1,
nullptr, &atomicIndex);
1391 glGetActiveAtomicCounterBufferiv(
mShaderProgramHandle, atomicIndex, GL_ATOMIC_COUNTER_BUFFER_DATA_SIZE, &bufferSize);
1405 int numUniformBlocks = 0;
1407 return numUniformBlocks;
1432 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1442 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1457 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1472 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1498 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %s in Shader Program %u. Dimension must be [1,4]\n", dim, uniformName,
mShaderProgramHandle);
1502 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1512 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1522 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1532 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1542 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1552 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1562 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1572 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1594 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %s in Shader Program %u. Dimension must be [1,4]\n", dim, uniformName,
mShaderProgramHandle);
1598 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1608 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1618 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1628 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1638 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1648 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1658 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1668 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1690 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %s in Shader Program %u. Dimension must be [1,4]\n", dim, uniformName,
mShaderProgramHandle);
1694 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1702 glProgramUniformMatrix2fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1704 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1712 glProgramUniformMatrix3fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1714 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1722 glProgramUniformMatrix4fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1724 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1732 glProgramUniformMatrix2x3fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1734 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1742 glProgramUniformMatrix3x2fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1744 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1752 glProgramUniformMatrix2x4fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1754 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1762 glProgramUniformMatrix4x2fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1764 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1772 glProgramUniformMatrix3x4fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1774 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1782 glProgramUniformMatrix4x3fv(
mShaderProgramHandle, uniformIter->second, 1, GL_FALSE, glm::value_ptr(mtx) );
1784 fprintf(stderr,
"[ERROR]: Could not find uniform \"%s\" for Shader Program %u\n", uniformName,
mShaderProgramHandle);
1838 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %i in Shader Program %u. Dimension must be [1,4]\n", dim, uniformLocation,
mShaderProgramHandle);
1893 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %i in Shader Program %u. Dimension must be [1,4]\n", dim, uniformLocation,
mShaderProgramHandle);
1948 fprintf(stderr,
"[ERROR]: invalid dimension %u for uniform %i in Shader Program %u. Dimension must be [1,4]\n", dim, uniformLocation,
mShaderProgramHandle);
1955 glProgramUniformMatrix2fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1960 glProgramUniformMatrix3fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1965 glProgramUniformMatrix4fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1970 glProgramUniformMatrix2x3fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1975 glProgramUniformMatrix3x2fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1980 glProgramUniformMatrix2x4fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1985 glProgramUniformMatrix4x2fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1990 glProgramUniformMatrix3x4fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
1995 glProgramUniformMatrix4x3fv(
mShaderProgramHandle, uniformLocation, 1, GL_FALSE, glm::value_ptr(mtx) );
2000 GLbitfield shaderBits = 0;
2013inline void CSCI441::ShaderProgram::_initialize() {
2026 int infoLogLength = 0;
2027 int maxLength = 1000;
2032 char* infoLog =
new char[maxLength];
2039 if(infoLogLength > 0 ) {
2041 if(
sDEBUG ) printf(
"[INFO]: Program Handle %d Delete Status %s: %s\n",
mShaderProgramHandle, (status == GL_TRUE ?
"Success" :
" Error"), infoLog );
2051 glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats);
2053 fprintf(stderr,
"[ERROR]: Driver does not support any binary formats.\n");
2062 std::vector<GLubyte> buffer(length);
2067 fprintf(stdout,
"[INFO]: Writing to %s, binary format %d\n",BINARY_FILE_NAME, format);
2068 std::ofstream out(BINARY_FILE_NAME, std::ios::binary);
2069 out.write(
reinterpret_cast<char *
>(buffer.data()), length );
2077 glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats);
2079 fprintf(stderr,
"[ERROR]: Driver does not support any binary formats.\n");
2083 GLuint program = glCreateProgram();
2086 std::ifstream inputStream(BINARY_FILE_NAME, std::ios::binary);
2087 std::istreambuf_iterator<char> startIt(inputStream), endIt;
2088 std::vector<char> buffer(startIt, endIt);
2089 inputStream.close();
2092 glProgramBinary(program, FORMAT, buffer.data(), buffer.size() );
2096 glGetProgramiv(program, GL_LINK_STATUS, &status);
2097 if( GL_FALSE == status ) {
2098 CSCI441_INTERNAL::ShaderUtils::printProgramLog(program);
2103 shaderProgram->mShaderProgramHandle = program;
2104 return shaderProgram;
Helper functions to work with OpenGL Shaders.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:35
GLuint mTessellationEvaluationShaderHandle
handle to the tessellation evaluation shader stage
Definition: ShaderProgram.hpp:832
virtual GLint getAtomicCounterBufferOffset(const char *atomicName) const final
Returns the offset into the buffer for the corresponding atomic counter buffer.
Definition: ShaderProgram.hpp:1366
GLuint mVertexShaderHandle
handle to the vertex shader stage
Definition: ShaderProgram.hpp:824
static ShaderProgram * loadShaderProgramFromBinaryFile(const char *BINARY_FILE_NAME, GLenum format)
loads precompiled shader program binary from external file
Definition: ShaderProgram.hpp:2075
virtual GLint getAtomicCounterBufferBinding(const char *atomicName) const final
Returns the binding point for the corresponding atomic counter buffer.
Definition: ShaderProgram.hpp:1349
GLuint mShaderProgramHandle
handle to the shader program
Definition: ShaderProgram.hpp:845
GLuint mTessellationControlShaderHandle
handle to the tessellation control shader stage
Definition: ShaderProgram.hpp:828
virtual void useProgram() const final
Sets the Shader Program to be active.
Definition: ShaderProgram.hpp:1422
virtual GLuint getNumAttributes() const final
Returns the number of active attributes in this shader program.
Definition: ShaderProgram.hpp:1411
std::map< std::string, GLint > * mpUniformLocationsMap
caches locations of uniform names within shader program
Definition: ShaderProgram.hpp:850
ShaderProgram & operator=(const ShaderProgram &)=delete
do not allow shader programs to be copied
virtual GLbitfield getProgramStages() const final
returns a single value corresponding to which shader stages are present in this shader program
Definition: ShaderProgram.hpp:1999
virtual GLuint getNumUniforms() const final
Returns the number of active uniforms in this shader program.
Definition: ShaderProgram.hpp:1397
virtual GLubyte * getUniformBlockBuffer(const char *uniformBlockName) const final
Returns an allocated buffer for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1249
virtual GLint getImageBinding(const char *imageName) const final
Returns the binding point for the corresponding image uniform.
Definition: ShaderProgram.hpp:1317
virtual GLint getUniformLocation(const char *uniformName) const final
Returns the location of the given uniform in this shader program.
Definition: ShaderProgram.hpp:1228
virtual ~ShaderProgram()
Clean up memory associated with the Shader Program.
Definition: ShaderProgram.hpp:2024
virtual GLuint getShaderProgramHandle() const final
Returns the handle for this shader program.
Definition: ShaderProgram.hpp:1417
GLuint mGeometryShaderHandle
handle to the geometry shader stage
Definition: ShaderProgram.hpp:836
virtual GLuint getSubroutineIndex(GLenum shaderStage, const char *subroutineName) const final
Returns the index of the given subroutine for a shader stage in this shader program.
Definition: ShaderProgram.hpp:1308
virtual GLint getAttributeLocation(const char *attributeName) const final
Returns the location of the given attribute in this shader program.
Definition: ShaderProgram.hpp:1298
static bool sDEBUG
if DEBUG information should be printed or not
Definition: ShaderProgram.hpp:819
virtual bool writeShaderProgramBinaryToFile(const char *BINARY_FILE_NAME) const final
writes precompiled shader program binary to external file
Definition: ShaderProgram.hpp:2049
static void disableDebugMessages()
Disables debug messages from Shader Program functions.
Definition: ShaderProgram.hpp:889
static void enableDebugMessages()
Enables debug messages from Shader Program functions.
Definition: ShaderProgram.hpp:884
virtual GLint getAtomicCounterBufferSize(const char *atomicName) const final
Returns the full buffer size for the corresponding atomic counter buffer.
Definition: ShaderProgram.hpp:1380
std::map< std::string, GLint > * mpAttributeLocationsMap
caches locations of attribute names within shader program
Definition: ShaderProgram.hpp:854
virtual void setProgramUniform(const char *uniformName, GLfloat v0) const final
sets the program uniform consisting of one float
Definition: ShaderProgram.hpp:1427
virtual GLuint getNumUniformBlocks() const final
Returns the number of active uniform blocks in this shader program.
Definition: ShaderProgram.hpp:1404
ShaderProgram(const ShaderProgram &)=delete
do not allow shader programs to be copied
virtual void setUniformBlockBinding(const char *uniformBlockName, GLuint binding) const final
Set the binding point for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1293
virtual GLint getUniformBlockSize(const char *uniformBlockName) const final
Returns the size of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1242
GLuint mFragmentShaderHandle
handle to the fragment shader stage
Definition: ShaderProgram.hpp:840
virtual GLint * getUniformBlockOffsets(const char *uniformBlockName) const final
Returns an array of offsets into the buffer for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1260
virtual GLint getUniformBlockIndex(const char *uniformBlockName) const final
Returns the index of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1235
ShaderProgram()
creates an empty shader program object
Definition: ShaderProgram.hpp:2009
virtual bool mRegisterShaderProgram(const char *vertexShaderFilename, const char *tessellationControlShaderFilename, const char *tessellationEvaluationShaderFilename, const char *geometryShaderFilename, const char *fragmentShaderFilename, bool isSeparable) final
registers a shader program with the GPU
Definition: ShaderProgram.hpp:1033
virtual GLint getShaderStorageBlockBinding(const char *ssboName) const final
Returns the binding point for the corresponding shader storage block.
Definition: ShaderProgram.hpp:1332
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17