Mac OS X: Make OpenGL context selection more robust, avoid software fallback.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 01 Mar 2014 20:46:45 -0500
changeset 82769bd65b58278e
parent 8275 39b104a55d19
child 8277 cd0a00060738
Mac OS X: Make OpenGL context selection more robust, avoid software fallback.

Fixes Bugzilla #2197.
src/video/cocoa/SDL_cocoaopengl.m
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Sat Mar 01 20:28:40 2014 -0500
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Sat Mar 01 20:46:45 2014 -0500
     1.3 @@ -35,17 +35,14 @@
     1.4  
     1.5  #define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
     1.6  
     1.7 -#ifndef kCGLPFAOpenGLProfile
     1.8 -#define kCGLPFAOpenGLProfile 99
     1.9 +#ifndef NSOpenGLPFAOpenGLProfile
    1.10 +#define NSOpenGLPFAOpenGLProfile 99
    1.11  #endif
    1.12 -#ifndef kCGLOGLPVersion_Legacy
    1.13 -#define kCGLOGLPVersion_Legacy 0x1000
    1.14 +#ifndef NSOpenGLProfileVersionLegacy
    1.15 +#define NSOpenGLProfileVersionLegacy 0x1000
    1.16  #endif
    1.17 -#ifndef kCGLOGLPVersion_GL3_Core
    1.18 -#define kCGLOGLPVersion_GL3_Core 0x3200
    1.19 -#endif
    1.20 -#ifndef kCGLOGLPVersion_GL4_Core
    1.21 -#define kCGLOGLPVersion_GL4_Core 0x4100
    1.22 +#ifndef NSOpenGLProfileVersion3_2Core
    1.23 +#define NSOpenGLProfileVersion3_2Core 0x3200
    1.24  #endif
    1.25  
    1.26  @implementation SDLOpenGLContext : NSOpenGLContext
    1.27 @@ -164,8 +161,6 @@
    1.28  SDL_GLContext
    1.29  Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
    1.30  {
    1.31 -    const int wantver = (_this->gl_config.major_version << 8) |
    1.32 -                        (_this->gl_config.minor_version);
    1.33      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    1.34      NSAutoreleasePool *pool;
    1.35      SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    1.36 @@ -175,16 +170,16 @@
    1.37      SDLOpenGLContext *context;
    1.38      NSOpenGLContext *share_context = nil;
    1.39      int i = 0;
    1.40 +    const char *glversion;
    1.41 +    int glversion_major;
    1.42 +    int glversion_minor;
    1.43  
    1.44      if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
    1.45          SDL_SetError ("OpenGL ES is not supported on this platform");
    1.46          return NULL;
    1.47      }
    1.48 -
    1.49 -    /* Sadly, we'll have to update this as life progresses, since we need to
    1.50 -       set an enum for context profiles, not a context version number */
    1.51 -    if (wantver > 0x0401) {
    1.52 -        SDL_SetError ("OpenGL > 4.1 is not supported on this platform");
    1.53 +    if ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) && (data->osversion < 0x1070)) {
    1.54 +        SDL_SetError ("OpenGL Core Profile is not supported on this platform version");
    1.55          return NULL;
    1.56      }
    1.57  
    1.58 @@ -192,19 +187,11 @@
    1.59  
    1.60      /* specify a profile if we're on Lion (10.7) or later. */
    1.61      if (data->osversion >= 0x1070) {
    1.62 -        NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
    1.63 +        NSOpenGLPixelFormatAttribute profile = NSOpenGLProfileVersionLegacy;
    1.64          if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
    1.65 -            if (wantver == 0x0302) {
    1.66 -                profile = kCGLOGLPVersion_GL3_Core;
    1.67 -            } else if ((wantver == 0x0401) && (data->osversion >= 0x1090)) {
    1.68 -                profile = kCGLOGLPVersion_GL4_Core;
    1.69 -            } else {
    1.70 -                SDL_SetError("Requested GL version is not supported on this platform");
    1.71 -                [pool release];
    1.72 -                return NULL;
    1.73 -            }
    1.74 +            profile = NSOpenGLProfileVersion3_2Core;
    1.75          }
    1.76 -        attr[i++] = kCGLPFAOpenGLProfile;
    1.77 +        attr[i++] = NSOpenGLPFAOpenGLProfile;
    1.78          attr[i++] = profile;
    1.79      }
    1.80  
    1.81 @@ -284,9 +271,33 @@
    1.82  
    1.83      if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
    1.84          Cocoa_GL_DeleteContext(_this, context);
    1.85 +        SDL_SetError ("Failed making OpenGL context current");
    1.86          return NULL;
    1.87      }
    1.88  
    1.89 +    glversion = (const char *)glGetString(GL_VERSION);
    1.90 +    if (glversion == NULL) {
    1.91 +        Cocoa_GL_DeleteContext(_this, context);
    1.92 +        SDL_SetError ("Failed getting OpenGL context version");
    1.93 +        return NULL;
    1.94 +    }
    1.95 +
    1.96 +    if (SDL_sscanf(glversion, "%d.%d", &glversion_major, &glversion_minor) != 2) {
    1.97 +        Cocoa_GL_DeleteContext(_this, context);
    1.98 +        SDL_SetError ("Failed parsing OpenGL context version");
    1.99 +        return NULL;
   1.100 +    }
   1.101 +
   1.102 +    if ((glversion_major <  _this->gl_config.major_version) ||
   1.103 +       ((glversion_major == _this->gl_config.major_version) && (glversion_minor < _this->gl_config.minor_version))) {
   1.104 +        Cocoa_GL_DeleteContext(_this, context);
   1.105 +        SDL_SetError ("Failed creating OpenGL context at version requested");
   1.106 +        return NULL;
   1.107 +    }
   1.108 +
   1.109 +    _this->gl_config.major_version = glversion_major;
   1.110 +    _this->gl_config.minor_version = glversion_minor;
   1.111 +
   1.112      return context;
   1.113  }
   1.114