src/video/cocoa/SDL_cocoaopengl.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 6044 35448a5ea044
child 6260 fd494c5f305b
permissions -rwxr-xr-x
Happy New Year!
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
slouken@1936
    35
slouken@1952
    36
#define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
slouken@1936
    37
slouken@1936
    38
int
slouken@1936
    39
Cocoa_GL_LoadLibrary(_THIS, const char *path)
slouken@1936
    40
{
slouken@3057
    41
    /* Load the OpenGL library */
slouken@1936
    42
    if (path == NULL) {
slouken@1952
    43
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    44
    }
slouken@1952
    45
    if (path == NULL) {
slouken@1952
    46
        path = DEFAULT_OPENGL;
slouken@1936
    47
    }
slouken@1936
    48
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@1936
    49
    if (!_this->gl_config.dll_handle) {
slouken@1936
    50
        return -1;
slouken@1936
    51
    }
slouken@1936
    52
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1936
    53
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1936
    54
    return 0;
slouken@1936
    55
}
slouken@1936
    56
slouken@1936
    57
void *
slouken@1936
    58
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
slouken@1936
    59
{
slouken@1936
    60
    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1936
    61
}
slouken@1936
    62
slouken@3057
    63
void
slouken@1936
    64
Cocoa_GL_UnloadLibrary(_THIS)
slouken@1936
    65
{
slouken@3057
    66
    SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
    67
    _this->gl_config.dll_handle = NULL;
slouken@1936
    68
}
slouken@1936
    69
slouken@1936
    70
SDL_GLContext
slouken@1936
    71
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1936
    72
{
slouken@1936
    73
    NSAutoreleasePool *pool;
slouken@5246
    74
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1936
    75
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
    76
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
    77
    NSOpenGLPixelFormat *fmt;
slouken@2178
    78
    NSOpenGLContext *context;
slouken@1936
    79
    int i = 0;
slouken@1936
    80
slouken@1936
    81
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
    82
slouken@5249
    83
#ifndef FULLSCREEN_TOGGLEABLE
slouken@1936
    84
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1936
    85
        attr[i++] = NSOpenGLPFAFullScreen;
slouken@1936
    86
    }
slouken@5249
    87
#endif
slouken@1936
    88
slouken@1936
    89
    attr[i++] = NSOpenGLPFAColorSize;
slouken@1936
    90
    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
slouken@1936
    91
slouken@1936
    92
    attr[i++] = NSOpenGLPFADepthSize;
slouken@1936
    93
    attr[i++] = _this->gl_config.depth_size;
slouken@1936
    94
slouken@1936
    95
    if (_this->gl_config.double_buffer) {
slouken@1936
    96
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@1936
    97
    }
slouken@1936
    98
slouken@1936
    99
    if (_this->gl_config.stereo) {
slouken@1936
   100
        attr[i++] = NSOpenGLPFAStereo;
slouken@1936
   101
    }
slouken@1936
   102
slouken@1936
   103
    if (_this->gl_config.stencil_size) {
slouken@1936
   104
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@1936
   105
        attr[i++] = _this->gl_config.stencil_size;
slouken@1936
   106
    }
slouken@1936
   107
slouken@1936
   108
    if ((_this->gl_config.accum_red_size +
slouken@1936
   109
         _this->gl_config.accum_green_size +
slouken@1936
   110
         _this->gl_config.accum_blue_size +
slouken@1936
   111
         _this->gl_config.accum_alpha_size) > 0) {
slouken@1936
   112
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@1936
   113
        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
   114
    }
slouken@1936
   115
slouken@1936
   116
    if (_this->gl_config.multisamplebuffers) {
slouken@1936
   117
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@1936
   118
        attr[i++] = _this->gl_config.multisamplebuffers;
slouken@1936
   119
    }
slouken@1936
   120
slouken@1936
   121
    if (_this->gl_config.multisamplesamples) {
slouken@1936
   122
        attr[i++] = NSOpenGLPFASamples;
slouken@1936
   123
        attr[i++] = _this->gl_config.multisamplesamples;
slouken@1936
   124
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@1936
   125
    }
slouken@1936
   126
slouken@3571
   127
    if (_this->gl_config.accelerated >= 0) {
slouken@3571
   128
        if (_this->gl_config.accelerated) {
slouken@3571
   129
            attr[i++] = NSOpenGLPFAAccelerated;
slouken@3571
   130
        } else {
slouken@3571
   131
            attr[i++] = NSOpenGLPFARendererID;
slouken@3571
   132
            attr[i++] = kCGLRendererGenericFloatID;
slouken@3571
   133
        }
slouken@1936
   134
    }
slouken@1936
   135
slouken@1936
   136
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@1936
   137
    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
slouken@1936
   138
    attr[i] = 0;
slouken@1936
   139
slouken@1936
   140
    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
slouken@1936
   141
    if (fmt == nil) {
slouken@1936
   142
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@1936
   143
        [pool release];
slouken@1936
   144
        return NULL;
slouken@1936
   145
    }
slouken@1936
   146
slouken@2178
   147
    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
slouken@1936
   148
slouken@1936
   149
    [fmt release];
slouken@1936
   150
slouken@2178
   151
    if (context == nil) {
slouken@1936
   152
        SDL_SetError ("Failed creating OpenGL context");
slouken@1936
   153
        [pool release];
slouken@1936
   154
        return NULL;
slouken@1936
   155
    }
