Multi-threaded SDL applications and SDL_GL_MakeCurrent


I am porting some code that uses GLFW to SDL2, as SDL is a more
comprehensive library than GLFW is.

One issue I’ve been having is trying to find an equivalent to
glfwMakeContextCurrent. glfwMakeContextCurrent allows one to unbind its GL
context by passing it nullptr before rebinding it on another thread.

Looking at the docs, SDL_GL_MakeCurrent seems to provide similar behaviour,
like so:

SDL_GL_MakeCurrent(window, context); // Acquire the context
// Do some stuff...
SDL_GL_MakeCurrent(window, nullptr); // Release the context

My questions are:

  1. Is this correct?
  2. If so, could this be documented, especially regarding a nullptr argument?



The way you’re doing it seems to be the only way that actually works. Passing nullptr does indeed unbind the context and I believe this behaviour is intended, even though SDL_GLContext doesn’t look like the void pointer it actually is. And you basically have to unbind the context prior to binding it to another thread, failing to do so will either crash the driver (Nvidia) or make GL dispatching unnecessarily slow (Mesa).

However, since I’m doing something similar in my code, I’d like to know if it really is safe to call SDL_GL_SwapWindow from the rendering thread if that thread is not the main thread.
The wiki says it is, but the wiki also says that calling any function related to window management may not work when called from any thread other than the main one, depending on the platform.