2
0

BsD3D9RenderWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. #include "BsD3D9RenderWindow.h"
  2. #include "BsInput.h"
  3. #include "BsCoreThread.h"
  4. #include "BsViewport.h"
  5. #include "BsException.h"
  6. #include "BsD3D9RenderAPI.h"
  7. #include "BsRenderAPI.h"
  8. #include "BsBitwise.h"
  9. #include "Win32/BsWin32Platform.h"
  10. #include "BsD3D9VideoModeInfo.h"
  11. #include "BsD3D9DeviceManager.h"
  12. #include "BsRenderWindowManager.h"
  13. namespace BansheeEngine
  14. {
  15. D3D9RenderWindowProperties::D3D9RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  16. :RenderWindowProperties(desc)
  17. {
  18. }
  19. D3D9RenderWindowCore::D3D9RenderWindowCore(const RENDER_WINDOW_DESC& desc, UINT32 windowId, HINSTANCE instance)
  20. : RenderWindowCore(desc, windowId), mInstance(instance), mProperties(desc), mSyncedProperties(desc), mIsDepthBuffered(true), mIsChild(false), mHWnd(0),
  21. mStyle(0), mWindowedStyle(0), mWindowedStyleEx(0), mDevice(nullptr), mIsExternal(false), mDisplayFrequency(0), mDeviceValid(false)
  22. { }
  23. D3D9RenderWindowCore::~D3D9RenderWindowCore()
  24. {
  25. if (mDevice != nullptr)
  26. {
  27. mDevice->detachRenderWindow(this);
  28. mDevice = nullptr;
  29. }
  30. if (mHWnd && !mIsExternal)
  31. {
  32. DestroyWindow(mHWnd);
  33. }
  34. mHWnd = 0;
  35. mProperties.mActive = false;
  36. }
  37. void D3D9RenderWindowCore::initialize()
  38. {
  39. RenderWindowCore::initialize();
  40. HINSTANCE hInst = mInstance;
  41. mMultisampleType = D3DMULTISAMPLE_NONE;
  42. mMultisampleQuality = 0;
  43. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  44. HWND parentHWnd = 0;
  45. HWND externalHandle = 0;
  46. NameValuePairList::const_iterator opt;
  47. opt = mDesc.platformSpecific.find("parentWindowHandle");
  48. if (opt != mDesc.platformSpecific.end())
  49. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  50. opt = mDesc.platformSpecific.find("externalWindowHandle");
  51. if (opt != mDesc.platformSpecific.end())
  52. externalHandle = (HWND)parseUnsignedInt(opt->second);
  53. mIsChild = parentHWnd != 0;
  54. mWindowedStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  55. mWindowedStyleEx = 0;
  56. if (!mDesc.fullscreen || mIsChild)
  57. {
  58. if (parentHWnd)
  59. {
  60. if (mDesc.toolWindow)
  61. mWindowedStyleEx = WS_EX_TOOLWINDOW;
  62. else
  63. mWindowedStyle |= WS_CHILD;
  64. }
  65. if (!parentHWnd || mDesc.toolWindow)
  66. {
  67. if (mDesc.border == WindowBorder::None)
  68. mWindowedStyle |= WS_POPUP;
  69. else if (mDesc.border == WindowBorder::Fixed)
  70. mWindowedStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  71. WS_SYSMENU | WS_MINIMIZEBOX;
  72. else
  73. mWindowedStyle |= WS_OVERLAPPEDWINDOW;
  74. }
  75. }
  76. D3D9RenderWindowProperties& props = mProperties;
  77. if (!externalHandle)
  78. {
  79. MONITORINFO monitorInfo;
  80. RECT rc;
  81. HMONITOR hMonitor = NULL;
  82. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  83. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  84. if (numOutputs > 0)
  85. {
  86. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  87. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  88. hMonitor = outputInfo.getMonitorHandle();
  89. }
  90. // If we didn't specified the adapter index, or if it didn't find it
  91. if (hMonitor == NULL)
  92. {
  93. POINT windowAnchorPoint;
  94. // Fill in anchor point.
  95. windowAnchorPoint.x = mDesc.left;
  96. windowAnchorPoint.y = mDesc.top;
  97. // Get the nearest monitor to this window.
  98. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  99. }
  100. // Get the target monitor info
  101. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  102. monitorInfo.cbSize = sizeof(MONITORINFO);
  103. GetMonitorInfo(hMonitor, &monitorInfo);
  104. unsigned int winWidth, winHeight;
  105. winWidth = mDesc.videoMode.getWidth();
  106. winHeight = mDesc.videoMode.getHeight();
  107. UINT32 left = mDesc.left;
  108. UINT32 top = mDesc.top;
  109. // No specified top left -> Center the window in the middle of the monitor
  110. if (left == -1 || top == -1)
  111. {
  112. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  113. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  114. // clamp window dimensions to screen size
  115. int outerw = (int(winWidth) < screenw) ? int(winWidth) : screenw;
  116. int outerh = (int(winHeight) < screenh) ? int(winHeight) : screenh;
  117. if (left == -1)
  118. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  119. else if (hMonitor != NULL)
  120. left += monitorInfo.rcWork.left;
  121. if (top == -1)
  122. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  123. else if (hMonitor != NULL)
  124. top += monitorInfo.rcWork.top;
  125. }
  126. else if (hMonitor != NULL)
  127. {
  128. left += monitorInfo.rcWork.left;
  129. top += monitorInfo.rcWork.top;
  130. }
  131. props.mWidth = mDesc.videoMode.getWidth();
  132. props.mHeight = mDesc.videoMode.getHeight();
  133. props.mTop = top;
  134. props.mLeft = left;
  135. DWORD dwStyle = 0;
  136. DWORD dwStyleEx = 0;
  137. if (mDesc.fullscreen && !mIsChild)
  138. {
  139. dwStyle = WS_VISIBLE | WS_CLIPCHILDREN | WS_POPUP;
  140. props.mTop = monitorInfo.rcMonitor.top;
  141. props.mLeft = monitorInfo.rcMonitor.left;
  142. }
  143. else
  144. {
  145. dwStyle = mWindowedStyle;
  146. dwStyleEx = mWindowedStyleEx;
  147. getAdjustedWindowSize(mDesc.videoMode.getWidth(), mDesc.videoMode.getHeight(), dwStyle, &winWidth, &winHeight);
  148. if (!mDesc.outerDimensions)
  149. {
  150. // Calculate window dimensions required
  151. // to get the requested client area
  152. SetRect(&rc, 0, 0, props.mWidth, props.mHeight);
  153. AdjustWindowRect(&rc, dwStyle, false);
  154. props.mWidth = rc.right - rc.left;
  155. props.mHeight = rc.bottom - rc.top;
  156. // Clamp window rect to the nearest display monitor.
  157. if (props.mLeft < monitorInfo.rcWork.left)
  158. props.mLeft = monitorInfo.rcWork.left;
  159. if (props.mTop < monitorInfo.rcWork.top)
  160. props.mTop = monitorInfo.rcWork.top;
  161. if (static_cast<int>(winWidth) > monitorInfo.rcWork.right - props.mLeft)
  162. winWidth = monitorInfo.rcWork.right - props.mLeft;
  163. if (static_cast<int>(winHeight) > monitorInfo.rcWork.bottom - props.mTop)
  164. winHeight = monitorInfo.rcWork.bottom - props.mTop;
  165. }
  166. }
  167. // Register the window class
  168. WNDCLASS wc = { 0, Win32Platform::_win32WndProc, 0, 0, hInst,
  169. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  170. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D9Wnd" };
  171. RegisterClass(&wc);
  172. // Create our main window
  173. // Pass pointer to self
  174. mIsExternal = false;
  175. mHWnd = CreateWindowEx(dwStyleEx, "D3D9Wnd", mDesc.title.c_str(), dwStyle,
  176. props.mLeft, props.mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  177. mStyle = dwStyle;
  178. }
  179. else
  180. {
  181. mHWnd = externalHandle;
  182. mIsExternal = true;
  183. }
  184. RECT rc;
  185. GetWindowRect(mHWnd, &rc);
  186. props.mTop = rc.top;
  187. props.mLeft = rc.left;
  188. GetClientRect(mHWnd, &rc);
  189. props.mWidth = rc.right;
  190. props.mHeight = rc.bottom;
  191. mIsDepthBuffered = mDesc.depthBuffer;
  192. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  193. props.mColorDepth = 32;
  194. props.mActive = true;
  195. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(RenderAPICore::instancePtr());
  196. rs->registerWindow(*this);
  197. {
  198. ScopedSpinLock lock(mLock);
  199. mSyncedProperties = props;
  200. }
  201. RenderWindowManager::instance().notifySyncDataDirty(this);
  202. }
  203. void D3D9RenderWindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  204. {
  205. THROW_IF_NOT_CORE_THREAD;
  206. if (mIsChild)
  207. return;
  208. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  209. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  210. if (numOutputs == 0)
  211. return;
  212. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  213. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  214. D3D9RenderWindowProperties& props = mProperties;
  215. bool oldFullscreen = props.mIsFullScreen;
  216. props.mWidth = width;
  217. props.mHeight = height;
  218. mDisplayFrequency = Math::roundToInt(refreshRate);
  219. props.mIsFullScreen = true;
  220. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  221. MONITORINFO monitorInfo;
  222. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  223. monitorInfo.cbSize = sizeof(MONITORINFO);
  224. GetMonitorInfo(hMonitor, &monitorInfo);
  225. props.mTop = monitorInfo.rcMonitor.top;
  226. props.mLeft = monitorInfo.rcMonitor.left;
  227. // Invalidate device, which resets it
  228. mDevice->invalidate(this);
  229. mDevice->acquire();
  230. {
  231. ScopedSpinLock lock(mLock);
  232. mSyncedProperties.mTop = props.mTop;
  233. mSyncedProperties.mLeft = props.mLeft;
  234. mSyncedProperties.mWidth = props.mWidth;
  235. mSyncedProperties.mHeight = props.mHeight;
  236. }
  237. RenderWindowManager::instance().notifySyncDataDirty(this);
  238. RenderWindowManager::instance().notifyMovedOrResized(this);
  239. }
  240. void D3D9RenderWindowCore::setFullscreen(const VideoMode& mode)
  241. {
  242. THROW_IF_NOT_CORE_THREAD;
  243. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  244. }
  245. void D3D9RenderWindowCore::setWindowed(UINT32 width, UINT32 height)
  246. {
  247. THROW_IF_NOT_CORE_THREAD;
  248. D3D9RenderWindowProperties& props = mProperties;
  249. if (!props.mIsFullScreen)
  250. return;
  251. props.mIsFullScreen = false;
  252. mStyle = mWindowedStyle;
  253. props.mWidth = width;
  254. props.mHeight = height;
  255. unsigned int winWidth, winHeight;
  256. getAdjustedWindowSize(props.mWidth, props.mHeight, mStyle, &winWidth, &winHeight);
  257. // Deal with centering when switching down to smaller resolution
  258. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  259. MONITORINFO monitorInfo;
  260. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  261. monitorInfo.cbSize = sizeof(MONITORINFO);
  262. GetMonitorInfo(hMonitor, &monitorInfo);
  263. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  264. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  265. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  266. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  267. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  268. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  269. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  270. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  271. mDevice->invalidate(this);
  272. mDevice->acquire();
  273. {
  274. ScopedSpinLock lock(mLock);
  275. mSyncedProperties.mTop = props.mTop;
  276. mSyncedProperties.mLeft = props.mLeft;
  277. mSyncedProperties.mWidth = props.mWidth;
  278. mSyncedProperties.mHeight = props.mHeight;
  279. }
  280. RenderWindowManager::instance().notifySyncDataDirty(this);
  281. RenderWindowManager::instance().notifyMovedOrResized(this);
  282. }
  283. void D3D9RenderWindowCore::setHidden(bool hidden)
  284. {
  285. THROW_IF_NOT_CORE_THREAD;
  286. D3D9RenderWindowProperties& props = mProperties;
  287. props.mHidden = hidden;
  288. if (!mIsExternal)
  289. {
  290. if (hidden)
  291. ShowWindow(mHWnd, SW_HIDE);
  292. else
  293. ShowWindow(mHWnd, SW_SHOWNORMAL);
  294. }
  295. RenderWindowCore::setHidden(hidden);
  296. }
  297. void D3D9RenderWindowCore::minimize()
  298. {
  299. THROW_IF_NOT_CORE_THREAD;
  300. if (mHWnd)
  301. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  302. }
  303. void D3D9RenderWindowCore::maximize()
  304. {
  305. THROW_IF_NOT_CORE_THREAD;
  306. if (mHWnd)
  307. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  308. }
  309. void D3D9RenderWindowCore::restore()
  310. {
  311. THROW_IF_NOT_CORE_THREAD;
  312. if (mHWnd)
  313. SendMessage(mHWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  314. }
  315. void D3D9RenderWindowCore::move(INT32 top, INT32 left)
  316. {
  317. THROW_IF_NOT_CORE_THREAD;
  318. D3D9RenderWindowProperties& props = mProperties;
  319. if (mHWnd && !props.mIsFullScreen)
  320. {
  321. props.mLeft = left;
  322. props.mTop = top;
  323. SetWindowPos(mHWnd, 0, top, left, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  324. {
  325. ScopedSpinLock lock(mLock);
  326. mSyncedProperties.mLeft = props.mLeft;
  327. mSyncedProperties.mTop = props.mTop;
  328. }
  329. RenderWindowManager::instance().notifySyncDataDirty(this);
  330. }
  331. }
  332. void D3D9RenderWindowCore::resize(UINT32 width, UINT32 height)
  333. {
  334. THROW_IF_NOT_CORE_THREAD;
  335. D3D9RenderWindowProperties& props = mProperties;
  336. if (mHWnd && !props.mIsFullScreen)
  337. {
  338. props.mWidth = width;
  339. props.mHeight = height;
  340. unsigned int winWidth, winHeight;
  341. getAdjustedWindowSize(width, height, mStyle, &winWidth, &winHeight);
  342. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  343. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  344. {
  345. ScopedSpinLock lock(mLock);
  346. mSyncedProperties.mWidth = props.mWidth;
  347. mSyncedProperties.mHeight = props.mHeight;
  348. }
  349. RenderWindowManager::instance().notifySyncDataDirty(this);
  350. }
  351. }
  352. void D3D9RenderWindowCore::getCustomAttribute(const String& name, void* pData) const
  353. {
  354. // Valid attributes and their equivalent native functions:
  355. // D3DDEVICE : getD3DDevice
  356. // WINDOW : getWindowHandle
  357. if( name == "D3DDEVICE" )
  358. {
  359. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  360. *pDev = _getD3D9Device();
  361. return;
  362. }
  363. else if( name == "WINDOW" )
  364. {
  365. UINT64 *pHwnd = (UINT64*)pData;
  366. *pHwnd = (UINT64)_getWindowHandle();
  367. return;
  368. }
  369. else if( name == "isTexture" )
  370. {
  371. bool *b = reinterpret_cast< bool * >( pData );
  372. *b = false;
  373. return;
  374. }
  375. else if( name == "D3DZBUFFER" )
  376. {
  377. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  378. *pSurf = mDevice->getDepthBuffer(this);
  379. return;
  380. }
  381. else if( name == "DDBACKBUFFER" )
  382. {
  383. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  384. *pSurf = mDevice->getBackBuffer(this);
  385. return;
  386. }
  387. else if( name == "DDFRONTBUFFER" )
  388. {
  389. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  390. *pSurf = mDevice->getBackBuffer(this);
  391. return;
  392. }
  393. }
  394. void D3D9RenderWindowCore::swapBuffers()
  395. {
  396. THROW_IF_NOT_CORE_THREAD;
  397. if (mDeviceValid)
  398. mDevice->present(this);
  399. }
  400. void D3D9RenderWindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  401. {
  402. THROW_IF_NOT_CORE_THREAD;
  403. mDevice->copyContentsToMemory(this, dst, buffer);
  404. }
  405. void D3D9RenderWindowCore::_windowMovedOrResized()
  406. {
  407. THROW_IF_NOT_CORE_THREAD;
  408. D3D9RenderWindowProperties& props = mProperties;
  409. if (!mHWnd || IsIconic(mHWnd))
  410. return;
  411. updateWindowRect();
  412. RenderWindowCore::_windowMovedOrResized();
  413. }
  414. /************************************************************************/
  415. /* D3D9 IMPLEMENTATION SPECIFIC */
  416. /************************************************************************/
  417. void D3D9RenderWindowCore::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight,
  418. DWORD style, UINT32* winWidth, UINT32* winHeight)
  419. {
  420. D3D9RenderWindowProperties& props = mProperties;
  421. RECT rc;
  422. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  423. AdjustWindowRect(&rc, style, false);
  424. *winWidth = rc.right - rc.left;
  425. *winHeight = rc.bottom - rc.top;
  426. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  427. MONITORINFO monitorInfo;
  428. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  429. monitorInfo.cbSize = sizeof(MONITORINFO);
  430. GetMonitorInfo(hMonitor, &monitorInfo);
  431. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  432. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  433. if (*winWidth > (unsigned int)maxW)
  434. *winWidth = maxW;
  435. if (*winHeight > (unsigned int)maxH)
  436. *winHeight = maxH;
  437. }
  438. void D3D9RenderWindowCore::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  439. {
  440. const D3D9RenderWindowProperties& props = mProperties;
  441. IDirect3D9* pD3D = D3D9RenderAPI::getDirect3D9();
  442. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  443. if (mDevice != NULL)
  444. devType = mDevice->getDeviceType();
  445. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  446. presentParams->Windowed = !props.mIsFullScreen;
  447. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  448. presentParams->BackBufferCount = 1;
  449. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  450. presentParams->hDeviceWindow = mHWnd;
  451. presentParams->BackBufferWidth = props.mWidth;
  452. presentParams->BackBufferHeight = props.mHeight;
  453. presentParams->FullScreen_RefreshRateInHz = props.mIsFullScreen ? mDisplayFrequency : 0;
  454. if (presentParams->BackBufferWidth == 0)
  455. presentParams->BackBufferWidth = 1;
  456. if (presentParams->BackBufferHeight == 0)
  457. presentParams->BackBufferHeight = 1;
  458. if (props.getVSync())
  459. {
  460. if (props.isFullScreen())
  461. {
  462. switch(mVSyncInterval)
  463. {
  464. case 1:
  465. default:
  466. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  467. break;
  468. case 2:
  469. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  470. break;
  471. case 3:
  472. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  473. break;
  474. case 4:
  475. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  476. break;
  477. };
  478. D3DCAPS9 caps;
  479. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  480. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  481. {
  482. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  483. }
  484. }
  485. else
  486. {
  487. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  488. }
  489. }
  490. else
  491. {
  492. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  493. }
  494. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  495. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  496. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  497. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  498. {
  499. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  500. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  501. D3DRTYPE_SURFACE, D3DFMT_D32)))
  502. {
  503. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  504. }
  505. else
  506. {
  507. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  508. }
  509. }
  510. else
  511. {
  512. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  513. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  514. {
  515. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  516. }
  517. else
  518. {
  519. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  520. }
  521. }
  522. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(BansheeEngine::RenderAPICore::instancePtr());
  523. D3DMULTISAMPLE_TYPE multisampleType;
  524. DWORD multisampleQuality;
  525. rs->determineMultisampleSettings(mDevice->getD3D9Device(), props.getMultisampleCount(),
  526. presentParams->BackBufferFormat, props.isFullScreen(), &multisampleType, &multisampleQuality);
  527. presentParams->MultiSampleType = multisampleType;
  528. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  529. }
  530. HWND D3D9RenderWindowCore::_getWindowHandle() const
  531. {
  532. return mHWnd;
  533. }
  534. IDirect3DDevice9* D3D9RenderWindowCore::_getD3D9Device() const
  535. {
  536. return mDevice->getD3D9Device();
  537. }
  538. IDirect3DSurface9* D3D9RenderWindowCore::_getRenderSurface() const
  539. {
  540. return mDevice->getBackBuffer(this);
  541. }
  542. D3D9Device* D3D9RenderWindowCore::_getDevice() const
  543. {
  544. return mDevice;
  545. }
  546. void D3D9RenderWindowCore::_setDevice(D3D9Device* device)
  547. {
  548. mDevice = device;
  549. mDeviceValid = false;
  550. }
  551. bool D3D9RenderWindowCore::_isDepthBuffered() const
  552. {
  553. return mIsDepthBuffered;
  554. }
  555. void D3D9RenderWindowCore::updateWindowRect()
  556. {
  557. RECT rc;
  558. BOOL result;
  559. D3D9RenderWindowProperties& props = mProperties;
  560. // Update top left parameters
  561. result = GetWindowRect(_getWindowHandle(), &rc);
  562. if (result == FALSE)
  563. {
  564. props.mTop = 0;
  565. props.mLeft = 0;
  566. props.mWidth = 0;
  567. props.mHeight = 0;
  568. return;
  569. }
  570. props.mTop = rc.top;
  571. props.mLeft = rc.left;
  572. // Width and height represent drawable area only
  573. result = GetClientRect(_getWindowHandle(), &rc);
  574. if (result == FALSE)
  575. {
  576. props.mTop = 0;
  577. props.mLeft = 0;
  578. props.mWidth = 0;
  579. props.mHeight = 0;
  580. return;
  581. }
  582. props.mWidth = rc.right - rc.left;
  583. props.mHeight = rc.bottom - rc.top;
  584. }
  585. bool D3D9RenderWindowCore::_validateDevice()
  586. {
  587. mDeviceValid = mDevice->validate(this);
  588. return mDeviceValid;
  589. }
  590. void D3D9RenderWindowCore::syncProperties()
  591. {
  592. ScopedSpinLock lock(mLock);
  593. mProperties = mSyncedProperties;
  594. }
  595. D3D9RenderWindow::D3D9RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, HINSTANCE instance)
  596. :RenderWindow(desc, windowId), mInstance(instance), mProperties(desc)
  597. {
  598. }
  599. void D3D9RenderWindow::getCustomAttribute(const String& name, void* pData) const
  600. {
  601. if (name == "WINDOW")
  602. {
  603. UINT64 *pHwnd = (UINT64*)pData;
  604. *pHwnd = (UINT64)getHWnd();
  605. return;
  606. }
  607. }
  608. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  609. {
  610. POINT pos;
  611. pos.x = screenPos.x;
  612. pos.y = screenPos.y;
  613. ScreenToClient(getHWnd(), &pos);
  614. return Vector2I(pos.x, pos.y);
  615. }
  616. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  617. {
  618. POINT pos;
  619. pos.x = windowPos.x;
  620. pos.y = windowPos.y;
  621. ClientToScreen(getHWnd(), &pos);
  622. return Vector2I(pos.x, pos.y);
  623. }
  624. HWND D3D9RenderWindow::getHWnd() const
  625. {
  626. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  627. // could be returned here if requested too soon after initialization.
  628. return getCore()->_getWindowHandle();
  629. }
  630. SPtr<D3D9RenderWindowCore> D3D9RenderWindow::getCore() const
  631. {
  632. return std::static_pointer_cast<D3D9RenderWindowCore>(mCoreSpecific);
  633. }
  634. void D3D9RenderWindow::syncProperties()
  635. {
  636. ScopedSpinLock lock(getCore()->mLock);
  637. mProperties = getCore()->mSyncedProperties;
  638. }
  639. }