Non-constant array indices for uniforms

Is there (or will there) be support for
non-constant array indices in shader programs?

For example, in my app code I want to do this:

int numItems = 10;
float * myArray = new float[numVecs];

… // Set some values in myArray

glUniform1iARB (glGetUniformLocationARB (progObj, “num”), numItems);
glUniform1fvARB (glGetUniformLocationARB (progObj, “inArray”), numItems, myArray);

And then in my vertex shader, I want to do this:

uniform int num;
uniform float inArray[num]; /// Will this work?

void main (void)
int i = 0;
int j = 0, k = 0;
for (i = 0; i < num-1; i++)
if (inArray[i] <= gl_Vertex.y && inArray[i+1] >= gl_Vertex.y)
j = i;
k = i+1;

/// Then use j & k for other stuff


I tried, with ATI’s 4.3 drivers on my 9600 card, hardcoding my uniform array in my
vertex shader since I knew in my app how
large my array is going to be:

uniform int num;
uniform float inArray[10];

But I get a compilation error saying that
some feature is not supported or something
like that.

What I’m trying to do is pass some fairly
large arrays (maybe 5 or 6 arrays with up
to 300+ items in each array) that have been
precomputed in my application, somehow pass
them to my vertex shader, and have my shader
use these arrays as lookups to do its

[This message has been edited by Jixer (edited 03-18-2004).]

You’re probably blowing past the limits of data that can be passed via uniform values. You don’t get more than 256 4-vectors or so.

Yes, you’re right. Maybe six 300-element arrays is a little excessive.

But is there a way (or will there be a way) to pass in a uniform array without specifying the size of the array as a constant in the shader?

Pass them as a texture, and
pass the size of the texture in as a uniform.

This limits you to fragment shaders for the time being, but it appears that future vertex shaders will be able to read from texture(dx9-vs3.0 can do it)


the array implementation in cat 4.3 should work

the problem is the for construct, which isn´t implemented yet, as I know

since my radeon 9700 burnt out, I use a FX 5900 but the forceware hangs with assertion while compiling, shame NVIDIA

Originally posted by jmpCrash:
forceware hangs with assertion while compiling, shame NVIDIA

Can you send me your shaders?

Thanks jmpCrash!

You are correct in that 4.3 does now support non-constant array indices.

Also your tip on the “for” loop not being implemented was also dead-on. I changed my “for” to a “while” loop and it works!

The weird thing is that when during the link, I get the following meesage for the info log:

“Link successful. The GLSL vertex shader will run in software - unsupported language element used. The GLSL fragment shader will run in software.”

I don’t understand what could be the “unsupported language elemented used” in my vertex shader. It appears to be working, albeit interactively rotating my scene is really slow/choppy.

[This message has been edited by Jixer (edited 03-20-2004).]

Originally posted by Jixer:
uniform int num;
uniform float inArray[num]; /// Will this work?

This should not be accepted by the compiler. The specification requires array sizes to be known at compile time.

As justification, note that uniforms can be changed at each primitive, meaning the above could imply a different layout of uniform memory for different triangles while running the same shader binary on the graphics hardware. That is potentially quite difficult to implement.


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