src/video/x11/SDL_x11gl.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 26 c447d5b9275d
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>	/* For getenv() prototype */
slouken@0
    29
#include <string.h>
slouken@0
    30
slouken@0
    31
#include "SDL_events_c.h"
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_x11video.h"
slouken@0
    34
#include "SDL_x11dga_c.h"
slouken@0
    35
#include "SDL_x11gl_c.h"
slouken@0
    36
slouken@0
    37
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@0
    38
slouken@0
    39
/* return the preferred visual to use for openGL graphics */
slouken@0
    40
XVisualInfo *X11_GL_GetVisual(_THIS)
slouken@0
    41
{
slouken@0
    42
#ifdef HAVE_OPENGL
slouken@0
    43
	/* 64 seems nice. */
slouken@0
    44
	int attribs[64];
slouken@0
    45
	int i;
slouken@0
    46
slouken@0
    47
	/* load the gl driver from a default path */
slouken@0
    48
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    49
	        /* no driver has been loaded, use default (ourselves) */
slouken@0
    50
	        if ( X11_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    51
		        return NULL;
slouken@0
    52
		}
slouken@0
    53
	}
slouken@0
    54
slouken@0
    55
	/* See if we already have a window which we must use */
slouken@0
    56
	if ( SDL_windowid ) {
slouken@0
    57
		XWindowAttributes a;
slouken@0
    58
		XVisualInfo vi_in;
slouken@0
    59
		int out_count;
slouken@0
    60
slouken@0
    61
		XGetWindowAttributes(SDL_Display, SDL_Window, &a);
slouken@0
    62
		vi_in.screen = SDL_Screen;
slouken@0
    63
		vi_in.visualid = XVisualIDFromVisual(a.visual);
slouken@0
    64
		glx_visualinfo = XGetVisualInfo(SDL_Display,
slouken@0
    65
	                     VisualScreenMask|VisualIDMask, &vi_in, &out_count);
slouken@0
    66
		return glx_visualinfo;
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
        /* Setup our GLX attributes according to the gl_config. */
slouken@0
    70
        i = 0;
slouken@0
    71
        attribs[i++] = GLX_RGBA;
slouken@0
    72
	attribs[i++] = GLX_RED_SIZE;
slouken@0
    73
	attribs[i++] = this->gl_config.red_size;
slouken@0
    74
	attribs[i++] = GLX_GREEN_SIZE;
slouken@0
    75
	attribs[i++] = this->gl_config.green_size;
slouken@0
    76
	attribs[i++] = GLX_BLUE_SIZE;
slouken@0
    77
	attribs[i++] = this->gl_config.blue_size;
slouken@0
    78
slouken@0
    79
	if( this->gl_config.alpha_size ) {
slouken@0
    80
		attribs[i++] = GLX_ALPHA_SIZE;
slouken@0
    81
		attribs[i++] = this->gl_config.alpha_size;
slouken@0
    82
	}
slouken@0
    83
slouken@0
    84
	if( this->gl_config.buffer_size ) {
slouken@0
    85
                attribs[i++] = GLX_BUFFER_SIZE;
slouken@0
    86
	        attribs[i++] = this->gl_config.buffer_size;
slouken@0
    87
	}
slouken@0
    88
slouken@0
    89
	if( this->gl_config.double_buffer ) {
slouken@0
    90
		attribs[i++] = GLX_DOUBLEBUFFER;
slouken@0
    91
	}
slouken@0
    92
slouken@0
    93
	attribs[i++] = GLX_DEPTH_SIZE;
slouken@0
    94
	attribs[i++] = this->gl_config.depth_size;
slouken@0
    95
slouken@0
    96
	if( this->gl_config.stencil_size ) {
slouken@0
    97
		attribs[i++] = GLX_STENCIL_SIZE;
slouken@0
    98
		attribs[i++] = this->gl_config.stencil_size;
slouken@0
    99
	}
slouken@0
   100
slouken@0
   101
	if( this->gl_config.accum_red_size ) {
slouken@0
   102
	        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@0
   103
		attribs[i++] = this->gl_config.accum_red_size;
slouken@0
   104
	}
slouken@0
   105
slouken@0
   106
	if( this->gl_config.accum_green_size ) {
slouken@0
   107
	        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@0
   108
		attribs[i++] = this->gl_config.accum_green_size;
slouken@0
   109
	}
slouken@0
   110
slouken@0
   111
	if( this->gl_config.accum_blue_size ) {
slouken@0
   112
	        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@0
   113
		attribs[i++] = this->gl_config.accum_blue_size;
slouken@0
   114
	}
slouken@0
   115
slouken@0
   116
	if( this->gl_config.accum_alpha_size ) {
slouken@0
   117
	        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   118
		attribs[i++] = this->gl_config.accum_alpha_size;
slouken@0
   119
	}
slouken@0
   120
slouken@0
   121
#ifdef GLX_DIRECT_COLOR /* Try for a DirectColor visual for gamma support */
slouken@0
   122
	attribs[i++] = GLX_X_VISUAL_TYPE;
slouken@0
   123
	attribs[i++] = GLX_DIRECT_COLOR;
slouken@0
   124
#endif
slouken@0
   125
	attribs[i++] = None;
slouken@0
   126
slouken@0
   127
 	glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   128
						  SDL_Screen, attribs);
