FlasCChttp://blogs.adobe.com/flascc
Using Adobe Flash C++ CompilerMon, 18 Mar 2013 16:57:53 +0000en-UShourly1http://wordpress.org/?v=4.1.1FlasCC and Link Time Optimizationhttp://blogs.adobe.com/flascc/2013/03/18/flascc-and-link-time-optimization/
http://blogs.adobe.com/flascc/2013/03/18/flascc-and-link-time-optimization/#commentsMon, 18 Mar 2013 16:57:53 +0000http://blogs.adobe.com/flascc/?p=450Continue reading →]]>The FlasCC toolchain is closely modeled after a typical native C/C++ development toolchain. It contains all of the tools you would expect from such a toolchain, including a compiler (with preprocesser), an assembler, a linker, an nm symbol lister, an ar archiver, etc.

As with many such toolchains, FlasCC uses the preprocess, compile, assemble, link model. Each is an individual step, although the compiler driver may perform multiple steps with a single invocation.

In the following example, the compiler driver is used to go from a simple .c file to a final executable in a single command:

The compilation stage compiles the preprocessed C code into “assembly code.” In a native toolchain, this would result in x86, arm, etc. assembly code. For FlasCC, “assembly code” is ActionScript 3 (AS3) code.

The assembly stage “assembles” the ActionScript 3 code generated in the previous stage into “object code.” In actuality, it is doing a full compile of the AS3 code using the ASC2 compiler, resulting in an ActionScript Bytecode (or ABC) file. There is some additional work done to compile the single file into multiple AS3 “scripts” to allow lazy initialization of FlasCC modules at runtime. While the resulting file is actually ABC, the nm and ar utilities have been extended to understand them.

Link Time Optimization (LTO)

FlasCC is built on the LLVM Compiler Infrastructure and is therefore able to take advantage of LLVM’s powerful link time optimization features. LTO can, in many cases, result in substantially faster and smaller SWFs. LTO is enabled on a file-by-file basis by passing the –flto flag to the compiler driver for each source file that is to participate.

Using the –flto flag (or the –O4 optimization level which implies –flto) when compiling source into object code results in an LLVM bitcode file instead of an ABC.

FlasCC comes with both ABC and LLVM bitcode flavored standard libraries. So a fully LTO build (–flto or –O4 passed to the compiler driver for each source file and for the link step) will do link time optimization of the standard C and C++ libraries as well as object files generated from user code.

Differences Between LTO and non-LTO Builds in FlasCC

One big difference between LTO and non-LTO builds in FlasCC is where AS3 generation and compilation happens. In a non-LTO build, AS3 is generated and compiled to ABC as each source file is compiled. Subsequently, the generated ABCs are combined in the link step to create a SWF or executable. Compared to compiling AS3, combining ABCs is a relatively inexpensive operation. So compiling each source file is relatively expensive, but linking is relatively cheap. Changing a single source file in a project means recompiling that source file and relinking.

In an LTO build, each source file is compiled to LLVM bitcode. In the link step, the LLVM bitcode files generated from source are linked together into a single aggregate bitcode file. That bitcode file can then be optimized. Since bitcode from various source files have been combined into this single unit, optimizations can be applied across functions and data from multiple source files.

After optimization, the bitcode is then used to generate a single AS3 file that is then compiled with ASC2. This results in compile time characteristics that are the opposite of non-LTO builds. Here, compilation of source is cheap and linking in expensive. This also means that, since the generated AS3 depends on the aggregate LLVM bitcode file, and the aggregate LLVM bitcode file depends on each LLVM bitcode file derived from source (as well as bitcode for the C/C++ standard libraries in a fully LTO build), all AS3 must be regenerated and recompiled every time any source file in a project is changed!

So, as a general rule, it makes sense to use LTO for release-type builds because the longer compile time will result in smaller/faster SWF files. On the other hand if you are creating debug-type builds it makes sense to avoid LTO for faster compile times making it easier to iterate on the code.

Fixed Issues

Debug builds no longer fails at runtime with Reference Errors mentioning symbols with “THUNK” in their name

avm2_uithunk no longer fails when content was started using CModule.startAsync() due to incorrect assignment of thread ids

AS3_GoAsync() can now be used from main when started via CModule.startBackground()

