Can't share display lists between OpenGL contexts

I’m in the middle of porting a very large legacy code base from MS Windows to Linux.
After looking at the alternatives, I chose SDL as my cross-platform window manager. I’m using the main branch of the SDL git repo.

Our existing code creates display-lists in a separate thread, so I called SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); before calling SDL_CreateWindow() and SDL_GL_CreateContext(). But that context isn’t available in the other threads where I try to create an OpenGL context that’ll share its display-lists (e.g. done with wglShareLists() in MS Windows).

The problem is that wglMakeCurrent() fails if that OpenGL context has already been made current in another thread, but there’s no other way in SDL2 to specify the OpenGL context to share with other than the current one. The non-SDL version of our code calls wglShareLists() with the external OpenGL context without trying to make it current, and it works fine.

Why is it only possible to share with the current context? The docs to wglMakeCurrent() say “A rendering context can be current to only one thread at a time. You cannot make a rendering context current to multiple threads.” so what SDL2 is doing would never work under MS Windows anyway.

Is sharing display-lists like this not implemented in a cross-platform sense? My immediate other target is Linux, but it’d be nice to not do anything that prevents porting to something else in the future.

Display lists? I haven’t touched those in 20 years. That is some legacy code! :slight_smile: Generally speaking, only non-container “objects” can be shared between contexts. That includes things like textures and buffers, but excludes VAOs and FBOs since the latter reference other objects. Display lists can reference textures and such, so they are probably included in the later. More info: OpenGL Object - OpenGL Wiki

My best guess is that they are not shareable without platform extensions like wglShareLists(). If that’s true, I’d share the main context between threads with a mutex. Kinda nullifies the reason to have multiple threads, but it seems like the easiest way forward.

I ended up solving the problem by creating all my OpenGL contexts in the main thread (where the shared context could be made “current”), then calling SDL_GL_MakeCurrent() on the appropriate context in the appropriate thread.

Now the legacy code works as expected.