# Lookup table in shader opengl es

What I need is apparently very simple, a lookup table in a fragment shader. It seems that the natural way would be an array float lut[], but in some implementations of OpenGL ES I am getting an error

"profile requires index expression to be compile-time constant".

Next approach would be a 1D texture filled with the values, but again OpenGL ES doesn't like that :)

What would be the simplest way to have a lookup table of 256 float elements in the fragment shader? (I guess it will be a 2d texture but it looks like it is some overhead and I am not sure if it's possible to handle two textures on a shader)

P.S. I am a very beginner in OpenGL but what I need is simple handling of images in textures.

Yes, indeed, some ES implementations don't support non-const indexing of arrays, which complicates code quite a bit. 1D textures are also not supported on GL ES 2.0 or lower.

You can certainly overcome this by using a 2D texture with a height of 1 pixel. It will effectively behave like a 1D array.

Another approach might also be computing the value dynamically, instead of using a lookup. This might actually be faster, since memory access patterns are usually a problem on GPUs, whereas a lot of computing power is available. So you might also consider the possibility of replacing the lookup by an on-the-fly computation of the value.

• Thanks. Not sure of the advantage in speed doing the computations in the shader, from my unscientifical tests, especially in smartphones, when you add some operations the tradeoff goes to the LUT....
– Joan
Aug 31 '14 at 9:25
• By the way, do you know if a texture of height 1 can cause problems in some implementations requiring power of 2 sides?
– Joan
Aug 31 '14 at 9:27
• Good question, I would assume that a 1px side is always acceptable, to allow simulating a 1D texture, for example. But I don't know that for sure. Aug 31 '14 at 17:21
• Yes doing the unnecessarily complicated way to cope with the limitations of certain systems that are the most likely to pose that additiona limitation... I will allocate a 256*256 texture to store precalculated stuff, now I have to guess what to put there :-)
– Joan
Sep 1 '14 at 9:29