CmWin32GLSupport.cpp 13 KB

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