src/video/x11/SDL_x11opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 01 Aug 2012 20:29:36 -0400
changeset 6382 64d54101773a
parent 6375 93f9a24d1c02
child 6393 a773384edf20
permissions -rwxr-xr-x
Add support for (GLX|WGL)_EXT_swap_control_tear.

This required a small public API change: SDL_GL_SetSwapInterval() now accepts
negative values, and SDL_GL_GetSwapInterval() doesn't report errors anymore
(if it can't work, it'll return zero as a reasonable default).

If you need to test for errors, such as a lack of swap_control_tear support,
check the results of SDL_GL_SetSwapInterval() when you set your desired
value.
slouken@1952
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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@6370
    32
#include "SDL_x11opengles.h"
slouken@1952
    33
slouken@1952
    34
#if defined(__IRIX__)
slouken@1952
    35
/* IRIX doesn't have a GL library versioning system */
slouken@1952
    36
#define DEFAULT_OPENGL	"libGL.so"
slouken@1952
    37
#elif defined(__MACOSX__)
slouken@1952
    38
#define DEFAULT_OPENGL	"/usr/X11R6/lib/libGL.1.dylib"
slouken@1952
    39
#elif defined(__QNXNTO__)
slouken@1952
    40
#define DEFAULT_OPENGL	"libGL.so.3"
slouken@1952
    41
#else
slouken@1952
    42
#define DEFAULT_OPENGL	"libGL.so.1"
slouken@1952
    43
#endif
slouken@1952
    44
slouken@3241
    45
#ifndef GLX_NONE_EXT
slouken@3241
    46
#define GLX_NONE_EXT                       0x8000
slouken@3241
    47
#endif
slouken@3241
    48
slouken@1952
    49
#ifndef GLX_ARB_multisample
slouken@1952
    50
#define GLX_ARB_multisample
slouken@1952
    51
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@1952
    52
#define GLX_SAMPLES_ARB                    100001
slouken@1952
    53
#endif
slouken@1952
    54
slouken@1952
    55
#ifndef GLX_EXT_visual_rating
slouken@1952
    56
#define GLX_EXT_visual_rating
slouken@1952
    57
#define GLX_VISUAL_CAVEAT_EXT              0x20
slouken@1952
    58
#define GLX_NONE_EXT                       0x8000
slouken@1952
    59
#define GLX_SLOW_VISUAL_EXT                0x8001
slouken@1952
    60
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1952
    61
#endif
slouken@1952
    62
slouken@3100
    63
#ifndef GLX_ARB_create_context
slouken@3100
    64
#define GLX_ARB_create_context
slouken@3100
    65
#define GLX_CONTEXT_MAJOR_VERSION_ARB      0x2091
slouken@3100
    66
#define GLX_CONTEXT_MINOR_VERSION_ARB      0x2092
slouken@3100
    67
#define GLX_CONTEXT_FLAGS_ARB              0x2094
slouken@3100
    68
#define GLX_CONTEXT_DEBUG_BIT_ARB          0x0001
slouken@3100
    69
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
slouken@5021
    70
slouken@5021
    71
/* Typedef for the GL 3.0 context creation function */
slouken@5021
    72
typedef GLXContext(*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display * dpy,
slouken@5021
    73
                                                        GLXFBConfig config,
slouken@5021
    74
                                                        GLXContext
slouken@5021
    75
                                                        share_context,
slouken@5021
    76
                                                        Bool direct,
slouken@5021
    77
                                                        const int
slouken@5021
    78
                                                        *attrib_list);
icculus@6360
    79
#endif
slouken@6296
    80
slouken@6296
    81
#ifndef GLX_ARB_create_context_profile
slouken@6296
    82
#define GLX_ARB_create_context_profile
slouken@6296
    83
#define GLX_CONTEXT_PROFILE_MASK_ARB       0x9126
slouken@6296
    84
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB   0x00000001
slouken@6296
    85
#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
slouken@6296
    86
#endif
slouken@6296
    87
slouken@6296
    88
#ifndef GLX_ARB_create_context_robustness
slouken@6296
    89
#define GLX_ARB_create_context_robustness
slouken@6296
    90
#define GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB  0x00000004
slouken@6296
    91
#define GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB     0x8256
slouken@6296
    92
#define GLX_NO_RESET_NOTIFICATION_ARB                   0x8261
slouken@6296
    93
