wgl_context.c 26 KB

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