CmWin32GLSupport.cpp 13 KB

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