slouken@0
   129
#ifdef GLX_DIRECT_COLOR
slouken@0
   130
	if( !glx_visualinfo ) { /* No DirectColor visual?  Try again.. */
slouken@0
   131
		attribs[i-3] = None;
slouken@0
   132
 		glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display, 
slouken@0
   133
						  SDL_Screen, attribs);
slouken@0
   134
	}
slouken@0
   135
#endif
slouken@0
   136
	if( !glx_visualinfo ) {
slouken@0
   137
		SDL_SetError( "Couldn't find matching GLX visual");
slouken@0
   138
		return NULL;
slouken@0
   139
	}
slouken@0
   140
	return glx_visualinfo;
slouken@0
   141
#else
slouken@0
   142
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   143
	return NULL;
slouken@0
   144
#endif
slouken@0
   145
}
slouken@0
   146
slouken@0
   147
int X11_GL_CreateWindow(_THIS, int w, int h)
slouken@0
   148
{
slouken@0
   149
	int retval;
slouken@0
   150
#ifdef HAVE_OPENGL
slouken@0
   151
	XSetWindowAttributes attributes;
slouken@0
   152
	unsigned long mask;
slouken@0
   153
	unsigned long black;
slouken@0
   154
slouken@0
   155
	black = (glx_visualinfo->visual == DefaultVisual(SDL_Display,
slouken@0
   156
						 	SDL_Screen))
slouken@0
   157
	       	? BlackPixel(SDL_Display, SDL_Screen) : 0;
slouken@0
   158
	attributes.background_pixel = black;
slouken@0
   159
	attributes.border_pixel = black;
slouken@0
   160
	attributes.colormap = SDL_XColorMap;
slouken@0
   161
	mask = CWBackPixel | CWBorderPixel | CWColormap;
slouken@0
   162
slouken@0
   163
	SDL_Window = XCreateWindow(SDL_Display, WMwindow,
slouken@0
   164
			0, 0, w, h, 0, glx_visualinfo->depth,
slouken@0
   165
			InputOutput, glx_visualinfo->visual,
slouken@0
   166
			mask, &attributes);
slouken@0
   167
	if ( !SDL_Window ) {
slouken@0
   168
		SDL_SetError("Could not create window");
slouken@0
   169
		return -1;
slouken@0
   170
	}
slouken@0
   171
	retval = 0;
slouken@0
   172
#else
slouken@0
   173
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   174
	retval = -1;
slouken@0
   175
#endif
slouken@0
   176
	return(retval);
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
int X11_GL_CreateContext(_THIS)
slouken@0
   180
{
slouken@0
   181
	int retval;
slouken@0
   182
#ifdef HAVE_OPENGL
slouken@0
   183
	/* We do this to create a clean separation between X and GLX errors. */
slouken@0
   184
	XSync( SDL_Display, False );
slouken@0
   185
	glx_context = this->gl_data->glXCreateContext(GFX_Display, 
slouken@0
   186
				     glx_visualinfo, NULL, True);
slouken@0
   187
	XSync( GFX_Display, False );
slouken@0
   188
slouken@0
   189
	if (glx_context == NULL) {
slouken@0
   190
		SDL_SetError("Could not create GL context");
slouken@0
   191
		return -1;
slouken@0
   192
	}
slouken@0
   193
slouken@0
   194
	gl_active = 1;
slouken@0
   195
#else
slouken@0
   196
	SDL_SetError("X11 driver not configured with OpenGL");
slouken@0
   197
#endif
slouken@0
   198
	if ( gl_active ) {
slouken@0
   199
		retval = 0;
slouken@0
   200
	} else {
slouken@0
   201
		retval = -1;
slouken@0
   202
	}
slouken@0
   203
	return(retval);
slouken@0
   204
}
slouken@0
   205
slouken@0
   206
void X11_GL_Shutdown(_THIS)
slouken@0
   207
{
slouken@0
   208
#ifdef HAVE_OPENGL
slouken@0
   209
	/* Clean up OpenGL */
slouken@0
   210
	if( glx_context ) {
slouken@0
   211
		this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
slouken@0
   212
slouken@0
   213
		if (glx_context != NULL)
slouken@0
   214
			this->gl_data->glXDestroyContext(GFX_Display, glx_context);
slouken@0
   215
slouken@0
   216
		if( this->gl_data->glXReleaseBuffersMESA ) {
slouken@0
   217
		    this->gl_data->glXReleaseBuffersMESA(GFX_Display,SDL_Window);
slouken@0
   218
		}
slouken@0
   219
		glx_context = NULL;
slouken@0
   220
	}
slouken@0
   221
	gl_active = 0;
slouken@0
   222
#endif /* HAVE_OPENGL */
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
#ifdef HAVE_OPENGL
slouken@0
   226
slouken@0
   227
/* Make the current context active */
slouken@0
   228
int X11_GL_MakeCurrent(_THIS)
slouken@0
   229
{
slouken@0
   230
	int retval;
slouken@0
   231
slouken@0
   232
	retval = 0;
slouken@0
   233
	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
slouken@0
   234
	                                     SDL_Window, glx_context) ) {
slouken@0
   235
		SDL_SetError("Unable to make GL context current");
slouken@0
   236
		retval = -1;
slouken@0
   237
	}
slouken@0
   238
	XSync( GFX_Display, False );
slouken@0
   239
slouken@0
   240
	/* More Voodoo X server workarounds... Grr... */
slouken@0
   241
	SDL_Lock_EventThread();
slouken@0
   242
	X11_CheckDGAMouse(this);
slouken@0
   243
	SDL_Unlock_EventThread();
slouken@0
   244
slouken@0
   245
	return(retval);
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
/* Get attribute data from glX. */
slouken@0
   249
int X11_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   250
{
slouken@0
   251
	int retval;
slouken@0
   252
	int glx_attrib = None;
slouken@0
   253
slouken@0
   254
	switch( attrib ) {
slouken@0
   255
	    case SDL_GL_RED_SIZE:
slouken@0
   256
		glx_attrib = GLX_RED_SIZE;
slouken@0
   257
		break;
slouken@0
   258
	    case SDL_GL_GREEN_SIZE:
slouken@0
   259
		glx_attrib = GLX_GREEN_SIZE;
slouken@0
   260
		break;
slouken@0
   261
	    case SDL_GL_BLUE_SIZE:
slouken@0
   262
		glx_attrib = GLX_BLUE_SIZE;
slouken@0
   263
		break;
slouken@0
   264
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   265
		glx_attrib = GLX_ALPHA_SIZE;
slouken@0
   266
		break;
slouken@0
   267
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   268
		glx_attrib = GLX_DOUBLEBUFFER;
slouken@0
   269
		break;
slouken@0
   270
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   271
		glx_attrib = GLX_BUFFER_SIZE;
slouken@0
   272
		break;
slouken@0
   273
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   274
		glx_attrib = GLX_DEPTH_SIZE;
slouken@0
   275
		break;
slouken@0
   276
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   277
		glx_attrib = GLX_STENCIL_SIZE;
slouken@0
   278
		break;
slouken@0
   279
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   280
		glx_attrib = GLX_ACCUM_RED_SIZE;
slouken@0
   281
		break;
slouken@0
   282
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   283
		glx_attrib = GLX_ACCUM_GREEN_SIZE;
slouken@0
   284
		break;
slouken@0
   285
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   286
		glx_attrib = GLX_ACCUM_BLUE_SIZE;
slouken@0
   287
		break;
slouken@0
   288
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   289
		glx_attrib = GLX_ACCUM_ALPHA_SIZE;
slouken@0
   290
		break;
slouken@0
   291
	    default:
slouken@0
   292
		return(-1);
slouken@0
   293
	}
slouken@0
   294
slouken@0
   295
	retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
slouken@0
   296
slouken@0
   297
	return retval;
slouken@0
   298
}
slouken@0
   299
slouken@0
   300
void X11_GL_SwapBuffers(_THIS)
slouken@0
   301
{
slouken@0
   302
	this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window);
slouken@0
   303
}
slouken@0
   304
