9 Replies Latest reply on Sep 22, 2009 4:40 PM by Mrinal Wadhwa

    Passing a ByteArray to a kernel

    Mrinal Wadhwa

      Hi Everyone,

       

      I'm trying to pass a ByteArray created from an array of colors ... to an embedded pixelbender kernel .. what is the best way to do this ?

       

      I have defined an second input parameter in my kernel expecting an image4

      input image4 src2;
      

      And I'm hoping to read  the colors that I recieved in src2 using sampleNearest.. (is this correct? )

       

       

      Here's what I've tried so far ... but that doesn't work ...

       

      // want this to have any number of colors
      var arr:Array = [0xff000000 , 0xff000001 , 0xff000002 , 0xff000004 , 0xff000005 , 0xff000006 , 0xff000008 , 0xff00000a , 0xff00000b , 0xff00000d]
      
      var input:ByteArray = new ByteArray();
      
      var l:int = arr.length;
      for(var i:uint;i<l;i++)
           input.writeFloat(arr[i]);
      
      _shader.data.src2.width = input.length >> 2;
      _shader.data.src2.height = 1;
      _shader.data.src2.input = input;                     
                
      
      

       

      Is this the write approach? if so what am I doing wrong?

       

      Thank you for your help

       

      Appreciate it,

      Mrinal

        • 1. Re: Passing a ByteArray to a kernel
          Mrinal Wadhwa Level 1

          Attached is a simple test project to reproduce the problem with the AS code and the pbk .. I get a runtime error that "The Shader input src2 does not have enough data"

           

          Thanks for your help,

          Mrinal

          • 2. Re: Passing a ByteArray to a kernel
            Mrinal Wadhwa Level 1

            Although I still haven't found a way to fix the problem ...

             

            one mistake I realized in the above code is that since I'm dealing with colors I should be using writeUnsignedInt in place of writeFloat otherwise colors above 0xFFFFFF00 or 4294967040 get truncated

            • 3. Re: Passing a ByteArray to a kernel
              Kevin Goldsmith Level 3

              I have found it oddly difficult to compute the appropriate size for a byte array for a ShaderJob. I'll take a look, thanks for the sample project.

              • 4. Re: Passing a ByteArray to a kernel
                Mrinal Wadhwa Level 1

                Thank you Kevin, looking forward to your response.

                 

                In future versions, It would be helpful if the errors provide a little more information ... right now the error says ..

                "The Shader input src2 does not have enough data" or "The Shader input src2 is too large" ... it would be nice if it told you how many bytes it was expecting and how many did it get.

                 

                Thank you,

                Mrinal

                • 5. Re: Passing a ByteArray to a kernel
                  Pradeek

                  Why a ByteArray? Wouldn't it be easier to pass a vector of colors?

                  1 person found this helpful
                  • 6. Re: Passing a ByteArray to a kernel
                    Mrinal Wadhwa Level 1

                    Hi Pradeek,

                     

                    Thanks for replying.

                     

                    Is there any other way of passing a Vector to a kernel ? ... I only know of a ByteArray sent as an image to the PB kernel ... that's what I did above ... you could replace the colors array I used above with a typed vector but everything else remains the same ... please let me know if I missed something

                     

                    Thanks,

                    Mrinal

                    • 7. Re: Passing a ByteArray to a kernel
                      Mrinal Wadhwa Level 1

                      If anyone else is looking into it ... thank you   ... attached is updated test project with some improvements and some trace statements ...

                      just 'cause I'm stuck, let me verify all my assumptions ....

                       

                       

                      • An image4 is expecting a 2D array where the child arrays are 4 bytes each?

                       

                      • So if I want to send 4 colors as input the length ofthee bytearray should be 16? ... 10 colors 40 bytes ... 20 colors 80 bytes

                       

                      • the width parameter should be the same as the number of colors?

                       

                      • the height is 1

                       

                       

                      The trace statements in the attached project validate the above is what is being sent as input .. it also prints out the whole array .. seems like what I should be sending.

                       

                       

                      Is there any way to debug what pixelbender is recieving before it thows an exception?

                       

                       

                      Thank you,

                      mrinal

                      • 8. Re: Passing a ByteArray to a kernel
                        Kevin Goldsmith Level 3

                        Ok, looking at your project now and a couple things strike me:

                         

                        1) are you only ever going to have a small number of values in your second input? Is there a reason that you wouldn't just pass these in as parameters?

                        2) The usage isn't one I've tried before, setting the second input manually, but applying it as an graphics filter. I'm not 100% that it would work the way you wanted it to if it was working.

                        3) You definitely need to be writing floats, not unsigned ints to the byte array. Pixel Bender inputs are floats.

                        4) The issue with not enough data in the second input might be because you are computing the width incorrectly, you can just try different values there to make sure, but because you are applying the shader as a filter, you might need both inputs to have the same amount of data. Like I said, I haven't tried doing what you are doing, so I'm not 100% on this.

                         

                        I'm having some problems running your project, but I think it is just because of the way my machine is set up at the moment. I'll keep playing around with it if I have more time.

                        1 person found this helpful
                        • 9. Re: Passing a ByteArray to a kernel
                          Mrinal Wadhwa Level 1

                          OK so this worked (yay !) ...

                           

                          var colors:Vector.<uint> = new Vector.<uint>();
                          colors.push(0xffff0000,0xff00ff00,0xff0000ff);
                                         
                          var w:int = colors.length;
                          var h:int = 1;
                                         
                          var colorsBd:BitmapData = new BitmapData(w,h);
                          colorsBd.setVector(new Rectangle(0,0,w,h),colors);
                          
                          _shader.data.src2.input = colorsBd;
                          _shader.data.src2.width = w;
                          _shader.data.src2.height = h;
                          
                          var f:ShaderFilter = new ShaderFilter(_shader);
                          image.filters = [f];
                          

                           

                          but this did not ...

                           

                          var colors:Vector.<uint> = new Vector.<uint>();
                          colors.push(0xffff0000,0xff00ff00,0xff0000ff);
                                         
                          var w:int = colors.length;
                          var h:int = 1;
                                         
                          var colorsBd:BitmapData = new BitmapData(w,h);
                          colorsBd.setVector(new Rectangle(0,0,w,h),colors);
                          
                          var bytearray:ByteArray = colorsBd.getPixels(new Rectangle(0,0,w,h));
                          
                          _shader.data.src2.input = bytearray;
                          _shader.data.src2.width = w;
                          _shader.data.src2.height = h;
                          
                          var f:ShaderFilter = new ShaderFilter(_shader);
                          image.filters = [f];
                          

                           

                          I don't have to create a ShaderFilter .. I even tried creating a ShaderJob I get the same results as above ... bitmapdata gets passed .. a bytearray throws an error.

                           

                          Kevin, as you said its probably becuse I'm not setting the right width ... I tried several guesses though but nothing worked.

                           

                          I finally found the docs here (link) and those are quite detailed and helped me a lot .. till now I was relying on the docs with the toolkit download and examples I found on the web ... but from these I realized that Pradeek above was correct I can just send a Vector.<Number> to the input ... I havent tried that though .. since Number is too small for colors .. but I could maybe send them without the alpha byte though.

                           

                          Thank you all for your help,

                          With regards,

                          Mrinal