View Full Version : Array indexing in for loop

05-04-2014, 12:12 PM
As my struggle with uniform arrays of structs led me to nowhere, I decided to split those arrays to single uniforms, this time, the locations are caught right, but I encountered a problem, bug perhaps, when trying to index the arrays in a loop.
Well, not only in a loop - I can't index them by any variable - only by constants, which is quite pointless and annoying.

The situation looks like follows:
I have an array which looks like this:

uniform unsigned int lightType[MAX_LIGHTS];

(MAX_LIGHTS - #defined to be 10)

Then indexing the lightType:

float test = 1.0f;
unsigned int index = 0;

test = lightType[0] / 18.0f; // 18.0f is the value I send - this line works fine, and yields value of 1
test = lightType[index] / 18.0f; // this line apparently returns 0

const unsigned int index2 = 0; // maybe if index is const...
test = lightType[index2] / 18.0f; // yep, it works fine again

outColor = vec3(test, 0.0f, 0.0f); // ... as checked in here

It turns out, that the only accepted indices are constant values. It is quite uncomfortable.

I noticed the problems, when I switched my computer from one with NVidia GPU, to one with Radeon.
Apparently Radeons have strange issues with GL, as I today updated the drivers, thinking it could be the problem, but instead shaders suddenly stopped to compile returning me empty error log, just saying something like Vertex shader compile failed: and instead of log, there was one empty line.
Changing #version to different ones were actually giving me some errors in the log, but using 420 apparently fixed it.
But the indexing issue still is present.

I have also read somewhere on the net, that it could be related to exceeding maximum active uniforms, but when I checked GL_MAX_UNIFORM_LOCATIONS it returned 33390, while number of active uniforms if 20.
What could cause it, and how to index the array by a variable?

05-05-2014, 12:27 AM
Problem kind of solved itself.
The cause was that, the array apparently was deleted, because it was not used, even if it was. The solution to this was just to use the array somewhere before, like for example

unsigned int x[MAX_LIGHTS] = lightType;

After this line, we can use variable x and our old uniform lightType just as it is supposed to.
I assume, the compiler cannot detect that the indices of uniform arrays are used or not if they are indexed by dynamic expression, like a variable, and it removes theese uniforms.
It seems to me as a buggy behaviour, despite the fact the problem is fixed now, I would like to ask if there is a way to prevent deletion of specific variable, even if it is not used.