Skip navigation
Currently Being Moderated

Converting to Vector.<Number>

Apr 20, 2009 6:22 PM

I'm interested in using Alchemy to implement the parts of a 3D pipeline that are not already done by the platform.

 

The platform does projection (using the Utils3D.projectVectors) and drawing (using Graphics.drawTriangles).  Both of these, I assume, are optimal and cannot be written to be faster using either AS3 or Alchemy compiled code (if not, they should be).  However, there are parts of the pipeline that would benefit from faster code, such as frustum clipping.

 

The problem is that the Utils3D.projectVectors and Graphics.drawTriangles take Vector.<Number> as input/output parameters.  According to this benchmark:

 

http://arcanearcade.blogspot.com/2009/03/alchemy-bytearray-faster-than -vectors.html

 

Alchemy is much slower at accessing a Vector.<> object than doing so within AS3 (400ms vs. 80ms in the benchmark).

 

The question is; is there a way of putting data into a Vector.<Number> from within Alchemy compiled code that does not impose this large performance penalty?  or is it possible to convert a ByteArray into a Vector.<Number> with minimal penality? (e.g. something like Vector.fromByteArray?)

 

Thanks

Colin Breame

 
Replies
  • Currently Being Moderated
    Apr 24, 2009 5:32 AM   in reply to Colin Breame

    The platform does projection (using the Utils3D.projectVectors) and drawing (using Graphics.drawTriangles).  Both of these, I assume, are optimal and cannot be written to be faster using either AS3 or Alchemy compiled code (if not, they should be). 

    I would not necessarily assume this.  Flash's code for 3d graphics is probably going to be optimised first for work with 3D display objects baked by adobe's design tools, and second for advanced actionscript developers.

     

    Alchemy is much slower at accessing a Vector.<> object than doing so within AS3 (400ms vs. 80ms in the benchmark).

     

    The question is; is there a way of putting data into a Vector.<Number> from within Alchemy compiled code that does not impose this large performance penalty?

    The most efficient way to work with vectors inside alchemy is using inline assembly to embed actionscript in your c/c++.  For example, something like

    int index, value, result;
    asm("myVector[%0] = %1;" : : "r"(index), "r"(value)); // myVector[index] = value;
    asm("%0 myVector[%1];" : "=r"(result) : "r"(index)); // result = myVector[index];
    

    should have comparable speed to normal actionscript vector access.  If you're swapping between alchemy and actionscript, it would probably be simplest to declare myVector at file scope, as

    asm("Vector.<Number> myVector = new Vector.<Number>(1024, true);");
    

    outside any functyion block, but you could also use the normal alchemy marshaling functions.  These of course would have additional overhead.

     

    is it possible to convert a ByteArray into a Vector.<Number> with minimal penality? (e.g. something like Vector.fromByteArray?)

    Not that I am aware of.  The most efficient way would perhaps be to use inline assembly to read each element from alchemy memory as above.  These will translate to optimised bytecodes for reading numbers from bytearray that were added to Flash 10 specifically for Alchemy; pure actionscript does not yet have access to them.

     

    You should also explore the approach of not using Adobe's functions for transformation and drawing at all.  Another developer found a massive speed increase by using solely Alchemy and Pixel Bender to perform all the 3d manipulations.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2011 3:49 PM   in reply to zazzo9

    could you  please clearify how to access the vector in as3?

     

    you previously wrote:

     

     

    ... should have comparable speed to normal actionscript vector access.  If you're swapping between alchemy and actionscript, it would probably be simplest to declare myVector at file scope, as

     

    asm("Vector.<Number> myVector = new Vector.<Number>(1024, true);");
    

     

    outside any functyion block, but you could also use the normal alchemy marshaling functions.  These of course would have additional overhead. ...

     

     

     

    i added that line to the global scope in my cpp file, but now how do i access it on the as3 side?

     

    i assumed it to be:

     

    --- as3 code begin -------------------

     

     

      var loader:cmodule.test_alc.CLibInit = new cmodule.test_alc.CLibInit();

      var test:Object = loader.init();

     

      var v:Vector.<Number> = test.myVector; // doesn't work

     

    --- as3 code end -------------------
    but that member doesn't exist. what am i missing?
    thanks.
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points