本文介绍了使用PixelBender加倍的位图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于Pixel Bender的性能问题。我要放大很多的BitmapData(双其大小为新的BitmapData)。我和AS3这样做,但想用Pixel Bender的,以获得更好的性能。在我的机器,我得到像素的更好的表现出弯示威的那么AS3。

要我吃惊的是(或坏的编码/理解),我得到的更糟糕表现出来的Pixel Bender的 - 2秒VS 1/2秒!我期望得到的至少相同的性能AS3。我究竟做错了什么?

我得到了直观的Pixel Bender code 这里(它包括如下,以供参考)。

 包
{

进口flash.display.BitmapData;
进口flash.display.Shader;
进口flash.display.ShaderJob;
进口flash.display.Sprite;
进口flash.display.StageAlign;
进口flash.display.StageScaleMode;
进口对象类型:flash.events.Event;
进口flash.geom.Matrix;

公共类flashFlash扩展Sprite
{


[嵌入(来源=pixelbender / bilinearresample.pbj,MIMETYPE =应用程序/八位字节流)]
私人静止无功BilinearScaling:类;

公共职能flashFlash():无效
{
    stage.align = StageAlign.TOP_LEFT;
    在Stage.scaleMode = StageScaleMode.NO_SCALE时;

    的addEventListener(Event.ENTER_FRAME,efCb,假,0,真正的);
}

私有函数efCb(EVT:事件):无效
{
    removeEventListener(Event.ENTER_FRAME,efCb,假);

    traceTime(INIT);

    VAR srcBmd:的BitmapData =新的BitmapData(80,120,假,0);
    VAR destBmd:的BitmapData =新的BitmapData(160,240,假,0);

    VAR MX:矩阵=新的Matrix();
    mx.scale(2,2);
    对于(VAR我:UINT = 0; I< 3000;我++)
    {destBmd.draw(srcBmd,MX);
    }

    traceTime(缩放与AS3);

    //创建和配置Shader对象
    VAR着色:着色器=全新的Shader();
    shader.byte code =新BilinearScaling();
    shader.data.scale.value = [0.5]。
    shader.data.src.input = srcBmd;

    对于(VAR记者:UINT = 0; J< 3000; J ++)
    {
        VAR的ShaderJob:的ShaderJob =新的ShaderJob();
        shaderJob.shader =着色器;
        shaderJob.target = destBmd;
        shaderJob.start(真正的);
    }

    traceTime(按比例使用Pixel Bender bilinearresample.pbj);
}

私人静止无功_lastTraceTime:数=新的日期()的getTime()。
公共静态函数traceTime(注意:string):号码
{VAR nowTime:数=新的日期()的getTime()。
    VAR差异:数=(nowTime-_lastTraceTime);
    跟踪([T+差异+]+注);
    _lastTraceTime = nowTime;
    返回差异;
}

}
}
 

而Pixel Bender的code:

 < languageVersion:1.0;>

内核BilinearResample
<命名空间:com.brooksandrus.pixelbender;
    供应商:布鲁克斯安德鲁斯​​;
    版本:1;
    说明:使用双线性重采样的大小调整的图像约束宽高比 - 分Math.max(input.width / output.width,input.height / output.height),并在传递到尺度参数。
>
{
    参数浮规模
    <
        minValue(最小值):0.0;
        包括maxValue:1000.0;
        设置defaultValue:1.0;
    取代;

    输入为image4 SRC;
    输出pixel4 DST;

    空间
    evaluatePixel()
    {
        //规模应Math.max(src.width / output.width,src.height / output.height)
        DST = sampleLinear(SRC,outCoord()*标度); //双线性缩放
    }
}
 

解决方案

我认为这个问题是,你实际上是在比较的Pixel Bender对本土球员code,不是针对动作。我怀疑的Pixel Bender将永远赢得的情景。

这是发生在这里 destBmd.draw(srcBmd,MX)的比例; 是$ C $直接在播放CD;这可能是一样快,您可以得到(使用等效算法)。你的内核,另一方面也至少被编译(JIT或编译)首次以运行(可能还有一些其他的原因,它是比较慢的,我不很了解的细节,虽然)。

看看这个职位从一个Flash播放器工程师的博客:

所以,基本上,Pixel Bender的是不是直接在Actionscript中操纵像素更快。这将跑赢setPixel和getPixel调用等效数不胜数。但它不会比播放器本身更快(再次,使用相同的算法)。

在某种程度上,你正在试图做的是什么样的书面方式,也就是说,一个发光的PB过滤器。当然,这是很酷,你可以从中学到很多东西,如果你有兴趣的图像处理。但是,如果你的过滤器是为了工作,就像本机过滤器,有没有在里面多一点,除了教育目的:本机过滤器会更快,它已经可用,没有code一个额外的行

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 machine, I get better performance out of pixel bender demonstrations then as3.

To my surprise (or bad coding / understanding), I am getting much worse performance out of pixel bender -- 2 seconds vs 1/2 second! 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 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 = [.5];
    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
    }
}
解决方案

I think the problem is that you are really comparing Pixel Bender against native player code, not against "actionscript". I doubt Pixel Bender will ever win on that scenario.

The scaling that takes place here destBmd.draw( srcBmd, mx ); is coded directly in the player; that's probably as fast as you can get (using an equivalent algorithm). Your kernel on the other hand has at least to be compiled (or JIT compiled) first in order to run (probably there are a number of other reasons for it to be slower; I don't know much about the specifics, though).

Check out this post from the blog of a Flash Player engineer:

So, basically, Pixel Bender is faster than manipulating pixels directly in Actionscript. It will outperform an equivalent zillion of setPixel and getPixel calls. But it won't be faster than the player itself (again, using the same algorithm).

In a way, what you're trying to do is like writting, say, a glow filter in PB. Sure, it's cool and you can learn a lot from it if you're interested in image processing. But if your filter is meant to work just like the native filter, there's not much point in it, aside from educational purposes: the native filter will be faster and it's already available, without an extra line of code.

这篇关于使用PixelBender加倍的位图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 00:55