1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "glxbackend.h"
24 #include <GL/glxext.h>
27 #if !defined(CON_GL1) && !defined(CON_GL3)
28 #error "Valid GL contexts for GLX are: GL1, GL3"
31 // attributes for a double buffered framebuffer in RGBA format with at least 4 bits per color
32 static int configAttribs[] =
34 GLX_RENDER_TYPE, GLX_RGBA_BIT,
35 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
36 GLX_DOUBLEBUFFER, True,
44 // attributes for a GL3 context
45 static int contextAttribs[] =
47 GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
48 GLX_CONTEXT_MINOR_VERSION_ARB, 0,
49 GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
54 VisualID GLXBackend::initialize(Display *display)
56 if (this->display == NULL) { // this implies that the context is also unitialized
57 this->display = display;
58 // debugging: show version information
59 int glxMajor, glxMinor;
60 glXQueryVersion(display, &glxMajor, &glxMinor);
61 printf("Using GLX version: %d.%d\n", glxMajor, glxMinor);
62 if (glxMajor == 1 && glxMinor < 4) {
63 // glXChooseFBConfig and glXCreateNewContext require GLX 1.3; GLX_ARB_create_context requires GLX 1.4
64 die("Need at least GLX 1.4 to function properly\n");
66 // check for extension-based functions
67 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)resolveGLXFunction("GLX_MESA_swap_control", "glXSwapIntervalMESA");
68 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)resolveGLXFunction("GLX_EXT_swap_control", "glXSwapIntervalEXT");
69 funCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)resolveGLXFunction("GLX_ARB_create_context", "glXCreateContextAttribsARB");
70 // get the first usable framebuffer configuration
72 GLXFBConfig *configs = glXChooseFBConfig(display, DefaultScreen(display), configAttribs, &count);
74 die("Failed to choose framebuffer configuration\n");
80 XVisualInfo *vi = glXGetVisualFromFBConfig(display, config);
82 die("The GLXFBConfig I got is invalid\n");
84 VisualID visualid = vi->visualid;
89 bool GLXBackend::haveGLXExtension(const std::string &name)
91 assert(display != NULL);
92 std::string extensions = glXQueryExtensionsString(display, DefaultScreen(display));
93 return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
96 T_proc GLXBackend::resolveGLXFunction(const char *extension, const char *function)
98 if (!haveGLXExtension(extension)) return NULL;
99 T_proc f = glXGetProcAddress((const GLubyte*)function);
101 printf("%s is supported, using it for %s\n", extension, function);
105 void GLXBackend::createContext(Window window)
107 assert(display != NULL && context == None);
108 this->window = window;
109 // create context with that window
111 context = glXCreateNewContext(display, config, GLX_RGBA_TYPE, NULL, GL_TRUE);
113 if (!funCreateContextAttribsARB) {
114 die("Cannot create GL3 context: GLX_ARB_create_context not supported\n");
116 context = funCreateContextAttribsARB(display, config, NULL, GL_TRUE, contextAttribs);
119 die("Error creating context\n");
121 glXMakeCurrent(display, window, context);
122 assert(glXIsDirect(display, context));
123 printf("Using GL version: %s\n", glGetString(GL_VERSION));
124 // initialise GL utilities
125 resolveFunctionPointers((T_glGetProcAddress)glXGetProcAddress);
128 GLXBackend::~GLXBackend()
130 if (display != NULL) {
131 if (context != None) {
132 glXMakeCurrent(display, None, NULL);
133 glXDestroyContext(display, context);
138 void GLXBackend::swapBuffers() const
140 assert(context != None); // this implies the display is also initialized
141 glXSwapBuffers(display, window);
144 void GLXBackend::setSwapInterval(int i) const
146 assert(context != None);
147 // check if swap interval value is supported
149 die("Cannot set swap interval to %d, must not be negative\n", i);
152 if (funSwapIntervalExt)
153 funSwapIntervalExt(display, window, i);
154 else if (funSwapIntervalMesa)
155 funSwapIntervalMesa(i);
157 die("At least one of GLX_EXT_swap_control, GLX_MESA_swap_control must be supported by the system\n");