a5c9e0c84911384d693c98d4a8c9680da0dec6ad
[gltest.git] / glxbackend.cpp
1 /* gltest - small OpenGL tearing test program
2  * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "glxbackend.h"
20 #include "glutil.h"
21
22 #include <stdio.h>
23 #include <assert.h>
24 #include <GL/glxext.h>
25 #include <string>
26
27 #if !defined(CON_GL1) && !defined(CON_GL3)
28 #error "Valid GL contexts for GLX are: GL1, GL3"
29 #endif
30
31 // attributes for a double buffered framebuffer in RGBA format with at least 4 bits per color
32 static int configAttribs[] =                                             
33 {
34         GLX_RENDER_TYPE, GLX_RGBA_BIT,
35         GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
36         GLX_DOUBLEBUFFER, True,
37         GLX_RED_SIZE, 4,
38         GLX_GREEN_SIZE, 4,
39         GLX_BLUE_SIZE, 4,
40         None
41 };
42
43 #ifdef CON_GL3
44 // attributes for a GL3 context
45 static int contextAttribs[] =
46 {
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,
50         None
51 };
52 #endif
53
54 VisualID GLXBackend::initialize(Display *display)
55 {
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");
65                 }
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
71                 int count = 0;
72                 GLXFBConfig *configs = glXChooseFBConfig(display, DefaultScreen(display), configAttribs, &count);
73                 if (count < 1) {
74                         die("Failed to choose framebuffer configuration\n");
75                 }
76                 config = configs[0];
77                 XFree(configs);
78         }
79         // return visual ID
80         XVisualInfo *vi = glXGetVisualFromFBConfig(display, config);
81         if (vi== NULL) {
82                 die("The GLXFBConfig I got is invalid\n");
83         }
84         VisualID visualid = vi->visualid;
85         XFree(vi);
86         return visualid;
87 }
88
89 bool GLXBackend::haveGLXExtension(const std::string &name)
90 {
91         assert(display != NULL);
92         std::string extensions = glXQueryExtensionsString(display, DefaultScreen(display));
93         return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
94 }
95
96 T_proc GLXBackend::resolveGLXFunction(const char *extension, const char *function)
97 {
98         if (!haveGLXExtension(extension)) return NULL;
99         T_proc f = glXGetProcAddress((const GLubyte*)function);
100         if (f)
101                 printf("%s is supported, using it for %s\n", extension, function);
102         return f;
103 }
104
105 void GLXBackend::createContext(Window window)
106 {
107         assert(display != NULL && context == None);
108         this->window = window;
109         // create context with that window
110 #ifdef CON_GL1
111         context = glXCreateNewContext(display, config, GLX_RGBA_TYPE, NULL, GL_TRUE);
112 #else
113         if (!funCreateContextAttribsARB) {
114                 die("Cannot create GL3 context: GLX_ARB_create_context not supported\n");
115         }
116         context = funCreateContextAttribsARB(display, config, NULL, GL_TRUE, contextAttribs);
117 #endif
118         if (!context) {
119                 die("Error creating context\n");
120         }
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);
126 }
127
128 GLXBackend::~GLXBackend()
129 {
130         if (display != NULL) {
131                 if (context != None) {
132                         glXMakeCurrent(display, None, NULL);
133                         glXDestroyContext(display, context);
134                 }
135         }
136 }
137
138 void GLXBackend::swapBuffers() const
139 {
140         assert(context != None); // this implies the display is also initialized
141         glXSwapBuffers(display, window);
142 }
143
144 void GLXBackend::setSwapInterval(int i) const
145 {
146         assert(context != None);
147         // check if swap interval value is supported
148         if (i < 0) {
149                 die("Cannot set swap interval to %d, must not be negative\n", i);
150         }
151         // set it
152         if (funSwapIntervalExt)
153                 funSwapIntervalExt(display, window, i);
154         else if (funSwapIntervalMesa)
155                 funSwapIntervalMesa(i);
156         else {
157                 die("At least one of GLX_EXT_swap_control, GLX_MESA_swap_control must be supported by the system\n");
158         }
159 }