How to use bit operations in GLSL 1.3 with OpenGL 2.1

I am trying to write a shader that uses many bits of operations. In fact, they are supported with glsl 1.30, but I'm only on OpenGL 2.1.

Can I use bit operations with my version of OpenGL?

+4
bitwise-operators opengl glsl
source share
2 answers

All SM3 compatible (~ OpenGL 2.1) hardware supports a limited whole functionality. This is usually done by emulating integers using float and does not include bit operations.

For bit operations, you need either GLSL 1.3 or EXT_gpu_shader4 .

If the reason you only have OpenGL 2.1 is because your driver is somewhat outdated, you might be lucky to have EXT_gpu_shader4 (updating drivers might be a good idea, but in this case).

If the reason is that your video card just doesn't support anything, you're out of luck.

If you have EXT_gpu_shader4 (check the extension line), you can add:

#extension EXT_gpu_shader4 : require 

for your shaders GLSL 1.2 and it should work.

+3
source share

This will help you get started.

 lowp ivec4 imod4_2(lowp ivec4 x) { return x - (2 * (x/2)); } lowp ivec4 parselowbits(lowp int x) { // Implement (x % y) where y is known to be the constant 2 // by first dividing x by (8, 4, 2, 1) and then doing a mod // by (2, 2, 2, 2) to generate an int vector. lowp ivec4 numerator = ivec4(x); lowp ivec4 denominator = ivec4(8, 4, 2, 1); lowp ivec4 modNumerator = numerator / denominator; lowp ivec4 modResult = imod4_2(modNumerator); return modResult; } lowp ivec4 parsehighbits(lowp int x) { // Implement (x % y) where y is known to be the constant 2 // by first dividing by (8*16, 4*16, 2*16, 1*16) and then doing a mod // by (2, 2, 2, 2) to generate an int vector. lowp ivec4 numerator = ivec4(x); lowp ivec4 denominator = ivec4(8*16, 4*16, 2*16, 1*16); lowp ivec4 modNumerator = numerator / denominator; lowp ivec4 modResult = imod4_2(modNumerator); return modResult; } 

The above functions work on a high low (4 bit) component, such as .rg input vector. Of course, you will need to read the values ​​and multiply by 255 to denormalize. Then the AND implementation is simple:

 lowp ivec4 and4(lowp ivec4 a, lowp ivec4 b) { lowp ivec4 a_and_b = a * b; return a_and_b; } 
0
source share

All Articles