BsWin32GLSupport.cpp 10 KB

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