e40e90c5ba99dd23ad193a3e715f1e5e19c08d7b
[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 <stdlib.h>
25 #include <GL/glxext.h>
26 #include <string>
27
28 #if !defined(CON_GL1) && !defined(CON_GL3)
29 #error "Valid GL contexts for GLX are: GL1, GL3"
30 #endif
31
32 // attributes for a double buffered framebuffer in RGBA format with at least 4 bits per color
33 static int configAttribs[] =                                             
34 {
35         GLX_RENDER_TYPE, GLX_RGBA_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 || (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");
65                         exit(1);
66                 }
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
72                 int count = 0;
73                 GLXFBConfig *configs = glXChooseFBConfig(display, DefaultScreen(display), configAttribs, &count);
74                 if (count < 1) {
75                         fprintf(stderr, "Failed to choose framebuffer configuration\n");
76                         exit(1);
77                 }
78                 config = configs[0];
79                 XFree(configs);
80         }
81         // return visual ID
82         XVisualInfo *vi = glXGetVisualFromFBConfig(display, config);
83         VisualID visualid = vi->visualid;
84         XFree(vi);
85         return visualid;
86 }
87
88 bool GLXBackend::haveGLXExtension(const std::string &name)
89 {
90         assert(display != NULL);
91         std::string extensions = glXQueryExtensionsString(display, DefaultScreen(display));
92         return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
93 }
94
95 T_proc GLXBackend::resolveGLXFunction(const char *extension, const char *function)
96 {
97         if (!haveGLXExtension(extension)) return NULL;
98         T_proc f = glXGetProcAddress((const GLubyte*)function);
99         if (f)
100                 printf("%s is supported, using it for %s\n", extension, function);
101         return f;
102 }
103
104 void GLXBackend::createContext(Window window)
105 {
106         assert(display != NULL && context == None);
107         this->window = window;
108         // create context with that window
109 #ifdef CON_GL1
110         context = glXCreateNewContext(display, config, GLX_RGBA_TYPE, NULL, GL_TRUE);
111 #else
112         if (!funCreateContextAttribsARB) {
113                 fprintf(stderr, "Cannot create GL3 context: GLX_ARB_create_context not supported\n");
114                 exit(1);
115         }
116         context = funCreateContextAttribsARB(display, config, NULL, GL_TRUE, contextAttribs);
117 #endif
118         if (!context) {
119                 fprintf(stderr, "Error creating context\n");
120                 exit(1);
121         }
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);
127 }
128
129 GLXBackend::~GLXBackend()
130 {
131         if (display != NULL) {
132                 if (context != None) {
133                         glXMakeCurrent(display, None, NULL);
134                         glXDestroyContext(display, context);
135                 }
136         }
137 }
138
139 void GLXBackend::swapBuffers() const
140 {
141         assert(context != None); // this implies the display is also initialized
142         glXSwapBuffers(display, window);
143 }
144
145 void GLXBackend::setSwapInterval(int i) const
146 {
147         assert(context != None);
148         // check if swap interval value is supported
149         if (i < 0) {
150                 fprintf(stderr, "Cannot set swap interval to %d, must not be negative\n", i);
151                 exit(1);
152         }
153         // set it
154         if (funSwapIntervalExt)
155                 funSwapIntervalExt(display, window, i);
156         else if (funSwapIntervalMesa)
157                 funSwapIntervalMesa(i);
158         else {
159                 fprintf(stderr, "At least one of GLX_EXT_swap_control, GLX_MESA_swap_control must be supported by the system\n");
160                 abort();
161         }
162 }