CmWin32GLSupport.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. #include "CmWin32RenderTexture.h"
  8. using namespace CamelotEngine;
  9. #if CM_THREAD_SUPPORT != 1
  10. GLenum wglewContextInit (CamelotEngine::GLSupport *glSupport);
  11. #endif
  12. namespace CamelotEngine {
  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. void Win32GLSupport::addConfig()
  30. {
  31. //TODO: EnumDisplayDevices http://msdn.microsoft.com/library/en-us/gdi/devcons_2303.asp
  32. /*vector<string> DisplayDevices;
  33. DISPLAY_DEVICE DisplayDevice;
  34. DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
  35. DWORD i=0;
  36. while (EnumDisplayDevices(NULL, i++, &DisplayDevice, 0) {
  37. DisplayDevices.push_back(DisplayDevice.DeviceName);
  38. }*/
  39. ConfigOption optFullScreen;
  40. ConfigOption optVideoMode;
  41. ConfigOption optColourDepth;
  42. ConfigOption optDisplayFrequency;
  43. ConfigOption optVSync;
  44. ConfigOption optVSyncInterval;
  45. ConfigOption optFSAA;
  46. ConfigOption optRTTMode;
  47. ConfigOption optSRGB;
  48. // FS setting possiblities
  49. optFullScreen.name = "Full Screen";
  50. optFullScreen.possibleValues.push_back("Yes");
  51. optFullScreen.possibleValues.push_back("No");
  52. optFullScreen.currentValue = "Yes";
  53. optFullScreen.immutable = false;
  54. // Video mode possiblities
  55. DEVMODE DevMode;
  56. DevMode.dmSize = sizeof(DEVMODE);
  57. optVideoMode.name = "Video Mode";
  58. optVideoMode.immutable = false;
  59. for (DWORD i = 0; EnumDisplaySettings(NULL, i, &DevMode); ++i)
  60. {
  61. if (DevMode.dmBitsPerPel < 16 || DevMode.dmPelsHeight < 480)
  62. continue;
  63. mDevModes.push_back(DevMode);
  64. StringUtil::StrStreamType str;
  65. str << DevMode.dmPelsWidth << " x " << DevMode.dmPelsHeight;
  66. optVideoMode.possibleValues.push_back(str.str());
  67. }
  68. remove_duplicates(optVideoMode.possibleValues);
  69. optVideoMode.currentValue = optVideoMode.possibleValues.front();
  70. optColourDepth.name = "Colour Depth";
  71. optColourDepth.immutable = false;
  72. optColourDepth.currentValue.clear();
  73. optDisplayFrequency.name = "Display Frequency";
  74. optDisplayFrequency.immutable = false;
  75. optDisplayFrequency.currentValue.clear();
  76. optVSync.name = "VSync";
  77. optVSync.immutable = false;
  78. optVSync.possibleValues.push_back("No");
  79. optVSync.possibleValues.push_back("Yes");
  80. optVSync.currentValue = "No";
  81. optVSyncInterval.name = "VSync Interval";
  82. optVSyncInterval.immutable = false;
  83. optVSyncInterval.possibleValues.push_back( "1" );
  84. optVSyncInterval.possibleValues.push_back( "2" );
  85. optVSyncInterval.possibleValues.push_back( "3" );
  86. optVSyncInterval.possibleValues.push_back( "4" );
  87. optVSyncInterval.currentValue = "1";
  88. optFSAA.name = "FSAA";
  89. optFSAA.immutable = false;
  90. optFSAA.possibleValues.push_back("0");
  91. for (vector<int>::type::iterator it = mFSAALevels.begin(); it != mFSAALevels.end(); ++it)
  92. {
  93. String val = toString(*it);
  94. optFSAA.possibleValues.push_back(val);
  95. /* not implementing CSAA in GL for now
  96. if (*it >= 8)
  97. optFSAA.possibleValues.push_back(val + " [Quality]");
  98. */
  99. }
  100. optFSAA.currentValue = "0";
  101. optRTTMode.name = "RTT Preferred Mode";
  102. optRTTMode.possibleValues.push_back("FBO");
  103. optRTTMode.possibleValues.push_back("PBuffer");
  104. optRTTMode.possibleValues.push_back("Copy");
  105. optRTTMode.currentValue = "FBO";
  106. optRTTMode.immutable = false;
  107. // SRGB on auto window
  108. optSRGB.name = "sRGB Gamma Conversion";
  109. optSRGB.possibleValues.push_back("Yes");
  110. optSRGB.possibleValues.push_back("No");
  111. optSRGB.currentValue = "No";
  112. optSRGB.immutable = false;
  113. mOptions[optFullScreen.name] = optFullScreen;
  114. mOptions[optVideoMode.name] = optVideoMode;
  115. mOptions[optColourDepth.name] = optColourDepth;
  116. mOptions[optDisplayFrequency.name] = optDisplayFrequency;
  117. mOptions[optVSync.name] = optVSync;
  118. mOptions[optVSyncInterval.name] = optVSyncInterval;
  119. mOptions[optFSAA.name] = optFSAA;
  120. mOptions[optRTTMode.name] = optRTTMode;
  121. mOptions[optSRGB.name] = optSRGB;
  122. refreshConfig();
  123. }
  124. void Win32GLSupport::refreshConfig()
  125. {
  126. ConfigOptionMap::iterator optVideoMode = mOptions.find("Video Mode");
  127. ConfigOptionMap::iterator moptColourDepth = mOptions.find("Colour Depth");
  128. ConfigOptionMap::iterator moptDisplayFrequency = mOptions.find("Display Frequency");
  129. if(optVideoMode == mOptions.end() || moptColourDepth == mOptions.end() || moptDisplayFrequency == mOptions.end())
  130. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find mOptions!", "Win32GLSupport::refreshConfig");
  131. ConfigOption* optColourDepth = &moptColourDepth->second;
  132. ConfigOption* optDisplayFrequency = &moptDisplayFrequency->second;
  133. const String& val = optVideoMode->second.currentValue;
  134. String::size_type pos = val.find('x');
  135. if (pos == String::npos)
  136. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Invalid Video Mode provided", "Win32GLSupport::refreshConfig");
  137. DWORD width = parseUnsignedInt(val.substr(0, pos));
  138. DWORD height = parseUnsignedInt(val.substr(pos+1, String::npos));
  139. for(vector<DEVMODE>::type::const_iterator i = mDevModes.begin(); i != mDevModes.end(); ++i)
  140. {
  141. if (i->dmPelsWidth != width || i->dmPelsHeight != height)
  142. continue;
  143. optColourDepth->possibleValues.push_back(toString((unsigned int)i->dmBitsPerPel));
  144. optDisplayFrequency->possibleValues.push_back(toString((unsigned int)i->dmDisplayFrequency));
  145. }
  146. remove_duplicates(optColourDepth->possibleValues);
  147. remove_duplicates(optDisplayFrequency->possibleValues);
  148. optColourDepth->currentValue = optColourDepth->possibleValues.back();
  149. bool freqValid = std::find(optDisplayFrequency->possibleValues.begin(),
  150. optDisplayFrequency->possibleValues.end(),
  151. optDisplayFrequency->currentValue) != optDisplayFrequency->possibleValues.end();
  152. if ( (optDisplayFrequency->currentValue != "N/A") && !freqValid )
  153. optDisplayFrequency->currentValue = optDisplayFrequency->possibleValues.front();
  154. }
  155. void Win32GLSupport::setConfigOption(const String &name, const String &value)
  156. {
  157. ConfigOptionMap::iterator it = mOptions.find(name);
  158. // Update
  159. if(it != mOptions.end())
  160. it->second.currentValue = value;
  161. else
  162. {
  163. StringUtil::StrStreamType str;
  164. str << "Option named '" << name << "' does not exist.";
  165. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, str.str(), "Win32GLSupport::setConfigOption" );
  166. }
  167. if( name == "Video Mode" )
  168. refreshConfig();
  169. if( name == "Full Screen" )
  170. {
  171. it = mOptions.find( "Display Frequency" );
  172. if( value == "No" )
  173. {
  174. it->second.currentValue = "N/A";
  175. it->second.immutable = true;
  176. }
  177. else
  178. {
  179. if (it->second.currentValue.empty() || it->second.currentValue == "N/A")
  180. it->second.currentValue = it->second.possibleValues.front();
  181. it->second.immutable = false;
  182. }
  183. }
  184. }
  185. String Win32GLSupport::validateConfig()
  186. {
  187. // TODO, DX9
  188. return StringUtil::BLANK;
  189. }
  190. RenderWindow* Win32GLSupport::createWindow(bool autoCreateWindow, GLRenderSystem* renderSystem, const String& windowTitle)
  191. {
  192. if (autoCreateWindow)
  193. {
  194. ConfigOptionMap::iterator opt = mOptions.find("Full Screen");
  195. if (opt == mOptions.end())
  196. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find full screen options!", "Win32GLSupport::createWindow");
  197. bool fullscreen = (opt->second.currentValue == "Yes");
  198. opt = mOptions.find("Video Mode");
  199. if (opt == mOptions.end())
  200. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find video mode options!", "Win32GLSupport::createWindow");
  201. String val = opt->second.currentValue;
  202. String::size_type pos = val.find('x');
  203. if (pos == String::npos)
  204. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Invalid Video Mode provided", "Win32GLSupport::createWindow");
  205. unsigned int w = parseUnsignedInt(val.substr(0, pos));
  206. unsigned int h = parseUnsignedInt(val.substr(pos + 1));
  207. // Parse optional parameters
  208. NameValuePairList winOptions;
  209. opt = mOptions.find("Colour Depth");
  210. if (opt == mOptions.end())
  211. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find Colour Depth options!", "Win32GLSupport::createWindow");
  212. unsigned int colourDepth =
  213. parseUnsignedInt(opt->second.currentValue);
  214. winOptions["colourDepth"] = toString(colourDepth);
  215. opt = mOptions.find("VSync");
  216. if (opt == mOptions.end())
  217. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find VSync options!", "Win32GLSupport::createWindow");
  218. bool vsync = (opt->second.currentValue == "Yes");
  219. winOptions["vsync"] = toString(vsync);
  220. renderSystem->setWaitForVerticalBlank(vsync);
  221. opt = mOptions.find("VSync Interval");
  222. if (opt == mOptions.end())
  223. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find VSync Interval options!", "Win32GLSupport::createWindow");
  224. winOptions["vsyncInterval"] = opt->second.currentValue;
  225. opt = mOptions.find("Display Frequency");
  226. if (opt != mOptions.end())
  227. {
  228. unsigned int displayFrequency =
  229. parseUnsignedInt(opt->second.currentValue);
  230. winOptions["displayFrequency"] = toString(displayFrequency);
  231. }
  232. opt = mOptions.find("FSAA");
  233. if (opt == mOptions.end())
  234. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find FSAA options!", "Win32GLSupport::createWindow");
  235. std::vector<CamelotEngine::String> aavalues = StringUtil::split(opt->second.currentValue, " ", 1);
  236. unsigned int multisample = parseUnsignedInt(aavalues[0]);
  237. String multisample_hint;
  238. if (aavalues.size() > 1)
  239. multisample_hint = aavalues[1];
  240. winOptions["FSAA"] = toString(multisample);
  241. winOptions["FSAAHint"] = multisample_hint;
  242. opt = mOptions.find("sRGB Gamma Conversion");
  243. if (opt == mOptions.end())
  244. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Can't find sRGB options!", "Win32GLSupport::createWindow");
  245. bool hwGamma = (opt->second.currentValue == "Yes");
  246. winOptions["gamma"] = toString(hwGamma);
  247. return renderSystem->_createRenderWindow(windowTitle, w, h, fullscreen, &winOptions);
  248. }
  249. else
  250. {
  251. // XXX What is the else?
  252. return NULL;
  253. }
  254. }
  255. BOOL CALLBACK Win32GLSupport::sCreateMonitorsInfoEnumProc(
  256. HMONITOR hMonitor, // handle to display monitor
  257. HDC hdcMonitor, // handle to monitor DC
  258. LPRECT lprcMonitor, // monitor intersection rectangle
  259. LPARAM dwData // data
  260. )
  261. {
  262. DisplayMonitorInfoList* pArrMonitorsInfo = (DisplayMonitorInfoList*)dwData;
  263. // Get monitor info
  264. DisplayMonitorInfo displayMonitorInfo;
  265. displayMonitorInfo.hMonitor = hMonitor;
  266. memset(&displayMonitorInfo.monitorInfoEx, 0, sizeof(MONITORINFOEX));
  267. displayMonitorInfo.monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  268. GetMonitorInfo(hMonitor, &displayMonitorInfo.monitorInfoEx);
  269. pArrMonitorsInfo->push_back(displayMonitorInfo);
  270. return TRUE;
  271. }
  272. RenderWindow* Win32GLSupport::newWindow(const String &name, unsigned int width,
  273. unsigned int height, bool fullScreen, const NameValuePairList *miscParams)
  274. {
  275. Win32Window* window = new Win32Window(*this);
  276. NameValuePairList newParams;
  277. if (miscParams != NULL)
  278. {
  279. newParams = *miscParams;
  280. miscParams = &newParams;
  281. NameValuePairList::const_iterator monitorIndexIt = miscParams->find("monitorIndex");
  282. HMONITOR hMonitor = NULL;
  283. int monitorIndex = -1;
  284. // If monitor index found, try to assign the monitor handle based on it.
  285. if (monitorIndexIt != miscParams->end())
  286. {
  287. if (mMonitorInfoList.empty())
  288. EnumDisplayMonitors(NULL, NULL, sCreateMonitorsInfoEnumProc, (LPARAM)&mMonitorInfoList);
  289. monitorIndex = parseInt(monitorIndexIt->second);
  290. if (monitorIndex < (int)mMonitorInfoList.size())
  291. {
  292. hMonitor = mMonitorInfoList[monitorIndex].hMonitor;
  293. }
  294. }
  295. // If we didn't specified the monitor index, or if it didn't find it
  296. if (hMonitor == NULL)
  297. {
  298. POINT windowAnchorPoint;
  299. NameValuePairList::const_iterator opt;
  300. int left = -1;
  301. int top = -1;
  302. if ((opt = newParams.find("left")) != newParams.end())
  303. left = parseInt(opt->second);
  304. if ((opt = newParams.find("top")) != newParams.end())
  305. top = parseInt(opt->second);
  306. // Fill in anchor point.
  307. windowAnchorPoint.x = left;
  308. windowAnchorPoint.y = top;
  309. // Get the nearest monitor to this window.
  310. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  311. }
  312. newParams["monitorHandle"] = toString((size_t)hMonitor);
  313. }
  314. window->create(name, width, height, fullScreen, miscParams);
  315. if(!mInitialWindow)
  316. mInitialWindow = window;
  317. return window;
  318. }
  319. void Win32GLSupport::start()
  320. {
  321. }
  322. void Win32GLSupport::stop()
  323. {
  324. mInitialWindow = 0; // Since there is no removeWindow, although there should be...
  325. }
  326. void Win32GLSupport::initialiseExtensions()
  327. {
  328. assert(mInitialWindow);
  329. // First, initialise the normal extensions
  330. GLSupport::initialiseExtensions();
  331. // wglew init
  332. #if CM_THREAD_SUPPORT != 1
  333. wglewContextInit(this);
  334. #endif
  335. // Check for W32 specific extensions probe function
  336. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
  337. (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
  338. if(!_wglGetExtensionsStringARB)
  339. return;
  340. const char *wgl_extensions = _wglGetExtensionsStringARB(mInitialWindow->getHDC());
  341. // Parse them, and add them to the main list
  342. StringStream ext;
  343. String instr;
  344. ext << wgl_extensions;
  345. while(ext >> instr)
  346. {
  347. extensionList.insert(instr);
  348. }
  349. }
  350. void* Win32GLSupport::getProcAddress(const String& procname)
  351. {
  352. return (void*)wglGetProcAddress( procname.c_str() );
  353. }
  354. void Win32GLSupport::initialiseWGL()
  355. {
  356. // wglGetProcAddress does not work without an active OpenGL context,
  357. // but we need wglChoosePixelFormatARB's address before we can
  358. // create our main window. Thank you very much, Microsoft!
  359. //
  360. // The solution is to create a dummy OpenGL window first, and then
  361. // test for WGL_ARB_pixel_format support. If it is not supported,
  362. // we make sure to never call the ARB pixel format functions.
  363. //
  364. // If is is supported, we call the pixel format functions at least once
  365. // to initialise them (pointers are stored by glprocs.h). We can also
  366. // take this opportunity to enumerate the valid FSAA modes.
  367. LPCSTR dummyText = "OgreWglDummy";
  368. #ifdef CM_STATIC_LIB
  369. HINSTANCE hinst = GetModuleHandle( NULL );
  370. #else
  371. # if CM_DEBUG_MODE == 1
  372. HINSTANCE hinst = GetModuleHandle("RenderSystem_GL_d.dll");
  373. # else
  374. HINSTANCE hinst = GetModuleHandle("RenderSystem_GL.dll");
  375. # endif
  376. #endif
  377. WNDCLASS dummyClass;
  378. memset(&dummyClass, 0, sizeof(WNDCLASS));
  379. dummyClass.style = CS_OWNDC;
  380. dummyClass.hInstance = hinst;
  381. dummyClass.lpfnWndProc = dummyWndProc;
  382. dummyClass.lpszClassName = dummyText;
  383. RegisterClass(&dummyClass);
  384. HWND hwnd = CreateWindow(dummyText, dummyText,
  385. WS_POPUP | WS_CLIPCHILDREN,
  386. 0, 0, 32, 32, 0, 0, hinst, 0);
  387. // if a simple CreateWindow fails, then boy are we in trouble...
  388. if (hwnd == NULL)
  389. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "CreateWindow() failed", "Win32GLSupport::initializeWGL");
  390. // no chance of failure and no need to release thanks to CS_OWNDC
  391. HDC hdc = GetDC(hwnd);
  392. // assign a simple OpenGL pixel format that everyone supports
  393. PIXELFORMATDESCRIPTOR pfd;
  394. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  395. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  396. pfd.nVersion = 1;
  397. pfd.cColorBits = 16;
  398. pfd.cDepthBits = 15;
  399. pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
  400. pfd.iPixelType = PFD_TYPE_RGBA;
  401. // if these fail, wglCreateContext will also quietly fail
  402. int format;
  403. if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
  404. SetPixelFormat(hdc, format, &pfd);
  405. HGLRC hrc = wglCreateContext(hdc);
  406. if (hrc)
  407. {
  408. HGLRC oldrc = wglGetCurrentContext();
  409. HDC oldhdc = wglGetCurrentDC();
  410. // if wglMakeCurrent fails, wglGetProcAddress will return null
  411. wglMakeCurrent(hdc, hrc);
  412. PFNWGLGETEXTENSIONSSTRINGARBPROC _wglGetExtensionsStringARB =
  413. (PFNWGLGETEXTENSIONSSTRINGARBPROC)
  414. wglGetProcAddress("wglGetExtensionsStringARB");
  415. // check for pixel format and multisampling support
  416. if (_wglGetExtensionsStringARB)
  417. {
  418. std::istringstream wglexts(_wglGetExtensionsStringARB(hdc));
  419. std::string ext;
  420. while (wglexts >> ext)
  421. {
  422. if (ext == "WGL_ARB_pixel_format")
  423. mHasPixelFormatARB = true;
  424. else if (ext == "WGL_ARB_multisample")
  425. mHasMultisample = true;
  426. else if (ext == "WGL_EXT_framebuffer_sRGB")
  427. mHasHardwareGamma = true;
  428. }
  429. }
  430. if (mHasPixelFormatARB && mHasMultisample)
  431. {
  432. // enumerate all formats w/ multisampling
  433. static const int iattr[] = {
  434. WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  435. WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  436. WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  437. WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  438. WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  439. /* We are no matter about the colour, depth and stencil buffers here
  440. WGL_COLOR_BITS_ARB, 24,
  441. WGL_ALPHA_BITS_ARB, 8,
  442. WGL_DEPTH_BITS_ARB, 24,
  443. WGL_STENCIL_BITS_ARB, 8,
  444. */
  445. WGL_SAMPLES_ARB, 2,
  446. 0
  447. };
  448. int formats[256];
  449. unsigned int count;
  450. // cheating here. wglChoosePixelFormatARB procc address needed later on
  451. // when a valid GL context does not exist and glew is not initialized yet.
  452. WGLEW_GET_FUN(__wglewChoosePixelFormatARB) =
  453. (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
  454. if (WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, iattr, 0, 256, formats, &count))
  455. {
  456. // determine what multisampling levels are offered
  457. int query = WGL_SAMPLES_ARB, samples;
  458. for (unsigned int i = 0; i < count; ++i)
  459. {
  460. PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB =
  461. (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)
  462. wglGetProcAddress("wglGetPixelFormatAttribivARB");
  463. if (_wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, &query, &samples))
  464. {
  465. mFSAALevels.push_back(samples);
  466. }
  467. }
  468. remove_duplicates(mFSAALevels);
  469. }
  470. }
  471. wglMakeCurrent(oldhdc, oldrc);
  472. wglDeleteContext(hrc);
  473. }
  474. // clean up our dummy window and class
  475. DestroyWindow(hwnd);
  476. UnregisterClass(dummyText, hinst);
  477. }
  478. LRESULT Win32GLSupport::dummyWndProc(HWND hwnd, UINT umsg, WPARAM wp, LPARAM lp)
  479. {
  480. return DefWindowProc(hwnd, umsg, wp, lp);
  481. }
  482. bool Win32GLSupport::selectPixelFormat(HDC hdc, int colourDepth, int multisample, bool hwGamma)
  483. {
  484. PIXELFORMATDESCRIPTOR pfd;
  485. memset(&pfd, 0, sizeof(pfd));
  486. pfd.nSize = sizeof(pfd);
  487. pfd.nVersion = 1;
  488. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  489. pfd.iPixelType = PFD_TYPE_RGBA;
  490. pfd.cColorBits = (colourDepth > 16)? 24 : colourDepth;
  491. pfd.cAlphaBits = (colourDepth > 16)? 8 : 0;
  492. pfd.cDepthBits = 24;
  493. pfd.cStencilBits = 8;
  494. int format = 0;
  495. int useHwGamma = hwGamma? GL_TRUE : GL_FALSE;
  496. if (multisample && (!mHasMultisample || !mHasPixelFormatARB))
  497. return false;
  498. if (hwGamma && !mHasHardwareGamma)
  499. return false;
  500. if ((multisample || hwGamma) && WGLEW_GET_FUN(__wglewChoosePixelFormatARB))
  501. {
  502. // Use WGL to test extended caps (multisample, sRGB)
  503. vector<int>::type attribList;
  504. attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE);
  505. attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE);
  506. attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE);
  507. attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE);
  508. attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB);
  509. attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(pfd.cColorBits);
  510. attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(pfd.cAlphaBits);
  511. attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(24);
  512. attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(8);
  513. attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(multisample);
  514. if (useHwGamma && checkExtension("WGL_EXT_framebuffer_sRGB"))
  515. {
  516. attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE);
  517. }
  518. // terminator
  519. attribList.push_back(0);
  520. UINT nformats;
  521. // ChoosePixelFormatARB proc address was obtained when setting up a dummy GL context in initialiseWGL()
  522. // since glew hasn't been initialized yet, we have to cheat and use the previously obtained address
  523. if (!WGLEW_GET_FUN(__wglewChoosePixelFormatARB)(hdc, &(attribList[0]), NULL, 1, &format, &nformats) || nformats <= 0)
  524. return false;
  525. }
  526. else
  527. {
  528. format = ChoosePixelFormat(hdc, &pfd);
  529. }
  530. return (format && SetPixelFormat(hdc, format, &pfd));
  531. }
  532. bool Win32GLSupport::supportsPBuffers()
  533. {
  534. return WGLEW_GET_FUN(__WGLEW_ARB_pbuffer) != GL_FALSE;
  535. }
  536. GLPBuffer *Win32GLSupport::createPBuffer(PixelComponentType format, size_t width, size_t height)
  537. {
  538. return new Win32PBuffer(format, width, height);
  539. }
  540. unsigned int Win32GLSupport::getDisplayMonitorCount() const
  541. {
  542. if (mMonitorInfoList.empty())
  543. EnumDisplayMonitors(NULL, NULL, sCreateMonitorsInfoEnumProc, (LPARAM)&mMonitorInfoList);
  544. return (unsigned int)mMonitorInfoList.size();
  545. }
  546. String translateWGLError()
  547. {
  548. int winError = GetLastError();
  549. char* errDesc;
  550. int i;
  551. errDesc = new char[255];
  552. // Try windows errors first
  553. i = FormatMessage(
  554. FORMAT_MESSAGE_FROM_SYSTEM |
  555. FORMAT_MESSAGE_IGNORE_INSERTS,
  556. NULL,
  557. winError,
  558. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  559. (LPTSTR) errDesc,
  560. 255,
  561. NULL
  562. );
  563. return String(errDesc);
  564. }
  565. }