Skip to content

Commit

Permalink
Merge branch 'snprintf'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaoneMines committed Oct 29, 2024
2 parents 03aab7f + 3b29819 commit 6e7258f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ testBuildGLEW_multifile.cpp
testBuildGLAD_multifile.cpp
tests/
bin/*
compile_commands.json
39 changes: 25 additions & 14 deletions ShaderProgram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,55 +1142,66 @@ inline bool CSCI441::ShaderProgram::mRegisterShaderProgram(const char *vertexSha
glDeleteShader(mFragmentShaderHandle );
}


// map uniforms
mpUniformLocationsMap = new std::map<std::string, GLint>();
GLint numUniforms;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORMS, &numUniforms);
GLint max_uniform_name_size;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);
if( numUniforms > 0 ) {
for(GLint i = 0; i < numUniforms; i++) {
char name[64];
int max_length = 64;
char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform(mShaderProgramHandle, i, max_length, &actual_length, &size, &type, name );
glGetActiveUniform(mShaderProgramHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
GLint location = -1;
if(size > 1) {
for(int j = 0; j < size; j++) {
char long_name[64];
sprintf(long_name, "%s[%i]", name, j);
location = glGetUniformLocation(mShaderProgramHandle, long_name);
int max_array_size = actual_length + 4 + 2 + 1;
char* array_name = (char*) malloc(max_array_size * sizeof(char));
snprintf(array_name, max_array_size, "%s[%i]", name, j);
location = glGetUniformLocation(mShaderProgramHandle, array_name);
mpUniformLocationsMap->emplace(array_name, location);
free(array_name);
}
} else {
location = glGetUniformLocation(mShaderProgramHandle, name);
mpUniformLocationsMap->emplace(name, location);
}
mpUniformLocationsMap->emplace(name, location );
free(name);
}
}

// map attributes
mpAttributeLocationsMap = new std::map<std::string, GLint>();
GLint numAttributes;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_ATTRIBUTES, &numAttributes );
GLint max_attr_name_size;
glGetProgramiv(mShaderProgramHandle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_attr_name_size);
if( numAttributes > 0 ) {
for(GLint i = 0; i < numAttributes; i++) {
char name[64];
int max_length = 64;
char* name = (char*) malloc(max_attr_name_size * sizeof(char));
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveAttrib(mShaderProgramHandle, i, max_length, &actual_length, &size, &type, name );
glGetActiveAttrib(mShaderProgramHandle, i, max_attr_name_size, &actual_length, &size, &type, name );
GLint location = -1;
if( size > 1 ) {
for( int j = 0; j < size; j++ ) {
char long_name[64];
sprintf( long_name, "%s[%i]", name, j );
location = glGetAttribLocation(mShaderProgramHandle, long_name );
int max_array_size = actual_length + 4 + 2 + 1;
char* array_name = (char*) malloc(max_array_size * sizeof(char));
snprintf( array_name, max_array_size, "%s[%i]", name, j );
location = glGetAttribLocation(mShaderProgramHandle, array_name );
mpAttributeLocationsMap->emplace(array_name, location);
free(array_name);
}
} else {
location = glGetAttribLocation(mShaderProgramHandle, name );
mpAttributeLocationsMap->emplace(name, location);
}
mpAttributeLocationsMap->emplace(name, location );
free(name);
}
}

Expand Down
45 changes: 31 additions & 14 deletions ShaderUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ inline void CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(
GLuint shaders[6];
int max_count = 6;
int actual_count;

GLint max_attr_name_size;
GLint max_uniform_name_size;

// get max var name from program
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetActiveAttrib.xhtml
glGetProgramiv(programHandle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_attr_name_size);
glGetProgramiv(programHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_size);

glGetAttachedShaders(programHandle, max_count, &actual_count, shaders );
if(actual_count > 0) {
if( sDEBUG ) printf( "[INFO]: >--------------------------------------------------------<\n");
Expand Down Expand Up @@ -468,23 +477,29 @@ inline void CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(
if( sDEBUG ) printf( "[INFO]: >--------------------------------------------------------<\n");
if( sDEBUG ) printf( "[INFO]: | GL_ACTIVE_ATTRIBUTES: %32i |\n", params );
for( int i = 0; i < params; i++ ) {
char name[64];
int max_length = 64;
char* name = (char*) malloc(max_attr_name_size * sizeof(char));
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveAttrib(programHandle, i, max_length, &actual_length, &size, &type, name );
glGetActiveAttrib(programHandle, i, max_attr_name_size, &actual_length, &size, &type, name );
if( size > 1 ) {
for( int j = 0; j < size; j++ ) {
char long_name[64];
sprintf( long_name, "%s[%i]", name, j );
int location = glGetAttribLocation(programHandle, long_name );
if( sDEBUG ) printf( "[INFO]: | %i) type: %-15s name: %-13s loc: %2i |\n", i, GLSL_type_to_string( type ), long_name, location );
// length of string + max array value size (technically it depends on the size of GL type, but I'm not aware a way to get the size)
// + array accessors + null
int max_array_size = actual_length + 4 + 2 + 1;
char* array_name = (char*) malloc(max_array_size * sizeof(char));

snprintf( array_name, max_array_size, "%s[%i]", name, j );
int location = glGetAttribLocation(programHandle, array_name);
if( sDEBUG ) printf( "[INFO]: | %i) type: %-15s name: %-13s loc: %2i |\n", i, GLSL_type_to_string( type ), array_name, location );
free(array_name);
}
} else {
int location = glGetAttribLocation(programHandle, name );
if( sDEBUG ) printf( "[INFO]: | %i) type: %-15s name: %-13s loc: %2i |\n",i, GLSL_type_to_string( type ), name, location );
}

free(name);
}
}
}
Expand All @@ -494,27 +509,29 @@ inline void CSCI441_INTERNAL::ShaderUtils::printShaderProgramInfo(
if( sDEBUG ) printf( "[INFO]: >--------------------------------------------------------<\n" );
if( sDEBUG ) printf("[INFO]: | GL_ACTIVE_UNIFORMS: %34i |\n", params);
for(int i = 0; i < params; i++) {
char name[64];
int max_length = 64;
char* name = (char*) malloc(max_uniform_name_size * sizeof(char));
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform(programHandle, i, max_length, &actual_length, &size, &type, name );
glGetActiveUniform(programHandle, i, max_uniform_name_size, &actual_length, &size, &type, name );
if(size > 1) {
for(int j = 0; j < size; j++) {
char long_name[64];
sprintf(long_name, "%s[%i]", name, j);
int location = glGetUniformLocation(programHandle, long_name);
int max_array_size = actual_length + 4 + 2 + 1;
char* array_name = (char*) malloc(max_array_size * sizeof(char));
snprintf(array_name, max_array_size, "%s[%i]", name, j);
int location = glGetUniformLocation(programHandle, array_name);
if(location != -1) {
if (sDEBUG) printf("[INFO]: | %2i) type: %-15s name: %-13s loc: %2i |\n", i, GLSL_type_to_string(type), long_name, location);
if (sDEBUG) printf("[INFO]: | %2i) type: %-15s name: %-13s loc: %2i |\n", i, GLSL_type_to_string(type), array_name, location);
}
free(array_name);
}
} else {
int location = glGetUniformLocation(programHandle, name);
if(location != -1) {
if (sDEBUG) printf("[INFO]: | %2i) type: %-15s name: %-13s loc: %2i |\n",i, GLSL_type_to_string(type), name, location);
}
}
free(name);
}
}

Expand Down

0 comments on commit 6e7258f

Please sign in to comment.