Are multiple EGL contexts with a single SDL window supported in SDL3?
I’ve tried multiple combinations, no success.
Are multiple EGL contexts with a single SDL window supported in SDL3?
I’ve tried multiple combinations, no success.
I resolved this after coming back from vacation. Creating them is the easy part. You just need to manually use the contexts, as there are no SDL API calls available that can be used to set the context.
Snippet:
SDL_GL_ResetAttributes();
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
...
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
...
if (!SDL_CreateWindowAndRenderer(
common.app_id.c_str(), static_cast<int>(width_),
static_cast<int>(height_),
static_cast<SDL_WindowFlags>(window_flags), &window_,
&renderer_)) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "SDL_CreateWindow failed: %s",
SDL_GetError());
}
...
SDL_GLContext gl_context{};
gl_context = SDL_GL_CreateContext(window_);
if (!gl_context) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "SDL_GL_CreateContext failed: %s",
SDL_GetError());
}
SDL_GL_MakeCurrent(window_, nullptr);
io_context = SDL_GL_CreateContext(window_);
if (!io_context) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "SDL_GL_CreateContext failed: %s",
SDL_GetError());
}
SDL_GL_MakeCurrent(window_, nullptr);
Then set shared context later using:
const auto dpy = SDL_EGL_GetCurrentDisplay();
return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
state->io_context);
};
Do you know if doing something like this will allow textures to be reused across windows?
There’s SDL_GL_MakeCurrent()
in both SDL2 and SDL3.
These calls implicitly set the surface value. Only one active context per surface is allowed, or GL/EGL will return an error.
In case of async loading of resources from another thread, you set the shared context with surfaces set to null. So these calls would not apply.
The texture would be available to anything in the current initialized EGL/GLES session. If multiple calls to eglInitialize happen (each window calls eglInitialize) then a common texture would not be shareable. So it depends on how you create your surfaces/windows. You would want a single eglInitialize, then use shared context(s) from either window to access the common texture.