OgreD3D9RenderWindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreD3D9RenderWindow.h"
  25. #include "CmViewport.h"
  26. #include "OgreException.h"
  27. #include "OgreD3D9RenderSystem.h"
  28. #include "CmRenderSystem.h"
  29. #include "OgreBitwise.h"
  30. #include "OgreStringConverter.h"
  31. #include "OgreWindowEventUtilities.h"
  32. #include "OgreD3D9DeviceManager.h"
  33. #include "CmRenderSystemManager.h"
  34. namespace CamelotEngine
  35. {
  36. D3D9RenderWindow::D3D9RenderWindow(HINSTANCE instance)
  37. : mInstance(instance)
  38. {
  39. mDevice = NULL;
  40. mIsFullScreen = false;
  41. mIsExternal = false;
  42. mHWnd = 0;
  43. mActive = false;
  44. mClosed = false;
  45. mSwitchingFullscreen = false;
  46. mDisplayFrequency = 0;
  47. mDeviceValid = false;
  48. mUseNVPerfHUD = false;
  49. }
  50. D3D9RenderWindow::~D3D9RenderWindow()
  51. {
  52. destroy();
  53. }
  54. void D3D9RenderWindow::create(const String& name, unsigned int width, unsigned int height,
  55. bool fullScreen, const NameValuePairList *miscParams)
  56. {
  57. HINSTANCE hInst = mInstance;
  58. HWND parentHWnd = 0;
  59. HWND externalHandle = 0;
  60. mFSAAType = D3DMULTISAMPLE_NONE;
  61. mFSAAQuality = 0;
  62. mFSAA = 0;
  63. mVSync = false;
  64. mVSyncInterval = 1;
  65. String title = name;
  66. unsigned int colourDepth = 32;
  67. int left = INT_MAX; // Defaults to screen center
  68. int top = INT_MAX; // Defaults to screen center
  69. bool depthBuffer = true;
  70. String border = "";
  71. bool outerSize = false;
  72. mUseNVPerfHUD = false;
  73. size_t fsaaSamples = 0;
  74. String fsaaHint;
  75. int monitorIndex = -1; //Default by detecting the adapter from left / top position
  76. if(miscParams)
  77. {
  78. // Get variable-length params
  79. NameValuePairList::const_iterator opt;
  80. // left (x)
  81. opt = miscParams->find("left");
  82. if(opt != miscParams->end())
  83. left = StringConverter::parseInt(opt->second);
  84. // top (y)
  85. opt = miscParams->find("top");
  86. if(opt != miscParams->end())
  87. top = StringConverter::parseInt(opt->second);
  88. // Window title
  89. opt = miscParams->find("title");
  90. if(opt != miscParams->end())
  91. title = opt->second;
  92. // parentWindowHandle -> parentHWnd
  93. opt = miscParams->find("parentWindowHandle");
  94. if(opt != miscParams->end())
  95. parentHWnd = (HWND)StringConverter::parseUnsignedInt(opt->second);
  96. // externalWindowHandle -> externalHandle
  97. opt = miscParams->find("externalWindowHandle");
  98. if(opt != miscParams->end())
  99. externalHandle = (HWND)StringConverter::parseUnsignedInt(opt->second);
  100. // vsync [parseBool]
  101. opt = miscParams->find("vsync");
  102. if(opt != miscParams->end())
  103. mVSync = StringConverter::parseBool(opt->second);
  104. // vsyncInterval [parseUnsignedInt]
  105. opt = miscParams->find("vsyncInterval");
  106. if(opt != miscParams->end())
  107. mVSyncInterval = StringConverter::parseUnsignedInt(opt->second);
  108. // displayFrequency
  109. opt = miscParams->find("displayFrequency");
  110. if(opt != miscParams->end())
  111. mDisplayFrequency = StringConverter::parseUnsignedInt(opt->second);
  112. // colourDepth
  113. opt = miscParams->find("colourDepth");
  114. if(opt != miscParams->end())
  115. colourDepth = StringConverter::parseUnsignedInt(opt->second);
  116. // depthBuffer [parseBool]
  117. opt = miscParams->find("depthBuffer");
  118. if(opt != miscParams->end())
  119. depthBuffer = StringConverter::parseBool(opt->second);
  120. // FSAA settings
  121. opt = miscParams->find("FSAA");
  122. if(opt != miscParams->end())
  123. {
  124. mFSAA = StringConverter::parseUnsignedInt(opt->second);
  125. }
  126. opt = miscParams->find("FSAAHint");
  127. if(opt != miscParams->end())
  128. {
  129. mFSAAHint = opt->second;
  130. }
  131. // window border style
  132. opt = miscParams->find("border");
  133. if(opt != miscParams->end())
  134. border = opt->second;
  135. // set outer dimensions?
  136. opt = miscParams->find("outerDimensions");
  137. if(opt != miscParams->end())
  138. outerSize = StringConverter::parseBool(opt->second);
  139. // NV perf HUD?
  140. opt = miscParams->find("useNVPerfHUD");
  141. if(opt != miscParams->end())
  142. mUseNVPerfHUD = StringConverter::parseBool(opt->second);
  143. // sRGB?
  144. opt = miscParams->find("gamma");
  145. if(opt != miscParams->end())
  146. mHwGamma = StringConverter::parseBool(opt->second);
  147. // monitor index
  148. opt = miscParams->find("monitorIndex");
  149. if(opt != miscParams->end())
  150. monitorIndex = StringConverter::parseInt(opt->second);
  151. }
  152. // Destroy current window if any
  153. if( mHWnd )
  154. destroy();
  155. if (!externalHandle)
  156. {
  157. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  158. DWORD dwStyleEx = 0;
  159. HMONITOR hMonitor = NULL;
  160. MONITORINFO monitorInfo;
  161. RECT rc;
  162. // If we specified which adapter we want to use - find it's monitor.
  163. if (monitorIndex != -1)
  164. {
  165. IDirect3D9* direct3D9 = D3D9RenderSystem::getDirect3D9();
  166. for (UINT32 i=0; i < direct3D9->GetAdapterCount(); ++i)
  167. {
  168. if (i == monitorIndex)
  169. {
  170. hMonitor = direct3D9->GetAdapterMonitor(i);
  171. break;
  172. }
  173. }
  174. }
  175. // If we didn't specified the adapter index, or if it didn't find it
  176. if (hMonitor == NULL)
  177. {
  178. POINT windowAnchorPoint;
  179. // Fill in anchor point.
  180. windowAnchorPoint.x = left;
  181. windowAnchorPoint.y = top;
  182. // Get the nearest monitor to this window.
  183. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  184. }
  185. // Get the target monitor info
  186. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  187. monitorInfo.cbSize = sizeof(MONITORINFO);
  188. GetMonitorInfo(hMonitor, &monitorInfo);
  189. unsigned int winWidth, winHeight;
  190. winWidth = width;
  191. winHeight = height;
  192. // No specified top left -> Center the window in the middle of the monitor
  193. if (left == INT_MAX || top == INT_MAX)
  194. {
  195. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  196. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  197. // clamp window dimensions to screen size
  198. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  199. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  200. if (left == INT_MAX)
  201. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  202. else if (monitorIndex != -1)
  203. left += monitorInfo.rcWork.left;
  204. if (top == INT_MAX)
  205. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  206. else if (monitorIndex != -1)
  207. top += monitorInfo.rcWork.top;
  208. }
  209. else if (monitorIndex != -1)
  210. {
  211. left += monitorInfo.rcWork.left;
  212. top += monitorInfo.rcWork.top;
  213. }
  214. mWidth = mDesiredWidth = width;
  215. mHeight = mDesiredHeight = height;
  216. mTop = top;
  217. mLeft = left;
  218. if (fullScreen)
  219. {
  220. dwStyleEx |= WS_EX_TOPMOST;
  221. dwStyle |= WS_POPUP;
  222. mTop = monitorInfo.rcMonitor.top;
  223. mLeft = monitorInfo.rcMonitor.left;
  224. }
  225. else
  226. {
  227. if (parentHWnd)
  228. {
  229. dwStyle |= WS_CHILD;
  230. }
  231. else
  232. {
  233. if (border == "none")
  234. dwStyle |= WS_POPUP;
  235. else if (border == "fixed")
  236. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  237. WS_SYSMENU | WS_MINIMIZEBOX;
  238. else
  239. dwStyle |= WS_OVERLAPPEDWINDOW;
  240. }
  241. adjustWindow(width, height, dwStyle, &winWidth, &winHeight);
  242. if (!outerSize)
  243. {
  244. // Calculate window dimensions required
  245. // to get the requested client area
  246. SetRect(&rc, 0, 0, mWidth, mHeight);
  247. AdjustWindowRect(&rc, dwStyle, false);
  248. mWidth = rc.right - rc.left;
  249. mHeight = rc.bottom - rc.top;
  250. // Clamp window rect to the nearest display monitor.
  251. if (mLeft < monitorInfo.rcWork.left)
  252. mLeft = monitorInfo.rcWork.left;
  253. if (mTop < monitorInfo.rcWork.top)
  254. mTop = monitorInfo.rcWork.top;
  255. if (static_cast<int>(winWidth) > monitorInfo.rcWork.right - mLeft)
  256. winWidth = monitorInfo.rcWork.right - mLeft;
  257. if (static_cast<int>(winHeight) > monitorInfo.rcWork.bottom - mTop)
  258. winHeight = monitorInfo.rcWork.bottom - mTop;
  259. }
  260. }
  261. // Register the window class
  262. // NB allow 4 bytes of window data for D3D9RenderWindow pointer
  263. WNDCLASS wc = { 0, WindowEventUtilities::_WndProc, 0, 0, hInst,
  264. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  265. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "OgreD3D9Wnd" };
  266. RegisterClass(&wc);
  267. // Create our main window
  268. // Pass pointer to self
  269. mIsExternal = false;
  270. mHWnd = CreateWindowEx(dwStyleEx, "OgreD3D9Wnd", title.c_str(), dwStyle,
  271. mLeft, mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  272. mStyle = dwStyle;
  273. WindowEventUtilities::_addRenderWindow(this);
  274. }
  275. else
  276. {
  277. mHWnd = externalHandle;
  278. mIsExternal = true;
  279. }
  280. RECT rc;
  281. // top and left represent outer window coordinates
  282. GetWindowRect(mHWnd, &rc);
  283. mTop = rc.top;
  284. mLeft = rc.left;
  285. // width and height represent interior drawable area
  286. GetClientRect(mHWnd, &rc);
  287. mWidth = rc.right;
  288. mHeight = rc.bottom;
  289. mName = name;
  290. mIsDepthBuffered = depthBuffer;
  291. mIsFullScreen = fullScreen;
  292. mColourDepth = colourDepth;
  293. mActive = true;
  294. mClosed = false;
  295. }
  296. void D3D9RenderWindow::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  297. {
  298. if (fullScreen != mIsFullScreen || width != mWidth || height != mHeight)
  299. {
  300. if (fullScreen != mIsFullScreen)
  301. mSwitchingFullscreen = true;
  302. mStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  303. bool oldFullscreen = mIsFullScreen;
  304. mIsFullScreen = fullScreen;
  305. mWidth = mDesiredWidth = width;
  306. mHeight = mDesiredHeight = height;
  307. if (fullScreen)
  308. {
  309. mStyle |= WS_POPUP;
  310. // Get the nearest monitor to this window.
  311. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  312. // Get monitor info
  313. MONITORINFO monitorInfo;
  314. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  315. monitorInfo.cbSize = sizeof(MONITORINFO);
  316. GetMonitorInfo(hMonitor, &monitorInfo);
  317. mTop = monitorInfo.rcMonitor.top;
  318. mLeft = monitorInfo.rcMonitor.left;
  319. // need different ordering here
  320. if (oldFullscreen)
  321. {
  322. // was previously fullscreen, just changing the resolution
  323. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height, SWP_NOACTIVATE);
  324. }
  325. else
  326. {
  327. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height, SWP_NOACTIVATE);
  328. //MoveWindow(mHWnd, mLeft, mTop, mWidth, mHeight, FALSE);
  329. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  330. SetWindowPos(mHWnd, 0, 0,0, 0,0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  331. }
  332. }
  333. else
  334. {
  335. mStyle |= WS_OVERLAPPEDWINDOW;
  336. // Calculate window dimensions required
  337. // to get the requested client area
  338. unsigned int winWidth, winHeight;
  339. adjustWindow(mWidth, mHeight, mStyle, &winWidth, &winHeight);
  340. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  341. SetWindowPos(mHWnd, HWND_NOTOPMOST, 0, 0, winWidth, winHeight,
  342. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE);
  343. // Note that we also set the position in the restoreLostDevice method
  344. // via _finishSwitchingFullScreen
  345. }
  346. // Have to release & trigger device reset
  347. // NB don't use windowMovedOrResized since Win32 doesn't know
  348. // about the size change yet
  349. mDevice->invalidate(this);
  350. // Notify viewports of resize
  351. ViewportList::iterator it = mViewportList.begin();
  352. while(it != mViewportList.end()) (*it++).second->_updateDimensions();
  353. }
  354. }
  355. void D3D9RenderWindow::adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  356. DWORD style, unsigned int* winWidth, unsigned int* winHeight)
  357. {
  358. // NB only call this for non full screen
  359. RECT rc;
  360. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  361. AdjustWindowRect(&rc, style, false);
  362. *winWidth = rc.right - rc.left;
  363. *winHeight = rc.bottom - rc.top;
  364. // adjust to monitor
  365. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  366. // Get monitor info
  367. MONITORINFO monitorInfo;
  368. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  369. monitorInfo.cbSize = sizeof(MONITORINFO);
  370. GetMonitorInfo(hMonitor, &monitorInfo);
  371. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  372. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  373. if (*winWidth > (unsigned int)maxW)
  374. *winWidth = maxW;
  375. if (*winHeight > (unsigned int)maxH)
  376. *winHeight = maxH;
  377. }
  378. void D3D9RenderWindow::_finishSwitchingFullscreen()
  379. {
  380. if(mIsFullScreen)
  381. {
  382. // Need to reset the region on the window sometimes, when the
  383. // windowed mode was constrained by desktop
  384. HRGN hRgn = CreateRectRgn(0,0,mWidth, mHeight);
  385. SetWindowRgn(mHWnd, hRgn, FALSE);
  386. }
  387. else
  388. {
  389. // When switching back to windowed mode, need to reset window size
  390. // after device has been restored
  391. // We may have had a resize event which polluted our desired sizes
  392. unsigned int winWidth, winHeight;
  393. adjustWindow(mDesiredWidth, mDesiredHeight, mStyle, &winWidth, &winHeight);
  394. // deal with centreing when switching down to smaller resolution
  395. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  396. MONITORINFO monitorInfo;
  397. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  398. monitorInfo.cbSize = sizeof(MONITORINFO);
  399. GetMonitorInfo(hMonitor, &monitorInfo);
  400. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  401. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  402. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  403. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  404. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  405. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  406. if (mWidth != mDesiredWidth ||
  407. mHeight != mDesiredHeight)
  408. {
  409. mWidth = mDesiredWidth;
  410. mHeight = mDesiredHeight;
  411. // Notify viewports of resize
  412. ViewportList::iterator it = mViewportList.begin();
  413. while( it != mViewportList.end() )
  414. (*it++).second->_updateDimensions();
  415. }
  416. }
  417. mSwitchingFullscreen = false;
  418. }
  419. void D3D9RenderWindow::buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams)
  420. {
  421. // Set up the presentation parameters
  422. IDirect3D9* pD3D = D3D9RenderSystem::getDirect3D9();
  423. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  424. if (mDevice != NULL)
  425. devType = mDevice->getDeviceType();
  426. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  427. presentParams->Windowed = !mIsFullScreen;
  428. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  429. // triple buffer if VSync is on
  430. presentParams->BackBufferCount = mVSync ? 2 : 1;
  431. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  432. presentParams->hDeviceWindow = mHWnd;
  433. presentParams->BackBufferWidth = mWidth;
  434. presentParams->BackBufferHeight = mHeight;
  435. presentParams->FullScreen_RefreshRateInHz = mIsFullScreen ? mDisplayFrequency : 0;
  436. if (presentParams->BackBufferWidth == 0)
  437. presentParams->BackBufferWidth = 1;
  438. if (presentParams->BackBufferHeight == 0)
  439. presentParams->BackBufferHeight = 1;
  440. if (mVSync)
  441. {
  442. // D3D9 only seems to support 2-4 presentation intervals in fullscreen
  443. if (mIsFullScreen)
  444. {
  445. switch(mVSyncInterval)
  446. {
  447. case 1:
  448. default:
  449. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  450. break;
  451. case 2:
  452. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  453. break;
  454. case 3:
  455. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  456. break;
  457. case 4:
  458. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  459. break;
  460. };
  461. // check that the interval was supported, revert to 1 to be safe otherwise
  462. D3DCAPS9 caps;
  463. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  464. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  465. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  466. }
  467. else
  468. {
  469. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  470. }
  471. }
  472. else
  473. {
  474. // NB not using vsync in windowed mode in D3D9 can cause jerking at low
  475. // frame rates no matter what buffering modes are used (odd - perhaps a
  476. // timer issue in D3D9 since GL doesn't suffer from this)
  477. // low is < 200fps in this context
  478. if (!mIsFullScreen)
  479. {
  480. // TODO LOG PORT - Enable this warning later?
  481. //LogManager::getSingleton().logMessage("D3D9 : WARNING - "
  482. // "disabling VSync in windowed mode can cause timing issues at lower "
  483. // "frame rates, turn VSync on if you observe this problem.");
  484. }
  485. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  486. }
  487. presentParams->BackBufferFormat = D3DFMT_R5G6B5;
  488. if( mColourDepth > 16 )
  489. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  490. if (mColourDepth > 16 )
  491. {
  492. // Try to create a 32-bit depth, 8-bit stencil
  493. if( FAILED( pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  494. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  495. D3DRTYPE_SURFACE, D3DFMT_D24S8 )))
  496. {
  497. // Bugger, no 8-bit hardware stencil, just try 32-bit zbuffer
  498. if( FAILED( pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  499. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  500. D3DRTYPE_SURFACE, D3DFMT_D32 )))
  501. {
  502. // Jeez, what a naff card. Fall back on 16-bit depth buffering
  503. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  504. }
  505. else
  506. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  507. }
  508. else
  509. {
  510. // Woohoo!
  511. if( SUCCEEDED( pD3D->CheckDepthStencilMatch( mDevice->getAdapterNumber(), devType,
  512. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8 ) ) )
  513. {
  514. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  515. }
  516. else
  517. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  518. }
  519. }
  520. else
  521. // 16-bit depth, software stencil
  522. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  523. D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  524. rsys->determineFSAASettings(mDevice->getD3D9Device(),
  525. mFSAA, mFSAAHint, presentParams->BackBufferFormat, mIsFullScreen,
  526. &mFSAAType, &mFSAAQuality);
  527. presentParams->MultiSampleType = mFSAAType;
  528. presentParams->MultiSampleQuality = (mFSAAQuality == 0) ? 0 : mFSAAQuality;
  529. // Check sRGB
  530. if (mHwGamma)
  531. {
  532. /* hmm, this never succeeds even when device does support??
  533. if(FAILED(pD3D->CheckDeviceFormat(mDriver->getAdapterNumber(),
  534. devType, presentParams->BackBufferFormat, D3DUSAGE_QUERY_SRGBWRITE,
  535. D3DRTYPE_SURFACE, presentParams->BackBufferFormat )))
  536. {
  537. // disable - not supported
  538. mHwGamma = false;
  539. }
  540. */
  541. }
  542. }
  543. void D3D9RenderWindow::destroy()
  544. {
  545. if (mDevice != NULL)
  546. {
  547. mDevice->detachRenderWindow(this);
  548. mDevice = NULL;
  549. }
  550. if (mHWnd && !mIsExternal)
  551. {
  552. WindowEventUtilities::_removeRenderWindow(this);
  553. DestroyWindow(mHWnd);
  554. }
  555. mHWnd = 0;
  556. mActive = false;
  557. mClosed = true;
  558. }
  559. bool D3D9RenderWindow::isActive() const
  560. {
  561. if (isFullScreen())
  562. return isVisible();
  563. return mActive && isVisible();
  564. }
  565. bool D3D9RenderWindow::isVisible() const
  566. {
  567. return (mHWnd && !IsIconic(mHWnd));
  568. }
  569. void D3D9RenderWindow::reposition(int top, int left)
  570. {
  571. if (mHWnd && !mIsFullScreen)
  572. {
  573. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  574. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  575. }
  576. }
  577. void D3D9RenderWindow::resize(unsigned int width, unsigned int height)
  578. {
  579. if (mHWnd && !mIsFullScreen)
  580. {
  581. unsigned int winWidth, winHeight;
  582. adjustWindow(width, height, mStyle, &winWidth, &winHeight);
  583. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  584. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  585. }
  586. }
  587. void D3D9RenderWindow::windowMovedOrResized()
  588. {
  589. if (!mHWnd || IsIconic(mHWnd))
  590. return;
  591. updateWindowRect();
  592. }
  593. void D3D9RenderWindow::swapBuffers( bool waitForVSync )
  594. {
  595. if (mDeviceValid)
  596. mDevice->present(this);
  597. }
  598. void D3D9RenderWindow::getCustomAttribute( const String& name, void* pData )
  599. {
  600. // Valid attributes and their equvalent native functions:
  601. // D3DDEVICE : getD3DDevice
  602. // WINDOW : getWindowHandle
  603. if( name == "D3DDEVICE" )
  604. {
  605. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  606. *pDev = getD3D9Device();
  607. return;
  608. }
  609. else if( name == "WINDOW" )
  610. {
  611. HWND *pHwnd = (HWND*)pData;
  612. *pHwnd = getWindowHandle();
  613. return;
  614. }
  615. else if( name == "isTexture" )
  616. {
  617. bool *b = reinterpret_cast< bool * >( pData );
  618. *b = false;
  619. return;
  620. }
  621. else if( name == "D3DZBUFFER" )
  622. {
  623. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  624. *pSurf = mDevice->getDepthBuffer(this);
  625. return;
  626. }
  627. else if( name == "DDBACKBUFFER" )
  628. {
  629. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  630. *pSurf = mDevice->getBackBuffer(this);
  631. return;
  632. }
  633. else if( name == "DDFRONTBUFFER" )
  634. {
  635. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  636. *pSurf = mDevice->getBackBuffer(this);
  637. return;
  638. }
  639. }
  640. void D3D9RenderWindow::copyContentsToMemory(const PixelBox &dst, FrameBuffer buffer)
  641. {
  642. mDevice->copyContentsToMemory(this, dst, buffer);
  643. }
  644. //-----------------------------------------------------------------------------
  645. void D3D9RenderWindow::_beginUpdate()
  646. {
  647. // External windows should update per frame
  648. // since it dosen't get the window resize/move messages.
  649. if (mIsExternal)
  650. {
  651. updateWindowRect();
  652. }
  653. if (mWidth == 0 || mHeight == 0)
  654. {
  655. mDeviceValid = false;
  656. return;
  657. }
  658. D3D9RenderSystem::getDeviceManager()->setActiveRenderTargetDevice(mDevice);
  659. // Check that device can be used for rendering operations.
  660. mDeviceValid = mDevice->validate(this);
  661. if (mDeviceValid)
  662. {
  663. // Finish window / fullscreen mode switch.
  664. if (_getSwitchingFullscreen())
  665. {
  666. _finishSwitchingFullscreen();
  667. // have to re-validate since this may have altered dimensions
  668. mDeviceValid = mDevice->validate(this);
  669. }
  670. }
  671. RenderWindow::_beginUpdate();
  672. }
  673. //---------------------------------------------------------------------
  674. void D3D9RenderWindow::_updateViewport(Viewport* viewport, bool updateStatistics)
  675. {
  676. if (mDeviceValid)
  677. {
  678. RenderWindow::_updateViewport(viewport, updateStatistics);
  679. }
  680. }
  681. //---------------------------------------------------------------------
  682. void D3D9RenderWindow::_endUpdate()
  683. {
  684. RenderWindow::_endUpdate();
  685. D3D9RenderSystem::getDeviceManager()->setActiveRenderTargetDevice(NULL);
  686. }
  687. //-----------------------------------------------------------------------------
  688. IDirect3DDevice9* D3D9RenderWindow::getD3D9Device()
  689. {
  690. return mDevice->getD3D9Device();
  691. }
  692. //-----------------------------------------------------------------------------
  693. IDirect3DSurface9* D3D9RenderWindow::getRenderSurface()
  694. {
  695. return mDevice->getBackBuffer(this);
  696. }
  697. //-----------------------------------------------------------------------------
  698. bool D3D9RenderWindow::_getSwitchingFullscreen() const
  699. {
  700. return mSwitchingFullscreen;
  701. }
  702. //-----------------------------------------------------------------------------
  703. D3D9Device* D3D9RenderWindow::getDevice()
  704. {
  705. return mDevice;
  706. }
  707. //-----------------------------------------------------------------------------
  708. void D3D9RenderWindow::setDevice(D3D9Device* device)
  709. {
  710. mDevice = device;
  711. mDeviceValid = false;
  712. }
  713. //-----------------------------------------------------------------------------
  714. bool D3D9RenderWindow::isDepthBuffered() const
  715. {
  716. return mIsDepthBuffered;
  717. }
  718. //-----------------------------------------------------------------------------
  719. void D3D9RenderWindow::updateWindowRect()
  720. {
  721. RECT rc;
  722. BOOL result;
  723. // Update top left parameters
  724. result = GetWindowRect(mHWnd, &rc);
  725. if (result == FALSE)
  726. {
  727. mTop = 0;
  728. mLeft = 0;
  729. mWidth = 0;
  730. mHeight = 0;
  731. return;
  732. }
  733. mTop = rc.top;
  734. mLeft = rc.left;
  735. // width and height represent drawable area only
  736. result = GetClientRect(mHWnd, &rc);
  737. if (result == FALSE)
  738. {
  739. mTop = 0;
  740. mLeft = 0;
  741. mWidth = 0;
  742. mHeight = 0;
  743. return;
  744. }
  745. unsigned int width = rc.right - rc.left;
  746. unsigned int height = rc.bottom - rc.top;
  747. // Case window resized.
  748. if (width != mWidth || height != mHeight)
  749. {
  750. mWidth = rc.right - rc.left;
  751. mHeight = rc.bottom - rc.top;
  752. // Notify viewports of resize
  753. ViewportList::iterator it = mViewportList.begin();
  754. while( it != mViewportList.end() )
  755. (*it++).second->_updateDimensions();
  756. }
  757. }
  758. //-----------------------------------------------------------------------------
  759. bool D3D9RenderWindow::isNvPerfHUDEnable() const
  760. {
  761. return mUseNVPerfHUD;
  762. }
  763. //---------------------------------------------------------------------
  764. bool D3D9RenderWindow::_validateDevice()
  765. {
  766. mDeviceValid = mDevice->validate(this);
  767. return mDeviceValid;
  768. }
  769. }