src/video/cocoa/SDL_cocoaopengl.m
author Ryan C. Gordon <icculus@icculus.org>
Wed, 10 Oct 2012 23:10:04 -0400
changeset 6567 d50613615139
parent 6382 64d54101773a
child 6832 156e608ec4ef
permissions -rw-r--r--
Allow specifying of OpenGL 3.2 Core Profile on Mac OS X.
slouken@1936
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1936
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1936
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1936
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1936
    20
*/
slouken@1936
    21
#include "SDL_config.h"
slouken@1936
    22
slouken@1936
    23
/* NSOpenGL implementation of SDL OpenGL support */
slouken@1936
    24
slouken@1952
    25
#if SDL_VIDEO_OPENGL_CGL
slouken@6044
    26
#include "SDL_cocoavideo.h"
slouken@6044
    27
slouken@1936
    28
#include <OpenGL/CGLTypes.h>
slouken@2738
    29
#include <OpenGL/OpenGL.h>
slouken@3570
    30
#include <OpenGL/CGLRenderers.h>
slouken@1936
    31
slouken@1936
    32
#include "SDL_loadso.h"
slouken@1936
    33
#include "SDL_opengl.h"
slouken@1936
    34
icculus@6567
    35
#define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
slouken@1936
    36
icculus@6567
    37
icculus@6567
    38
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
icculus@6567
    39
#define kCGLPFAOpenGLProfile 99
icculus@6567
    40
#define kCGLOGLPVersion_Legacy 0x1000
icculus@6567
    41
#define kCGLOGLPVersion_3_2_Core 0x3200
icculus@6567
    42
#endif
icculus@6567
    43
slouken@1936
    44
slouken@1936
    45
int
slouken@1936
    46
Cocoa_GL_LoadLibrary(_THIS, const char *path)
slouken@1936
    47
{
slouken@3057
    48
    /* Load the OpenGL library */
slouken@1936
    49
    if (path == NULL) {
slouken@1952
    50
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    51
    }
slouken@1952
    52
    if (path == NULL) {
slouken@1952
    53
        path = DEFAULT_OPENGL;
slouken@1936
    54
    }
slouken@1936
    55
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@1936
    56
    if (!_this->gl_config.dll_handle) {
slouken@1936
    57
        return -1;
slouken@1936
    58
    }
slouken@1936
    59
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1936
    60
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1936
    61
    return 0;
slouken@1936
    62
}
slouken@1936
    63
slouken@1936
    64
void *
slouken@1936
    65
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
slouken@1936
    66
{
slouken@1936
    67
    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1936
    68
}
slouken@1936
    69
slouken@3057
    70
void
slouken@1936
    71
Cocoa_GL_UnloadLibrary(_THIS)
slouken@1936
    72
{
slouken@3057
    73
    SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
    74
    _this->gl_config.dll_handle = NULL;
slouken@1936
    75
}
slouken@1936
    76
slouken@1936
    77
SDL_GLContext
slouken@1936
    78
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1936
    79
{
icculus@6567
    80
    const int wantver = (_this->gl_config.major_version << 8) |
icculus@6567
    81
                        (_this->gl_config.minor_version);
icculus@6567
    82
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1936
    83
    NSAutoreleasePool *pool;
slouken@5246
    84
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1936
    85
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
    86
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
    87
    NSOpenGLPixelFormat *fmt;
slouken@2178
    88
    NSOpenGLContext *context;
slouken@1936
    89
    int i = 0;
slouken@1936
    90
icculus@6567
    91
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
icculus@6567
    92
        SDL_SetError ("OpenGL ES not supported on this platform");
icculus@6567
    93
        return NULL;
icculus@6567
    94
    }
icculus@6567
    95
icculus@6567
    96
    /* Sadly, we'll have to update this as life progresses, since we need to
icculus@6567
    97
       set an enum for context profiles, not a context version number */
icculus@6567
    98
    if (wantver > 0x0302) {
icculus@6567
    99
        SDL_SetError ("OpenGL > 3.2 is not supported on this platform");
icculus@6567
   100
        return NULL;
icculus@6567
   101
    }
icculus@6567
   102
slouken@1936
   103
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   104
icculus@6567
   105
    /* specify a profile if we're on Lion (10.7) or later. */
icculus@6567
   106
    if (data->osversion >= 0x1070) {
icculus@6567
   107
        NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
icculus@6567
   108
        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
icculus@6567
   109
            if (wantver == 0x0302) {
icculus@6567
   110
                profile = kCGLOGLPVersion_3_2_Core;
icculus@6567
   111
            }
icculus@6567
   112
        }