#define GLX_LOSE_CONTEXT_ON_RESET_ARB                   0x8252
slouken@6296
    94
#endif
slouken@6296
    95
slouken@6296
    96
#ifndef GLX_EXT_create_context_es2_profile
slouken@6296
    97
#define GLX_EXT_create_context_es2_profile
icculus@6375
    98
#ifndef GLX_CONTEXT_ES2_PROFILE_BIT_EXT
slouken@6296
    99
#define GLX_CONTEXT_ES2_PROFILE_BIT_EXT    0x00000002
slouken@6296
   100
#endif
icculus@6375
   101
#endif
slouken@6296
   102
slouken@6296
   103
#ifndef GLX_EXT_swap_control
slouken@6296
   104
#define GLX_SWAP_INTERVAL_EXT              0x20F1
slouken@6296
   105
#define GLX_MAX_SWAP_INTERVAL_EXT          0x20F2
slouken@3100
   106
#endif
slouken@3100
   107
icculus@6382
   108
#ifndef GLX_EXT_swap_control_tear
icculus@6382
   109
#define GLX_LATE_SWAPS_TEAR_EXT 0x20F3
icculus@6382
   110
#endif
icculus@6382
   111
slouken@6183
   112
#define OPENGL_REQUIRES_DLOPEN
slouken@6183
   113
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@1952
   114
#include <dlfcn.h>
slouken@1952
   115
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@1952
   116
#define GL_LoadFunction		dlsym
slouken@1952
   117
#define GL_UnloadObject		dlclose
slouken@1952
   118
#else
slouken@1952
   119
#define GL_LoadObject	SDL_LoadObject
slouken@1952
   120
#define GL_LoadFunction	SDL_LoadFunction
slouken@1952
   121
#define GL_UnloadObject	SDL_UnloadObject
slouken@1952
   122
#endif
slouken@1952
   123
slouken@3057
   124
static void X11_GL_InitExtensions(_THIS);
bob@2322
   125
slouken@3100
   126
slouken@1952
   127
int
slouken@1952
   128
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@1952
   129
{
slouken@1952
   130
    void *handle;
slouken@1952
   131
slouken@6370
   132
    if (_this->gl_data) {
slouken@6370
   133
        SDL_SetError("OpenGL context already created");
slouken@6370
   134
        return -1;
slouken@6370
   135
    }
slouken@6370
   136
slouken@6370
   137
    /* If SDL_GL_CONTEXT_EGL has been changed to 1, switch over to X11_GLES functions  */
slouken@6370
   138
    if (_this->gl_config.use_egl == 1) {
slouken@6373
   139
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6370
   140
        _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
slouken@6370
   141
        _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
slouken@6370
   142
        _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
slouken@6370
   143
        _this->GL_CreateContext = X11_GLES_CreateContext;
slouken@6370
   144
        _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
slouken@6370
   145
        _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
slouken@6370
   146
        _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
slouken@6370
   147
        _this->GL_SwapWindow = X11_GLES_SwapWindow;
slouken@6370
   148
        _this->GL_DeleteContext = X11_GLES_DeleteContext;
slouken@6370
   149
        return X11_GLES_LoadLibrary(_this, path);
slouken@6373
   150
#else
slouken@6373
   151
        SDL_SetError("SDL not configured with OpenGL ES/EGL support");
slouken@6373
   152
        return -1;
slouken@6373
   153
#endif
slouken@6370
   154
    }
slouken@6370
   155
slouken@6370
   156
slouken@3057
   157
    /* Load the OpenGL library */
slouken@3057
   158
    if (path == NULL) {
slouken@3057
   159
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
   160
    }
slouken@3057
   161
    if (path == NULL) {
slouken@3057
   162
        path = DEFAULT_OPENGL;
slouken@3057
   163
    }
slouken@6183
   164
    _this->gl_config.dll_handle = GL_LoadObject(path);
slouken@3057
   165
    if (!_this->gl_config.dll_handle) {
slouken@6183
   166
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@6183
   167
        SDL_SetError("Failed loading %s: %s", path, dlerror());
slouken@6183
   168
#endif
slouken@3057
   169
        return -1;
slouken@3057
   170
    }
slouken@3057
   171
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3057
   172
                SDL_arraysize(_this->gl_config.driver_path));
slouken@2244
   173
slouken@3057
   174
    /* Allocate OpenGL memory */
