问题
I'm looking for some help converting as3 code to pixelbender code in an attempt to improve the performance of my application.
This as3 code goes as follows. I scan through the Number values of a bytearray in chunks. Lets say this chunk lenght was 100 numbers I read 2 numbers (left and right) and try find the maximum values. The numbers in my bytearray are PCM data so there is millions of them and this code can often take a long time to execute, especially on a low spec machine.
The whole aim of this is to render a waveform as quickly as possible. I know very little about pixel bender. I can basically make a new file and create a shaderJob of it in flash but I'm really uncertain how to approach this...
I guess I'm really asking how do I pass pixelbender either
A) a "chunk" of numbers and get it to pass me back 2 maximum values (left and right)
or
B) my entire bytearray and get pixel bender to do the chunking stuff for me
var spritePixelIndex:Number=0;
var spriteSize:Number;
spriteSize=_sizes[_numberOfZoomLevels - 1];
blockSize=Math.floor(_pcmLength / spriteSize);
chunksize=blockSize * 100;
for (var i:int=0; i < chunksize; ++i)
{
if (_pcmData.bytesAvailable)
{
var la:Number=_pcmData.readFloat();
var ra:Number=_pcmData.readFloat();
var l:Number=la > 0.0 ? la : -la;
var r:Number=ra > 0.0 ? ra : -ra;
++_divCount;
var ml:Number=0;
var mr:Number=0;
var a_ml:Number=ml > 0.0 ? ml : -ml;
var a_mr:Number=mr > 0.0 ? mr : -mr;
ml=a_ml > (l) ? ml : l;
mr=a_mr > (r) ? mr : r;
}
}
回答1:
Pixel Bender is not the right tool for this task, since it does not support loops and thus cannot search through an array of data to return you single values. It can also not pass values over to the next processed chunk, which would be nice since then you could just compare the current chunk with the previously found maximum, like in a bucket chain.
Instead I recommend you to have a look at Alchemy and use its fast bytearray opcodes to quickly search through your data. Joa Ebert and Burak Kalayci have both build helper tools that allow you to use these opcodes even without the need to program in C: http://philippe.elsass.me/2010/05/as3-fast-memory-access-without-alchemy/
来源:https://stackoverflow.com/questions/3211755/help-converting-this-as3-code-to-pixel-bender-code