Wednesday, March 6, 2013

Particle Bit Packing Math Experiment

Was wondering if I could make a more efficient particle system by packing the position, velocity, and acceleration values of the particle into a SINGLE signed integer. The thought was that access would be faster since it is all in the same variable. So far... I don't see any support for this in my research. Here is my math stuff. Note: was also packing color information. A bit more looking, looks like you may be able to get a marginal speed increase. But so marginal that, you would have to pack and unpack in a flawless manner to get that speed increase. And the obfuscation would make the system too hard to work with and edit.

        //Packs position, velocity, acceleration, and color into a SIGNED(+-) integer.
        public function putPVAC(p2047:int, v127:int, a7:int, c3A:int, c3R:int, c3G:int, c3B:int):int
        {
            //SIZES: HEX   BITS   UINT   INT        BIN
            //pos:   FFF   12     4095   +-2047     1111 1111 1111
            //vel:   FF    8      255    +-127      1111 1111
            //acc:   F     4      15     +-7        1111
            //clr:   FF    8      255    N/A        1111 1111
            // aa:   3     2      3      3          11
            // rr:   3     2      3      3          11
            // gg:   3     2      3      3          11
            // bb:   3     2      3      3          11
            
            //pack in position:
            var packed:int = p2047;
            packed = packed << 8; //shifting to make room for VELOCITY.
            
            //pack in velocity.
            if (v127 < 0) { v127 = 127 + (0 - v127);} //handle negative numbers.
            packed = (packed | (v127 & 0xFF)) << 4; //shifting to make room for ACCELERATION.
            
            //pack in acceleration:
            if (a7 < 0) { a7 = 7 + (0 - a7); } //handle negative numbers.
            packed = (packed | (a7 & 0xF)) << 2; //shift to make room for first color component.
            
            //   42       127       212
            //0 ----- 85 ----- 170 ----- 255
            //pack in ARGB:
            if (c3A > 212) { packed = (packed | (0x3)); } else
            if (c3A > 127) { packed = (packed | (0x2)); } else
            if (c3A > 42 ) { packed = (packed | (0x1)); };
            //other wise it is zero and nothing needs to be set.
            
            
            packed = (packed  << 2); //shift to make room for SECOND color component.
            
            //   42       127       212
            //0 ----- 85 ----- 170 ----- 255
            //pack in ARGB:
            if (c3R > 212) { packed = (packed | (0x3)); } else
            if (c3R > 127) { packed = (packed | (0x2)); } else
            if (c3R > 42 ) { packed = (packed | (0x1)); };
            //other wise it is zero and nothing needs to be set.
            
            packed = (packed  << 2); //shift to make room for THIRD color component. (G)
            
            //   42       127       212
            //0 ----- 85 ----- 170 ----- 255
            //pack in ARGB:
            if (c3G > 212) { packed = (packed | (0x3)); } else
            if (c3G > 127) { packed = (packed | (0x2)); } else
            if (c3G > 42 ) { packed = (packed | (0x1)); };
            //other wise it is zero and nothing needs to be set.
            
            packed = (packed  << 2); //shift to make room for FOURTH color component. (B)
            
            //   42       127       212
            //0 ----- 85 ----- 170 ----- 255
            //pack in ARGB:
            if (c3B > 212) { packed = (packed | (0x3)); } else
            if (c3B > 127) { packed = (packed | (0x2)); } else
            if (c3B > 42 ) { packed = (packed | (0x1)); };
            //other wise it is zero and nothing needs to be set.
            
            return packed;
        }
        
        
        public function getPVAC(com:int):void
        {
            var acc:int;
            var vel:int;
            var pos:int;
            var clr:int;
            var color:uint;
            
            //                     AA     RR     GG    BB
            //                     11     11     11    11
            //extract color: //   0xC0 | 0x30 | 0xC | 0x3
            clr = com & 0xFF; //ARGB color channels.
            
            color =     (  ((clr & 0xC0)>>6) * 85) << 24 | 
                        (  ((clr & 0x30)>>4) * 85) << 16 | 
                        (  ((clr & 0xC )>>2) * 85) << 8  | 
                        (  ((clr & 0x3 )>>0) * 85) << 0;
            
                        
        
            
            //extract acceleration.
            com = com >> 8;
            acc = com & 0xF;
            if (acc > 7) { acc = 7 - acc; }
            
            //extract velocity.
            com = com >> 4; // ==com >> 12 when looking at cumulatively.
            vel = com & 0xFF;
            if (vel > 127) { vel = 127 - vel; }
            
            //extract position:
            //This is only ONE line because the signed bit on the com integer affects this.
            pos = com >> 8; // ==com >> 20 when looking at cumulatively.
            
            trace("color==" + color);
            trace("pos ==" + pos);
            trace("vel ==" + vel);
            trace("acc ==" + acc);    
            
            //make sure color is what you think it is:
            var rgb:RGB = RGB.make(color);
            trace("a ==" + rgb.Alpha);
            trace("r ==" + rgb.Red);
            trace("g ==" + rgb.Green);
            trace("b ==" + rgb.Blue);
            
            
        }//getPVAC

No comments:

Post a Comment