1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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.... \$\endgroup\$ – Joan Aug 31 '14 at 9:25
  • 1
    \$\begingroup\$ By the way, do you know if a texture of height 1 can cause problems in some implementations requiring power of 2 sides? \$\endgroup\$ – Joan Aug 31 '14 at 9:27
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – glampert Aug 31 '14 at 17:21
  • \$\begingroup\$ 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 :-) \$\endgroup\$ – Joan Sep 1 '14 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.