Setting/getting arrays of uniforms


I am having trouble setting/getting arrays of uniforms. Say I have a shader with only one uniform:

uniform float[2] af = float[](1.0, 1.0);

glGetObjectParameteriv returns 1 for GL_OBJECT_ACTIVE_UNIFORMS. For index 0, glGetActiveUniform return a size of 2 as expected. Now how do I query the values in the array? The SDK says to call glGetUniform for each element of the array. How do I get the index for each element? The orange book explicitly says not to assume the array elements are in order (e.g. index 0 is af[0], index 1 is af[1]). This seems reasonable since if I add another uniform to the shader, glGetActiveUniform reports index 0 is af and index 1 is the new uniform:

uniform float[2] af = float[](1.0, 1.0);
uniform float f = 5.0;

Things get really interested when I call glGetUniformLocation with various strings:

glGetUniformLocation(handle, "af") -> 0
glGetUniformLocation(handle, "af[0]") -> 0
glGetUniformLocation(handle, "af[1]") -> 1
glGetUniformLocation(handle, "f") -> 2

Now I’m confused. glGetActiveUniform return “f” for index 1 but glGetUniformLocation says its index 2. It looks like only the index returned from glGetActiveUniform can be trusted, not the one input to glGetActiveUniform.

Perhaps I need to call glGetUniformLocation for each string after I call glGetActiveUniform for each index?

Thanks for any clarification.

Maybe, you are confusing the meaning of “location” and “active-uniform-index”

When you get uniform location, you retrieve its location in the program uniform name space. But it is possible that all uniforms are not active simultaneously. So the number of active uniforms and actual uniforms declared in shaders may differ.
Moreover, in your case, glGetActiveUniform ask for the index of an active uniform not a location. So ‘af’ is one uniform, the 1st active one (though it is an array) and ‘f’ another, the 2nd active one.
Hope my explanation is clear. :slight_smile:

Thanks. You are correct. I suppose I should have caught this in the spec:

“Note that index simply identifies a member in a list of active uniforms, and has no relation to the location assigned to the corresponding uniform variable.”