slouken@3057
   175
    _this->gl_data =
slouken@3057
   176
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@3057
   177
                                               sizeof(struct
slouken@3057
   178
                                                      SDL_GLDriverData));
slouken@3057
   179
    if (!_this->gl_data) {
slouken@3057
   180
        SDL_OutOfMemory();
slouken@3057
   181
        return -1;
slouken@3057
   182
    }
slouken@3057
   183
slouken@3057
   184
    /* Load function pointers */
slouken@3057
   185
    handle = _this->gl_config.dll_handle;
slouken@1952
   186
    _this->gl_data->glXGetProcAddress =
slouken@4550
   187
        (void *(*)(const GLubyte *))
slouken@4550
   188
            GL_LoadFunction(handle, "glXGetProcAddressARB");
slouken@1952
   189
    _this->gl_data->glXChooseVisual =
slouken@4550
   190
        (XVisualInfo * (*)(Display *, int, int *))
slouken@4550
   191
            X11_GL_GetProcAddress(_this, "glXChooseVisual");
slouken@1952
   192
    _this->gl_data->glXCreateContext =
slouken@1952
   193
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@4550
   194
            X11_GL_GetProcAddress(_this, "glXCreateContext");
slouken@1952
   195
    _this->gl_data->glXDestroyContext =
slouken@4550
   196
        (void (*)(Display *, GLXContext))
slouken@4550
   197
            X11_GL_GetProcAddress(_this, "glXDestroyContext");
slouken@1952
   198
    _this->gl_data->glXMakeCurrent =
slouken@4550
   199
        (int (*)(Display *, GLXDrawable, GLXContext))
slouken@4550
   200
            X11_GL_GetProcAddress(_this, "glXMakeCurrent");
slouken@1952
   201
    _this->gl_data->glXSwapBuffers =
slouken@4550
   202
        (void (*)(Display *, GLXDrawable))
slouken@4550
   203
            X11_GL_GetProcAddress(_this, "glXSwapBuffers");
icculus@5572
   204
    _this->gl_data->glXQueryDrawable =
icculus@5572
   205
        (void (*)(Display*,GLXDrawable,int,unsigned int*))
icculus@5572
   206
            X11_GL_GetProcAddress(_this, "glXQueryDrawable");
slouken@1952
   207
slouken@1952
   208
    if (!_this->gl_data->glXChooseVisual ||
slouken@1952
   209
        !_this->gl_data->glXCreateContext ||
slouken@1952
   210
        !_this->gl_data->glXDestroyContext ||
slouken@4550
   211
        !_this->gl_data->glXMakeCurrent ||
slouken@4550
   212
        !_this->gl_data->glXSwapBuffers) {
slouken@1952
   213
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   214
        return -1;
slouken@1952
   215
    }
slouken@1952
   216
slouken@3057
   217
    /* Initialize extensions */
slouken@3057
   218
    X11_GL_InitExtensions(_this);
slouken@3057
   219
slouken@1952
   220
    return 0;
slouken@1952
   221
}
slouken@1952
   222
slouken@1952
   223
void *
slouken@1952
   224
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@1952
   225
{
slouken@1952
   226
    if (_this->gl_data->glXGetProcAddress) {
slouken@1952
   227
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1952
   228
    }
slouken@4550
   229
    return GL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1952
   230
}
slouken@1952
   231
slouken@3057
   232
void
slouken@1952
   233
X11_GL_UnloadLibrary(_THIS)
slouken@1952
   234
{
slouken@3057
   235
    /* Don't actually unload the library, since it may have registered
slouken@3057
   236
     * X11 shutdown hooks, per the notes at:
slouken@3057
   237
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@3057
   238
     */
slouken@3057
   239
#if 0
slouken@3057
   240
    GL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
   241
    _this->gl_config.dll_handle = NULL;
slouken@3057
   242
#endif
slouken@3057
   243
slouken@3057
   244
    /* Free OpenGL memory */
slouken@6181
   245
    if (_this->gl_data) {
slouken@6181
   246
        SDL_free(_this->gl_data);
slouken@6181
   247
        _this->gl_data = NULL;
slouken@6181
   248
    }
slouken@1952
   249
}
slouken@1952
   250
slouken@1952
   251
static SDL_bool
slouken@1952
   252
