1 //========================================================================
2 // GLFW 3.4 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2016 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 // Please use C89 style variable declarations in this file because VS 2010
28 //========================================================================
39 //////////////////////////////////////////////////////////////////////////
40 ////// GLFW internal API //////
41 //////////////////////////////////////////////////////////////////////////
43 // Checks whether the desired context attributes are valid
45 // This function checks things like whether the specified client API version
46 // exists and whether all relevant options have supported and non-conflicting
49 GLFWbool _glfwIsValidContextConfig(const _GLFWctxconfig* ctxconfig)
53 if (ctxconfig->client == GLFW_NO_API ||
54 ctxconfig->share->context.client == GLFW_NO_API)
56 _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
61 if (ctxconfig->source != GLFW_NATIVE_CONTEXT_API &&
62 ctxconfig->source != GLFW_EGL_CONTEXT_API &&
63 ctxconfig->source != GLFW_OSMESA_CONTEXT_API)
65 _glfwInputError(GLFW_INVALID_ENUM,
66 "Invalid context creation API 0x%08X",
71 if (ctxconfig->client != GLFW_NO_API &&
72 ctxconfig->client != GLFW_OPENGL_API &&
73 ctxconfig->client != GLFW_OPENGL_ES_API)
75 _glfwInputError(GLFW_INVALID_ENUM,
76 "Invalid client API 0x%08X",
81 if (ctxconfig->client == GLFW_OPENGL_API)
83 if ((ctxconfig->major < 1 || ctxconfig->minor < 0) ||
84 (ctxconfig->major == 1 && ctxconfig->minor > 5) ||
85 (ctxconfig->major == 2 && ctxconfig->minor > 1) ||
86 (ctxconfig->major == 3 && ctxconfig->minor > 3))
88 // OpenGL 1.0 is the smallest valid version
89 // OpenGL 1.x series ended with version 1.5
90 // OpenGL 2.x series ended with version 2.1
91 // OpenGL 3.x series ended with version 3.3
92 // For now, let everything else through
94 _glfwInputError(GLFW_INVALID_VALUE,
95 "Invalid OpenGL version %i.%i",
96 ctxconfig->major, ctxconfig->minor);
100 if (ctxconfig->profile)
102 if (ctxconfig->profile != GLFW_OPENGL_CORE_PROFILE &&
103 ctxconfig->profile != GLFW_OPENGL_COMPAT_PROFILE)
105 _glfwInputError(GLFW_INVALID_ENUM,
106 "Invalid OpenGL profile 0x%08X",
111 if (ctxconfig->major <= 2 ||
112 (ctxconfig->major == 3 && ctxconfig->minor < 2))
114 // Desktop OpenGL context profiles are only defined for version 3.2
117 _glfwInputError(GLFW_INVALID_VALUE,
118 "Context profiles are only defined for OpenGL version 3.2 and above");
123 if (ctxconfig->forward && ctxconfig->major <= 2)
125 // Forward-compatible contexts are only defined for OpenGL version 3.0 and above
126 _glfwInputError(GLFW_INVALID_VALUE,
127 "Forward-compatibility is only defined for OpenGL version 3.0 and above");
131 else if (ctxconfig->client == GLFW_OPENGL_ES_API)
133 if (ctxconfig->major < 1 || ctxconfig->minor < 0 ||
134 (ctxconfig->major == 1 && ctxconfig->minor > 1) ||
135 (ctxconfig->major == 2 && ctxconfig->minor > 0))
137 // OpenGL ES 1.0 is the smallest valid version
138 // OpenGL ES 1.x series ended with version 1.1
139 // OpenGL ES 2.x series ended with version 2.0
140 // For now, let everything else through
142 _glfwInputError(GLFW_INVALID_VALUE,
143 "Invalid OpenGL ES version %i.%i",
144 ctxconfig->major, ctxconfig->minor);
149 if (ctxconfig->robustness)
151 if (ctxconfig->robustness != GLFW_NO_RESET_NOTIFICATION &&
152 ctxconfig->robustness != GLFW_LOSE_CONTEXT_ON_RESET)
154 _glfwInputError(GLFW_INVALID_ENUM,
155 "Invalid context robustness mode 0x%08X",
156 ctxconfig->robustness);
161 if (ctxconfig->release)
163 if (ctxconfig->release != GLFW_RELEASE_BEHAVIOR_NONE &&
164 ctxconfig->release != GLFW_RELEASE_BEHAVIOR_FLUSH)
166 _glfwInputError(GLFW_INVALID_ENUM,
167 "Invalid context release behavior 0x%08X",
176 // Chooses the framebuffer config that best matches the desired one
178 const _GLFWfbconfig* _glfwChooseFBConfig(const _GLFWfbconfig* desired,
179 const _GLFWfbconfig* alternatives,
183 unsigned int missing, leastMissing = UINT_MAX;
184 unsigned int colorDiff, leastColorDiff = UINT_MAX;
185 unsigned int extraDiff, leastExtraDiff = UINT_MAX;
186 const _GLFWfbconfig* current;
187 const _GLFWfbconfig* closest = NULL;
189 for (i = 0; i < count; i++)
191 current = alternatives + i;
193 if (desired->stereo > 0 && current->stereo == 0)
195 // Stereo is a hard constraint
199 if (desired->doublebuffer != current->doublebuffer)
201 // Double buffering is a hard constraint
205 // Count number of missing buffers
209 if (desired->alphaBits > 0 && current->alphaBits == 0)
212 if (desired->depthBits > 0 && current->depthBits == 0)
215 if (desired->stencilBits > 0 && current->stencilBits == 0)
218 if (desired->auxBuffers > 0 &&
219 current->auxBuffers < desired->auxBuffers)
221 missing += desired->auxBuffers - current->auxBuffers;
224 if (desired->samples > 0 && current->samples == 0)
226 // Technically, several multisampling buffers could be
227 // involved, but that's a lower level implementation detail and
228 // not important to us here, so we count them as one
232 if (desired->transparent != current->transparent)
236 // These polynomials make many small channel size differences matter
237 // less than one large channel size difference
239 // Calculate color channel size difference value
243 if (desired->redBits != GLFW_DONT_CARE)
245 colorDiff += (desired->redBits - current->redBits) *
246 (desired->redBits - current->redBits);
249 if (desired->greenBits != GLFW_DONT_CARE)
251 colorDiff += (desired->greenBits - current->greenBits) *
252 (desired->greenBits - current->greenBits);
255 if (desired->blueBits != GLFW_DONT_CARE)
257 colorDiff += (desired->blueBits - current->blueBits) *
258 (desired->blueBits - current->blueBits);
262 // Calculate non-color channel size difference value
266 if (desired->alphaBits != GLFW_DONT_CARE)
268 extraDiff += (desired->alphaBits - current->alphaBits) *
269 (desired->alphaBits - current->alphaBits);
272 if (desired->depthBits != GLFW_DONT_CARE)
274 extraDiff += (desired->depthBits - current->depthBits) *
275 (desired->depthBits - current->depthBits);
278 if (desired->stencilBits != GLFW_DONT_CARE)
280 extraDiff += (desired->stencilBits - current->stencilBits) *
281 (desired->stencilBits - current->stencilBits);
284 if (desired->accumRedBits != GLFW_DONT_CARE)
286 extraDiff += (desired->accumRedBits - current->accumRedBits) *
287 (desired->accumRedBits - current->accumRedBits);
290 if (desired->accumGreenBits != GLFW_DONT_CARE)
292 extraDiff += (desired->accumGreenBits - current->accumGreenBits) *
293 (desired->accumGreenBits - current->accumGreenBits);
296 if (desired->accumBlueBits != GLFW_DONT_CARE)
298 extraDiff += (desired->accumBlueBits - current->accumBlueBits) *
299 (desired->accumBlueBits - current->accumBlueBits);
302 if (desired->accumAlphaBits != GLFW_DONT_CARE)
304 extraDiff += (desired->accumAlphaBits - current->accumAlphaBits) *
305 (desired->accumAlphaBits - current->accumAlphaBits);
308 if (desired->samples != GLFW_DONT_CARE)
310 extraDiff += (desired->samples - current->samples) *
311 (desired->samples - current->samples);
314 if (desired->sRGB && !current->sRGB)
318 // Figure out if the current one is better than the best one found so far
319 // Least number of missing buffers is the most important heuristic,
320 // then color buffer size match and lastly size match for other buffers
322 if (missing < leastMissing)
324 else if (missing == leastMissing)
326 if ((colorDiff < leastColorDiff) ||
327 (colorDiff == leastColorDiff && extraDiff < leastExtraDiff))
333 if (current == closest)
335 leastMissing = missing;
336 leastColorDiff = colorDiff;
337 leastExtraDiff = extraDiff;
344 // Retrieves the attributes of the current context
346 GLFWbool _glfwRefreshContextAttribs(_GLFWwindow* window,
347 const _GLFWctxconfig* ctxconfig)
350 _GLFWwindow* previous;
352 const char* prefixes[] =
360 window->context.source = ctxconfig->source;
361 window->context.client = GLFW_OPENGL_API;
363 previous = _glfwPlatformGetTls(&_glfw.contextSlot);
364 glfwMakeContextCurrent((GLFWwindow*) window);
366 window->context.GetIntegerv = (PFNGLGETINTEGERVPROC)
367 window->context.getProcAddress("glGetIntegerv");
368 window->context.GetString = (PFNGLGETSTRINGPROC)
369 window->context.getProcAddress("glGetString");
370 if (!window->context.GetIntegerv || !window->context.GetString)
372 _glfwInputError(GLFW_PLATFORM_ERROR, "Entry point retrieval is broken");
373 glfwMakeContextCurrent((GLFWwindow*) previous);
377 version = (const char*) window->context.GetString(GL_VERSION);
380 if (ctxconfig->client == GLFW_OPENGL_API)
382 _glfwInputError(GLFW_PLATFORM_ERROR,
383 "OpenGL version string retrieval is broken");
387 _glfwInputError(GLFW_PLATFORM_ERROR,
388 "OpenGL ES version string retrieval is broken");
391 glfwMakeContextCurrent((GLFWwindow*) previous);
395 for (i = 0; prefixes[i]; i++)
397 const size_t length = strlen(prefixes[i]);
399 if (strncmp(version, prefixes[i], length) == 0)
402 window->context.client = GLFW_OPENGL_ES_API;
407 if (!sscanf(version, "%d.%d.%d",
408 &window->context.major,
409 &window->context.minor,
410 &window->context.revision))
412 if (window->context.client == GLFW_OPENGL_API)
414 _glfwInputError(GLFW_PLATFORM_ERROR,
415 "No version found in OpenGL version string");
419 _glfwInputError(GLFW_PLATFORM_ERROR,
420 "No version found in OpenGL ES version string");
423 glfwMakeContextCurrent((GLFWwindow*) previous);
427 if (window->context.major < ctxconfig->major ||
428 (window->context.major == ctxconfig->major &&
429 window->context.minor < ctxconfig->minor))
431 // The desired OpenGL version is greater than the actual version
432 // This only happens if the machine lacks {GLX|WGL}_ARB_create_context
433 // /and/ the user has requested an OpenGL version greater than 1.0
435 // For API consistency, we emulate the behavior of the
436 // {GLX|WGL}_ARB_create_context extension and fail here
438 if (window->context.client == GLFW_OPENGL_API)
440 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
441 "Requested OpenGL version %i.%i, got version %i.%i",
442 ctxconfig->major, ctxconfig->minor,
443 window->context.major, window->context.minor);
447 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
448 "Requested OpenGL ES version %i.%i, got version %i.%i",
449 ctxconfig->major, ctxconfig->minor,
450 window->context.major, window->context.minor);
453 glfwMakeContextCurrent((GLFWwindow*) previous);
457 if (window->context.major >= 3)
459 // OpenGL 3.0+ uses a different function for extension string retrieval
460 // We cache it here instead of in glfwExtensionSupported mostly to alert
461 // users as early as possible that their build may be broken
463 window->context.GetStringi = (PFNGLGETSTRINGIPROC)
464 window->context.getProcAddress("glGetStringi");
465 if (!window->context.GetStringi)
467 _glfwInputError(GLFW_PLATFORM_ERROR,
468 "Entry point retrieval is broken");
469 glfwMakeContextCurrent((GLFWwindow*) previous);
474 if (window->context.client == GLFW_OPENGL_API)
476 // Read back context flags (OpenGL 3.0 and above)
477 if (window->context.major >= 3)
480 window->context.GetIntegerv(GL_CONTEXT_FLAGS, &flags);
482 if (flags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT)
483 window->context.forward = GLFW_TRUE;
485 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
486 window->context.debug = GLFW_TRUE;
487 else if (glfwExtensionSupported("GL_ARB_debug_output") &&
490 // HACK: This is a workaround for older drivers (pre KHR_debug)
491 // not setting the debug bit in the context flags for
493 window->context.debug = GLFW_TRUE;
496 if (flags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR)
497 window->context.noerror = GLFW_TRUE;
500 // Read back OpenGL context profile (OpenGL 3.2 and above)
501 if (window->context.major >= 4 ||
502 (window->context.major == 3 && window->context.minor >= 2))
505 window->context.GetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
507 if (mask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT)
508 window->context.profile = GLFW_OPENGL_COMPAT_PROFILE;
509 else if (mask & GL_CONTEXT_CORE_PROFILE_BIT)
510 window->context.profile = GLFW_OPENGL_CORE_PROFILE;
511 else if (glfwExtensionSupported("GL_ARB_compatibility"))
513 // HACK: This is a workaround for the compatibility profile bit
514 // not being set in the context flags if an OpenGL 3.2+
515 // context was created without having requested a specific
517 window->context.profile = GLFW_OPENGL_COMPAT_PROFILE;
521 // Read back robustness strategy
522 if (glfwExtensionSupported("GL_ARB_robustness"))
524 // NOTE: We avoid using the context flags for detection, as they are
525 // only present from 3.0 while the extension applies from 1.1
528 window->context.GetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
531 if (strategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
532 window->context.robustness = GLFW_LOSE_CONTEXT_ON_RESET;
533 else if (strategy == GL_NO_RESET_NOTIFICATION_ARB)
534 window->context.robustness = GLFW_NO_RESET_NOTIFICATION;
539 // Read back robustness strategy
540 if (glfwExtensionSupported("GL_EXT_robustness"))
542 // NOTE: The values of these constants match those of the OpenGL ARB
543 // one, so we can reuse them here
546 window->context.GetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
549 if (strategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
550 window->context.robustness = GLFW_LOSE_CONTEXT_ON_RESET;
551 else if (strategy == GL_NO_RESET_NOTIFICATION_ARB)
552 window->context.robustness = GLFW_NO_RESET_NOTIFICATION;
556 if (glfwExtensionSupported("GL_KHR_context_flush_control"))
559 window->context.GetIntegerv(GL_CONTEXT_RELEASE_BEHAVIOR, &behavior);
561 if (behavior == GL_NONE)
562 window->context.release = GLFW_RELEASE_BEHAVIOR_NONE;
563 else if (behavior == GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH)
564 window->context.release = GLFW_RELEASE_BEHAVIOR_FLUSH;
567 // Clearing the front buffer to black to avoid garbage pixels left over from
568 // previous uses of our bit of VRAM
570 PFNGLCLEARPROC glClear = (PFNGLCLEARPROC)
571 window->context.getProcAddress("glClear");
572 glClear(GL_COLOR_BUFFER_BIT);
573 window->context.swapBuffers(window);
576 glfwMakeContextCurrent((GLFWwindow*) previous);
580 // Searches an extension string for the specified extension
582 GLFWbool _glfwStringInExtensionString(const char* string, const char* extensions)
584 const char* start = extensions;
589 const char* terminator;
591 where = strstr(start, string);
595 terminator = where + strlen(string);
596 if (where == start || *(where - 1) == ' ')
598 if (*terminator == ' ' || *terminator == '\0')
609 //////////////////////////////////////////////////////////////////////////
610 ////// GLFW public API //////
611 //////////////////////////////////////////////////////////////////////////
613 GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle)
615 _GLFWwindow* window = (_GLFWwindow*) handle;
616 _GLFWwindow* previous = _glfwPlatformGetTls(&_glfw.contextSlot);
618 _GLFW_REQUIRE_INIT();
620 if (window && window->context.client == GLFW_NO_API)
622 _glfwInputError(GLFW_NO_WINDOW_CONTEXT,
623 "Cannot make current with a window that has no OpenGL or OpenGL ES context");
629 if (!window || window->context.source != previous->context.source)
630 previous->context.makeCurrent(NULL);
634 window->context.makeCurrent(window);
637 GLFWAPI GLFWwindow* glfwGetCurrentContext(void)
639 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
640 return _glfwPlatformGetTls(&_glfw.contextSlot);
643 GLFWAPI void glfwSwapBuffers(GLFWwindow* handle)
645 _GLFWwindow* window = (_GLFWwindow*) handle;
646 assert(window != NULL);
648 _GLFW_REQUIRE_INIT();
650 if (window->context.client == GLFW_NO_API)
652 _glfwInputError(GLFW_NO_WINDOW_CONTEXT,
653 "Cannot swap buffers of a window that has no OpenGL or OpenGL ES context");
657 window->context.swapBuffers(window);
660 GLFWAPI void glfwSwapInterval(int interval)
664 _GLFW_REQUIRE_INIT();
666 window = _glfwPlatformGetTls(&_glfw.contextSlot);
669 _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
670 "Cannot set swap interval without a current OpenGL or OpenGL ES context");
674 window->context.swapInterval(interval);
677 GLFWAPI int glfwExtensionSupported(const char* extension)
680 assert(extension != NULL);
682 _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
684 window = _glfwPlatformGetTls(&_glfw.contextSlot);
687 _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
688 "Cannot query extension without a current OpenGL or OpenGL ES context");
692 if (*extension == '\0')
694 _glfwInputError(GLFW_INVALID_VALUE, "Extension name cannot be an empty string");
698 if (window->context.major >= 3)
703 // Check if extension is in the modern OpenGL extensions string list
705 window->context.GetIntegerv(GL_NUM_EXTENSIONS, &count);
707 for (i = 0; i < count; i++)
709 const char* en = (const char*)
710 window->context.GetStringi(GL_EXTENSIONS, i);
713 _glfwInputError(GLFW_PLATFORM_ERROR,
714 "Extension string retrieval is broken");
718 if (strcmp(en, extension) == 0)
724 // Check if extension is in the old style OpenGL extensions string
726 const char* extensions = (const char*)
727 window->context.GetString(GL_EXTENSIONS);
730 _glfwInputError(GLFW_PLATFORM_ERROR,
731 "Extension string retrieval is broken");
735 if (_glfwStringInExtensionString(extension, extensions))
739 // Check if extension is in the platform-specific string
740 return window->context.extensionSupported(extension);
743 GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname)
746 assert(procname != NULL);
748 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
750 window = _glfwPlatformGetTls(&_glfw.contextSlot);
753 _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
754 "Cannot query entry point without a current OpenGL or OpenGL ES context");
758 return window->context.getProcAddress(procname);