src/video/x11/SDL_x11opengl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5981 75caa8a7d559
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@1952
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@1952
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1952
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1952
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1952
    20
*/
slouken@1952
    21
#include "SDL_config.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_X11
slouken@6044
    24
slouken@1952
    25
#include "SDL_x11video.h"
icculus@5981
    26
#include "SDL_assert.h"
slouken@1952
    27
slouken@1952
    28
/* GLX implementation of SDL OpenGL support */
slouken@1952
    29
slouken@1952
    30
#if SDL_VIDEO_OPENGL_GLX
slouken@1952
    31
#include "SDL_loadso.h"
slouken@1952
    32
slouken@1952
    33
#if defined(__IRIX__)
slouken@1952
    34
/* IRIX doesn't have a GL library versioning system */
slouken@1952
    35
#define DEFAULT_OPENGL	"libGL.so"
slouken@1952
    36
#elif defined(__MACOSX__)
slouken@1952
    37
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@1952
    38
#elif defined(__QNXNTO__)
slouken@1952
    39
#define DEFAULT_OPENGL	"libGL.so.3"
slouken@1952
    40
#else
slouken@1952
    41
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@1952
    42
#endif
slouken@1952
    43
slouken@3241
    44
#ifndef GLX_NONE_EXT
slouken@3241
    45
#define GLX_NONE_EXT                       0x8000
slouken@3241
    46
#endif
slouken@3241
    47
slouken@1952
    48
#ifndef GLX_ARB_multisample
slouken@1952
    49
#define GLX_ARB_multisample
slouken@1952
    50
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@1952
    51
#define GLX_SAMPLES_ARB                    100001
slouken@1952
    52
#endif
slouken@1952
    53
slouken@1952
    54
#ifndef GLX_EXT_visual_rating
slouken@1952
    55
#define GLX_EXT_visual_rating
slouken@1952
    56
#define GLX_VISUAL_CAVEAT_EXT              0x20
slouken@1952
    57
#define GLX_NONE_EXT                       0x8000
slouken@1952
    58
#define GLX_SLOW_VISUAL_EXT                0x8001
slouken@1952
    59
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1952
    60
#endif
slouken@1952
    61
slouken@3100
    62
#ifndef GLX_ARB_create_context
slouken@3100
    63
#define GLX_ARB_create_context
slouken@3100
    64
#define GLX_CONTEXT_MAJOR_VERSION_ARB      0x2091
slouken@3100
    65
#define GLX_CONTEXT_MINOR_VERSION_ARB      0x2092
slouken@3100
    66
#define GLX_CONTEXT_FLAGS_ARB              0x2094
slouken@3100
    67
#define GLX_CONTEXT_DEBUG_BIT_ARB          0x0001
slouken@3100
    68
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
slouken@5021
    69
icculus@5573
    70
#ifndef GLX_EXT_swap_control
icculus@5573
    71
#define GLX_SWAP_INTERVAL_EXT              0x20F1
icculus@5573
    72
#define GLX_MAX_SWAP_INTERVAL_EXT          0x20F2
icculus@5573
    73
#endif
icculus@5573
    74
slouken@5021
    75
/* Typedef for the GL 3.0 context creation function */
slouken@5021
    76
typedef GLXContext(*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display * dpy,
slouken@5021
    77
                                                        GLXFBConfig config,
slouken@5021
    78
                                                        GLXContext
slouken@5021
    79
                                                        share_context,
slouken@5021
    80
                                                        Bool direct,
slouken@5021
    81
                                                        const int
slouken@5021
    82
                                                        *attrib_list);
slouken@3100
    83
#endif
slouken@3100
    84
slouken@1952
    85
#define OPENGL_REQUIRS_DLOPEN
slouken@1952
    86
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@1952
    87
#include <dlfcn.h>
slouken@1952
    88
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@1952
    89
#define GL_LoadFunction		dlsym
slouken@1952
    90
#define GL_UnloadObject		dlclose
slouken@1952
    91
#else
slouken@1952
    92
#define GL_LoadObject	SDL_LoadObject
slouken@1952
    93
