From 020858fa4e9bd72668cc49adcddfe36aaf34d672 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 10 Mar 2013 09:07:23 -0700 Subject: [PATCH] Fixed bug 1749 - SDL_GL_CreateContext() causes fatal X11 protocol errors that should just be caught instead Lee Salzman When using SDL_GL_CreateContext() to create a >= 3.0 version or core/forward-compatible context, internally glXCreateContextAttribsARB is used. Mesa in particular seems to be having trouble with this call and returning all sorts of errors, so it is dangerous to poll for the highest GL version by using calls to SDL_GL_CreateContext unless you are sure, a priori, that the call will suceed, defeating the point of its use. X11 protocol errors are of the following form, with varying details depending on user, but the cause is always SDL_GL_CreateContext as above... X Error of failed request: GLXBadFBConfig Major opcode of failed request: 153 (GLX) Minor opcode of failed request: 34 () Serial number of failed request: 215 Current serial number in output stream: 221 These sorts of errors can be temporarily filtered out by setting an X11 error handler to catch and ignore them, which is safe with respect to SDL_GL_CreateContext behavior because this function is allowed to return NULL to indicate failure. A patch is attached to do this temporary filtering/catching of errors generated by trying to use glXCreateContextAttribs and friends... --- src/video/x11/SDL_x11opengl.c | 36 ++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/video/x11/SDL_x11opengl.c b/src/video/x11/SDL_x11opengl.c index 43760d0b1..400ef84e9 100644 --- a/src/video/x11/SDL_x11opengl.c +++ b/src/video/x11/SDL_x11opengl.c @@ -496,6 +496,33 @@ X11_GL_GetVisual(_THIS, Display * display, int screen) return vinfo; } +#ifndef GLXBadContext +#define GLXBadContext 0 +#endif +#ifndef GLXBadFBConfig +#define GLXBadFBConfig 9 +#endif +#ifndef GLXBadProfileARB +#define GLXBadProfileARB 13 +#endif +static int (*handler) (Display *, XErrorEvent *) = NULL; +static int +X11_GL_CreateContextErrorHandler(Display * d, XErrorEvent * e) +{ + switch (e->error_code) { + case GLXBadContext: + case GLXBadFBConfig: + case GLXBadProfileARB: + case BadRequest: + case BadMatch: + case BadValue: + case BadAlloc: + return (0); + default: + return (handler(d, e)); + } +} + SDL_GLContext X11_GL_CreateContext(_THIS, SDL_Window * window) { @@ -516,6 +543,7 @@ X11_GL_CreateContext(_THIS, SDL_Window * window) /* We do this to create a clean separation between X and GLX errors. */ XSync(display, False); + handler = XSetErrorHandler(X11_GL_CreateContextErrorHandler); XGetWindowAttributes(display, data->xwindow, &xattr); v.screen = screen; v.visualid = XVisualIDFromVisual(xattr.visual); @@ -532,10 +560,7 @@ X11_GL_CreateContext(_THIS, SDL_Window * window) context to grab the new context creation function */ GLXContext temp_context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True); - if (!temp_context) { - SDL_SetError("Could not create GL context"); - return NULL; - } else { + if (temp_context) { /* max 8 attributes plus terminator */ int attribs[9] = { GLX_CONTEXT_MAJOR_VERSION_ARB, @@ -609,7 +634,8 @@ X11_GL_CreateContext(_THIS, SDL_Window * window) XFree(vinfo); } XSync(display, False); - + XSetErrorHandler(handler); + if (!context) { SDL_SetError("Could not create GL context"); return NULL;