blog
Pixel Bender Inputs – Vector or ByteArray
Pixel Bender for Flash Player lets you play some more advanced games. Based on your needs you can force your kernels to “eat” not only BitmapData, but also ByteArray-s or Vector-s. With this knowledge, you can simply use Pixel Bender kernels for some fast math or processing like 3D engines (3D to 2D projection) etc. Lets have a look at some simple demos, how to push vector and raw bytes directly into shader via ShaderJob:
Vector.<Number>
Simple (one-dimension) vector may be recognized by pixel bender as 3-channel input just as simple as providing correct width/height for shader:
var src:Vector.<Number> = new Vector.<Number>();
while(someLooping)
{
src.push(x);
src.push(y);
src.push(z);
}
var shader:Shader = new Shader(code);
shader.data.src.input = src;
shader.data.src.width = src.length / 3; // forces pixel bender to use image3 as input
shader.data.src.height = 1;
var res:Vector.<Number> = new Vector.<Number>();
var job:ShaderJob = new ShaderJob(shader, res, src.length / 3, 1);
job.start(true);
for(var i:uint=0; i<src.length; i+=3)
{
x = res[i];
y = res[i+1];
z = res[i+2];
}ByteArray
If you are more in “low-level” things, try using raw bytes as input. Do not forget to setup LITTLE_ENDIAN for source and result variables. Also make sure width*height*4 results in source bytes length.
var src:ByteArray = new ByteArray();
src.endian = Endian.LITTLE_ENDIAN;
src.position = 0;
while(someLooping)
{
src.writeFloat(x);
src.writeFloat(y);
src.writeFloat(z);
}
var shader:Shader = new Shader(code);
shader.data.src.input = src;
shader.data.src.width = 256; // e.g.
shader.data.src.height = src.length/4/256;
var res:ByteArray = new ByteArray();
res.endian = Endian.LITTLE_ENDIAN;
var job:ShaderJob = new ShaderJob(shader, res, 256, src.length/4/256);
job.start(true);
res.position = 0;
for(var i:uint = 0; i<src.length/4; i++)
{
x = res.readFloat();
y = res.readFloat();
z = res.readFloat();
}Lets have a look at how pixel bender handles input and outputs. As with current version of pixel bender/flash player it seems like:
'res' : cannot have 1 or 2 channel outputs… but you can use 3 or 4 channel outputs (float3, pixel3).
<languageVersion : 1.0;>
kernel threed <namespace : "sk.yoz";vendor : "Yoz";version : 1;>
{
input image3 src;
output float3 res; // or pixel3
void evaluatePixel()
{
res = sample(src, outCoord());
// we have res.x, res.y and res.z
}
}Where to go from here:
Using Pixel Bender to calculate information
Number crunching with as3 and pixelbender
Using Pixel Bender with Flash Builder 4 as a number crunching engine
Flash 10, Massive amounts of 3D particles with Alchemy (source included).
Edgar — 2010-08-02: http://www.adobe.com/devnet/flex/articles/flashbuilder4_pixelbender.html
Jozef Chúťka — 2010-08-02: @Edgar thnx, good source. Added to article
karanvir singh — 2010-09-14: hii can any one say whether can i play games that run with shader pixel 3.0(assassin 2 so on….) in a graphic card that has shader pixel value 2.0. please suggest.
Jozef Chúťka — 2010-09-14: sory karanvir singh, this is a very different technology
sastradhar — 2011-10-28: if i want to pixel bender to work on unsigned short as input can you tell me what should be the width and height of the source i need to pass.
Jozef Chúťka — 2011-10-31: sastradhar, you have to use Numbers as input, pixel bender does not work with integers. width * height * input type must match the input data length