Jan
08
2010

Pixel Bender Quirks

I just wanted to quickly document several issues I’ve had so far when developing with Pixel Bender in case anyone else runs into similar issues.

  • Reapplying an altered shader as a blend mode does not update a display object. The blend modes I was working with all had a percent parameter. If I changed the percent, then applied the altered shader to the blendShader property of the display object, nothing would happen. The way I could get it to update (I’m sure there are other ways) was to set the blend mode to “normal”, then assign the shader to the blendShader. Only then would the display object update.
  • When developing blend modes, order of the input declarations is important. This might seem obvious, but caught me up initially mostly because I started with blend modes that didn’t care about order, like add or multiply or difference. But when you are coding a blend mode where the order is important, remember that the first input declaration will be filled by the background pixels below your display object while the second declaration will be filled by the pixels of the display object you are applying the blend mode to.
  • For instance, with the following code it should be obvious which input will take the background pixels and which will take the foreground, or source display object:

    1
    2
    input image4 background;
    input image4 source;

    However, the names of course make no difference since you can name them whatever you like. Just don’t make the mistake of reversing the names:

    1
    2
    input image4 source;
    input image4 background;

    Despite the names, the “source” input here will still be filled with the background pixels below your display object while the “background” input will be filled with the pixels from the display object. So be careful!

  • normalize() bug? This is one of the two cases I’ve found where a function that worked fine within the Pixel Bender Toolkit didn’t work in the Flash Player runtime. This is so frustrating since everything appears fine when developing the kernel, and there’s no great debugging way to know why it fails in the Flash Player.
  • As an example, this worked for me in the Toolkit, but failed in the Flash Player:

    1
    2
    float3 p = float3(relativePos.x, relativePos.y, -z);
    p = normalize(p);

    This code, which performs the normalization, worked:

    1
    2
    3
    float3 p = float3(relativePos.x, relativePos.y, -z);
    float pLength = length(p);
    p = float3(p.x/pLength, p.y/pLength, p.z/pLength);

    Huh.

  • Nested min()/max() calls produce erroneous results. This is the other of the two cases when something worked fine in the Toolkit but not the Flash Player. When I had a max() call within another max() call (and the same with min()) I got bad data, but putting them in separate statements resolved the issue.
  • Broken:

    1
    float saturation = max(px.r, max(px.g, px.b)) - min(px.r, min(px.g, px.b));

    Works:

    1
    2
    3
    4
    5
    float maxSat = max(px.r, px.g);
    maxSat = max(maxSat, px.b);
    float minSat = min(px.r, px.g);
    minSat = min(minSat, px.b);
    float saturation = maxSat - minSat;

    Maybe I’m missing something in these broken examples, but the frustration was mostly in the fact that it all worked fine in the Toolkit, but produced a nice black image in the Flash Player. It would have been nice to at least have it broken in both places. :)

    • Share/Bookmark

    3 Responses to “Pixel Bender Quirks”

    1. I am reading your book AS3-Image Effects and like it very much. I tried ch 7 planets.as and have a couple of questions/comments. The rotation is very jerky. I looked at the aeon.animation routine for tweeen but did not completely understand the time segmentation between jumps. If I make the image smaller (1/2 size-not much help), make the timer longer (300,000-not much help), make the timer shorter (3000-better/ok). so the question is: is there any way to control the time between redraw so the motion is smooth but yet have a long rotation period so it looks like a real planet? This would allow a parameter for redraw periods and a seperate parameter for rotation time.

      Sorry for putting the question in this post but looking at your web site I could not find any place to leave a message other than on one of these articles.

      BTW. AS3-Animation Keith Peters has a very nice smooth scrolling version of planet rotation – pg 351.

    2. when using two input sources I’m getting very different results by selecting CPU or Flash at pixel bender toolkit, even for very simple codes.

      f.e.

      pixel4 samp1 = sampleNearest(src1, outCoord());
      pixel4 samp2 = sampleNearest(src2, outCoord());
      dst = pixel4(samp1.r – samp2.r, samp1.g – samp2.g, samp1.b – samp2.b, 1);

      Looks like it scales the second input. Do you know if it’s a know issue?

    3. I have not seen this exact issue myself, no, but I usually don’t run using the force CPU option. I wonder, though, if a number of the differences I have seen between running in Toolkit and in Flash would be more obvious with this setting.