glcontext_wgl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright 2011-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #if (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # if BGFX_USE_WGL
  9. namespace bgfx { namespace gl
  10. {
  11. PFNWGLGETPROCADDRESSPROC wglGetProcAddress;
  12. PFNWGLMAKECURRENTPROC wglMakeCurrent;
  13. PFNWGLCREATECONTEXTPROC wglCreateContext;
  14. PFNWGLDELETECONTEXTPROC wglDeleteContext;
  15. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
  16. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
  17. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
  18. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
  19. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
  20. # include "glimports.h"
  21. struct SwapChainGL
  22. {
  23. SwapChainGL(void* _nwh)
  24. : m_hwnd( (HWND)_nwh)
  25. {
  26. m_hdc = GetDC(m_hwnd);
  27. }
  28. ~SwapChainGL()
  29. {
  30. wglMakeCurrent(NULL, NULL);
  31. wglDeleteContext(m_context);
  32. ReleaseDC(m_hwnd, m_hdc);
  33. }
  34. void makeCurrent()
  35. {
  36. wglMakeCurrent(m_hdc, m_context);
  37. GLenum err = glGetError();
  38. BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err);
  39. }
  40. void swapBuffers()
  41. {
  42. SwapBuffers(m_hdc);
  43. }
  44. HWND m_hwnd;
  45. HDC m_hdc;
  46. HGLRC m_context;
  47. };
  48. static HGLRC createContext(HDC _hdc)
  49. {
  50. PIXELFORMATDESCRIPTOR pfd;
  51. bx::memSet(&pfd, 0, sizeof(pfd) );
  52. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  53. pfd.nVersion = 1;
  54. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  55. pfd.iPixelType = PFD_TYPE_RGBA;
  56. pfd.cColorBits = 32;
  57. pfd.cAlphaBits = 8;
  58. pfd.cDepthBits = 24;
  59. pfd.cStencilBits = 8;
  60. pfd.iLayerType = PFD_MAIN_PLANE;
  61. int pixelFormat = ChoosePixelFormat(_hdc, &pfd);
  62. BGFX_FATAL(0 != pixelFormat, Fatal::UnableToInitialize, "ChoosePixelFormat failed!");
  63. DescribePixelFormat(_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  64. BX_TRACE("Pixel format:\n"
  65. "\tiPixelType %d\n"
  66. "\tcColorBits %d\n"
  67. "\tcAlphaBits %d\n"
  68. "\tcDepthBits %d\n"
  69. "\tcStencilBits %d\n"
  70. , pfd.iPixelType
  71. , pfd.cColorBits
  72. , pfd.cAlphaBits
  73. , pfd.cDepthBits
  74. , pfd.cStencilBits
  75. );
  76. int result;
  77. result = SetPixelFormat(_hdc, pixelFormat, &pfd);
  78. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed!");
  79. HGLRC context = wglCreateContext(_hdc);
  80. BGFX_FATAL(NULL != context, Fatal::UnableToInitialize, "wglCreateContext failed!");
  81. result = wglMakeCurrent(_hdc, context);
  82. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
  83. return context;
  84. }
  85. void GlContext::create(uint32_t /*_width*/, uint32_t /*_height*/)
  86. {
  87. m_opengl32dll = bx::dlopen("opengl32.dll");
  88. BGFX_FATAL(NULL != m_opengl32dll, Fatal::UnableToInitialize, "Failed to load opengl32.dll.");
  89. wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
  90. BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
  91. // If g_platformHooks.nwh is NULL, the assumption is that GL context was created
  92. // by user (for example, using SDL, GLFW, etc.)
  93. BX_WARN(NULL != g_platformData.nwh
  94. , "bgfx::setPlatform with valid window is not called. This might "
  95. "be intentional when GL context is created by the user."
  96. );
  97. if (NULL != g_platformData.nwh)
  98. {
  99. wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
  100. BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
  101. wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext");
  102. BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
  103. wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext");
  104. BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
  105. m_hdc = GetDC( (HWND)g_platformData.nwh);
  106. BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!");
  107. // Dummy window to peek into WGL functionality.
  108. //
  109. // An application can only set the pixel format of a window one time.
  110. // Once a window's pixel format is set, it cannot be changed.
  111. // MSDN: https://web.archive.org/web/20190207230357/https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/nf-wingdi-setpixelformat
  112. HWND hwnd = CreateWindowA("STATIC"
  113. , ""
  114. , WS_POPUP|WS_DISABLED
  115. , -32000
  116. , -32000
  117. , 0
  118. , 0
  119. , NULL
  120. , NULL
  121. , GetModuleHandle(NULL)
  122. , 0
  123. );
  124. HDC hdc = GetDC(hwnd);
  125. BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!");
  126. HGLRC context = createContext(hdc);
  127. wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  128. wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  129. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  130. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  131. if (NULL != wglGetExtensionsStringARB)
  132. {
  133. const char* extensions = (const char*)wglGetExtensionsStringARB(hdc);
  134. BX_TRACE("WGL extensions:");
  135. dumpExtensions(extensions);
  136. }
  137. if (NULL != wglChoosePixelFormatARB
  138. && NULL != wglCreateContextAttribsARB)
  139. {
  140. int32_t attrs[] =
  141. {
  142. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  143. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  144. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  145. WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
  146. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  147. WGL_COLOR_BITS_ARB, 32,
  148. WGL_ALPHA_BITS_ARB, 8,
  149. WGL_DEPTH_BITS_ARB, 24,
  150. WGL_STENCIL_BITS_ARB, 8,
  151. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  152. WGL_SAMPLES_ARB, 4,
  153. 0
  154. };
  155. int result;
  156. uint32_t numFormats = 0;
  157. do
  158. {
  159. result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &m_pixelFormat, &numFormats);
  160. if (0 == result
  161. || 0 == numFormats)
  162. {
  163. attrs[3] >>= 1;
  164. attrs[1] = attrs[3] == 0 ? 0 : 1;
  165. }
  166. } while (0 == numFormats);
  167. DescribePixelFormat(m_hdc, m_pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &m_pfd);
  168. BX_TRACE("Pixel format:\n"
  169. "\tiPixelType %d\n"
  170. "\tcColorBits %d\n"
  171. "\tcAlphaBits %d\n"
  172. "\tcDepthBits %d\n"
  173. "\tcStencilBits %d\n"
  174. , m_pfd.iPixelType
  175. , m_pfd.cColorBits
  176. , m_pfd.cAlphaBits
  177. , m_pfd.cDepthBits
  178. , m_pfd.cStencilBits
  179. );
  180. result = SetPixelFormat(m_hdc, m_pixelFormat, &m_pfd);
  181. // When window is created by SDL and SDL_WINDOW_OPENGL is set, SetPixelFormat
  182. // will fail. Just warn and continue. In case it failed for some other reason
  183. // create context will fail and it will error out there.
  184. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
  185. int32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
  186. BX_UNUSED(flags);
  187. int32_t contextAttrs[9] =
  188. {
  189. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  190. WGL_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
  191. WGL_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
  192. WGL_CONTEXT_FLAGS_ARB, flags,
  193. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  194. #else
  195. WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
  196. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  197. 0, 0,
  198. 0, 0,
  199. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  200. 0
  201. };
  202. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  203. if (NULL == m_context)
  204. {
  205. // nVidia doesn't like context profile mask for contexts below 3.2?
  206. contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6];
  207. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  208. }
  209. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() );
  210. BX_STATIC_ASSERT(sizeof(contextAttrs) == sizeof(m_contextAttrs) );
  211. bx::memCopy(m_contextAttrs, contextAttrs, sizeof(contextAttrs) );
  212. }
  213. wglMakeCurrent(NULL, NULL);
  214. wglDeleteContext(context);
  215. DestroyWindow(hwnd);
  216. if (NULL == m_context)
  217. {
  218. m_context = createContext(m_hdc);
  219. }
  220. int result = wglMakeCurrent(m_hdc, m_context);
  221. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
  222. m_current = NULL;
  223. if (NULL != wglSwapIntervalEXT)
  224. {
  225. wglSwapIntervalEXT(0);
  226. }
  227. }
  228. import();
  229. g_internalData.context = m_context;
  230. }
  231. void GlContext::destroy()
  232. {
  233. if (NULL != g_platformData.nwh)
  234. {
  235. wglMakeCurrent(NULL, NULL);
  236. wglDeleteContext(m_context);
  237. m_context = NULL;
  238. ReleaseDC( (HWND)g_platformData.nwh, m_hdc);
  239. m_hdc = NULL;
  240. }
  241. bx::dlclose(m_opengl32dll);
  242. m_opengl32dll = NULL;
  243. }
  244. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t _flags)
  245. {
  246. if (NULL != wglSwapIntervalEXT)
  247. {
  248. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  249. wglSwapIntervalEXT(vsync ? 1 : 0);
  250. }
  251. }
  252. uint64_t GlContext::getCaps() const
  253. {
  254. return BGFX_CAPS_SWAP_CHAIN;
  255. }
  256. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  257. {
  258. SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(_nwh);
  259. int result = SetPixelFormat(swapChain->m_hdc, m_pixelFormat, &m_pfd);
  260. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() ); BX_UNUSED(result);
  261. swapChain->m_context = wglCreateContextAttribsARB(swapChain->m_hdc, m_context, m_contextAttrs);
  262. BX_CHECK(NULL != swapChain->m_context, "Create swap chain failed: %x", glGetError() );
  263. return swapChain;
  264. }
  265. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  266. {
  267. BX_DELETE(g_allocator, _swapChain);
  268. wglMakeCurrent(m_hdc, m_context);
  269. }
  270. void GlContext::swap(SwapChainGL* _swapChain)
  271. {
  272. makeCurrent(_swapChain);
  273. if (NULL == _swapChain)
  274. {
  275. if (NULL != g_platformData.nwh)
  276. {
  277. SwapBuffers(m_hdc);
  278. }
  279. }
  280. else
  281. {
  282. _swapChain->swapBuffers();
  283. }
  284. }
  285. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  286. {
  287. if (m_current != _swapChain)
  288. {
  289. m_current = _swapChain;
  290. if (NULL == _swapChain)
  291. {
  292. wglMakeCurrent(m_hdc, m_context);
  293. GLenum err = glGetError();
  294. BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err);
  295. }
  296. else
  297. {
  298. _swapChain->makeCurrent();
  299. }
  300. }
  301. }
  302. void GlContext::import()
  303. {
  304. BX_TRACE("Import:");
  305. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  306. { \
  307. if (NULL == _func) \
  308. { \
  309. _func = (_proto)wglGetProcAddress(#_import); \
  310. if (_func == NULL) \
  311. { \
  312. _func = (_proto)bx::dlsym(m_opengl32dll, #_import); \
  313. BX_TRACE(" %p " #_func " (" #_import ")", _func); \
  314. } \
  315. else \
  316. { \
  317. BX_TRACE("wgl %p " #_func " (" #_import ")", _func); \
  318. } \
  319. BGFX_FATAL(BX_IGNORE_C4127(_optional) || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_import); \
  320. } \
  321. }
  322. # include "glimports.h"
  323. }
  324. } } // namespace bgfx
  325. # endif // BGFX_USE_WGL
  326. #endif // (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)