diff --git a/src/glx_context.c b/src/glx_context.c index e58584ee..19ccf7f9 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -726,7 +726,6 @@ _GLFWusercontext* _glfwCreateUserContextGLX(_GLFWwindow* window) context->makeCurrent = _glfwMakeUserContextCurrentGLX; context->destroy = _glfwDestroyUserContextGLX; - return context; } diff --git a/src/null_platform.h b/src/null_platform.h index 49436dcc..d476f2fa 100644 --- a/src/null_platform.h +++ b/src/null_platform.h @@ -34,6 +34,7 @@ #define _GLFW_PLATFORM_CONTEXT_STATE struct { int dummyContext; } #define _GLFW_PLATFORM_CURSOR_STATE struct { int dummyCursor; } #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE struct { int dummyLibraryContext; } +#define _GLFW_PLATFORM_USER_CONTEXT_STATE struct { int dummyUserContext; } #include "posix_time.h" #include "posix_thread.h" diff --git a/src/null_window.c b/src/null_window.c index ba85571b..1f760e84 100644 --- a/src/null_window.c +++ b/src/null_window.c @@ -649,3 +649,12 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, return VK_ERROR_EXTENSION_NOT_PRESENT; } +_GLFWusercontext* _glfwPlatformCreateUserContext(_GLFWwindow* window) +{ + if (window->context.osmesa.handle) + { + return _glfwCreateUserContextOSMesa(window); + } + + return GLFW_FALSE; +} diff --git a/src/osmesa_context.c b/src/osmesa_context.c index 07baabed..69ae4e2e 100644 --- a/src/osmesa_context.c +++ b/src/osmesa_context.c @@ -195,9 +195,9 @@ void _glfwTerminateOSMesa(void) attribs[index++] = v; \ } -GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, - const _GLFWctxconfig* ctxconfig, - const _GLFWfbconfig* fbconfig) +GLFWbool _glfwCreateContextForConfigOSMesa(const _GLFWctxconfig* ctxconfig, + const _GLFWfbconfig* fbconfig, + OSMesaContext* context ) { OSMesaContext share = NULL; const int accumBits = fbconfig->accumRedBits + @@ -248,7 +248,7 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, setAttrib(0, 0); - window->context.osmesa.handle = + *context = OSMesaCreateContextAttribs(attribs, share); } else @@ -260,7 +260,7 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, return GLFW_FALSE; } - window->context.osmesa.handle = + *context = OSMesaCreateContextExt(OSMESA_RGBA, fbconfig->depthBits, fbconfig->stencilBits, @@ -268,13 +268,27 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, share); } - if (window->context.osmesa.handle == NULL) + if (*context == NULL) { _glfwInputError(GLFW_VERSION_UNAVAILABLE, "OSMesa: Failed to create context"); return GLFW_FALSE; } + return GLFW_TRUE; +} + +#undef setAttrib + +GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, + const _GLFWctxconfig* ctxconfig, + const _GLFWfbconfig* fbconfig) +{ + if(!_glfwCreateContextForConfigOSMesa(ctxconfig,fbconfig,&window->context.osmesa.handle)) + { + return GLFW_FALSE; + } + window->context.makeCurrent = makeContextCurrentOSMesa; window->context.swapBuffers = swapBuffersOSMesa; window->context.swapInterval = swapIntervalOSMesa; @@ -285,12 +299,57 @@ GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, return GLFW_TRUE; } -#undef setAttrib +static void _glfwMakeUserContextCurrentOSMesa(_GLFWusercontext* context) +{ + if(context) + { + if (!OSMesaMakeCurrent(context->osmesa.handle, + context->window->context.osmesa.buffer, + GL_UNSIGNED_BYTE, + context->window->context.osmesa.width, context->window->context.osmesa.height)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "OSMesa: Failed to make context current"); + return; + } + } +} + +static void _glfwDestroyUserContextOSMesa(_GLFWusercontext* context) +{ + if (context->osmesa.handle) + { + OSMesaDestroyContext(context->osmesa.handle); + } + free(context); +} _GLFWusercontext* _glfwCreateUserContextOSMesa(_GLFWwindow* window) { - // TODO - return NULL; + _GLFWusercontext* context; + _GLFWctxconfig ctxconfig; + _GLFWfbconfig fbconfig; + + context = calloc(1, sizeof(_GLFWusercontext)); + context->window = window; + + ctxconfig = _glfw.hints.context; + ctxconfig.share = window; + + fbconfig = _glfw.hints.framebuffer; + + if(!_glfwCreateContextForConfigOSMesa(&ctxconfig,&fbconfig,&context->osmesa.handle)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "OSMesa: Failed to create user OpenGL context"); + free(context); + return NULL; + } + + context->makeCurrent = _glfwMakeUserContextCurrentOSMesa; + context->destroy = _glfwDestroyUserContextOSMesa; + + return context; } //////////////////////////////////////////////////////////////////////////