BsD3D9RenderWindow.cpp 16 KB

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