dllb
dllb

Reputation: 111

WEBGL 1.0 Shaders: What's the point of using an array if I can't use a variable to compute the index?

    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).

Upvotes: 2

Views: 783

Answers (1)

Rabbid76
Rabbid76

Reputation: 211277

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.

Upvotes: 2

Related Questions