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