How to Use ANGLE in your SDL Program

ANGLE is handy. Built to support WebGL in Chromium, it acts as a polyfill for OpenGL ES atop a plethora of other rendering APIs. Since it exposes a single graphics API you can build your application around, you might wonder about how you could use it with SDL, which provides a similarly “single API” experience for windowing and input. The answer is embarrassingly easy.

Going into this, I had thought that the proper way to use ANGLE would be to tell SDL not to initialize any kind of graphics API, and then I’d get out the native handles and give those to ANGLE, and then interact with ANGLE from then on. If you were using GLFW instead of SDL, I think you could do this – GLFW provides a NO_API value for its CLIENT_API hint, along with functions to access all the native handles you could want. But SDL does not seem to have a way to create a window without a context – if you omit a graphics API flag, SDL will add one in for you and create one (and then set it up for its own 2D graphics). So this isn’t the right way.

Something to know about ANGLE is it builds into two shared objects, libGLESv2.so and libEGL.so (or .dylib/.dll, as you will). These implement standard OpenGL ES and Khronos EGL functions. It just so happens that if these libraries are present and loadable (e.g. through dlopen), SDL will use them(*).

So actually, you really don’t need to write any code for ANGLE at all! SDL already does it for you. One catch though (but it’s actually a good thing). SDL will prefer to use your graphics driver’s built-in support for OpenGL ES, if possible. That’s probably a good thing – maybe it implements it better than ANGLE does, for example. SDL will fall back to loading ANGLE if the system’s graphics drivers don’t support ES.

But suppose you want to force ANGLE to be used – what then? SDL’s got you covered there too – there’s a hint, SDL_HINT_OPENGL_ES_DRIVER, you can use. Set it to 1 to force a library to be used instead of the driver. And if the libraries aren’t being found correctly, you can set the SDL_VIDEO_EGL_DRIVER environment variable to the path to libEGL.so and SDL_VIDEO_GL_DRIVER environment variable to the path of libGLESv2.so.

One last thing: SDL also supports a configuration where ANGLE is built in statically. This is controlled by defining the SDL_VIDEO_STATIC_ANGLE preprocessor definition while building SDL. This doesn’t seem to be an officially-supported configuration, but there’s some hints about how it’s supposed to be used in practice in the bug report that caused it to be added. The only user of this appears to be Haxe NME.

Tags: , ,

Comments are closed.