#define GL_LoadFunction	SDL_LoadFunction
slouken@1952
    94
#define GL_UnloadObject	SDL_UnloadObject
slouken@1952
    95
#endif
slouken@1952
    96
slouken@3057
    97
static void X11_GL_InitExtensions(_THIS);
bob@2322
    98
slouken@3100
    99
slouken@1952
   100
int
slouken@1952
   101
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@1952
   102
{
slouken@1952
   103
    void *handle;
slouken@1952
   104
slouken@3057
   105
    /* Load the OpenGL library */
slouken@3057
   106
    if (path == NULL) {
slouken@3057
   107
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
   108
    }
slouken@3057
   109
    if (path == NULL) {
slouken@3057
   110
        path = DEFAULT_OPENGL;
slouken@3057
   111
    }
slouken@3057
   112
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@3057
   113
    if (!_this->gl_config.dll_handle) {
slouken@3057
   114
        return -1;
slouken@3057
   115
    }
slouken@3057
   116
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3057
   117
                SDL_arraysize(_this->gl_config.driver_path));
slouken@2244
   118
slouken@3057
   119
    /* Allocate OpenGL memory */
slouken@3057
   120
    _this->gl_data =
slouken@3057
   121
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@3057
   122
                                               sizeof(struct
slouken@3057
   123
                                                      SDL_GLDriverData));
slouken@3057
   124
    if (!_this->gl_data) {
slouken@3057
   125
        SDL_OutOfMemory();
slouken@3057
   126
        return -1;
slouken@3057
   127
    }
slouken@3057
   128
slouken@3057
   129
    /* Load function pointers */
slouken@3057
   130
    handle = _this->gl_config.dll_handle;
slouken@1952
   131
    _this->gl_data->glXGetProcAddress =
slouken@4550
   132
        (void *(*)(const GLubyte *))
slouken@4550
   133
            GL_LoadFunction(handle, "glXGetProcAddressARB");
slouken@1952
   134
    _this->gl_data->glXChooseVisual =
slouken@4550
   135
        (XVisualInfo * (*)(Display *, int, int *))
slouken@4550
   136
            X11_GL_GetProcAddress(_this, "glXChooseVisual");
slouken@1952
   137
    _this->gl_data->glXCreateContext =
slouken@1952
   138
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@4550
   139
            X11_GL_GetProcAddress(_this, "glXCreateContext");
slouken@1952
   140
    _this->gl_data->glXDestroyContext =
slouken@4550
   141
        (void (*)(Display *, GLXContext))
slouken@4550
   142
            X11_GL_GetProcAddress(_this, "glXDestroyContext");
slouken@1952
   143
    _this->gl_data->glXMakeCurrent =
slouken@4550
   144
        (int (*)(Display *, GLXDrawable, GLXContext))
slouken@4550
   145
            X11_GL_GetProcAddress(_this, "glXMakeCurrent");
slouken@1952
   146
    _this->gl_data->glXSwapBuffers =
slouken@4550
   147
        (void (*)(Display *, GLXDrawable))
slouken@4550
   148
            X11_GL_GetProcAddress(_this, "glXSwapBuffers");
icculus@5572
   149
    _this->gl_data->glXQueryDrawable =
icculus@5572
   150
        (void (*)(Display*,GLXDrawable,int,unsigned int*))
icculus@5572
   151
            X11_GL_GetProcAddress(_this, "glXQueryDrawable");
slouken@1952
   152
slouken@1952
   153
    if (!_this->gl_data->glXChooseVisual ||
slouken@1952
   154
        !_this->gl_data->glXCreateContext ||
slouken@1952
   155
        !_this->gl_data->glXDestroyContext ||
slouken@4550
   156
        !_this->gl_data->glXMakeCurrent ||
slouken@4550
   157
        !_this->gl_data->glXSwapBuffers) {
slouken@1952
   158
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   159
        return -1;
slouken@1952
   160
    }
slouken@1952
   161
slouken@3057
   162
    /* Initialize extensions */
slouken@3057
   163
    X11_GL_InitExtensions(_this);
