array access of vector

I’m attempting to do:
to get position.x where
vec4 position
and I’m getting a “cannot index a non-array value”. I’m using GLSL 1.2.

The OpenGL Shading Language book states:

Vectors can also be indexed as a zero-based array to obtain components. For instant, position[2] returns the third component of position. Variable indices are allowed, making it possible to loop over the components of a vector.

What am I doing wrong?


Did you index it with a uniform value on a pre-geforce8 GPU? This case, you can’t do that.

I am indexing it with a local “int” value which is derived from a uniform. I’m using a Quadro FX 5600. When I index with a constant (i.e. position[0], I don’t seem to have a problem but if I do position[index] where index is defined as “int”, then I seem to have a problem.

Most hardware can’t do variable indexing into vectors. I think all OpenGL 3.0-class hardware can do it, but I don’t know about earlier. I know that R400 (and earlier) and i945 (and earlier) can’t. If you really need to do this, you can fake it:

float f = (i == 0) ? v.x :
    (i == 1) ? v.y :
    (i == 2) ? v.z : v.w;

Really, the compiler should just generate code to do this on earlier chips, but, as far as I’m aware, nobody does.

Just requires SM3 hw, afaik.

I really don’t see why the GPU peeps don’t roll back simple instancing for earlier GPUs. It’s totally doable. But as modus says, it’s a 1.3 and up only feature.