src/video/quartz/SDL_QuartzGL.m
author Ryan C. Gordon
Tue, 22 Nov 2005 08:21:39 +0000
changeset 1181 49d3efec6651
parent 1177 e967ab22e6fd
child 1189 c96b326b90ba
permissions -rw-r--r--
Quartz driver OpenGL updates:

Driver can now open whatever library is specified in SDL_GL_LoadLibrary()
call (previously, it ignored this parameter), and uses the default system
library when NULL is specified.

Also, library is loaded once in SDL_GL_LoadLibrary() and not every call to
SDL_GL_GetProcAddress().
slouken@761
     1
/*
slouken@761
     2
    SDL - Simple DirectMedia Layer
slouken@761
     3
    Copyright (C) 1997-2003  Sam Lantinga
slouken@761
     4
slouken@761
     5
    This library is free software; you can redistribute it and/or
slouken@761
     6
    modify it under the terms of the GNU Library General Public
slouken@761
     7
    License as published by the Free Software Foundation; either
slouken@761
     8
    version 2 of the License, or (at your option) any later version.
slouken@761
     9
slouken@761
    10
    This library is distributed in the hope that it will be useful,
slouken@761
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@761
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@761
    13
    Library General Public License for more details.
slouken@761
    14
slouken@761
    15
    You should have received a copy of the GNU Library General Public
slouken@761
    16
    License along with this library; if not, write to the Free
slouken@761
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@761
    18
slouken@761
    19
    Sam Lantinga
slouken@761
    20
    slouken@libsdl.org
slouken@761
    21
*/
slouken@761
    22
slouken@761
    23
#include "SDL_QuartzVideo.h"
slouken@761
    24
icculus@857
    25
/*
icculus@857
    26
 * GL_ARB_Multisample is supposed to be available in 10.1, according to Apple:
icculus@857
    27
 *
icculus@857
    28
 *   http://developer.apple.com/opengl/extensions.html#GL_ARB_multisample
icculus@857
    29
 *
icculus@857
    30
 *  ...but it isn't in the system headers, according to Sam:
icculus@857
    31
 *
icculus@857
    32
 *   http://www.libsdl.org/pipermail/sdl/2003-December/058335.html
icculus@857
    33
 *
icculus@857
    34
 * These are normally enums and not #defines in the system headers.
icculus@857
    35
 *
icculus@857
    36
 *   --ryan.
icculus@857
    37
 */
icculus@857
    38
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1020)
icculus@857
    39
#define NSOpenGLPFASampleBuffers ((NSOpenGLPixelFormatAttribute) 55)
icculus@857
    40
#define NSOpenGLPFASamples ((NSOpenGLPixelFormatAttribute) 56)
icculus@857
    41
#endif
icculus@857
    42
slouken@761
    43
slouken@761
    44
@implementation NSOpenGLContext (CGLContextAccess)
slouken@761
    45
- (CGLContextObj) cglContext;
slouken@761
    46
{
slouken@761
    47
    return _contextAuxiliary;
slouken@761
    48
}
slouken@761
    49
@end
slouken@761
    50
slouken@761
    51
/* OpenGL helper functions (used internally) */
slouken@761
    52
slouken@761
    53
int QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags) {
slouken@761
    54
slouken@761
    55
    NSOpenGLPixelFormatAttribute attr[32];
slouken@761
    56
    NSOpenGLPixelFormat *fmt;
slouken@761
    57
    int i = 0;
slouken@761
    58
    int colorBits = bpp;
slouken@761
    59
slouken@761
    60
    if ( flags & SDL_FULLSCREEN ) {
slouken@761
    61
slouken@761
    62
        attr[i++] = NSOpenGLPFAFullScreen;
slouken@761
    63
    }
slouken@761
    64
    /* In windowed mode, the OpenGL pixel depth must match device pixel depth */
slouken@761
    65
    else if ( colorBits != device_bpp ) {
slouken@761
    66
slouken@761
    67
        colorBits = device_bpp;
slouken@761
    68
    }
slouken@761
    69
slouken@761
    70
    attr[i++] = NSOpenGLPFAColorSize;
slouken@761
    71
    attr[i++] = colorBits;
slouken@761
    72
slouken@761
    73
    attr[i++] = NSOpenGLPFADepthSize;
slouken@761
    74
    attr[i++] = this->gl_config.depth_size;
slouken@761
    75
slouken@761
    76
    if ( this->gl_config.double_buffer ) {
slouken@761
    77
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@761
    78
    }
slouken@761
    79
slouken@761
    80
    if ( this->gl_config.stereo ) {
slouken@761
    81
        attr[i++] = NSOpenGLPFAStereo;
slouken@761
    82
    }
slouken@761
    83
slouken@761
    84
    if ( this->gl_config.stencil_size != 0 ) {
slouken@761
    85
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@761
    86
        attr[i++] = this->gl_config.stencil_size;
slouken@761
    87
    }
slouken@761
    88
slouken@890
    89
    if ( (this->gl_config.accum_red_size +
slouken@890
    90
          this->gl_config.accum_green_size +
slouken@890
    91
          this->gl_config.accum_blue_size +
slouken@890
    92
          this->gl_config.accum_alpha_size) > 0 ) {
slouken@890
    93
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@890
    94
        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@890
    95
    }
slouken@890
    96
slouken@761
    97
    if ( this->gl_config.multisamplebuffers != 0 ) {
slouken@761
    98
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@761
    99
        attr[i++] = this->gl_config.multisamplebuffers;
slouken@761
   100
    }
slouken@761
   101
slouken@761
   102
    if ( this->gl_config.multisamplesamples != 0 ) {
slouken@761
   103
        attr[i++] = NSOpenGLPFASamples;
slouken@761
   104
        attr[i++] = this->gl_config.multisamplesamples;
slouken@900
   105
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@900
   106
        attr[i++] = (NSOpenGLPixelFormatAttribute)nil;
slouken@761
   107
    }
slouken@761
   108
slouken@761
   109
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@761
   110
    attr[i++] = CGDisplayIDToOpenGLDisplayMask (display_id);
slouken@761
   111
    attr[i] = 0;
slouken@761
   112
slouken@761
   113
    fmt = [ [ NSOpenGLPixelFormat alloc ] initWithAttributes:attr ];
slouken@761
   114
    if (fmt == nil) {
slouken@761
   115
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@761
   116
        return 0;
slouken@761
   117
    }
slouken@761
   118
slouken@761
   119
    gl_context = [ [ NSOpenGLContext alloc ] initWithFormat:fmt
slouken@761
   120
                                               shareContext:nil];
slouken@761
   121
icculus@1160
   122
    [ fmt release ];
icculus@1160
   123
slouken@761
   124
    if (gl_context == nil) {
slouken@761
   125
        SDL_SetError ("Failed creating OpenGL context");
slouken@761
   126
        return 0;
slouken@761
   127
    }
slouken@761
   128
slouken@761
   129
    /*
slouken@761
   130
     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
slouken@761
   131
     *  "You are blowing a couple of the internal OpenGL function caches. This
slouken@761
   132
     *  appears to be happening in the VAO case.  You can tell OpenGL to up
slouken@761
   133
     *  the cache size by issuing the following calls right after you create
slouken@761
   134
     *  the OpenGL context.  The default cache size is 16."    --ryan.
slouken@761
   135
     */
slouken@761
   136
slouken@761
   137
    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
slouken@761
   138
    #define GLI_ARRAY_FUNC_CACHE_MAX 284
slouken@761
   139
    #endif
slouken@761
   140
slouken@761
   141
    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
slouken@761
   142
    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
slouken@761
   143
    #endif
slouken@761
   144
slouken@761
   145
    {
slouken@761
   146
        long cache_max = 64;
slouken@761
   147
        CGLContextObj ctx = [ gl_context cglContext ];
slouken@761
   148
        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
slouken@761
   149
        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@761
   150
    }
slouken@761
   151
slouken@761
   152
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@761
   153
slouken@761
   154
    /* Convince SDL that the GL "driver" is loaded */
slouken@761
   155
    this->gl_config.driver_loaded = 1;
slouken@761
   156
slouken@761
   157
    return 1;
slouken@761
   158
}
slouken@761
   159
slouken@761
   160
void QZ_TearDownOpenGL (_THIS) {
slouken@761
   161
slouken@761
   162
    [ NSOpenGLContext clearCurrentContext ];
slouken@761
   163
    [ gl_context clearDrawable ];
slouken@761
   164
    [ gl_context release ];
slouken@761
   165
}
slouken@761
   166
slouken@761
   167
slouken@761
   168
/* SDL OpenGL functions */
slouken@761
   169
slouken@761
   170
int    QZ_GL_LoadLibrary    (_THIS, const char *location) {
icculus@1181
   171
    CFURLRef bundleURL;
icculus@1181
   172
    CFStringRef cfstr;
icculus@1181
   173
icculus@1181
   174
    if ( gl_context != NULL ) {
icculus@1181
   175
        SDL_SetError("OpenGL context already created");
icculus@1181
   176
        return -1;
icculus@1181
   177
    }
icculus@1181
   178
icculus@1181
   179
    if (opengl_bundle != NULL)
icculus@1181
   180
        CFRelease(opengl_bundle);
icculus@1181
   181
icculus@1181
   182
    opengl_bundle = NULL;
icculus@1181
   183
    this->gl_config.driver_loaded = 0;
icculus@1181
   184
icculus@1181
   185
    if (location == NULL)
icculus@1181
   186
        location = "/System/Library/Frameworks/OpenGL.framework";
icculus@1181
   187
icculus@1181
   188
    cfstr = CFStringCreateWithCString(kCFAllocatorDefault, location,
icculus@1181
   189
                                      kCFStringEncodingUTF8);
icculus@1181
   190
    if (cfstr == NULL) {
icculus@1181
   191
        SDL_OutOfMemory();
icculus@1181
   192
        return -1;
icculus@1181
   193
    }
icculus@1181
   194
icculus@1181
   195
    bundleURL = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
icculus@1181
   196
                        cfstr, kCFURLPOSIXPathStyle, true);
icculus@1181
   197
