CmWin32GLSupport.cpp 13 KB

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