5 #include <msp/strings/utils.h>
7 #include "display_private.h"
8 #include "window_private.h"
15 typedef GLXContext ContextHandle;
17 struct GLContext::Private
19 ContextHandle context;
20 // We need to create a window with the chosen visual
26 void GLContext::platform_init(const GLOptions &opts)
28 DisplayHandle dpy = display.get_private().display;
31 if(!glXQueryExtension(dpy, &event_base, &error_base))
32 throw runtime_error("glX not found");
35 glXQueryVersion(dpy, &major, &minor);
37 set<string> extensions;
38 if(major>1 || (major==1 && minor>=1))
40 if(const char *ext_str = glXQueryExtensionsString(dpy, DefaultScreen(dpy)))
42 vector<string> exts = split(ext_str);
43 extensions.insert(exts.begin(), exts.end());
47 if(major>1 || (major==1 && minor>=3))
49 vector<int> fb_attribs;
51 fb_attribs.push_back(GLX_DRAWABLE_TYPE);
52 fb_attribs.push_back(GLX_WINDOW_BIT);
54 fb_attribs.push_back(GLX_RENDER_TYPE);
55 fb_attribs.push_back(GLX_RGBA_BIT);
57 fb_attribs.push_back(GLX_DEPTH_SIZE);
58 fb_attribs.push_back(1);
62 fb_attribs.push_back(GLX_ALPHA_SIZE);
63 fb_attribs.push_back(1);
68 fb_attribs.push_back(GLX_STENCIL_SIZE);
69 fb_attribs.push_back(1);
72 fb_attribs.push_back(GLX_DOUBLEBUFFER);
73 fb_attribs.push_back(opts.doublebuffer);
75 if(opts.multisample>0)
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);
83 fb_attribs.push_back(0);
86 GLXFBConfig *fb_configs = glXChooseFBConfig(dpy, DefaultScreen(dpy), &fb_attribs[0], &n_configs);
88 throw unsupported_gl_mode(opts);
90 XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fb_configs[0]);
92 XSetWindowAttributes attr;
93 attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
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);
99 priv->glxwnd = glXCreateWindow(dpy, fb_configs[0], priv->subwnd, 0);
101 if(opts.forward_compatible || opts.gl_version_major)
103 if(!extensions.count("GLX_ARB_create_context") || !extensions.count("GLX_ARB_get_proc_address"))
104 throw unsupported_gl_mode(opts);
106 vector<int> ctx_attribs;
108 ctx_attribs.push_back(GLX_RENDER_TYPE);
109 ctx_attribs.push_back(GLX_RGBA_TYPE);
111 if(opts.forward_compatible)
113 ctx_attribs.push_back(GLX_CONTEXT_FLAGS_ARB);
114 ctx_attribs.push_back(GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
117 if(opts.gl_version_major)
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);
125 ctx_attribs.push_back(0);
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]);
132 priv->context = glXCreateNewContext(dpy, fb_configs[0], GLX_RGBA_TYPE, 0, true);
137 glXMakeContextCurrent(dpy, priv->glxwnd, priv->glxwnd, priv->context);
139 else if(opts.forward_compatible || opts.gl_version_major)
140 throw unsupported_gl_mode(opts);
145 attribs.push_back(GLX_RGBA);
146 attribs.push_back(GLX_DEPTH_SIZE);
147 attribs.push_back(1);
151 attribs.push_back(GLX_ALPHA_SIZE);
152 attribs.push_back(1);
157 attribs.push_back(GLX_STENCIL_SIZE);
158 attribs.push_back(1);
161 if(opts.doublebuffer)
162 attribs.push_back(GLX_DOUBLEBUFFER);
164 if(opts.multisample>0)
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);
172 attribs.push_back(0);
174 XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
176 throw unsupported_gl_mode(opts);
180 priv->context = glXCreateContext(dpy, vi, 0, true);
182 XSetWindowAttributes attr;
183 attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
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);
190 glXMakeCurrent(dpy, priv->subwnd, priv->context);
194 GLContext::~GLContext()
196 DisplayHandle dpy = display.get_private().display;
200 glXMakeContextCurrent(dpy, 0, 0, 0);
201 glXDestroyWindow(dpy, priv->glxwnd);
204 glXMakeCurrent(dpy, 0, 0);
205 glXDestroyContext(dpy, priv->context);
206 XDestroyWindow(dpy, priv->subwnd);
211 void GLContext::set_swap_interval(unsigned i)
213 const GLubyte *name = reinterpret_cast<const GLubyte *>("glXSwapIntervalEXT");
214 PFNGLXSWAPINTERVALEXTPROC func = reinterpret_cast<PFNGLXSWAPINTERVALEXTPROC>(glXGetProcAddress(name));
216 throw runtime_error("glXSwapIntervalEXT not found");
217 func(display.get_private().display, (priv->glxwnd ? priv->glxwnd : priv->subwnd), i);
220 void GLContext::swap_buffers()
222 glXSwapBuffers(display.get_private().display, (priv->glxwnd ? priv->glxwnd : priv->subwnd));
225 void GLContext::window_resized(unsigned w, unsigned h)
227 XMoveResizeWindow(display.get_private().display, priv->subwnd, 0, 0, w, h);
230 } // namespace Graphics