gpt4 book ai didi

flash - 使用 PixelBender 将位图大小加倍

转载 作者:行者123 更新时间:2023-12-03 01:35:06 24 4
gpt4 key购买 nike

我有一个关于像素弯曲器的性能问题。我想放大许多 BitmapData(将它们的大小加倍为新的 BitmapData)。我是用 as3 做的,但想使用 Pixel Bender 来获得更好的性能。在我的机器上,我从像素弯曲器中获得了更好的性能demonstrations然后是as3。

令我惊讶的是(或者糟糕的编码/理解),我的像素弯曲器的性能差很多 - 2 秒 vs 1/2 秒!我期望获得至少相同与 as3 的性能。我做错了什么?

我得到了简单的像素弯曲器代码 here (为了方便引用,它包含在下面)。

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;
}

}
}

像素弯曲器代码:

<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
}
}

最佳答案

我认为问题在于您实际上是将 Pixel Bender 与 native 播放器代码进行比较,而不是与“actionscript”进行比较。我怀疑 Pixel Bender 在这种情况下是否会获胜。

此处发生的缩放destBmd.draw( srcBmd, mx ); 直接在播放器中编码;这可能是您能达到的最快速度(使用等效算法)。另一方面,您的内核至少必须首先编译(或 JIT 编译)才能运行(可能还有许多其他原因导致它变慢;不过,我不太了解具体细节)。

查看来自 the blog of a Flash Player engineer 的这篇文章:

A long time ago, back in Flash Player 8 days we had the idea of adding a generic way to do bitmap filters. Hard coding bitmap filters like we did for Flash Player 8 is not only not flexible, but has the burden of adding huge amounts of native code into the player and having to optimize it for each and every platform. The issue for us has always been how you would author such generic filters. Various ideas were floating around but in the end there was one sticking point: we had no language and no compiler. After Macromedia’s merger with Adobe the Flash Player and the Adobe Pixel Bender team came together and we finally had what we needed: a language and a compiler.

因此,基本上,Pixel Bender 比直接在 Actionscript 中操作像素更快。它将胜过无数次 setPixel 和 getPixel 调用。但它不会比玩家本身更快(同样,使用相同的算法)。

在某种程度上,您想要做的事情就像在 PB 中编写发光滤镜一样。当然,它很酷,如果您对图像处理感兴趣,您可以从中学到很多东西。但是,如果您的过滤器旨在像 native 过滤器一样工作,那么除了教育目的之外,它没有太多意义: native 过滤器会更快并且已经可用,无需额外的代码行。

关于flash - 使用 PixelBender 将位图大小加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2853257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com