GLXContext CreateContext(Display *dpy, int screen, FBCONFIG config) { int pbSupport = QueryPbuffers(dpy, screen); #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { /* GLX 1.3 */ GLXContext c; c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, True); if (!c) { /* try indirect */ c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, False); } return c; } #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { GLXContext c; c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, True); if (!c) { c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, False); } return c; } #endif return 0; }
/** * Either use glXGetFBConfigAttrib() or glXGetFBConfigAttribSGIX() * to query an fbconfig attribute. */ static int GetFBConfigAttrib(Display *dpy, int screen, #if defined(GLX_VERSION_1_3) const GLXFBConfig config, #elif defined(GLX_SGIX_fbconfig) const GLXFBConfigSGIX config, #endif int attrib ) { int pbSupport = QueryPbuffers(dpy, screen); int value = 0; #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { /* ok */ if (glXGetFBConfigAttrib(dpy, config, attrib, &value) != 0) { value = 0; } return value; } /* fall-through */ #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { if (glXGetFBConfigAttribSGIX(dpy, config, attrib, &value) != 0) { value = 0; } return value; } #endif return value; }
/** * Create a Pbuffer. Use an X error handler to deal with potential * BadAlloc errors. * * Input: dpy - the X display * fbConfig - an FBConfig as returned by glXChooseFBConfigSGIX(). * width, height - size of pixel buffer to request, in pixels. * pbAttribs - list of optional pixel buffer attributes * Return: a Pbuffer or None. */ PBUFFER CreatePbuffer(Display *dpy, int screen, FBCONFIG config, int width, int height, Bool largest, Bool preserve) { int (*oldHandler)(Display *, XErrorEvent *); PBUFFER pBuffer = None; int pbSupport = QueryPbuffers(dpy, screen); /* Catch X protocol errors with our own error handler */ oldHandler = XSetErrorHandler(HandleXError); XErrorFlag = 0; #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { /* GLX 1.3 */ int attribs[100], i = 0; attribs[i++] = GLX_PBUFFER_WIDTH; attribs[i++] = width; attribs[i++] = GLX_PBUFFER_HEIGHT; attribs[i++] = height; attribs[i++] = GLX_PRESERVED_CONTENTS; attribs[i++] = preserve; attribs[i++] = GLX_LARGEST_PBUFFER; attribs[i++] = largest; attribs[i++] = 0; pBuffer = glXCreatePbuffer(dpy, config, attribs); } else #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { int attribs[100], i = 0; attribs[i++] = GLX_PRESERVED_CONTENTS; attribs[i++] = preserve; attribs[i++] = GLX_LARGEST_PBUFFER; attribs[i++] = largest; attribs[i++] = 0; pBuffer = glXCreateGLXPbufferSGIX(dpy, config, width, height, attribs); } else #endif { pBuffer = None; } /* Restore original X error handler */ (void) XSetErrorHandler(oldHandler); /* Return pbuffer (may be None) */ if (!XErrorFlag && pBuffer != None) { /*printf("config %d worked!\n", i);*/ return pBuffer; } else { return None; } }
FBCONFIG * ChooseFBConfig(Display *dpy, int screen, const int attribs[], int *nConfigs) { int pbSupport = QueryPbuffers(dpy, screen); #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { return glXChooseFBConfig(dpy, screen, attribs, nConfigs); } #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { return glXChooseFBConfigSGIX(dpy, screen, (int *) attribs, nConfigs); } #endif return NULL; }
XVisualInfo * GetVisualFromFBConfig(Display *dpy, int screen, FBCONFIG config) { int pbSupport = QueryPbuffers(dpy, screen); #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { return glXGetVisualFromFBConfig(dpy, config); } #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { return glXGetVisualFromFBConfigSGIX(dpy, config); } #endif return NULL; }
void DestroyPbuffer(Display *dpy, int screen, PBUFFER pbuffer) { int pbSupport = QueryPbuffers(dpy, screen); #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { glXDestroyPbuffer(dpy, pbuffer); return; } #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { glXDestroyGLXPbufferSGIX(dpy, pbuffer); return; } #endif }
FBCONFIG * GetAllFBConfigs(Display *dpy, int screen, int *nConfigs) { int pbSupport = QueryPbuffers(dpy, screen); #if defined(GLX_VERSION_1_3) if (pbSupport == 1) { return glXGetFBConfigs(dpy, screen, nConfigs); } #endif #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) if (pbSupport == 2) { /* The GLX_SGIX_fbconfig extensions says to pass NULL to get list * of all available configurations. */ return glXChooseFBConfigSGIX(dpy, screen, NULL, nConfigs); } #endif return NULL; }
/* * Do all the X / GLX setup stuff. */ static int Setup(int width, int height) { #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) XVisualInfo *visInfo; GLXContext glCtx; /* Open the X display */ gDpy = XOpenDisplay(NULL); if (!gDpy) { printf("Error: couldn't open default X display.\n"); return 0; } /* Get default screen */ gScreen = DefaultScreen(gDpy); /* Test that pbuffers are available */ if (!QueryPbuffers(gDpy, gScreen)) { printf("Error: pbuffers not available on this screen\n"); XCloseDisplay(gDpy); return 0; } /* Create Pbuffer */ gPBuffer = MakePbuffer( gDpy, gScreen, width, height ); if (gPBuffer==None) { printf("Error: couldn't create pbuffer\n"); XCloseDisplay(gDpy); return 0; } /* Get corresponding XVisualInfo */ visInfo = glXGetVisualFromFBConfigSGIX(gDpy, gFBconfig); if (!visInfo) { printf("Error: can't get XVisualInfo from FBconfig\n"); XCloseDisplay(gDpy); return 0; } /* Create GLX context */ glCtx = glXCreateContext(gDpy, visInfo, NULL, True); if (!glCtx) { /* try indirect */ glCtx = glXCreateContext(gDpy, visInfo, NULL, False); if (!glCtx) { printf("Error: Couldn't create GLXContext\n"); XFree(visInfo); XCloseDisplay(gDpy); return 0; } else { printf("Warning: using indirect GLXContext\n"); } } /* Bind context to pbuffer */ if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) { printf("Error: glXMakeCurrent failed\n"); XFree(visInfo); XCloseDisplay(gDpy); return 0; } return 1; /* Success!! */ #else printf("Error: GLX_SGIX_fbconfig and/or GLX_SGIX_pbuffer extensions not" " available at compile-time.\n"); return 0; #endif }