1 //========================================================================
2 // GLFW 3.4 GLX - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
26 //========================================================================
27 // It is fine to use C99 in this file because it will not be built with VS
28 //========================================================================
36 #ifndef GLXBadProfileARB
37 #define GLXBadProfileARB 13
41 // Returns the specified attribute of the specified GLXFBConfig
43 static int getGLXFBConfigAttrib(GLXFBConfig fbconfig, int attrib)
46 glXGetFBConfigAttrib(_glfw.x11.display, fbconfig, attrib, &value);
50 // Return the GLXFBConfig most closely matching the specified hints
52 static GLFWbool chooseGLXFBConfig(const _GLFWfbconfig* desired,
55 GLXFBConfig* nativeConfigs;
56 _GLFWfbconfig* usableConfigs;
57 const _GLFWfbconfig* closest;
58 int i, nativeCount, usableCount;
60 GLFWbool trustWindowBit = GLFW_TRUE;
62 // HACK: This is a (hopefully temporary) workaround for Chromium
63 // (VirtualBox GL) not setting the window bit on any GLXFBConfigs
64 vendor = glXGetClientString(_glfw.x11.display, GLX_VENDOR);
65 if (vendor && strcmp(vendor, "Chromium") == 0)
66 trustWindowBit = GLFW_FALSE;
69 glXGetFBConfigs(_glfw.x11.display, _glfw.x11.screen, &nativeCount);
70 if (!nativeConfigs || !nativeCount)
72 _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: No GLXFBConfigs returned");
76 usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig));
79 for (i = 0; i < nativeCount; i++)
81 const GLXFBConfig n = nativeConfigs[i];
82 _GLFWfbconfig* u = usableConfigs + usableCount;
84 // Only consider RGBA GLXFBConfigs
85 if (!(getGLXFBConfigAttrib(n, GLX_RENDER_TYPE) & GLX_RGBA_BIT))
88 // Only consider window GLXFBConfigs
89 if (!(getGLXFBConfigAttrib(n, GLX_DRAWABLE_TYPE) & GLX_WINDOW_BIT))
95 if (desired->transparent)
97 XVisualInfo* vi = glXGetVisualFromFBConfig(_glfw.x11.display, n);
100 u->transparent = _glfwIsVisualTransparentX11(vi->visual);
105 u->redBits = getGLXFBConfigAttrib(n, GLX_RED_SIZE);
106 u->greenBits = getGLXFBConfigAttrib(n, GLX_GREEN_SIZE);
107 u->blueBits = getGLXFBConfigAttrib(n, GLX_BLUE_SIZE);
109 u->alphaBits = getGLXFBConfigAttrib(n, GLX_ALPHA_SIZE);
110 u->depthBits = getGLXFBConfigAttrib(n, GLX_DEPTH_SIZE);
111 u->stencilBits = getGLXFBConfigAttrib(n, GLX_STENCIL_SIZE);
113 u->accumRedBits = getGLXFBConfigAttrib(n, GLX_ACCUM_RED_SIZE);
114 u->accumGreenBits = getGLXFBConfigAttrib(n, GLX_ACCUM_GREEN_SIZE);
115 u->accumBlueBits = getGLXFBConfigAttrib(n, GLX_ACCUM_BLUE_SIZE);
116 u->accumAlphaBits = getGLXFBConfigAttrib(n, GLX_ACCUM_ALPHA_SIZE);
118 u->auxBuffers = getGLXFBConfigAttrib(n, GLX_AUX_BUFFERS);
120 if (getGLXFBConfigAttrib(n, GLX_STEREO))
121 u->stereo = GLFW_TRUE;
122 if (getGLXFBConfigAttrib(n, GLX_DOUBLEBUFFER))
123 u->doublebuffer = GLFW_TRUE;
125 if (_glfw.glx.ARB_multisample)
126 u->samples = getGLXFBConfigAttrib(n, GLX_SAMPLES);
128 if (_glfw.glx.ARB_framebuffer_sRGB || _glfw.glx.EXT_framebuffer_sRGB)
129 u->sRGB = getGLXFBConfigAttrib(n, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB);
131 u->handle = (uintptr_t) n;
135 closest = _glfwChooseFBConfig(desired, usableConfigs, usableCount);
137 *result = (GLXFBConfig) closest->handle;
139 XFree(nativeConfigs);
142 return closest != NULL;
145 // Create the OpenGL context using legacy API
147 static GLXContext createLegacyContextGLX(_GLFWwindow* window,
148 GLXFBConfig fbconfig,
151 return glXCreateNewContext(_glfw.x11.display,
158 static void makeContextCurrentGLX(_GLFWwindow* window)
162 if (!glXMakeCurrent(_glfw.x11.display,
163 window->context.glx.window,
164 window->context.glx.handle))
166 _glfwInputError(GLFW_PLATFORM_ERROR,
167 "GLX: Failed to make context current");
173 if (!glXMakeCurrent(_glfw.x11.display, None, NULL))
175 _glfwInputError(GLFW_PLATFORM_ERROR,
176 "GLX: Failed to clear current context");
181 _glfwPlatformSetTls(&_glfw.contextSlot, window);
184 static void swapBuffersGLX(_GLFWwindow* window)
186 glXSwapBuffers(_glfw.x11.display, window->context.glx.window);
189 static void swapIntervalGLX(int interval)
191 _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
193 if (_glfw.glx.EXT_swap_control)
195 _glfw.glx.SwapIntervalEXT(_glfw.x11.display,
196 window->context.glx.window,
199 else if (_glfw.glx.MESA_swap_control)
200 _glfw.glx.SwapIntervalMESA(interval);
201 else if (_glfw.glx.SGI_swap_control)
204 _glfw.glx.SwapIntervalSGI(interval);
208 static int extensionSupportedGLX(const char* extension)
210 const char* extensions =
211 glXQueryExtensionsString(_glfw.x11.display, _glfw.x11.screen);
214 if (_glfwStringInExtensionString(extension, extensions))
221 static GLFWglproc getProcAddressGLX(const char* procname)
223 if (_glfw.glx.GetProcAddress)
224 return _glfw.glx.GetProcAddress((const GLubyte*) procname);
225 else if (_glfw.glx.GetProcAddressARB)
226 return _glfw.glx.GetProcAddressARB((const GLubyte*) procname);
228 return _glfw_dlsym(_glfw.glx.handle, procname);
231 static void destroyContextGLX(_GLFWwindow* window)
233 if (window->context.glx.window)
235 glXDestroyWindow(_glfw.x11.display, window->context.glx.window);
236 window->context.glx.window = None;
239 if (window->context.glx.handle)
241 glXDestroyContext(_glfw.x11.display, window->context.glx.handle);
242 window->context.glx.handle = NULL;
247 //////////////////////////////////////////////////////////////////////////
248 ////// GLFW internal API //////
249 //////////////////////////////////////////////////////////////////////////
253 GLFWbool _glfwInitGLX(void)
256 const char* sonames[] =
258 #if defined(_GLFW_GLX_LIBRARY)
260 #elif defined(__CYGWIN__)
269 if (_glfw.glx.handle)
272 for (i = 0; sonames[i]; i++)
274 _glfw.glx.handle = _glfw_dlopen(sonames[i]);
275 if (_glfw.glx.handle)
279 if (!_glfw.glx.handle)
281 _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: Failed to load GLX");
285 _glfw.glx.GetFBConfigs =
286 _glfw_dlsym(_glfw.glx.handle, "glXGetFBConfigs");
287 _glfw.glx.GetFBConfigAttrib =
288 _glfw_dlsym(_glfw.glx.handle, "glXGetFBConfigAttrib");
289 _glfw.glx.GetClientString =
290 _glfw_dlsym(_glfw.glx.handle, "glXGetClientString");
291 _glfw.glx.QueryExtension =
292 _glfw_dlsym(_glfw.glx.handle, "glXQueryExtension");
293 _glfw.glx.QueryVersion =
294 _glfw_dlsym(_glfw.glx.handle, "glXQueryVersion");
295 _glfw.glx.DestroyContext =
296 _glfw_dlsym(_glfw.glx.handle, "glXDestroyContext");
297 _glfw.glx.MakeCurrent =
298 _glfw_dlsym(_glfw.glx.handle, "glXMakeCurrent");
299 _glfw.glx.SwapBuffers =
300 _glfw_dlsym(_glfw.glx.handle, "glXSwapBuffers");
301 _glfw.glx.QueryExtensionsString =
302 _glfw_dlsym(_glfw.glx.handle, "glXQueryExtensionsString");
303 _glfw.glx.CreateNewContext =
304 _glfw_dlsym(_glfw.glx.handle, "glXCreateNewContext");
305 _glfw.glx.CreateWindow =
306 _glfw_dlsym(_glfw.glx.handle, "glXCreateWindow");
307 _glfw.glx.DestroyWindow =
308 _glfw_dlsym(_glfw.glx.handle, "glXDestroyWindow");
309 _glfw.glx.GetProcAddress =
310 _glfw_dlsym(_glfw.glx.handle, "glXGetProcAddress");
311 _glfw.glx.GetProcAddressARB =
312 _glfw_dlsym(_glfw.glx.handle, "glXGetProcAddressARB");
313 _glfw.glx.GetVisualFromFBConfig =
314 _glfw_dlsym(_glfw.glx.handle, "glXGetVisualFromFBConfig");
316 if (!_glfw.glx.GetFBConfigs ||
317 !_glfw.glx.GetFBConfigAttrib ||
318 !_glfw.glx.GetClientString ||
319 !_glfw.glx.QueryExtension ||
320 !_glfw.glx.QueryVersion ||
321 !_glfw.glx.DestroyContext ||
322 !_glfw.glx.MakeCurrent ||
323 !_glfw.glx.SwapBuffers ||
324 !_glfw.glx.QueryExtensionsString ||
325 !_glfw.glx.CreateNewContext ||
326 !_glfw.glx.CreateWindow ||
327 !_glfw.glx.DestroyWindow ||
328 !_glfw.glx.GetProcAddress ||
329 !_glfw.glx.GetProcAddressARB ||
330 !_glfw.glx.GetVisualFromFBConfig)
332 _glfwInputError(GLFW_PLATFORM_ERROR,
333 "GLX: Failed to load required entry points");
337 if (!glXQueryExtension(_glfw.x11.display,
338 &_glfw.glx.errorBase,
339 &_glfw.glx.eventBase))
341 _glfwInputError(GLFW_API_UNAVAILABLE, "GLX: GLX extension not found");
345 if (!glXQueryVersion(_glfw.x11.display, &_glfw.glx.major, &_glfw.glx.minor))
347 _glfwInputError(GLFW_API_UNAVAILABLE,
348 "GLX: Failed to query GLX version");
352 if (_glfw.glx.major == 1 && _glfw.glx.minor < 3)
354 _glfwInputError(GLFW_API_UNAVAILABLE,
355 "GLX: GLX version 1.3 is required");
359 if (extensionSupportedGLX("GLX_EXT_swap_control"))
361 _glfw.glx.SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)
362 getProcAddressGLX("glXSwapIntervalEXT");
364 if (_glfw.glx.SwapIntervalEXT)
365 _glfw.glx.EXT_swap_control = GLFW_TRUE;
368 if (extensionSupportedGLX("GLX_SGI_swap_control"))
370 _glfw.glx.SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)
371 getProcAddressGLX("glXSwapIntervalSGI");
373 if (_glfw.glx.SwapIntervalSGI)
374 _glfw.glx.SGI_swap_control = GLFW_TRUE;
377 if (extensionSupportedGLX("GLX_MESA_swap_control"))
379 _glfw.glx.SwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)
380 getProcAddressGLX("glXSwapIntervalMESA");
382 if (_glfw.glx.SwapIntervalMESA)
383 _glfw.glx.MESA_swap_control = GLFW_TRUE;
386 if (extensionSupportedGLX("GLX_ARB_multisample"))
387 _glfw.glx.ARB_multisample = GLFW_TRUE;
389 if (extensionSupportedGLX("GLX_ARB_framebuffer_sRGB"))
390 _glfw.glx.ARB_framebuffer_sRGB = GLFW_TRUE;
392 if (extensionSupportedGLX("GLX_EXT_framebuffer_sRGB"))
393 _glfw.glx.EXT_framebuffer_sRGB = GLFW_TRUE;
395 if (extensionSupportedGLX("GLX_ARB_create_context"))
397 _glfw.glx.CreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
398 getProcAddressGLX("glXCreateContextAttribsARB");
400 if (_glfw.glx.CreateContextAttribsARB)
401 _glfw.glx.ARB_create_context = GLFW_TRUE;
404 if (extensionSupportedGLX("GLX_ARB_create_context_robustness"))
405 _glfw.glx.ARB_create_context_robustness = GLFW_TRUE;
407 if (extensionSupportedGLX("GLX_ARB_create_context_profile"))
408 _glfw.glx.ARB_create_context_profile = GLFW_TRUE;
410 if (extensionSupportedGLX("GLX_EXT_create_context_es2_profile"))
411 _glfw.glx.EXT_create_context_es2_profile = GLFW_TRUE;
413 if (extensionSupportedGLX("GLX_ARB_create_context_no_error"))
414 _glfw.glx.ARB_create_context_no_error = GLFW_TRUE;
416 if (extensionSupportedGLX("GLX_ARB_context_flush_control"))
417 _glfw.glx.ARB_context_flush_control = GLFW_TRUE;
424 void _glfwTerminateGLX(void)
426 // NOTE: This function must not call any X11 functions, as it is called
427 // after XCloseDisplay (see _glfwPlatformTerminate for details)
429 if (_glfw.glx.handle)
431 _glfw_dlclose(_glfw.glx.handle);
432 _glfw.glx.handle = NULL;
436 #define setAttrib(a, v) \
438 assert(((size_t) index + 1) < sizeof(attribs) / sizeof(attribs[0])); \
439 attribs[index++] = a; \
440 attribs[index++] = v; \
443 // Create the OpenGL or OpenGL ES context
445 GLFWbool _glfwCreateContextGLX(_GLFWwindow* window,
446 const _GLFWctxconfig* ctxconfig,
447 const _GLFWfbconfig* fbconfig)
450 GLXFBConfig native = NULL;
451 GLXContext share = NULL;
453 if (ctxconfig->share)
454 share = ctxconfig->share->context.glx.handle;
456 if (!chooseGLXFBConfig(fbconfig, &native))
458 _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
459 "GLX: Failed to find a suitable GLXFBConfig");
463 if (ctxconfig->client == GLFW_OPENGL_ES_API)
465 if (!_glfw.glx.ARB_create_context ||
466 !_glfw.glx.ARB_create_context_profile ||
467 !_glfw.glx.EXT_create_context_es2_profile)
469 _glfwInputError(GLFW_API_UNAVAILABLE,
470 "GLX: OpenGL ES requested but GLX_EXT_create_context_es2_profile is unavailable");
475 if (ctxconfig->forward)
477 if (!_glfw.glx.ARB_create_context)
479 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
480 "GLX: Forward compatibility requested but GLX_ARB_create_context_profile is unavailable");
485 if (ctxconfig->profile)
487 if (!_glfw.glx.ARB_create_context ||
488 !_glfw.glx.ARB_create_context_profile)
490 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
491 "GLX: An OpenGL profile requested but GLX_ARB_create_context_profile is unavailable");
496 _glfwGrabErrorHandlerX11();
498 if (_glfw.glx.ARB_create_context)
500 int index = 0, mask = 0, flags = 0;
502 if (ctxconfig->client == GLFW_OPENGL_API)
504 if (ctxconfig->forward)
505 flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
507 if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
508 mask |= GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
509 else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
510 mask |= GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
513 mask |= GLX_CONTEXT_ES2_PROFILE_BIT_EXT;
515 if (ctxconfig->debug)
516 flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
518 if (ctxconfig->robustness)
520 if (_glfw.glx.ARB_create_context_robustness)
522 if (ctxconfig->robustness == GLFW_NO_RESET_NOTIFICATION)
524 setAttrib(GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
525 GLX_NO_RESET_NOTIFICATION_ARB);
527 else if (ctxconfig->robustness == GLFW_LOSE_CONTEXT_ON_RESET)
529 setAttrib(GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
530 GLX_LOSE_CONTEXT_ON_RESET_ARB);
533 flags |= GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB;
537 if (ctxconfig->release)
539 if (_glfw.glx.ARB_context_flush_control)
541 if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_NONE)
543 setAttrib(GLX_CONTEXT_RELEASE_BEHAVIOR_ARB,
544 GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB);
546 else if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_FLUSH)
548 setAttrib(GLX_CONTEXT_RELEASE_BEHAVIOR_ARB,
549 GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB);
554 if (ctxconfig->noerror)
556 if (_glfw.glx.ARB_create_context_no_error)
557 setAttrib(GLX_CONTEXT_OPENGL_NO_ERROR_ARB, GLFW_TRUE);
560 // NOTE: Only request an explicitly versioned context when necessary, as
561 // explicitly requesting version 1.0 does not always return the
562 // highest version supported by the driver
563 if (ctxconfig->major != 1 || ctxconfig->minor != 0)
565 setAttrib(GLX_CONTEXT_MAJOR_VERSION_ARB, ctxconfig->major);
566 setAttrib(GLX_CONTEXT_MINOR_VERSION_ARB, ctxconfig->minor);
570 setAttrib(GLX_CONTEXT_PROFILE_MASK_ARB, mask);
573 setAttrib(GLX_CONTEXT_FLAGS_ARB, flags);
575 setAttrib(None, None);
577 window->context.glx.handle =
578 _glfw.glx.CreateContextAttribsARB(_glfw.x11.display,
584 // HACK: This is a fallback for broken versions of the Mesa
585 // implementation of GLX_ARB_create_context_profile that fail
586 // default 1.0 context creation with a GLXBadProfileARB error in
587 // violation of the extension spec
588 if (!window->context.glx.handle)
590 if (_glfw.x11.errorCode == _glfw.glx.errorBase + GLXBadProfileARB &&
591 ctxconfig->client == GLFW_OPENGL_API &&
592 ctxconfig->profile == GLFW_OPENGL_ANY_PROFILE &&
593 ctxconfig->forward == GLFW_FALSE)
595 window->context.glx.handle =
596 createLegacyContextGLX(window, native, share);
602 window->context.glx.handle =
603 createLegacyContextGLX(window, native, share);
606 _glfwReleaseErrorHandlerX11();
608 if (!window->context.glx.handle)
610 _glfwInputErrorX11(GLFW_VERSION_UNAVAILABLE, "GLX: Failed to create context");
614 window->context.glx.window =
615 glXCreateWindow(_glfw.x11.display, native, window->x11.handle, NULL);
616 if (!window->context.glx.window)
618 _glfwInputError(GLFW_PLATFORM_ERROR, "GLX: Failed to create window");
622 window->context.makeCurrent = makeContextCurrentGLX;
623 window->context.swapBuffers = swapBuffersGLX;
624 window->context.swapInterval = swapIntervalGLX;
625 window->context.extensionSupported = extensionSupportedGLX;
626 window->context.getProcAddress = getProcAddressGLX;
627 window->context.destroy = destroyContextGLX;
634 // Returns the Visual and depth of the chosen GLXFBConfig
636 GLFWbool _glfwChooseVisualGLX(const _GLFWwndconfig* wndconfig,
637 const _GLFWctxconfig* ctxconfig,
638 const _GLFWfbconfig* fbconfig,
639 Visual** visual, int* depth)
644 if (!chooseGLXFBConfig(fbconfig, &native))
646 _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
647 "GLX: Failed to find a suitable GLXFBConfig");
651 result = glXGetVisualFromFBConfig(_glfw.x11.display, native);
654 _glfwInputError(GLFW_PLATFORM_ERROR,
655 "GLX: Failed to retrieve Visual for GLXFBConfig");
659 *visual = result->visual;
660 *depth = result->depth;
667 //////////////////////////////////////////////////////////////////////////
668 ////// GLFW native API //////
669 //////////////////////////////////////////////////////////////////////////
671 GLFWAPI GLXContext glfwGetGLXContext(GLFWwindow* handle)
673 _GLFWwindow* window = (_GLFWwindow*) handle;
674 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
676 if (window->context.client == GLFW_NO_API)
678 _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
682 return window->context.glx.handle;
685 GLFWAPI GLXWindow glfwGetGLXWindow(GLFWwindow* handle)
687 _GLFWwindow* window = (_GLFWwindow*) handle;
688 _GLFW_REQUIRE_INIT_OR_RETURN(None);
690 if (window->context.client == GLFW_NO_API)
692 _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
696 return window->context.glx.window;