CmWin32GLSupport.cpp 12 KB

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