icculus@6567
   113
        attr[i++] = kCGLPFAOpenGLProfile;
icculus@6567
   114
        attr[i++] = profile;
icculus@6567
   115
    }
icculus@6567
   116
slouken@5249
   117
#ifndef FULLSCREEN_TOGGLEABLE
slouken@1936
   118
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1936
   119
        attr[i++] = NSOpenGLPFAFullScreen;
slouken@1936
   120
    }
slouken@5249
   121
#endif
slouken@1936
   122
slouken@1936
   123
    attr[i++] = NSOpenGLPFAColorSize;
slouken@1936
   124
    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
slouken@1936
   125
slouken@1936
   126
    attr[i++] = NSOpenGLPFADepthSize;
slouken@1936
   127
    attr[i++] = _this->gl_config.depth_size;
slouken@1936
   128
slouken@1936
   129
    if (_this->gl_config.double_buffer) {
slouken@1936
   130
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@1936
   131
    }
slouken@1936
   132
slouken@1936
   133
    if (_this->gl_config.stereo) {
slouken@1936
   134
        attr[i++] = NSOpenGLPFAStereo;
slouken@1936
   135
    }
slouken@1936
   136
slouken@1936
   137
    if (_this->gl_config.stencil_size) {
slouken@1936
   138
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@1936
   139
        attr[i++] = _this->gl_config.stencil_size;
slouken@1936
   140
    }
slouken@1936
   141
slouken@1936
   142
    if ((_this->gl_config.accum_red_size +
slouken@1936
   143
         _this->gl_config.accum_green_size +
slouken@1936
   144
         _this->gl_config.accum_blue_size +
slouken@1936
   145
         _this->gl_config.accum_alpha_size) > 0) {
slouken@1936
   146
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@1936
   147
        attr[i++] = _this->gl_config.accum_red_size + _this->gl_config.accum_green_size + _this->gl_config.accum_blue_size + _this->gl_config.accum_alpha_size;
slouken@1936
   148
    }
slouken@1936
   149
slouken@1936
   150
    if (_this->gl_config.multisamplebuffers) {
slouken@1936
   151
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@1936
   152
        attr[i++] = _this->gl_config.multisamplebuffers;
slouken@1936
   153
    }
slouken@1936
   154
slouken@1936
   155
    if (_this->gl_config.multisamplesamples) {
slouken@1936
   156
        attr[i++] = NSOpenGLPFASamples;
slouken@1936
   157
        attr[i++] = _this->gl_config.multisamplesamples;
slouken@1936
   158
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@1936
   159
    }
slouken@1936
   160
slouken@3571
   161
    if (_this->gl_config.accelerated >= 0) {
slouken@3571
   162
        if (_this->gl_config.accelerated) {
slouken@3571
   163
            attr[i++] = NSOpenGLPFAAccelerated;
slouken@3571
   164
        } else {
slouken@3571
   165
            attr[i++] = NSOpenGLPFARendererID;
slouken@3571
   166
            attr[i++] = kCGLRendererGenericFloatID;
slouken@3571
   167
        }
slouken@1936
   168
    }
slouken@1936
   169
slouken@1936
   170
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@1936
   171
    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
slouken@1936
   172
    attr[i] = 0;
slouken@1936
   173
slouken@1936
   174
    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
slouken@1936
   175
    if (fmt == nil) {
slouken@1936
   176
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@1936
   177
        [pool release];
slouken@1936
   178
        return NULL;
slouken@1936
   179
    }
slouken@1936
   180
slouken@2178
   181
    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
slouken@1936
   182
slouken@1936
   183
    [fmt release];
slouken@1936
   184
slouken@2178
   185
    if (context == nil) {
slouken@1936
   186
        SDL_SetError ("Failed creating OpenGL context");
slouken@1936
   187
        [pool release];
slouken@1936
   188
        return NULL;
slouken@1936
   189
    }
slouken@1936
   190
slouken@1936
   191
    /*
slouken@1936
   192
     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
slouken@1936
   193
     *  "You are blowing a couple of the internal OpenGL function caches. This
slouken@1936
   194
     *  appears to be happening in the VAO case.  You can tell OpenGL to up
slouken@1936
   195
     *  the cache size by issuing the following calls right after you create
slouken@1936
   196
     *  the OpenGL context.  The default cache size is 16."    --ryan.
slouken@1936
   197
     */
slouken@1936
   198
slouken@1936
   199
    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
slouken@1936
   200
    #define GLI_ARRAY_FUNC_CACHE_MAX 284
slouken@1936
   201
    #endif
slouken@1936
   202
