glcontext_wgl.cpp 8.8 KB

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