CmWin32GLSupport.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "CmException.h"
  2. #include <algorithm>
  3. #include "CmWin32GLSupport.h"
  4. #include "CmGLTexture.h"
  5. #include "CmWin32Window.h"
  6. #if CM_THREAD_SUPPORT != 1
  7. GLenum __stdcall wglewContextInit (BansheeEngine::GLSupport *glSupport);
  8. #endif
  9. namespace BansheeEngine
  10. {
  11. Win32GLSupport::Win32GLSupport()
  12. : mInitialWindow(0)
  13. , mHasPixelFormatARB(false)
  14. , mHasMultisample(false)
  15. , mHasHardwareGamma(false)
  16. {
  17. // immediately test WGL_ARB_pixel_format and FSAA support
  18. // so we can set configuration options appropriately
  19. initialiseWGL();
  20. }
  21. template<class C> void remove_duplicates(C& c)
  22. {
  23. std::sort(c.begin(), c.end());
  24. typename C::iterator p = std::unique(c.begin(), c.end());
  25. c.erase(p, c.end());
  26. }
  27. BOOL CALLBACK Win32GLSupport::sCreateMonitorsInfoEnumProc(
  28. HMONITOR hMonitor, // handle to display monitor
  29. HDC hdcMonitor, // handle to monitor DC
  30. LPRECT lprcMonitor, // monitor intersection rectangle
  31. LPARAM dwData // data
  32. )
  33. {
  34. DisplayMonitorInfoList* pArrMonitorsInfo = (DisplayMonitorInfoList*)dwData;
  35. // Get monitor info
  36. DisplayMonitorInfo displayMonitorInfo;
  37. displayMonitorInfo.hMonitor = hMonitor;
  38. memset(&displayMonitorInfo.monitorInfoEx, 0, sizeof(MONITORINFOEX));
  39. displayMonitorInfo.monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  40. GetMonitorInfo(hMonitor, &displayMonitorInfo.monitorInfoEx);
  41. pArrMonitorsInfo->push_back(displayMonitorInfo);
  42. return TRUE;
  43. }
  44. RenderWindowPtr Win32GLSupport::newWindow(RENDER_WINDOW_DESC& desc, RenderWindowPtr parentWindow)
  45. {
  46. if(parentWindow != nullptr)
  47. {
  48. HWND hWnd;
  49. parentWindow->getCustomAttribute("WINDOW", &hWnd);
  50. desc.platformSpecific["parentWindowHandle"] = toString((UINT64)hWnd);
  51. }
  52. Win32Window* window = new (cm_alloc<Win32Window, PoolAlloc>()) Win32Window(desc, *this);
  53. // TODO - Looking for monitors is disabled for now, as it should be done on the core thread and I need to port it but
  54. // I don't feel like it at the moment. Plus I'll probably implemented a more streamlined approach to this anyway.
  55. //HMONITOR hMonitor = NULL;
  56. //int monitorIndex = desc.monitorIndex;
  57. //
  58. //// If monitor index found, try to assign the monitor handle based on it.
  59. //if(monitorIndex >= 0)
  60. //{
  61. // if (mMonitorInfoList.empty())
  62. // EnumDisplayMonitors(NULL, NULL, sCreateMonitorsInfoEnumProc, (LPARAM)&mMonitorInfoList);
  63. // if (monitorIndex < (int)mMonitorInfoList.size())
  64. // hMonitor = mMonitorInfoList[monitorIndex].hMonitor;
  65. //}
  66. //// If we didn't specified the monitor index, or if it didn't find it
  67. //if (hMonitor == NULL)
  68. //{
  69. // POINT windowAnchorPoint;
  70. //
  71. // // Fill in anchor point.
  72. // windowAnchorPoint.x = desc.left;
  73. // windowAnchorPoint.y = desc.top;
  74. // // Get the nearest monitor to this window.
  75. // hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  76. //}
  77. //RENDER_WINDOW_DESC newDesc = desc;
  78. //newDesc.platformSpecific["monitorHandle"] = toString((size_t)hMonitor);
  79. //window->initialize(newDesc);
  80. if(!mInitialWindow)
  81. mInitialWindow = window;
  82. return RenderWindowPtr(window, &CoreObject::_deleteDelayed<Win32Window, PoolAlloc>);
  83. }
  84. void Win32GLSupport::start()
  85. {
  86. }
  87. void Win32GLSupport::stop()
  88. {
  89. mInitialWindow = 0; // Since there is no removeWindow, although there should be...
  90. }
  91. void Win32GLSupport::initialiseExtensions()
  92. {
  93. assert(mInitialWindow);
  94. // First, initialise the normal extensions
  95. GLSupport::initialiseExtensions();
  96. // wglew init
  97. #if CM_THREAD_SUPPORT != 1
  98. wglewContextInit(this);
  99. #endif
  100. // Check for W32 specific extensions probe function
  101. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsString =
  102. (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsString");
  103. if(!_wglGetExtensionsString)
  104. return;
  105. const char *wgl_extensions = _wglGetExtensionsString(mInitialWindow->_getHDC());
  106. // Parse them, and add them to the main list
  107. StringStream ext;
  108. String instr;
  109. ext << wgl_extensions;
  110. while(ext >> instr)
  111. {
  112. extensionList.insert(instr);
  113. }
  114. }
  115. void* Win32GLSupport::getProcAddress(const String& procname)
  116. {
  117. return (void*)wglGetProcAddress( procname.c_str() );
  118. }
  119. void Win32GLSupport::initialiseWGL()
  120. {
  121. // wglGetProcAddress does not work without an active OpenGL context,
  122. // but we need wglChoosePixelFormatARB's address before we can
  123. // create our main window. Thank you very much, Microsoft!
  124. //
  125. // The solution is to create a dummy OpenGL window first, and then
  126. // test for WGL_ARB_pixel_format support. If it is not supported,
  127. // we make sure to never call the ARB pixel format functions.
  128. //
  129. // If is is supported, we call the pixel format functions at least once
  130. // to initialise them (pointers are stored by glprocs.h). We can also
  131. // take this opportunity to enumerate the valid FSAA modes.
  132. LPCSTR dummyText = "OgreWglDummy";
  133. #ifdef CM_STATIC_LIB
  134. HINSTANCE hinst = GetModuleHandle( NULL );
  135. #else
  136. # if CM_DEBUG_MODE == 1
  137. HINSTANCE hinst = GetModuleHandle("RenderSystem_GL_d.dll");
  138. # else
  139. HINSTANCE hinst = GetModuleHandle("RenderSystem_GL.dll");
  140. # endif
  141. #endif
  142. WNDCLASS dummyClass;
  143. memset(&dummyClass, 0, sizeof(WNDCLASS));
  144. dummyClass.style = CS_OWNDC;
  145. dummyClass.hInstance = hinst;
  146. dummyClass.lpfnWndProc = dummyWndProc;
  147. dummyClass.lpszClassName = dummyText;
  148. RegisterClass(&dummyClass);
  149. HWND hwnd = CreateWindow(dummyText, dummyText,
  150. WS_POPUP | WS_CLIPCHILDREN,
  151. 0, 0, 32, 32, 0, 0, hinst, 0);
  152. // if a simple CreateWindow fails, then boy are we in trouble...
  153. if (hwnd == NULL)
  154. CM_EXCEPT(RenderingAPIException, "CreateWindow() failed");
  155. // no chance of failure and no need to release thanks to CS_OWNDC
  156. HDC hdc = GetDC(hwnd);
  157. // assign a simple OpenGL pixel format that everyone supports
  158. PIXELFORMATDESCRIPTOR pfd;
  159. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  160. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  161. pfd.nVersion = 1;
  162. pfd.cColorBits = 16;
  163. pfd.cDepthBits = 15;
  164. pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
  165. pfd.iPixelType = PFD_TYPE_RGBA;
  166. // if these fail, wglCreateContext will also quietly fail
  167. int format;
  168. if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
  169. SetPixelFormat(hdc, format, &pfd);
  170. HGLRC hrc = wglCreateContext(hdc);
  171. if (hrc)
  172. {
  173. HGLRC oldrc = wglGetCurrentContext();
  174. HDC oldhdc = wglGetCurrentDC();
  175. // if wglMakeCurrent fails, wglGetProcAddress will return null
  176. wglMakeCurrent(hdc, hrc);
  177. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
  178. (PFNWGLGETEXTENSIONSSTRINGARBPROC)
  179. wglGetProcAddress("wglGetExtensionsStringARB");
  180. // check for pixel format and multisampling support
  181. if (_wglGetExtensionsStringARB)
  182. {
  183. std::istringstream wglexts(_wglGetExtensionsStringARB(hdc));
  184. String ext;
  185. while (wglexts >> ext)
  186. {
  187. if (ext == "WGL_ARB_pixel_format")
  188. mHasPixelFormatARB = true;
  189. else if (ext == "WGL_ARB_multisample")
  190. mHasMultisample = true;
  191. else if (ext == "WGL_EXT_framebuffer_sRGB")
  192. mHasHardwareGamma = true;
  193. }
  194. }
  195. if (mHasPixelFormatARB && mHasMultisample)
  196. {
  197. // enumerate all formats w/ multisampling
  198. static const int iattr[] = {
  199. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  200. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  201. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  202. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  203. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  204. /* We are no matter about the colour, depth and stencil buffers here
  205. WGL_COLOR_BITS_ARB, 24,
  206. WGL_ALPHA_BITS_ARB, 8,
  207. WGL_DEPTH_BITS_ARB, 24,
  208. WGL_STENCIL_BITS_ARB, 8,
  209. */
  210. WGL_SAMPLES_ARB, 2,
  211. 0
  212. };
  213. int formats[256];
  214. unsigned int count;
  215. // cheating here. wglChoosePixelFormatARB procc address needed later on
  216. // when a valid GL context does not exist and glew is not initialized yet.
  217. WGLEW_GET_FUN(__wglewChoosePixelFormatARB) =
  218. (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  219. if (WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, iattr, 0, 256, formats, &count))
  220. {
  221. // determine what multisampling levels are offered
  222. int query = WGL_SAMPLES_ARB, samples;
  223. for (unsigned int i = 0; i < count; ++i)
  224. {
  225. PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB =
  226. (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)
  227. wglGetProcAddress("wglGetPixelFormatAttribivARB");
  228. if (_wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, &query, &samples))
  229. {
  230. mFSAALevels.push_back(samples);
  231. }
  232. }
  233. remove_duplicates(mFSAALevels);
  234. }
  235. }
  236. wglMakeCurrent(oldhdc, oldrc);
  237. wglDeleteContext(hrc);
  238. }
  239. // clean up our dummy window and class
  240. DestroyWindow(hwnd);
  241. UnregisterClass(dummyText, hinst);
  242. }
  243. LRESULT Win32GLSupport::dummyWndProc(HWND hwnd, UINT umsg, WPARAM wp, LPARAM lp)
  244. {
  245. return DefWindowProc(hwnd, umsg, wp, lp);
  246. }
  247. bool Win32GLSupport::selectPixelFormat(HDC hdc, int colourDepth, int multisample, bool hwGamma)
  248. {
  249. PIXELFORMATDESCRIPTOR pfd;
  250. memset(&pfd, 0, sizeof(pfd));
  251. pfd.nSize = sizeof(pfd);
  252. pfd.nVersion = 1;
  253. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  254. pfd.iPixelType = PFD_TYPE_RGBA;
  255. pfd.cColorBits = (colourDepth > 16)? 24 : colourDepth;
  256. pfd.cAlphaBits = (colourDepth > 16)? 8 : 0;
  257. pfd.cDepthBits = 24;
  258. pfd.cStencilBits = 8;
  259. int format = 0;
  260. int useHwGamma = hwGamma? GL_TRUE : GL_FALSE;
  261. if (multisample && (!mHasMultisample || !mHasPixelFormatARB))
  262. return false;
  263. if (hwGamma && !mHasHardwareGamma)
  264. return false;
  265. if ((multisample || hwGamma) && WGLEW_GET_FUN(__wglewChoosePixelFormatARB))
  266. {
  267. // Use WGL to test extended caps (multisample, sRGB)
  268. Vector<int>::type attribList;
  269. attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE);
  270. attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE);
  271. attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE);
  272. attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE);
  273. attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB);
  274. attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(pfd.cColorBits);
  275. attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(pfd.cAlphaBits);
  276. attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(24);
  277. attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(8);
  278. attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(multisample);
  279. if (useHwGamma && checkExtension("WGL_EXT_framebuffer_sRGB"))
  280. {
  281. attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE);
  282. }
  283. // terminator
  284. attribList.push_back(0);
  285. UINT nformats;
  286. // ChoosePixelFormatARB proc address was obtained when setting up a dummy GL context in initialiseWGL()
  287. // since glew hasn't been initialized yet, we have to cheat and use the previously obtained address
  288. if (!WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, &(attribList[0]), NULL, 1, &format, &nformats) || nformats <= 0)
  289. return false;
  290. }
  291. else
  292. {
  293. format = ChoosePixelFormat(hdc, &pfd);
  294. }
  295. return (format && SetPixelFormat(hdc, format, &pfd));
  296. }
  297. bool Win32GLSupport::supportsPBuffers()
  298. {
  299. return WGLEW_GET_FUN(__WGLEW_ARB_pbuffer) != GL_FALSE;
  300. }
  301. unsigned int Win32GLSupport::getDisplayMonitorCount() const
  302. {
  303. if (mMonitorInfoList.empty())
  304. EnumDisplayMonitors(NULL, NULL, sCreateMonitorsInfoEnumProc, (LPARAM)&mMonitorInfoList);
  305. return (unsigned int)mMonitorInfoList.size();
  306. }
  307. String translateWGLError()
  308. {
  309. int winError = GetLastError();
  310. char errDesc[255];
  311. int i;
  312. // Try windows errors first
  313. i = FormatMessage(
  314. FORMAT_MESSAGE_FROM_SYSTEM |
  315. FORMAT_MESSAGE_IGNORE_INSERTS,
  316. NULL,
  317. winError,
  318. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  319. (LPTSTR) errDesc,
  320. 255,
  321. NULL
  322. );
  323. return String(errDesc);
  324. }
  325. }