glcontext_wgl.cpp 14 KB

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