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"
25 #include <GL/glxext.h>
28 #if !defined(CON_GL1) && !defined(CON_GL3)
29 #error "Valid GL contexts for GLX are: GL1, GL3"
32 // attributes for a double buffered framebuffer in RGBA format with at least 4 bits per color
33 static int configAttribs[] =
35 GLX_RENDER_TYPE, GLX_RGBA_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 || (glxMajor == 1 && glxMinor < 3)) {
63 // glXChooseFBConfig and glXCreateNewContext require GLX 1.3
64 fprintf(stderr, "Need at least GLX 1.3 to function properly\n");
67 // check for extension-based functions
68 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)resolveGLXFunction("GLX_MESA_swap_control", "glXSwapIntervalMESA");
69 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)resolveGLXFunction("GLX_EXT_swap_control", "glXSwapIntervalEXT");
70 funCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)resolveGLXFunction("GLX_ARB_create_context", "glXCreateContextAttribsARB");
71 // get the first usable framebuffer configuration
73 GLXFBConfig *configs = glXChooseFBConfig(display, DefaultScreen(display), configAttribs, &count);
75 fprintf(stderr, "Failed to choose framebuffer configuration\n");
82 XVisualInfo *vi = glXGetVisualFromFBConfig(display, config);
83 VisualID visualid = vi->visualid;
88 bool GLXBackend::haveGLXExtension(const std::string &name)
90 assert(display != NULL);
91 std::string extensions = glXQueryExtensionsString(display, DefaultScreen(display));
92 return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
95 T_proc GLXBackend::resolveGLXFunction(const char *extension, const char *function)
97 if (!haveGLXExtension(extension)) return NULL;
98 T_proc f = glXGetProcAddress((const GLubyte*)function);
100 printf("%s is supported, using it for %s\n", extension, function);
104 void GLXBackend::createContext(Window window)
106 assert(display != NULL && context == None);
107 this->window = window;
108 // create context with that window
110 context = glXCreateNewContext(display, config, GLX_RGBA_TYPE, NULL, GL_TRUE);
112 if (!funCreateContextAttribsARB) {
113 fprintf(stderr, "Cannot create GL3 context: GLX_ARB_create_context not supported\n");
116 context = funCreateContextAttribsARB(display, config, NULL, GL_TRUE, contextAttribs);
119 fprintf(stderr, "Error creating context\n");
122 glXMakeCurrent(display, window, context);
123 assert(glXIsDirect(display, context));
124 printf("Using GL version: %s\n", glGetString(GL_VERSION));
125 // initialise GL utilities
126 resolveFunctionPointers((T_glGetProcAddress)glXGetProcAddress);
129 GLXBackend::~GLXBackend()
131 if (display != NULL) {
132 if (context != None) {
133 glXMakeCurrent(display, None, NULL);
134 glXDestroyContext(display, context);
139 void GLXBackend::swapBuffers() const
141 assert(context != None); // this implies the display is also initialized
142 glXSwapBuffers(display, window);
145 void GLXBackend::setSwapInterval(int i) const
147 assert(context != None);
148 // check if swap interval value is supported
150 fprintf(stderr, "Cannot set swap interval to %d, must not be negative\n", i);
154 if (funSwapIntervalExt)
155 funSwapIntervalExt(display, window, i);
156 else if (funSwapIntervalMesa)
157 funSwapIntervalMesa(i);
159 fprintf(stderr, "At least one of GLX_EXT_swap_control, GLX_MESA_swap_control must be supported by the system\n");