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