Getting location of variable in shader

Say I am using the same fragment/vertex shader for multiple programs, so something like this:


GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
//initialize fragment shader

GLuint progA = glCreateProgram();
GLuint progB = glCreateProgram();

glAttachShader(progA, frag);
glAttachShader(progB, frag);

GLuint colorLocA = glGetUniformLocation(progA, "color");//color is a uniform variable in fragment shader
GLuint colorLocB = glGetUniformLocation(progB, "color");

Would I only need to find the location once.

Yes,but after linking.
Two different programs - so, getUniform for each. The var could be optimized-out in either

You can setup a fixed uniform layout that you don’t have to query per program if you use uniform buffer object with std140 layout. see also OpenGL wiki page.

Using uniform buffers can also improve performance, but they need a bit more work from the application.

You still have to use glGetUniformBlockIndex to get the uniform block’s index. However, once you set the binding point with glUniformBlockBinding, you won’t need to set either one again (assuming you always use the same binding point for this program). So it is still helpful.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.