comparison src/video/quartz/SDL_QuartzGL.m @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 96c2f89cc7e1
children
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
40 #define NSOpenGLPFASampleBuffers ((NSOpenGLPixelFormatAttribute) 55) 40 #define NSOpenGLPFASampleBuffers ((NSOpenGLPixelFormatAttribute) 55)
41 #define NSOpenGLPFASamples ((NSOpenGLPixelFormatAttribute) 56) 41 #define NSOpenGLPFASamples ((NSOpenGLPixelFormatAttribute) 56)
42 #endif 42 #endif
43 43
44 44
45 @implementation NSOpenGLContext (CGLContextAccess) 45 @ implementation NSOpenGLContext (CGLContextAccess)
46 - (CGLContextObj) cglContext; 46 - (CGLContextObj) cglContext;
47 { 47 {
48 return _contextAuxiliary; 48 return _contextAuxiliary;
49 } 49 }
50
50 @end 51 @end
51
52 /* OpenGL helper functions (used internally) */ 52 /* OpenGL helper functions (used internally) */
53 53 int
54 int QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags) { 54 QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags)
55 {
55 56
56 NSOpenGLPixelFormatAttribute attr[32]; 57 NSOpenGLPixelFormatAttribute attr[32];
57 NSOpenGLPixelFormat *fmt; 58 NSOpenGLPixelFormat *fmt;
58 int i = 0; 59 int i = 0;
59 int colorBits = bpp; 60 int colorBits = bpp;
60 61
61 /* if a GL library hasn't been loaded at this point, load the default. */ 62 /* if a GL library hasn't been loaded at this point, load the default. */
62 if (!this->gl_config.driver_loaded) { 63 if (!this->gl_config.driver_loaded) {
63 if (QZ_GL_LoadLibrary(this, NULL) == -1) 64 if (QZ_GL_LoadLibrary (this, NULL) == -1)
64 return 0; 65 return 0;
65 } 66 }
66 67
67 if ( flags & SDL_FULLSCREEN ) { 68 if (flags & SDL_FULLSCREEN) {
68 69
69 attr[i++] = NSOpenGLPFAFullScreen; 70 attr[i++] = NSOpenGLPFAFullScreen;
70 } 71 }
71 /* In windowed mode, the OpenGL pixel depth must match device pixel depth */ 72 /* In windowed mode, the OpenGL pixel depth must match device pixel depth */
72 else if ( colorBits != device_bpp ) { 73 else if (colorBits != device_bpp) {
73 74
74 colorBits = device_bpp; 75 colorBits = device_bpp;
75 } 76 }
76 77
77 attr[i++] = NSOpenGLPFAColorSize; 78 attr[i++] = NSOpenGLPFAColorSize;
78 attr[i++] = colorBits; 79 attr[i++] = colorBits;
79 80
80 attr[i++] = NSOpenGLPFADepthSize; 81 attr[i++] = NSOpenGLPFADepthSize;
81 attr[i++] = this->gl_config.depth_size; 82 attr[i++] = this->gl_config.depth_size;
82 83
83 if ( this->gl_config.double_buffer ) { 84 if (this->gl_config.double_buffer) {
84 attr[i++] = NSOpenGLPFADoubleBuffer; 85 attr[i++] = NSOpenGLPFADoubleBuffer;
85 } 86 }
86 87
87 if ( this->gl_config.stereo ) { 88 if (this->gl_config.stereo) {
88 attr[i++] = NSOpenGLPFAStereo; 89 attr[i++] = NSOpenGLPFAStereo;
89 } 90 }
90 91
91 if ( this->gl_config.stencil_size != 0 ) { 92 if (this->gl_config.stencil_size != 0) {
92 attr[i++] = NSOpenGLPFAStencilSize; 93 attr[i++] = NSOpenGLPFAStencilSize;
93 attr[i++] = this->gl_config.stencil_size; 94 attr[i++] = this->gl_config.stencil_size;
94 } 95 }
95 96
96 if ( (this->gl_config.accum_red_size + 97 if ((this->gl_config.accum_red_size +
97 this->gl_config.accum_green_size + 98 this->gl_config.accum_green_size +
98 this->gl_config.accum_blue_size + 99 this->gl_config.accum_blue_size +
99 this->gl_config.accum_alpha_size) > 0 ) { 100 this->gl_config.accum_alpha_size) > 0) {
100 attr[i++] = NSOpenGLPFAAccumSize; 101 attr[i++] = NSOpenGLPFAAccumSize;
101 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; 102 attr[i++] =
102 } 103 this->gl_config.accum_red_size +
103 104 this->gl_config.accum_green_size +
104 if ( this->gl_config.multisamplebuffers != 0 ) { 105 this->gl_config.accum_blue_size +
106 this->gl_config.accum_alpha_size;
107 }
108
109 if (this->gl_config.multisamplebuffers != 0) {
105 attr[i++] = NSOpenGLPFASampleBuffers; 110 attr[i++] = NSOpenGLPFASampleBuffers;
106 attr[i++] = this->gl_config.multisamplebuffers; 111 attr[i++] = this->gl_config.multisamplebuffers;
107 } 112 }
108 113
109 if ( this->gl_config.multisamplesamples != 0 ) { 114 if (this->gl_config.multisamplesamples != 0) {
110 attr[i++] = NSOpenGLPFASamples; 115 attr[i++] = NSOpenGLPFASamples;
111 attr[i++] = this->gl_config.multisamplesamples; 116 attr[i++] = this->gl_config.multisamplesamples;
112 attr[i++] = NSOpenGLPFANoRecovery; 117 attr[i++] = NSOpenGLPFANoRecovery;
113 } 118 }
114 119
115 if ( this->gl_config.accelerated > 0 ) { 120 if (this->gl_config.accelerated > 0) {
116 attr[i++] = NSOpenGLPFAAccelerated; 121 attr[i++] = NSOpenGLPFAAccelerated;
117 } 122 }
118 123
119 attr[i++] = NSOpenGLPFAScreenMask; 124 attr[i++] = NSOpenGLPFAScreenMask;
120 attr[i++] = CGDisplayIDToOpenGLDisplayMask (display_id); 125 attr[i++] = CGDisplayIDToOpenGLDisplayMask (display_id);
121 attr[i] = 0; 126 attr[i] = 0;
122 127
123 fmt = [ [ NSOpenGLPixelFormat alloc ] initWithAttributes:attr ]; 128 fmt =[[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
124 if (fmt == nil) { 129 if (fmt == nil) {
125 SDL_SetError ("Failed creating OpenGL pixel format"); 130 SDL_SetError ("Failed creating OpenGL pixel format");
126 return 0; 131 return 0;
127 } 132 }
128 133
129 gl_context = [ [ NSOpenGLContext alloc ] initWithFormat:fmt 134 gl_context =[[NSOpenGLContext alloc] initWithFormat: fmt shareContext:nil];
130 shareContext:nil]; 135
131 136 [fmt release];
132 [ fmt release ];
133 137
134 if (gl_context == nil) { 138 if (gl_context == nil) {
135 SDL_SetError ("Failed creating OpenGL context"); 139 SDL_SetError ("Failed creating OpenGL context");
136 return 0; 140 return 0;
137 } 141 }
139 /* Synchronize QZ_GL_SwapBuffers() to vertical retrace. 143 /* Synchronize QZ_GL_SwapBuffers() to vertical retrace.
140 * (Apple's documentation is not completely clear about what this setting 144 * (Apple's documentation is not completely clear about what this setting
141 * exactly does, IMHO - for a detailed explanation see 145 * exactly does, IMHO - for a detailed explanation see
142 * http://lists.apple.com/archives/mac-opengl/2006/Jan/msg00080.html ) 146 * http://lists.apple.com/archives/mac-opengl/2006/Jan/msg00080.html )
143 */ 147 */
144 if ( this->gl_config.swap_control >= 0 ) { 148 if (this->gl_config.swap_control >= 0) {
145 long value; 149 long value;
146 value = this->gl_config.swap_control; 150 value = this->gl_config.swap_control;
147 [ gl_context setValues: &value forParameter: NSOpenGLCPSwapInterval ]; 151 [gl_context setValues: &value forParameter:NSOpenGLCPSwapInterval];
148 } 152 }
149 153
150 /* 154 /*
151 * Wisdom from Apple engineer in reference to UT2003's OpenGL performance: 155 * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
152 * "You are blowing a couple of the internal OpenGL function caches. This 156 * "You are blowing a couple of the internal OpenGL function caches. This
153 * appears to be happening in the VAO case. You can tell OpenGL to up 157 * appears to be happening in the VAO case. You can tell OpenGL to up
154 * the cache size by issuing the following calls right after you create 158 * the cache size by issuing the following calls right after you create
155 * the OpenGL context. The default cache size is 16." --ryan. 159 * the OpenGL context. The default cache size is 16." --ryan.
156 */ 160 */
157 161
158 #ifndef GLI_ARRAY_FUNC_CACHE_MAX 162 #ifndef GLI_ARRAY_FUNC_CACHE_MAX
159 #define GLI_ARRAY_FUNC_CACHE_MAX 284 163 #define GLI_ARRAY_FUNC_CACHE_MAX 284
160 #endif 164 #endif
161 165
162 #ifndef GLI_SUBMIT_FUNC_CACHE_MAX 166 #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
163 #define GLI_SUBMIT_FUNC_CACHE_MAX 280 167 #define GLI_SUBMIT_FUNC_CACHE_MAX 280
164 #endif 168 #endif
165 169
166 { 170 {
167 long cache_max = 64; 171 long cache_max = 64;
168 CGLContextObj ctx = [ gl_context cglContext ]; 172 CGLContextObj ctx =[gl_context cglContext];
169 CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max); 173 CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
170 CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max); 174 CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
171 } 175 }
172 176
173 /* End Wisdom from Apple Engineer section. --ryan. */ 177 /* End Wisdom from Apple Engineer section. --ryan. */
174 178
175 return 1; 179 return 1;
176 } 180 }
177 181
178 void QZ_TearDownOpenGL (_THIS) { 182 void
179 183 QZ_TearDownOpenGL (_THIS)
180 [ NSOpenGLContext clearCurrentContext ]; 184 {
181 [ gl_context clearDrawable ]; 185
182 [ gl_context release ]; 186 [NSOpenGLContext clearCurrentContext];
187 [gl_context clearDrawable];
188 [gl_context release];
183 } 189 }
184 190
185 191
186 /* SDL OpenGL functions */ 192 /* SDL OpenGL functions */
187 static const char *DEFAULT_OPENGL_LIB_NAME = 193 static const char *DEFAULT_OPENGL_LIB_NAME =
188 "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"; 194 "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib";
189 195
190 int QZ_GL_LoadLibrary (_THIS, const char *location) { 196 int
191 if ( gl_context != NULL ) { 197 QZ_GL_LoadLibrary (_THIS, const char *location)
192 SDL_SetError("OpenGL context already created"); 198 {
199 if (gl_context != NULL) {
200 SDL_SetError ("OpenGL context already created");
193 return -1; 201 return -1;
194 } 202 }
195 203
196 if (opengl_library != NULL) 204 if (opengl_library != NULL)
197 SDL_UnloadObject(opengl_library); 205 SDL_UnloadObject (opengl_library);
198 206
199 if (location == NULL) 207 if (location == NULL)
200 location = DEFAULT_OPENGL_LIB_NAME; 208 location = DEFAULT_OPENGL_LIB_NAME;
201 209
202 opengl_library = SDL_LoadObject(location); 210 opengl_library = SDL_LoadObject (location);
203 if (opengl_library != NULL) { 211 if (opengl_library != NULL) {
204 this->gl_config.driver_loaded = 1; 212 this->gl_config.driver_loaded = 1;
205 return 0; 213 return 0;
206 } 214 }
207 215
208 this->gl_config.driver_loaded = 0; 216 this->gl_config.driver_loaded = 0;
209 return -1; 217 return -1;
210 } 218 }
211 219
212 void* QZ_GL_GetProcAddress (_THIS, const char *proc) { 220 void *
213 return SDL_LoadFunction(opengl_library, proc); 221 QZ_GL_GetProcAddress (_THIS, const char *proc)
214 } 222 {
215 223 return SDL_LoadFunction (opengl_library, proc);
216 int QZ_GL_GetAttribute (_THIS, SDL_GLattr attrib, int* value) { 224 }
225
226 int
227 QZ_GL_GetAttribute (_THIS, SDL_GLattr attrib, int *value)
228 {
217 229
218 GLenum attr = 0; 230 GLenum attr = 0;
219 231
220 QZ_GL_MakeCurrent (this); 232 QZ_GL_MakeCurrent (this);
221 233
222 switch (attrib) { 234 switch (attrib) {
223 case SDL_GL_RED_SIZE: attr = GL_RED_BITS; break; 235 case SDL_GL_RED_SIZE:
224 case SDL_GL_BLUE_SIZE: attr = GL_BLUE_BITS; break; 236 attr = GL_RED_BITS;
225 case SDL_GL_GREEN_SIZE: attr = GL_GREEN_BITS; break; 237 break;
226 case SDL_GL_ALPHA_SIZE: attr = GL_ALPHA_BITS; break; 238 case SDL_GL_BLUE_SIZE:
227 case SDL_GL_DOUBLEBUFFER: attr = GL_DOUBLEBUFFER; break; 239 attr = GL_BLUE_BITS;
228 case SDL_GL_DEPTH_SIZE: attr = GL_DEPTH_BITS; break; 240 break;
229 case SDL_GL_STENCIL_SIZE: attr = GL_STENCIL_BITS; break; 241 case SDL_GL_GREEN_SIZE:
230 case SDL_GL_ACCUM_RED_SIZE: attr = GL_ACCUM_RED_BITS; break; 242 attr = GL_GREEN_BITS;
231 case SDL_GL_ACCUM_GREEN_SIZE: attr = GL_ACCUM_GREEN_BITS; break; 243 break;
232 case SDL_GL_ACCUM_BLUE_SIZE: attr = GL_ACCUM_BLUE_BITS; break; 244 case SDL_GL_ALPHA_SIZE:
233 case SDL_GL_ACCUM_ALPHA_SIZE: attr = GL_ACCUM_ALPHA_BITS; break; 245 attr = GL_ALPHA_BITS;
234 case SDL_GL_STEREO: attr = GL_STEREO; break; 246 break;
235 case SDL_GL_MULTISAMPLEBUFFERS: attr = GL_SAMPLE_BUFFERS_ARB; break; 247 case SDL_GL_DOUBLEBUFFER:
236 case SDL_GL_MULTISAMPLESAMPLES: attr = GL_SAMPLES_ARB; break; 248 attr = GL_DOUBLEBUFFER;
237 case SDL_GL_BUFFER_SIZE: 249 break;
250 case SDL_GL_DEPTH_SIZE:
251 attr = GL_DEPTH_BITS;
252 break;
253 case SDL_GL_STENCIL_SIZE:
254 attr = GL_STENCIL_BITS;
255 break;
256 case SDL_GL_ACCUM_RED_SIZE:
257 attr = GL_ACCUM_RED_BITS;
258 break;
259 case SDL_GL_ACCUM_GREEN_SIZE:
260 attr = GL_ACCUM_GREEN_BITS;
261 break;
262 case SDL_GL_ACCUM_BLUE_SIZE:
263 attr = GL_ACCUM_BLUE_BITS;
264 break;
265 case SDL_GL_ACCUM_ALPHA_SIZE:
266 attr = GL_ACCUM_ALPHA_BITS;
267 break;
268 case SDL_GL_STEREO:
269 attr = GL_STEREO;
270 break;
271 case SDL_GL_MULTISAMPLEBUFFERS:
272 attr = GL_SAMPLE_BUFFERS_ARB;
273 break;
274 case SDL_GL_MULTISAMPLESAMPLES:
275 attr = GL_SAMPLES_ARB;
276 break;
277 case SDL_GL_BUFFER_SIZE:
238 { 278 {
239 GLint bits = 0; 279 GLint bits = 0;
240 GLint component; 280 GLint component;
241 281
242 /* there doesn't seem to be a single flag in OpenGL for this! */ 282 /* there doesn't seem to be a single flag in OpenGL for this! */
243 glGetIntegerv (GL_RED_BITS, &component); bits += component; 283 glGetIntegerv (GL_RED_BITS, &component);
244 glGetIntegerv (GL_GREEN_BITS,&component); bits += component; 284 bits += component;
245 glGetIntegerv (GL_BLUE_BITS, &component); bits += component; 285 glGetIntegerv (GL_GREEN_BITS, &component);
246 glGetIntegerv (GL_ALPHA_BITS, &component); bits += component; 286 bits += component;
287 glGetIntegerv (GL_BLUE_BITS, &component);
288 bits += component;
289 glGetIntegerv (GL_ALPHA_BITS, &component);
290 bits += component;
247 291
248 *value = bits; 292 *value = bits;
249 return 0; 293 return 0;
250 } 294 }
251 case SDL_GL_ACCELERATED_VISUAL: 295 case SDL_GL_ACCELERATED_VISUAL:
252 { 296 {
253 long val; 297 long val;
254 /* FIXME: How do we get this information here? 298 /* FIXME: How do we get this information here?
255 [fmt getValues: &val forAttribute: NSOpenGLPFAAccelerated attr forVirtualScreen: 0]; 299 [fmt getValues: &val forAttribute: NSOpenGLPFAAccelerated attr forVirtualScreen: 0];
256 */ 300 */
257 val = (this->gl_config.accelerated != 0);; 301 val = (this->gl_config.accelerated != 0);;
258 *value = val; 302 *value = val;
259 return 0; 303 return 0;
260 } 304 }
261 case SDL_GL_SWAP_CONTROL: 305 case SDL_GL_SWAP_CONTROL:
262 { 306 {
263 long val; 307 long val;
264 [ gl_context getValues: &val forParameter: NSOpenGLCPSwapInterval ]; 308 [gl_context getValues: &val forParameter:NSOpenGLCPSwapInterval];
265 *value = val; 309 *value = val;
266 return 0; 310 return 0;
267 } 311 }
268 } 312 }
269 313
270 glGetIntegerv (attr, (GLint *)value); 314 glGetIntegerv (attr, (GLint *) value);
271 return 0; 315 return 0;
272 } 316 }
273 317
274 int QZ_GL_MakeCurrent (_THIS) { 318 int
275 [ gl_context makeCurrentContext ]; 319 QZ_GL_MakeCurrent (_THIS)
320 {
321 [gl_context makeCurrentContext];
276 return 0; 322 return 0;
277 } 323 }
278 324
279 void QZ_GL_SwapBuffers (_THIS) { 325 void
280 [ gl_context flushBuffer ]; 326 QZ_GL_SwapBuffers (_THIS)
281 } 327 {
328 [gl_context flushBuffer];
329 }