slouken@3057
   164
slouken@1952
   165
    return 0;
slouken@1952
   166
}
slouken@1952
   167
slouken@1952
   168
void *
slouken@1952
   169
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@1952
   170
{
slouken@1952
   171
    if (_this->gl_data->glXGetProcAddress) {
slouken@1952
   172
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1952
   173
    }
slouken@4550
   174
    return GL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1952
   175
}
slouken@1952
   176
slouken@3057
   177
void
slouken@1952
   178
X11_GL_UnloadLibrary(_THIS)
slouken@1952
   179
{
slouken@3057
   180
    /* Don't actually unload the library, since it may have registered
slouken@3057
   181
     * X11 shutdown hooks, per the notes at:
slouken@3057
   182
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@3057
   183
     */
slouken@3057
   184
#if 0
slouken@3057
   185
    GL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
   186
    _this->gl_config.dll_handle = NULL;
slouken@3057
   187
#endif
slouken@3057
   188
slouken@3057
   189
    /* Free OpenGL memory */
slouken@3057
   190
    SDL_free(_this->gl_data);
slouken@3057
   191
    _this->gl_data = NULL;
slouken@1952
   192
}
slouken@1952
   193
slouken@1952
   194
static SDL_bool
slouken@1952
   195
HasExtension(const char *extension, const char *extensions)
slouken@1952
   196
{
slouken@1952
   197
    const char *start;
slouken@1952
   198
    const char *where, *terminator;
slouken@1952
   199
slouken@1952
   200
    /* Extension names should not have spaces. */
slouken@1952
   201
    where = SDL_strchr(extension, ' ');
slouken@1952
   202
    if (where || *extension == '\0')
slouken@1952
   203
        return SDL_FALSE;
slouken@1952
   204
slouken@1952
   205
    if (!extensions)
slouken@1952
   206
        return SDL_FALSE;
slouken@1952
   207
slouken@1952
   208
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1952
   209
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1952
   210
     * etc. */
slouken@1952
   211
slouken@1952
   212
    start = extensions;
slouken@1952
   213
slouken@1952
   214
    for (;;) {
slouken@1952
   215
        where = SDL_strstr(start, extension);
slouken@1952
   216
        if (!where)
slouken@1952
   217
            break;
slouken@1952
   218
slouken@1952
   219
        terminator = where + SDL_strlen(extension);
slouken@1952
   220
        if (where == start || *(where - 1) == ' ')
slouken@1952
   221
            if (*terminator == ' ' || *terminator == '\0')
slouken@1952
   222
                return SDL_TRUE;
slouken@1952
   223
slouken@1952
   224
        start = terminator;
slouken@1952
   225
    }
slouken@1952
   226
    return SDL_FALSE;
slouken@1952
   227
}
slouken@1952
   228
slouken@1952
   229
static void
slouken@1952
   230
X11_GL_InitExtensions(_THIS)
slouken@1952
   231
{
slouken@1952
   232
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@5244
   233
    int screen = DefaultScreen(display);
slouken@1952
   234
    XVisualInfo *vinfo;
slouken@1952
   235
    XSetWindowAttributes xattr;
slouken@1952
   236
    Window w;
slouken@1952
   237
    GLXContext context;
slouken@1952
   238
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
slouken@1952
   239
    const char *extensions;
slouken@1952
   240
slouken@1952
   241
    vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   242
    if (!vinfo) {
slouken@1952
   243
        return;
slouken@1952
   244
    }
slouken@1952
   245
    xattr.background_pixel = 0;
slouken@1952
   246
    xattr.border_pixel = 0;
slouken@1952
   247
    xattr.colormap =
slouken@1952
   248
        XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
slouken@1952
   249
                        AllocNone);
slouken@1952
   250
    w = XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
slouken@1952
   251
                      vinfo->depth, InputOutput, vinfo->visual,
slouken@1952
   252
                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
slouken@1952
   253
    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   254
    if (context) {
slouken@1952
   255
        _this->gl_data->glXMakeCurrent(display, w, context);
slouken@1952
   256
    }
