glcontext_wgl.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #if (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # if BGFX_USE_WGL
  9. namespace bgfx
  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) _proto _func
  20. # include "glimports.h"
  21. # undef GL_IMPORT
  22. static HGLRC createContext(HDC _hdc)
  23. {
  24. PIXELFORMATDESCRIPTOR pfd;
  25. memset(&pfd, 0, sizeof(pfd) );
  26. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  27. pfd.nVersion = 1;
  28. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  29. pfd.iPixelType = PFD_TYPE_RGBA;
  30. pfd.cColorBits = 32;
  31. pfd.cAlphaBits = 8;
  32. pfd.cDepthBits = 24;
  33. pfd.cStencilBits = 8;
  34. pfd.iLayerType = PFD_MAIN_PLANE;
  35. int pixelFormat = ChoosePixelFormat(_hdc, &pfd);
  36. BGFX_FATAL(0 != pixelFormat, Fatal::UnableToInitialize, "ChoosePixelFormat failed!");
  37. DescribePixelFormat(_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  38. BX_TRACE("Pixel format:\n"
  39. "\tiPixelType %d\n"
  40. "\tcColorBits %d\n"
  41. "\tcAlphaBits %d\n"
  42. "\tcDepthBits %d\n"
  43. "\tcStencilBits %d\n"
  44. , pfd.iPixelType
  45. , pfd.cColorBits
  46. , pfd.cAlphaBits
  47. , pfd.cDepthBits
  48. , pfd.cStencilBits
  49. );
  50. int result;
  51. result = SetPixelFormat(_hdc, pixelFormat, &pfd);
  52. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed!");
  53. HGLRC context = wglCreateContext(_hdc);
  54. BGFX_FATAL(NULL != context, Fatal::UnableToInitialize, "wglCreateContext failed!");
  55. result = wglMakeCurrent(_hdc, context);
  56. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
  57. return context;
  58. }
  59. void GlContext::create(uint32_t /*_width*/, uint32_t /*_height*/)
  60. {
  61. m_opengl32dll = bx::dlopen("opengl32.dll");
  62. BGFX_FATAL(NULL != m_opengl32dll, Fatal::UnableToInitialize, "Failed to load opengl32.dll.");
  63. wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
  64. BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
  65. // If g_bgfxHwnd is NULL, the assumption is that GL context was created
  66. // by user (for example, using SDL, GLFW, etc.)
  67. BX_WARN(NULL != g_bgfxHwnd
  68. , "bgfx::winSetHwnd with valid window is not called. This might "
  69. "be intentional when GL context is created by the user."
  70. );
  71. if (NULL != g_bgfxHwnd)
  72. {
  73. wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
  74. BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
  75. wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext");
  76. BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
  77. wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext");
  78. BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
  79. m_hdc = GetDC(g_bgfxHwnd);
  80. BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!");
  81. // Dummy window to peek into WGL functionality.
  82. //
  83. // An application can only set the pixel format of a window one time.
  84. // Once a window's pixel format is set, it cannot be changed.
  85. // MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx
  86. HWND hwnd = CreateWindowA("STATIC"
  87. , ""
  88. , WS_POPUP|WS_DISABLED
  89. , -32000
  90. , -32000
  91. , 0
  92. , 0
  93. , NULL
  94. , NULL
  95. , GetModuleHandle(NULL)
  96. , 0
  97. );
  98. HDC hdc = GetDC(hwnd);
  99. BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!");
  100. HGLRC context = createContext(hdc);
  101. wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  102. wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  103. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  104. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  105. if (NULL != wglGetExtensionsStringARB)
  106. {
  107. const char* extensions = (const char*)wglGetExtensionsStringARB(hdc);
  108. BX_TRACE("WGL extensions:");
  109. dumpExtensions(extensions);
  110. }
  111. if (NULL != wglChoosePixelFormatARB
  112. && NULL != wglCreateContextAttribsARB)
  113. {
  114. int32_t attrs[] =
  115. {
  116. WGL_SAMPLE_BUFFERS_ARB, 0,
  117. WGL_SAMPLES_ARB, 0,
  118. WGL_SUPPORT_OPENGL_ARB, true,
  119. WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
  120. WGL_DRAW_TO_WINDOW_ARB, true,
  121. WGL_DOUBLE_BUFFER_ARB, true,
  122. WGL_COLOR_BITS_ARB, 32,
  123. WGL_DEPTH_BITS_ARB, 24,
  124. WGL_STENCIL_BITS_ARB, 8,
  125. 0
  126. };
  127. int result;
  128. int pixelFormat;
  129. uint32_t numFormats = 0;
  130. do
  131. {
  132. result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &pixelFormat, &numFormats);
  133. if (0 == result
  134. || 0 == numFormats)
  135. {
  136. attrs[3] >>= 1;
  137. attrs[1] = attrs[3] == 0 ? 0 : 1;
  138. }
  139. } while (0 == numFormats);
  140. PIXELFORMATDESCRIPTOR pfd;
  141. DescribePixelFormat(m_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  142. BX_TRACE("Pixel format:\n"
  143. "\tiPixelType %d\n"
  144. "\tcColorBits %d\n"
  145. "\tcAlphaBits %d\n"
  146. "\tcDepthBits %d\n"
  147. "\tcStencilBits %d\n"
  148. , pfd.iPixelType
  149. , pfd.cColorBits
  150. , pfd.cAlphaBits
  151. , pfd.cDepthBits
  152. , pfd.cStencilBits
  153. );
  154. result = SetPixelFormat(m_hdc, pixelFormat, &pfd);
  155. // When window is created by SDL and SDL_WINDOW_OPENGL is set SetPixelFormat
  156. // will fail. Just warn and continue. In case it failed for some other reason
  157. // create context will fail and it will error out there.
  158. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
  159. uint32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
  160. BX_UNUSED(flags);
  161. int32_t contextAttrs[9] =
  162. {
  163. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  164. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  165. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  166. WGL_CONTEXT_FLAGS_ARB, flags,
  167. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  168. #else
  169. WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
  170. WGL_CONTEXT_MINOR_VERSION_ARB, 1,
  171. 0, 0,
  172. 0, 0,
  173. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  174. 0
  175. };
  176. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  177. if (NULL == m_context)
  178. {
  179. // nVidia doesn't like context profile mask for contexts below 3.2?
  180. contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6];
  181. m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
  182. }
  183. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() );
  184. }
  185. wglMakeCurrent(NULL, NULL);
  186. wglDeleteContext(context);
  187. DestroyWindow(hwnd);
  188. if (NULL == m_context)
  189. {
  190. m_context = createContext(m_hdc);
  191. }
  192. int result = wglMakeCurrent(m_hdc, m_context);
  193. BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
  194. if (NULL != wglSwapIntervalEXT)
  195. {
  196. wglSwapIntervalEXT(0);
  197. }
  198. }
  199. import();
  200. }
  201. void GlContext::destroy()
  202. {
  203. if (NULL != g_bgfxHwnd)
  204. {
  205. wglMakeCurrent(NULL, NULL);
  206. wglDeleteContext(m_context);
  207. m_context = NULL;
  208. ReleaseDC(g_bgfxHwnd, m_hdc);
  209. m_hdc = NULL;
  210. }
  211. bx::dlclose(m_opengl32dll);
  212. m_opengl32dll = NULL;
  213. }
  214. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  215. {
  216. if (NULL != wglSwapIntervalEXT)
  217. {
  218. wglSwapIntervalEXT(_vsync ? 1 : 0);
  219. }
  220. }
  221. void GlContext::swap()
  222. {
  223. if (NULL != g_bgfxHwnd)
  224. {
  225. wglMakeCurrent(m_hdc, m_context);
  226. SwapBuffers(m_hdc);
  227. }
  228. }
  229. void GlContext::import()
  230. {
  231. # define GL_IMPORT(_optional, _proto, _func) \
  232. { \
  233. _func = (_proto)wglGetProcAddress(#_func); \
  234. if (_func == NULL) \
  235. { \
  236. _func = (_proto)bx::dlsym(m_opengl32dll, #_func); \
  237. } \
  238. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_func); \
  239. }
  240. # include "glimports.h"
  241. # undef GL_IMPORT
  242. }
  243. } // namespace bgfx
  244. # endif // BGFX_USE_WGL
  245. #endif // (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)