HasExtension(const char *extension, const char *extensions)
slouken@1952
   253
{
slouken@1952
   254
    const char *start;
slouken@1952
   255
    const char *where, *terminator;
slouken@1952
   256
slouken@1952
   257
    /* Extension names should not have spaces. */
slouken@1952
   258
    where = SDL_strchr(extension, ' ');
slouken@1952
   259
    if (where || *extension == '\0')
slouken@1952
   260
        return SDL_FALSE;
slouken@1952
   261
slouken@1952
   262
    if (!extensions)
slouken@1952
   263
        return SDL_FALSE;
slouken@1952
   264
slouken@1952
   265
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1952
   266
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1952
   267
     * etc. */
slouken@1952
   268
slouken@1952
   269
    start = extensions;
slouken@1952
   270
slouken@1952
   271
    for (;;) {
slouken@1952
   272
        where = SDL_strstr(start, extension);
slouken@1952
   273
        if (!where)
slouken@1952
   274
            break;
slouken@1952
   275
slouken@1952
   276
        terminator = where + SDL_strlen(extension);
slouken@1952
   277
        if (where == start || *(where - 1) == ' ')
slouken@1952
   278
            if (*terminator == ' ' || *terminator == '\0')
slouken@1952
   279
                return SDL_TRUE;
slouken@1952
   280
slouken@1952
   281
        start = terminator;
slouken@1952
   282
    }
slouken@1952
   283
    return SDL_FALSE;
slouken@1952
   284
}
slouken@1952
   285
slouken@1952
   286
static void
slouken@1952
   287
X11_GL_InitExtensions(_THIS)
slouken@1952
   288
{
slouken@1952
   289
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@5244
   290
    int screen = DefaultScreen(display);
slouken@1952
   291
    XVisualInfo *vinfo;
slouken@1952
   292
    XSetWindowAttributes xattr;
slouken@1952
   293
    Window w;
slouken@1952
   294
    GLXContext context;
slouken@1952
   295
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
slouken@1952
   296
    const char *extensions;
slouken@1952
   297
slouken@1952
   298
    vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   299
    if (!vinfo) {
slouken@1952
   300
        return;
slouken@1952
   301
    }
slouken@1952
   302
    xattr.background_pixel = 0;
slouken@1952
   303
    xattr.border_pixel = 0;
slouken@1952
   304
    xattr.colormap =
slouken@1952
   305
        XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
slouken@1952
   306
                        AllocNone);
slouken@1952
   307
    w = XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
slouken@1952
   308
                      vinfo->depth, InputOutput, vinfo->visual,
slouken@1952
   309
                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
slouken@1952
   310
    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@1952
   311
    if (context) {
slouken@1952
   312
        _this->gl_data->glXMakeCurrent(display, w, context);
slouken@1952
   313
    }
slouken@1952
   314
    XFree(vinfo);
slouken@1952
   315
slouken@1952
   316
    glXQueryExtensionsStringFunc =
slouken@1952
   317
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
slouken@1952
   318
                                                                "glXQueryExtensionsString");
slouken@1952
   319
    if (glXQueryExtensionsStringFunc) {
slouken@1952
   320
        extensions = glXQueryExtensionsStringFunc(display, screen);
slouken@1952
   321
    } else {
slouken@1952
   322
        extensions = NULL;
slouken@1952
   323
    }
slouken@1952
   324
icculus@6382
   325
    /* Check for GLX_EXT_swap_control(_tear) */
icculus@6382
   326
    _this->gl_data->HAS_GLX_EXT_swap_control_tear = SDL_FALSE;
icculus@5572
   327
    if (HasExtension("GLX_EXT_swap_control", extensions)) {
icculus@5572
   328
        _this->gl_data->glXSwapIntervalEXT =
icculus@5630
   329
            (int (*)(Display*,GLXDrawable,int))
icculus@5572
   330
                X11_GL_GetProcAddress(_this, "glXSwapIntervalEXT");
icculus@6382
   331
        if (HasExtension("GLX_EXT_swap_control_tear", extensions)) {
icculus@6382
   332
            _this->gl_data->HAS_GLX_EXT_swap_control_tear = SDL_TRUE;
icculus@6382
   333
        }
slouken@1952
   334
    }
slouken@1952
   335
slouken@1952
   336
    /* Check for GLX_MESA_swap_control */
slouken@1952
   337
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
slouken@1952
   338
        _this->gl_data->glXSwapIntervalMESA =
