BsWin32GLSupport.cpp 9.9 KB

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