Adding directories via the InMemoryBackingStore AS3 API with trailing slashes now works

]]>http://blogs.adobe.com/flascc/2013/02/07/flascc-1-0-1-released/feed/7Porting a C++/OpenGL game to run in the browserhttp://blogs.adobe.com/flascc/2013/01/18/porting-a-c-opengl-game-to-run-in-the-browser/
http://blogs.adobe.com/flascc/2013/01/18/porting-a-c-opengl-game-to-run-in-the-browser/#commentsFri, 18 Jan 2013 23:46:10 +0000http://blogs.adobe.com/flascc/?p=429Continue reading →]]>The Flash C++ Compiler (FlasCC) was designed to allow C/C++ developers to deliver existing C/C++ codebases via a web browser using the Flash Player. One common use case is to port an existing game written in C++ and OpenGL. The FlasCC engineering team has taken a popular C++/OpenGL game called Neverball and ported it to the web. This post demonstrates the game running in multiple web browsers and provides links to resources that will help you learn how to port your C++/OpenGL game to the web.

Neverball

The Neverball project is an open source computer game written in C++ using OpenGL. The goal is to tilt the floor to roll a ball around picking up coins and getting to the exit. You can download the native executable to try the game yourself from the Neverball Downloads page.

The Neverball Downloads page also allows you to download the source code for the game. You can try downloading the source code and using a native g++ toolchain to build a native executable.

Live Demo

Screencast

This screencast demonstrates the game in action across various web browsers:

]]>http://blogs.adobe.com/flascc/2013/01/18/porting-a-c-opengl-game-to-run-in-the-browser/feed/2Using printf within a SWChttp://blogs.adobe.com/flascc/2013/01/18/using-printf-within-a-swc/
http://blogs.adobe.com/flascc/2013/01/18/using-printf-within-a-swc/#commentsFri, 18 Jan 2013 21:54:49 +0000http://blogs.adobe.com/flascc/?p=335Continue reading →]]>A SWF built with gcc/g++ using the -emit-swf option has a default Console that prints stdout into a TextField on the screen (and to the Flash log file). However when you build a C/C++ library into a SWC using the -emit-swc flag there is no default Console. This means that if code in your library calls printf() no output will be shown. This post demonstrates how to use a custom Console to show the output of printf() calls from within a SWC.

This post assumes you already know how to compile a SWC in FlasCC. If you do not know how see the samples/05_SWC sample application that ships with the FlasCC SDK.

Let’s start with a very simple C library that will be compiled into a SWC:

Now this SWC is ready to be dropped into an ActionScript project in Flash Builder. If you were to then call that function from the ActionScript application you would notice that no output was written to the screen or traced out. In order to see this output we need to tell the library which object is available to receive the data. This is done by setting the CModule.vfs.console property to an Object that implements the ISpecialFile interface.

Here is a sample ActionScript application which itself implements ISpecialFile:

Typically this error is experienced when you have built a FlasCC SWF that uses multi-threading, but the version of the Flash Player that is running the SWF does not support multi-threading. Multi-threading in FlasCC requires at least Flash Player 11.5.

To fix this error you will need to upgrade your Flash Player to at least version 11.5.

If your application does not use any multi-threading features, but you are still experiencing this error check to make sure that you aren’t compiling with the -pthread flag. When you compile with -pthread you link in some thread-safe variants of parts of the standard library that require threading to actually be supported.

Note that multi-threaded code is not yet supported by Flash Player in Google Chrome (PPAPI). Google
and Adobe are working to provide support in a future version of Chrome.

]]>http://blogs.adobe.com/flascc/2013/01/11/troubleshooting-error-1065/feed/0Sample C++ libraries running in the browser via Flash Playerhttp://blogs.adobe.com/flascc/2013/01/04/sample-c-libraries-running-in-the-browser-via-flash-player/
http://blogs.adobe.com/flascc/2013/01/04/sample-c-libraries-running-in-the-browser-via-flash-player/#commentsFri, 04 Jan 2013 23:26:31 +0000http://blogs.adobe.com/flascc/?p=367Continue reading →]]>Learning by example is always the best way to understand a new tool. So with the FlasCC SDK we ship a number of fully working examples of open source libraries being compiled and used within SWFs. By reading the documentation and investigating these examples you should be able to get a good understanding of how best to use FlasCC.

These projects can be found in the /samples/ folder of the FlasCC SDK which can be downloaded from the Adobe Gaming Tools page.

Box2D

The Box2D example creates a SWC from the C++ Box2D library. SWIG is used to automate the process of generating all of the wrapper code which is incredibly helpful as it avoids you writing lots of tedious and bug-prone wrapper code by hand. Having the ActionScript API auto-generated like this can be an important timesaver if you need to update the underlying C++ code.

Bullet Physics

