Indexing tables in GLSL

In my fragment shader I have:

int A[10];
int value, idx;

When I do …

value = A[3];

… program takes fourth element of the table A and works properly - is OK.

But as I do:

idx = 3;
value = A[idx];

program takes first element (like in idx=0).

Is it possible to indexing tables by variable? How to do it? What I need to add or configure in OpenGL environment? I use 2.16 version of OpenGL, and a card ATI Radeon HD 3870.

Well it is possible and it should work the way you pointed it out ,but in ATI’s world this problem could be possible. Make shure you have the latest drives. Or use a nvidia card xD
Maybe you can post the shader, it can be related to an other problem. I can remember that indexing worked for me that way on ati cards.

If it can reassure you, on my ATI Radeon X1600 mobility, it is clearly impossible to index arrays with variables: compiler error (with the latest linux ATI driver). Only static indexing is supported on array of primitives types like int. This is completly useless, Thank you ATI! :frowning:

Well it worked for me on a 9800 XT and a x700 mobility with windows.

i dont think there is any hardware now thats supports indexing into at shader runtime generated arrays.

but compilers can still “emulate” indexing (i think the dx hlsl-compiler does) by using alot of compares/conditional assignments linear to your array size, so i.e. expect for indexing into a 10-element array about 20 shaderinstructions.
so a
will be expanded to
if(x==0) y=a[0];
if(x==1) y=a[1];
if(x==2) y=a[2];

if(x==9) y=a[9];

this is because shaders dont have any read-write data storage except registers, and i dont know of any hardware that can index into registerspace.

indexing into uniform arrays works on newer hardware.

I used indexed uniform arrays on ATI Hardware (in vertex shader) and it rand quite fast. Can’t really believe that it has been split into 32 if’s

Well, vertex shaders has long supported indexing into uniform arrays. As for fragment shaders, I suppose only lastgen can do it.

I tried to index by variable arrays that are not uniform in a fragment shader on a Geforce 8800 GTS and it works perfectly and fast.
I can’t find the usability of such arrays that can only be indexed by constant expressions…

The GF8 uses unified shaders, so the fragment shaders uses the same hardware like the vertex shader. On GF7 and earlier the fragment shaders were not be be able to do that. As long the all indices are known at compile time (i.E. by unrolling loops) it should work too. If it doesn’t work on the HD 3870, it may be that the drivers are still broken.

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