src/video/cocoa/SDL_cocoaopengl.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 07:28:01 +0000
changeset 3688 6512cba48440
parent 3685 64ce267332c6
child 4498 3d91e31fcf71
permissions -rw-r--r--
Fixed Cocoa and OpenGL builds
slouken@1936
     1
/*
slouken@1936
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1936
     4
slouken@1936
     5
    This library is free software; you can redistribute it and/or
slouken@1936
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1936
     7
    License as published by the Free Software Foundation; either
slouken@1936
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1936
     9
slouken@1936
    10
    This library is distributed in the hope that it will be useful,
slouken@1936
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1936
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1936
    13
    Lesser General Public License for more details.
slouken@1936
    14
slouken@1936
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1936
    16
    License along with this library; if not, write to the Free Software
slouken@1936
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1936
    18
slouken@1936
    19
    Sam Lantinga
slouken@1936
    20
    slouken@libsdl.org
slouken@1936
    21
*/
slouken@1936
    22
#include "SDL_config.h"
slouken@1936
    23
slouken@1936
    24
#include "SDL_cocoavideo.h"
slouken@1936
    25
slouken@1936
    26
/* NSOpenGL implementation of SDL OpenGL support */
slouken@1936
    27
slouken@1952
    28
#if SDL_VIDEO_OPENGL_CGL
slouken@1936
    29
#include <OpenGL/CGLTypes.h>
slouken@2738
    30
#include <OpenGL/OpenGL.h>
slouken@3570
    31
#include <OpenGL/CGLRenderers.h>
slouken@1936
    32
slouken@1936
    33
#include "SDL_loadso.h"
slouken@1936
    34
#include "SDL_opengl.h"
slouken@1936
    35
slouken@1936
    36
slouken@1952
    37
#define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
slouken@1936
    38
slouken@1936
    39
/* This is implemented in Mac OS X 10.3 and above */
slouken@1936
    40
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
slouken@1936
    41
@implementation NSOpenGLContext(CGLContextAccess)
slouken@1936
    42
- (CGLContextObj)CGLContextObj;
slouken@1936
    43
{
slouken@1936
    44
    return _contextAuxiliary;
slouken@1936
    45
}
slouken@1936
    46
@end
slouken@1936
    47
#endif /* < 10.3 */
slouken@1936
    48
slouken@1936
    49
int
slouken@1936
    50
Cocoa_GL_LoadLibrary(_THIS, const char *path)
slouken@1936
    51
{
slouken@3057
    52
    /* Load the OpenGL library */
slouken@1936
    53
    if (path == NULL) {
slouken@1952
    54
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    55
    }
slouken@1952
    56
    if (path == NULL) {
slouken@1952
    57
        path = DEFAULT_OPENGL;
slouken@1936
    58
    }
slouken@1936
    59
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@1936
    60
    if (!_this->gl_config.dll_handle) {
slouken@1936
    61
        return -1;
slouken@1936
    62
    }
slouken@1936
    63
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1936
    64
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1936
    65
    return 0;
slouken@1936
    66
}
slouken@1936
    67
slouken@1936
    68
void *
slouken@1936
    69
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
slouken@1936
    70
{
slouken@1936
    71
    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1936
    72
}
slouken@1936
    73
slouken@3057
    74
void
slouken@1936
    75
Cocoa_GL_UnloadLibrary(_THIS)
slouken@1936
    76
{
slouken@3057
    77
    SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
    78
    _this->gl_config.dll_handle = NULL;
slouken@1936
    79
}
slouken@1936
    80
slouken@1936
    81
SDL_GLContext
slouken@1936
    82
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1936
    83
{
slouken@1936
    84
    NSAutoreleasePool *pool;
slouken@3685
    85
    SDL_VideoDisplay *display = window->display;
slouken@1936
    86
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
    87
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
    88
    NSOpenGLPixelFormat *fmt;
slouken@2178
    89
    NSOpenGLContext *context;
slouken@1936
    90
    int i = 0;
slouken@1936
    91
slouken@1936
    92
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
    93
slouken@1936
    94
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1936
    95
        attr[i++] = NSOpenGLPFAFullScreen;
slouken@1936
    96
    }
slouken@1936
    97
