using PixelBender to double the size of a bitmap
Posted
by jedierikb
on Stack Overflow
See other posts from Stack Overflow
or by jedierikb
Published on 2010-05-17T22:12:58Z
Indexed on
2010/05/17
22:21 UTC
Read the original article
Hit count: 404
I have a performance question about pixel bender. I want to enlarge many BitmapData (double their size into new BitmapData). I was doing this with as3, but wanted to use pixel bender to get better performance. On my machines, I get great comparative performance out of many pixel bender demonstrations.
To my surprise (or bad coding / understanding), I am getting much worse performance out of pixel bender -- 2 seconds to do 3000 scalings vs .5 seconds! I expected to get at least the same performance as as3. What am I doing wrong?
I got the straightforward pixel bender code here (and it is included below for easy reference).
package
{
import aCore.aUtil.timingUtils;
import flash.display.BitmapData;
import flash.display.Shader;
import flash.display.ShaderJob;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Matrix;
public class flashFlash extends Sprite
{
[Embed ( source="pixelbender/bilinearresample.pbj", mimeType="application/octet-stream" ) ]
private static var BilinearScaling:Class;
public function flashFlash( ):void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener( Event.ENTER_FRAME, efCb, false, 0, true );
}
private function efCb( evt:Event ):void
{
removeEventListener( Event.ENTER_FRAME, efCb, false );
traceTime( "init" );
var srcBmd:BitmapData = new BitmapData( 80, 120, false, 0 );
var destBmd:BitmapData = new BitmapData( 160, 240, false, 0 );
var mx:Matrix = new Matrix( );
mx.scale( 2, 2 );
for (var i:uint = 0; i < 3000; i++)
{ destBmd.draw( srcBmd, mx );
}
traceTime( "scaled with as3" );
// create and configure a Shader object
var shader:Shader = new Shader( );
shader.byteCode = new BilinearScaling( );
shader.data.scale.value = [2];
shader.data.src.input = srcBmd;
for (var j:uint = 0; j < 3000; j++)
{
var shaderJob:ShaderJob = new ShaderJob( );
shaderJob.shader = shader;
shaderJob.target = destBmd;
shaderJob.start( true );
}
traceTime( "scaled with pixel bender bilinearresample.pbj" );
}
private static var _lastTraceTime:Number = new Date().getTime();
public static function traceTime( note:String ):Number
{ var nowTime:Number = new Date().getTime();
var diff:Number = (nowTime-_lastTraceTime);
trace( "[t" + diff + "] " + note );
_lastTraceTime = nowTime;
return diff;
}
}
}
And the pixel bender code:
<languageVersion : 1.0;>
kernel BilinearResample
< namespace : "com.brooksandrus.pixelbender";
vendor : "Brooks Andrus";
version : 1;
description : "Resizes an image using bilinear resampling. Constrains aspect ratio - divide Math.max( input.width / output.width, input.height / output.height ) and pass in to the scale parameter";
>
{
parameter float scale
<
minValue: 0.0;
maxValue: 1000.0;
defaultValue: 1.0;
>;
input image4 src;
output pixel4 dst;
void
evaluatePixel()
{
// scale should be Math.max( src.width / output.width, src.height / output.height )
dst = sampleLinear( src, outCoord() * scale ); // bilinear scaling
}
}
© Stack Overflow or respective owner