src/video/cocoa/SDL_cocoaopengl.m
author stopiccot <alexey.petruchik@gmail.com>
Sun, 06 Jan 2013 19:04:53 +0300
changeset 6832 156e608ec4ef
parent 6567 d50613615139
child 6848 478ecc8a58b3
permissions -rw-r--r--
Replaced manual NSAutoreleasePool handing with @autorelease
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@5246
    83
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1936
    84
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
    85
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
    86
    NSOpenGLPixelFormat *fmt;
slouken@2178
    87
    NSOpenGLContext *context;
slouken@1936
    88
    int i = 0;
slouken@1936
    89
icculus@6567
    90
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
icculus@6567
    91
        SDL_SetError ("OpenGL ES not supported on this platform");
icculus@6567
    92
        return NULL;
icculus@6567
    93
    }
icculus@6567
    94
icculus@6567
    95
    /* Sadly, we'll have to update this as life progresses, since we need to
icculus@6567
    96
       set an enum for context profiles, not a context version number */
icculus@6567
    97
    if (wantver > 0x0302) {
icculus@6567
    98
        SDL_SetError ("OpenGL > 3.2 is not supported on this platform");
icculus@6567
    99
        return NULL;
icculus@6567
   100
    }
icculus@6567
   101
alexey@6832
   102
    @autoreleasepool {
alexey@6832
   103
        /* specify a profile if we're on Lion (10.7) or later. */
alexey@6832
   104
        if (data->osversion >= 0x1070) {
alexey@6832
   105
            NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
alexey@6832
   106
            if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
alexey@6832
   107
                if (wantver == 0x0302) {
alexey@6832
   108
                    profile = kCGLOGLPVersion_3_2_Core;
alexey@6832
   109
                }
alexey@6832
   110
            }
alexey@6832
   111
            attr[i++] = kCGLPFAOpenGLProfile;
alexey@6832
   112
            attr[i++] = profile;
alexey@6832
   113
        }
slouken@1936
   114
alexey@6832
   115
    #ifndef FULLSCREEN_TOGGLEABLE
alexey@6832
   116
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
alexey@6832
   117
            attr[i++] = NSOpenGLPFAFullScreen;
alexey@6832
   118
        }
alexey@6832
   119
    #endif
alexey@6832
   120
alexey@6832
   121
        attr[i++] = NSOpenGLPFAColorSize;
alexey@6832
   122
        attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
alexey@6832
   123
alexey@6832
   124
        attr[i++] = NSOpenGLPFADepthSize;
alexey@6832
   125
        attr[i++] = _this->gl_config.depth_size;
alexey@6832
   126
alexey@6832
   127
        if (_this->gl_config.double_buffer) {
alexey@6832
   128
            attr[i++] = NSOpenGLPFADoubleBuffer;
alexey@6832
   129
        }
alexey@6832
   130
alexey@6832
   131
        if (_this->gl_config.stereo) {
alexey@6832
   132
            attr[i++] = NSOpenGLPFAStereo;
alexey@6832
   133
        }
alexey@6832
   134
alexey@6832
   135
        if (_this->gl_config.stencil_size) {
alexey@6832
   136
            attr[i++] = NSOpenGLPFAStencilSize;
alexey@6832
   137
            attr[i++] = _this->gl_config.stencil_size;
alexey@6832
   138
        }
alexey@6832
   139
alexey@6832
   140
        if ((_this->gl_config.accum_red_size +
alexey@6832
   141
             _this->gl_config.accum_green_size +
alexey@6832
   142
             _this->gl_config.accum_blue_size +
alexey@6832
   143
             _this->gl_config.accum_alpha_size) > 0) {
alexey@6832
   144
            attr[i++] = NSOpenGLPFAAccumSize;
alexey@6832
   145
            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;
alexey@6832
   146
        }
alexey@6832
   147
alexey@6832
   148
        if (_this->gl_config.multisamplebuffers) {
alexey@6832
   149
            attr[i++] = NSOpenGLPFASampleBuffers;
alexey@6832
   150
            attr[i++] = _this->gl_config.multisamplebuffers;
alexey@6832
   151
        }
alexey@6832
   152
alexey@6832
   153
        if (_this->gl_config.multisamplesamples) {
alexey@6832
   154
            attr[i++] = NSOpenGLPFASamples;
alexey@6832
   155
            attr[i++] = _this->gl_config.multisamplesamples;
alexey@6832
   156
            attr[i++] = NSOpenGLPFANoRecovery;
alexey@6832
   157
        }
alexey@6832
   158
alexey@6832
   159
        if (_this->gl_config.accelerated >= 0) {
alexey@6832
   160
            if (_this->gl_config.accelerated) {
alexey@6832
   161
                attr[i++] = NSOpenGLPFAAccelerated;
alexey@6832
   162
            } else {
alexey@6832
   163
                attr[i++] = NSOpenGLPFARendererID;
alexey@6832
   164
                attr[i++] = kCGLRendererGenericFloatID;
icculus@6567
   165
            }
icculus@6567
   166
        }
icculus@6567
   167
alexey@6832
   168
        attr[i++] = NSOpenGLPFAScreenMask;
alexey@6832
   169
        attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
alexey@6832
   170
        attr[i] = 0;
slouken@1936
   171
alexey@6832
   172
        fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
alexey@6832
   173
        if (fmt == nil) {
alexey@6832
   174
            SDL_SetError ("Failed creating OpenGL pixel format");
alexey@6832
   175
            return NULL;
alexey@6832
   176
        }
slouken@1936
   177
alexey@6832
   178
        context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
slouken@1936
   179
alexey@6832
   180
        [fmt release];
slouken@1936
   181
