Constant arrays a no-go

So what’s the status on constant arrays in GLSL? When I do something like this:


  const int  TEXMODE_OFF = 0;
  ...
  const int  TEX_MODE[ MAX_TEX_UNITS ] 
      = int[]( TEXMODE_OFF, TEXMODE_OFF, TEXMODE_OFF, 
               TEXMODE_OFF, TEXMODE_OFF, TEXMODE_OFF );

I get this from NVidia:

error C7516: OpenGL does not allow constant arrays

Fortunately NVidia’s compiler is aggressive and, if I just remove the const from the array, will recognize the elements and size are constant and fold them into the shader (collapsing ifs/for iterations/etc.).

But it’d be nice for clarity sake to have the const’s on there, and to protect against less aggressive GLSL compilers.

ATI compilers are even more aggressive. afaik writing doubles as integers is an error (according to GLSL spec) where nvidia is fine with it. but if you receive this error you should try to remove the const then. recommend taking a look at GLSL specs .

Thanks for the reply. Actually I did consult the spec. See section 5.4.4 Array Constructors, where it explicitly lists the example:

const float c[3] = float[3]( 5.0, 7.2, 1.1 );
const float d[3] = float[]( 5.0, 7.2, 1.1 );

Kinda suggests that it should be valid, no?

you got me :slight_smile: it got interesting, i started to look for that. unfortunately neaither the string nor the GLSL/Cg error code gives me any results except this forum thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=422784

Guess this is just an NVidia GLSL compiler bug. Thanks anyway.

For future folks finding this thread, I should also mention I did have this:

#version 120

at the top of the shader.

I also never managed to get constant arrays either, even with “#version 120” on Nvidia. I eventually did the same thing as you which is to use a non-constant array.

What is the performance tradeoff likely to be in using a non-constant array, particularly if the array is quite long (like 66x vec4s, for example)?

a|x

declared as const the compiler can use that hint as an optimization. i would think the compiler will see if something is modified or not even without const and use it internally as const (placing in special registers or something, dont know). const seems to be obsolete to me these days with compilers that have crazy optimizations. anyone any comments on this?

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