slouken@1952
   257
    XFree(vinfo);
slouken@1952
   258
slouken@1952
   259
    glXQueryExtensionsStringFunc =
slouken@1952
   260
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
slouken@1952
   261
                                                                "glXQueryExtensionsString");
slouken@1952
   262
    if (glXQueryExtensionsStringFunc) {
slouken@1952
   263
        extensions = glXQueryExtensionsStringFunc(display, screen);
slouken@1952
   264
    } else {
slouken@1952
   265
        extensions = NULL;
slouken@1952
   266
    }
slouken@1952
   267
icculus@5572
   268
    /* Check for GLX_EXT_swap_control */
icculus@5572
   269
    if (HasExtension("GLX_EXT_swap_control", extensions)) {
icculus@5572
   270
        _this->gl_data->glXSwapIntervalEXT =
icculus@5630
   271
            (int (*)(Display*,GLXDrawable,int))
icculus@5572
   272
                X11_GL_GetProcAddress(_this, "glXSwapIntervalEXT");
slouken@1952
   273
    }
slouken@1952
   274
slouken@1952
   275
    /* Check for GLX_MESA_swap_control */
slouken@1952
   276
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
slouken@1952
   277
        _this->gl_data->glXSwapIntervalMESA =
icculus@5572
   278
            (int(*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalMESA");
slouken@1952
   279
        _this->gl_data->glXGetSwapIntervalMESA =
icculus@5572
   280
            (int(*)(void)) X11_GL_GetProcAddress(_this,
slouken@1952
   281
                                                   "glXGetSwapIntervalMESA");
slouken@1952
   282
    }
slouken@1952
   283
icculus@5572
   284
    /* Check for GLX_SGI_swap_control */
icculus@5572
   285
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
icculus@5572
   286
        _this->gl_data->glXSwapIntervalSGI =
icculus@5572
   287
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
icculus@5572
   288
    }
icculus@5572
   289
slouken@1952
   290
    /* Check for GLX_EXT_visual_rating */
slouken@1952
   291
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
slouken@1952
   292
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
slouken@1952
   293
    }
slouken@1952
   294
slouken@1952
   295
    if (context) {
slouken@1952
   296
        _this->gl_data->glXMakeCurrent(display, None, NULL);
slouken@1952
   297
        _this->gl_data->glXDestroyContext(display, context);
slouken@1952
   298
    }
slouken@1952
   299
    XDestroyWindow(display, w);
bob@2323
   300
    X11_PumpEvents(_this);
slouken@1952
   301
}
slouken@1952
   302
slouken@5395
   303
int 
slouken@5395
   304
X11_GL_GetAttributes(_THIS, Display * display, int screen, int * attribs, int size)
slouken@1952
   305
{
slouken@5395
   306
    int i = 0;
slouken@1952
   307
slouken@5395
   308
    /* assert buffer is large enough to hold all SDL attributes. */ 
slouken@5395
   309
    /* assert(size >= 32);*/
slouken@3139
   310
slouken@1952
   311
    /* Setup our GLX attributes according to the gl_config. */
slouken@1952
   312
    attribs[i++] = GLX_RGBA;
slouken@1952
   313
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   314
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   315
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   316
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   317
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   318
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   319
slouken@1952
   320
    if (_this->gl_config.alpha_size) {
slouken@1952
   321
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   322
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   323
    }
slouken@1952
   324
slouken@1952
   325
    if (_this->gl_config.buffer_size) {
slouken@1952
   326
        attribs[i++] = GLX_BUFFER_SIZE;
slouken@1952
   327
        attribs[i++] = _this->gl_config.buffer_size;
slouken@1952
   328
    }
slouken@1952
   329
slouken@1952
   330
    if (_this->gl_config.double_buffer) {
slouken@1952
   331
        attribs[i++] = GLX_DOUBLEBUFFER;
slouken@1952
   332
    }
slouken@1952
   333
slouken@1952
   334
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   335
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   336
slouken@1952
   337
    if (_this->gl_config.stencil_size) {
slouken@1952
   338
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   339
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   340
    }
slouken@1952
   341
slouken@1952
   342
    if (_this->gl_config.accum_red_size) {
slouken@1952
   343
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   344
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   345
    }
slouken@1952
   346
slouken@1952
   347
    if (_this->gl_config.accum_green_size) {
slouken@1952
   348
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   349
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   350
    }
slouken@1952
   351
slouken@1952
   352
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   353
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   354
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   355
    }