slouken@0
   305
#endif /* HAVE_OPENGL */
slouken@0
   306
slouken@0
   307
void X11_GL_UnloadLibrary(_THIS)
slouken@0
   308
{
slouken@0
   309
#ifdef HAVE_OPENGL
slouken@0
   310
	if ( this->gl_config.driver_loaded ) {
slouken@0
   311
		dlclose(this->gl_config.dll_handle);
slouken@0
   312
slouken@0
   313
		this->gl_data->glXGetProcAddress = NULL;
slouken@0
   314
		this->gl_data->glXChooseVisual = NULL;
slouken@0
   315
		this->gl_data->glXCreateContext = NULL;
slouken@0
   316
		this->gl_data->glXDestroyContext = NULL;
slouken@0
   317
		this->gl_data->glXMakeCurrent = NULL;
slouken@0
   318
		this->gl_data->glXSwapBuffers = NULL;
slouken@0
   319
slouken@0
   320
		this->gl_config.dll_handle = NULL;
slouken@0
   321
		this->gl_config.driver_loaded = 0;
slouken@0
   322
	}
slouken@0
   323
#endif
slouken@0
   324
}
slouken@0
   325
slouken@0
   326
#ifdef HAVE_OPENGL
slouken@0
   327
slouken@0
   328
/* Passing a NULL path means load pointers from the application */
slouken@0
   329
