开发者

Emulating a nested loop in Pixel Bender for Flash/Flex

I have two vectors of a large, but arbitrary (v1.length need not equal v2.length), number of points, and I want to pairwise multiply them. Because this is the main bottleneck in a large loop of AS3 code, I thought I would try to pull out the code and run it async via Pixel Bender. Now, to represent the data I would have two image2 variables as input.

How can I get a pixel from one image and do the calculations with each pixel of the other image? I am just l开发者_JS百科earning Pixel Bender, so I may have overlooked some work around for looping.

EDIT: Perhaps I need to clarify. Let's say I have something like this

var v1:Vector.<Point> = ...;
var v2:Vector.<Point> = ...;
var result:Vector.<Point> = ...;
for (var i:int = 0; i < v1.length; ++i)
    for (var j:int = 0; j < v2.length; ++j)
        result[i] += v1[i] * v2[j];

This is a nested loop--how can I emulate it in Pixel Bender?


Loops and nested loops (other than those built-in) are prohibited in pixel bender for flash.

http://forums.adobe.com/thread/840318

http://blog.leeburrows.com/2011/02/pixelbender-filters-3/

I guess the exception is pixelbender 3D, which is in beta and designed to be used with the molehill API's.

EDIT

Here are links that are official Adobe docs that explicitly confirm the above:

http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pixelbender/pdfs/pixelbender_guide.pdf

From page 17:

Pixel Bender in Flash Player does not support: -> loops or control structures other than if and else.

This was a real bummer for me when I found out myself and I think this is a major failure on Adobe's part.


The main function inside a pixel bender kernel is a loop, and is called back for every single pixel being evaluated by the kernel. Here is a link to a tutorial on how do to exactly what you want (working with multiple inputs).

http://www.adobe.com/devnet/pixelbender/articles/creating_effects_pt09.html#articlecontentAdobe_numberedheader

Essentially it just comes down to defining two inputs:

<languageVersion : 1.0;>

kernel blendy
<   namespace : "com.adobe.devnet.pixelbender";
    vendor : "Kevin's Filters";
    version : 1;
    description : "mashes two inputs together";
> 
{
    input image4 src;  //Input image 1 as image4 (RGBA)
    input image4 src2; //Input image 2 as image4 (RGBA)
    output pixel4 dst; //Single pixel data type/represents single pixel value (RGBA)

    void evaluatePixel()
    {
       dst = sampleNearest(src,outCoord());
    }
}

Note that the two parameters of sampleNearest are the source image, and the coordinates of the pixel to sample. outCoord() I believe is simply the current pixel within the loop. As mentioned, evaludatePixel (to my understanding) is called once per pixel that exists in the input. Here is a modified version of the above code (from the link) that is reading the value of both inputs at the same time:

<languageVersion : 1.0;>

kernel blendy
<   namespace : "com.adobe.devnet.pixelbender";
    vendor : "Kevin's Filters";
    version : 1;
    description : "mashes two inputs together";
> 
{
    input image4 src;  //Input image 1 as image4 (RGBA)
    input image4 src2; //Input image 2 as image4 (RGBA)
    output pixel4 dst; //Output image

    void evaluatePixel()
    {
       dst = sampleNearest(src2,outCoord()) + sampleNearest(src, outCoord());
    }
}

Here are two video tutorials that will explain much more about how pixel works at length:

http://gotoandlearn.com/play.php?id=83

http://gotoandlearn.com/play.php?id=84

http://www.gotoandlearn.com/play.php?id=95

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