2010-09-07 11:34:51 -04:00
|
|
|
//========================================================================
|
2016-10-19 18:50:54 -04:00
|
|
|
// GLFW 3.3 macOS - www.glfw.org
|
2010-09-07 11:34:51 -04:00
|
|
|
//------------------------------------------------------------------------
|
2016-06-01 10:17:06 -04:00
|
|
|
// Copyright (c) 2009-2016 Camilla Berglund <elmindreda@glfw.org>
|
2010-09-07 11:34:51 -04:00
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2010-09-15 21:25:36 -04:00
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
static void makeContextCurrentNSGL(_GLFWwindow* window)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
if (window)
|
|
|
|
[window->context.nsgl.object makeCurrentContext];
|
|
|
|
else
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
|
|
|
|
|
|
|
_glfwPlatformSetCurrentContext(window);
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
static void swapBuffersNSGL(_GLFWwindow* window)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
// ARP appears to be unnecessary, but this is future-proof
|
|
|
|
[window->context.nsgl.object flushBuffer];
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
static void swapIntervalNSGL(int interval)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
_GLFWwindow* window = _glfwPlatformGetCurrentContext();
|
|
|
|
|
|
|
|
GLint sync = interval;
|
|
|
|
[window->context.nsgl.object setValues:&sync
|
|
|
|
forParameter:NSOpenGLCPSwapInterval];
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
static int extensionSupportedNSGL(const char* extension)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
// There are no NSGL extensions
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
static GLFWglproc getProcAddressNSGL(const char* procname)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
procname,
|
|
|
|
kCFStringEncodingASCII);
|
|
|
|
|
|
|
|
GLFWglproc symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
|
|
|
|
symbolName);
|
|
|
|
|
|
|
|
CFRelease(symbolName);
|
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the OpenGL context
|
|
|
|
//
|
2016-06-14 06:27:19 -04:00
|
|
|
static void destroyContextNSGL(_GLFWwindow* window)
|
2016-03-28 07:19:31 -04:00
|
|
|
{
|
|
|
|
[window->context.nsgl.pixelFormat release];
|
|
|
|
window->context.nsgl.pixelFormat = nil;
|
|
|
|
|
|
|
|
[window->context.nsgl.object release];
|
|
|
|
window->context.nsgl.object = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-15 21:25:36 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2012-12-29 18:45:22 -05:00
|
|
|
////// GLFW internal API //////
|
2010-09-15 21:25:36 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-08-13 10:03:44 -04:00
|
|
|
// Initialize OpenGL support
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2015-12-10 11:10:05 -05:00
|
|
|
GLFWbool _glfwInitNSGL(void)
|
2012-08-13 10:03:44 -04:00
|
|
|
{
|
2016-07-20 06:52:06 -04:00
|
|
|
if (_glfw.nsgl.framework)
|
|
|
|
return GLFW_TRUE;
|
|
|
|
|
2013-05-02 16:41:09 -04:00
|
|
|
_glfw.nsgl.framework =
|
|
|
|
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
|
|
|
|
if (_glfw.nsgl.framework == NULL)
|
|
|
|
{
|
2015-03-10 14:51:14 -04:00
|
|
|
_glfwInputError(GLFW_API_UNAVAILABLE,
|
2013-05-02 16:41:09 -04:00
|
|
|
"NSGL: Failed to locate OpenGL framework");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2013-05-02 16:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_TRUE;
|
2012-08-13 10:03:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate OpenGL support
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2015-12-03 12:16:46 -05:00
|
|
|
void _glfwTerminateNSGL(void)
|
2012-08-13 10:03:44 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-29 18:58:18 -05:00
|
|
|
// Create the OpenGL context
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2015-12-10 11:10:05 -05:00
|
|
|
GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
|
|
|
|
const _GLFWctxconfig* ctxconfig,
|
|
|
|
const _GLFWfbconfig* fbconfig)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
|
|
|
unsigned int attributeCount = 0;
|
|
|
|
|
2016-03-28 07:19:31 -04:00
|
|
|
if (ctxconfig->client == GLFW_OPENGL_ES_API)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2015-08-09 07:37:27 -04:00
|
|
|
_glfwInputError(GLFW_API_UNAVAILABLE,
|
2016-10-19 18:50:54 -04:00
|
|
|
"NSGL: OpenGL ES is not available on macOS");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
2014-03-06 14:05:32 -05:00
|
|
|
if (ctxconfig->major == 3 && ctxconfig->minor < 2)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2012-12-31 15:05:28 -05:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2016-10-19 18:50:54 -04:00
|
|
|
"NSGL: The targeted version of macOS does not support OpenGL 3.0 or 3.1");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
2014-03-06 14:05:32 -05:00
|
|
|
if (ctxconfig->major > 2)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2014-03-06 14:05:32 -05:00
|
|
|
if (!ctxconfig->forward)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2012-12-31 15:05:28 -05:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2016-10-19 18:50:54 -04:00
|
|
|
"NSGL: The targeted version of macOS only supports forward-compatible contexts for OpenGL 3.2 and above");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
2014-03-06 14:05:32 -05:00
|
|
|
if (ctxconfig->profile != GLFW_OPENGL_CORE_PROFILE)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2012-12-31 15:05:28 -05:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2016-10-19 18:50:54 -04:00
|
|
|
"NSGL: The targeted version of macOS only supports core profile contexts for OpenGL 3.2 and above");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 14:12:59 -04:00
|
|
|
// Context robustness modes (GL_KHR_robustness) are not yet supported on
|
2016-10-19 18:50:54 -04:00
|
|
|
// macOS but are not a hard constraint, so ignore and continue
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2014-08-21 13:21:45 -04:00
|
|
|
// Context release behaviors (GL_KHR_context_flush_control) are not yet
|
2016-10-19 18:50:54 -04:00
|
|
|
// supported on macOS but are not a hard constraint, so ignore and continue
|
2014-08-21 13:21:45 -04:00
|
|
|
|
2012-12-29 18:58:18 -05:00
|
|
|
#define ADD_ATTR(x) { attributes[attributeCount++] = x; }
|
|
|
|
#define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
|
|
|
|
|
|
|
|
// Arbitrary array size here
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[40];
|
|
|
|
|
2014-12-27 16:31:30 -05:00
|
|
|
ADD_ATTR(NSOpenGLPFAAccelerated);
|
2013-05-02 11:35:09 -04:00
|
|
|
ADD_ATTR(NSOpenGLPFAClosestPolicy);
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2014-12-27 16:28:13 -05:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
|
|
|
|
if (ctxconfig->major >= 4)
|
2014-11-04 18:33:43 -05:00
|
|
|
{
|
2014-12-27 16:28:13 -05:00
|
|
|
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core);
|
2014-11-04 18:33:43 -05:00
|
|
|
}
|
|
|
|
else
|
2012-12-29 18:58:18 -05:00
|
|
|
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
2014-12-27 16:28:13 -05:00
|
|
|
if (ctxconfig->major >= 3)
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctxconfig->major <= 2)
|
2014-11-04 18:33:43 -05:00
|
|
|
{
|
|
|
|
if (fbconfig->auxBuffers != GLFW_DONT_CARE)
|
|
|
|
ADD_ATTR2(NSOpenGLPFAAuxBuffers, fbconfig->auxBuffers);
|
|
|
|
|
|
|
|
if (fbconfig->accumRedBits != GLFW_DONT_CARE &&
|
|
|
|
fbconfig->accumGreenBits != GLFW_DONT_CARE &&
|
|
|
|
fbconfig->accumBlueBits != GLFW_DONT_CARE &&
|
|
|
|
fbconfig->accumAlphaBits != GLFW_DONT_CARE)
|
|
|
|
{
|
|
|
|
const int accumBits = fbconfig->accumRedBits +
|
|
|
|
fbconfig->accumGreenBits +
|
|
|
|
fbconfig->accumBlueBits +
|
|
|
|
fbconfig->accumAlphaBits;
|
|
|
|
|
|
|
|
ADD_ATTR2(NSOpenGLPFAAccumSize, accumBits);
|
|
|
|
}
|
|
|
|
}
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2014-04-08 12:57:43 -04:00
|
|
|
if (fbconfig->redBits != GLFW_DONT_CARE &&
|
|
|
|
fbconfig->greenBits != GLFW_DONT_CARE &&
|
|
|
|
fbconfig->blueBits != GLFW_DONT_CARE)
|
|
|
|
{
|
|
|
|
int colorBits = fbconfig->redBits +
|
|
|
|
fbconfig->greenBits +
|
|
|
|
fbconfig->blueBits;
|
|
|
|
|
2016-10-19 18:50:54 -04:00
|
|
|
// macOS needs non-zero color size, so set reasonable values
|
2014-04-08 12:57:43 -04:00
|
|
|
if (colorBits == 0)
|
|
|
|
colorBits = 24;
|
|
|
|
else if (colorBits < 15)
|
|
|
|
colorBits = 15;
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2014-04-08 12:57:43 -04:00
|
|
|
ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbconfig->alphaBits != GLFW_DONT_CARE)
|
2012-12-29 18:58:18 -05:00
|
|
|
ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
|
|
|
|
|
2014-04-08 12:57:43 -04:00
|
|
|
if (fbconfig->depthBits != GLFW_DONT_CARE)
|
2012-12-29 18:58:18 -05:00
|
|
|
ADD_ATTR2(NSOpenGLPFADepthSize, fbconfig->depthBits);
|
|
|
|
|
2014-04-08 12:57:43 -04:00
|
|
|
if (fbconfig->stencilBits != GLFW_DONT_CARE)
|
2012-12-29 18:58:18 -05:00
|
|
|
ADD_ATTR2(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
|
|
|
|
|
|
|
|
if (fbconfig->stereo)
|
2016-10-08 19:39:31 -04:00
|
|
|
{
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
|
|
|
_glfwInputError(GLFW_FORMAT_UNAVAILABLE,
|
|
|
|
"NSGL: Stereo rendering is deprecated");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
#else
|
2012-12-29 18:58:18 -05:00
|
|
|
ADD_ATTR(NSOpenGLPFAStereo);
|
2016-10-08 19:39:31 -04:00
|
|
|
#endif
|
|
|
|
}
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2014-04-24 13:21:10 -04:00
|
|
|
if (fbconfig->doublebuffer)
|
|
|
|
ADD_ATTR(NSOpenGLPFADoubleBuffer);
|
|
|
|
|
2014-04-08 12:57:43 -04:00
|
|
|
if (fbconfig->samples != GLFW_DONT_CARE)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2014-04-08 12:57:43 -04:00
|
|
|
if (fbconfig->samples == 0)
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFASampleBuffers, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
|
|
|
|
ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
|
|
|
|
}
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
|
2015-01-05 15:55:15 -05:00
|
|
|
// framebuffer, so there's no need (and no way) to request it
|
2012-12-29 18:58:18 -05:00
|
|
|
|
|
|
|
ADD_ATTR(0);
|
|
|
|
|
|
|
|
#undef ADD_ATTR
|
|
|
|
#undef ADD_ATTR2
|
|
|
|
|
2015-11-09 10:48:55 -05:00
|
|
|
window->context.nsgl.pixelFormat =
|
2012-12-29 18:58:18 -05:00
|
|
|
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
2015-11-09 10:48:55 -05:00
|
|
|
if (window->context.nsgl.pixelFormat == nil)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2015-09-16 10:24:21 -04:00
|
|
|
_glfwInputError(GLFW_FORMAT_UNAVAILABLE,
|
|
|
|
"NSGL: Failed to find a suitable pixel format");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NSOpenGLContext* share = NULL;
|
|
|
|
|
2014-03-06 14:05:32 -05:00
|
|
|
if (ctxconfig->share)
|
2015-11-09 10:48:55 -05:00
|
|
|
share = ctxconfig->share->context.nsgl.object;
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2015-11-09 10:48:55 -05:00
|
|
|
window->context.nsgl.object =
|
|
|
|
[[NSOpenGLContext alloc] initWithFormat:window->context.nsgl.pixelFormat
|
2012-12-29 18:58:18 -05:00
|
|
|
shareContext:share];
|
2015-11-09 10:48:55 -05:00
|
|
|
if (window->context.nsgl.object == nil)
|
2012-12-29 18:58:18 -05:00
|
|
|
{
|
2015-03-10 14:51:14 -04:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 16:48:07 -04:00
|
|
|
"NSGL: Failed to create OpenGL context");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-12-29 18:58:18 -05:00
|
|
|
}
|
|
|
|
|
2015-11-09 10:48:55 -05:00
|
|
|
[window->context.nsgl.object setView:window->ns.view];
|
2012-12-29 18:58:18 -05:00
|
|
|
|
2016-06-14 06:27:19 -04:00
|
|
|
window->context.makeCurrent = makeContextCurrentNSGL;
|
|
|
|
window->context.swapBuffers = swapBuffersNSGL;
|
|
|
|
window->context.swapInterval = swapIntervalNSGL;
|
|
|
|
window->context.extensionSupported = extensionSupportedNSGL;
|
|
|
|
window->context.getProcAddress = getProcAddressNSGL;
|
|
|
|
window->context.destroy = destroyContextNSGL;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2016-03-28 07:19:31 -04:00
|
|
|
return GLFW_TRUE;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:38:14 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW native API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
|
|
|
|
{
|
|
|
|
_GLFWwindow* window = (_GLFWwindow*) handle;
|
2013-02-19 18:28:08 -05:00
|
|
|
_GLFW_REQUIRE_INIT_OR_RETURN(nil);
|
2015-06-18 08:03:02 -04:00
|
|
|
|
2016-03-28 07:19:31 -04:00
|
|
|
if (window->context.client == GLFW_NO_API)
|
2015-06-18 08:03:02 -04:00
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-09 10:48:55 -05:00
|
|
|
return window->context.nsgl.object;
|
2013-01-15 16:38:14 -05:00
|
|
|
}
|
|
|
|
|