glcontext_wgl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright 2011-2017 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: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx
  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_SAMPLE_BUFFERS_ARB, 0,
  143. WGL_SAMPLES_ARB, 0,
  144. WGL_SUPPORT_OPENGL_ARB, true,
  145. WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
  146. WGL_DRAW_TO_WINDOW_ARB, true,
  147. WGL_DOUBLE_BUFFER_ARB, true,
  148. WGL_COLOR_BITS_ARB, 32,
  149. WGL_DEPTH_BITS_ARB, 24,
  150. WGL_STENCIL_BITS_ARB, 8,
  151. 0
  152. };
  153. int result;
  154. uint32_t numFormats = 0;
  155. do
  156. {
  157. result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &m_pixelFormat, &numFormats);
  158. if (0 == result
  159. || 0 == numFormats)
  160. {
  161. attrs[3] >>= 1;
  162. attrs[1] = attrs[3] == 0 ? 0 : 1;
  163. }
  164. } while (0 == numFormats);
  165. DescribePixelFormat(m_hdc, m_pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &m_pfd);
  166. BX_TRACE("Pixel format:\n"
  167. "\tiPixelType %d\n"
  168. "\tcColorBits %d\n"
  169. "\tcAlphaBits %d\n"
  170. "\tcDepthBits %d\n"
  171. "\tcStencilBits %d\n"
  172. , m_pfd.iPixelType
  173. , m_pfd.cColorBits
  174. , m_pfd.cAlphaBits
  175. , m_pfd.cDepthBits
  176. , m_pfd.cStencilBits
  177. );
  178. result = SetPixelFormat(m_hdc, m_pixelFormat, &m_pfd);
  179. // When window is created by SDL and SDL_WINDOW_OPENGL is set, SetPixelFormat
  180. // will fail. Just warn and continue. In case it failed for some other reason
  181. // create context will fail and it will error out there.
  182. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
  183. int32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
  184. BX_UNUSED(flags);
  185. int32_t contextAttrs[9] =
  186. {
  187. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  188. WGL_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
  189. WGL_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
  190. WGL_CONTEXT_FLAGS_ARB, flags,
  191. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  192. #else
  193. WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
  194. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  195. 0, 0,
  196. 0, 0,
  197. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  198. 0
  199. };
  200. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  201. if (NULL == m_context)
  202. {
  203. // nVidia doesn't like context profile mask for contexts below 3.2?
  204. contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6];
  205. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  206. }
  207. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() );
  208. BX_STATIC_ASSERT(sizeof(contextAttrs) == sizeof(m_contextAttrs) );
  209. bx::memCopy(m_contextAttrs, contextAttrs, sizeof(contextAttrs) );
  210. }
  211. wglMakeCurrent(NULL, NULL);
  212. wglDeleteContext(context);
  213. DestroyWindow(hwnd);
  214. if (NULL == m_context)
  215. {
  216. m_context = createContext(m_hdc);
  217. }
  218. int result = wglMakeCurrent(m_hdc, m_context);
  219. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
  220. m_current = NULL;
  221. if (NULL != wglSwapIntervalEXT)
  222. {
  223. wglSwapIntervalEXT(0);
  224. }
  225. }
  226. import();
  227. g_internalData.context = m_context;
  228. }
  229. void GlContext::destroy()
  230. {
  231. if (NULL != g_platformData.nwh)
  232. {
  233. wglMakeCurrent(NULL, NULL);
  234. wglDeleteContext(m_context);
  235. m_context = NULL;
  236. ReleaseDC( (HWND)g_platformData.nwh, m_hdc);
  237. m_hdc = NULL;
  238. }
  239. bx::dlclose(m_opengl32dll);
  240. m_opengl32dll = NULL;
  241. }
  242. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t _flags)
  243. {
  244. if (NULL != wglSwapIntervalEXT)
  245. {
  246. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  247. wglSwapIntervalEXT(vsync ? 1 : 0);
  248. }
  249. }
  250. uint64_t GlContext::getCaps() const
  251. {
  252. return BGFX_CAPS_SWAP_CHAIN;
  253. }
  254. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  255. {
  256. SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(_nwh);
  257. int result = SetPixelFormat(swapChain->m_hdc, m_pixelFormat, &m_pfd);
  258. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() ); BX_UNUSED(result);
  259. swapChain->m_context = wglCreateContextAttribsARB(swapChain->m_hdc, m_context, m_contextAttrs);
  260. BX_CHECK(NULL != swapChain->m_context, "Create swap chain failed: %x", glGetError() );
  261. return swapChain;
  262. }
  263. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  264. {
  265. BX_DELETE(g_allocator, _swapChain);
  266. wglMakeCurrent(m_hdc, m_context);
  267. }
  268. void GlContext::swap(SwapChainGL* _swapChain)
  269. {
  270. makeCurrent(_swapChain);
  271. if (NULL == _swapChain)
  272. {
  273. if (NULL != g_platformData.nwh)
  274. {
  275. SwapBuffers(m_hdc);
  276. }
  277. }
  278. else
  279. {
  280. _swapChain->swapBuffers();
  281. }
  282. }
  283. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  284. {
  285. if (m_current != _swapChain)
  286. {
  287. m_current = _swapChain;
  288. if (NULL == _swapChain)
  289. {
  290. wglMakeCurrent(m_hdc, m_context);
  291. GLenum err = glGetError();
  292. BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err);
  293. }
  294. else
  295. {
  296. _swapChain->makeCurrent();
  297. }
  298. }
  299. }
  300. void GlContext::import()
  301. {
  302. BX_TRACE("Import:");
  303. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  304. { \
  305. if (NULL == _func) \
  306. { \
  307. _func = (_proto)wglGetProcAddress(#_import); \
  308. if (_func == NULL) \
  309. { \
  310. _func = (_proto)bx::dlsym(m_opengl32dll, #_import); \
  311. BX_TRACE(" %p " #_func " (" #_import ")", _func); \
  312. } \
  313. else \
  314. { \
  315. BX_TRACE("wgl %p " #_func " (" #_import ")", _func); \
  316. } \
  317. BGFX_FATAL(BX_IGNORE_C4127(_optional) || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_import); \
  318. } \
  319. }
  320. # include "glimports.h"
  321. }
  322. } } // namespace bgfx
  323. # endif // BGFX_USE_WGL
  324. #endif // (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)