BsD3D9RenderWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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)parseUnsignedInt(opt->second);
  64. opt = mDesc.platformSpecific.find("externalWindowHandle");
  65. if (opt != mDesc.platformSpecific.end())
  66. windowDesc.external = (HWND)parseUnsignedInt(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. props.mTop = mWindow->getTop();
  290. props.mLeft = mWindow->getLeft();
  291. props.mWidth = mWindow->getWidth();
  292. props.mHeight = mWindow->getHeight();
  293. RenderWindowCore::_windowMovedOrResized();
  294. }
  295. /************************************************************************/
  296. /* D3D9 IMPLEMENTATION SPECIFIC */
  297. /************************************************************************/
  298. void D3D9RenderWindowCore::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  299. {
  300. const D3D9RenderWindowProperties& props = mProperties;
  301. IDirect3D9* pD3D = D3D9RenderAPI::getDirect3D9();
  302. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  303. if (mDevice != NULL)
  304. devType = mDevice->getDeviceType();
  305. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  306. presentParams->Windowed = !props.mIsFullScreen;
  307. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  308. presentParams->BackBufferCount = 1;
  309. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  310. presentParams->hDeviceWindow = mWindow->getHWnd();
  311. presentParams->BackBufferWidth = props.mWidth;
  312. presentParams->BackBufferHeight = props.mHeight;
  313. presentParams->FullScreen_RefreshRateInHz = props.mIsFullScreen ? mDisplayFrequency : 0;
  314. if (presentParams->BackBufferWidth == 0)
  315. presentParams->BackBufferWidth = 1;
  316. if (presentParams->BackBufferHeight == 0)
  317. presentParams->BackBufferHeight = 1;
  318. if (props.getVSync())
  319. {
  320. if (props.isFullScreen())
  321. {
  322. switch(mVSyncInterval)
  323. {
  324. case 1:
  325. default:
  326. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  327. break;
  328. case 2:
  329. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  330. break;
  331. case 3:
  332. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  333. break;
  334. case 4:
  335. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  336. break;
  337. };
  338. D3DCAPS9 caps;
  339. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  340. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  341. {
  342. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  343. }
  344. }
  345. else
  346. {
  347. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  348. }
  349. }
  350. else
  351. {
  352. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  353. }
  354. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  355. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  356. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  357. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  358. {
  359. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  360. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  361. D3DRTYPE_SURFACE, D3DFMT_D32)))
  362. {
  363. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  364. }
  365. else
  366. {
  367. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  368. }
  369. }
  370. else
  371. {
  372. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  373. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  374. {
  375. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  376. }
  377. else
  378. {
  379. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  380. }
  381. }
  382. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(BansheeEngine::RenderAPICore::instancePtr());
  383. D3DMULTISAMPLE_TYPE multisampleType;
  384. DWORD multisampleQuality;
  385. rs->determineMultisampleSettings(mDevice->getD3D9Device(), props.getMultisampleCount(),
  386. presentParams->BackBufferFormat, props.isFullScreen(), &multisampleType, &multisampleQuality);
  387. presentParams->MultiSampleType = multisampleType;
  388. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  389. }
  390. HWND D3D9RenderWindowCore::_getWindowHandle() const
  391. {
  392. return mWindow->getHWnd();
  393. }
  394. IDirect3DDevice9* D3D9RenderWindowCore::_getD3D9Device() const
  395. {
  396. return mDevice->getD3D9Device();
  397. }
  398. IDirect3DSurface9* D3D9RenderWindowCore::_getRenderSurface() const
  399. {
  400. return mDevice->getBackBuffer(this);
  401. }
  402. D3D9Device* D3D9RenderWindowCore::_getDevice() const
  403. {
  404. return mDevice;
  405. }
  406. void D3D9RenderWindowCore::_setDevice(D3D9Device* device)
  407. {
  408. mDevice = device;
  409. mDeviceValid = false;
  410. }
  411. bool D3D9RenderWindowCore::_isDepthBuffered() const
  412. {
  413. return mIsDepthBuffered;
  414. }
  415. bool D3D9RenderWindowCore::_validateDevice()
  416. {
  417. mDeviceValid = mDevice->validate(this);
  418. return mDeviceValid;
  419. }
  420. void D3D9RenderWindowCore::syncProperties()
  421. {
  422. ScopedSpinLock lock(mLock);
  423. mProperties = mSyncedProperties;
  424. }
  425. D3D9RenderWindow::D3D9RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId, HINSTANCE instance)
  426. :RenderWindow(desc, windowId), mInstance(instance), mProperties(desc)
  427. {
  428. }
  429. void D3D9RenderWindow::getCustomAttribute(const String& name, void* pData) const
  430. {
  431. if (name == "WINDOW")
  432. {
  433. UINT64 *pHwnd = (UINT64*)pData;
  434. *pHwnd = (UINT64)getHWnd();
  435. return;
  436. }
  437. }
  438. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  439. {
  440. POINT pos;
  441. pos.x = screenPos.x;
  442. pos.y = screenPos.y;
  443. ScreenToClient(getHWnd(), &pos);
  444. return Vector2I(pos.x, pos.y);
  445. }
  446. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  447. {
  448. POINT pos;
  449. pos.x = windowPos.x;
  450. pos.y = windowPos.y;
  451. ClientToScreen(getHWnd(), &pos);
  452. return Vector2I(pos.x, pos.y);
  453. }
  454. HWND D3D9RenderWindow::getHWnd() const
  455. {
  456. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  457. // could be returned here if requested too soon after initialization.
  458. return getCore()->_getWindowHandle();
  459. }
  460. SPtr<D3D9RenderWindowCore> D3D9RenderWindow::getCore() const
  461. {
  462. return std::static_pointer_cast<D3D9RenderWindowCore>(mCoreSpecific);
  463. }
  464. void D3D9RenderWindow::syncProperties()
  465. {
  466. ScopedSpinLock lock(getCore()->mLock);
  467. mProperties = getCore()->mSyncedProperties;
  468. }
  469. }