// build like this: // c++ glxtest.cpp -o glxtest -ldl -lX11 // run like this: // ./glxtest // Example output: // VENDOR // Tungsten Graphics, Inc // RENDERER // Mesa DRI Intel(R) Sandybridge Mobile // VERSION // 3.0 Mesa 8.0.4 // TFP // TRUE #include #include #include #include #include #include #include #include "X11/Xlib.h" #include "X11/Xutil.h" // stuff from glx.h typedef struct __GLXcontextRec *GLXContext; typedef XID GLXPixmap; typedef XID GLXDrawable; /* GLX 1.3 and later */ typedef struct __GLXFBConfigRec *GLXFBConfig; typedef XID GLXFBConfigID; typedef XID GLXContextID; typedef XID GLXWindow; typedef XID GLXPbuffer; #define GLX_RGBA 4 #define GLX_RED_SIZE 8 #define GLX_GREEN_SIZE 9 #define GLX_BLUE_SIZE 10 // stuff from gl.h typedef uint8_t GLubyte; typedef uint32_t GLenum; #define GL_VENDOR 0x1F00 #define GL_RENDERER 0x1F01 #define GL_VERSION 0x1F02 // the write end of the pipe, which we're going to write to // XXX for the standalone version, this is replaced by stderr (2) static int write_end_of_the_pipe = 2; // C++ standard collides with C standard in that it doesn't allow casting void* to function pointer types. // So the work-around is to convert first to size_t. // http://www.trilithium.com/johan/2004/12/problem-with-dlsym/ template static func_ptr_type cast(void *ptr) { return reinterpret_cast( reinterpret_cast(ptr) ); } static void fatal_error(const char *str) { write(write_end_of_the_pipe, str, strlen(str)); write(write_end_of_the_pipe, "\n", 1); exit(EXIT_FAILURE); } static int x_error_handler(Display *, XErrorEvent *ev) { enum { bufsize = 1024 }; char buf[bufsize]; int length = snprintf(buf, bufsize, "X error occurred in GLX probe, error_code=%d, request_code=%d, minor_code=%d\n", ev->error_code, ev->request_code, ev->minor_code); write(write_end_of_the_pipe, buf, length); exit(EXIT_FAILURE); return 0; } static void glxtest() { ///// Open libGL and load needed symbols ///// #ifdef __OpenBSD__ #define LIBGL_FILENAME "libGL.so" #else #define LIBGL_FILENAME "libGL.so.1" #endif void *libgl = dlopen(LIBGL_FILENAME, RTLD_LAZY); if (!libgl) fatal_error("Unable to load " LIBGL_FILENAME); typedef void* (* PFNGLXGETPROCADDRESS) (const char *); PFNGLXGETPROCADDRESS glXGetProcAddress = cast(dlsym(libgl, "glXGetProcAddress")); if (!glXGetProcAddress) fatal_error("Unable to find glXGetProcAddress in " LIBGL_FILENAME); typedef GLXFBConfig* (* PFNGLXQUERYEXTENSION) (Display *, int *, int *); PFNGLXQUERYEXTENSION glXQueryExtension = cast(glXGetProcAddress("glXQueryExtension")); typedef GLXFBConfig* (* PFNGLXQUERYVERSION) (Display *, int *, int *); PFNGLXQUERYVERSION glXQueryVersion = cast(dlsym(libgl, "glXQueryVersion")); typedef XVisualInfo* (* PFNGLXCHOOSEVISUAL) (Display *, int, int *); PFNGLXCHOOSEVISUAL glXChooseVisual = cast(glXGetProcAddress("glXChooseVisual")); typedef GLXContext (* PFNGLXCREATECONTEXT) (Display *, XVisualInfo *, GLXContext, Bool); PFNGLXCREATECONTEXT glXCreateContext = cast(glXGetProcAddress("glXCreateContext")); typedef Bool (* PFNGLXMAKECURRENT) (Display*, GLXDrawable, GLXContext); PFNGLXMAKECURRENT glXMakeCurrent = cast(glXGetProcAddress("glXMakeCurrent")); typedef void (* PFNGLXDESTROYCONTEXT) (Display*, GLXContext); PFNGLXDESTROYCONTEXT glXDestroyContext = cast(glXGetProcAddress("glXDestroyContext")); typedef GLubyte* (* PFNGLGETSTRING) (GLenum); PFNGLGETSTRING glGetString = cast(glXGetProcAddress("glGetString")); if (!glXQueryExtension || !glXQueryVersion || !glXChooseVisual || !glXCreateContext || !glXMakeCurrent || !glXDestroyContext || !glGetString) { fatal_error("glXGetProcAddress couldn't find required functions"); } ///// Open a connection to the X server ///// Display *dpy = XOpenDisplay(NULL); if (!dpy) fatal_error("Unable to open a connection to the X server"); ///// Check that the GLX extension is present ///// if (!glXQueryExtension(dpy, NULL, NULL)) fatal_error("GLX extension missing"); XSetErrorHandler(x_error_handler); ///// Get a visual ///// int attribs[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None }; XVisualInfo *vInfo = glXChooseVisual(dpy, DefaultScreen(dpy), attribs); if (!vInfo) fatal_error("No visuals found"); // using a X11 Window instead of a GLXPixmap does not crash // fglrx in indirect rendering. bug 680644 Window window; XSetWindowAttributes swa; swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vInfo->screen), vInfo->visual, AllocNone); swa.border_pixel = 0; window = XCreateWindow(dpy, RootWindow(dpy, vInfo->screen), 0, 0, 16, 16, 0, vInfo->depth, InputOutput, vInfo->visual, CWBorderPixel | CWColormap, &swa); ///// Get a GL context and make it current ////// GLXContext context = glXCreateContext(dpy, vInfo, NULL, True); glXMakeCurrent(dpy, window, context); ///// Look for this symbol to determine texture_from_pixmap support ///// void* glXBindTexImageEXT = glXGetProcAddress("glXBindTexImageEXT"); ///// Get GL vendor/renderer/versions strings ///// enum { bufsize = 1024 }; char buf[bufsize]; const GLubyte *vendorString = glGetString(GL_VENDOR); const GLubyte *rendererString = glGetString(GL_RENDERER); const GLubyte *versionString = glGetString(GL_VERSION); if (!vendorString || !rendererString || !versionString) fatal_error("glGetString returned null"); int length = snprintf(buf, bufsize, "VENDOR\n%s\nRENDERER\n%s\nVERSION\n%s\nTFP\n%s\n", vendorString, rendererString, versionString, glXBindTexImageEXT ? "TRUE" : "FALSE"); if (length >= bufsize) fatal_error("GL strings length too large for buffer size"); ///// Clean up. Indeed, the parent process might fail to kill us (e.g. if it doesn't need to check GL info) ///// so we might be staying alive for longer than expected, so it's important to consume as little memory as ///// possible. Also we want to check that we're able to do that too without generating X errors. glXMakeCurrent(dpy, None, NULL); // must release the GL context before destroying it glXDestroyContext(dpy, context); XDestroyWindow(dpy, window); XFreeColormap(dpy, swa.colormap); XCloseDisplay(dpy); dlclose(libgl); ///// Finally write data to the pipe write(write_end_of_the_pipe, buf, length); } int main(int argc, char **argv) { glxtest(); return 0; }