src/video/x11/SDL_x11opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 18 Jul 2011 14:34:19 -0700
changeset 5572 32a785da3f49
parent 5535 96594ac5fd1a
child 5573 7dd4caf23bf5
permissions -rw-r--r--
Added support for GLX_EXT_swap_control, and cleaned up some other extensions.

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