Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 664
Geri Borbás Avatar asked May 06 '12 00:05

Geri Borbás


People also ask

How do you declare an array in GLSL?

To initialize an array you can use a constructor. Like in object orientation GLSL has a constructor for arrays. The syntax is as follows. int a[4] = int[](4, 2, 0, 5, 1); float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); int[] c = int[3](1, 2, 3); int[] d = int[](5, 7, 3, 4, 5, 6);

What is vec3 in OpenGL?

vec3 is a floating point vector with three components. It can be initialized by: Providing a scalar value for each component. Providing one scalar value. This value is used for all components.

What is the difference between OpenGL and GLSL?

The short version is: OpenGL is an API for rendering graphics, while GLSL (which stands for GL shading language) is a language that gives programmers the ability to modify pipeline shaders. To put it another way, GLSL is a (small) part of the overall OpenGL framework.

Is it possible to use OpenGL SL with GLSL?

Linking to OpenGL SL 1.2 has ZERO barring on what can be done in OpenGL ES GLSL 1.0 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.

Is the GLSL register allocator part of the OpenGL standard?

However, the register allocator is not part of the GLSL standard. Different OpenGL implementations can have varying levels of quality when it comes to converting high-level GLSL code into the low-level machine code the GPU understands. One of the more complicated parts of a compiler (GLSL or otherwise) is register allocation.

Why do different OpenGL implementations have different levels of quality?

Different OpenGL implementations can have varying levels of quality when it comes to converting high-level GLSL code into the low-level machine code the GPU understands. One of the more complicated parts of a compiler (GLSL or otherwise) is register allocation.

What are the requirements for array indices in GLSL?

When an array indexing expression, including struct field member accesses, results in an opaque types, the standard has special requirements on those array indices. Under GLSL version 3.30, Sampler arrays (the only opaque type 3.30 provides) can be declared, but they can only be accessed by compile-time integral Constant Expressions.


1 Answers

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
like image 122
Stefan Hanke Avatar answered Sep 30 '22 23:09

Stefan Hanke