Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch)
authorSam Lantinga <slouken@libsdl.org>
Thu, 24 Feb 2011 17:52:47 -0800
changeset 5395 f0e399961f3a
parent 5393 17da19f8ca05
child 5396 109b7c071f70
Fixed bug 1145 (GL Context creation fails for OpenGL 3.2 + Alpha buffer with X11 BadMatch) Matthias 2011-02-23 09:37:51 PST Please view the attached source file. Using this minimal program (as attached), it creates an OpenGL 2.0 context with a cleared color buffer. If I set the OpenGL version to 3.2, the function SDL_GL_CreateContext fails (or more specifically, glXMakeCurrent fails) with an X11 BadMatch error: X Error of failed request: BadMatch (invalid parameter attributes) Major opcode of failed request: 128 (GLX) Minor opcode of failed request: 5 (X_GLXMakeCurrent) Serial number of failed request: 153 Current serial number in output stream: 153 Also note that if I do not specify the alpha buffer size, the program works for OpenGL 2.0 and OpenGL 3.2. After some further analysis, I believe I have found the problem. The specific issue is in: SDL_x11opengl.c::X11_GL_CreateContext. Note that for OpenGL 3.2 contexts, the GLXFBConfig to use is chosen as the best match from glXChooseFBConfig. However, opengl attributes originally set with SDL_GL_SetAttribute are not mapped to GLX attributes and then passed to the glXChooseFBConfig function. According to the GLX 1.4 specification, if the attributes are not specified, the function falls back to defaults (which, in this particular case, prefer alpha channel size == 0). For testing purposes, I modified the call to glXChooseFBConfig to look something like this: int glxAttribs[] = { GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, GLX_ALPHA_SIZE,8, None }; if (!glXChooseFBConfig || !(framebuffer_config = glXChooseFBConfig(display, DefaultScreen(display), glxAttribs, &fbcount))) { ... } The best match GLXFBConfig then supports 8 bit alpha channel. The program then works as intended. Hope this helps!
src/video/x11/SDL_x11opengl.c
--- a/src/video/x11/SDL_x11opengl.c	Thu Feb 24 09:41:04 2011 -0800
+++ b/src/video/x11/SDL_x11opengl.c	Thu Feb 24 17:52:47 2011 -0800
@@ -284,14 +284,13 @@
     X11_PumpEvents(_this);
 }
 
-XVisualInfo *
-X11_GL_GetVisual(_THIS, Display * display, int screen)
+int 
+X11_GL_GetAttributes(_THIS, Display * display, int screen, int * attribs, int size)
 {
-    XVisualInfo *vinfo;
+    int i = 0;
 
-    /* 64 seems nice. */
-    int attribs[64];
-    int i = 0;
+    /* assert buffer is large enough to hold all SDL attributes. */ 
+    /* assert(size >= 32);*/
 
     /* Setup our GLX attributes according to the gl_config. */
     attribs[i++] = GLX_RGBA;
@@ -366,6 +365,18 @@
     }
 
     attribs[i++] = None;
+ 
+    return i;
+}
+
+XVisualInfo *
+X11_GL_GetVisual(_THIS, Display * display, int screen)
+{
+    XVisualInfo *vinfo;
+
+    /* 64 seems nice. */
+    int attribs[64];
+    int i = X11_GL_GetAttributes(_this,display,screen,attribs,64);
 
     vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
     if (!vinfo) {
@@ -422,6 +433,8 @@
                     SDL_SetError("GL 3.x is not supported");
                     context = temp_context;
                 } else {
+                    int glxAttribs[64];
+
                     /* Create a GL 3.x context */
                     GLXFBConfig *framebuffer_config = NULL;
                     int fbcount = 0;
@@ -436,10 +449,12 @@
                              int *)) _this->gl_data->
                         glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
 
+                    X11_GL_GetAttributes(_this,display,screen,glxAttribs,64);
+
                     if (!glXChooseFBConfig
                         || !(framebuffer_config =
                              glXChooseFBConfig(display,
-                                               DefaultScreen(display), NULL,
+                                               DefaultScreen(display), glxAttribs,
                                                &fbcount))) {
                         SDL_SetError
                             ("No good framebuffers found. GL 3.x disabled");