slouken@1936
    98
    attr[i++] = NSOpenGLPFAColorSize;
slouken@1936
    99
    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
slouken@1936
   100
slouken@1936
   101
    attr[i++] = NSOpenGLPFADepthSize;
slouken@1936
   102
    attr[i++] = _this->gl_config.depth_size;
slouken@1936
   103
slouken@1936
   104
    if (_this->gl_config.double_buffer) {
slouken@1936
   105
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@1936
   106
    }
slouken@1936
   107
slouken@1936
   108
    if (_this->gl_config.stereo) {
slouken@1936
   109
        attr[i++] = NSOpenGLPFAStereo;
slouken@1936
   110
    }
slouken@1936
   111
slouken@1936
   112
    if (_this->gl_config.stencil_size) {
slouken@1936
   113
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@1936
   114
        attr[i++] = _this->gl_config.stencil_size;
slouken@1936
   115
    }
slouken@1936
   116
slouken@1936
   117
    if ((_this->gl_config.accum_red_size +
slouken@1936
   118
         _this->gl_config.accum_green_size +
slouken@1936
   119
         _this->gl_config.accum_blue_size +
slouken@1936
   120
         _this->gl_config.accum_alpha_size) > 0) {
slouken@1936
   121
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@1936
   122
        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
   123
    }
slouken@1936
   124
slouken@1936
   125
    if (_this->gl_config.multisamplebuffers) {
slouken@1936
   126
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@1936
   127
        attr[i++] = _this->gl_config.multisamplebuffers;
slouken@1936
   128
    }
slouken@1936
   129
slouken@1936
   130
    if (_this->gl_config.multisamplesamples) {
slouken@1936
   131
        attr[i++] = NSOpenGLPFASamples;
slouken@1936
   132
        attr[i++] = _this->gl_config.multisamplesamples;
slouken@1936
   133
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@1936
   134
    }
slouken@1936
   135
slouken@3571
   136
    if (_this->gl_config.accelerated >= 0) {
slouken@3571
   137
        if (_this->gl_config.accelerated) {
slouken@3571
   138
            attr[i++] = NSOpenGLPFAAccelerated;
slouken@3571
   139
        } else {
slouken@3571
   140
            attr[i++] = NSOpenGLPFARendererID;
slouken@3571
   141
            attr[i++] = kCGLRendererGenericFloatID;
slouken@3571
   142
        }
slouken@1936
   143
    }
slouken@1936
   144
slouken@1936
   145
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@1936
   146
    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
slouken@1936
   147
    attr[i] = 0;
slouken@1936
   148
slouken@1936
   149
    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
slouken@1936
   150
    if (fmt == nil) {
slouken@1936
   151
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@1936
   152
        [pool release];
slouken@1936
   153
        return NULL;
slouken@1936
   154
    }
slouken@1936
   155
slouken@2178
   156
    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
slouken@1936
   157
slouken@1936
   158
    [fmt release];
slouken@1936
   159
slouken@2178
   160
    if (context == nil) {
slouken@1936
   161
        SDL_SetError ("Failed creating OpenGL context");
slouken@1936
   162
        [pool release];
slouken@1936
   163
        return NULL;
slouken@1936
   164
    }
slouken@1936
   165
slouken@1936
   166
    /*
slouken@1936
   167
     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
slouken@1936
   168
     *  "You are blowing a couple of the internal OpenGL function caches. This
slouken@1936
   169
     *  appears to be happening in the VAO case.  You can tell OpenGL to up
slouken@1936
   170
     *  the cache size by issuing the following calls right after you create
slouken@1936
   171
     *  the OpenGL context.  The default cache size is 16."    --ryan.
slouken@1936
   172
     */
slouken@1936
   173
slouken@1936
   174
    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
slouken@1936
   175
    #define GLI_ARRAY_FUNC_CACHE_MAX 284
slouken@1936
   176
    #endif
slouken@1936
   177
slouken@1936
   178
    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
slouken@1936
   179
    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
slouken@1936
   180
    #endif
slouken@1936
   181
slouken@1936
   182
    {
slouken@1936
   183
        long cache_max = 64;
slouken@2178
   184
        CGLContextObj ctx = [context CGLContextObj];
slouken@1936
   185
        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   186
        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   187
    }
