src/video/cocoa/SDL_cocoaopengl.m
changeset 1936 83946ee0ff1f
child 1952 420716272158
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Tue Jul 25 06:22:42 2006 +0000
     1.3 @@ -0,0 +1,357 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_cocoavideo.h"
    1.28 +
    1.29 +/* NSOpenGL implementation of SDL OpenGL support */
    1.30 +
    1.31 +#if SDL_VIDEO_OPENGL
    1.32 +#include <OpenGL/CGLTypes.h>
    1.33 +
    1.34 +#include "SDL_loadso.h"
    1.35 +#include "SDL_opengl.h"
    1.36 +
    1.37 +
    1.38 +#define DEFAULT_OPENGL_PATH  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
    1.39 +
    1.40 +/* This is implemented in Mac OS X 10.3 and above */
    1.41 +#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
    1.42 +@implementation NSOpenGLContext(CGLContextAccess)
    1.43 +- (CGLContextObj)CGLContextObj;
    1.44 +{
    1.45 +    return _contextAuxiliary;
    1.46 +}
    1.47 +@end
    1.48 +#endif /* < 10.3 */
    1.49 +
    1.50 +int
    1.51 +Cocoa_GL_LoadLibrary(_THIS, const char *path)
    1.52 +{
    1.53 +    if (_this->gl_config.driver_loaded) {
    1.54 +        if (path) {
    1.55 +            SDL_SetError("OpenGL library already loaded");
    1.56 +            return -1;
    1.57 +        } else {
    1.58 +            ++_this->gl_config.driver_loaded;
    1.59 +            return 0;
    1.60 +        }
    1.61 +    }
    1.62 +    if (path == NULL) {
    1.63 +        path = DEFAULT_OPENGL_PATH;
    1.64 +    }
    1.65 +    _this->gl_config.dll_handle = SDL_LoadObject(path);
    1.66 +    if (!_this->gl_config.dll_handle) {
    1.67 +        return -1;
    1.68 +    }
    1.69 +    SDL_strlcpy(_this->gl_config.driver_path, path,
    1.70 +                SDL_arraysize(_this->gl_config.driver_path));
    1.71 +    _this->gl_config.driver_loaded = 1;
    1.72 +    return 0;
    1.73 +}
    1.74 +
    1.75 +void *
    1.76 +Cocoa_GL_GetProcAddress(_THIS, const char *proc)
    1.77 +{
    1.78 +    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
    1.79 +}
    1.80 +
    1.81 +static void
    1.82 +Cocoa_GL_UnloadLibrary(_THIS)
    1.83 +{
    1.84 +    if (_this->gl_config.driver_loaded > 0) {
    1.85 +        if (--_this->gl_config.driver_loaded > 0) {
    1.86 +            return;
    1.87 +        }
    1.88 +        SDL_UnloadObject(_this->gl_config.dll_handle);
    1.89 +        _this->gl_config.dll_handle = NULL;
    1.90 +    }
    1.91 +}
    1.92 +
    1.93 +static void
    1.94 +Cocoa_GL_Shutdown(_THIS)
    1.95 +{
    1.96 +    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
    1.97 +        return;
    1.98 +    }
    1.99 +
   1.100 +    Cocoa_GL_UnloadLibrary(_this);
   1.101 +
   1.102 +    SDL_free(_this->gl_data);
   1.103 +    _this->gl_data = NULL;
   1.104 +}
   1.105 +
   1.106 +static int
   1.107 +Cocoa_GL_Initialize(_THIS)
   1.108 +{
   1.109 +    if (_this->gl_data) {
   1.110 +        ++_this->gl_data->initialized;
   1.111 +        return 0;
   1.112 +    }
   1.113 +
   1.114 +    _this->gl_data =
   1.115 +        (struct SDL_GLDriverData *) SDL_calloc(1,
   1.116 +                                               sizeof(struct
   1.117 +                                                      SDL_GLDriverData));
   1.118 +    if (!_this->gl_data) {
   1.119 +        SDL_OutOfMemory();
   1.120 +        return -1;
   1.121 +    }
   1.122 +    _this->gl_data->initialized = 1;
   1.123 +
   1.124 +    if (Cocoa_GL_LoadLibrary(_this, NULL) < 0) {
   1.125 +        return -1;
   1.126 +    }
   1.127 +    return 0;
   1.128 +}
   1.129 +
   1.130 +int
   1.131 +Cocoa_GL_SetupWindow(_THIS, SDL_Window * window)
   1.132 +{
   1.133 +    if (Cocoa_GL_Initialize(_this) < 0) {
   1.134 +        return -1;
   1.135 +    }
   1.136 +    return 0;
   1.137 +}
   1.138 +
   1.139 +void
   1.140 +Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window)
   1.141 +{
   1.142 +    Cocoa_GL_Shutdown(_this);
   1.143 +}
   1.144 +
   1.145 +SDL_GLContext
   1.146 +Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
   1.147 +{
   1.148 +    NSAutoreleasePool *pool;
   1.149 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.150 +    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
   1.151 +    NSOpenGLPixelFormatAttribute attr[32];
   1.152 +    NSOpenGLPixelFormat *fmt;
   1.153 +    NSOpenGLContext *nscontext;
   1.154 +    int i = 0;
   1.155 +
   1.156 +    pool = [[NSAutoreleasePool alloc] init];
   1.157 +
   1.158 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.159 +        attr[i++] = NSOpenGLPFAFullScreen;
   1.160 +    }
   1.161 +
   1.162 +    attr[i++] = NSOpenGLPFAColorSize;
   1.163 +    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
   1.164 +
   1.165 +    attr[i++] = NSOpenGLPFADepthSize;
   1.166 +    attr[i++] = _this->gl_config.depth_size;
   1.167 +
   1.168 +    if (_this->gl_config.double_buffer) {
   1.169 +        attr[i++] = NSOpenGLPFADoubleBuffer;
   1.170 +    }
   1.171 +
   1.172 +    if (_this->gl_config.stereo) {
   1.173 +        attr[i++] = NSOpenGLPFAStereo;
   1.174 +    }
   1.175 +
   1.176 +    if (_this->gl_config.stencil_size) {
   1.177 +        attr[i++] = NSOpenGLPFAStencilSize;
   1.178 +        attr[i++] = _this->gl_config.stencil_size;
   1.179 +    }
   1.180 +
   1.181 +    if ((_this->gl_config.accum_red_size +
   1.182 +         _this->gl_config.accum_green_size +
   1.183 +         _this->gl_config.accum_blue_size +
   1.184 +         _this->gl_config.accum_alpha_size) > 0) {
   1.185 +        attr[i++] = NSOpenGLPFAAccumSize;
   1.186 +        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;
   1.187 +    }
   1.188 +
   1.189 +    if (_this->gl_config.multisamplebuffers) {
   1.190 +        attr[i++] = NSOpenGLPFASampleBuffers;
   1.191 +        attr[i++] = _this->gl_config.multisamplebuffers;
   1.192 +    }
   1.193 +
   1.194 +    if (_this->gl_config.multisamplesamples) {
   1.195 +        attr[i++] = NSOpenGLPFASamples;
   1.196 +        attr[i++] = _this->gl_config.multisamplesamples;
   1.197 +        attr[i++] = NSOpenGLPFANoRecovery;
   1.198 +    }
   1.199 +
   1.200 +    if (_this->gl_config.accelerated > 0) {
   1.201 +        attr[i++] = NSOpenGLPFAAccelerated;
   1.202 +    }
   1.203 +
   1.204 +    attr[i++] = NSOpenGLPFAScreenMask;
   1.205 +    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
   1.206 +    attr[i] = 0;
   1.207 +
   1.208 +    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
   1.209 +    if (fmt == nil) {
   1.210 +        SDL_SetError ("Failed creating OpenGL pixel format");
   1.211 +        [pool release];
   1.212 +        return NULL;
   1.213 +    }
   1.214 +
   1.215 +    nscontext = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
   1.216 +
   1.217 +    [fmt release];
   1.218 +
   1.219 +    if (nscontext == nil) {
   1.220 +        SDL_SetError ("Failed creating OpenGL context");
   1.221 +        [pool release];
   1.222 +        return NULL;
   1.223 +    }
   1.224 +
   1.225 +    /*
   1.226 +     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
   1.227 +     *  "You are blowing a couple of the internal OpenGL function caches. This
   1.228 +     *  appears to be happening in the VAO case.  You can tell OpenGL to up
   1.229 +     *  the cache size by issuing the following calls right after you create
   1.230 +     *  the OpenGL context.  The default cache size is 16."    --ryan.
   1.231 +     */
   1.232 +
   1.233 +    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
   1.234 +    #define GLI_ARRAY_FUNC_CACHE_MAX 284
   1.235 +    #endif
   1.236 +
   1.237 +    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
   1.238 +    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
   1.239 +    #endif
   1.240 +
   1.241 +    {
   1.242 +        long cache_max = 64;
   1.243 +        CGLContextObj ctx = [nscontext CGLContextObj];
   1.244 +        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
   1.245 +        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
   1.246 +    }
   1.247 +
   1.248 +    /* End Wisdom from Apple Engineer section. --ryan. */
   1.249 +
   1.250 +    /* FIXME: should this go somewhere else? */
   1.251 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.252 +        [nscontext setFullScreen];
   1.253 +    }
   1.254 +
   1.255 +    [pool release];
   1.256 +    return nscontext;
   1.257 +}
   1.258 +
   1.259 +int
   1.260 +Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
   1.261 +{
   1.262 +    NSAutoreleasePool *pool;
   1.263 +
   1.264 +    pool = [[NSAutoreleasePool alloc] init];
   1.265 +
   1.266 +    if (context) {
   1.267 +        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   1.268 +        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   1.269 +
   1.270 +        [nscontext setView:[windowdata->window contentView]];
   1.271 +        [nscontext makeCurrentContext];
   1.272 +    } else {
   1.273 +        [NSOpenGLContext clearCurrentContext];
   1.274 +    }
   1.275 +
   1.276 +    [pool release];
   1.277 +    return 0;
   1.278 +}
   1.279 +
   1.280 +int
   1.281 +Cocoa_GL_SetSwapInterval(_THIS, int interval)
   1.282 +{
   1.283 +    NSAutoreleasePool *pool;
   1.284 +    NSOpenGLContext *nscontext;
   1.285 +    long value;
   1.286 +    int status;
   1.287 +
   1.288 +    pool = [[NSAutoreleasePool alloc] init];
   1.289 +
   1.290 +    nscontext = [NSOpenGLContext currentContext];
   1.291 +    if (nscontext != nil) {
   1.292 +        value = interval;
   1.293 +        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.294 +        status = 0;
   1.295 +    } else {
   1.296 +        SDL_SetError("No current OpenGL context");
   1.297 +        status = -1;
   1.298 +    }
   1.299 +
   1.300 +    [pool release];
   1.301 +    return status;
   1.302 +}
   1.303 +
   1.304 +int
   1.305 +Cocoa_GL_GetSwapInterval(_THIS)
   1.306 +{
   1.307 +    NSAutoreleasePool *pool;
   1.308 +    NSOpenGLContext *nscontext;
   1.309 +    long value;
   1.310 +    int status;
   1.311 +
   1.312 +    pool = [[NSAutoreleasePool alloc] init];
   1.313 +
   1.314 +    nscontext = [NSOpenGLContext currentContext];
   1.315 +    if (nscontext != nil) {
   1.316 +        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.317 +        status = (int)value;
   1.318 +    } else {
   1.319 +        SDL_SetError("No current OpenGL context");
   1.320 +        status = -1;
   1.321 +    }
   1.322 +
   1.323 +    [pool release];
   1.324 +    return status;
   1.325 +}
   1.326 +
   1.327 +void
   1.328 +Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
   1.329 +{
   1.330 +    NSAutoreleasePool *pool;
   1.331 +    NSOpenGLContext *nscontext;
   1.332 +
   1.333 +    pool = [[NSAutoreleasePool alloc] init];
   1.334 +
   1.335 +    /* FIXME: Do we need to get the context for the window? */
   1.336 +    nscontext = [NSOpenGLContext currentContext];
   1.337 +    if (nscontext != nil) {
   1.338 +        [nscontext flushBuffer];
   1.339 +    }
   1.340 +
   1.341 +    [pool release];
   1.342 +}
   1.343 +
   1.344 +void
   1.345 +Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
   1.346 +{
   1.347 +    NSAutoreleasePool *pool;
   1.348 +    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   1.349 +
   1.350 +    pool = [[NSAutoreleasePool alloc] init];
   1.351 +
   1.352 +    [nscontext clearDrawable];
   1.353 +    [nscontext release];
   1.354 +
   1.355 +    [pool release];
   1.356 +}
   1.357 +
   1.358 +#endif /* SDL_VIDEO_OPENGL */
   1.359 +
   1.360 +/* vi: set ts=4 sw=4 expandtab: */