Sunday, December 23, 2012

Use HxSL/Stage3D to Create 2D Filters/Shaders

Update on 2014/01/04:
Many things changed since the original post was published. Now HaXe is 3.0 and HXSL reaches 2.0. So here is the updated source code of the example for HaXe 3 and HXSL 2:

Maybe you know how to use Pixel Bender in your Flash project to create some custom filters. Alternatively, with the new Stage3D API introduced in Flash 11, we can write 2D filters running with GPU. Here is the example:

The idea is simple: draw a plane using two triangles, upload the images you want to process as textures and do all the magic in your fragment shaders. I recommended you to try HxSL, an advanced shader language which has similar grammar to the Pixel Bender language. The above example - Poster Effect is ported from the Pixel Bender filter with only a few modifications of the original Pixel Bender filter code.

The full source code of the above example (HaXe/HxSL):
The source code of the Poster Shader in Pixel Bender Language:

Announcing HxSL:
Using Flash 3D API:


  1. hi.. just wanted to ask if this can run on ios? with the pixel bender pbj files?

    1. The example will run as a swf, so will not work on IOS, until HxSL can compile to IOS. However, the idea will work on IOS, because you can create apps using AIR and Stage3D. And please note that the above example has nothing to do with pixel bender, it is Stage3D.

  2. Have you managed to get this example to run in Haxe3? With a little work, I got it to compile, but it crashes with an assert when I run it. Shame, because it's close to exactly what I need for my project :(

    1. Updated for HaXe3 and HXSL2:
      P.S. It seems the line "c = int(c);" will cause some runtime error, I'm not sure whether this is a bug or something else.

    2. The new link:

  3. That is really very informative and useful blog. Need to keep this script for future reference. Thanks for sharing it and keep posting