2
0

BsD3D9RenderWindow.cpp 16 KB

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