Has anyone successfully done subroutines in glsl? It would be very helpful, especially for larger shaders.
I have some test code printing the Subroutine Uniforms just fine.
int maxSub,maxSubU,activeS,countActiveSU;
char name[256]; int len, numCompS;
glGetIntegerv(GL_MAX_SUBROUTINES, &maxSub);
glGetIntegerv(GL_MAX_SUBROUTINE_UNIFORM_LOCATIONS, &maxSubU);
printf("Max Subroutines: %d Max Subroutine Uniforms: %d\n", maxSub,maxSubU);
glGetProgramStageiv(mBatch->getGlslProg()->getHandle(), GL_FRAGMENT_SHADER, GL_ACTIVE_SUBROUTINE_UNIFORMS, &countActiveSU);
for (int i = 0; i < countActiveSU; ++i) {
glGetActiveSubroutineUniformName(mBatch->getGlslProg()->getHandle(), GL_FRAGMENT_SHADER, i, 256, &len, name);
printf("Suroutine Uniform: %d name: %s\n", i,name);
glGetActiveSubroutineUniformiv(mBatch->getGlslProg()->getHandle(), GL_FRAGMENT_SHADER, i, GL_NUM_COMPATIBLE_SUBROUTINES, &numCompS);
int *s = (int *)malloc(sizeof(int) * numCompS);
glGetActiveSubroutineUniformiv(mBatch->getGlslProg()->getHandle(), GL_FRAGMENT_SHADER, i, GL_COMPATIBLE_SUBROUTINES, s);
printf("Compatible Subroutines:\n");
for (int j=0; j < numCompS; ++j) {
glGetActiveSubroutineName(mBatch->getGlslProg()->getHandle(), GL_FRAGMENT_SHADER, s[j], 256, &len, name);
printf("\t%d - %s\n", s[j],name);
}
printf("\n");
free(s);
}
But I don’t know how to implement the subroutines.
I was trying
mBatch->getGlslProg()->uniform("Color", "ColorBlue");
Using this shader tutorial for source
http://www.geeks3d.com/20140701/opengl-4-shader-subroutines-introduction-3d-programming-tutorial/
it’s saying to
glUseProgram(program);
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &color_red_index);
where would I put that now?
solved!!!
indeed, I just had to put
auto shader = gl::context()->getGlslProg();
if( !shader ) return;
GLuint color_red_index = glGetSubroutineIndex(shader->getHandle(), GL_FRAGMENT_SHADER, “ColorBlue”);
glUseProgram(shader->getHandle());
glUniformSubroutinesuiv( GL_FRAGMENT_SHADER, 1, &color_red_index);
This basically acts as a pointer function in glsl.