icculus@1181
   198
    CFRelease(cfstr);
icculus@1181
   199
icculus@1181
   200
    if (bundleURL == NULL) {
icculus@1181
   201
        SDL_OutOfMemory();
icculus@1181
   202
        return -1;
icculus@1181
   203
    }
icculus@1181
   204
icculus@1181
   205
    opengl_bundle = CFBundleCreate (kCFAllocatorDefault, bundleURL);
icculus@1181
   206
icculus@1181
   207
    CFRelease(bundleURL);
icculus@1181
   208
icculus@1181
   209
    if (opengl_bundle != NULL) {
icculus@1181
   210
        this->gl_config.driver_loaded = 1;
icculus@1181
   211
        return 0;
icculus@1181
   212
    }
icculus@1181
   213
icculus@1181
   214
    /* not exactly descriptive, but okay... */
icculus@1181
   215
    SDL_SetError("Could not load OpenGL library");
icculus@1181
   216
    return -1;
slouken@761
   217
}
slouken@761
   218
slouken@761
   219
void*  QZ_GL_GetProcAddress (_THIS, const char *proc) {
icculus@1181
   220
    CFStringRef funcName = CFStringCreateWithCString
slouken@761
   221
        (kCFAllocatorDefault, proc, kCFStringEncodingASCII);
slouken@761
   222
icculus@1181
   223
    void *func = CFBundleGetFunctionPointerForName(opengl_bundle, funcName);
icculus@1181
   224
    CFRelease (funcName);
icculus@1181
   225
    return func;
slouken@761
   226
}
slouken@761
   227
slouken@761
   228
int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value) {
slouken@761
   229
slouken@761
   230
    GLenum attr = 0;
slouken@761
   231
slouken@761
   232
    QZ_GL_MakeCurrent (this);
slouken@761
   233
slouken@761
   234
    switch (attrib) {
slouken@761
   235
        case SDL_GL_RED_SIZE: attr = GL_RED_BITS;   break;
slouken@761
   236
        case SDL_GL_BLUE_SIZE: attr = GL_BLUE_BITS;  break;
slouken@761
   237
        case SDL_GL_GREEN_SIZE: attr = GL_GREEN_BITS; break;
slouken@761
   238
        case SDL_GL_ALPHA_SIZE: attr = GL_ALPHA_BITS; break;
slouken@761
   239
        case SDL_GL_DOUBLEBUFFER: attr = GL_DOUBLEBUFFER; break;
slouken@761
   240
        case SDL_GL_DEPTH_SIZE: attr = GL_DEPTH_BITS;  break;
slouken@761
   241
        case SDL_GL_STENCIL_SIZE: attr = GL_STENCIL_BITS; break;
slouken@761
   242
        case SDL_GL_ACCUM_RED_SIZE: attr = GL_ACCUM_RED_BITS; break;
slouken@761
   243
        case SDL_GL_ACCUM_GREEN_SIZE: attr = GL_ACCUM_GREEN_BITS; break;
slouken@761
   244
        case SDL_GL_ACCUM_BLUE_SIZE: attr = GL_ACCUM_BLUE_BITS; break;
slouken@761
   245
        case SDL_GL_ACCUM_ALPHA_SIZE: attr = GL_ACCUM_ALPHA_BITS; break;
slouken@761
   246
        case SDL_GL_STEREO: attr = GL_STEREO; break;
slouken@761
   247
        case SDL_GL_MULTISAMPLEBUFFERS: attr = GL_SAMPLE_BUFFERS_ARB; break;
slouken@761
   248
        case SDL_GL_MULTISAMPLESAMPLES: attr = GL_SAMPLES_ARB; break;
slouken@761
   249
        case SDL_GL_BUFFER_SIZE:
slouken@761
   250
        {
slouken@761
   251
            GLint bits = 0;
slouken@761
   252
            GLint component;
slouken@761
   253
slouken@761
   254
            /* there doesn't seem to be a single flag in OpenGL for this! */
slouken@761
   255
            glGetIntegerv (GL_RED_BITS, &component);   bits += component;
slouken@761
   256
            glGetIntegerv (GL_GREEN_BITS,&component);  bits += component;
slouken@761
   257
            glGetIntegerv (GL_BLUE_BITS, &component);  bits += component;
slouken@761
   258
            glGetIntegerv (GL_ALPHA_BITS, &component); bits += component;
slouken@761
   259
slouken@761
   260
            *value = bits;
slouken@761
   261
        }
slouken@761
   262
        return 0;
slouken@761
   263
    }
slouken@761
   264
slouken@761
   265
    glGetIntegerv (attr, (GLint *)value);
slouken@761
   266
    return 0;
slouken@761
   267
}
slouken@761
   268
slouken@761
   269
int    QZ_GL_MakeCurrent    (_THIS) {
slouken@761
   270
    [ gl_context makeCurrentContext ];
slouken@761
   271
    return 0;
slouken@761
   272
}
slouken@761
   273
slouken@761
   274
void   QZ_GL_SwapBuffers    (_THIS) {
slouken@761
   275
    [ gl_context flushBuffer ];
slouken@761
   276
}