]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/glx/glcontext.cpp
54541e969802216d19b51fe79ee40d05555c0471
[libs/gui.git] / source / graphics / glx / glcontext.cpp
1 #include <set>
2 #include <vector>
3 #include <GL/glx.h>
4 #include <GL/glxext.h>
5 #include <msp/strings/utils.h>
6 #include "glcontext.h"
7 #include "display_private.h"
8 #include "window_private.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace Graphics {
14
15 typedef GLXContext ContextHandle;
16
17 struct GLContext::Private
18 {
19         ContextHandle context;
20         // We need to create a window with the chosen visual
21         WindowHandle subwnd;
22         GLXWindow glxwnd;
23 };
24
25
26 void GLContext::platform_init(const GLOptions &opts)
27 {
28         DisplayHandle dpy = display.get_private().display;
29         int event_base;
30         int error_base;
31         if(!glXQueryExtension(dpy, &event_base, &error_base))
32                 throw runtime_error("glX not found");
33
34         int major, minor;
35         glXQueryVersion(dpy, &major, &minor);
36
37         set<string> extensions;
38         if(major>1 || (major==1 && minor>=1))
39         {
40                 if(const char *ext_str = glXQueryExtensionsString(dpy, DefaultScreen(dpy)))
41                 {
42                         vector<string> exts = split(ext_str);
43                         extensions.insert(exts.begin(), exts.end());
44                 }
45         }
46
47         if(major>1 || (major==1 && minor>=3))
48         {
49                 vector<int> fb_attribs;
50
51                 fb_attribs.push_back(GLX_DRAWABLE_TYPE);
52                 fb_attribs.push_back(GLX_WINDOW_BIT);
53
54                 fb_attribs.push_back(GLX_RENDER_TYPE);
55                 fb_attribs.push_back(GLX_RGBA_BIT);
56
57                 fb_attribs.push_back(GLX_DEPTH_SIZE);
58                 fb_attribs.push_back(1);
59
60                 if(opts.alpha)
61                 {
62                         fb_attribs.push_back(GLX_ALPHA_SIZE);
63                         fb_attribs.push_back(1);
64                 }
65
66                 if(opts.stencil)
67                 {
68                         fb_attribs.push_back(GLX_STENCIL_SIZE);
69                         fb_attribs.push_back(1);
70                 }
71
72                 fb_attribs.push_back(GLX_DOUBLEBUFFER);
73                 fb_attribs.push_back(opts.doublebuffer);
74
75                 if(opts.multisample>0)
76                 {
77                         fb_attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
78                         fb_attribs.push_back(1);
79                         fb_attribs.push_back(GLX_SAMPLES_ARB);
80                         fb_attribs.push_back(opts.multisample);
81                 }
82
83                 fb_attribs.push_back(0);
84
85                 int n_configs = 0;
86                 GLXFBConfig *fb_configs = glXChooseFBConfig(dpy, DefaultScreen(dpy), &fb_attribs[0], &n_configs);
87                 if(!fb_configs)
88                         throw unsupported_gl_mode(opts);
89
90                 XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fb_configs[0]);
91
92                 XSetWindowAttributes attr;
93                 attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
94
95                 priv = new Private;
96                 priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
97                 XMapWindow(dpy, priv->subwnd);
98
99                 priv->glxwnd = glXCreateWindow(dpy, fb_configs[0], priv->subwnd, 0);
100
101                 if(opts.forward_compatible || opts.gl_version_major)
102                 {
103                         if(!extensions.count("GLX_ARB_create_context") || !extensions.count("GLX_ARB_get_proc_address"))
104                                 throw unsupported_gl_mode(opts);
105
106                         vector<int> ctx_attribs;
107
108                         ctx_attribs.push_back(GLX_RENDER_TYPE);
109                         ctx_attribs.push_back(GLX_RGBA_TYPE);
110
111                         if(opts.forward_compatible)
112                         {
113                                 ctx_attribs.push_back(GLX_CONTEXT_FLAGS_ARB);
114                                 ctx_attribs.push_back(GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
115                         }
116
117                         if(opts.gl_version_major)
118                         {
119                                 ctx_attribs.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB);
120                                 ctx_attribs.push_back(opts.gl_version_major);
121                                 ctx_attribs.push_back(GLX_CONTEXT_MINOR_VERSION_ARB);
122                                 ctx_attribs.push_back(opts.gl_version_minor);
123                         }
124
125                         ctx_attribs.push_back(0);
126
127                         const GLubyte *name = reinterpret_cast<const GLubyte *>("glXCreateContextAttribsARB");
128                         PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(glXGetProcAddressARB(name));
129                         priv->context = glXCreateContextAttribs(dpy, fb_configs[0], 0, true, &ctx_attribs[0]);
130                 }
131                 else
132                         priv->context = glXCreateNewContext(dpy, fb_configs[0], GLX_RGBA_TYPE, 0, true);
133
134                 XFree(vi);
135                 XFree(fb_configs);
136
137                 glXMakeContextCurrent(dpy, priv->glxwnd, priv->glxwnd, priv->context);
138         }
139         else if(opts.forward_compatible || opts.gl_version_major)
140                 throw unsupported_gl_mode(opts);
141         else
142         {
143                 vector<int> attribs;
144
145                 attribs.push_back(GLX_RGBA);
146                 attribs.push_back(GLX_DEPTH_SIZE);
147                 attribs.push_back(1);
148
149                 if(opts.alpha)
150                 {
151                         attribs.push_back(GLX_ALPHA_SIZE);
152                         attribs.push_back(1);
153                 }
154
155                 if(opts.stencil)
156                 {
157                         attribs.push_back(GLX_STENCIL_SIZE);
158                         attribs.push_back(1);
159                 }
160
161                 if(opts.doublebuffer)
162                         attribs.push_back(GLX_DOUBLEBUFFER);
163
164                 if(opts.multisample>0)
165                 {
166                         attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
167                         attribs.push_back(1);
168                         attribs.push_back(GLX_SAMPLES_ARB);
169                         attribs.push_back(opts.multisample);
170                 }
171
172                 attribs.push_back(0);
173
174                 XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
175                 if(!vi)
176                         throw unsupported_gl_mode(opts);
177
178                 priv = new Private;
179                 priv->glxwnd = 0;
180                 priv->context = glXCreateContext(dpy, vi, 0, true);
181
182                 XSetWindowAttributes attr;
183                 attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
184
185                 priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
186                 XMapWindow(dpy, priv->subwnd);
187
188                 XFree(vi);
189
190                 glXMakeCurrent(dpy, priv->subwnd, priv->context);
191         }
192 }
193
194 GLContext::~GLContext()
195 {
196         DisplayHandle dpy = display.get_private().display;
197
198         if(priv->glxwnd)
199         {
200                 glXMakeContextCurrent(dpy, 0, 0, 0);
201                 glXDestroyWindow(dpy, priv->glxwnd);
202         }
203         else
204                 glXMakeCurrent(dpy, 0, 0);
205         glXDestroyContext(dpy, priv->context);
206         XDestroyWindow(dpy, priv->subwnd);
207
208         delete priv;
209 }
210
211 void GLContext::set_swap_interval(unsigned i)
212 {
213         const GLubyte *name = reinterpret_cast<const GLubyte *>("glXSwapIntervalEXT");
214         PFNGLXSWAPINTERVALEXTPROC glXSwapInterval = reinterpret_cast<PFNGLXSWAPINTERVALEXTPROC>(glXGetProcAddress(name));
215         if(!glXSwapInterval)
216                 throw runtime_error("glXSwapIntervalEXT not found");
217         glXSwapInterval(display.get_private().display, (priv->glxwnd ? priv->glxwnd : priv->subwnd), i);
218 }
219
220 void GLContext::swap_buffers()
221 {
222         glXSwapBuffers(display.get_private().display, (priv->glxwnd ? priv->glxwnd : priv->subwnd));
223 }
224
225 void GLContext::window_resized(unsigned w, unsigned h)
226 {
227         XMoveResizeWindow(display.get_private().display, priv->subwnd, 0, 0, w, h);
228 }
229
230 } // namespace Graphics
231 } // namespace Msp