BsWin32GLSupport.cpp 10 KB

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