BsWin32GLSupport.cpp 10 KB

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