Jyaif
Jyaif

Reputation: 1095

Fragment shader reading value in uniform array in OpenGL ES 2.0

I've got a fragment shader that downsamples the colors to 4 different colors. To do this, I compute an index based on the color that varies from 0 to 3. I then lookup the value of the final color in an uniform array of vec4:

The declaration of the array:

uniform vec4 u_colors[4];

The lookup:

gl_FragColor = u_colors[int(index)];

This works well on some devices, but on some devices (such on a Tegra 4) it seems I can't access an array with a variable, and the output is garbage. The only work-around that I found is this:

index = floor(index);
if (index < 2.0) {
  if (index == 0.0) {
    gl_FragColor = u_colors[0];
  } else {
    gl_FragColor = u_colors[1];
  }
} else {
  if (index == 2.0) {
    gl_FragColor = u_colors[2];
  } else {
    gl_FragColor = u_colors[3];
  }
}

This works, and surprisingly the performance is still pretty good, but it's obviously not ideal.

Am I doing something wrong or is this a bug in the GPU driver/compiler? What efficient alternatives do I have?

Upvotes: 1

Views: 1299

Answers (1)

Nazar554
Nazar554

Reputation: 4195

GLSL ES 1.00 for OpenGL ES 2.0 spec has this line:

Uniforms (excluding samplers)

In the vertex shader, support for all forms of array indexing is mandated. In the fragment shader, support for indexing is only mandated for constant-index-expressions.

Most drivers implement this functionality in fragment shaders, but it's not required so Tegra 4 has all rights to refuse to accept your code.

Upvotes: 2

Related Questions