slouken@1952
   356
slouken@1952
   357
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   358
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   359
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   360
    }
slouken@1952
   361
slouken@1952
   362
    if (_this->gl_config.stereo) {
slouken@1952
   363
        attribs[i++] = GLX_STEREO;
slouken@1952
   364
    }
slouken@1952
   365
slouken@1952
   366
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   367
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   368
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   369
    }
slouken@1952
   370
slouken@1952
   371
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   372
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   373
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   374
    }
slouken@1952
   375
slouken@3571
   376
    if (_this->gl_config.accelerated >= 0 &&
slouken@3571
   377
        _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   378
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@3570
   379
        attribs[i++] = _this->gl_config.accelerated ? GLX_NONE_EXT :
slouken@3570
   380
                                                      GLX_SLOW_VISUAL_EXT;
slouken@1952
   381
    }
slouken@3570
   382
slouken@1952
   383
    attribs[i++] = None;
slouken@5395
   384
 
slouken@5395
   385
    return i;
slouken@5395
   386
}
slouken@5395
   387
slouken@5395
   388
XVisualInfo *
slouken@5395
   389
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@5395
   390
{
slouken@5395
   391
    XVisualInfo *vinfo;
slouken@5395
   392
slouken@5395
   393
    /* 64 seems nice. */
icculus@5981
   394
    const int max_attrs = 64;
icculus@5981
   395
    int attribs[max_attrs];
icculus@5981
   396
    const int i = X11_GL_GetAttributes(_this,display,screen,attribs,max_attrs);
icculus@5981
   397
    SDL_assert(i <= max_attrs);
slouken@1952
   398
slouken@1952
   399
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   400
    if (!vinfo) {
slouken@1952
   401
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   402
    }
slouken@1952
   403
    return vinfo;
slouken@1952
   404
}
slouken@1952
   405
slouken@1952
   406
