SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL3 feature request: create OpenGL contexts detached from windows

Open vittorioromeo opened this issue 8 months ago • 3 comments

Somewhat related to #12787. In order to share some OpenGL resources across multiple GL contexts, I create a hidden base shared OpenGL context like this:

// Create a hidden window for the context
m_window = SDL_CreateWindow("", 1, 1, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
if (m_window == nullptr) { /* ...error handling... */ }

// Create the OpenGL context
m_context = SDL_GL_CreateContext(m_window);
if (!m_context) { /* ...error handling... */ }

Note that, despite not needing event handling or any other window functionality, I am forced to create a hidden window just to create the the OpenGL context. This also complicates my code as I now have to manage the lifetime of the window as well as the one for the context.

It would be nice if I could pass nullptr as an argument to SDL_GL_CreateContext to create a context detached from any window, e.g.:

m_sharedContext = SDL_GL_CreateContext(nullptr); // detached context

Similarly, it would also be nice if I could pass nullptr as the first argument of SDL_GL_MakeCurrent to set a detached context as active:

SDL_GL_MakeCurrent(nullptr, m_sharedContext); // set detached context as active

How difficult would it be to implement these changes? What is the fundamental reason why OpenGL contexts are architecturally tied to windows in SDL3?

vittorioromeo avatar Apr 09 '25 16:04 vittorioromeo

Is it possible at all to use OpenGL without a window, even outside the scope of SDL? I always thought OpenGL required a window, and that offscreen rendering was traditionally done with hidden windows...

I did a quick research, and I've found sparse references to an extension (ARB_create_context, OpenGL 3+ only) on 15+ years old forum threads, with discussions about how it was then not widely supported. Everything else seems to state that OpenGL requires a window to work at all, but I can't find any modern information on this so my sources could be outdated.

Is anything known to work with OpenGL without any (hidden) window?


The stuff I read, not sure what it's worth in 2025:

  • Khronos source stating OpenGL requires a window: https://www.khronos.org/opengl/wiki/FAQ#What_is_an_OpenGL_context_and_why_do_you_need_a_window_to_do_GL_rendering?
  • A Khronos forum thread discussing the extension: https://community.khronos.org/t/using-opengl-without-creating-window/65020/3
  • A blog (?) giving instructions on how to use the extension, and hacks to do it on Windows and Linux for OpenGL < 3: https://sidvind.com/wiki/Opengl/windowless
  • A StackOverflow answer discussing methods to do offscreen rendering, which partially rely on using hidden windows: https://stackoverflow.com/a/12482274

Semphriss avatar Apr 10 '25 14:04 Semphriss

Is it possible at all to use OpenGL without a window, even outside the scope of SDL? I always thought OpenGL required a window, and that offscreen rendering was traditionally done with hidden windows...

I did a quick research, and I've found sparse references to an extension (ARB_create_context, OpenGL 3+ only) on 15+ years old forum threads, with discussions about how it was then not widely supported. Everything else seems to state that OpenGL requires a window to work at all, but I can't find any modern information on this so my sources could be outdated.

Is anything known to work with OpenGL without any (hidden) window?

The stuff I read, not sure what it's worth in 2025:

  • Khronos source stating OpenGL requires a window: https://www.khronos.org/opengl/wiki/FAQ#What_is_an_OpenGL_context_and_why_do_you_need_a_window_to_do_GL_rendering?
  • A Khronos forum thread discussing the extension: https://community.khronos.org/t/using-opengl-without-creating-window/65020/3
  • A blog (?) giving instructions on how to use the extension, and hacks to do it on Windows and Linux for OpenGL < 3: https://sidvind.com/wiki/Opengl/windowless
  • A StackOverflow answer discussing methods to do offscreen rendering, which partially rely on using hidden windows: https://stackoverflow.com/a/12482274

SFML is able to provide a windowless context for many platforms, e.g.:

  • https://github.com/SFML/SFML/blob/master/src/SFML/Window/Win32/WglContext.cpp#L126-L139
  • https://github.com/vittorioromeo/VRSFML/blob/bubble_idle/src/SFML/GLUtils/EGL/EGLContext.cpp#L248-L274
  • https://github.com/vittorioromeo/VRSFML/blob/bubble_idle/src/SFML/GLUtils/EGL/EGLContext.cpp#L248-L274

There are platform-specific workarounds, but it would be nice to have a somewhat portable API to use them in SDL.

vittorioromeo avatar Apr 10 '25 15:04 vittorioromeo

What is the fundamental reason why OpenGL contexts are architecturally tied to windows in SDL3?

They always were, both in SDL 1.x and in SDL2. I believe this is due to a popular misconception that an OpenGL context is bound to a specific window upon creation. As far as I know, this originally arose in the Windows world, where WGL requires to obtain the pixel format from an existing "window" or "bitmap" to create a context. Traces of this misconception can be found in many older codebases.

cher-nov avatar May 31 '25 09:05 cher-nov