github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/raylib/external/glfw/src/wgl_context.c (about)

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