SDL_GLContext
slouken@1952
   407
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   408
{
slouken@1952
   409
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   410
    Display *display = data->videodata->display;
slouken@1952
   411
    int screen =
slouken@5246
   412
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
slouken@1952
   413
    XWindowAttributes xattr;
slouken@1952
   414
    XVisualInfo v, *vinfo;
slouken@1952
   415
    int n;
slouken@1952
   416
    GLXContext context = NULL;
slouken@1952
   417
slouken@1955
   418
    /* We do this to create a clean separation between X and GLX errors. */
slouken@1952
   419
    XSync(display, False);
slouken@3685
   420
    XGetWindowAttributes(display, data->xwindow, &xattr);
slouken@1952
   421
    v.screen = screen;
slouken@1952
   422
    v.visualid = XVisualIDFromVisual(xattr.visual);
slouken@1952
   423
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   424
    if (vinfo) {
slouken@3139
   425
        if (_this->gl_config.major_version < 3) {
slouken@3100
   426
            context =
slouken@3100
   427
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@3100
   428
        } else {
slouken@3100
   429
            /* If we want a GL 3.0 context or later we need to get a temporary
slouken@3100
   430
               context to grab the new context creation function */
slouken@3139
   431
            GLXContext temp_context =
slouken@3139
   432
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@3100
   433
            if (!temp_context) {
slouken@3139
   434
                SDL_SetError("Could not create GL context");
slouken@3100
   435
                return NULL;
slouken@3100
   436
            } else {
slouken@3100
   437
                int attribs[] = {
slouken@3139
   438
                    GLX_CONTEXT_MAJOR_VERSION_ARB,
slouken@3139
   439
                    _this->gl_config.major_version,
slouken@3139
   440
                    GLX_CONTEXT_MINOR_VERSION_ARB,
slouken@3139
   441
                    _this->gl_config.minor_version,
slouken@3139
   442
                    0
slouken@3100
   443
                };
slouken@3139
   444
slouken@3100
   445
                /* Get a pointer to the context creation function for GL 3.0 */
slouken@3139
   446
                PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs =
slouken@3139
   447
                    (PFNGLXCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
slouken@3139
   448
                    glXGetProcAddress((GLubyte *)
slouken@3139
   449
                                      "glXCreateContextAttribsARB");
slouken@3100
   450
                if (!glXCreateContextAttribs) {
slouken@3100
   451
                    SDL_SetError("GL 3.x is not supported");
slouken@3100
   452
                    context = temp_context;
slouken@3100
   453
                } else {
slouken@5395
   454
                    int glxAttribs[64];
slouken@5395
   455
slouken@3105
   456
                    /* Create a GL 3.x context */
slouken@3100
   457
                    GLXFBConfig *framebuffer_config = NULL;
slouken@3139
   458
                    int fbcount = 0;
slouken@3139
   459
                    GLXFBConfig *(*glXChooseFBConfig) (Display * disp,
slouken@3139
   460
                                                       int screen,
slouken@3139
   461
                                                       const int *attrib_list,
slouken@3139
   462
                                                       int *nelements);
slouken@3105
   463
slouken@3139
   464
                    glXChooseFBConfig =
slouken@3139
   465
                        (GLXFBConfig *
slouken@3139
   466
                         (*)(Display *, int, const int *,
slouken@3139
   467
                             int *)) _this->gl_data->
slouken@3139
   468
                        glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
slouken@3105
   469
slouken@5395
   470
                    X11_GL_GetAttributes(_this,display,screen,glxAttribs,64);
slouken@5395
   471
slouken@3139
   472
                    if (!glXChooseFBConfig
slouken@3139
   473
                        || !(framebuffer_config =
slouken@3139
   474
                             glXChooseFBConfig(display,
slouken@5395
   475
                                               DefaultScreen(display), glxAttribs,
slouken@3139
   476
                                               &fbcount))) {
slouken@3139
   477
                        SDL_SetError
slouken@3139
   478
                            ("No good framebuffers found. GL 3.x disabled");
slouken@3100
   479
                        context = temp_context;
slouken@3139
   480
                    } else {
slouken@3139
   481
                        context =
slouken@3139
   482
                            glXCreateContextAttribs(display,
slouken@3139
   483
                                                    framebuffer_config[0],
slouken@3139
   484
                                                    NULL, True, attribs);
slouken@3139
   485
                        _this->gl_data->glXDestroyContext(display,
slouken@3139
   486
                                                          temp_context);
slouken@3100
   487
                    }
slouken@3100
   488
                }
slouken@3100
   489
            }
slouken@3100
   490
        }
slouken@1952
   491
        XFree(vinfo);
slouken@1952
   492
    }
slouken@1952
   493
    XSync(display, False);
slouken@1952
   494
slouken@1952
   495
    if (!context) {
slouken@1952
   496
        SDL_SetError("Could not create GL context");
slouken@2178
   497
        return NULL;
slouken@1952
   498
    }
slouken@2178
   499
slouken@2178
   500
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   501
        X11_GL_DeleteContext(_this, context);
slouken@2178
   502
        return NULL;
slouken@2178
   503
    }
slouken@2178
   504
slouken@2178
   505
    return context;
slouken@1952
   506
}
slouken@1952
   507
slouken@1952
   508
int
slouken@1952
   509
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   510
{
slouken@1952
   511
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   512
    Window drawable =
slouken@3685
   513
        (window ? ((SDL_WindowData *) window->driverdata)->xwindow : None);
slouken@1952
   514
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   515
    int status;
slouken@1952
   516
slouken@1952
   517
    status = 0;
slouken@1952
   518
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
slouken@1952
   519
        SDL_SetError("Unable to make GL context current");
slouken@1952
   520
        status = -1;
slouken@1952
   521
    }
