22

I just want to store an array of weights that needs to every fragment calculation.

This:

float weights[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1);

Just throws this:

ERROR: 0:30: ']' : syntax error syntax error
ERROR: 0:30: ';' : syntax error syntax error
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • 1
    See also https://stackoverflow.com/questions/28435438/how-to-write-const-array-in-glsl-es – Bergi Dec 01 '20 at 11:33

2 Answers2

29

From the OpenGL ES SL 1.0 spec, paragraph 4.1.9 Arrays (p. 24):

There is no mechanism for initializing arrays at declaration time from within a shader.

Note that this has been intentionally left out. According to this post, the OpenGL ES SL version for OpenGL ES 2 is based on OpenGL SL 1.2. The same paragraph (p. 20) contains:

Arrays can have initializers formed from array constructors:

      float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1);
      float a[5] = float[](3.4, 4.2, 5.0, 5.2, 1.1);  // same thing
Community
  • 1
  • 1
Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34
  • Yep, but it seems that it can do it for const arrays, give it a try! – Gustavo Maciel Feb 12 '14 at 02:51
  • @Stefan So you can initialize a const array in gl es sl 1.0? – Viktor Sehr Jul 10 '16 at 18:34
  • @Viktor Sehr I've cited the specs. Don't use features that an implementation supports, but are strictly not covered. The code might not work. See also Reto Koradi's comment on the other answer here. – Stefan Hanke Jul 11 '16 at 17:06
  • 1
    What is the alternative? What can you do without having arrays? – Benedikt S. Vogler May 21 '17 at 21:43
  • 1
    this just flat out wrong. Linking to OpenGL SL 1.2 has ZERO barring on what can be done in OpenGL ES GLSL 1.0 – gman Feb 27 '19 at 04:17
  • @gman Sorry I cannot follow you. The spec for latter directly contains a reference to the former. So I think it's ok to compare both. – Stefan Hanke Feb 27 '19 at 15:36
  • 1
    it's not a conjecture. The ES spec makes.it very clear its not allowed. I helped implement strict compliance implementations. They follow the ES GLSL spec. There are lots of features in GLSL 1.20 that are not in GLSL ES 1.0. There are even official Khronos conformance tests for it – gman Feb 27 '19 at 23:38
6
precision highp float;

const float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1);

It's working with Android KitKat version (OpenGL ES 2.0).

Bart
  • 2,062
  • 15
  • 19
ivivaldi
  • 99
  • 1
  • 5
  • 7
    What GPU is in your device? That looks like a shader compiler bug to allow this, since the ES 2.0 spec clearly says: "There is no mechanism for initializing arrays at declaration time from within a shader." Some compilers are fairly liberal about allowing ES 3.0 features in ES 2.0 shaders without reporting errors. The problem is that the same code will then fail on devices with GPUs from different vendors. – Reto Koradi Jul 23 '14 at 02:39