From be8e2cf3a4a23a8da9731717289889db2d7f1661 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 20 Sep 2021 20:39:17 +0100 Subject: [PATCH] Move OpenGL context creation to the main thread. --- OpenRA.Platforms.Default/Sdl2GraphicsContext.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs index 81152c302f..b6fdc82fa8 100644 --- a/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs +++ b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs @@ -25,15 +25,20 @@ namespace OpenRA.Platforms.Default public Sdl2GraphicsContext(Sdl2PlatformWindow window) { this.window = window; + + // SDL requires us to create the GL context on the main thread to avoid various platform-specific issues. + // We must then release it from the main thread before we rebind it to the render thread (in InitializeOpenGL below). + context = SDL.SDL_GL_CreateContext(window.Window); + if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window.Window, IntPtr.Zero) < 0) + throw new InvalidOperationException($"Can not create OpenGL context. (Error: {SDL.SDL_GetError()})"); } internal void InitializeOpenGL() { SetThreadAffinity(); - context = SDL.SDL_GL_CreateContext(window.Window); - if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window.Window, context) < 0) - throw new InvalidOperationException($"Can not create OpenGL context. (Error: {SDL.SDL_GetError()})"); + if (SDL.SDL_GL_MakeCurrent(window.Window, context) < 0) + throw new InvalidOperationException($"Can not bind OpenGL context. (Error: {SDL.SDL_GetError()})"); OpenGL.Initialize(window.GLProfile == GLProfile.Legacy); OpenGL.CheckGLError();