Bullet is a large C++ library with roughly 130 thousand lines of code. It is used throughout the industry for both offline and real-time physics simulations. It not only supports basic rigid-body physics, but also more advanced soft-body physics for simulating deformable objects such as cloth. Creating a SWC with an ActionScript API for Bullet is a trivial process and allows ActionScript developers high-performance access to this great library. The demo we ship with the FlasCC SDK shows how to create a simple 3D scene using the Away3D library and how Bullet can be used to provide physical simulation for the objects in the Away3D scenegraph.

Lua

Due to its small size and how easy it is to embed within other environments a lot of people are choosing Lua as their scripting language of choice. The reference implementation of the Lua interpreter is written in C and is designed to be compiled as a library and linked into other projects. As with the other examples we used SWIG here to automatically generate the AS3 bindings. We also write some interop code by hand that reflects the AS3 API into Lua so that Lua programs can be run within the Flash Player and can interoperate with the Flash API set. We wrote a simple real-world example that shows a Starling-based AS3 application that is controlled by a Lua script that can be updated while the SWF is executing.

Click “Run Script” to execute the Lua script. Note that you can edit the script live and click “Run Script” again to execute your latest changes. For example try changing the line “gravity = 5″ to “gravity = 0.01″ and click Run Script again to see your changes.

Quake 1

This was the killer demo for the original Alchemy prototype and showed that complex C code could be compiled correctly into a SWF with excellent runtime performance. The game might be old, but it still looks surprisingly good and performs better when compiled with FlasCC than it did with the old Alchemy prototype.

When the FlasCC binaries invoke the JVM, the default heap size is set to 2GB. If you are using FlasCC on a machine that does not have sufficient memory, you may see errors like:
Error: Failed to execute compiler: Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

If you run into these errors, you can tweak the JVM heap size by adding the -jvmopt option to the command line options, for example:
gcc -jvmopt="-Xmx1024M" hello.c -o -emit-swf -o hello.swf

Also if you are running on a machine that has spare memory, then you can use the same -jvmopt option to bump up the heap size from its default size.

]]>http://blogs.adobe.com/flascc/2012/12/20/troubleshooting-the-could-not-create-the-java-virtual-machine-error/feed/5Bringing OpenGL C/C++ code to the web with FlasCChttp://blogs.adobe.com/flascc/2012/12/14/bringing-opengl-cc-code-to-the-web-with-flascc/
http://blogs.adobe.com/flascc/2012/12/14/bringing-opengl-cc-code-to-the-web-with-flascc/#commentsFri, 14 Dec 2012 23:38:57 +0000http://blogs.adobe.com/flascc/?p=372Continue reading →]]>Stage3D allows GPU accelerated 3D content to run in the Flash Player accross operating systems and browsers. We have a number of interesting libraries that will help you bring your OpenGL C/C++ code to the web with FlasCC.

GLS3D

The GLS3D library is an open source project we started to provide a basic OpenGL implementation for FlasCC users targeting Stage3D.

FreeGLUT

The GLUT library was created to make it easier to write cross-platform OpenGL based apps. I’ve implemented a Stage3D backend for the FreeGLUT library and used it in conjunction with the GLS3D project to compile some of the popular NeHe OpenGL tutorials.

GLSL To AGAL

If you’ve investigated the Stage3D API you’ll know that the shader format is a custom bytecode format designed to be easily compiled down to DirectX bytecode or GLSL depending on the platform the Flash Player is running on. Sadly this makes it hard for humans to write. This project is based on the open-source Mesa codebase and contains a full GLSL parser and optimizer hooked up to a custom AGAL backend so that GLSL shaders can be compiled and optimized down to AGAL bytecode. It can be used offline as a pre-processing step within your applications asset creation workflow or at runtime by compiling the library to a SWC with FlasCC and using it in your SWF.

The repository also includes a precompiled SWC so that you can get started with it immediately.

Starling Filter Playground

Starling 1.2 supports AGAL based filters that can be applied to any Sprite to alter the way they render. Rather than writing the filter by hand with AGAL asm its possible to hook up the GLSL2AGAL project to Starling so that these Filters can be written using GLSL. Modifying the shader in the UI causes the filter to update in realtime.

]]>http://blogs.adobe.com/flascc/2012/12/14/bringing-opengl-cc-code-to-the-web-with-flascc/feed/1Operating on ActionScript objects in C++http://blogs.adobe.com/flascc/2012/12/08/operating-on-actionscript-objects-in-c/
http://blogs.adobe.com/flascc/2012/12/08/operating-on-actionscript-objects-in-c/#commentsSat, 08 Dec 2012 01:33:49 +0000http://blogs.adobe.com/flascc/?p=176Continue reading →]]>FlasCC allows you to create a SWC file from a C++ library. This is useful if you have an existing ActionScript project and want to add some logic that is already written in C++. This post demonstrates how you can operate on ActionScript objects that were passed as parameters into functions of a C++ library.

