Cannot get MacOS + libANGLE + SDL2 to work

Hi,

I am trying to get SDL2 to work with libANGLE for GLES on MacOS. This is what I have done so far:

  • Built Angle and copied libGLESv2.dylib and libEGL.dylib to /usr/local/lib
  • Also tested the angle build with the tests ./angle_deqp_khr_gles3_tests, most tests pass.
  • Built my own SDL2 from sources with the EGL, GLES2 and GLES3 present on my system at /usr/local/include. Build options was with default configuration, thus on make install put the libs and headers in /usr/local.

Now, the code I use to create a GLES context with SDL, I have used the following:

if (SDL_Init(SDL_InitEverything) < 0)
  {
     std::cerr << "\nFailed on SDL_Init\n";
     return -1;
  }

m_window = SDL_CreateWindow("",
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              m_width.value(),
                              m_height.value(),
                              SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

if (m_window == nullptr)
    {
      std::cerr << "\nFailed on SDL_SetVideoMode\n";
      return -1;
    }

SDL_SetHint("SDL_HINT_OPENGL_ES_DRIVER", "1");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);

m_ctx = SDL_GL_CreateContext(m_window);

if (m_ctx == nullptr)
  {
    std::cerr << SDL_GetError() << "\n";
    return -1;
  }

when building the program, in addition to the flags from pkg-config, I’ve added the link -lEGL -lGLESv2

Program builds with no issue, otool -l confirms that libSDL2.dylib, libEGL.dylib and libGLESv2.dylib are dependencies. However, running is sad:

  • ANGLE_DEFAULT_PLATFORM=metal ./program gives “Could not create EGL context (call to eglCreateContext failed, reporting an error of EGL_BAD_CONFIG)”
  • ANGLE_DEFAULT_PLATFORM=gl ./path_testGLES-debug gives “Could not get EGL display”
  • ./program gives “Could not create EGL context (call to eglCreateContext failed, reporting an error of EGL_BAD_CONFIG)”

so what am I doing wrong?

Hi,

I recently successfully set up ANGLE with SDL2 on macOS. Here’s my minimal working example:

However, I didn’t build SDL2 from source but obtained it through brew instead.

Thomas

Try adding this hint before your SDL_CreateWindow() to ensure you get an OpenGLES context:

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles");

I’m not sure it will help (as you’re not calling SDL_CreateRenderer()).

So, I managed to stumble to a solution which is basically have the

SDL_SetHint("SDL_HINT_OPENGL_ES_DRIVER", "1");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);

before the call to SDL_CreateWindow(). With that, I can got it to work with ANGLE_DEFAULT_PLATFORM=metal, though ANGLE_DEFAULT_PLATFORM=gl gave me a blank screen (and not a single error). The main purpose was to make it work through metal so I am semi-happy. This is what DrNoob’s link does.

I am not fully happy because I still cannot get the application to use the integrated GPU instead (where as for GL I can force it to use with a crafted Info.plist file and unplugging my MacbookPro from the monitor and power).

Last addendum, using brew’s SDL2 also worked fine with me as well once I moved those calls before SDL_CreateWindow().

With OpenGL, macOS decides which GPU to use. With Metal, it’s up to the application which to use. So Angle is probably choosing the discrete GPU.

When you say ANGLE_DEFAULT_PLATFORM=metal, is that an option you set during the compilation of ANGLE or is that something you can #define/set after compilation, e.g. before including “angle_gl.h”?

ANGLE_DEFAULT_PLATFORM is an environmental variable that Angle reads to decide on the backend unless an application uses the EGL extension that Angle provides to let an application specify the backend. I found the gl backend to give a blank screen on the application I am making where as teh metal backend worked fine.