BsWin32GLSupport.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsWin32GLSupport.h"
  5. #include "BsGLTexture.h"
  6. #include "BsWin32Window.h"
  7. #include "BsGLRenderSystem.h"
  8. #include "BsWin32Context.h"
  9. #include "BsWin32VideoModeInfo.h"
  10. #include "BsException.h"
  11. #include "GL/wglew.h"
  12. #include <algorithm>
  13. GLenum __stdcall wglewContextInit (BansheeEngine::GLSupport *glSupport);
  14. namespace BansheeEngine
  15. {
  16. template<class C> void remove_duplicates(C& c)
  17. {
  18. std::sort(c.begin(), c.end());
  19. typename C::iterator p = std::unique(c.begin(), c.end());
  20. c.erase(p, c.end());
  21. }
  22. Win32GLSupport::Win32GLSupport()
  23. : mInitialWindow(0), mHasPixelFormatARB(false), mHasMultisample(false),
  24. mHasHardwareGamma(false), mHasAdvancedContext(false)
  25. {
  26. initialiseWGL();
  27. }
  28. RenderWindowPtr Win32GLSupport::newWindow(RENDER_WINDOW_DESC& desc, RenderWindowPtr parentWindow)
  29. {
  30. if(parentWindow != nullptr)
  31. {
  32. HWND hWnd;
  33. parentWindow->getCustomAttribute("WINDOW", &hWnd);
  34. desc.platformSpecific["parentWindowHandle"] = toString((UINT64)hWnd);
  35. }
  36. Win32Window* window = new (bs_alloc<Win32Window, PoolAlloc>()) Win32Window(desc, *this);
  37. if(!mInitialWindow)
  38. mInitialWindow = window;
  39. return RenderWindowPtr(window, &CoreObject::_deleteDelayed<Win32Window, PoolAlloc>);
  40. }
  41. void Win32GLSupport::start()
  42. {
  43. }
  44. void Win32GLSupport::stop()
  45. {
  46. mInitialWindow = nullptr;
  47. }
  48. void Win32GLSupport::initializeExtensions()
  49. {
  50. assert(mInitialWindow != nullptr);
  51. GLSupport::initializeExtensions();
  52. wglewContextInit(this);
  53. // Check for W32 specific extensions probe function
  54. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsString = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsString");
  55. if(_wglGetExtensionsString == nullptr)
  56. return;
  57. const char *wgl_extensions = _wglGetExtensionsString(mInitialWindow->_getHDC());
  58. // Parse them, and add them to the main list
  59. StringStream ext;
  60. String instr;
  61. ext << wgl_extensions;
  62. while(ext >> instr)
  63. {
  64. extensionList.insert(instr);
  65. }
  66. }
  67. Win32Context* Win32GLSupport::createContext(HDC hdc, HGLRC externalGlrc)
  68. {
  69. GLRenderSystem* rs = static_cast<GLRenderSystem*>(RenderSystem::instancePtr());
  70. // If RenderSystem has initialized a context use that, otherwise we create our own
  71. HGLRC glrc = externalGlrc;
  72. bool createdNew = false;
  73. if (!rs->_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
  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. rs->getMainContext()->setCurrent();
  104. glrc = wglGetCurrentContext();
  105. }
  106. return bs_new<Win32Context>(hdc, glrc, createdNew);
  107. }
  108. void* Win32GLSupport::getProcAddress(const String& procname)
  109. {
  110. return (void*)wglGetProcAddress(procname.c_str());
  111. }
  112. void Win32GLSupport::initialiseWGL()
  113. {
  114. // We need to create a dummy context in order to get functions
  115. // that allow us to create a more advanced context. It seems
  116. // 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.c_str());
  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(dummyText, dummyText, WS_POPUP | WS_CLIPCHILDREN,
  131. 0, 0, 32, 32, 0, 0, hinst, 0);
  132. if (hwnd == nullptr)
  133. BS_EXCEPT(RenderingAPIException, "CreateWindow() failed");
  134. HDC hdc = GetDC(hwnd);
  135. PIXELFORMATDESCRIPTOR pfd;
  136. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  137. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  138. pfd.nVersion = 1;
  139. pfd.cColorBits = 16;
  140. pfd.cDepthBits = 15;
  141. pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
  142. pfd.iPixelType = PFD_TYPE_RGBA;
  143. int format;
  144. if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
  145. SetPixelFormat(hdc, format, &pfd);
  146. HGLRC hrc = wglCreateContext(hdc);
  147. if (hrc)
  148. {
  149. HGLRC oldrc = wglGetCurrentContext();
  150. HDC oldhdc = wglGetCurrentDC();
  151. // if wglMakeCurrent fails, wglGetProcAddress will return null
  152. wglMakeCurrent(hdc, hrc);
  153. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
  154. (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  155. PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB =
  156. (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  157. if (_wglCreateContextAttribsARB != nullptr)
  158. {
  159. WGLEW_GET_FUN(__wglewCreateContextAttribsARB) = _wglCreateContextAttribsARB;
  160. mHasAdvancedContext = true;
  161. }
  162. // Check for pixel format and multisampling support
  163. if (_wglGetExtensionsStringARB != nullptr)
  164. {
  165. std::istringstream wglexts(_wglGetExtensionsStringARB(hdc));
  166. String ext;
  167. while (wglexts >> ext)
  168. {
  169. if (ext == "WGL_ARB_pixel_format")
  170. mHasPixelFormatARB = true;
  171. else if (ext == "WGL_ARB_multisample")
  172. mHasMultisample = true;
  173. else if (ext == "WGL_EXT_framebuffer_sRGB")
  174. mHasHardwareGamma = true;
  175. }
  176. }
  177. if (mHasPixelFormatARB && mHasMultisample)
  178. {
  179. // Enumerate all formats w/ multisampling
  180. static const int iattr[] = {
  181. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  182. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  183. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  184. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  185. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  186. WGL_SAMPLES_ARB, 2,
  187. 0
  188. };
  189. int formats[256];
  190. unsigned int count;
  191. WGLEW_GET_FUN(__wglewChoosePixelFormatARB) = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  192. if (WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, iattr, 0, 256, formats, &count))
  193. {
  194. // determine what multisampling levels are offered
  195. int query = WGL_SAMPLES_ARB, samples;
  196. for (unsigned int i = 0; i < count; ++i)
  197. {
  198. PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB =
  199. (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)
  200. wglGetProcAddress("wglGetPixelFormatAttribivARB");
  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)
  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. pfd.cDepthBits = 24;
  231. pfd.cStencilBits = 8;
  232. int format = 0;
  233. int useHwGamma = hwGamma? GL_TRUE : GL_FALSE;
  234. if (multisample && (!mHasMultisample || !mHasPixelFormatARB))
  235. return false;
  236. if (hwGamma && !mHasHardwareGamma)
  237. return false;
  238. if ((multisample || hwGamma) && WGLEW_GET_FUN(__wglewChoosePixelFormatARB))
  239. {
  240. // Use WGL to test extended caps (multisample, sRGB)
  241. Vector<int> attribList;
  242. attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE);
  243. attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE);
  244. attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE);
  245. attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE);
  246. attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB);
  247. attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(pfd.cColorBits);
  248. attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(pfd.cAlphaBits);
  249. attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(24);
  250. attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(8);
  251. attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(multisample);
  252. if (useHwGamma && checkExtension("WGL_EXT_framebuffer_sRGB"))
  253. {
  254. attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE);
  255. }
  256. // terminator
  257. attribList.push_back(0);
  258. UINT nformats;
  259. if (!WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, &(attribList[0]), NULL, 1, &format, &nformats) || nformats <= 0)
  260. return false;
  261. }
  262. else
  263. {
  264. format = ChoosePixelFormat(hdc, &pfd);
  265. }
  266. return (format && SetPixelFormat(hdc, format, &pfd));
  267. }
  268. VideoModeInfoPtr Win32GLSupport::getVideoModeInfo() const
  269. {
  270. return bs_shared_ptr<Win32VideoModeInfo>();
  271. }
  272. String translateWGLError()
  273. {
  274. int winError = GetLastError();
  275. char errDesc[255];
  276. int i;
  277. // Try windows errors first
  278. i = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  279. NULL, winError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  280. (LPTSTR) errDesc, 255, NULL);
  281. return String(errDesc);
  282. }
  283. }