From f2b86a25b37962dd9e44c7265f8a674b547d03ed Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Tue, 14 Jul 2020 19:10:31 +0100 Subject: [PATCH] Cocoa and NSGL Implementation --- src/cocoa_window.m | 19 +++++++++++++++++++ src/nsgl_context.h | 10 ++++++++++ src/nsgl_context.m | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index e34fb876..e7d048a9 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1885,6 +1885,25 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, } +_GLFWusercontext* _glfwPlatformCreateUserContext(_GLFWwindow* window) +{ + if (window->context.nsgl.object) + { + return _glfwCreateUserContextNSGL(window); + } + else if (window->context.egl.handle) + { + return _glfwCreateUserContextEGL(window); + } + else if (window->context.osmesa.handle) + { + return _glfwCreateUserContextOSMesa(window); + } + + return GLFW_FALSE; +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW native API ////// ////////////////////////////////////////////////////////////////////////// diff --git a/src/nsgl_context.h b/src/nsgl_context.h index 9c31436c..598f642b 100644 --- a/src/nsgl_context.h +++ b/src/nsgl_context.h @@ -34,6 +34,7 @@ #define _GLFW_PLATFORM_CONTEXT_STATE _GLFWcontextNSGL nsgl #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE _GLFWlibraryNSGL nsgl +#define _GLFW_PLATFORM_USER_CONTEXT_STATE _GLFWusercontextNSGL nsgl #include @@ -47,6 +48,14 @@ typedef struct _GLFWcontextNSGL } _GLFWcontextNSGL; +// NSGL-specific per user context data +// +typedef struct _GLFWusercontextNSGL +{ + id object; + +} _GLFWusercontextNSGL; + // NSGL-specific global data // typedef struct _GLFWlibraryNSGL @@ -63,4 +72,5 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig); void _glfwDestroyContextNSGL(_GLFWwindow* window); +_GLFWusercontext* _glfwCreateUserContextNSGL(_GLFWwindow* window); diff --git a/src/nsgl_context.m b/src/nsgl_context.m index 3f73f7a6..29430060 100644 --- a/src/nsgl_context.m +++ b/src/nsgl_context.m @@ -349,6 +349,53 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window, } +static void _glfwMakeUserContextCurrentNSGL(_GLFWusercontext* context) +{ + @autoreleasepool { + + if (context) + [context->nsgl.object makeCurrentContext]; + else + [NSOpenGLContext clearCurrentContext]; + + } // autoreleasepool +} + +static void _glfwDestroyUserContextNSGL(_GLFWusercontext* context) +{ + @autoreleasepool { + + if (context->nsgl.object) + [context->nsgl.object release]; + + } // autoreleasepool + free(context); +} + +_GLFWusercontext* _glfwCreateUserContextNSGL(_GLFWwindow* window) +{ + _GLFWusercontext* context; + + context = calloc(1, sizeof(_GLFWusercontext)); + context->window = window; + + context->nsgl.object = + [[NSOpenGLContext alloc] initWithFormat:window->context.nsgl.pixelFormat + shareContext:window->context.nsgl.object]; + if (window->context.nsgl.object == nil) + { + _glfwInputError(GLFW_VERSION_UNAVAILABLE, + "NSGL: Failed to create OpenGL user context"); + free(context); + return NULL; + } + + context->makeCurrent = _glfwMakeUserContextCurrentNSGL; + context->destroy = _glfwDestroyUserContextNSGL; + + return context; +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW native API ////// //////////////////////////////////////////////////////////////////////////