# Bit operation in fragment program?

Hi
I have to use 16 bits to my fragment program. Is there an elegant way to do that? It mean, I want to be then able to read each of this 16bits, easily (e.g: MUL myout, myin, bit)
Thanks

No shading languages have bitwise operations. Shader ops tend to be floating-point in nature.

Ok. But if I have 16 bits to access, I can put them in a float. And if I was in C++, I could simply use modulo operation to find the desired bit. Can I do the same in fragment program?
An other solution would be to pass the 16 bit in a texture, but a texture lookup is too slow and I can’t use it.

I seriously doubt it. The mod operator is an integer operation (technically not true, but close enough), and I don’t recall seeing any mention of such a function in the glslang spec. It certainly isn’t in any fragment program specs.

There is no mod operation in GLSL or in the asm languages, but integer operations are possible.
With a series of multiplication by 2 and division by 2, it’s possible to extract bits.

Unfortunatly, the overflow behavior is not portable and also, implementation are free to choose the integer precision they want.

Looking at the Orange book there is a mod() function. I was able to create a bit-wise testing function that returns the value of a desired bit. See the following:

``````float BitSelect(vec3 pixel, float bit)
{
float value = 0.0;
float bitlcl = bit;

if (bit < 8.0)
value = pixel.r;
else if (bit < 16.0)
{
value = pixel.g;
bitlcl = bit - 8.0;
}
else
{
value = pixel.b;
bitlcl = bit - 16.0;
}

float ison = floor(mod(floor(value*255.0)/pow(2.0,bitlcl), 2.0));

return ison;
}
``````

Cheers,
-B

Hmmm. perhaps I was wrong back then.
Also, this thread is 1 year old. You will be able to do bitwise operations on the next generation of hw.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.