Array of constants

How are we suppose to do an array of constants?

I thought it was

const vec2 myarray[2]=
{
vec2(3.0, 2.0),
vec2(4.0, 2.0)
};

I needed that recently as well.
I could not find a solution besides creating the array and assigning each element on startup. (and hoping that the compiler was smart and ultimately directly resolved all lookups to direct constants…)

The spec for GLSL 1.10,59 says that

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

to define a const array is not possible at the moment.
i have the same problem, in great need of a constant array. i assigned the values outside, then passed it as a normal. it might make things slow. but if you assigned the values one by one in the shader, its also slow.

This is supported by NVIDIA’s GLSL implementation.

How does it work in NV?

My other guesses were
const vec2 myarray[2]= vec2[](
vec2(3.0, 2.0),
vec2(4.0, 2.0)
);

const vec2 myarray[2]= vec2[2](
vec2(3.0, 2.0),
vec2(4.0, 2.0)
);

const vec2 myarray[2]= vec2[](
3.0, 2.0,
4.0, 2.0
);

Something like the last one is better cause it avoids typing vec2 many times.

It works just like the example you showed in your first post.

yup works on NV the following way

const vec4 stuff[2] = { vec4(x,x,x,x), vec4(s,s,s,s) };

Does declaring the array as a uniform, and setting it from the application work? Should be high performing. The language supports indirect indexing into uniform arrays.

Yes, using a uniform array works fine.
I didn’t do a performance test on this. I have big fillrate performance problem for the moment.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.