2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
2014-01-22 01:32:00 +01:00
|
|
|
// GLFW 3.1 OS X - www.glfw.org
|
2010-09-07 17:34:51 +02:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
|
|
|
|
//
|
|
|
|
// 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-16 03:25:36 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2012-12-30 00:45:22 +01:00
|
|
|
////// GLFW internal API //////
|
2010-09-16 03:25:36 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-13 16:03:44 +02:00
|
|
|
// Initialize OpenGL support
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2013-01-15 20:49:29 +01:00
|
|
|
int _glfwInitContextAPI(void)
|
2012-08-13 16:03:44 +02:00
|
|
|
{
|
2014-03-30 14:37:20 +02:00
|
|
|
if (!_glfwInitTLS())
|
2012-08-13 16:03:44 +02:00
|
|
|
return GL_FALSE;
|
|
|
|
|
2013-05-02 22:41:09 +02:00
|
|
|
_glfw.nsgl.framework =
|
|
|
|
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
|
|
|
|
if (_glfw.nsgl.framework == NULL)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"NSGL: Failed to locate OpenGL framework");
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-08-13 16:03:44 +02:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate OpenGL support
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2013-01-15 20:49:29 +01:00
|
|
|
void _glfwTerminateContextAPI(void)
|
2012-08-13 16:03:44 +02:00
|
|
|
{
|
2014-03-30 14:37:20 +02:00
|
|
|
_glfwTerminateTLS();
|
2012-08-13 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
2012-12-30 00:58:18 +01:00
|
|
|
// Create the OpenGL context
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2012-12-30 00:58:18 +01:00
|
|
|
int _glfwCreateContext(_GLFWwindow* window,
|
2014-03-06 20:05:32 +01:00
|
|
|
const _GLFWctxconfig* ctxconfig,
|
2012-12-30 00:58:18 +01:00
|
|
|
const _GLFWfbconfig* fbconfig)
|
|
|
|
{
|
|
|
|
unsigned int attributeCount = 0;
|
|
|
|
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->api == GLFW_OPENGL_ES_API)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: This API does not support OpenGL ES");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->major == 3 && ctxconfig->minor < 2)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: The targeted version of OS X does not "
|
2013-07-10 15:03:14 +02:00
|
|
|
"support OpenGL 3.0 or 3.1");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->major > 2)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2014-03-06 20:05:32 +01:00
|
|
|
if (!ctxconfig->forward)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: The targeted version of OS X only "
|
2013-07-10 15:03:14 +02:00
|
|
|
"supports OpenGL 3.2 and later versions if they "
|
|
|
|
"are forward-compatible");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->profile != GLFW_OPENGL_CORE_PROFILE)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: The targeted version of OS X only "
|
2013-07-10 15:03:14 +02:00
|
|
|
"supports OpenGL 3.2 and later versions if they "
|
|
|
|
"use the core profile");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Fail if OpenGL 3.0 or above was requested
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->major > 2)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_VERSION_UNAVAILABLE,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: The targeted version of OS X does not "
|
2012-12-31 21:05:28 +01:00
|
|
|
"support OpenGL version 3.0 or above");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
|
|
|
|
2014-08-20 20:12:59 +02:00
|
|
|
// Context robustness modes (GL_KHR_robustness) are not yet supported on
|
|
|
|
// OS X but are not a hard constraint, so ignore and continue
|
2012-12-30 00:58:18 +01:00
|
|
|
|
2014-08-21 19:21:45 +02:00
|
|
|
// Context release behaviors (GL_KHR_context_flush_control) are not yet
|
|
|
|
// supported on OS X but are not a hard constraint, so ignore and continue
|
|
|
|
|
2012-12-30 00:58:18 +01: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];
|
|
|
|
|
2013-05-02 17:35:09 +02:00
|
|
|
ADD_ATTR(NSOpenGLPFAClosestPolicy);
|
2012-12-30 00:58:18 +01:00
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->major > 2)
|
2014-11-05 00:33:43 +01:00
|
|
|
{
|
2012-12-30 00:58:18 +01:00
|
|
|
ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
|
2014-11-05 00:33:43 +01:00
|
|
|
}
|
|
|
|
else
|
2012-12-30 00:58:18 +01:00
|
|
|
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
2014-11-05 00:33:43 +01: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-30 00:58:18 +01:00
|
|
|
|
2014-04-08 18:57:43 +02: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;
|
|
|
|
|
|
|
|
// OS X needs non-zero color size, so set resonable values
|
|
|
|
if (colorBits == 0)
|
|
|
|
colorBits = 24;
|
|
|
|
else if (colorBits < 15)
|
|
|
|
colorBits = 15;
|
2012-12-30 00:58:18 +01:00
|
|
|
|
2014-04-08 18:57:43 +02:00
|
|
|
ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbconfig->alphaBits != GLFW_DONT_CARE)
|
2012-12-30 00:58:18 +01:00
|
|
|
ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
|
|
|
|
|
2014-04-08 18:57:43 +02:00
|
|
|
if (fbconfig->depthBits != GLFW_DONT_CARE)
|
2012-12-30 00:58:18 +01:00
|
|
|
ADD_ATTR2(NSOpenGLPFADepthSize, fbconfig->depthBits);
|
|
|
|
|
2014-04-08 18:57:43 +02:00
|
|
|
if (fbconfig->stencilBits != GLFW_DONT_CARE)
|
2012-12-30 00:58:18 +01:00
|
|
|
ADD_ATTR2(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
|
|
|
|
|
|
|
|
if (fbconfig->stereo)
|
|
|
|
ADD_ATTR(NSOpenGLPFAStereo);
|
|
|
|
|
2014-04-24 19:21:10 +02:00
|
|
|
if (fbconfig->doublebuffer)
|
|
|
|
ADD_ATTR(NSOpenGLPFADoubleBuffer);
|
|
|
|
|
2014-04-08 18:57:43 +02:00
|
|
|
if (fbconfig->samples != GLFW_DONT_CARE)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2014-04-08 18:57:43 +02:00
|
|
|
if (fbconfig->samples == 0)
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFASampleBuffers, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
|
|
|
|
ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
|
|
|
|
}
|
2012-12-30 00:58:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
|
2013-11-10 14:03:07 +01:00
|
|
|
// frambuffer, so there's no need (and no way) to request it
|
2012-12-30 00:58:18 +01:00
|
|
|
|
|
|
|
ADD_ATTR(0);
|
|
|
|
|
|
|
|
#undef ADD_ATTR
|
|
|
|
#undef ADD_ATTR2
|
|
|
|
|
2013-01-02 01:40:42 +01:00
|
|
|
window->nsgl.pixelFormat =
|
2012-12-30 00:58:18 +01:00
|
|
|
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
|
2013-01-02 01:40:42 +01:00
|
|
|
if (window->nsgl.pixelFormat == nil)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: Failed to create OpenGL pixel format");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSOpenGLContext* share = NULL;
|
|
|
|
|
2014-03-06 20:05:32 +01:00
|
|
|
if (ctxconfig->share)
|
|
|
|
share = ctxconfig->share->nsgl.context;
|
2012-12-30 00:58:18 +01:00
|
|
|
|
2013-01-02 01:40:42 +01:00
|
|
|
window->nsgl.context =
|
|
|
|
[[NSOpenGLContext alloc] initWithFormat:window->nsgl.pixelFormat
|
2012-12-30 00:58:18 +01:00
|
|
|
shareContext:share];
|
2013-01-02 01:40:42 +01:00
|
|
|
if (window->nsgl.context == nil)
|
2012-12-30 00:58:18 +01:00
|
|
|
{
|
2012-12-31 21:05:28 +01:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
2013-08-26 22:48:07 +02:00
|
|
|
"NSGL: Failed to create OpenGL context");
|
2012-12-30 00:58:18 +01:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the OpenGL context
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2012-12-30 00:58:18 +01:00
|
|
|
void _glfwDestroyContext(_GLFWwindow* window)
|
|
|
|
{
|
2013-01-02 01:40:42 +01:00
|
|
|
[window->nsgl.pixelFormat release];
|
|
|
|
window->nsgl.pixelFormat = nil;
|
2012-12-30 00:58:18 +01:00
|
|
|
|
2013-01-02 01:40:42 +01:00
|
|
|
[window->nsgl.context release];
|
|
|
|
window->nsgl.context = nil;
|
2012-12-30 00:58:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 00:45:22 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-07-27 16:01:27 +02:00
|
|
|
void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
|
|
|
|
{
|
|
|
|
if (window)
|
2013-01-02 01:40:42 +01:00
|
|
|
[window->nsgl.context makeCurrentContext];
|
2011-07-27 16:01:27 +02:00
|
|
|
else
|
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2012-08-12 14:13:18 +02:00
|
|
|
|
2014-03-30 14:37:20 +02:00
|
|
|
_glfwSetCurrentContext(window);
|
2011-07-27 16:01:27 +02:00
|
|
|
}
|
|
|
|
|
2012-08-06 18:13:37 +02:00
|
|
|
void _glfwPlatformSwapBuffers(_GLFWwindow* window)
|
2011-03-04 14:25:12 +01:00
|
|
|
{
|
|
|
|
// ARP appears to be unnecessary, but this is future-proof
|
2013-01-02 01:40:42 +01:00
|
|
|
[window->nsgl.context flushBuffer];
|
2011-03-04 14:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _glfwPlatformSwapInterval(int interval)
|
|
|
|
{
|
2012-08-13 16:03:44 +02:00
|
|
|
_GLFWwindow* window = _glfwPlatformGetCurrentContext();
|
2011-03-04 14:25:12 +01:00
|
|
|
|
|
|
|
GLint sync = interval;
|
2013-01-02 01:40:42 +01:00
|
|
|
[window->nsgl.context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
|
2011-03-04 14:25:12 +01:00
|
|
|
}
|
|
|
|
|
2010-09-15 18:57:25 +02:00
|
|
|
int _glfwPlatformExtensionSupported(const char* extension)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2011-03-04 14:29:05 +01:00
|
|
|
// There are no NSGL extensions
|
2010-09-07 17:34:51 +02:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-06-05 00:16:40 +02:00
|
|
|
GLFWglproc _glfwPlatformGetProcAddress(const char* procname)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2010-09-15 18:57:25 +02:00
|
|
|
CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
procname,
|
|
|
|
kCFStringEncodingASCII);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-01-02 01:40:42 +01:00
|
|
|
GLFWglproc symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
|
2012-06-05 00:16:40 +02:00
|
|
|
symbolName);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-15 18:57:25 +02:00
|
|
|
CFRelease(symbolName);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:38:14 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW native API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
|
|
|
|
{
|
|
|
|
_GLFWwindow* window = (_GLFWwindow*) handle;
|
2013-02-20 00:28:08 +01:00
|
|
|
_GLFW_REQUIRE_INIT_OR_RETURN(nil);
|
2013-01-15 22:38:14 +01:00
|
|
|
return window->nsgl.context;
|
|
|
|
}
|
|
|
|
|