Cleanup: remove contributors for CMake files
[blender.git] / intern / ghost / intern / GHOST_ContextCGL.mm
index ab9181e9b9e6d7d55836433870d84aac3071d400..dc0a5089d7476716b67829596ec98eaef8306b4f 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <Cocoa/Cocoa.h>
 
+//#define GHOST_MULTITHREADED_OPENGL
+
 #ifdef GHOST_MULTITHREADED_OPENGL
 #include <OpenGL/OpenGL.h>
 #endif
@@ -54,7 +56,22 @@ GHOST_ContextCGL::GHOST_ContextCGL(
       m_openGLContext(nil),
       m_debug(contextFlags)
 {
-       assert(openGLView != nil);
+       // for now be very strict about OpenGL version requested
+       switch (contextMajorVersion) {
+               case 2:
+                       assert(contextMinorVersion == 1);
+                       assert(contextProfileMask == 0);
+                       m_coreProfile = false;
+                       break;
+               case 3:
+                       // Apple didn't implement 3.0 or 3.1
+                       assert(contextMinorVersion == 3);
+                       assert(contextProfileMask == GL_CONTEXT_CORE_PROFILE_BIT);
+                       m_coreProfile = true;
+                       break;
+               default:
+                       assert(false);
+       }
 }
 
 
@@ -63,7 +80,10 @@ GHOST_ContextCGL::~GHOST_ContextCGL()
        if (m_openGLContext != nil) {
                if (m_openGLContext == [NSOpenGLContext currentContext]) {
                        [NSOpenGLContext clearCurrentContext];
-                       [m_openGLView clearGLContext];
+
+                       if(m_openGLView) {
+                               [m_openGLView clearGLContext];
+                       }
                }
 
                if (m_openGLContext != s_sharedOpenGLContext || s_sharedCount == 1) {
@@ -134,9 +154,6 @@ GHOST_TSuccess GHOST_ContextCGL::activateDrawingContext()
        if (m_openGLContext != nil) {
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                [m_openGLContext makeCurrentContext];
-
-               activateGLEW();
-
                [pool drain];
                return GHOST_kSuccess;
        }
@@ -145,6 +162,18 @@ GHOST_TSuccess GHOST_ContextCGL::activateDrawingContext()
        }
 }
 
+GHOST_TSuccess GHOST_ContextCGL::releaseDrawingContext()
+{
+       if (m_openGLContext != nil) {
+               NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+               [NSOpenGLContext clearCurrentContext];
+               [pool drain];
+               return GHOST_kSuccess;
+       }
+       else {
+               return GHOST_kFailure;
+       }
+}
 
 GHOST_TSuccess GHOST_ContextCGL::updateDrawingContext()
 {
@@ -162,6 +191,7 @@ GHOST_TSuccess GHOST_ContextCGL::updateDrawingContext()
 
 static void makeAttribList(
         std::vector<NSOpenGLPixelFormatAttribute>& attribs,
+        bool coreProfile, 
         bool stereoVisual,
         int numOfAASamples,
         bool needAlpha,
@@ -170,6 +200,9 @@ static void makeAttribList(
 {
        attribs.clear();
 
+       attribs.push_back(NSOpenGLPFAOpenGLProfile);
+       attribs.push_back(coreProfile ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy);
+       
        // Pixel Format Attributes for the windowed NSOpenGLContext
        attribs.push_back(NSOpenGLPFADoubleBuffer);
 
@@ -182,15 +215,11 @@ static void makeAttribList(
                attribs.push_back(NSOpenGLPFANoRecovery);
        }
 
-       /* Removed to allow 10.4 builds, and 2 GPUs rendering is not used anyway */
-       //attribs.push_back(NSOpenGLPFAAllowOfflineRenderers);
+       attribs.push_back(NSOpenGLPFAAllowOfflineRenderers); // for automatic GPU switching
 
        attribs.push_back(NSOpenGLPFADepthSize);
        attribs.push_back((NSOpenGLPixelFormatAttribute) 32);
 
-       attribs.push_back(NSOpenGLPFAAccumSize);
-       attribs.push_back((NSOpenGLPixelFormatAttribute) 32);
-
        if (stereoVisual)
                attribs.push_back(NSOpenGLPFAStereo);
 
@@ -236,7 +265,7 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
        std::vector<NSOpenGLPixelFormatAttribute> attribs;
        attribs.reserve(40);
 
-       NSOpenGLContext *prev_openGLContext = [m_openGLView openGLContext];
+       NSOpenGLContext *prev_openGLContext = (m_openGLView) ? [m_openGLView openGLContext] : NULL;
 
 #ifdef GHOST_OPENGL_ALPHA
        static const bool needAlpha   = true;
@@ -255,7 +284,7 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
        NSOpenGLPixelFormat *pixelFormat;
        // TODO: keep pixel format for subsequent windows/contexts instead of recreating each time
 
-       makeAttribList(attribs, m_stereoVisual, m_numOfAASamples, needAlpha, needStencil, softwareGL);
+       makeAttribList(attribs, m_coreProfile, m_stereoVisual, m_numOfAASamples, needAlpha, needStencil, softwareGL);
 
        pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attribs[0]];
 
@@ -266,7 +295,7 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
                // (Now that I think about it, does WGL really require the code that it has for finding a lesser match?)
 
                attribs.clear();
-               makeAttribList(attribs, m_stereoVisual, 0, needAlpha, needStencil, softwareGL);
+               makeAttribList(attribs, m_coreProfile, m_stereoVisual, 0, needAlpha, needStencil, softwareGL);
                pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attribs[0]];
        }
 
@@ -308,7 +337,7 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
                [m_openGLContext release];
 
                // create software GL context
-               makeAttribList(attribs, m_stereoVisual, m_numOfAASamples, needAlpha, needStencil, softwareGL);
+               makeAttribList(attribs, m_coreProfile, m_stereoVisual, m_numOfAASamples, needAlpha, needStencil, softwareGL);
                pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attribs[0]];
                m_openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:s_sharedOpenGLContext];
                [pixelFormat release];
@@ -324,8 +353,7 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
 
 #ifdef GHOST_MULTITHREADED_OPENGL
        //Switch openGL to multhreaded mode
-       CGLContextObj cglCtx = (CGLContextObj)[tmpOpenGLContext CGLContextObj];
-       if (CGLEnable(cglCtx, kCGLCEMPEngine) == kCGLNoError)
+       if (CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine) == kCGLNoError)
                if (m_debug)
                        fprintf(stderr, "\nSwitched OpenGL to multithreaded mode\n");
 #endif
@@ -340,8 +368,10 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
 
        initContextGLEW();
 
-       [m_openGLView setOpenGLContext:m_openGLContext];
-       [m_openGLContext setView:m_openGLView];
+       if (m_openGLView) {
+               [m_openGLView setOpenGLContext:m_openGLContext];
+               [m_openGLContext setView:m_openGLView];
+       }
 
        if (s_sharedCount == 0)
                s_sharedOpenGLContext = m_openGLContext;
@@ -358,7 +388,10 @@ GHOST_TSuccess GHOST_ContextCGL::initializeDrawingContext()
 
 error:
 
-       [m_openGLView setOpenGLContext:prev_openGLContext];
+       if (m_openGLView) {
+               [m_openGLView setOpenGLContext:prev_openGLContext];
+       }
+
        [pixelFormat release];
 
        [pool drain];