icculus@5572
   339
            (int(*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalMESA");
slouken@1952
   340
        _this->gl_data->glXGetSwapIntervalMESA =
icculus@5572
   341
            (int(*)(void)) X11_GL_GetProcAddress(_this,
slouken@1952
   342
                                                   "glXGetSwapIntervalMESA");
slouken@1952
   343
    }
slouken@1952
   344
icculus@5572
   345
    /* Check for GLX_SGI_swap_control */
icculus@5572
   346
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
icculus@5572
   347
        _this->gl_data->glXSwapIntervalSGI =
icculus@5572
   348
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
icculus@5572
   349
    }
icculus@5572
   350
slouken@1952
   351
    /* Check for GLX_EXT_visual_rating */
slouken@1952
   352
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
slouken@1952
   353
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
slouken@1952
   354
    }
slouken@1952
   355
slouken@1952
   356
    if (context) {
slouken@1952
   357
        _this->gl_data->glXMakeCurrent(display, None, NULL);
slouken@1952
   358
        _this->gl_data->glXDestroyContext(display, context);
slouken@1952
   359
    }
slouken@1952
   360
    XDestroyWindow(display, w);
bob@2323
   361
    X11_PumpEvents(_this);
slouken@1952
   362
}
slouken@1952
   363
slouken@6296
   364
/* glXChooseVisual and glXChooseFBConfig have some small differences in
slouken@6296
   365
 * the attribute encoding, it can be chosen with the for_FBConfig parameter. 
slouken@6296
   366
 */
slouken@5395
   367
int 
slouken@6296
   368
X11_GL_GetAttributes(_THIS, Display * display, int screen, int * attribs, int size, Bool for_FBConfig)
slouken@1952
   369
{
slouken@5395
   370
    int i = 0;
slouken@1952
   371
slouken@5395
   372
    /* assert buffer is large enough to hold all SDL attributes. */ 
icculus@6281
   373
    SDL_assert(size >= 32);
slouken@3139
   374
slouken@1952
   375
    /* Setup our GLX attributes according to the gl_config. */
slouken@6296
   376
    if( for_FBConfig ) {
slouken@6296
   377
        attribs[i++] = GLX_RENDER_TYPE;
slouken@6296
   378
	attribs[i++] = GLX_RGBA_BIT;
slouken@6296
   379
    } else {
slouken@6296
   380
        attribs[i++] = GLX_RGBA;
slouken@6296
   381
    }
slouken@1952
   382
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   383
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   384
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   385
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   386
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   387
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   388
slouken@1952
   389
    if (_this->gl_config.alpha_size) {
slouken@1952
   390
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   391
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   392
    }
slouken@1952
   393
slouken@1952
   394
    if (_this->gl_config.double_buffer) {
slouken@1952
   395
        attribs[i++] = GLX_DOUBLEBUFFER;
slouken@6296
   396
	if( for_FBConfig )
slouken@6296
   397
	    attribs[i++] = True;
slouken@1952
   398
    }
slouken@1952
   399
slouken@1952
   400
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   401
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   402
slouken@1952
   403
    if (_this->gl_config.stencil_size) {
slouken@1952
   404
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   405
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   406
    }
slouken@1952
   407
slouken@1952
   408
    if (_this->gl_config.accum_red_size) {
slouken@1952
   409
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   410
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   411
    }
slouken@1952
   412
slouken@1952
   413
    if (_this->gl_config.accum_green_size) {
slouken@1952
   414
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   415
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   416
    }
slouken@1952
   417
slouken@1952
   418
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   419
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   420
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   421
    }
slouken@1952
   422
slouken@1952
   423
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   424
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   425
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   426
    }
slouken@1952
   427
slouken@1952
   428
    if (_this->gl_config.stereo) {
slouken@1952
   429
        attribs[i++] = GLX_STEREO;
slouken@6296
   430
	if( for_FBConfig )
slouken@6296
   431
	    attribs[i++] = True;
slouken@1952
   432
    }
slouken@1952
   433
slouken@1952
   434
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   435
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   436
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   437
    }
slouken@1952
   438
slouken@1952
   439
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   440
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   441
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   442
    }
slouken@1952
   443
slouken@3571
   444
    if (_this->gl_config.accelerated >= 0 &&
slouken@3571
   445
        _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   446
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@3570
   447
        attribs[i++] = _this->gl_config.accelerated ? GLX_NONE_EXT :
slouken@3570
   448
                                                      GLX_SLOW_VISUAL_EXT;
slouken@1952
   449
    }
