views:

105

answers:

3

Hi,

I am working on a OpenGL ES 2.0 shader and I have tightly packed data e.g. three 5-bit unsigned integers within a block of two bytes. To unpack this data I obviously need bit-shifting, but this is not supported in OpenGL ES Shading Language (see page 29 http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf)

Consequently I perform a number of *2 and /2 operations to emulate bit shifting.

Does anyone know a more efficient/elegant way to do this? Is there a trick I am not aware of?

Thanks!

A: 

Maybe a+=a is faster than a*=2. Benchmark to be sure.

mvds
+2  A: 

If you are performing multiple shifts, you can use power operations. A bit shift is a multiplication or division by 2n, and a power operation would be more readable than multiple multiplication or division operations, I think, but I'm not sure about the performance. I suppose this is a more elegant solution, but probably not a more efficient one.

Thomas Owens
+1  A: 

I've never used OpenGL, but the most efficient method would be a 16 bit lookup table for each type if your environment supports it. You would need to populate the table once on startup, but this should be very quick. You could use seperate tables for each type or a 2 dimensional table, eg, theTable[65536][3].

JackN
I like this idea. I'll try to misuse a texture for this purpose.
Lars Schneider