BsWin32GLSupport.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Win32/BsWin32GLSupport.h"
  4. #include "BsGLTexture.h"
  5. #include "Win32/BsWin32RenderWindow.h"
  6. #include "BsGLRenderAPI.h"
  7. #include "Win32/BsWin32Context.h"
  8. #include "Win32/BsWin32VideoModeInfo.h"
  9. #include "Error/BsException.h"
  10. #include "GL/wglew.h"
  11. #include <algorithm>
  12. GLenum __stdcall wglewContextInit (bs::ct::GLSupport* glSupport);
  13. namespace bs { namespace ct
  14. {
  15. template<class C> void remove_duplicates(C& c)
  16. {
  17. std::sort(c.begin(), c.end());
  18. typename C::iterator p = std::unique(c.begin(), c.end());
  19. c.erase(p, c.end());
  20. }
  21. Win32GLSupport::Win32GLSupport()
  22. : mInitialWindow(nullptr), mHasPixelFormatARB(false), mHasMultisample(false), mHasHardwareGamma(false)
  23. , mHasAdvancedContext(false)
  24. {
  25. initialiseWGL();
  26. }
  27. SPtr<bs::RenderWindow> Win32GLSupport::newWindow(RENDER_WINDOW_DESC& desc, UINT32 windowId, SPtr<bs::RenderWindow> parentWindow)
  28. {
  29. if(parentWindow != nullptr)
  30. {
  31. UINT64 hWnd;
  32. parentWindow->getCustomAttribute("WINDOW", &hWnd);
  33. desc.platformSpecific["parentWindowHandle"] = toString(hWnd);
  34. }
  35. bs::Win32RenderWindow* window = new (bs_alloc<bs::Win32RenderWindow>()) bs::Win32RenderWindow(desc, windowId, *this);
  36. return SPtr<bs::RenderWindow>(window, &bs::CoreObject::_delete<bs::Win32RenderWindow, GenAlloc>);
  37. }
  38. void Win32GLSupport::_notifyWindowCreated(Win32RenderWindow* window)
  39. {
  40. if (!mInitialWindow)
  41. mInitialWindow = window;
  42. }
  43. void Win32GLSupport::start()
  44. {
  45. }
  46. void Win32GLSupport::stop()
  47. {
  48. mInitialWindow = nullptr;
  49. }
  50. void Win32GLSupport::initializeExtensions()
  51. {
  52. assert(mInitialWindow != nullptr);
  53. GLSupport::initializeExtensions();
  54. wglewContextInit(this);
  55. // Check for W32 specific extensions probe function
  56. auto _wglGetExtensionsString = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsString");
  57. if(_wglGetExtensionsString == nullptr)
  58. return;
  59. const char* wglExtensions = _wglGetExtensionsString(mInitialWindow->_getHDC());
  60. // Parse them, and add them to the main list
  61. StringStream ext;
  62. ext << wglExtensions;
  63. String instr;
  64. while (ext >> instr)
  65. extensionList.insert(instr);
  66. }
  67. SPtr<Win32Context> Win32GLSupport::createContext(HDC hdc, HGLRC externalGlrc)
  68. {
  69. GLRenderAPI* rapi = static_cast<GLRenderAPI*>(RenderAPI::instancePtr());
  70. // If RenderAPI has initialized a context use that, otherwise we create our own
  71. HGLRC glrc = externalGlrc;
  72. bool createdNew = false;
  73. if (!rapi->_isContextInitialized())
  74. {
  75. if (externalGlrc == 0)
  76. {
  77. if (mHasAdvancedContext)
  78. {
  79. int attribs[40];
  80. int i = 0;
  81. attribs[i++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
  82. attribs[i++] = 4;
  83. attribs[i++] = WGL_CONTEXT_MINOR_VERSION_ARB;
  84. attribs[i++] = 3;
  85. attribs[i++] = WGL_CONTEXT_PROFILE_MASK_ARB;
  86. attribs[i++] = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
  87. #if (BS_DEBUG_MODE && (BS_OPENGL_4_3 || BS_OPENGLES_3_2))
  88. attribs[i++] = WGL_CONTEXT_FLAGS_ARB;
  89. attribs[i++] = WGL_CONTEXT_DEBUG_BIT_ARB;
  90. #endif
  91. attribs[i++] = 0;
  92. glrc = wglCreateContextAttribsARB(hdc, 0, attribs);
  93. }
  94. else
  95. glrc = wglCreateContext(hdc);
  96. if (glrc == 0)
  97. BS_EXCEPT(RenderingAPIException, "wglCreateContext failed: " + translateWGLError());
  98. createdNew = true;
  99. }
  100. }
  101. else
  102. {
  103. glrc = wglGetCurrentContext();
  104. }
  105. return bs_shared_ptr_new<Win32Context>(hdc, glrc, createdNew);
  106. }
  107. void* Win32GLSupport::getProcAddress(const String& procname)
  108. {
  109. return (void*)wglGetProcAddress(procname.c_str());
  110. }
  111. void Win32GLSupport::initialiseWGL()
  112. {
  113. // We need to create a dummy context in order to get functions that allow us to create a more advanced context.
  114. // It seems hacky but that's the only way to do it.
  115. LPCSTR dummyText = "Dummy";
  116. #ifdef BS_STATIC_LIB
  117. HINSTANCE hinst = GetModuleHandle(NULL);
  118. #else
  119. HINSTANCE hinst = GetModuleHandle(MODULE_NAME);
  120. #endif
  121. WNDCLASS dummyClass;
  122. memset(&dummyClass, 0, sizeof(WNDCLASS));
  123. dummyClass.style = CS_OWNDC;
  124. dummyClass.hInstance = hinst;
  125. dummyClass.lpfnWndProc = dummyWndProc;
  126. dummyClass.lpszClassName = dummyText;
  127. RegisterClass(&dummyClass);
  128. HWND hwnd = CreateWindow(
  129. dummyText,
  130. dummyText,
  131. WS_POPUP | WS_CLIPCHILDREN,
  132. 0,
  133. 0,
  134. 32,
  135. 32,
  136. 0,
  137. 0,
  138. hinst,
  139. 0);
  140. if (hwnd == nullptr)
  141. BS_EXCEPT(RenderingAPIException, "CreateWindow() failed");
  142. HDC hdc = GetDC(hwnd);
  143. PIXELFORMATDESCRIPTOR pfd;
  144. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  145. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  146. pfd.nVersion = 1;
  147. pfd.cColorBits = 16;
  148. pfd.cDepthBits = 15;
  149. pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
  150. pfd.iPixelType = PFD_TYPE_RGBA;
  151. int format;
  152. if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
  153. SetPixelFormat(hdc, format, &pfd);
  154. HGLRC hrc = wglCreateContext(hdc);
  155. if (hrc)
  156. {
  157. HGLRC oldrc = wglGetCurrentContext();
  158. HDC oldhdc = wglGetCurrentDC();
  159. // If wglMakeCurrent fails, wglGetProcAddress will return null
  160. wglMakeCurrent(hdc, hrc);
  161. auto _wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  162. auto _wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  163. if (_wglCreateContextAttribsARB != nullptr)
  164. {
  165. WGLEW_GET_FUN(__wglewCreateContextAttribsARB) = _wglCreateContextAttribsARB;
  166. mHasAdvancedContext = true;
  167. }
  168. // Check for pixel format and multisampling support
  169. if (_wglGetExtensionsStringARB != nullptr)
  170. {
  171. std::istringstream wglexts(_wglGetExtensionsStringARB(hdc));
  172. String ext;
  173. while (wglexts >> ext)
  174. {
  175. if (ext == "WGL_ARB_pixel_format")
  176. mHasPixelFormatARB = true;
  177. else if (ext == "WGL_ARB_multisample")
  178. mHasMultisample = true;
  179. else if (ext == "WGL_EXT_framebuffer_sRGB")
  180. mHasHardwareGamma = true;
  181. }
  182. }
  183. if (mHasPixelFormatARB && mHasMultisample)
  184. {
  185. // Enumerate all formats w/ multisampling
  186. static const int iattr[] = {
  187. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  188. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  189. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  190. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  191. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  192. WGL_SAMPLES_ARB, 2,
  193. 0
  194. };
  195. int formats[256];
  196. unsigned int count;
  197. WGLEW_GET_FUN(__wglewChoosePixelFormatARB) = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  198. PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB =
  199. (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
  200. if (WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, iattr, 0, 256, formats, &count))
  201. {
  202. // Determine what multisampling levels are offered
  203. int query = WGL_SAMPLES_ARB, samples;
  204. for (unsigned int i = 0; i < count; ++i)
  205. {
  206. if (_wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, &query, &samples))
  207. {
  208. mMultisampleLevels.push_back(samples);
  209. }
  210. }
  211. remove_duplicates(mMultisampleLevels);
  212. }
  213. }
  214. wglMakeCurrent(oldhdc, oldrc);
  215. wglDeleteContext(hrc);
  216. }
  217. // Clean up our dummy window and class
  218. DestroyWindow(hwnd);
  219. UnregisterClass(dummyText, hinst);
  220. }
  221. LRESULT Win32GLSupport::dummyWndProc(HWND hwnd, UINT umsg, WPARAM wp, LPARAM lp)
  222. {
  223. return DefWindowProc(hwnd, umsg, wp, lp);
  224. }
  225. bool Win32GLSupport::selectPixelFormat(HDC hdc, int colorDepth, int multisample, bool hwGamma, bool depthStencil)
  226. {
  227. PIXELFORMATDESCRIPTOR pfd;
  228. memset(&pfd, 0, sizeof(pfd));
  229. pfd.nSize = sizeof(pfd);
  230. pfd.nVersion = 1;
  231. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  232. pfd.iPixelType = PFD_TYPE_RGBA;
  233. pfd.cColorBits = (colorDepth > 16)? 24 : colorDepth;
  234. pfd.cAlphaBits = (colorDepth > 16)? 8 : 0;
  235. if (depthStencil)
  236. {
  237. pfd.cDepthBits = 24;
  238. pfd.cStencilBits = 8;
  239. }
  240. int format = 0;
  241. int useHwGamma = hwGamma? GL_TRUE : GL_FALSE;
  242. if (multisample && (!mHasMultisample || !mHasPixelFormatARB))
  243. return false;
  244. if (hwGamma && !mHasHardwareGamma)
  245. return false;
  246. if ((multisample || hwGamma) && WGLEW_GET_FUN(__wglewChoosePixelFormatARB))
  247. {
  248. // Use WGL to test extended caps (multisample, sRGB)
  249. Vector<int> attribList;
  250. attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE);
  251. attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE);
  252. attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE);
  253. attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE);
  254. attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB);
  255. attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(pfd.cColorBits);
  256. attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(pfd.cAlphaBits);
  257. attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(pfd.cDepthBits);
  258. attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(pfd.cStencilBits);
  259. attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(multisample);
  260. if (useHwGamma && checkExtension("WGL_EXT_framebuffer_sRGB"))
  261. attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE);
  262. attribList.push_back(0); // Terminator
  263. UINT numFormats;
  264. if (!WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, &(attribList[0]), NULL, 1, &format, &numFormats) || numFormats <= 0)
  265. return false;
  266. }
  267. else
  268. {
  269. format = ChoosePixelFormat(hdc, &pfd);
  270. }
  271. return format && SetPixelFormat(hdc, format, &pfd);
  272. }
  273. SPtr<VideoModeInfo> Win32GLSupport::getVideoModeInfo() const
  274. {
  275. return bs_shared_ptr_new<Win32VideoModeInfo>();
  276. }
  277. String translateWGLError()
  278. {
  279. int winError = GetLastError();
  280. char errDesc[255];
  281. FormatMessage(
  282. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  283. NULL,
  284. winError,
  285. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  286. (LPTSTR) errDesc,
  287. 255,
  288. NULL);
  289. return String(errDesc);
  290. }
  291. }}