How to define constant array in GLSL (OpenGL ES 2.0)?

43,162

Solution 1

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

Solution 2

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

Share:
43,162
Geri Borbás
Author by

Geri Borbás

I love this industry. In the past 10 years I made numerous Apps and Games from zero to market, both teamed and solo. I've been doing remote consultancy for the past 7 years. Built a tutoring app with a UK edtech startup, made a gamified fintech solution for a London studio, developed augmented reality experience for a Dutch game publisher, created smaller UI/UX design projects across the globe, and more. I also worked full-time for a global agency for years, collaborated on various products, early-phase start-ups in close-knit agile teams. Besides, I released several self-published products throughout the years embracing every responsibility from ideating through every phase of production to customer support.

Updated on July 09, 2022

Comments

  • Geri Borbás
    Geri Borbás almost 2 years

    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
    
  • Gustavo Maciel
    Gustavo Maciel over 10 years
    Yep, but it seems that it can do it for const arrays, give it a try!
  • Reto Koradi
    Reto Koradi almost 10 years
    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.
  • Viktor Sehr
    Viktor Sehr almost 8 years
    @Stefan So you can initialize a const array in gl es sl 1.0?
  • Stefan Hanke
    Stefan Hanke almost 8 years
    @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.
  • Benedikt S. Vogler
    Benedikt S. Vogler almost 7 years
    What is the alternative? What can you do without having arrays?
  • gman
    gman about 5 years
    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
  • Stefan Hanke
    Stefan Hanke about 5 years
    @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.
  • gman
    gman about 5 years
    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