alexey@6832
   182
        if (context == nil) {
alexey@6832
   183
            SDL_SetError ("Failed creating OpenGL context");
alexey@6832
   184
            return NULL;
alexey@6832
   185
        }
slouken@1936
   186
alexey@6832
   187
        /*
alexey@6832
   188
         * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
alexey@6832
   189
         *  "You are blowing a couple of the internal OpenGL function caches. This
alexey@6832
   190
         *  appears to be happening in the VAO case.  You can tell OpenGL to up
alexey@6832
   191
         *  the cache size by issuing the following calls right after you create
alexey@6832
   192
         *  the OpenGL context.  The default cache size is 16."    --ryan.
alexey@6832
   193
         */
slouken@1936
   194
alexey@6832
   195
        #ifndef GLI_ARRAY_FUNC_CACHE_MAX
alexey@6832
   196
        #define GLI_ARRAY_FUNC_CACHE_MAX 284
alexey@6832
   197
        #endif
slouken@1936
   198
alexey@6832
   199
        #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
alexey@6832
   200
        #define GLI_SUBMIT_FUNC_CACHE_MAX 280
alexey@6832
   201
        #endif
slouken@1936
   202
alexey@6832
   203
        {
alexey@6832
   204
            GLint cache_max = 64;
alexey@6832
   205
            CGLContextObj ctx = [context CGLContextObj];
alexey@6832
   206
            CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
alexey@6832
   207
            CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@3571
   208
        }
slouken@1936
   209
    }
slouken@1936
   210
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@1936
   211
slouken@2178
   212
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   213
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   214
        return NULL;
slouken@2178
   215
    }
slouken@2178
   216
slouken@2178
   217
    return context;
slouken@1936
   218
}
slouken@1936
   219
slouken@1936
   220
int
slouken@1936
   221
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   222
{
alexey@6832
   223
    @autoreleasepool {
alexey@6832
   224
        if (context) {
alexey@6832
   225
            SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
alexey@6832
   226
            NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   227
alexey@6832
   228
            if (window->flags & SDL_WINDOW_SHOWN) {
alexey@6832
   229
    #ifndef FULLSCREEN_TOGGLEABLE
alexey@6832
   230
                if (window->flags & SDL_WINDOW_FULLSCREEN) {
alexey@6832
   231
                    [nscontext setFullScreen];
alexey@6832
   232
                } else
alexey@6832
   233
    #endif
alexey@6832
   234
                {
alexey@6832
   235
                    [nscontext setView:[windowdata->nswindow contentView]];
alexey@6832
   236
                    [nscontext update];
alexey@6832
   237
                }
slouken@6260
   238
            }
alexey@6832
   239
            [nscontext makeCurrentContext];
alexey@6832
   240
        } else {
alexey@6832
   241
            [NSOpenGLContext clearCurrentContext];
slouken@1973
   242
        }
slouken@1936
   243
    }
slouken@1936
   244
slouken@1936
   245
    return 0;
slouken@1936
   246
}
slouken@1936
   247
slouken@1936
   248
int
slouken@1936
   249
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   250
{
slouken@1936
   251
    NSOpenGLContext *nscontext;
slouken@2703
   252
    GLint value;
slouken@1936
   253
    int status;
slouken@1936
   254
alexey@6832
   255
    @autoreleasepool {
alexey@6832
   256
        nscontext = [NSOpenGLContext currentContext];
alexey@6832
   257
        if (nscontext != nil) {
alexey@6832
   258
            value = interval;
alexey@6832
   259
            [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
alexey@6832
   260
            status = 0;
alexey@6832
   261
        } else {
alexey@6832
   262
            SDL_SetError("No current OpenGL context");
alexey@6832
   263
            status = -1;
alexey@6832
   264
        }
slouken@1936
   265
    }
slouken@1936
   266
slouken@1936
   267
    return status;
slouken@1936
   268
}
slouken@1936
   269
slouken@1936
   270
int
slouken@1936
   271
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   272
{
slouken@1936
   273
    NSOpenGLContext *nscontext;
slouken@2703
   274
    GLint value;
icculus@6382
   275
    int status = 0;
slouken@1936
   276
alexey@6832
   277
    @autoreleasepool {
alexey@6832
   278
        nscontext = [NSOpenGLContext currentContext];
alexey@6832
   279
        if (nscontext != nil) {
alexey@6832
   280
            [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
alexey@6832
   281
            status = (int)value;
alexey@6832
   282
        }
slouken@1936
   283
    }
slouken@1936
   284
slouken@1936
   285
    return status;
slouken@1936
   286
}
slouken@1936
   287
slouken@1936
   288
void
slouken@1936
   289
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   290
{
slouken@1936
   291
    NSOpenGLContext *nscontext;
slouken@1936
   292
alexey@6832
   293
    @autoreleasepool {
alexey@6832
   294
        /* FIXME: Do we need to get the context for the window? */
alexey@6832
   295
        nscontext = [NSOpenGLContext currentContext];
alexey@6832
   296
        if (nscontext != nil) {
alexey@6832
   297
            [nscontext flushBuffer];
alexey@6832
   298
        }
slouken@1936
   299
    }
slouken@1936
   300
}
slouken@1936
   301
slouken@1936
   302
void
slouken@1936
   303
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   304
{
slouken@1936
   305
    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   306
alexey@6832
   307
    @autoreleasepool {
alexey@6832
   308
        [nscontext clearDrawable];
alexey@6832
   309
        [nscontext release];
alexey@6832
   310
    }
slouken@1936
   311
}
slouken@1936
   312
slouken@1952
   313
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   314
slouken@1936
   315
/* vi: set ts=4 sw=4 expandtab: */