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