int X11_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   330
{
slouken@0
   331
	void* handle;
slouken@0
   332
	int dlopen_flags;
slouken@0
   333
slouken@0
   334
 	if ( gl_active ) {
slouken@0
   335
 		SDL_SetError("OpenGL context already created");
slouken@0
   336
 		return -1;
slouken@0
   337
 	}
slouken@0
   338
slouken@0
   339
#ifdef RTLD_GLOBAL
slouken@0
   340
	dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
slouken@0
   341
#else
slouken@0
   342
	dlopen_flags = RTLD_LAZY;
slouken@0
   343
#endif
slouken@0
   344
	handle = dlopen(path, dlopen_flags);
slouken@0
   345
	/* Catch the case where the application isn't linked with GL */
slouken@0
   346
	if ( (dlsym(handle, "glXChooseVisual") == NULL) && (path == NULL) ) {
slouken@0
   347
		dlclose(handle);
slouken@0
   348
		path = getenv("SDL_VIDEO_GL_DRIVER");
slouken@0
   349
		if ( path == NULL ) {
slouken@0
   350
			path = DEFAULT_OPENGL;
slouken@0
   351
		}
slouken@0
   352
		handle = dlopen(path, dlopen_flags);
slouken@0
   353
	}
slouken@0
   354
	if ( handle == NULL ) {
slouken@0
   355
		SDL_SetError("Could not load OpenGL library");
slouken@0
   356
		return -1;
slouken@0
   357
	}
slouken@0
   358
slouken@0
   359
	/* Unload the old driver and reset the pointers */
slouken@0
   360
	X11_GL_UnloadLibrary(this);
slouken@0
   361
slouken@0
   362
	/* Load new function pointers */
slouken@0
   363
	this->gl_data->glXGetProcAddress = dlsym(handle, "glXGetProcAddressARB");
slouken@0
   364
	this->gl_data->glXChooseVisual = dlsym(handle, "glXChooseVisual");
slouken@0
   365
	this->gl_data->glXCreateContext = dlsym(handle, "glXCreateContext");
slouken@0
   366
	this->gl_data->glXDestroyContext = dlsym(handle, "glXDestroyContext");
slouken@0
   367
	this->gl_data->glXMakeCurrent = dlsym(handle, "glXMakeCurrent");
slouken@0
   368
	this->gl_data->glXSwapBuffers = dlsym(handle, "glXSwapBuffers");
slouken@0
   369
	this->gl_data->glXGetConfig = dlsym(handle, "glXGetConfig");
slouken@0
   370
	/* We don't compare below for this in case we're not using Mesa. */
slouken@0
   371
	this->gl_data->glXReleaseBuffersMESA = dlsym( handle, "glXReleaseBuffersMESA" );
slouken@0
   372
slouken@0
   373
	if ( (this->gl_data->glXChooseVisual == NULL) || 
slouken@0
   374
	     (this->gl_data->glXCreateContext == NULL) ||
slouken@0
   375
	     (this->gl_data->glXDestroyContext == NULL) ||
slouken@0
   376
	     (this->gl_data->glXMakeCurrent == NULL) ||
slouken@0
   377
	     (this->gl_data->glXSwapBuffers == NULL) ||
slouken@0
   378
	     (this->gl_data->glXGetConfig == NULL) ) {
slouken@0
   379
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   380
		return -1;
slouken@0
   381
	}
slouken@0
   382
slouken@0
   383
	this->gl_config.dll_handle = handle;
slouken@0
   384
	this->gl_config.driver_loaded = 1;
slouken@0
   385
	if ( path ) {
slouken@0
   386
		strncpy(this->gl_config.driver_path, path,
slouken@0
   387
			sizeof(this->gl_config.driver_path)-1);
slouken@0
   388
	} else {
slouken@0
   389
		strcpy(this->gl_config.driver_path, "");
slouken@0
   390
	}
slouken@0
   391
	return 0;
slouken@0
   392
}
slouken@0
   393
slouken@0
   394
void *X11_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   395
{
slouken@0
   396
	void* handle;
slouken@0
   397
	
slouken@0
   398
	handle = this->gl_config.dll_handle;
slouken@0
   399
#if 0 /* This doesn't work correctly yet */
slouken@0
   400
	if ( this->gl_data->glXGetProcAddress ) {
slouken@0
   401
        void *func, *func2;
slouken@0
   402
		func = this->gl_data->glXGetProcAddress(proc);
slouken@0
   403
        func2 = dlsym(handle, proc);
slouken@0
   404
        if ( func != func2 ) {
slouken@0
   405
fprintf(stderr, "glXGetProcAddress returned %p and dlsym returned %p for %s\n", func, func2, proc);
slouken@0
   406
        }
slouken@0
   407
        return this->gl_data->glXGetProcAddress(proc);
slouken@0
   408
	}
slouken@0
   409
#endif
slouken@0
   410
	return dlsym(handle, proc);
slouken@0
   411
}
slouken@0
   412
slouken@0
   413
#endif /* HAVE_OPENGL */