Issue about accessing array element of uniform block (AMD glsl compiler)
I have encountered an issue about accessing array element of uniform block, the follow code can demonstrate this issue:
Code :
struct SLightSource {
vec3 color1;
vec3 color2;
};
layout(std140) uniform LightSourcesBlock {
SLightSource lightSources[32];
};
vec3 CalcLightColor(const in SLightSource lightSource)
{
return lightSource.color1 + lightSource.color2;
}
uniform int lightCount;
out vec4 fragColor;
void main()
{
vec3 lightColor = vec3(0, 0, 0);
for (int i = 0; i < lightCount; i++)
lightColor += CalcLightColor(lightSources[i]);
fragColor = vec4(lightColor, 1);
}
The Above code is very simple but it is enough to demonstrate this issue. Suppose the array in uniform block has been initialized with uniform buffer object: lightSources[0].color1 = (0.25, 0.25, 0.25), lightSources[0].color2 = (0.25, 0.25, 0.25),lightSources[1].color1 = (0.25, 0.25, 0.25),lightSources[1].color2 = (0.25, 0.25, 0.25), and the uniform lightCount is set to 2;
Run the program and the expected output color should be (1, 1, 1). In nVidia video card the output color is OK, but if I run this code in AMD video card the result will be (0.5, 0.5, 0.5), (I have an AMD Radeon HD 7770 card with Catalyst 13.1 driver)
I seems that the uniform value of the second array element in the uniform block is zero when the program evalulate their value. I have checked that both lightSources[1].color1 and lightSources[1].color2 are active uniform after the program is linked.
But if I change the light calcaultion code as follow the output color will be correct:
Code :
void main()
{
vec3 lightColor = vec3(0, 0, 0);
lightColor += CalcLightColor(lightSources[0]);
lightColor += CalcLightColor(lightSources[1]);
fragColor = vec4(lightColor, 1);
}
It looks like that when I access array element by a constant number as its index every thing is fine, but if I use a variable as the array index some strange thing will happen. So is this a feature of GLSL language I have not fully understand or a bug in AMD GLSL compiler?