Flash: More Efficient Blur filter Values
February 27th, 2009 by LuisLast year during the development of a project where blur filters were used extensively we put into practice several tips and tricks to be able to render this visual effects quicker.
What caught my attention on top of everything was something really obviuos, in fact it is mentioned in the Flash documentation (I never read every single line), values that are a power of 2 (such as 2, 4, 8, 16 and 32) are optimized to render more quickly than other values.
Having this into account, the problem (a fairly common problem in systems programming) was to locate the "next highest power of 2", whenever I need the value to be dynamic.
Here there are a few AS3 algorithms to find the next highest power of 2:
- var powerOf2:int=1;
- var val:int=456;
- while ( powerOf2 <val )
- {
- trace(powerOf2 <<= 1);
- }
- function nextPowerOfTwo( value_ : int ):int
- {
- value_--;
- value_ = (value_>> 1) | value_;
- value_ = (value_>> 2) | value_;
- value_ = (value_>> 4) | value_;
- value_ = (value_>> 8) | value_;
- value_ = (value_>> 16) | value_;
- value_++;
- return value_;
- }
- function nextPowerOfTwo( value_ : int ):int
- {
- return int(Math.pow(2,Math.ceil(Math.log(value_) / Math.log(2))));
- }


March 3rd, 2009 at 9:13 pm
… inspiring !
What about:
function nextPowerOfTwo(value:int):int {
return value<2 ? 1 : 1 << (value_-1).toString(2).length;
}
// get the log2 by the length of value in binary and 2pow by shifting
// I needed the value_<2 condition for cases 0 and 1
April 30th, 2009 at 10:53 am
[…] http://www.blog.lessrain.com/flash-more-efficient-blur-filter-values/ […]
February 7th, 2010 at 1:13 am
[…] From: http://www.blog.lessrain.com/flash-more-efficient-blur-filter-values/ […]