# glsl: zero sized arrays not possible?

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make my life easier by creating one shader that can handle all situations. My plan was to have it controlled by a set of compile time constants for things like the number of textures, spot lights point lights and so on. So if I had an object with 1 texture being lit by 1 spot light and no point lights i would set the following constants:
const int g_num_textures = 1;
const int g_num_spot_lights = 1;
const int g_num_point_lights = 0;

And I'd have some arrays to go along with that like so:
...
varying vec3 g_texture_coords[g_num_textures];

uniform vec3 g_spot_position[g_num_spot_lights];
uniform vec3 g_spot_dir[g_num_spot_lights];
uniform vec4 g_spot_atten[g_num_spot_lights];
uniform sampler2D g_spot_textures[g_num_spot_lights];

uniform vec3 g_point_light_color[g_num_point_lights];
uniform vec3 g_point_light_position[g_num_point_lights];
uniform vec2 g_point_light_atten[g_num_point_lights];
...

That way in this example when I dont have any point lights the spot light arrays will all be zero sized and therefore compiled out. Unfortunatley the nvidia drivers (76.76 linux) have a fit and tell me: Info Log: (16) : error C1043: size of dimension cannot be less than 1 is what I'm trying to do forbidden in glsl? it seems like a neat way of making shaders since I can just write one and then cache all the different versions of it as and when they are needed

##### Share on other sites
First place to check when something like this happens is the spec, it can be found on the opengl.org site [smile]

However, think about it, a 0 sized array just makes no sense as a concept, even a normal variable is technically an array of size 1.
When you need todo is take advantage of the whole static looping thing and let compiler work out how large the array should be.

In theory, if you declare all the arrays as;
varying vec3 g_texture_coords[];

and then have static length loops (controled by your variables) to access each one the compiler should be able to figure out what todo size wise and dead code remove for you.

In theory anyways, I've never tried it myself but I'm pretty sure the spec agrees with me on this idea (I know you can do it so that if you statically access an array element it will make it the right size, its just a matter of is the compiler smart enuff to figure it out from a static length loop... )

##### Share on other sites
I've tried doing that and it will now compile (but it wont link... not sure why but that may be my fault). But I'm not sure this is allowed in the spec, it only says "It is legal to declare an array without a size and then later re-declare the same name as an array of the same type and specify a size" it says nothing about what happens why you dont specify a size.

The 3dlabs glsl compiler gives me an error "array must be redeclared with a size before being indexed with a variable" which leads meto believe that this just nvidia adding 'features' that are not spec-compliant

• 10
• 17
• 9
• 14
• 41