Debian Codegraph @

code/sdl/sdl_glimp.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #ifdef USE_INTERNAL_SDL_HEADERS
24 # include "SDL.h"
25 #else
26 # include <SDL.h>
27 #endif
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33
34 #include "../renderercommon/tr_common.h"
35 #include "../sys/sys_local.h"
36 #include "sdl_icon.h"
37
38 typedef enum
39 {
40 RSERR_OK,
41
42 RSERR_INVALID_FULLSCREEN,
43 RSERR_INVALID_MODE,
44
45 RSERR_UNKNOWN
46 } rserr_t;
47
48 SDL_Window *SDL_window = NULL;
49 static SDL_GLContext SDL_glContext = NULL;
50
51 cvar_t *r_allowSoftwareGL; // Don't abort out if a hardware visual can't be obtained
52 cvar_t *r_allowResize; // make window resizable
53 cvar_t *r_centerWindow;
54 cvar_t *r_sdlDriver;
55 cvar_t *r_preferOpenGLES;
56
57 int qglMajorVersion, qglMinorVersion;
58 int qglesMajorVersion, qglesMinorVersion;
59
60 void (APIENTRYP qglActiveTextureARB) (GLenum texture);
61 void (APIENTRYP qglClientActiveTextureARB) (GLenum texture);
62 void (APIENTRYP qglMultiTexCoord2fARB) (GLenum target, GLfloat s, GLfloat t);
63
64 void (APIENTRYP qglLockArraysEXT) (GLint first, GLsizei count);
65 void (APIENTRYP qglUnlockArraysEXT) (void);
66
67 #define GLE(ret, name, ...) name##proc * qgl##name = NULL;
68 QGL_1_1_PROCS;
69 QGL_1_1_FIXED_FUNCTION_PROCS;
70 QGL_DESKTOP_1_1_PROCS;
71 QGL_DESKTOP_1_1_FIXED_FUNCTION_PROCS;
72 QGL_ES_1_1_PROCS;
73 QGL_ES_1_1_FIXED_FUNCTION_PROCS;
74 QGL_1_3_PROCS;
75 QGL_1_5_PROCS;
76 QGL_2_0_PROCS;
77 QGL_3_0_PROCS;
78 QGL_ARB_occlusion_query_PROCS;
79 QGL_ARB_framebuffer_object_PROCS;
80 QGL_ARB_vertex_array_object_PROCS;
81 QGL_EXT_direct_state_access_PROCS;
82 #undef GLE
83
84 /*
85 ===============
86 GLimp_Shutdown
87 ===============
88 */
89 void GLimp_Shutdown( void )
90 {
91 ri.IN_Shutdown();
92
93 SDL_QuitSubSystem( SDL_INIT_VIDEO );
94 }
95
96 /*
97 ===============
98 GLimp_Minimize
99
100 Minimize the game so that user is back at the desktop
101 ===============
102 */
103 void GLimp_Minimize( void )
104 {
105 SDL_MinimizeWindow( SDL_window );
106 }
107
108
109 /*
110 ===============
111 GLimp_LogComment
112 ===============
113 */
114 void GLimp_LogComment( char *comment )
115 {
116 }
117
118 /*
119 ===============
120 GLimp_CompareModes
121 ===============
122 */
123 static int GLimp_CompareModes( const void *a, const void *b )
124 {
125 const float ASPECT_EPSILON = 0.001f;
126 SDL_Rect *modeA = (SDL_Rect *)a;
127 SDL_Rect *modeB = (SDL_Rect *)b;
128 float aspectA = (float)modeA->w / (float)modeA->h;
129 float aspectB = (float)modeB->w / (float)modeB->h;
130 int areaA = modeA->w * modeA->h;
131 int areaB = modeB->w * modeB->h;
132 float aspectDiffA = fabs( aspectA - displayAspect );
133 float aspectDiffB = fabs( aspectB - displayAspect );
134 float aspectDiffsDiff = aspectDiffA - aspectDiffB;
135
136 if( aspectDiffsDiff > ASPECT_EPSILON )
137 return 1;
138 else if( aspectDiffsDiff < -ASPECT_EPSILON )
139 return -1;
140 else
141 return areaA - areaB;
142 }
143
144
145 /*
146 ===============
147 GLimp_DetectAvailableModes
148 ===============
149 */
150 static void GLimp_DetectAvailableModes(void)
151 {
152 int i, j;
153 char buf[ MAX_STRING_CHARS ] = { 0 };
154 int numSDLModes;
155 SDL_Rect *modes;
156 int numModes = 0;
157
158 SDL_DisplayMode windowMode;
159 int display = SDL_GetWindowDisplayIndex( SDL_window );
160 if( display < 0 )
161 {
162 ri.Printf( PRINT_WARNING, "Couldn't get window display index, no resolutions detected: %s\n", SDL_GetError() );
163 return;
164 }
165 numSDLModes = SDL_GetNumDisplayModes( display );
166
167 if( SDL_GetWindowDisplayMode( SDL_window, &windowMode ) < 0 || numSDLModes <= 0 )
168 {
169 ri.Printf( PRINT_WARNING, "Couldn't get window display mode, no resolutions detected: %s\n", SDL_GetError() );
170 return;
171 }
172
173 modes = SDL_calloc( (size_t)numSDLModes, sizeof( SDL_Rect ) );
174 if ( !modes )
175 {
176 ri.Error( ERR_FATAL, "Out of memory" );
177 }
178
179 for( i = 0; i < numSDLModes; i++ )
180 {
181 SDL_DisplayMode mode;
182
183 if( SDL_GetDisplayMode( display, i, &mode ) < 0 )
184 continue;
185
186 if( !mode.w || !mode.h )
187 {
188 ri.Printf( PRINT_ALL, "Display supports any resolution\n" );
189 SDL_free( modes );
190 return;
191 }
192
193 if( windowMode.format != mode.format )
194 continue;
195
196 // SDL can give the same resolution with different refresh rates.
197 // Only list resolution once.
198 for( j = 0; j < numModes; j++ )
199 {
200 if( mode.w == modes[ j ].w && mode.h == modes[ j ].h )
201 break;
202 }
203
204 if( j != numModes )
205 continue;
206
207 modes[ numModes ].w = mode.w;
208 modes[ numModes ].h = mode.h;
209 numModes++;
210 }
211
212 if( numModes > 1 )
213 qsort( modes, numModes, sizeof( SDL_Rect ), GLimp_CompareModes );
214
215 for( i = 0; i < numModes; i++ )
216 {
217 const char *newModeString = va( "%ux%u ", modes[ i ].w, modes[ i ].h );
218
219 if( strlen( newModeString ) < (int)sizeof( buf ) - strlen( buf ) )
220 Q_strcat( buf, sizeof( buf ), newModeString );
221 else
222 ri.Printf( PRINT_WARNING, "Skipping mode %ux%u, buffer too small\n", modes[ i ].w, modes[ i ].h );
223 }
224
225 if( *buf )
226 {
227 buf[ strlen( buf ) - 1 ] = 0;
228 ri.Printf( PRINT_ALL, "Available modes: '%s'\n", buf );
229 ri.Cvar_Set( "r_availableModes", buf );
230 }
231 SDL_free( modes );
232 }
233
234 /*
235 ===============
236 OpenGL ES compatibility
237 ===============
238 */
239 static void APIENTRY GLimp_GLES_ClearDepth( GLclampd depth ) {
240 qglClearDepthf( depth );
241 }
242
243 static void APIENTRY GLimp_GLES_DepthRange( GLclampd near_val, GLclampd far_val ) {
244 qglDepthRangef( near_val, far_val );
245 }
246
247 static void APIENTRY GLimp_GLES_DrawBuffer( GLenum mode ) {
248 // unsupported
249 }
250
251 static void APIENTRY GLimp_GLES_PolygonMode( GLenum face, GLenum mode ) {
252 // unsupported
253 }
254
255 /*
256 ===============
257 GLimp_GetProcAddresses
258
259 Get addresses for OpenGL functions.
260 ===============
261 */
262 static qboolean GLimp_GetProcAddresses( qboolean fixedFunction ) {
263 qboolean success = qtrue;
264 const char *version;
265
266 #ifdef __SDL_NOGETPROCADDR__
267 #define GLE( ret, name, ... ) qgl##name = gl#name;
268 #else
269 #define GLE( ret, name, ... ) qgl##name = (name##proc *) SDL_GL_GetProcAddress("gl" #name); \
270 if ( qgl##name == NULL ) { \
271 ri.Printf( PRINT_ALL, "ERROR: Missing OpenGL function %s\n", "gl" #name ); \
272 success = qfalse; \
273 }
274 #endif
275
276 // OpenGL 1.0 and OpenGL ES 1.0
277 GLE(const GLubyte *, GetString, GLenum name)
278
279 if ( !qglGetString ) {
280 Com_Error( ERR_FATAL, "glGetString is NULL" );
281 }
282
283 version = (const char *)qglGetString( GL_VERSION );
284
285 if ( !version ) {
286 Com_Error( ERR_FATAL, "GL_VERSION is NULL" );
287 }
288
289 if ( Q_stricmpn( "OpenGL ES", version, 9 ) == 0 ) {
290 char profile[6]; // ES, ES-CM, or ES-CL
291 sscanf( version, "OpenGL %5s %d.%d", profile, &qglesMajorVersion, &qglesMinorVersion );
292 // common lite profile (no floating point) is not supported
293 if ( Q_stricmp( profile, "ES-CL" ) == 0 ) {
294 qglesMajorVersion = 0;
295 qglesMinorVersion = 0;
296 }
297 } else {
298 sscanf( version, "%d.%d", &qglMajorVersion, &qglMinorVersion );
299 }
300
301 if ( fixedFunction ) {
302 if ( QGL_VERSION_ATLEAST( 1, 1 ) ) {
303 QGL_1_1_PROCS;
304 QGL_1_1_FIXED_FUNCTION_PROCS;
305 QGL_DESKTOP_1_1_PROCS;
306 QGL_DESKTOP_1_1_FIXED_FUNCTION_PROCS;
307 } else if ( qglesMajorVersion == 1 && qglesMinorVersion >= 1 ) {
308 // OpenGL ES 1.1 (2.0 is not backward compatible)
309 QGL_1_1_PROCS;
310 QGL_1_1_FIXED_FUNCTION_PROCS;
311 QGL_ES_1_1_PROCS;
312 QGL_ES_1_1_FIXED_FUNCTION_PROCS;
313 // error so this doesn't segfault due to NULL desktop GL functions being used
314 Com_Error( ERR_FATAL, "Unsupported OpenGL Version: %s", version );
315 } else {
316 Com_Error( ERR_FATAL, "Unsupported OpenGL Version (%s), OpenGL 1.1 is required", version );
317 }
318 } else {
319 if ( QGL_VERSION_ATLEAST( 2, 0 ) ) {
320 QGL_1_1_PROCS;
321 QGL_DESKTOP_1_1_PROCS;
322 QGL_1_3_PROCS;
323 QGL_1_5_PROCS;
324 QGL_2_0_PROCS;
325 } else if ( QGLES_VERSION_ATLEAST( 2, 0 ) ) {
326 QGL_1_1_PROCS;
327 QGL_ES_1_1_PROCS;
328 QGL_1_3_PROCS;
329 QGL_1_5_PROCS;
330 QGL_2_0_PROCS;
331
332 qglClearDepth = GLimp_GLES_ClearDepth;
333 qglDepthRange = GLimp_GLES_DepthRange;
334 qglDrawBuffer = GLimp_GLES_DrawBuffer;
335 qglPolygonMode = GLimp_GLES_PolygonMode;
336 } else {
337 Com_Error( ERR_FATAL, "Unsupported OpenGL Version (%s), OpenGL 2.0 is required", version );
338 }
339 }
340
341 if ( QGL_VERSION_ATLEAST( 3, 0 ) || QGLES_VERSION_ATLEAST( 3, 0 ) ) {
342 QGL_3_0_PROCS;
343 }
344
345 #undef GLE
346
347 return success;
348 }
349
350 /*
351 ===============
352 GLimp_ClearProcAddresses
353
354 Clear addresses for OpenGL functions.
355 ===============
356 */
357 static void GLimp_ClearProcAddresses( void ) {
358 #define GLE( ret, name, ... ) qgl##name = NULL;
359
360 qglMajorVersion = 0;
361 qglMinorVersion = 0;
362 qglesMajorVersion = 0;
363 qglesMinorVersion = 0;
364
365 QGL_1_1_PROCS;
366 QGL_1_1_FIXED_FUNCTION_PROCS;
367 QGL_DESKTOP_1_1_PROCS;
368 QGL_DESKTOP_1_1_FIXED_FUNCTION_PROCS;
369 QGL_ES_1_1_PROCS;
370 QGL_ES_1_1_FIXED_FUNCTION_PROCS;
371 QGL_1_3_PROCS;
372 QGL_1_5_PROCS;
373 QGL_2_0_PROCS;
374 QGL_3_0_PROCS;
375 QGL_ARB_occlusion_query_PROCS;
376 QGL_ARB_framebuffer_object_PROCS;
377 QGL_ARB_vertex_array_object_PROCS;
378 QGL_EXT_direct_state_access_PROCS;
379
380 qglActiveTextureARB = NULL;
381 qglClientActiveTextureARB = NULL;
382 qglMultiTexCoord2fARB = NULL;
383
384 qglLockArraysEXT = NULL;
385 qglUnlockArraysEXT = NULL;
386
387 #undef GLE
388 }
389
390 /*
391 ===============
392 GLimp_SetMode
393 ===============
394 */
395 static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder, qboolean fixedFunction)
396 {
397 struct GLimp_ContextType {
398 int profileMask;
399 int majorVersion;
400 int minorVersion;
401 } contexts[4];
402 int numContexts, type;
403 const char *glstring;
404 int perChannelColorBits;
405 int colorBits, depthBits, stencilBits;
406 int samples;
407 int i = 0;
408 SDL_Surface *icon = NULL;
409 Uint32 flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL;
410 SDL_DisplayMode desktopMode;
411 int display = 0;
412 int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
413
414 ri.Printf( PRINT_ALL, "Initializing OpenGL display\n");
415
416 if ( r_allowResize->integer )
417 flags |= SDL_WINDOW_RESIZABLE;
418
419 #ifdef USE_ICON
420 icon = SDL_CreateRGBSurfaceFrom(
421 (void *)CLIENT_WINDOW_ICON.pixel_data,
422 CLIENT_WINDOW_ICON.width,
423 CLIENT_WINDOW_ICON.height,
424 CLIENT_WINDOW_ICON.bytes_per_pixel * 8,
425 CLIENT_WINDOW_ICON.bytes_per_pixel * CLIENT_WINDOW_ICON.width,
426 #ifdef Q3_LITTLE_ENDIAN
427 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
428 #else
429 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
430 #endif
431 );
432 #endif
433
434 // If a window exists, note its display index
435 if( SDL_window != NULL )
436 {
437 display = SDL_GetWindowDisplayIndex( SDL_window );
438 if( display < 0 )
439 {
440 ri.Printf( PRINT_DEVELOPER, "SDL_GetWindowDisplayIndex() failed: %s\n", SDL_GetError() );
441 display = 0;
442 }
443 }
444
445 if( SDL_GetDesktopDisplayMode( display, &desktopMode ) == 0 )
446 {
447 displayAspect = (float)desktopMode.w / (float)desktopMode.h;
448
449 ri.Printf( PRINT_ALL, "Display aspect: %.3f\n", displayAspect );
450 }
451 else
452 {
453 Com_Memset( &desktopMode, 0, sizeof( SDL_DisplayMode ) );
454
455 ri.Printf( PRINT_ALL,
456 "Cannot determine display aspect, assuming 1.333\n" );
457 }
458
459 ri.Printf (PRINT_ALL, "...setting mode %d:", mode );
460
461 if (mode == -2)
462 {
463 // use desktop video resolution
464 if( desktopMode.h > 0 )
465 {
466 glConfig.vidWidth = desktopMode.w;
467 glConfig.vidHeight = desktopMode.h;
468 }
469 else
470 {
471 glConfig.vidWidth = 640;
472 glConfig.vidHeight = 480;
473 ri.Printf( PRINT_ALL,
474 "Cannot determine display resolution, assuming 640x480\n" );
475 }
476
477 glConfig.windowAspect = (float)glConfig.vidWidth / (float)glConfig.vidHeight;
478 }
479 else if ( !R_GetModeInfo( &glConfig.vidWidth, &glConfig.vidHeight, &glConfig.windowAspect, mode ) )
480 {
481 ri.Printf( PRINT_ALL, " invalid mode\n" );
482 return RSERR_INVALID_MODE;
483 }
484 ri.Printf( PRINT_ALL, " %d %d\n", glConfig.vidWidth, glConfig.vidHeight);
485
486 // Center window
487 if( r_centerWindow->integer && !fullscreen )
488 {
489 x = ( desktopMode.w / 2 ) - ( glConfig.vidWidth / 2 );
490 y = ( desktopMode.h / 2 ) - ( glConfig.vidHeight / 2 );
491 }
492
493 // Destroy existing state if it exists
494 if( SDL_glContext != NULL )
495 {
496 GLimp_ClearProcAddresses();
497 SDL_GL_DeleteContext( SDL_glContext );
498 SDL_glContext = NULL;
499 }
500
501 if( SDL_window != NULL )
502 {
503 SDL_GetWindowPosition( SDL_window, &x, &y );
504 ri.Printf( PRINT_DEVELOPER, "Existing window at %dx%d before being destroyed\n", x, y );
505 SDL_DestroyWindow( SDL_window );
506 SDL_window = NULL;
507 }
508
509 if( fullscreen )
510 {
511 flags |= SDL_WINDOW_FULLSCREEN;
512 glConfig.isFullscreen = qtrue;
513 }
514 else
515 {
516 if( noborder )
517 flags |= SDL_WINDOW_BORDERLESS;
518
519 glConfig.isFullscreen = qfalse;
520 }
521
522 colorBits = r_colorbits->value;
523 if ((!colorBits) || (colorBits >= 32))
524 colorBits = 24;
525
526 if (!r_depthbits->value)
527 depthBits = 24;
528 else
529 depthBits = r_depthbits->value;
530
531 stencilBits = r_stencilbits->value;
532 samples = r_ext_multisample->value;
533
534 numContexts = 0;
535
536 if ( !fixedFunction ) {
537 int profileMask;
538 qboolean preferOpenGLES;
539
540 SDL_GL_ResetAttributes();
541 SDL_GL_GetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, &profileMask );
542
543 preferOpenGLES = ( r_preferOpenGLES->integer == 1 ||
544 ( r_preferOpenGLES->integer == -1 && profileMask == SDL_GL_CONTEXT_PROFILE_ES ) );
545
546 if ( preferOpenGLES ) {
547 #ifdef __EMSCRIPTEN__
548 // WebGL 2.0 isn't fully backward compatible so you have to ask for it specifically
549 contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
550 contexts[numContexts].majorVersion = 3;
551 contexts[numContexts].minorVersion = 0;
552 numContexts++;
553 #endif
554
555 contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
556 contexts[numContexts].majorVersion = 2;
557 contexts[numContexts].minorVersion = 0;
558 numContexts++;
559 }
560
561 contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_CORE;
562 contexts[numContexts].majorVersion = 3;
563 contexts[numContexts].minorVersion = 2;
564 numContexts++;
565
566 contexts[numContexts].profileMask = 0;
567 contexts[numContexts].majorVersion = 2;
568 contexts[numContexts].minorVersion = 0;
569 numContexts++;
570
571 if ( !preferOpenGLES ) {
572 #ifdef __EMSCRIPTEN__
573 contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
574 contexts[numContexts].majorVersion = 3;
575 contexts[numContexts].minorVersion = 0;
576 numContexts++;
577 #endif
578
579 contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
580 contexts[numContexts].majorVersion = 2;
581 contexts[numContexts].minorVersion = 0;
582 numContexts++;
583 }
584 } else {
585 contexts[numContexts].profileMask = 0;
586 contexts[numContexts].majorVersion = 1;
587 contexts[numContexts].minorVersion = 1;
588 numContexts++;
589 }
590
591 for (i = 0; i < 16; i++)
592 {
593 int testColorBits, testDepthBits, testStencilBits;
594 int realColorBits[3];
595
596 // 0 - default
597 // 1 - minus colorBits
598 // 2 - minus depthBits
599 // 3 - minus stencil
600 if ((i % 4) == 0 && i)
601 {
602 // one pass, reduce
603 switch (i / 4)
604 {
605 case 2 :
606 if (colorBits == 24)
607 colorBits = 16;
608 break;
609 case 1 :
610 if (depthBits == 24)
611 depthBits = 16;
612 else if (depthBits == 16)
613 depthBits = 8;
614 case 3 :
615 if (stencilBits == 24)
616 stencilBits = 16;
617 else if (stencilBits == 16)
618 stencilBits = 8;
619 }
620 }
621
622 testColorBits = colorBits;
623 testDepthBits = depthBits;
624 testStencilBits = stencilBits;
625
626 if ((i % 4) == 3)
627 { // reduce colorBits
628 if (testColorBits == 24)
629 testColorBits = 16;
630 }
631
632 if ((i % 4) == 2)
633 { // reduce depthBits
634 if (testDepthBits == 24)
635 testDepthBits = 16;
636 else if (testDepthBits == 16)
637 testDepthBits = 8;
638 }
639
640 if ((i % 4) == 1)
641 { // reduce stencilBits
642 if (testStencilBits == 24)
643 testStencilBits = 16;
644 else if (testStencilBits == 16)
645 testStencilBits = 8;
646 else
647 testStencilBits = 0;
648 }
649
650 if (testColorBits == 24)
651 perChannelColorBits = 8;
652 else
653 perChannelColorBits = 4;
654
655 #ifdef __sgi /* Fix for SGIs grabbing too many bits of color */
656 if (perChannelColorBits == 4)
657 perChannelColorBits = 0; /* Use minimum size for 16-bit color */
658
659 /* Need alpha or else SGIs choose 36+ bit RGB mode */
660 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 1);
661 #endif
662
663 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, perChannelColorBits );
664 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, perChannelColorBits );
665 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, perChannelColorBits );
666 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, testDepthBits );
667 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, testStencilBits );
668
669 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, samples ? 1 : 0 );
670 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, samples );
671
672 if(r_stereoEnabled->integer)
673 {
674 glConfig.stereoEnabled = qtrue;
675 SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
676 }
677 else
678 {
679 glConfig.stereoEnabled = qfalse;
680 SDL_GL_SetAttribute(SDL_GL_STEREO, 0);
681 }
682
683 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
684
685 #if 0 // if multisampling is enabled on X11, this causes create window to fail.
686 // If not allowing software GL, demand accelerated
687 if( !r_allowSoftwareGL->integer )
688 SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
689 #endif
690
691 for ( type = 0; type < numContexts; type++ ) {
692 char contextName[32];
693
694 switch ( contexts[type].profileMask ) {
695 default:
696 case 0:
697 Com_sprintf( contextName, sizeof( contextName ), "OpenGL %d.%d",
698 contexts[type].majorVersion, contexts[type].minorVersion );
699 break;
700 case SDL_GL_CONTEXT_PROFILE_CORE:
701 Com_sprintf( contextName, sizeof( contextName ), "OpenGL %d.%d Core",
702 contexts[type].majorVersion, contexts[type].minorVersion );
703 break;
704 case SDL_GL_CONTEXT_PROFILE_ES:
705 Com_sprintf( contextName, sizeof( contextName ), "OpenGL ES %d.%d",
706 contexts[type].majorVersion, contexts[type].minorVersion );
707 break;
708 }
709
710 SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, contexts[type].profileMask );
711 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, contexts[type].majorVersion );
712 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, contexts[type].minorVersion );
713
714 if( ( SDL_window = SDL_CreateWindow( CLIENT_WINDOW_TITLE, x, y,
715 glConfig.vidWidth, glConfig.vidHeight, flags ) ) == NULL )
716 {
717 ri.Printf( PRINT_DEVELOPER, "SDL_CreateWindow failed: %s\n", SDL_GetError( ) );
718 break;
719 }
720
721 SDL_glContext = SDL_GL_CreateContext( SDL_window );
722 if ( !SDL_glContext )
723 {
724 SDL_DestroyWindow( SDL_window );
725 SDL_window = NULL;
726 ri.Printf( PRINT_ALL, "SDL_GL_CreateContext() for %s context failed: %s\n", contextName, SDL_GetError() );
727 continue;
728 }
729
730 if ( !GLimp_GetProcAddresses( fixedFunction ) )
731 {
732 ri.Printf( PRINT_ALL, "GLimp_GetProcAddresses() for %s context failed\n", contextName );
733 GLimp_ClearProcAddresses();
734 SDL_GL_DeleteContext( SDL_glContext );
735 SDL_glContext = NULL;
736 SDL_DestroyWindow( SDL_window );
737 SDL_window = NULL;
738 continue;
739 }
740
741 if ( contexts[type].profileMask == SDL_GL_CONTEXT_PROFILE_CORE ) {
742 const char *renderer;
743
744 renderer = (const char *)qglGetString( GL_RENDERER );
745
746 if ( !renderer || strstr( renderer, "Software Renderer" ) || strstr( renderer, "Software Rasterizer" ) )
747 {
748 ri.Printf( PRINT_ALL, "GL_RENDERER is %s, rejecting %s context\n", renderer, contextName );
749
750 GLimp_ClearProcAddresses();
751 SDL_GL_DeleteContext( SDL_glContext );
752 SDL_glContext = NULL;
753 SDL_DestroyWindow( SDL_window );
754 SDL_window = NULL;
755 continue;
756 }
757 }
758
759 break;
760 }
761
762 if ( !SDL_window ) {
763 continue;
764 }
765
766 if ( !SDL_glContext ) {
767 SDL_DestroyWindow( SDL_window );
768 SDL_window = NULL;
769 continue;
770 }
771
772 if( fullscreen )
773 {
774 SDL_DisplayMode desiredMode;
775
776 switch( testColorBits )
777 {
778 case 16: desiredMode.format = SDL_PIXELFORMAT_RGB565; break;
779 case 24: desiredMode.format = SDL_PIXELFORMAT_RGB24; break;
780 default: ri.Printf( PRINT_DEVELOPER, "testColorBits is %d, can't fullscreen\n", testColorBits ); continue;
781 }
782
783 desiredMode.w = glConfig.vidWidth;
784 desiredMode.h = glConfig.vidHeight;
785 desiredMode.refresh_rate = glConfig.displayFrequency = ri.Cvar_VariableIntegerValue( "r_displayRefresh" );
786 desiredMode.driverdata = NULL;
787
788 if( SDL_SetWindowDisplayMode( SDL_window, &desiredMode ) < 0 )
789 {
790 ri.Printf( PRINT_DEVELOPER, "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) );
791 continue;
792 }
793 }
794
795 SDL_SetWindowIcon( SDL_window, icon );
796
797 qglClearColor( 0, 0, 0, 1 );
798 qglClear( GL_COLOR_BUFFER_BIT );
799 SDL_GL_SwapWindow( SDL_window );
800
801 if( SDL_GL_SetSwapInterval( r_swapInterval->integer ) == -1 )
802 {
803 ri.Printf( PRINT_DEVELOPER, "SDL_GL_SetSwapInterval failed: %s\n", SDL_GetError( ) );
804 }
805
806 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &realColorBits[0] );
807 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &realColorBits[1] );
808 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &realColorBits[2] );
809 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &glConfig.depthBits );
810 SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &glConfig.stencilBits );
811
812 glConfig.colorBits = realColorBits[0] + realColorBits[1] + realColorBits[2];
813
814 ri.Printf( PRINT_ALL, "Using %d color bits, %d depth, %d stencil display.\n",
815 glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits );
816 break;
817 }
818
819 SDL_FreeSurface( icon );
820
821 if( !SDL_window )
822 {
823 ri.Printf( PRINT_ALL, "Couldn't get a visual\n" );
824 return RSERR_INVALID_MODE;
825 }
826
827 SDL_ShowWindow( SDL_window );
828
829 GLimp_DetectAvailableModes();
830
831 glstring = (char *) qglGetString (GL_RENDERER);
832 ri.Printf( PRINT_ALL, "GL_RENDERER: %s\n", glstring );
833
834 return RSERR_OK;
835 }
836
837 /*
838 ===============
839 GLimp_StartDriverAndSetMode
840 ===============
841 */
842 static qboolean GLimp_StartDriverAndSetMode(int mode, qboolean fullscreen, qboolean noborder, qboolean gl3Core)
843 {
844 rserr_t err;
845
846 if (!SDL_WasInit(SDL_INIT_VIDEO))
847 {
848 const char *driverName;
849
850 if (SDL_Init(SDL_INIT_VIDEO) != 0)
851 {
852 ri.Printf( PRINT_ALL, "SDL_Init( SDL_INIT_VIDEO ) FAILED (%s)\n", SDL_GetError());
853 return qfalse;
854 }
855
856 driverName = SDL_GetCurrentVideoDriver( );
857 ri.Printf( PRINT_ALL, "SDL using driver \"%s\"\n", driverName );
858 ri.Cvar_Set( "r_sdlDriver", driverName );
859 }
860
861 if (fullscreen && ri.Cvar_VariableIntegerValue( "in_nograb" ) )
862 {
863 ri.Printf( PRINT_ALL, "Fullscreen not allowed with in_nograb 1\n");
864 ri.Cvar_Set( "r_fullscreen", "0" );
865 r_fullscreen->modified = qfalse;
866 fullscreen = qfalse;
867 }
868
869 err = GLimp_SetMode(mode, fullscreen, noborder, gl3Core);
870
871 switch ( err )
872 {
873 case RSERR_INVALID_FULLSCREEN:
874 ri.Printf( PRINT_ALL, "...WARNING: fullscreen unavailable in this mode\n" );
875 return qfalse;
876 case RSERR_INVALID_MODE:
877 ri.Printf( PRINT_ALL, "...WARNING: could not set the given mode (%d)\n", mode );
878 return qfalse;
879 default:
880 break;
881 }
882
883 return qtrue;
884 }
885
886
887 /*
888 ===============
889 GLimp_InitExtensions
890 ===============
891 */
892 static void GLimp_InitExtensions( qboolean fixedFunction )
893 {
894 if ( !r_allowExtensions->integer )
895 {
896 ri.Printf( PRINT_ALL, "* IGNORING OPENGL EXTENSIONS *\n" );
897 return;
898 }
899
900 ri.Printf( PRINT_ALL, "Initializing OpenGL extensions\n" );
901
902 glConfig.textureCompression = TC_NONE;
903
904 // GL_EXT_texture_compression_s3tc
905 if ( ( QGLES_VERSION_ATLEAST( 2, 0 ) || SDL_GL_ExtensionSupported( "GL_ARB_texture_compression" ) ) &&
906 SDL_GL_ExtensionSupported( "GL_EXT_texture_compression_s3tc" ) )
907 {
908 if ( r_ext_compressed_textures->value )
909 {
910 glConfig.textureCompression = TC_S3TC_ARB;
911 ri.Printf( PRINT_ALL, "...using GL_EXT_texture_compression_s3tc\n" );
912 }
913 else
914 {
915 ri.Printf( PRINT_ALL, "...ignoring GL_EXT_texture_compression_s3tc\n" );
916 }
917 }
918 else
919 {
920 ri.Printf( PRINT_ALL, "...GL_EXT_texture_compression_s3tc not found\n" );
921 }
922
923 // GL_S3_s3tc ... legacy extension before GL_EXT_texture_compression_s3tc.
924 if (glConfig.textureCompression == TC_NONE)
925 {
926 if ( SDL_GL_ExtensionSupported( "GL_S3_s3tc" ) )
927 {
928 if ( r_ext_compressed_textures->value )
929 {
930 glConfig.textureCompression = TC_S3TC;
931 ri.Printf( PRINT_ALL, "...using GL_S3_s3tc\n" );
932 }
933 else
934 {
935 ri.Printf( PRINT_ALL, "...ignoring GL_S3_s3tc\n" );
936 }
937 }
938 else
939 {
940 ri.Printf( PRINT_ALL, "...GL_S3_s3tc not found\n" );
941 }
942 }
943
944 // OpenGL 1 fixed function pipeline
945 if ( fixedFunction )
946 {
947 // GL_EXT_texture_env_add
948 glConfig.textureEnvAddAvailable = qfalse;
949 if ( SDL_GL_ExtensionSupported( "GL_EXT_texture_env_add" ) )
950 {
951 if ( r_ext_texture_env_add->integer )
952 {
953 glConfig.textureEnvAddAvailable = qtrue;
954 ri.Printf( PRINT_ALL, "...using GL_EXT_texture_env_add\n" );
955 }
956 else
957 {
958 glConfig.textureEnvAddAvailable = qfalse;
959 ri.Printf( PRINT_ALL, "...ignoring GL_EXT_texture_env_add\n" );
960 }
961 }
962 else
963 {
964 ri.Printf( PRINT_ALL, "...GL_EXT_texture_env_add not found\n" );
965 }
966
967 // GL_ARB_multitexture
968 qglMultiTexCoord2fARB = NULL;
969 qglActiveTextureARB = NULL;
970 qglClientActiveTextureARB = NULL;
971 if ( SDL_GL_ExtensionSupported( "GL_ARB_multitexture" ) )
972 {
973 if ( r_ext_multitexture->value )
974 {
975 qglMultiTexCoord2fARB = SDL_GL_GetProcAddress( "glMultiTexCoord2fARB" );
976 qglActiveTextureARB = SDL_GL_GetProcAddress( "glActiveTextureARB" );
977 qglClientActiveTextureARB = SDL_GL_GetProcAddress( "glClientActiveTextureARB" );
978
979 if ( qglActiveTextureARB )
980 {
981 GLint glint = 0;
982 qglGetIntegerv( GL_MAX_TEXTURE_UNITS_ARB, &glint );
983 glConfig.numTextureUnits = (int) glint;
984 if ( glConfig.numTextureUnits > 1 )
985 {
986 ri.Printf( PRINT_ALL, "...using GL_ARB_multitexture\n" );
987 }
988 else
989 {
990 qglMultiTexCoord2fARB = NULL;
991 qglActiveTextureARB = NULL;
992 qglClientActiveTextureARB = NULL;
993 ri.Printf( PRINT_ALL, "...not using GL_ARB_multitexture, < 2 texture units\n" );
994 }
995 }
996 }
997 else
998 {
999 ri.Printf( PRINT_ALL, "...ignoring GL_ARB_multitexture\n" );
1000 }
1001 }
1002 else
1003 {
1004 ri.Printf( PRINT_ALL, "...GL_ARB_multitexture not found\n" );
1005 }
1006
1007 // GL_EXT_compiled_vertex_array
1008 if ( SDL_GL_ExtensionSupported( "GL_EXT_compiled_vertex_array" ) )
1009 {
1010 if ( r_ext_compiled_vertex_array->value )
1011 {
1012 ri.Printf( PRINT_ALL, "...using GL_EXT_compiled_vertex_array\n" );
1013 qglLockArraysEXT = ( void ( APIENTRY * )( GLint, GLint ) ) SDL_GL_GetProcAddress( "glLockArraysEXT" );
1014 qglUnlockArraysEXT = ( void ( APIENTRY * )( void ) ) SDL_GL_GetProcAddress( "glUnlockArraysEXT" );
1015 if (!qglLockArraysEXT || !qglUnlockArraysEXT)
1016 {
1017 ri.Error (ERR_FATAL, "bad getprocaddress");
1018 }
1019 }
1020 else
1021 {
1022 ri.Printf( PRINT_ALL, "...ignoring GL_EXT_compiled_vertex_array\n" );
1023 }
1024 }
1025 else
1026 {
1027 ri.Printf( PRINT_ALL, "...GL_EXT_compiled_vertex_array not found\n" );
1028 }
1029 }
1030
1031 textureFilterAnisotropic = qfalse;
1032 if ( SDL_GL_ExtensionSupported( "GL_EXT_texture_filter_anisotropic" ) )
1033 {
1034 if ( r_ext_texture_filter_anisotropic->integer ) {
1035 qglGetIntegerv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint *)&maxAnisotropy );
1036 if ( maxAnisotropy <= 0 ) {
1037 ri.Printf( PRINT_ALL, "...GL_EXT_texture_filter_anisotropic not properly supported!\n" );
1038 maxAnisotropy = 0;
1039 }
1040 else
1041 {
1042 ri.Printf( PRINT_ALL, "...using GL_EXT_texture_filter_anisotropic (max: %i)\n", maxAnisotropy );
1043 textureFilterAnisotropic = qtrue;
1044 }
1045 }
1046 else
1047 {
1048 ri.Printf( PRINT_ALL, "...ignoring GL_EXT_texture_filter_anisotropic\n" );
1049 }
1050 }
1051 else
1052 {
1053 ri.Printf( PRINT_ALL, "...GL_EXT_texture_filter_anisotropic not found\n" );
1054 }
1055
1056 haveClampToEdge = qfalse;
1057 if ( QGL_VERSION_ATLEAST( 1, 2 ) || QGLES_VERSION_ATLEAST( 1, 0 ) || SDL_GL_ExtensionSupported( "GL_SGIS_texture_edge_clamp" ) )
1058 {
1059 ri.Printf( PRINT_ALL, "...using GL_SGIS_texture_edge_clamp\n" );
1060 haveClampToEdge = qtrue;
1061 }
1062 else
1063 {
1064 ri.Printf( PRINT_ALL, "...GL_SGIS_texture_edge_clamp not found\n" );
1065 }
1066 }
1067
1068 #define R_MODE_FALLBACK 3 // 640 * 480
1069
1070 /*
1071 ===============
1072 GLimp_Init
1073
1074 This routine is responsible for initializing the OS specific portions
1075 of OpenGL
1076 ===============
1077 */
1078 void GLimp_Init( qboolean fixedFunction )
1079 {
1080 ri.Printf( PRINT_DEVELOPER, "Glimp_Init( )\n" );
1081
1082 r_allowSoftwareGL = ri.Cvar_Get( "r_allowSoftwareGL", "0", CVAR_LATCH );
1083 r_sdlDriver = ri.Cvar_Get( "r_sdlDriver", "", CVAR_ROM );
1084 r_allowResize = ri.Cvar_Get( "r_allowResize", "0", CVAR_ARCHIVE | CVAR_LATCH );
1085 r_centerWindow = ri.Cvar_Get( "r_centerWindow", "0", CVAR_ARCHIVE | CVAR_LATCH );
1086 r_preferOpenGLES = ri.Cvar_Get( "r_preferOpenGLES", "-1", CVAR_ARCHIVE | CVAR_LATCH );
1087
1088 if( ri.Cvar_VariableIntegerValue( "com_abnormalExit" ) )
1089 {
1090 ri.Cvar_Set( "r_mode", va( "%d", R_MODE_FALLBACK ) );
1091 ri.Cvar_Set( "r_fullscreen", "0" );
1092 ri.Cvar_Set( "r_centerWindow", "0" );
1093 ri.Cvar_Set( "com_abnormalExit", "0" );
1094 }
1095
1096 ri.Sys_GLimpInit( );
1097
1098 // Create the window and set up the context
1099 if(GLimp_StartDriverAndSetMode(r_mode->integer, r_fullscreen->integer, r_noborder->integer, fixedFunction))
1100 goto success;
1101
1102 // Try again, this time in a platform specific "safe mode"
1103 ri.Sys_GLimpSafeInit( );
1104
1105 if(GLimp_StartDriverAndSetMode(r_mode->integer, r_fullscreen->integer, qfalse, fixedFunction))
1106 goto success;
1107
1108 // Finally, try the default screen resolution
1109 if( r_mode->integer != R_MODE_FALLBACK )
1110 {
1111 ri.Printf( PRINT_ALL, "Setting r_mode %d failed, falling back on r_mode %d\n",
1112 r_mode->integer, R_MODE_FALLBACK );
1113
1114 if(GLimp_StartDriverAndSetMode(R_MODE_FALLBACK, qfalse, qfalse, fixedFunction))
1115 goto success;
1116 }
1117
1118 // Nothing worked, give up
1119 ri.Error( ERR_FATAL, "GLimp_Init() - could not load OpenGL subsystem" );
1120
1121 success:
1122 // These values force the UI to disable driver selection
1123 glConfig.driverType = GLDRV_ICD;
1124 glConfig.hardwareType = GLHW_GENERIC;
1125
1126 // Only using SDL_SetWindowBrightness to determine if hardware gamma is supported
1127 glConfig.deviceSupportsGamma = !r_ignorehwgamma->integer &&
1128 SDL_SetWindowBrightness( SDL_window, 1.0f ) >= 0;
1129
1130 // get our config strings
1131 Q_strncpyz( glConfig.vendor_string, (char *) qglGetString (GL_VENDOR), sizeof( glConfig.vendor_string ) );
1132 Q_strncpyz( glConfig.renderer_string, (char *) qglGetString (GL_RENDERER), sizeof( glConfig.renderer_string ) );
1133 if (*glConfig.renderer_string && glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] == '\n')
1134 glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] = 0;
1135 Q_strncpyz( glConfig.version_string, (char *) qglGetString (GL_VERSION), sizeof( glConfig.version_string ) );
1136
1137 // manually create extension list if using OpenGL 3
1138 if ( qglGetStringi )
1139 {
1140 int i, numExtensions, extensionLength, listLength;
1141 const char *extension;
1142
1143 qglGetIntegerv( GL_NUM_EXTENSIONS, &numExtensions );
1144 listLength = 0;
1145
1146 for ( i = 0; i < numExtensions; i++ )
1147 {
1148 extension = (char *) qglGetStringi( GL_EXTENSIONS, i );
1149 extensionLength = strlen( extension );
1150
1151 if ( ( listLength + extensionLength + 1 ) >= sizeof( glConfig.extensions_string ) )
1152 break;
1153
1154 if ( i > 0 ) {
1155 Q_strcat( glConfig.extensions_string, sizeof( glConfig.extensions_string ), " " );
1156 listLength++;
1157 }
1158
1159 Q_strcat( glConfig.extensions_string, sizeof( glConfig.extensions_string ), extension );
1160 listLength += extensionLength;
1161 }
1162 }
1163 else
1164 {
1165 Q_strncpyz( glConfig.extensions_string, (char *) qglGetString (GL_EXTENSIONS), sizeof( glConfig.extensions_string ) );
1166 }
1167
1168 // initialize extensions
1169 GLimp_InitExtensions( fixedFunction );
1170
1171 ri.Cvar_Get( "r_availableModes", "", CVAR_ROM );
1172
1173 // This depends on SDL_INIT_VIDEO, hence having it here
1174 ri.IN_Init( SDL_window );
1175 }
1176
1177
1178 /*
1179 ===============
1180 GLimp_EndFrame
1181
1182 Responsible for doing a swapbuffers
1183 ===============
1184 */
1185 void GLimp_EndFrame( void )
1186 {
1187 // don't flip if drawing to front buffer
1188 if ( Q_stricmp( r_drawBuffer->string, "GL_FRONT" ) != 0 )
1189 {
1190 SDL_GL_SwapWindow( SDL_window );
1191 }
1192
1193 if( r_fullscreen->modified )
1194 {
1195 int fullscreen;
1196 qboolean needToToggle;
1197 qboolean sdlToggled = qfalse;
1198
1199 // Find out the current state
1200 fullscreen = !!( SDL_GetWindowFlags( SDL_window ) & SDL_WINDOW_FULLSCREEN );
1201
1202 if( r_fullscreen->integer && ri.Cvar_VariableIntegerValue( "in_nograb" ) )
1203 {
1204 ri.Printf( PRINT_ALL, "Fullscreen not allowed with in_nograb 1\n");
1205 ri.Cvar_Set( "r_fullscreen", "0" );
1206 r_fullscreen->modified = qfalse;
1207 }
1208
1209 // Is the state we want different from the current state?
1210 needToToggle = !!r_fullscreen->integer != fullscreen;
1211
1212 if( needToToggle )
1213 {
1214 sdlToggled = SDL_SetWindowFullscreen( SDL_window, r_fullscreen->integer ) >= 0;
1215
1216 // SDL_WM_ToggleFullScreen didn't work, so do it the slow way
1217 if( !sdlToggled )
1218 ri.Cmd_ExecuteText(EXEC_APPEND, "vid_restart\n");
1219
1220 ri.IN_Restart( );
1221 }
1222
1223 r_fullscreen->modified = qfalse;
1224 }
1225 }