Shader bitwise manipulation

Hello everyone,

I’m using the alpha channel as a color lookup index and just found out that I need a hair more than 256 values, which means I need to get the extra bit from somewhere else. I figure since the human eye is least sensitive to red I can steal the high-order bit from it by writing a quick script to map all red values from 0-255 to 0-127 and save the resulting image. When it comes time to render I simply extract the low 7 bits, double the value, and render it, piece of cake. The problem is, how do I a) extract the low 7 bits and b) extract the high-order bit so I can append it to the alpha to create a value from 0-511? I can easily do bitwise manipulations in C#, but I don’t see any way to do it in a shader.

Thanks in advance,

Anthony

For red component r between 0 and 1 this might work:
a) fmod(r, 0.5)
b) round(r)

Hi Martin,

Will solution a actually work? I’ve worked it out in my head and it doesn’t compute, so maybe I’m missing something.

For example, for simplicity sake let’s say that instead of the usual 8 bits we have 3 bits (bits 1, 2, and 4 for a total of 8 values) represent the range from 0-1 :

000 = 0 = 0/7 = 0.0
001 = 1 = 1/7
010 = 2 = 2/7
011 = 3 = 3/7
100 = 4 = 4/7
101 = 5 = 5/7
110 = 6 = 6/7
111 = 7 = 7/7 = 1.0

First, I assume 0.5 is half of 1.0, and therefore 3/7 is half of 7/7, so 0.5 == 011 = 3 = 3/7.

Let’s take 111 as input (I expect to get 011 as a result) : 7/7 mod 3/7 = 1/7 = 001, which is not the 011 we expected. Already we are wrong (correct me if I’m wrong, lol).

However, you put me on the right track. All I need to do is check to see if the input is > 0.5. If true, I subtract 0.5 from it to get the low 7 bits; if false, I use the input as is.

Thanks,

Anthony

.

I don’t know whether it will actually work. What exactly are you doing? Reading the alpha channel from a 24-bits framebuffer?

I’m not sure but the mapping of the framebuffer value A from 0 to 255 to a floating-point number a from 0 to 1.0 might be: a = A/255 or a = A/256 (or even a = A/255.5). I assumed it is the second variant, but that’s probably not correct.

Could you multiply with 255 and then use the bit-wise operation (which should be support in Cg: The Cg Tutorial - Chapter 3. Parameters, Textures, and Expressions )?

What device are you working on?

Hi Martin,

Yes, I believe that it’s 0-255 (or 8 bits) but it was simpler to demo the issue in 3 bits instead. Also, although Cg theoretically supports bitwise operators, none of the profiles available implement them. In any case, I believe I have the answer, which I already submitted at the end of my previous post.

Regards,

Anthony

OK, I missed that sentence. :slight_smile:

I’d love to see a whole thread on bit manipulation in SM 2.0…

bit manipulation is the bleeding edge of graphics