1 //========================================================================
2 // GLFW 3.3 WGL - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
6 //
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.
10 //
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:
14 //
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.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not
21 //    be misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 //    distribution.
25 //
26 //========================================================================
27 
28 #include "internal.h"
29 
30 #include <stdlib.h>
31 #include <malloc.h>
32 #include <assert.h>
33 
34 // Return the value corresponding to the specified attribute
35 //
findPixelFormatAttribValue(const int * attribs,int attribCount,const int * values,int attrib)36 static int findPixelFormatAttribValue(const int* attribs,
37                                       int attribCount,
38                                       const int* values,
39                                       int attrib)
40 {
41     int i;
42 
43     for (i = 0;  i < attribCount;  i++)
44     {
45         if (attribs[i] == attrib)
46             return values[i];
47     }
48 
49     _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
50                          "WGL: Unknown pixel format attribute requested");
51     return 0;
52 }
53 
54 #define addAttrib(a) \
55 { \
56     assert((size_t) attribCount < sizeof(attribs) / sizeof(attribs[0])); \
57     attribs[attribCount++] = a; \
58 }
59 #define findAttribValue(a) \
60     findPixelFormatAttribValue(attribs, attribCount, values, a)
61 
62 // Return a list of available and usable framebuffer configs
63 //
choosePixelFormat(_GLFWwindow * window,const _GLFWctxconfig * ctxconfig,const _GLFWfbconfig * fbconfig)64 static int choosePixelFormat(_GLFWwindow* window,
65                              const _GLFWctxconfig* ctxconfig,
66                              const _GLFWfbconfig* fbconfig)
67 {
68     _GLFWfbconfig* usableConfigs;
69     const _GLFWfbconfig* closest;
70     int i, pixelFormat, nativeCount, usableCount = 0, attribCount = 0;
71     int attribs[40];
72     int values[sizeof(attribs) / sizeof(attribs[0])];
73 
74     if (_glfw.wgl.ARB_pixel_format)
75     {
76         const int attrib = WGL_NUMBER_PIXEL_FORMATS_ARB;
77 
78         if (!_glfw.wgl.GetPixelFormatAttribivARB(window->context.wgl.dc,
79                                                  1, 0, 1, &attrib, &nativeCount))
80         {
81             _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
82                                  "WGL: Failed to retrieve pixel format attribute");
83             return 0;
84         }
85 
86         addAttrib(WGL_SUPPORT_OPENGL_ARB);
87         addAttrib(WGL_DRAW_TO_WINDOW_ARB);
88         addAttrib(WGL_PIXEL_TYPE_ARB);
89         addAttrib(WGL_ACCELERATION_ARB);
90         addAttrib(WGL_RED_BITS_ARB);
91         addAttrib(WGL_RED_SHIFT_ARB);
92         addAttrib(WGL_GREEN_BITS_ARB);
93         addAttrib(WGL_GREEN_SHIFT_ARB);
94         addAttrib(WGL_BLUE_BITS_ARB);
95         addAttrib(WGL_BLUE_SHIFT_ARB);
96         addAttrib(WGL_ALPHA_BITS_ARB);
97         addAttrib(WGL_ALPHA_SHIFT_ARB);
98         addAttrib(WGL_DEPTH_BITS_ARB);
99         addAttrib(WGL_STENCIL_BITS_ARB);
100         addAttrib(WGL_ACCUM_BITS_ARB);
101         addAttrib(WGL_ACCUM_RED_BITS_ARB);
102         addAttrib(WGL_ACCUM_GREEN_BITS_ARB);
103         addAttrib(WGL_ACCUM_BLUE_BITS_ARB);
104         addAttrib(WGL_ACCUM_ALPHA_BITS_ARB);
105         addAttrib(WGL_AUX_BUFFERS_ARB);
106         addAttrib(WGL_STEREO_ARB);
107         addAttrib(WGL_DOUBLE_BUFFER_ARB);
108 
109         if (_glfw.wgl.ARB_multisample)
110             addAttrib(WGL_SAMPLES_ARB);
111 
112         if (ctxconfig->client == GLFW_OPENGL_API)
113         {
114             if (_glfw.wgl.ARB_framebuffer_sRGB || _glfw.wgl.EXT_framebuffer_sRGB)
115                 addAttrib(WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB);
116         }
117         else
118         {
119             if (_glfw.wgl.EXT_colorspace)
120                 addAttrib(WGL_COLORSPACE_EXT);
121         }
122     }
123     else
124     {
125         nativeCount = DescribePixelFormat(window->context.wgl.dc,
126                                           1,
127                                           sizeof(PIXELFORMATDESCRIPTOR),
128                                           NULL);
129     }
130 
131     usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig));
132 
133     for (i = 0;  i < nativeCount;  i++)
134     {
135         _GLFWfbconfig* u = usableConfigs + usableCount;
136         pixelFormat = i + 1;
137 
138         if (_glfw.wgl.ARB_pixel_format)
139         {
140             // Get pixel format attributes through "modern" extension
141 
142             if (!_glfw.wgl.GetPixelFormatAttribivARB(window->context.wgl.dc,
143                                                      pixelFormat, 0,
144                                                      attribCount,
145                                                      attribs, values))
146             {
147                 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
148                                     "WGL: Failed to retrieve pixel format attributes");
149 
150                 free(usableConfigs);
151                 return 0;
152             }
153 
154             if (!findAttribValue(WGL_SUPPORT_OPENGL_ARB) ||
155                 !findAttribValue(WGL_DRAW_TO_WINDOW_ARB))
156             {
157                 continue;
158             }
159 
160             if (findAttribValue(WGL_PIXEL_TYPE_ARB) != WGL_TYPE_RGBA_ARB)
161                 continue;
162 
163             if (findAttribValue(WGL_ACCELERATION_ARB) == WGL_NO_ACCELERATION_ARB)
164                 continue;
165 
166             u->redBits = findAttribValue(WGL_RED_BITS_ARB);
167             u->greenBits = findAttribValue(WGL_GREEN_BITS_ARB);
168             u->blueBits = findAttribValue(WGL_BLUE_BITS_ARB);
169             u->alphaBits = findAttribValue(WGL_ALPHA_BITS_ARB);
170 
171             u->depthBits = findAttribValue(WGL_DEPTH_BITS_ARB);
172             u->stencilBits = findAttribValue(WGL_STENCIL_BITS_ARB);
173 
174             u->accumRedBits = findAttribValue(WGL_ACCUM_RED_BITS_ARB);
175             u->accumGreenBits = findAttribValue(WGL_ACCUM_GREEN_BITS_ARB);
176             u->accumBlueBits = findAttribValue(WGL_ACCUM_BLUE_BITS_ARB);
177             u->accumAlphaBits = findAttribValue(WGL_ACCUM_ALPHA_BITS_ARB);
178 
179             u->auxBuffers = findAttribValue(WGL_AUX_BUFFERS_ARB);
180 
181             if (findAttribValue(WGL_STEREO_ARB))
182                 u->stereo = GLFW_TRUE;
183             if (findAttribValue(WGL_DOUBLE_BUFFER_ARB))
184                 u->doublebuffer = GLFW_TRUE;
185 
186             if (_glfw.wgl.ARB_multisample)
187                 u->samples = findAttribValue(WGL_SAMPLES_ARB);
188 
189             if (ctxconfig->client == GLFW_OPENGL_API)
190             {
191                 if (_glfw.wgl.ARB_framebuffer_sRGB ||
192                     _glfw.wgl.EXT_framebuffer_sRGB)
193                 {
194                     if (findAttribValue(WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB))
195                         u->sRGB = GLFW_TRUE;
196                 }
197             }
198             else
199             {
200                 if (_glfw.wgl.EXT_colorspace)
201                 {
202                     if (findAttribValue(WGL_COLORSPACE_EXT) == WGL_COLORSPACE_SRGB_EXT)
203                         u->sRGB = GLFW_TRUE;
204                 }
205             }
206         }
207         else
208         {
209             // Get pixel format attributes through legacy PFDs
210 
211             PIXELFORMATDESCRIPTOR pfd;
212 
213             if (!DescribePixelFormat(window->context.wgl.dc,
214                                      pixelFormat,
215                                      sizeof(PIXELFORMATDESCRIPTOR),
216                                      &pfd))
217             {
218                 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
219                                     "WGL: Failed to describe pixel format");
220 
221                 free(usableConfigs);
222                 return 0;
223             }
224 
225             if (!(pfd.dwFlags & PFD_DRAW_TO_WINDOW) ||
226                 !(pfd.dwFlags & PFD_SUPPORT_OPENGL))
227             {
228                 continue;
229             }
230 
231             if (!(pfd.dwFlags & PFD_GENERIC_ACCELERATED) &&
232                 (pfd.dwFlags & PFD_GENERIC_FORMAT))
233             {
234                 continue;
235             }
236 
237             if (pfd.iPixelType != PFD_TYPE_RGBA)
238                 continue;
239 
240             u->redBits = pfd.cRedBits;
241             u->greenBits = pfd.cGreenBits;
242             u->blueBits = pfd.cBlueBits;
243             u->alphaBits = pfd.cAlphaBits;
244 
245             u->depthBits = pfd.cDepthBits;
246             u->stencilBits = pfd.cStencilBits;
247 
248             u->accumRedBits = pfd.cAccumRedBits;
249             u->accumGreenBits = pfd.cAccumGreenBits;
250             u->accumBlueBits = pfd.cAccumBlueBits;
251             u->accumAlphaBits = pfd.cAccumAlphaBits;
252 
253             u->auxBuffers = pfd.cAuxBuffers;
254 
255             if (pfd.dwFlags & PFD_STEREO)
256                 u->stereo = GLFW_TRUE;
257             if (pfd.dwFlags & PFD_DOUBLEBUFFER)
258                 u->doublebuffer = GLFW_TRUE;
259         }
260 
261         u->handle = pixelFormat;
262         usableCount++;
263     }
264 
265     if (!usableCount)
266     {
267         _glfwInputError(GLFW_API_UNAVAILABLE,
268                         "WGL: The driver does not appear to support OpenGL");
269 
270         free(usableConfigs);
271         return 0;
272     }
273 
274     closest = _glfwChooseFBConfig(fbconfig, usableConfigs, usableCount);
275     if (!closest)
276     {
277         _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
278                         "WGL: Failed to find a suitable pixel format");
279 
280         free(usableConfigs);
281         return 0;
282     }
283 
284     pixelFormat = (int) closest->handle;
285     free(usableConfigs);
286 
287     return pixelFormat;
288 }
289 
290 #undef addAttrib
291 #undef findAttribValue
292 
makeContextCurrentWGL(_GLFWwindow * window)293 static void makeContextCurrentWGL(_GLFWwindow* window)
294 {
295     if (window)
296     {
297         if (wglMakeCurrent(window->context.wgl.dc, window->context.wgl.handle))
298             _glfwPlatformSetTls(&_glfw.contextSlot, window);
299         else
300         {
301             _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
302                                  "WGL: Failed to make context current");
303             _glfwPlatformSetTls(&_glfw.contextSlot, NULL);
304         }
305     }
306     else
307     {
308         if (!wglMakeCurrent(NULL, NULL))
309         {
310             _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
311                                  "WGL: Failed to clear current context");
312         }
313 
314         _glfwPlatformSetTls(&_glfw.contextSlot, NULL);
315     }
316 }
317 
swapBuffersWGL(_GLFWwindow * window)318 static void swapBuffersWGL(_GLFWwindow* window)
319 {
320     if (!window->monitor)
321     {
322         if (IsWindowsVistaOrGreater())
323         {
324             // DWM Composition is always enabled on Win8+
325             BOOL enabled = IsWindows8OrGreater();
326 
327             // HACK: Use DwmFlush when desktop composition is enabled
328             if (enabled ||
329                 (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
330             {
331                 int count = abs(window->context.wgl.interval);
332                 while (count--)
333                     DwmFlush();
334             }
335         }
336     }
337 
338     SwapBuffers(window->context.wgl.dc);
339 }
340 
swapIntervalWGL(int interval)341 static void swapIntervalWGL(int interval)
342 {
343     _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
344 
345     window->context.wgl.interval = interval;
346 
347     if (!window->monitor)
348     {
349         if (IsWindowsVistaOrGreater())
350         {
351             // DWM Composition is always enabled on Win8+
352             BOOL enabled = IsWindows8OrGreater();
353 
354             // HACK: Disable WGL swap interval when desktop composition is enabled to
355             //       avoid interfering with DWM vsync
356             if (enabled ||
357                 (SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled))
358                 interval = 0;
359         }
360     }
361 
362     if (_glfw.wgl.EXT_swap_control)
363         _glfw.wgl.SwapIntervalEXT(interval);
364 }
365 
extensionSupportedWGL(const char * extension)366 static int extensionSupportedWGL(const char* extension)
367 {
368     const char* extensions = NULL;
369 
370     if (_glfw.wgl.GetExtensionsStringARB)
371         extensions = _glfw.wgl.GetExtensionsStringARB(wglGetCurrentDC());
372     else if (_glfw.wgl.GetExtensionsStringEXT)
373         extensions = _glfw.wgl.GetExtensionsStringEXT();
374 
375     if (!extensions)
376         return GLFW_FALSE;
377 
378     return _glfwStringInExtensionString(extension, extensions);
379 }
380 
getProcAddressWGL(const char * procname)381 static GLFWglproc getProcAddressWGL(const char* procname)
382 {
383     const GLFWglproc proc = (GLFWglproc) wglGetProcAddress(procname);
384     if (proc)
385         return proc;
386 
387     return (GLFWglproc) GetProcAddress(_glfw.wgl.instance, procname);
388 }
389 
390 // Destroy the OpenGL context
391 //
destroyContextWGL(_GLFWwindow * window)392 static void destroyContextWGL(_GLFWwindow* window)
393 {
394     if (window->context.wgl.handle)
395     {
396         wglDeleteContext(window->context.wgl.handle);
397         window->context.wgl.handle = NULL;
398     }
399 }
400 
401 
402 //////////////////////////////////////////////////////////////////////////
403 //////                       GLFW internal API                      //////
404 //////////////////////////////////////////////////////////////////////////
405 
406 // Initialize WGL
407 //
_glfwInitWGL(void)408 GLFWbool _glfwInitWGL(void)
409 {
410     PIXELFORMATDESCRIPTOR pfd;
411     HGLRC prc, rc;
412     HDC pdc, dc;
413 
414     if (_glfw.wgl.instance)
415         return GLFW_TRUE;
416 
417     _glfw.wgl.instance = LoadLibraryA("opengl32.dll");
418     if (!_glfw.wgl.instance)
419     {
420         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
421                              "WGL: Failed to load opengl32.dll");
422         return GLFW_FALSE;
423     }
424 
425     _glfw.wgl.CreateContext = (PFN_wglCreateContext)
426         GetProcAddress(_glfw.wgl.instance, "wglCreateContext");
427     _glfw.wgl.DeleteContext = (PFN_wglDeleteContext)
428         GetProcAddress(_glfw.wgl.instance, "wglDeleteContext");
429     _glfw.wgl.GetProcAddress = (PFN_wglGetProcAddress)
430         GetProcAddress(_glfw.wgl.instance, "wglGetProcAddress");
431     _glfw.wgl.GetCurrentDC = (PFN_wglGetCurrentDC)
432         GetProcAddress(_glfw.wgl.instance, "wglGetCurrentDC");
433     _glfw.wgl.GetCurrentContext = (PFN_wglGetCurrentContext)
434         GetProcAddress(_glfw.wgl.instance, "wglGetCurrentContext");
435     _glfw.wgl.MakeCurrent = (PFN_wglMakeCurrent)
436         GetProcAddress(_glfw.wgl.instance, "wglMakeCurrent");
437     _glfw.wgl.ShareLists = (PFN_wglShareLists)
438         GetProcAddress(_glfw.wgl.instance, "wglShareLists");
439 
440     // NOTE: A dummy context has to be created for opengl32.dll to load the
441     //       OpenGL ICD, from which we can then query WGL extensions
442     // NOTE: This code will accept the Microsoft GDI ICD; accelerated context
443     //       creation failure occurs during manual pixel format enumeration
444 
445     dc = GetDC(_glfw.win32.helperWindowHandle);
446 
447     ZeroMemory(&pfd, sizeof(pfd));
448     pfd.nSize = sizeof(pfd);
449     pfd.nVersion = 1;
450     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
451     pfd.iPixelType = PFD_TYPE_RGBA;
452     pfd.cColorBits = 24;
453 
454     if (!SetPixelFormat(dc, ChoosePixelFormat(dc, &pfd), &pfd))
455     {
456         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
457                              "WGL: Failed to set pixel format for dummy context");
458         return GLFW_FALSE;
459     }
460 
461     rc = wglCreateContext(dc);
462     if (!rc)
463     {
464         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
465                              "WGL: Failed to create dummy context");
466         return GLFW_FALSE;
467     }
468 
469     pdc = wglGetCurrentDC();
470     prc = wglGetCurrentContext();
471 
472     if (!wglMakeCurrent(dc, rc))
473     {
474         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
475                              "WGL: Failed to make dummy context current");
476         wglMakeCurrent(pdc, prc);
477         wglDeleteContext(rc);
478         return GLFW_FALSE;
479     }
480 
481     // NOTE: Functions must be loaded first as they're needed to retrieve the
482     //       extension string that tells us whether the functions are supported
483     _glfw.wgl.GetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)
484         wglGetProcAddress("wglGetExtensionsStringEXT");
485     _glfw.wgl.GetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)
486         wglGetProcAddress("wglGetExtensionsStringARB");
487     _glfw.wgl.CreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)
488         wglGetProcAddress("wglCreateContextAttribsARB");
489     _glfw.wgl.SwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)
490         wglGetProcAddress("wglSwapIntervalEXT");
491     _glfw.wgl.GetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)
492         wglGetProcAddress("wglGetPixelFormatAttribivARB");
493 
494     // NOTE: WGL_ARB_extensions_string and WGL_EXT_extensions_string are not
495     //       checked below as we are already using them
496     _glfw.wgl.ARB_multisample =
497         extensionSupportedWGL("WGL_ARB_multisample");
498     _glfw.wgl.ARB_framebuffer_sRGB =
499         extensionSupportedWGL("WGL_ARB_framebuffer_sRGB");
500     _glfw.wgl.EXT_framebuffer_sRGB =
501         extensionSupportedWGL("WGL_EXT_framebuffer_sRGB");
502     _glfw.wgl.ARB_create_context =
503         extensionSupportedWGL("WGL_ARB_create_context");
504     _glfw.wgl.ARB_create_context_profile =
505         extensionSupportedWGL("WGL_ARB_create_context_profile");
506     _glfw.wgl.EXT_create_context_es2_profile =
507         extensionSupportedWGL("WGL_EXT_create_context_es2_profile");
508     _glfw.wgl.ARB_create_context_robustness =
509         extensionSupportedWGL("WGL_ARB_create_context_robustness");
510     _glfw.wgl.ARB_create_context_no_error =
511         extensionSupportedWGL("WGL_ARB_create_context_no_error");
512     _glfw.wgl.EXT_swap_control =
513         extensionSupportedWGL("WGL_EXT_swap_control");
514     _glfw.wgl.EXT_colorspace =
515         extensionSupportedWGL("WGL_EXT_colorspace");
516     _glfw.wgl.ARB_pixel_format =
517         extensionSupportedWGL("WGL_ARB_pixel_format");
518     _glfw.wgl.ARB_context_flush_control =
519         extensionSupportedWGL("WGL_ARB_context_flush_control");
520 
521     wglMakeCurrent(pdc, prc);
522     wglDeleteContext(rc);
523     return GLFW_TRUE;
524 }
525 
526 // Terminate WGL
527 //
_glfwTerminateWGL(void)528 void _glfwTerminateWGL(void)
529 {
530     if (_glfw.wgl.instance)
531         FreeLibrary(_glfw.wgl.instance);
532 }
533 
534 #define setAttrib(a, v) \
535 { \
536     assert(((size_t) index + 1) < sizeof(attribs) / sizeof(attribs[0])); \
537     attribs[index++] = a; \
538     attribs[index++] = v; \
539 }
540 
541 // Create the OpenGL or OpenGL ES context
542 //
_glfwCreateContextWGL(_GLFWwindow * window,const _GLFWctxconfig * ctxconfig,const _GLFWfbconfig * fbconfig)543 GLFWbool _glfwCreateContextWGL(_GLFWwindow* window,
544                                const _GLFWctxconfig* ctxconfig,
545                                const _GLFWfbconfig* fbconfig)
546 {
547     int attribs[40];
548     int pixelFormat;
549     PIXELFORMATDESCRIPTOR pfd;
550     HGLRC share = NULL;
551 
552     if (ctxconfig->share)
553         share = ctxconfig->share->context.wgl.handle;
554 
555     window->context.wgl.dc = GetDC(window->win32.handle);
556     if (!window->context.wgl.dc)
557     {
558         _glfwInputError(GLFW_PLATFORM_ERROR,
559                         "WGL: Failed to retrieve DC for window");
560         return GLFW_FALSE;
561     }
562 
563     pixelFormat = choosePixelFormat(window, ctxconfig, fbconfig);
564     if (!pixelFormat)
565         return GLFW_FALSE;
566 
567     if (!DescribePixelFormat(window->context.wgl.dc,
568                              pixelFormat, sizeof(pfd), &pfd))
569     {
570         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
571                              "WGL: Failed to retrieve PFD for selected pixel format");
572         return GLFW_FALSE;
573     }
574 
575     if (!SetPixelFormat(window->context.wgl.dc, pixelFormat, &pfd))
576     {
577         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
578                              "WGL: Failed to set selected pixel format");
579         return GLFW_FALSE;
580     }
581 
582     if (ctxconfig->client == GLFW_OPENGL_API)
583     {
584         if (ctxconfig->forward)
585         {
586             if (!_glfw.wgl.ARB_create_context)
587             {
588                 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
589                                 "WGL: A forward compatible OpenGL context requested but WGL_ARB_create_context is unavailable");
590                 return GLFW_FALSE;
591             }
592         }
593 
594         if (ctxconfig->profile)
595         {
596             if (!_glfw.wgl.ARB_create_context_profile)
597             {
598                 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
599                                 "WGL: OpenGL profile requested but WGL_ARB_create_context_profile is unavailable");
600                 return GLFW_FALSE;
601             }
602         }
603     }
604     else
605     {
606         if (!_glfw.wgl.ARB_create_context ||
607             !_glfw.wgl.ARB_create_context_profile ||
608             !_glfw.wgl.EXT_create_context_es2_profile)
609         {
610             _glfwInputError(GLFW_API_UNAVAILABLE,
611                             "WGL: OpenGL ES requested but WGL_ARB_create_context_es2_profile is unavailable");
612             return GLFW_FALSE;
613         }
614     }
615 
616     if (_glfw.wgl.ARB_create_context)
617     {
618         int index = 0, mask = 0, flags = 0;
619 
620         if (ctxconfig->client == GLFW_OPENGL_API)
621         {
622             if (ctxconfig->forward)
623                 flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
624 
625             if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
626                 mask |= WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
627             else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
628                 mask |= WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
629         }
630         else
631             mask |= WGL_CONTEXT_ES2_PROFILE_BIT_EXT;
632 
633         if (ctxconfig->debug)
634             flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
635 
636         if (ctxconfig->robustness)
637         {
638             if (_glfw.wgl.ARB_create_context_robustness)
639             {
640                 if (ctxconfig->robustness == GLFW_NO_RESET_NOTIFICATION)
641                 {
642                     setAttrib(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
643                               WGL_NO_RESET_NOTIFICATION_ARB);
644                 }
645                 else if (ctxconfig->robustness == GLFW_LOSE_CONTEXT_ON_RESET)
646                 {
647                     setAttrib(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
648                               WGL_LOSE_CONTEXT_ON_RESET_ARB);
649                 }
650 
651                 flags |= WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB;
652             }
653         }
654 
655         if (ctxconfig->release)
656         {
657             if (_glfw.wgl.ARB_context_flush_control)
658             {
659                 if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_NONE)
660                 {
661                     setAttrib(WGL_CONTEXT_RELEASE_BEHAVIOR_ARB,
662                               WGL_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB);
663                 }
664                 else if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_FLUSH)
665                 {
666                     setAttrib(WGL_CONTEXT_RELEASE_BEHAVIOR_ARB,
667                               WGL_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB);
668                 }
669             }
670         }
671 
672         if (ctxconfig->noerror)
673         {
674             if (_glfw.wgl.ARB_create_context_no_error)
675                 setAttrib(WGL_CONTEXT_OPENGL_NO_ERROR_ARB, GLFW_TRUE);
676         }
677 
678         // NOTE: Only request an explicitly versioned context when necessary, as
679         //       explicitly requesting version 1.0 does not always return the
680         //       highest version supported by the driver
681         if (ctxconfig->major != 1 || ctxconfig->minor != 0)
682         {
683             setAttrib(WGL_CONTEXT_MAJOR_VERSION_ARB, ctxconfig->major);
684             setAttrib(WGL_CONTEXT_MINOR_VERSION_ARB, ctxconfig->minor);
685         }
686 
687         if (flags)
688             setAttrib(WGL_CONTEXT_FLAGS_ARB, flags);
689 
690         if (mask)
691             setAttrib(WGL_CONTEXT_PROFILE_MASK_ARB, mask);
692 
693         setAttrib(0, 0);
694 
695         window->context.wgl.handle =
696             _glfw.wgl.CreateContextAttribsARB(window->context.wgl.dc,
697                                               share, attribs);
698         if (!window->context.wgl.handle)
699         {
700             const DWORD error = GetLastError();
701 
702             if (error == (0xc0070000 | ERROR_INVALID_VERSION_ARB))
703             {
704                 if (ctxconfig->client == GLFW_OPENGL_API)
705                 {
706                     _glfwInputError(GLFW_VERSION_UNAVAILABLE,
707                                     "WGL: Driver does not support OpenGL version %i.%i",
708                                     ctxconfig->major,
709                                     ctxconfig->minor);
710                 }
711                 else
712                 {
713                     _glfwInputError(GLFW_VERSION_UNAVAILABLE,
714                                     "WGL: Driver does not support OpenGL ES version %i.%i",
715                                     ctxconfig->major,
716                                     ctxconfig->minor);
717                 }
718             }
719             else if (error == (0xc0070000 | ERROR_INVALID_PROFILE_ARB))
720             {
721                 _glfwInputError(GLFW_VERSION_UNAVAILABLE,
722                                 "WGL: Driver does not support the requested OpenGL profile");
723             }
724             else if (error == (0xc0070000 | ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB))
725             {
726                 _glfwInputError(GLFW_INVALID_VALUE,
727                                 "WGL: The share context is not compatible with the requested context");
728             }
729             else
730             {
731                 if (ctxconfig->client == GLFW_OPENGL_API)
732                 {
733                     _glfwInputError(GLFW_VERSION_UNAVAILABLE,
734                                     "WGL: Failed to create OpenGL context");
735                 }
736                 else
737                 {
738                     _glfwInputError(GLFW_VERSION_UNAVAILABLE,
739                                     "WGL: Failed to create OpenGL ES context");
740                 }
741             }
742 
743             return GLFW_FALSE;
744         }
745     }
746     else
747     {
748         window->context.wgl.handle = wglCreateContext(window->context.wgl.dc);
749         if (!window->context.wgl.handle)
750         {
751             _glfwInputErrorWin32(GLFW_VERSION_UNAVAILABLE,
752                                  "WGL: Failed to create OpenGL context");
753             return GLFW_FALSE;
754         }
755 
756         if (share)
757         {
758             if (!wglShareLists(share, window->context.wgl.handle))
759             {
760                 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
761                                      "WGL: Failed to enable sharing with specified OpenGL context");
762                 return GLFW_FALSE;
763             }
764         }
765     }
766 
767     window->context.makeCurrent = makeContextCurrentWGL;
768     window->context.swapBuffers = swapBuffersWGL;
769     window->context.swapInterval = swapIntervalWGL;
770     window->context.extensionSupported = extensionSupportedWGL;
771     window->context.getProcAddress = getProcAddressWGL;
772     window->context.destroy = destroyContextWGL;
773 
774     return GLFW_TRUE;
775 }
776 
777 #undef setAttrib
778 
779 
780 //////////////////////////////////////////////////////////////////////////
781 //////                        GLFW native API                       //////
782 //////////////////////////////////////////////////////////////////////////
783 
glfwGetWGLContext(GLFWwindow * handle)784 GLFWAPI HGLRC glfwGetWGLContext(GLFWwindow* handle)
785 {
786     _GLFWwindow* window = (_GLFWwindow*) handle;
787     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
788 
789     if (window->context.client == GLFW_NO_API)
790     {
791         _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
792         return NULL;
793     }
794 
795     return window->context.wgl.handle;
796 }
797 
798