GLSL bitwise operators in jit.gl.slab

Esteban Lanter's icon

I am trying to port some GLSL-based Shadertoy shaders to max msp using jit.gl.slab. While most are straightforward to port by re-assigning bindings, some make use of bitwise operaters such as >>, <<, &. These appear to only to be supported starting at GLSL 1.3. My specific case:

inside a *.jxs file, this method is used to draw digits - capturing the individual pixels using the binary representations of decimals (here a 6x4 grid):

//p.x and p.y capture the position in a 6x4 pixel grid
//d selects one of the digits in the array
float digit(ivec2 p, int d){
    int[10] temp = int[10](
480599,
143911,
476951,
476999,
350020,
464711,
464727,
476228,
481111,
481095
);
float res = float((temp[d]>>p.x+p.y*4)&1);
    return(res);
}

Does anyone know a solution to porting this?
Jadie Rage's icon

It's been many years, maybe you already figured this out. Essentially what's being done with the expression (x >> n) & 1 is retrieving the nth bit of x. You could write a little function to do that using regular algebra, something like:

float getBit(int x, int n)
{
    int y = x;
    for (int i = 0; i < n; i++)
    {
        y = y / 2;
    }
    return float(y - ((y / 2) * 2)); // 1 if odd, 0 if even
}

Then you could do this on the second last line:

float res = getBit(temp[d], p.x+p.y*4);

Hope this helps!