2
    vec2 s[5];
    s[1] = vec2(0.0, 0.1);
    s[2] = vec2(0.5, 0.8);
    int dist = 0;
    dist++;
    float tst = s[dist].x;

The above test raises this error:

The purpose of using an array in my case, is to compute the index and get the contents of that index. If I have to use a constant, how am I going to compute the index? (by the way, using textures as input is not efficient in my case).

2
  • It might look weird, but GLSL ES 100 disallows computing array index, but allows doing logically the same thing in a clumsy way using if/else statements (e.g. if (dist == 0) { tst = s[0].x; } else { tst = s[1].x; }). Commented Mar 30, 2021 at 9:13
  • (I just saw your comment), that's what I was thinking a few minutes ago, they disallowed simple reads of memory (arrays) while they allowed 'if-then-else' which probably has far higher impact in performance (of parallel computation), that's bad specs design in my book... Commented Mar 30, 2021 at 14:29

1 Answer 1

2

See OpenGL ES Shading Language 1.00 Specification - Appendix A - Indexing of Arrays, Vectors and Matrices:

The following are constant-index-expressions:

  • Constant expressions
  • Loop indices as defined in section 4
  • Expressions composed of both of the above

Therefore, the index of an array can also be the control variable of a for loop:

for (int i = 0; i < 2; i++)
{
    float tst = s[i].x;

    // [...]
}

It should be mentioned that this restriction applies to OpenGL ES Shading Language 1.00 (WebGL 1.0), but not to OpenGL ES Shading Language 3.00 (WebGL 2.0).

See OpenGL ES Shading Language 3.00 Specification - 12.30 Dynamic Indexing:

For GLSL ES 1.00, support of dynamic indexing of arrays, vectors and matrices was not mandated because it was not directly supported by some implementations. Software solutions (via program transforms) exist for a subset of cases but lead to poor performance. Should support for dynamic indexing be mandated for GLSL ES 3.00?

RESOLUTION: Mandate support for dynamic indexing of arrays except for sampler arrays, fragment output arrays and uniform block arrays.

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

5 Comments

OK, thanks, that's a valid use, is this the only case? I'm asking as I aim to minimize or eliminate loops completely for efficiency. Now, if efficiency is the same, then I bet the loops are unfolded and converted to constant-based code, am I right?
Exactly - GLSL ES 100 restrictions were defined to allow trivial loop unfolding, as GPU hardware at that time wouldn't be able to do something else.
OK, good to know, so other than indexing via a basic loop, index computation is not allowed.
@dllb Also note that this limitations just apply to GLSL ES 1.0. GLSL ES 3.00 is much more tolerant.
@Rabbid76 I know, but unfortunately IOS does not support WEBGL 2.0 yet (Safari on IOS), so I can't exclude the whole range of Apple smartphones.

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.