glcontext_wgl.cpp 13 KB

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