BsD3D9RenderWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D9RenderWindow.h"
  4. #include "BsInput.h"
  5. #include "BsCoreThread.h"
  6. #include "BsViewport.h"
  7. #include "BsException.h"
  8. #include "BsD3D9RenderAPI.h"
  9. #include "BsRenderAPI.h"
  10. #include "BsBitwise.h"
  11. #include "Win32/BsWin32Platform.h"
  12. #include "BsD3D9VideoModeInfo.h"
  13. #include "BsD3D9DeviceManager.h"
  14. #include "BsRenderWindowManager.h"
  15. #include "Win32/BsWin32Window.h"
  16. namespace BansheeEngine
  17. {
  18. D3D9RenderWindowProperties::D3D9RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  19. :RenderWindowProperties(desc)
  20. {
  21. }
  22. D3D9RenderWindowCore::D3D9RenderWindowCore(const RENDER_WINDOW_DESC& desc, UINT32 windowId, HINSTANCE instance)
  23. : RenderWindowCore(desc, windowId), mInstance(instance), mProperties(desc), mSyncedProperties(desc),
  24. mIsDepthBuffered(true), mIsChild(false), mWindow(nullptr),mDevice(nullptr), mDisplayFrequency(0),
  25. mDeviceValid(false), mShowOnSwap(false)
  26. { }
  27. D3D9RenderWindowCore::~D3D9RenderWindowCore()
  28. {
  29. if (mDevice != nullptr)
  30. {
  31. mDevice->detachRenderWindow(this);
  32. mDevice = nullptr;
  33. }
  34. if (mWindow != nullptr)
  35. {
  36. bs_delete(mWindow);
  37. mWindow = nullptr;
  38. }
  39. mProperties.mActive = false;
  40. }
  41. void D3D9RenderWindowCore::initialize()
  42. {
  43. RenderWindowCore::initialize();
  44. D3D9RenderWindowProperties& props = mProperties;
  45. mMultisampleType = D3DMULTISAMPLE_NONE;
  46. mMultisampleQuality = 0;
  47. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  48. WINDOW_DESC windowDesc;
  49. windowDesc.border = mDesc.border;
  50. windowDesc.enableDoubleClick = mDesc.enableDoubleClick;
  51. windowDesc.fullscreen = mDesc.fullscreen;
  52. windowDesc.width = mDesc.videoMode.getWidth();
  53. windowDesc.height = mDesc.videoMode.getHeight();
  54. windowDesc.hidden = mDesc.hidden || mDesc.hideUntilSwap;
  55. windowDesc.left = mDesc.left;
  56. windowDesc.top = mDesc.top;
  57. windowDesc.outerDimensions = mDesc.outerDimensions;
  58. windowDesc.title = mDesc.title;
  59. windowDesc.toolWindow = mDesc.toolWindow;
  60. windowDesc.creationParams = this;
  61. windowDesc.module = mInstance;
  62. NameValuePairList::const_iterator opt;
  63. opt = mDesc.platformSpecific.find("parentWindowHandle");
  64. if (opt != mDesc.platformSpecific.end())
  65. windowDesc.parent = (HWND)parseUINT64(opt->second);
  66. opt = mDesc.platformSpecific.find("externalWindowHandle");
  67. if (opt != mDesc.platformSpecific.end())
  68. windowDesc.external = (HWND)parseUINT64(opt->second);
  69. mIsChild = windowDesc.parent != nullptr;
  70. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  71. props.mColorDepth = 32;
  72. props.mActive = true;
  73. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  74. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  75. if (numOutputs > 0)
  76. {
  77. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  78. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  79. windowDesc.monitor = outputInfo.getMonitorHandle();
  80. }
  81. if (!windowDesc.external)
  82. {
  83. mShowOnSwap = mDesc.hideUntilSwap;
  84. props.mHidden = mDesc.hideUntilSwap;
  85. }
  86. mWindow = bs_new<Win32Window>(windowDesc);
  87. props.mWidth = mWindow->getWidth();
  88. props.mHeight = mWindow->getHeight();
  89. props.mTop = mWindow->getTop();
  90. props.mLeft = mWindow->getLeft();
  91. mIsDepthBuffered = mDesc.depthBuffer;
  92. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(RenderAPICore::instancePtr());
  93. rs->registerWindow(*this);
  94. {
  95. ScopedSpinLock lock(mLock);
  96. mSyncedProperties = props;
  97. }
  98. RenderWindowManager::instance().notifySyncDataDirty(this);
  99. }
  100. void D3D9RenderWindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  101. {
  102. THROW_IF_NOT_CORE_THREAD;
  103. if (mIsChild)
  104. return;
  105. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  106. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  107. if (numOutputs == 0)
  108. return;
  109. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  110. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  111. D3D9RenderWindowProperties& props = mProperties;
  112. props.mWidth = width;
  113. props.mHeight = height;
  114. mDisplayFrequency = Math::roundToInt(refreshRate);
  115. props.mIsFullScreen = true;
  116. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  117. MONITORINFO monitorInfo;
  118. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  119. monitorInfo.cbSize = sizeof(MONITORINFO);
  120. GetMonitorInfo(hMonitor, &monitorInfo);
  121. props.mTop = monitorInfo.rcMonitor.top;
  122. props.mLeft = monitorInfo.rcMonitor.left;
  123. // Invalidate device, which resets it
  124. mDevice->invalidate(this);
  125. mDevice->acquire();
  126. _windowMovedOrResized();
  127. }
  128. void D3D9RenderWindowCore::setFullscreen(const VideoMode& mode)
  129. {
  130. THROW_IF_NOT_CORE_THREAD;
  131. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  132. }
  133. void D3D9RenderWindowCore::setWindowed(UINT32 width, UINT32 height)
  134. {
  135. THROW_IF_NOT_CORE_THREAD;
  136. D3D9RenderWindowProperties& props = mProperties;
  137. if (!props.mIsFullScreen)
  138. return;
  139. props.mIsFullScreen = false;
  140. props.mWidth = width;
  141. props.mHeight = height;
  142. UINT32 winWidth = 0;
  143. UINT32 winHeight = 0;
  144. RECT rect;
  145. SetRect(&rect, 0, 0, winWidth, winHeight);
  146. AdjustWindowRect(&rect, mWindow->getStyle(), false);
  147. winWidth = rect.right - rect.left;
  148. winHeight = rect.bottom - rect.top;
  149. // Deal with centering when switching down to smaller resolution
  150. HMONITOR hMonitor = MonitorFromWindow(mWindow->getHWnd(), MONITOR_DEFAULTTONEAREST);
  151. MONITORINFO monitorInfo;
  152. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  153. monitorInfo.cbSize = sizeof(MONITORINFO);
  154. GetMonitorInfo(hMonitor, &monitorInfo);
  155. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  156. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  157. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  158. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  159. SetWindowLong(mWindow->getHWnd(), GWL_STYLE, mWindow->getStyle());
  160. SetWindowLong(mWindow->getHWnd(), GWL_EXSTYLE, mWindow->getStyleEx());
  161. SetWindowPos(mWindow->getHWnd(), HWND_NOTOPMOST, left, top, winWidth, winHeight,
  162. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  163. mDevice->invalidate(this);
  164. mDevice->acquire();
  165. _windowMovedOrResized();
  166. }
  167. void D3D9RenderWindowCore::setActive(bool state)
  168. {
  169. THROW_IF_NOT_CORE_THREAD;
  170. mWindow->setActive(state);
  171. RenderWindowCore::setActive(state);
  172. }
  173. void D3D9RenderWindowCore::setHidden(bool hidden)
  174. {
  175. THROW_IF_NOT_CORE_THREAD;
  176. mShowOnSwap = false;
  177. mWindow->setHidden(hidden);
  178. RenderWindowCore::setHidden(hidden);
  179. }
  180. void D3D9RenderWindowCore::minimize()
  181. {
  182. THROW_IF_NOT_CORE_THREAD;
  183. mWindow->minimize();
  184. }
  185. void D3D9RenderWindowCore::maximize()
  186. {
  187. THROW_IF_NOT_CORE_THREAD;
  188. mWindow->maximize();
  189. }
  190. void D3D9RenderWindowCore::restore()
  191. {
  192. THROW_IF_NOT_CORE_THREAD;
  193. mWindow->restore();
  194. }
  195. void D3D9RenderWindowCore::move(INT32 left, INT32 top)
  196. {
  197. THROW_IF_NOT_CORE_THREAD;
  198. D3D9RenderWindowProperties& props = mProperties;
  199. if (!props.mIsFullScreen)
  200. {
  201. mWindow->move(left, top);
  202. props.mTop = mWindow->getTop();
  203. props.mLeft = mWindow->getLeft();
  204. {
  205. ScopedSpinLock lock(mLock);
  206. mSyncedProperties.mLeft = props.mLeft;
  207. mSyncedProperties.mTop = props.mTop;
  208. }
  209. RenderWindowManager::instance().notifySyncDataDirty(this);
  210. }
  211. }
  212. void D3D9RenderWindowCore::resize(UINT32 width, UINT32 height)
  213. {
  214. THROW_IF_NOT_CORE_THREAD;
  215. D3D9RenderWindowProperties& props = mProperties;
  216. if (!props.mIsFullScreen)
  217. {
  218. mWindow->resize(width, height);
  219. props.mWidth = mWindow->getWidth();
  220. props.mHeight = mWindow->getHeight();
  221. {
  222. ScopedSpinLock lock(mLock);
  223. mSyncedProperties.mWidth = props.mWidth;
  224. mSyncedProperties.mHeight = props.mHeight;
  225. }
  226. RenderWindowManager::instance().notifySyncDataDirty(this);
  227. }
  228. }
  229. void D3D9RenderWindowCore::getCustomAttribute(const String& name, void* pData) const
  230. {
  231. // Valid attributes and their equivalent native functions:
  232. // D3DDEVICE : getD3DDevice
  233. // WINDOW : getWindowHandle
  234. if( name == "D3DDEVICE" )
  235. {
  236. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  237. *pDev = _getD3D9Device();
  238. return;
  239. }
  240. else if( name == "WINDOW" )
  241. {
  242. UINT64 *pHwnd = (UINT64*)pData;
  243. *pHwnd = (UINT64)_getWindowHandle();
  244. return;
  245. }
  246. else if( name == "isTexture" )
  247. {
  248. bool *b = reinterpret_cast< bool * >( pData );
  249. *b = false;
  250. return;
  251. }
  252. else if( name == "D3DZBUFFER" )
  253. {
  254. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  255. *pSurf = mDevice->getDepthBuffer(this);
  256. return;
  257. }
  258. else if( name == "DDBACKBUFFER" )
  259. {
  260. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  261. *pSurf = mDevice->getBackBuffer(this);
  262. return;
  263. }
  264. else if( name == "DDFRONTBUFFER" )
  265. {
  266. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  267. *pSurf = mDevice->getBackBuffer(this);
  268. return;
  269. }
  270. }
  271. void D3D9RenderWindowCore::swapBuffers()
  272. {
  273. THROW_IF_NOT_CORE_THREAD;
  274. if (mShowOnSwap)
  275. setHidden(false);
  276. if (mDeviceValid)
  277. mDevice->present(this);
  278. }
  279. void D3D9RenderWindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  280. {
  281. THROW_IF_NOT_CORE_THREAD;
  282. mDevice->copyContentsToMemory(this, dst, buffer);
  283. }
  284. void D3D9RenderWindowCore::_windowMovedOrResized()
  285. {
  286. THROW_IF_NOT_CORE_THREAD;
  287. if (!mWindow)
  288. return;
  289. mWindow->_windowMovedOrResized();
  290. D3D9RenderWindowProperties& props = mProperties;
  291. if (!props.isFullScreen()) // Fullscreen is handled directly by this object
  292. {
  293. props.mTop = mWindow->getTop();
  294. props.mLeft = mWindow->getLeft();
  295. props.mWidth = mWindow->getWidth();
  296. props.mHeight = mWindow->getHeight();
  297. }
  298. RenderWindowCore::_windowMovedOrResized();
  299. }
  300. /************************************************************************/
  301. /* D3D9 IMPLEMENTATION SPECIFIC */
  302. /************************************************************************/
  303. void D3D9RenderWindowCore::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  304. {
  305. const D3D9RenderWindowProperties& props = mProperties;
  306. IDirect3D9* pD3D = D3D9RenderAPI::getDirect3D9();
  307. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  308. if (mDevice != NULL)
  309. devType = mDevice->getDeviceType();
  310. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  311. presentParams->Windowed = !props.mIsFullScreen;
  312. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  313. presentParams->BackBufferCount = 1;
  314. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  315. presentParams->hDeviceWindow = mWindow->getHWnd();
  316. presentParams->BackBufferWidth = props.mWidth;
  317. presentParams->BackBufferHeight = props.mHeight;
  318. presentParams->FullScreen_RefreshRateInHz = props.mIsFullScreen ? mDisplayFrequency : 0;
  319. if (presentParams->BackBufferWidth == 0)
  320. presentParams->BackBufferWidth = 1;
  321. if (presentParams->BackBufferHeight == 0)
  322. presentParams->BackBufferHeight = 1;
  323. if (props.getVSync())
  324. {
  325. if (props.isFullScreen())
  326. {
  327. switch(mVSyncInterval)
  328. {
  329. case 1:
  330. default:
  331. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  332. break;
  333. case 2:
  334. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  335. break;
  336. case 3:
  337. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  338. break;
  339. case 4:
  340. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  341. break;
  342. };
  343. D3DCAPS9 caps;
  344. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  345. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  346. {
  347. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  348. }
  349. }
  350. else
  351. {
  352. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  353. }
  354. }
  355. else
  356. {
  357. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  358. }
  359. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  360. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  361. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  362. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  363. {
  364. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  365. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  366. D3DRTYPE_SURFACE, D3DFMT_D32)))
  367. {
  368. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  369. }
  370. else
  371. {
  372. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  373. }
  374. }
  375. else
  376. {
  377. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  378. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  379. {
  380. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  381. }
  382. else
  383. {
  384. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  385. }
  386. }
  387. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(BansheeEngine::RenderAPICore::instancePtr());
  388. D3DMULTISAMPLE_TYPE multisampleType;
  389. DWORD multisampleQuality;
  390. rs->determineMultisampleSettings(mDevice->getD3D9Device(), props.getMultisampleCount(),
  391. presentParams->BackBufferFormat, props.isFullScreen(), &multisampleType, &multisampleQuality);
  392. presentParams->MultiSampleType = multisampleType;
  393. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  394. }
  395. HWND D3D9RenderWindowCore::_getWindowHandle() const
  396. {
  397. return mWindow->getHWnd();
  398. }
  399. IDirect3DDevice9* D3D9RenderWindowCore::_getD3D9Device() const
  400. {
  401. return mDevice->getD3D9Device();
  402. }
  403. IDirect3DSurface9* D3D9RenderWindowCore::_getRenderSurface() const
  404. {
  405. return mDevice->getBackBuffer(this);
  406. }
  407. D3D9Device* D3D9RenderWindowCore::_getDevice() const
  408. {
  409. return mDevice;
  410. }
  411. void D3D9RenderWindowCore::_setDevice(D3D9Device* device)
  412. {
  413. mDevice = device;
  414. mDeviceValid = false;
  415. }
  416. bool D3D9RenderWindowCore::_isDepthBuffered() const
  417. {
  418. return mIsDepthBuffered;
  419. }
  420. bool D3D9RenderWindowCore::_validateDevice()
  421. {
  422. mDeviceValid = mDevice->validate(this);
  423. return mDeviceValid;
  424. }
  425. void D3D9RenderWindowCore::syncProperties()
  426. {
  427. ScopedSpinLock lock(mLock);
  428. mProperties = mSyncedProperties;
  429. }
  430. D3D9RenderWindow::D3D9RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, HINSTANCE instance)
  431. :RenderWindow(desc, windowId), mInstance(instance), mProperties(desc)
  432. {
  433. }
  434. void D3D9RenderWindow::getCustomAttribute(const String& name, void* pData) const
  435. {
  436. if (name == "WINDOW")
  437. {
  438. UINT64 *pHwnd = (UINT64*)pData;
  439. *pHwnd = (UINT64)getHWnd();
  440. return;
  441. }
  442. }
  443. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  444. {
  445. POINT pos;
  446. pos.x = screenPos.x;
  447. pos.y = screenPos.y;
  448. ScreenToClient(getHWnd(), &pos);
  449. return Vector2I(pos.x, pos.y);
  450. }
  451. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  452. {
  453. POINT pos;
  454. pos.x = windowPos.x;
  455. pos.y = windowPos.y;
  456. ClientToScreen(getHWnd(), &pos);
  457. return Vector2I(pos.x, pos.y);
  458. }
  459. HWND D3D9RenderWindow::getHWnd() const
  460. {
  461. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  462. // could be returned here if requested too soon after initialization.
  463. return getCore()->_getWindowHandle();
  464. }
  465. SPtr<D3D9RenderWindowCore> D3D9RenderWindow::getCore() const
  466. {
  467. return std::static_pointer_cast<D3D9RenderWindowCore>(mCoreSpecific);
  468. }
  469. void D3D9RenderWindow::syncProperties()
  470. {
  471. ScopedSpinLock lock(getCore()->mLock);
  472. mProperties = getCore()->mSyncedProperties;
  473. }
  474. }