slouken@3570
   450
slouken@1952
   451
    attribs[i++] = None;
slouken@5395
   452
 
slouken@5395
   453
    return i;
slouken@5395
   454
}
slouken@5395
   455
slouken@5395
   456
XVisualInfo *
slouken@5395
   457
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@5395
   458
{
slouken@5395
   459
    XVisualInfo *vinfo;
slouken@5395
   460
slouken@5395
   461
    /* 64 seems nice. */
slouken@6296
   462
    int attribs[64];
icculus@6359
   463
    X11_GL_GetAttributes(_this,display,screen,attribs,64,SDL_FALSE);
slouken@1952
   464
slouken@6181
   465
    if (!_this->gl_data) {
slouken@6181
   466
        /* The OpenGL library wasn't loaded, SDL_GetError() should have info */
slouken@6181
   467
        return NULL;
slouken@6181
   468
    }
slouken@6181
   469
slouken@1952
   470
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
slouken@1952
   471
    if (!vinfo) {
slouken@1952
   472
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   473
    }
slouken@1952
   474
    return vinfo;
slouken@1952
   475
}
slouken@1952
   476
slouken@1952
   477
SDL_GLContext
slouken@1952
   478
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   479
{
slouken@1952
   480
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   481
    Display *display = data->videodata->display;
slouken@1952
   482
    int screen =
slouken@5246
   483
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
slouken@1952
   484
    XWindowAttributes xattr;
slouken@1952
   485
    XVisualInfo v, *vinfo;
slouken@1952
   486
    int n;
slouken@1952
   487
    GLXContext context = NULL;
slouken@1952
   488
slouken@1955
   489
    /* We do this to create a clean separation between X and GLX errors. */
slouken@1952
   490
    XSync(display, False);
slouken@3685
   491
    XGetWindowAttributes(display, data->xwindow, &xattr);
slouken@1952
   492
    v.screen = screen;
slouken@1952
   493
    v.visualid = XVisualIDFromVisual(xattr.visual);
slouken@1952
   494
    vinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   495
    if (vinfo) {
slouken@3139
   496
        if (_this->gl_config.major_version < 3) {
slouken@3100
   497
            context =
slouken@3100
   498
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@3100
   499
        } else {
slouken@3100
   500
            /* If we want a GL 3.0 context or later we need to get a temporary
slouken@3100
   501
               context to grab the new context creation function */
slouken@3139
   502
            GLXContext temp_context =
slouken@3139
   503
                _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
slouken@3100
   504
            if (!temp_context) {
slouken@3139
   505
                SDL_SetError("Could not create GL context");
slouken@3100
   506
                return NULL;
slouken@3100
   507
            } else {
slouken@6296
   508
	        /* max 8 attributes plus terminator */
slouken@6296
   509
                int attribs[9] = {
slouken@3139
   510
                    GLX_CONTEXT_MAJOR_VERSION_ARB,
slouken@3139
   511
                    _this->gl_config.major_version,
slouken@3139
   512
                    GLX_CONTEXT_MINOR_VERSION_ARB,
slouken@3139
   513
                    _this->gl_config.minor_version,
slouken@3139
   514
                    0
slouken@3100
   515
                };
slouken@6296
   516
		int iattr = 4;
slouken@6296
   517
slouken@6296
   518
		/* SDL profile bits match GLX profile bits */
slouken@6296
   519
		if( _this->gl_config.profile_mask != 0 ) {
slouken@6296
   520
		    attribs[iattr++] = GLX_CONTEXT_PROFILE_MASK_ARB;
slouken@6296
   521
		    attribs[iattr++] = _this->gl_config.profile_mask;
slouken@6296
   522
		}
slouken@6296
   523
slouken@6296
   524
		/* SDL flags match GLX flags */
slouken@6296
   525
		if( _this->gl_config.flags != 0 ) {
slouken@6296
   526
		    attribs[iattr++] = GLX_CONTEXT_FLAGS_ARB;
slouken@6296
   527
		    attribs[iattr++] = _this->gl_config.flags;
slouken@6296
   528
		}
slouken@6296
   529
slouken@6296
   530
		attribs[iattr++] = 0;
slouken@3139
   531
slouken@3100
   532
                /* Get a pointer to the context creation function for GL 3.0 */
slouken@3139
   533
                PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs =
slouken@3139
   534
                    (PFNGLXCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
slouken@3139
   535
                    glXGetProcAddress((GLubyte *)
slouken@3139
   536
                                      "glXCreateContextAttribsARB");
slouken@3100
   537
                if (!glXCreateContextAttribs) {
slouken@3100
   538
                    SDL_SetError("GL 3.x is not supported");
slouken@3100
   539
                    context = temp_context;
slouken@3100
   540
                } else {
slouken@5395
   541
                    int glxAttribs[64];
slouken@5395
   542
slouken@3105
   543
                    /* Create a GL 3.x context */
slouken@3100
   544
                    GLXFBConfig *framebuffer_config = NULL;
slouken@3139
   545
                    int fbcount = 0;
slouken@3139
   546
                    GLXFBConfig *(*glXChooseFBConfig) (Display * disp,
slouken@3139
   547
                                                       int screen,
slouken@3139
   548
                                                       const int *attrib_list,
slouken@3139
   549
                                                       int *nelements);
slouken@3105
   550
slouken@3139
   551
                    glXChooseFBConfig =
slouken@3139
   552
                        (GLXFBConfig *
slouken@3139
   553
                         (*)(Display *, int, const int *,
slouken@3139
   554
                             int *)) _this->gl_data->
slouken@3139
   555
                        glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
slouken@3105
   556
slouken@6296
   557
                    X11_GL_GetAttributes(_this,display,screen,glxAttribs,64,SDL_TRUE);
slouken@5395
   558
slouken@3139
   559
                    if (!glXChooseFBConfig
slouken@3139
   560
                        || !(framebuffer_config =
slouken@3139
   561
                             glXChooseFBConfig(display,
slouken@5395
   562
                                               DefaultScreen(display), glxAttribs,
slouken@3139
   563
                                               &fbcount))) {
slouken@3139
   564
                        SDL_SetError
slouken@3139
   565
                            ("No good framebuffers found. GL 3.x disabled");
slouken@3100
   566
                        context = temp_context;
slouken@3139
   567
                    } else {
slouken@3139
   568
                        context =
slouken@3139
   569
                            glXCreateContextAttribs(display,
slouken@3139
   570
                                                    framebuffer_config[0],
slouken@3139
   571
                                                    NULL, True, attribs);
slouken@3139
   572
                        _this->gl_data->glXDestroyContext(display,
slouken@3139
   573
                                                          temp_context);
slouken@3100
   574
                    }
slouken@3100
   575
                }
slouken@3100
   576
            }
slouken@3100
   577
        }
slouken@1952
   578
        XFree(vinfo);
slouken@1952
   579
    }
slouken@1952
   580
    XSync(display, False);
slouken@1952
   581
slouken@1952
   582
    if (!context) {
slouken@1952
   583
        SDL_SetError("Could not create GL context");
slouken@2178
   584
        return NULL;
slouken@1952
   585
    }
slouken@2178
   586
slouken@2178
   587
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   588
        X11_GL_DeleteContext(_this, context);
slouken@2178
   589
        return NULL;
slouken@2178
   590
    }
slouken@2178
   591
slouken@2178
   592
    return context;
slouken@1952
   593
}
slouken@1952
   594
