Saturday, October 7, 2017

actionscript 3 - How to use Pixel Bender (pbj) in ActionScript3 on large Vectors to make fast calculations?


Remember my old question: 2d game view camera zoom, rotation & offset using 'Filter' / 'Shader' processing?


I figured I could use a Pixel Bender Shader to do the computation for any large group of elements in a game to save on processing time. At least it's a theory worth checking.


I also read this question: Pass large array to pixel shader


Which I'm guessing is about accomplishing the same thing in a different language.


I read this tutorial: http://unitzeroone.com/blog/2009/03/18/flash-10-massive-amounts-of-3d-particles-with-alchemy-source-included/


I am attempting to do some tests. Here is some of the code:


        private const SIZE : int = Math.pow(10, 5);

private var testVectorNum : Vector.;

private function testShader():void
{
shader.data.ab.value = [1.0, 8.0];
shader.data.src.input = testVectorNum;

shader.data.src.width = SIZE/400;
shader.data.src.height = 100;
shaderJob = new ShaderJob(shader, testVectorNum, SIZE / 4, 1);

var time : int = getTimer(), i : int = 0;
shaderJob.start(true);
trace("TEST1 : ", getTimer() - time);
}

The problem is that I keep getting a error saying:


[Fault] exception, information=Error: Error #1000: The system is out of memory.


Update:


I managed to partially workaround the problem by converting the vector into bitmapData: (Using this technique I still get a speed boost of 3x using Pixel Bender)


    private function testShader():void

{
shader.data.ab.value = [1.0, 8.0];
var time : int = getTimer(), i : int = 0;
testBitmapData.setVector(testBitmapData.rect, testVectorInt);
shader.data.src.input = testBitmapData;

shaderJob = new ShaderJob(shader, testBitmapData);
shaderJob.start(true);
testVectorInt = testBitmapData.getVector(testBitmapData.rect);
trace("TEST1 : ", getTimer() - time);

}

Answer



I found a complete solution to this issue.


Please notice size is now 10,000,000


Here is the new code snippet:


        private const SIZE : int = Math.pow(10, 7);
private var testVectorInt : Vector.; //gotta use Number (not int)
private var testVectorInt2 : Vector.; //Need separate vectors for I & O




private function testShader():void
{
shader.data.ab.value = [1.0, 8.0];
var time : int = getTimer(), i : int = 0;
shader.data.src.input = testVectorInt;//testBitmapData;
shader.data.src.width = SIZE/4000;
shader.data.src.height = 1000;
shaderJob = new ShaderJob(shader, testVectorNum, SIZE / 4000, 1000);//(shader, testVectorInt2, SIZE/4, 1);
shaderJob.start(true);

trace("TEST1 : ", getTimer() - time);
}
}

You might be wondering what is the speed difference?


Processing the Numbers one by one in a loop took: 1956ms


Doing it in Pixel Bender took: 189ms


This results is true for Flash Player 10.3 on my pc


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...