src/video/x11/SDL_x11opengl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:37:57 -0500
changeset 6296 b42657486c0d
parent 6282 64ae376becce
child 6359 b2a768065c32
permissions -rwxr-xr-x
Add OpenGL 3.X context creation support

Matthias Bentrup 2011-10-30 03:58:24 PDT

I've updated the context creation patch to include the bugfixes by Martin
Schreiber and also included a profile bit to request a ES2 compatible profile.

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