slouken@1936
   203
    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
slouken@1936
   204
    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
slouken@1936
   205
    #endif
slouken@1936
   206
slouken@1936
   207
    {
slouken@4925
   208
        GLint cache_max = 64;
slouken@2178
   209
        CGLContextObj ctx = [context CGLContextObj];
slouken@1936
   210
        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   211
        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   212
    }
slouken@1936
   213
slouken@1936
   214
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@1936
   215
slouken@1936
   216
    [pool release];
slouken@2178
   217
slouken@2178
   218
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   219
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   220
        return NULL;
slouken@2178
   221
    }
slouken@2178
   222
slouken@2178
   223
    return context;
slouken@1936
   224
}
slouken@1936
   225
slouken@1936
   226
int
slouken@1936
   227
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   228
{
slouken@1936
   229
    NSAutoreleasePool *pool;
slouken@1936
   230
slouken@1936
   231
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   232
slouken@1936
   233
    if (context) {
slouken@1936
   234
        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
slouken@1936
   235
        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   236
slouken@6260
   237
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@5249
   238
#ifndef FULLSCREEN_TOGGLEABLE
slouken@6260
   239
            if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@6260
   240
                [nscontext setFullScreen];
slouken@6260
   241
            } else
slouken@5249
   242
#endif
slouken@6260
   243
            {
slouken@6260
   244
                [nscontext setView:[windowdata->nswindow contentView]];
slouken@6260
   245
                [nscontext update];
slouken@6260
   246
            }
slouken@1973
   247
        }
slouken@1936
   248
        [nscontext makeCurrentContext];
slouken@1936
   249
    } else {
slouken@1936
   250
        [NSOpenGLContext clearCurrentContext];
slouken@1936
   251
    }
slouken@1936
   252
slouken@1936
   253
    [pool release];
slouken@1936
   254
    return 0;
slouken@1936
   255
}
slouken@1936
   256
slouken@1936
   257
int
slouken@1936
   258
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   259
{
slouken@1936
   260
    NSAutoreleasePool *pool;
slouken@1936
   261
    NSOpenGLContext *nscontext;
slouken@2703
   262
    GLint value;
slouken@1936
   263
    int status;
slouken@1936
   264
slouken@1936
   265
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   266
slouken@1936
   267
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   268
    if (nscontext != nil) {
slouken@1936
   269
        value = interval;
slouken@1936
   270
        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   271
        status = 0;
slouken@1936
   272
    } else {
slouken@1936
   273
        SDL_SetError("No current OpenGL context");
slouken@1936
   274
        status = -1;
slouken@1936
   275
    }
slouken@1936
   276
slouken@1936
   277
    [pool release];
slouken@1936
   278
    return status;
slouken@1936
   279
}
slouken@1936
   280
slouken@1936
   281
int
slouken@1936
   282
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   283
{
slouken@1936
   284
    NSAutoreleasePool *pool;
slouken@1936
   285
    NSOpenGLContext *nscontext;
slouken@2703
   286
    GLint value;
icculus@6382
   287
    int status = 0;
slouken@1936
   288
slouken@1936
   289
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   290
slouken@1936
   291
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   292
    if (nscontext != nil) {
slouken@1936
   293
        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   294
        status = (int)value;
slouken@1936
   295
    }
slouken@1936
   296
slouken@1936
   297
    [pool release];
slouken@1936
   298
    return status;
slouken@1936
   299
}
slouken@1936
   300
slouken@1936
   301
void
slouken@1936
   302
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   303
{
slouken@1936
   304
    NSAutoreleasePool *pool;
slouken@1936
   305
    NSOpenGLContext *nscontext;
slouken@1936
   306
slouken@1936
   307
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   308
slouken@1936
   309
    /* FIXME: Do we need to get the context for the window? */
slouken@1936
   310
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   311
    if (nscontext != nil) {
slouken@1936
   312
        [nscontext flushBuffer];
slouken@1936
   313
    }
slouken@1936
   314
slouken@1936
   315
    [pool release];
slouken@1936
   316
}
slouken@1936
   317
slouken@1936
   318
void
slouken@1936
   319
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   320
{
slouken@1936
   321
    NSAutoreleasePool *pool;
slouken@1936
   322
    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   323
slouken@1936
   324
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   325
slouken@1936
   326
    [nscontext clearDrawable];
slouken@1936
   327
    [nscontext release];
slouken@1936
   328
slouken@1936
   329
    [pool release];
slouken@1936
   330
}
slouken@1936
   331
slouken@1952
   332
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   333
slouken@1936
   334
/* vi: set ts=4 sw=4 expandtab: */