Devlogs
Since last post about Alchemy, I have successfully ported SFFE library. It was ported with use of real number arithmetics. I'm not planning to port it with use of complex arithmetics. Why ? I will explain this later on. First I will write some words about ported library.
To port SFFE i have used GlueGen script to produce C source to be compiled, and AS3 library class. GlueGen is a command line to generate Alchemy interface between a C/C++ library and ActionScript, it is included in Alchemy package. GlueGen takes a special glue description file on input. On output we get C file to be compiled, and AS3 class to use ported library in Action Script. There is hardly any technical documentation about GlueGen. Developers have to relay on example codes and codes of ported libraries to learn more about it. I will write about how does GlueGen description file is organized in next Alchemy post (I still dont understand all of this).
After porting SFFE i have used it to color a single BitmapData, here I used method discussed on Alchemy forum and described by Francis Bourre on his blog. Below You can see a working example (You need to have Flash Player 10 installed to run it)
Flash player (ver. 10.0.0) is required to see this content. Please update to newest possible version.
Use TextField to enter function to be parsed. Next, based on values of parsed function, BitmapData pixels are created. This is all done in C and 'returned back' to FP. Valid variables in function definition are 'x', 'y' and 'a'. First two are defined to be in range from -PI/2 to PI/2. Last variable is a random number in betwwen 150 and 250. Use 'sffe lib' button to use entered function.
'Native' button implements the same scheme in pure AS3 using compiled in function '150cos(y)sin(x)'. This can be used for performance testing.
As can be easily seen ported library works 'slow'. This is also the answer why I will not try to port SFFE with complex number support.
SFFE works great with C, but is slow when ported to AS3. Conclusion - Not every library should be ported.
Code illustrates how C code is called from AS code. sffe is a AS3 class generated with GlueGen with only static function.
1 var sf:uint = sffe.parser(); //create new parser, and store pointer to it 2 var v1:uint = sffe.regvar( sf, "x" ); // register variables 3 var v2:uint = sffe.regvar( sf, "y" ); 4 var v3:uint = sffe.regvar( sf, "a" ); 5 6 sffe.parse( sf, expression ); //parse expression (error checking is missing here) 7 sffe.setvarval( v3, 150+100*Math.random() ); //set random value of 'a' parameter 8 9 var t:uint = getTimer(); 10 11 sffe.eval(sf); //execute C code, that produces new BitmapData pixels 12 13 t = getTimer() - t; 14 log.text = "Time : "+t+"ms."; 15 16 ram.position = ramptr; 17 bd.setPixels( bd.rect, ram ); 18 19 sffe.pfree(sf); //free parser
Where ram is cmodule ram ByteArray, and ramptr is a pointer the bitmap data buffer created in cmodule.
Most important function used in this example is eval, it is implemented in C and shown below
1 public function eval( s:uint ):Number 2 { 3 sffe *parser = (sffe*)s; 4 sfopr *optro, *optr, *optrl; 5 sfNumber *x = sffe_varptr( s, 'X'); 6 sfNumber *y = sffe_varptr( s, 'Y'); 7 int i,j; 8 sfNumber hpi = 2*atan(1); // PI/2 9 *y = *x = -hpi; 10 sfNumber dx = hpi/160; 11 sfNumber dy = hpi/120; 12 sfNumber fv; 13 int line = 1, row = 1; 14 unsigned char r,g,b; 15 unsigned int *pxptr = (unsigned int*)buffer; 16 while ( row oprs; 17 optrl = parser->oprs+parser->oprCount; 18 optro = optr; 19 for ( optr=optr; optr!=optrl; optr+=1, optro+=1 ) 20 { 21 optro->arg->parg = optro->arg-1; 22 optr->arg->parg = optr->f( optr->arg )->parg; 23 }; 24 /* sffe_eval(...) - end */ 25 26 fv = ((unsigned int)*(parser->result)); 27 r = (int)(-120 + fv * 6 )%255; 28 g = (int)(20 + fv * 12 )%255; 29 b = (int)(2 - fv * 2 )%255; 30 *pxptr = 0xFF000000|(rresult); 31 }
Complete source code of SFFE porting definition file for GlueGen, and source of presented example can be downloaded here.
Build script is also included. It was tested on MacOS machine with Alchemy 0.5, but should easily compile on other supported platforms. Have fun!
This is interesting example, but unfortunately SFFE is useless in AS3 :)
If to update page (Firefox) the first test shows at me
SFFE Time: 105ms.
AS3 - Time: 117ms.
1) 'Native' button runs compiled script using function 150*cos(y)*sin(x). it doesnt change when You enter new value.
2) interesting, I always get SFFE about 2 times slower than native. I have tested this only under Mac OS X 10.5, Flash Player 10 Debug.
Here screenshots. Made in WinXP sp3 (firefox)
not debug version plugin.
Just check in OS Ubuntu8.10?
http://groups.google.ru/group/flexlife/web/sffe.PNG?hl=ru
http://groups.google.ru/group/flexlife/web/native.PNG?hl=ru
I think there is a bug (I did not see the source)
But with better alchemy load and unload
swf for the release of the memory
Internet Explorer results in even better
SFFE Time: 94 ms
Native Time : 125ms.
And almost no change.
Without a page refresh
SFFE Time: 94/109 ms.
Native Time : 110/109 ms.
Last night, I tested SFFE port on Windows and Ubuntu as well. In both cases with Flash Player 10 Release version. I got the same results as You. It seems to work slower on Flash Player Debug version, that I have used previously. Thanks for pointing me this out.
What is the bug You mentioned ?
about 3 years ago tester #0
Why on other values AS3 does not display change?
100*cos (y) 100*sin (x)