Compiling C#/XNA to run in Web Browser as JavaScript

Today I attended a really interesting presentation at GDC Online in Austin, TX titled “Compiling C++ and C# Games to the Web“. The authors of the talk are Alon Zakai and Kevin Gadd.

The concept is that there are now some fairly sophisticated compiling tools that will translate either C++ or C# into efficient JavaScript. In the case of C++, the graphics are converted from OpenGL (ideally OpenGL ES2.0) to WebGL. In the case of C#, there is a subset of the XNA compatibility for graphics.

Alon is the expert on the C++ conversion. The compilation tool he uses is Emscripten which has a command line compiler similar to gcc. In his half of the presentation, he demonstrated a full 3D game running in the browser, ported from the C++ code with only a few minor tweaks. The original code was written in C++/OpenGL ES 2.0. In fact, you can play the game in your browser here.

Because of the current limits of WebGL, Chrome and Firefox work automatically because WebGL is enabled in those browsers. For Safari and Opera, you’ll need to turn on WebGL. For IE, Microsoft has not committed to support the WebGL standard.

Kevin is the expert on the C# conversion. The compilation tool he uses is JSIL. He demonstrated a series of XBLIG 2D titles that were compiled to JavaScript, including both games he had written, as well as games written by others.

In both cases, some optimization was needed, but not much. There are a few minor limitations (like no DirectX conversion available for C++). In addition, you’ll see a performance loss of between 1.5x to 6x depending on what kinds of code you write (although this is improving with both speakers expecting to see significant increases on the browser side by mid-2013.

In C#, Kevin mentioned an issue with the use of getters/setters and performance in JavaScript. However, in these cases there are workarounds. In his samples, Kevin explained that it only took a few minutes to compile the game into JavaScript, then a day working through some optimization. I’m sure the optimization wouldn’t be as big of an issue if planned to use the JavaScript compiling from the beginning of your project.

The slides from the presentation can be found at http://jsil.org/GDCOnline2012.

You can be rest assured that if I ever get some free time, I’ll be trying the C#/XNA conversion to JavaScript for myself. The C++/OpenGL conversion would be a great potential project for one of the Champlain College senior game programmers to plan to try an implement in Console Programming in the Spring.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.