This post assumes you already know how to compile a SWC in FlasCC. If you do not know how see the samples/05_SWC sample application that ships with the FlasCC SDK.

The AS3++.h library exposes some functions to make it easier to operate on ActionScript objects within C++.

Getting a property

Getting the value of a property of an ActionScript object is pretty straightforward by using the getproperty() function.

Setting a property

Calling a method

Calling a method is slightly more involved as you need to first use getproperty() on the name of the method to get a Function var that represents that method. Then pass that object into the call() or call_v() functions.

Sample Code

Here is a sample C++ library that demonstrates how to do each of these operations.

When compiling this library using g++ remember to set the -lAS3++ flag as described in the AS3++ documentation.

#include <stdio.h>#include "AS3/AS3.h"#include "AS3/AS3++.h"//////// Get a property of an ActionScript object in C++//////void getDelta() __attribute__((used,
annotate("as3import:flash.events.MouseEvent"),
annotate("as3sig:public function getDelta(m:MouseEvent):int"),
annotate("as3package:MyLibrary")));void getDelta(){// marshall the AS3 parameter into a C++ variable
AS3::local::var mouseEvent;
AS3_GetVarxxFromVar(mouseEvent, m);// now that we have it in C++ let's get a property on that object
AS3::local::var propertyName = AS3::local::internal::new_String("delta");
AS3::local::var outputValue = AS3::local::internal::getproperty(mouseEvent, propertyName);// convert the var to a scalarint output = AS3::local::internal::int_valueOf(outputValue);// return that scalar value
AS3_Return(output);}//////// Set a property on an ActionScript object in C++//////void setDelta() __attribute__((used,
annotate("as3import:flash.events.MouseEvent"),
annotate("as3sig:public function setDelta(m:MouseEvent):void"),
annotate("as3package:MyLibrary")));void setDelta(){// marshall the AS3 parameter into a C++ variable
AS3::local::var mouseEvent;
AS3_GetVarxxFromVar(mouseEvent, m);// now that we have it in C++ let's set a property on that object
AS3::local::var propertyName = AS3::local::internal::new_String("delta");
AS3::local::var newValue = AS3::local::internal::new_int(5);
AS3::local::internal::setproperty(mouseEvent, propertyName, newValue);}//////// Call a method of an ActionScript object in C++//////void getString() __attribute__((used,
annotate("as3import:flash.events.MouseEvent"),
annotate("as3sig:public function getString(m:MouseEvent):String"),
annotate("as3package:MyLibrary")));void getString(){// marshall the AS3 parameter into a C++ variable
AS3::local::var mouseEvent;
AS3_GetVarxxFromVar(mouseEvent, m);// demonstrate how to call a function of that object from C++
AS3::local::var functionName = AS3::local::internal::new_String("toString");// first get the property of the object called "toString" which is a Function var
AS3::local::varfunction= AS3::local::internal::getproperty(mouseEvent, functionName);// then call that Function object (in this case it takes no parameters)
AS3::local::var output = AS3::local::internal::call(function, AS3::local::internal::_null,0, NULL);// Since AS3_Return (as shown in getDelta()) can only accept a scalar value we need// to marshall this C++ object into an ActionScript String and return it using AS3_ReturnVar
AS3_DeclareVar(asOutputString, String);
AS3_CopyVarxxToVar(asOutputString, output);
AS3_ReturnAS3Var(asOutputString);}int main(){// The SWC still needs a main() function. // See the code comments in samples/05_SWC for more details.
AS3_GoAsync();}

And here is a sample ActionScript application that demonstrates how to call the methods in the SWC generated from that C++ library:

]]>http://blogs.adobe.com/flascc/2012/12/08/operating-on-actionscript-objects-in-c/feed/10FlasCC 1.0 Releasedhttp://blogs.adobe.com/flascc/2012/12/04/flascc-1-0-released/
http://blogs.adobe.com/flascc/2012/12/04/flascc-1-0-released/#commentsTue, 04 Dec 2012 23:39:51 +0000http://blogs.adobe.com/flascc/?p=288Continue reading →]]>The Adobe Flash C++ Compiler (FlasCC) is a new tool chain that allows game developers to take native games and game engines for PCs, Xbox 360, PlayStation 3, and iOS and compile them to run directly on the web across browsers on over 1.3 billion connected PCs using Adobe Flash Player.
FlasCC 1.0 was officially released today as part of the Game Developer tools in the Adobe Creative Cloud.