slouken@1952
   522
    XSync(display, False);
slouken@1952
   523
slouken@1952
   524
    return (status);
slouken@1952
   525
}
slouken@1952
   526
bob@2327
   527
/* 
icculus@5572
   528
   0 is a valid argument to glxSwapInterval(MESA|EXT) and setting it to 0
icculus@5572
   529
   will undo the effect of a previous call with a value that is greater
icculus@5572
   530
   than zero (or at least that is what the docs say). OTOH, 0 is an invalid
icculus@5572
   531
   argument to glxSwapIntervalSGI and it returns an error if you call it
icculus@5572
   532
   with 0 as an argument.
bob@2327
   533
*/
bob@2327
   534
bob@2327
   535
static int swapinterval = -1;
slouken@1952
   536
int
slouken@1952
   537
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   538
{
slouken@1952
   539
    int status;
slouken@1952
   540
icculus@5572
   541
    if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   542
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   543
        const SDL_WindowData *windowdata = (SDL_WindowData *)
icculus@5572
   544
            _this->current_glwin->driverdata;
icculus@5572
   545
        Window drawable = windowdata->xwindow;
icculus@5630
   546
        status = _this->gl_data->glXSwapIntervalEXT(display,drawable,interval);
icculus@5630
   547
        if (status != 0) {
icculus@5630
   548
            SDL_SetError("glxSwapIntervalEXT failed");
icculus@5630
   549
            status = -1;
icculus@5630
   550
        } else {
icculus@5630
   551
            swapinterval = interval;
icculus@5630
   552
        }
icculus@5572
   553
    } else if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   554
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   555
        if (status != 0) {
slouken@1952
   556
            SDL_SetError("glxSwapIntervalMESA failed");
slouken@1952
   557
            status = -1;
bob@2327
   558
        } else {
bob@2327
   559
            swapinterval = interval;
slouken@1952
   560
        }
slouken@1952
   561
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   562
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   563
        if (status != 0) {
slouken@1952
   564
            SDL_SetError("glxSwapIntervalSGI failed");
slouken@1952
   565
            status = -1;
bob@2327
   566
        } else {
bob@2327
   567
            swapinterval = interval;
slouken@1952
   568
        }
slouken@1952
   569
    } else {
slouken@1952
   570
        SDL_Unsupported();
slouken@1952
   571
        status = -1;
slouken@1952
   572
    }
slouken@1952
   573
    return status;
slouken@1952
   574
}
slouken@1952
   575
slouken@1952
   576
int
slouken@1952
   577
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   578
{
icculus@5572
   579
    if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   580
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   581
        const SDL_WindowData *windowdata = (SDL_WindowData *)
icculus@5572
   582
            _this->current_glwin->driverdata;
icculus@5572
   583
        Window drawable = windowdata->xwindow;
icculus@5572
   584
        unsigned int value = 0;
icculus@5572
   585
        _this->gl_data->glXQueryDrawable(display, drawable,
icculus@5572
   586
                                         GLX_SWAP_INTERVAL_EXT, &value);
icculus@5572
   587
        return (int) value;
icculus@5572
   588
    } else if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   589
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   590
    } else {
bob@2327
   591
        return swapinterval;
slouken@1952
   592
    }
slouken@1952
   593
}
slouken@1952
   594
slouken@1952
   595
void
slouken@1952
   596
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   597
{
slouken@1952
   598
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   599
    Display *display = data->videodata->display;
slouken@1952
   600
slouken@3685
   601
    _this->gl_data->glXSwapBuffers(display, data->xwindow);
slouken@1952
   602
}
slouken@1952
   603
slouken@1952
   604
void
slouken@1952
   605
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   606
{
slouken@1952
   607
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   608
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   609
slouken@1952
   610
    _this->gl_data->glXDestroyContext(display, glx_context);
bob@2324
   611
    XSync(display, False);
slouken@1952
   612
}
slouken@1952
   613
slouken@1952
   614
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   615
slouken@6044
   616
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@6044
   617
slouken@1952
   618
/* vi: set ts=4 sw=4 expandtab: */