So, I'm trying to optimize texture access. Currently, I'm doing four memory accesses per texel to get the red, green, blue and alpha components. It then occurred to me that I'm fetching four bytes out of what is basically an unsigned long and then recombining them. Seems wasteful. I still need to extract the components for alpha blending and such, but I thought I could do it faster.
Anyway, the current (working) code looks like this:
// "texture" is of type unsigned char...
offset = ((tv * tex_width) + tu) * 4;
tr = texture[offset];
tg = texture[offset + 1];
tb = texture[offset + 2];
ta = texture[offset + 3];
.
.
.
//"buffer" is of type unsigned long...
*buffer = (ta << 24) | (tr << 16) | (tg << 8) | tb;
What I thought would work and be faster was this:
// "texture" is of type unsigned long...
offset = (tv * tex_width) + tu;
t_val = texture[offset];
tr = (unsigned char)(t_val & 0x000000FF);
tg = (unsigned char)(t_val & 0x0000FF00);
tb = (unsigned char)(t_val & 0x00FF0000);
ta = (unsigned char)(t_val & 0xFF000000);
.
.
.
//"buffer" is of type unsigned long.
*buffer = (ta << 24) | (tr << 16) | (tg << 8) | tb;
Oddly, this doesn't work. I've played around with the byte masks, but the texture is always rendered in one of the component colors and never a combination.
I posted this here, instead of Graphics Programming and Theory, because this is more about byte manipulation that graphics. The byte masks may be accessing the wrong components, but that wouldn't cause the texture to be rendered in one component color. This makes me think that I'm not accessing the data like I want.
Byte and bit manipulation has always been a weakness for me (not sure why...). Any ideas?
[Edited by - maspeir on March 8, 2010 11:34:15 PM]