MacOS SDL2 / OpenGL context creation and Release build issues

Hi All,

Moving to MacOS Big Sur, I have encountered a number of issues with my SDL2-powered gamedev library. The same issues don’t occur on other platforms (Windows, Linux).

The current version is shipping the factory SDL2 (v2.0.7); at the moment I’m trying to transition to relying on package managers (vcpkg / brew / apt).

1, As of Big Sur and SDL2 v2.0.14 (which is the only version brew will serve), SDL_GL_CreateContext() no longer gives me a context.

  • I’m creating the window with SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN.
  • It doesn’t seem to be the matter of requesting a particular GL profile (was asking for 3.3 originally);
  • The same thing happens if I use the SDL2.framework package of the same version;
  • This works with earlier versions (<=v2.0.12 .framework) and on other platforms, however I didn’t try v2.0.14 pre-Big Sur (so that might be a red herring).

2, More alarmingly, I cannot seem to build release (‘Profile’ in XCode) anymore: any #include of “SDL.h” → “SDL_cpuinfo.h” → “immintrin.h” → “mmintrin.h” → “math.h” brings about compile errors concerning the use of undeclared identifiers __builtin_ia32_emms, __builtin_ia32_vec_init_v2si, __builtin_ia32_packsswb etc. (presumably Clang / LLVM builtins?)

  • I’ve found the same with v2.0.12 and v2.014 of SDL2, on BS. I’ll work backward, though I’m confident that this is an issue with the Apple compiler.

Any pointers / ideas would be appreciated. I’ll update the thread with my findings. Thanks in advance.

What I do is to build my MacOS apps on High Sierra (I keep a Mac with that OS specifically for the purpose). Apps built that way seem to run perfectly in Big Sur.

High Sierra is hard to find in CI providers and maybe OP is building for M1.

Thanks for the replies. I guess I’ll need a CI provider or virtual Mac; I can’t downgrade the one I’m working on.

I’ve had a bit of a breakthrough with issue #1. After I’ve created a minimal example which reproduces both issues, I’ve realised that the SDL_Renderer that I’ve been creating serves absolutely no purpose. Once I’ve removed it, I got the GL context I was asking for. (I have no idea why was it working all these years and other platforms.)

Now to get to the bottom of the gnarly looking compile(…r) errors…

My (x86) app runs perfectly well (and surprisingly quickly) on the M1, under Rosetta 2 emulation, so there is little motivation to want to build and maintain a native version. Anyway, SDL2_ttf and SDL2_net frameworks are still - when I last checked - not available precompiled for the M1. :frowning_face:

And the example:

// -I/usr/local/include
// -L/usr/local/lib
// -lSDL2
#include "SDL2/SDL.h"

int main(int argc, const char * argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        abort();
    }
    
    auto window = SDL_CreateWindow("HELLO", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    //auto renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);

    int width, height;
    SDL_GL_GetDrawableSize(window, &width, &height);

    //SDL_RenderSetLogicalSize(renderer, width, height);

    auto context = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, context);

    SDL_Event e;
    bool running = true;
    while (running)
    {
        while (SDL_PollEvent(&e) != 0)
        {
            if (e.type == SDL_QUIT)
            {
                running = false;
                break;
            }
        }
        SDL_GL_SwapWindow(window);
    }
    
    SDL_GL_DeleteContext(context);
    //SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    
    SDL_Quit();
    return 0;
}

Pardon my ignorance, what’s a M1? I’m only using core SDL2 so the lack of availability for the other modules is fine.

The new Apple CPU (AArch64 architecture) used in some of the latest Macs. So not binary compatible with x86 Macs, but most apps can be run under the Rosetta emulation layer.

Ah, right, this must be “Apple Silicon”.

1 Like

Going back to the issues with context creation for a bit…

I’ve noticed that trying to create the context on a separate thread no longer works:

  • first there is a Main Thread Checker assert caused by calling SDL_GL_GetDrawableSize() on my intended rendering thread. I could live without / work around, it. (I remember seeing similar warning previously, however things kept working (as they do, again, on other platforms), so I didn’t care much.)
  • the application then hangs upon SDL_GL_CreateContext();

Not sure how to resolve this for now, apart from declaring multi-threaded rendering unsupported on Mac.

Just to be clear, are all your accesses to the GL context being made from the same thread (but separate from the ‘main’ thread), or are you attempting to access the GL context from multiple threads? My understanding is that the former may work (on some platforms at least) but that the latter never works - reliably - in SDL2.

Yes, though there isn’t really even a context to speak of at that point.

The way the library works is, it will call SDL_CreateWindow() on the main thread, then SDL_GL_CreateContext() on either same thread (single threaded mode), or a worker thread that the rendering subsystem will start (in multi-threaded mode). EDIT: and the latter will hang on MacOS Big Sur, but was fine before.

I will have not called any openGL functions at this point.