BsD3D9RenderWindow.cpp 15 KB

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