diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f5d6ef6c..4ca8de957 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ option(GLFW_BUILD_TESTS "Build the GLFW test programs" ON) option(GLFW_BUILD_DOCS "Build the GLFW documentation" ON) option(GLFW_INSTALL "Generate installation target" ON) option(GLFW_VULKAN_STATIC "Use the Vulkan loader statically linked into application" OFF) +option(GLFW_OPENGL_SINGLE_GLRC "Build the GLFW to use only a single OpenGL render context" OFF) if (UNIX) option(GLFW_USE_OSMESA "Use OSMesa for offscreen context creation" OFF) @@ -73,6 +74,10 @@ if (GLFW_BUILD_DOCS) find_package(Doxygen) endif() +if (GLFW_OPENGL_SINGLE_GLRC) + add_definitions(-D_GLFW_OPENGL_SINGLE_GLRC) +endif() + #-------------------------------------------------------------------- # Set compiler specific flags #-------------------------------------------------------------------- diff --git a/src/internal.h b/src/internal.h index 92bbfccec..f0777102c 100644 --- a/src/internal.h +++ b/src/internal.h @@ -341,6 +341,11 @@ struct _GLFWcontext int profile; int robustness; int release; +#ifdef _GLFW_OPENGL_SINGLE_GLRC + // Specifies whether this _GLFWcontext has its own GLRC object. + // If not, it must not delete the GLRC object on destruction. + GLFWbool customctx; +#endif PFNGLGETSTRINGIPROC GetStringi; PFNGLGETINTEGERVPROC GetIntegerv; diff --git a/src/wgl_context.c b/src/wgl_context.c index beccb13ed..38748b4f1 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -324,7 +324,14 @@ static void destroyContextWGL(_GLFWwindow* window) { if (window->context.wgl.handle) { +#ifdef _GLFW_OPENGL_SINGLE_GLRC + if (window->context.customctx) + { + wglDeleteContext(window->context.wgl.handle); + } +#else wglDeleteContext(window->context.wgl.handle); +#endif window->context.wgl.handle = NULL; } } @@ -623,9 +630,23 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, setAttrib(0, 0); - window->context.wgl.handle = - _glfw.wgl.CreateContextAttribsARB(window->context.wgl.dc, - share, attribs); +#ifdef _GLFW_OPENGL_SINGLE_GLRC + if (share) + { + // Use shared context instead of creating a new one + window->context.wgl.handle = share; + window->context.customctx = GLFW_FALSE; + } + else + { + // Create new GL render context + window->context.wgl.handle = _glfw.wgl.CreateContextAttribsARB(window->context.wgl.dc, NULL, attribs); + window->context.customctx = GLFW_TRUE; + } +#else + window->context.wgl.handle = _glfw.wgl.CreateContextAttribsARB(window->context.wgl.dc, share, attribs); +#endif + if (!window->context.wgl.handle) { const DWORD error = GetLastError();