1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.