slouken@1936
   188
slouken@1936
   189
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@1936
   190
slouken@1936
   191
    [pool release];
slouken@2178
   192
slouken@2178
   193
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   194
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   195
        return NULL;
slouken@2178
   196
    }
slouken@2178
   197
slouken@2178
   198
    return context;
slouken@1936
   199
}
slouken@1936
   200
slouken@1936
   201
int
slouken@1936
   202
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   203
{
slouken@1936
   204
    NSAutoreleasePool *pool;
slouken@1936
   205
slouken@1936
   206
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   207
slouken@1936
   208
    if (context) {
slouken@1936
   209
        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
slouken@1936
   210
        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   211
slouken@1973
   212
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1973
   213
            [nscontext setFullScreen];
slouken@1973
   214
        } else {
slouken@3688
   215
            [nscontext setView:[windowdata->nswindow contentView]];
slouken@1973
   216
            [nscontext update];
slouken@1973
   217
        }
slouken@1936
   218
        [nscontext makeCurrentContext];
slouken@1936
   219
    } else {
slouken@1936
   220
        [NSOpenGLContext clearCurrentContext];
slouken@1936
   221
    }
slouken@1936
   222
slouken@1936
   223
    [pool release];
slouken@1936
   224
    return 0;
slouken@1936
   225
}
slouken@1936
   226
slouken@1936
   227
int
slouken@1936
   228
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   229
{
slouken@1936
   230
    NSAutoreleasePool *pool;
slouken@1936
   231
    NSOpenGLContext *nscontext;
slouken@2703
   232
    GLint value;
slouken@1936
   233
    int status;
slouken@1936
   234
slouken@1936
   235
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   236
slouken@1936
   237
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   238
    if (nscontext != nil) {
slouken@1936
   239
        value = interval;
slouken@1936
   240
        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   241
        status = 0;
slouken@1936
   242
    } else {
slouken@1936
   243
        SDL_SetError("No current OpenGL context");
slouken@1936
   244
        status = -1;
slouken@1936
   245
    }
slouken@1936
   246
slouken@1936
   247
    [pool release];
slouken@1936
   248
    return status;
slouken@1936
   249
}
slouken@1936
   250
slouken@1936
   251
int
slouken@1936
   252
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   253
{
slouken@1936
   254
    NSAutoreleasePool *pool;
slouken@1936
   255
    NSOpenGLContext *nscontext;
slouken@2703
   256
    GLint value;
slouken@1936
   257
    int status;
slouken@1936
   258
slouken@1936
   259
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   260
slouken@1936
   261
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   262
    if (nscontext != nil) {
slouken@1936
   263
        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   264
        status = (int)value;
slouken@1936
   265
    } else {
slouken@1936
   266
        SDL_SetError("No current OpenGL context");
slouken@1936
   267
        status = -1;
slouken@1936
   268
    }
slouken@1936
   269
slouken@1936
   270
    [pool release];
slouken@1936
   271
    return status;
slouken@1936
   272
}
slouken@1936
   273
slouken@1936
   274
void
slouken@1936
   275
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   276
{
slouken@1936
   277
    NSAutoreleasePool *pool;
slouken@1936
   278
    NSOpenGLContext *nscontext;
slouken@1936
   279
slouken@1936
   280
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   281
slouken@1936
   282
    /* FIXME: Do we need to get the context for the window? */
slouken@1936
   283
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   284
    if (nscontext != nil) {
slouken@1936
   285
        [nscontext flushBuffer];
slouken@1936
   286
    }
slouken@1936
   287
slouken@1936
   288
    [pool release];
slouken@1936
   289
}
slouken@1936
   290
slouken@1936
   291
void
slouken@1936
   292
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   293
{
slouken@1936
   294
    NSAutoreleasePool *pool;
slouken@1936
   295
    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   296
slouken@1936
   297
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   298
slouken@1936
   299
    [nscontext clearDrawable];
slouken@1936
   300
    [nscontext release];
slouken@1936
   301
slouken@1936
   302
    [pool release];
slouken@1936
   303
}
slouken@1936
   304
slouken@1952
   305
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   306
slouken@1936
   307
/* vi: set ts=4 sw=4 expandtab: */