slouken@1936
   156
slouken@1936
   157
    /*
slouken@1936
   158
     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
slouken@1936
   159
     *  "You are blowing a couple of the internal OpenGL function caches. This
slouken@1936
   160
     *  appears to be happening in the VAO case.  You can tell OpenGL to up
slouken@1936
   161
     *  the cache size by issuing the following calls right after you create
slouken@1936
   162
     *  the OpenGL context.  The default cache size is 16."    --ryan.
slouken@1936
   163
     */
slouken@1936
   164
slouken@1936
   165
    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
slouken@1936
   166
    #define GLI_ARRAY_FUNC_CACHE_MAX 284
slouken@1936
   167
    #endif
slouken@1936
   168
slouken@1936
   169
    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
slouken@1936
   170
    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
slouken@1936
   171
    #endif
slouken@1936
   172
slouken@1936
   173
    {
slouken@4925
   174
        GLint cache_max = 64;
slouken@2178
   175
        CGLContextObj ctx = [context CGLContextObj];
slouken@1936
   176
        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   177
        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   178
    }
slouken@1936
   179
slouken@1936
   180
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@1936
   181
slouken@1936
   182
    [pool release];
slouken@2178
   183
slouken@2178
   184
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   185
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   186
        return NULL;
slouken@2178
   187
    }
slouken@2178
   188
slouken@2178
   189
    return context;
slouken@1936
   190
}
slouken@1936
   191
slouken@1936
   192
int
slouken@1936
   193
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   194
{
slouken@1936
   195
    NSAutoreleasePool *pool;
slouken@1936
   196
slouken@1936
   197
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   198
slouken@1936
   199
    if (context) {
slouken@1936
   200
        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
slouken@1936
   201
        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   202
slouken@5249
   203
#ifndef FULLSCREEN_TOGGLEABLE
slouken@1973
   204
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1973
   205
            [nscontext setFullScreen];
slouken@5249
   206
        } else
slouken@5249
   207
#endif
slouken@5249
   208
        {
slouken@3688
   209
            [nscontext setView:[windowdata->nswindow contentView]];
slouken@1973
   210
            [nscontext update];
slouken@1973
   211
        }
slouken@1936
   212
        [nscontext makeCurrentContext];
slouken@1936
   213
    } else {
slouken@1936
   214
        [NSOpenGLContext clearCurrentContext];
slouken@1936
   215
    }
slouken@1936
   216
slouken@1936
   217
    [pool release];
slouken@1936
   218
    return 0;
slouken@1936
   219
}
slouken@1936
   220
slouken@1936
   221
int
slouken@1936
   222
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   223
{
slouken@1936
   224
    NSAutoreleasePool *pool;
slouken@1936
   225
    NSOpenGLContext *nscontext;
slouken@2703
   226
    GLint value;
slouken@1936
   227
    int status;
slouken@1936
   228
slouken@1936
   229
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   230
slouken@1936
   231
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   232
    if (nscontext != nil) {
slouken@1936
   233
        value = interval;
slouken@1936
   234
        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   235
        status = 0;
slouken@1936
   236
    } else {
slouken@1936
   237
        SDL_SetError("No current OpenGL context");
slouken@1936
   238
        status = -1;
slouken@1936
   239
    }
slouken@1936
   240
slouken@1936
   241
    [pool release];
slouken@1936
   242
    return status;
slouken@1936
   243
}
slouken@1936
   244
slouken@1936
   245
int
slouken@1936
   246
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   247
{
slouken@1936
   248
    NSAutoreleasePool *pool;
slouken@1936
   249
    NSOpenGLContext *nscontext;
slouken@2703
   250
    GLint value;
slouken@1936
   251
    int status;
slouken@1936
   252
slouken@1936
   253
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   254
slouken@1936
   255
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   256
    if (nscontext != nil) {
slouken@1936
   257
        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   258
        status = (int)value;
slouken@1936
   259
    } else {
slouken@1936
   260
        SDL_SetError("No current OpenGL context");
slouken@1936
   261
        status = -1;
slouken@1936
   262
    }
slouken@1936
   263
slouken@1936
   264
    [pool release];
slouken@1936
   265
    return status;
slouken@1936
   266
}
slouken@1936
   267
slouken@1936
   268
void
slouken@1936
   269
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   270
{
slouken@1936
   271
    NSAutoreleasePool *pool;
slouken@1936
   272
    NSOpenGLContext *nscontext;
slouken@1936
   273
slouken@1936
   274
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   275
slouken@1936
   276
    /* FIXME: Do we need to get the context for the window? */
slouken@1936
   277
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   278
    if (nscontext != nil) {
slouken@1936
   279
        [nscontext flushBuffer];
slouken@1936
   280
    }
slouken@1936
   281
slouken@1936
   282
    [pool release];
slouken@1936
   283
}
slouken@1936
   284
slouken@1936
   285
void
slouken@1936
   286
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   287
{
slouken@1936
   288
    NSAutoreleasePool *pool;
slouken@1936
   289
    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   290
slouken@1936
   291
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   292
slouken@1936
   293
    [nscontext clearDrawable];
slouken@1936
   294
    [nscontext release];
slouken@1936
   295
slouken@1936
   296
    [pool release];
slouken@1936
   297
}
slouken@1936
   298
slouken@1952
   299
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   300
slouken@1936
   301
/* vi: set ts=4 sw=4 expandtab: */