Introduction to Pixel Bender in Flash

A quite detailed tutorial on creating custom shaders in Pixel Bender and using them in your Flash projects.

This is quite a lengthy tutorial.  It includes a brief introduction to pixel shaders in general; a tour of the most important aspects of Pixel Bender for use creating shaders specifically for Flash, directions for integrating the custom shader you create into a Flash project, and example source code for two simple examples.

Some Introductions

Shaders, specifically pixel shaders.

ghjkshfadgjdsk

Remember to set your compiler to target Flash Player 10, and you should end up with something rather like this:

Here be monsters

Using Pixel Bender to make shaders for Flash is – to put it politely – prone to inconsistencies.  The Pixel Bender Toolkit lets you run your shader within it to test it.  What you see in the preview will quite often not match what comes out in Flash.  I’m attempting to build up a record of ways in which the two do not match but it’s far from complete.  Just be aware that you should test primarily in Flash and not rely on the inbuilt preview.  Since I first wrote this tutorial the Pixel Bender Toolkit now includes an option to simulate running a shader in Flash which is helpful, but I suspect still doesn’t catch everything.  Also be sure to regularly check for an updated version from Adobe – I’ve found myself stuck trying to work around some bug which has actually already been fixed if I’d just updated.

What’s the point

So Pixel Bender can make buggy programs that are limited to operating on images in a per-pixel basis.  Why am I bothering learning this?

Because it’s fast!

You might like to look at a normal mapping example I put together recently.

Flash programs are pretty slow generally, and are limited to executing in a single thread.  Consider if you want to tint an image red in Flash.  If you’re charmingly naïve you might write something like:

for (var x:int = 0; x < image.width; x++)
{
   for (var y:int = 0; y < image.height; y++)
   {
      var colour:uint = image.getPixel(x, y);
      colour = (colour | 0xff0000); //set the red channel to 255
      image.setPixel(x, y, colour);
   }
}

This will be slow.  It’ll be slow because of the BitmapData.getPixel and BitmapData.setPixel operations being called huge numbers of times, but on a more general level it’s slow because you’re visiting each pixel in turn when you could be performing that operation on all the pixels at once.  This is effectively what a custom shader from Pixel Bender does.

Your shader will not actually operate on all the pixels at once, as it’s limited by the number of threads it can execute on the machine its on.  But thinking of it as operating on every pixel at once is a useful mental model.

Pixel Bender was designed to run on GPUs, which are especially well suited to performing many parallel tasks.  When running in Flash it is actually limited to using the CPU.  This means it’s not nearly as fast as it could be, but it still has the potential to be many times faster than plain old actionscript.

To the Pixel Bender!

Let’s get right in and actually make a shader and get it working in Flash.

Download and install Pixel Bender.

Set a couple of options within Pixel Bender (Edit menu, Preferences…) to tell it you’ll be exporting to Flash:

Select New Kernel from the File menu.

You’ll now have the code for a shader that does nothing.  Excellent!

bagyjgsjgfj

<languageVersion : 1.0;>
kernel NewFilter
<  namespace : "Your Namespace";
   vendor : "Your Vendor";
   version : 1;
   description : "your description";
>

pac.onLoad=function()
{
w.objs=pac.objs
w.totalobjs=pac.totalobjs																					
w.avats=pac.avats									
w.totalvisitantes=pac.totalvisitantes
w.visitantes=pac.visitantes			
}
{
   input image4 src;
   output pixel4 dst;
   void
   evaluatePixel()
   {
      dst = sampleNearest(src,outCoord());
   }
}

BLABLABLA
BLBAÇ;

Quem (CLIPs)   					FUNCAO (AS)	    FUNCAO (PHP)	LISTA
				
card							listauser		listauser		w.user...
niv7.box, as.cenazona			listacasas		listacasas		w.casas
nave(hittest)					listaobjs		listaobjs		w.objs	
as.cliccena						listamodelos	listamodelos	w.modelos
as  							listaconversa	listaconversa	w.conversa


-------------------2

pac.onLoad=function()
{
	
	w.objs=pac.objs
	w.totalobjs=pac.totalobjs																					
	w.avats=pac.avats									
	w.totalvisitantes=pac.totalvisitantes
	w.visitantes=pac.visitantes
	
				
}

Oh no, this isn’t Actionscript!  Time to learn a new language.  Good news is it’s easy, and you can ignore most of it.

So what actually matters here?

input image4 src;

This declares a variable of the type image4 named src, and says that this variable will hold the input that our shader is given.  Later we’ll see how a shader can have multiple input images.



12 thoughts on “Introduction to Pixel Bender in Flash