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