slouken@1952
   595
int
slouken@1952
   596
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   597
{
slouken@1952
   598
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   599
    Window drawable =
slouken@3685
   600
        (window ? ((SDL_WindowData *) window->driverdata)->xwindow : None);
slouken@1952
   601
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   602
    int status;
slouken@1952
   603
slouken@1952
   604
    status = 0;
slouken@1952
   605
    if (!_this->gl_data->glXMakeCurrent(display, drawable, glx_context)) {
slouken@1952
   606
        SDL_SetError("Unable to make GL context current");
slouken@1952
   607
        status = -1;
slouken@1952
   608
    }
slouken@1952
   609
    XSync(display, False);
slouken@1952
   610
slouken@1952
   611
    return (status);
slouken@1952
   612
}
slouken@1952
   613
bob@2327
   614
/* 
icculus@5572
   615
   0 is a valid argument to glxSwapInterval(MESA|EXT) and setting it to 0
icculus@5572
   616
   will undo the effect of a previous call with a value that is greater
icculus@5572
   617
   than zero (or at least that is what the docs say). OTOH, 0 is an invalid
icculus@5572
   618
   argument to glxSwapIntervalSGI and it returns an error if you call it
icculus@5572
   619
   with 0 as an argument.
bob@2327
   620
*/
bob@2327
   621
