CmWin32GLSupport.cpp 12 KB

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