Sep 2, 2010
Thoughts on Alchemy, raytracing and life
So… alchemy… yeah. Install a bunch of crap on your system, write C/C++ code, compile it to a swc with gcc/g++, wait for the compiler to finish it’s work, use that heavy as hell swc in your AS3 code… realise calling alchemy code often is a performance killer, minimise calls to alchemy functions/code, write more C/C++ code…
It’s a little redundant to say the least, and thank god I use a Makefile…
All in all alchemy is a pain in the ass, in my opinion.
Anyway, I tried it out and got a small real time raytracer working, with a couple lights, some spheres and reflections. There is no way that would be possible in pure AS3 (well, I’m incapable of doing it in pure AS3), so there is no doubt alchemy give a pretty neat performance boost to AS3. Now that’s cool, and people have been doing some cool stuff with it. But then I think, if alchemy code if the same thing as compiled AS3, why the hell is it faster ? I’ve not really gotten into the details of what happens to the nice C code after the llvm has it’s way with it, but when I think about it, it’s like someone is asking me the meaning of life.
Ah well, who cares in the end, the performance is there, let’s take advantage of it.
About raytracing in AS3, there are a few better ones than mine out there. Frank is using alchemy, David too (I think), and Simo is using pixel bender, and I’ve done another one, not in real time and without alchemy, that I might finish one day if I get some time. It’s really cool to see where people are taking AS3, and pushing it to it’s limits.


Great work! Damn, now I’m gonna have to add multiple light sources too!
Alchemy is fast for more than just the gcc being able to optimize invariants, inlining, etc. It can also access Flash Player 10′s hidden fast memory, which AS3 (retardedly) doesn’t have access to (haXe on the other hand does). You can use Joa’s Apparat/TDSI tho, and still stick to AS3: http://blog.joa-ebert.com/2009/08/05/turbodieselsportinjection/
Hi there, this looks really good!
I have been dabbling in flash ray tracing for a bit over a year now, but recently gave the project a bit of a rest. A few weeks ago I decided to build a full on ray tracing library for flash which is coming together nicely. I currently haven’t used any Alchemy in the project but I would be very interested in using some, do you have any tips as I haven’t used it before.
Here is a link to some of the stuff I’ve done (the most recent two posts looking at the new library):
http://experimentalized.blogspot.com…h?q=ray+tracer
I also created a real time ray tracer a while ago in pure AS3 so that should be somewhere on the list.
Cheers
Sam
Well you’ll need to know a little bit of C/C++ and then it’s just a matter of installing the toolkit, nothing you can’t handle! Then from a few tests I did, the less calls you make to alchemy functions on each frame, the better the performance. There’s not much more about alchemy, just mess around with it!
Very nice raytracing scene on your blog by the way. You should consider using C/C++, you would get so much more power!
Hope this helps a little.
Hi Dominic,
I have worked with C++ quite a bit in the past so will definitely give it a go! You mention at the beginning of the article that quite often using alchemy calls can be a performance killer, how do I know what is best to use alchemy for and what isn’t, or is it just trial and error?
Just working on getting .obj 3D files into the ray tracer and efficiently drawing thousands of triangles. Its a bit of a headache but looking at octrees to boost performance in general for large numbers of objects. Will be pretty cool when if I manage to get it working. Full raytracing platform in flash wooh!
I’ll let you know how my ventures into Alchemy go, thanks for the help
The way I use alchemy, is that I basically do everything with C and then return a pointer to the memory location of my pixels back to AS3, that way I’m not “marshalling” anything between C and AS3. And from a few other tests I ran, using “pure” C is faster than C++. I was working on a Flash raytracer lib too, and stopped because even with nice datastructures and some alchemy, for every feature I was adding, rendering time was getting ridiculous! I’ve been contemplating porting the entire thing to C++ when I get some free time… one day!
But yeah, it would be nice to see your lib as you work on it, and even try it out
Thats exactly what I’m finding, just added “soft” lighting and thats a real drain. If you want a decent quality it means running all of the lighting rays 10-30 times as often. I think it will have to be a C/C++ port then. I had one render running for about 15 hours last night (but it did come out beautifully!)
So I guess the best way to carry out the trace is load everything into the C++ program, before the trace begins, run through the pixels, and then only return them at the very end.
If I did create a library that uses alchemy, and create an swc, will this be useable by anyone even if they don’t have alchemy installed?
The library is getting there, for some reason refraction is still going wrong, but I am enjoying the anaglyph 3D render feature (which just draws a scene from two view points and applies tint filters to each). I’ll put up a first release when I’ve fixed the refraction problem. Stay tuned!
The end user only needs flash player 10+, or 9+, can’t remember. Alchemy is only a toolkit you use to compile C to AS3, so there is no C code left at the end.
Just added a few more features and although not everything is working how I want it to I thought I’d send you a demo,
here are some renders: http://experimentalized.blogspot.com/2010/10/some-more-ray-tracer-features.html
and here is the library: http://www.experimentalized.com/astrace.html
I have installed alchemy now, so I’m going to start porting all of the features to C++. Any chance I could take a look at your C code for the raytracer?
At the moment nothing is documented so that is the next big task, also texturing isn’t quite there.
Enjoy!
Yep, I’m going to post a tiny “tutorial” on using alchemy with AS3 bitmaps, and I’ll post the source for my raytracer too, I’ll do this pretty soon.
Sounds great, it is really just getting started tranferring bitmapdata between the two that I need, so that’d be pretty ideal. Finally managed to get refraction working nicely, check it out:
http://experimentalized.blogspot.com/2010/10/material-properties.html
The main thing that is getting to me now is the lack of caustics but that would require photon mapping, something that flash certainly isn’t capable of.
I think I’m going to add a load of primitives (cones, cylinders, toroids etc) as my next project. Excited about getting a version running with Alchemy with real time refraction and environment textures though
!