Flash: More Efficient Blur filter Values

February 27th, 2009 by Luis

Last 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:

Actionscript:
  1. var powerOf2:int=1;
  2. var val:int=456;
  3. while ( powerOf2 <val )
  4. {
  5.     trace(powerOf2 <<= 1);
  6. }


Actionscript:
  1. function nextPowerOfTwo( value_ : int ):int
  2. {
  3.     value_--;
  4.     value_ = (value_>> 1) | value_;
  5.     value_ = (value_>> 2) | value_;
  6.     value_ = (value_>> 4) | value_;
  7.     value_ = (value_>> 8) | value_;
  8.     value_ = (value_>> 16) | value_;
  9.     value_++;
  10.     return value_;
  11. }


Actionscript:
  1. function nextPowerOfTwo( value_ : int ):int
  2. {
  3.     return int(Math.pow(2,Math.ceil(Math.log(value_) / Math.log(2))));
  4. }


3 Responses to “Flash: More Efficient Blur filter Values”

  1. piXelero Says:

    … 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

  2. Diferents approaches to get next highest power of 2 - blog.xperiments.es Says:

    […] http://www.blog.lessrain.com/flash-more-efficient-blur-filter-values/ […]

  3. Diferents approaches to get next highest power of 2 | xperiments.es Says:

    […] From: http://www.blog.lessrain.com/flash-more-efficient-blur-filter-values/ […]

Leave a Reply