2
0

BsWin32GLSupport.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 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
  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
  116. // that allow us to create a more advanced context. It seems
  117. // hacky but that's the only way to do it.
  118. LPCSTR dummyText = "Dummy";
  119. #ifdef BS_STATIC_LIB
  120. HINSTANCE hinst = GetModuleHandle(NULL);
  121. #else
  122. HINSTANCE hinst = GetModuleHandle(MODULE_NAME);
  123. #endif
  124. WNDCLASS dummyClass;
  125. memset(&dummyClass, 0, sizeof(WNDCLASS));
  126. dummyClass.style = CS_OWNDC;
  127. dummyClass.hInstance = hinst;
  128. dummyClass.lpfnWndProc = dummyWndProc;
  129. dummyClass.lpszClassName = dummyText;
  130. RegisterClass(&dummyClass);
  131. HWND hwnd = CreateWindow(dummyText, dummyText, WS_POPUP | WS_CLIPCHILDREN,
  132. 0, 0, 32, 32, 0, 0, hinst, 0);
  133. if (hwnd == nullptr)
  134. BS_EXCEPT(RenderingAPIException, "CreateWindow() failed");
  135. HDC hdc = GetDC(hwnd);
  136. PIXELFORMATDESCRIPTOR pfd;
  137. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  138. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  139. pfd.nVersion = 1;
  140. pfd.cColorBits = 16;
  141. pfd.cDepthBits = 15;
  142. pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
  143. pfd.iPixelType = PFD_TYPE_RGBA;
  144. int format;
  145. if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
  146. SetPixelFormat(hdc, format, &pfd);
  147. HGLRC hrc = wglCreateContext(hdc);
  148. if (hrc)
  149. {
  150. HGLRC oldrc = wglGetCurrentContext();
  151. HDC oldhdc = wglGetCurrentDC();
  152. // if wglMakeCurrent fails, wglGetProcAddress will return null
  153. wglMakeCurrent(hdc, hrc);
  154. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
  155. (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  156. PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB =
  157. (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  158. if (_wglCreateContextAttribsARB != nullptr)
  159. {
  160. WGLEW_GET_FUN(__wglewCreateContextAttribsARB) = _wglCreateContextAttribsARB;
  161. mHasAdvancedContext = true;
  162. }
  163. // Check for pixel format and multisampling support
  164. if (_wglGetExtensionsStringARB != nullptr)
  165. {
  166. std::istringstream wglexts(_wglGetExtensionsStringARB(hdc));
  167. String ext;
  168. while (wglexts >> ext)
  169. {
  170. if (ext == "WGL_ARB_pixel_format")
  171. mHasPixelFormatARB = true;
  172. else if (ext == "WGL_ARB_multisample")
  173. mHasMultisample = true;
  174. else if (ext == "WGL_EXT_framebuffer_sRGB")
  175. mHasHardwareGamma = true;
  176. }
  177. }
  178. if (mHasPixelFormatARB && mHasMultisample)
  179. {
  180. // Enumerate all formats w/ multisampling
  181. static const int iattr[] = {
  182. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  183. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  184. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  185. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  186. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  187. WGL_SAMPLES_ARB, 2,
  188. 0
  189. };
  190. int formats[256];
  191. unsigned int count;
  192. WGLEW_GET_FUN(__wglewChoosePixelFormatARB) = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  193. PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB =
  194. (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
  195. if (WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, iattr, 0, 256, formats, &count))
  196. {
  197. // determine what multisampling levels are offered
  198. int query = WGL_SAMPLES_ARB, samples;
  199. for (unsigned int i = 0; i < count; ++i)
  200. {
  201. if (_wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, &query, &samples))
  202. {
  203. mMultisampleLevels.push_back(samples);
  204. }
  205. }
  206. remove_duplicates(mMultisampleLevels);
  207. }
  208. }
  209. wglMakeCurrent(oldhdc, oldrc);
  210. wglDeleteContext(hrc);
  211. }
  212. // Clean up our dummy window and class
  213. DestroyWindow(hwnd);
  214. UnregisterClass(dummyText, hinst);
  215. }
  216. LRESULT Win32GLSupport::dummyWndProc(HWND hwnd, UINT umsg, WPARAM wp, LPARAM lp)
  217. {
  218. return DefWindowProc(hwnd, umsg, wp, lp);
  219. }
  220. bool Win32GLSupport::selectPixelFormat(HDC hdc, int colorDepth, int multisample, bool hwGamma, bool depthStencil)
  221. {
  222. PIXELFORMATDESCRIPTOR pfd;
  223. memset(&pfd, 0, sizeof(pfd));
  224. pfd.nSize = sizeof(pfd);
  225. pfd.nVersion = 1;
  226. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  227. pfd.iPixelType = PFD_TYPE_RGBA;
  228. pfd.cColorBits = (colorDepth > 16)? 24 : colorDepth;
  229. pfd.cAlphaBits = (colorDepth > 16)? 8 : 0;
  230. if (depthStencil)
  231. {
  232. pfd.cDepthBits = 24;
  233. pfd.cStencilBits = 8;
  234. }
  235. int format = 0;
  236. int useHwGamma = hwGamma? GL_TRUE : GL_FALSE;
  237. if (multisample && (!mHasMultisample || !mHasPixelFormatARB))
  238. return false;
  239. if (hwGamma && !mHasHardwareGamma)
  240. return false;
  241. if ((multisample || hwGamma) && WGLEW_GET_FUN(__wglewChoosePixelFormatARB))
  242. {
  243. // Use WGL to test extended caps (multisample, sRGB)
  244. Vector<int> attribList;
  245. attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE);
  246. attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE);
  247. attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE);
  248. attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE);
  249. attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB);
  250. attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(pfd.cColorBits);
  251. attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(pfd.cAlphaBits);
  252. attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(pfd.cDepthBits);
  253. attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(pfd.cStencilBits);
  254. attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(multisample);
  255. if (useHwGamma && checkExtension("WGL_EXT_framebuffer_sRGB"))
  256. {
  257. attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE);
  258. }
  259. // terminator
  260. attribList.push_back(0);
  261. UINT nformats;
  262. if (!WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, &(attribList[0]), NULL, 1, &format, &nformats) || nformats <= 0)
  263. return false;
  264. }
  265. else
  266. {
  267. format = ChoosePixelFormat(hdc, &pfd);
  268. }
  269. return (format && SetPixelFormat(hdc, format, &pfd));
  270. }
  271. SPtr<VideoModeInfo> Win32GLSupport::getVideoModeInfo() const
  272. {
  273. return bs_shared_ptr_new<Win32VideoModeInfo>();
  274. }
  275. String translateWGLError()
  276. {
  277. int winError = GetLastError();
  278. char errDesc[255];
  279. int i;
  280. // Try windows errors first
  281. i = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  282. NULL, winError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  283. (LPTSTR) errDesc, 255, NULL);
  284. return String(errDesc);
  285. }
  286. }}