View Full Version : Getting location of variable in shader

07-26-2011, 12:11 AM
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.

Ilian Dinev
07-26-2011, 12:37 AM
Yes,but after linking.
Two different programs - so, getUniform for each. The var could be optimized-out in either

08-02-2011, 03:34 AM
You can setup a fixed uniform layout that you don't have to query per program if you use uniform buffer object (http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt) with std140 layout. see also OpenGL wiki page (http://www.opengl.org/wiki/Uniform_Buffer_Object).

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

Alfonse Reinheart
08-02-2011, 04:40 PM
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.