bob@2327
   622
static int swapinterval = -1;
slouken@1952
   623
int
slouken@1952
   624
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   625
{
icculus@6382
   626
    int status = -1;
slouken@1952
   627
icculus@6382
   628
    if ((interval < 0) && (!_this->gl_data->HAS_GLX_EXT_swap_control_tear)) {
icculus@6382
   629
        SDL_SetError("Negative swap interval unsupported in this GL");
icculus@6382
   630
    } else if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   631
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   632
        const SDL_WindowData *windowdata = (SDL_WindowData *)
icculus@5572
   633
            _this->current_glwin->driverdata;
icculus@5572
   634
        Window drawable = windowdata->xwindow;
icculus@5630
   635
        status = _this->gl_data->glXSwapIntervalEXT(display,drawable,interval);
icculus@5630
   636
        if (status != 0) {
icculus@5630
   637
            SDL_SetError("glxSwapIntervalEXT failed");
icculus@5630
   638
        } else {
icculus@5630
   639
            swapinterval = interval;
icculus@5630
   640
        }
icculus@5572
   641
    } else if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   642
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   643
        if (status != 0) {
slouken@1952
   644
            SDL_SetError("glxSwapIntervalMESA failed");
bob@2327
   645
        } else {
bob@2327
   646
            swapinterval = interval;
slouken@1952
   647
        }
slouken@1952
   648
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   649
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   650
        if (status != 0) {
slouken@1952
   651
            SDL_SetError("glxSwapIntervalSGI failed");
bob@2327
   652
        } else {
bob@2327
   653
            swapinterval = interval;
slouken@1952
   654
        }
slouken@1952
   655
    } else {
slouken@1952
   656
        SDL_Unsupported();
slouken@1952
   657
    }
slouken@1952
   658
    return status;
slouken@1952
   659
}
slouken@1952
   660
slouken@1952
   661
int
slouken@1952
   662
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   663
{
icculus@5572
   664
    if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   665
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   666
        const SDL_WindowData *windowdata = (SDL_WindowData *)
icculus@5572
   667
            _this->current_glwin->driverdata;
icculus@5572
   668
        Window drawable = windowdata->xwindow;
icculus@6382
   669
        unsigned int allow_late_swap_tearing = 0;
icculus@6382
   670
        unsigned int interval = 0;
icculus@6382
   671
icculus@6382
   672
        if (_this->gl_data->HAS_GLX_EXT_swap_control_tear) {
icculus@6382
   673
            _this->gl_data->glXQueryDrawable(display, drawable,
icculus@6382
   674
                                            GLX_LATE_SWAPS_TEAR_EXT,
icculus@6382
   675
                                            &allow_late_swap_tearing);
icculus@6382
   676
        }
icculus@6382
   677
icculus@5572
   678
        _this->gl_data->glXQueryDrawable(display, drawable,
icculus@6382
   679
                                         GLX_SWAP_INTERVAL_EXT, &interval);
icculus@6382
   680
icculus@6382
   681
        if ((allow_late_swap_tearing) && (interval > 0)) {
icculus@6382
   682
            return -((int) interval);
icculus@6382
   683
        }
icculus@6382
   684
icculus@6382
   685
        return (int) interval;
icculus@5572
   686
    } else if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   687
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   688
    } else {
bob@2327
   689
        return swapinterval;
slouken@1952
   690
    }
slouken@1952
   691
}
slouken@1952
   692
slouken@1952
   693
void
slouken@1952
   694
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   695
{
slouken@1952
   696
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   697
    Display *display = data->videodata->display;
slouken@1952
   698
slouken@3685
   699
    _this->gl_data->glXSwapBuffers(display, data->xwindow);
slouken@1952
   700
}
slouken@1952
   701
slouken@1952
   702
void
slouken@1952
   703
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   704
{
slouken@1952
   705
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   706
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   707
slouken@1952
   708
    _this->gl_data->glXDestroyContext(display, glx_context);
bob@2324
   709
    XSync(display, False);
slouken@1952
   710
}
slouken@1952
   711
slouken@1952
   712
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   713
slouken@6044
   714
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@6044
   715
slouken@1952
   716
/* vi: set ts=4 sw=4 expandtab: */