BsD3D9RenderWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. namespace BansheeEngine
  14. {
  15. D3D9RenderWindowProperties::D3D9RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  16. :RenderWindowProperties(desc)
  17. {
  18. }
  19. D3D9RenderWindowCore::D3D9RenderWindowCore(const RENDER_WINDOW_DESC& desc, HINSTANCE instance)
  20. : RenderWindowCore(desc), mInstance(instance), mProperties(desc), mIsDepthBuffered(true), mIsChild(false), mHWnd(0),
  21. mStyle(0), mWindowedStyle(0), mWindowedStyleEx(0), mDevice(nullptr), mIsExternal(false), mDisplayFrequency(0), mDeviceValid(false)
  22. { }
  23. D3D9RenderWindowCore::~D3D9RenderWindowCore()
  24. {
  25. if (mDevice != nullptr)
  26. {
  27. mDevice->detachRenderWindow(this);
  28. mDevice = nullptr;
  29. }
  30. if (mHWnd && !mIsExternal)
  31. {
  32. DestroyWindow(mHWnd);
  33. }
  34. mHWnd = 0;
  35. mProperties.mActive = false;
  36. }
  37. void D3D9RenderWindowCore::initialize()
  38. {
  39. RenderWindowCore::initialize();
  40. HINSTANCE hInst = mInstance;
  41. mMultisampleType = D3DMULTISAMPLE_NONE;
  42. mMultisampleQuality = 0;
  43. mDisplayFrequency = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  44. HWND parentHWnd = 0;
  45. HWND externalHandle = 0;
  46. NameValuePairList::const_iterator opt;
  47. opt = mDesc.platformSpecific.find("parentWindowHandle");
  48. if (opt != mDesc.platformSpecific.end())
  49. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  50. opt = mDesc.platformSpecific.find("externalWindowHandle");
  51. if (opt != mDesc.platformSpecific.end())
  52. externalHandle = (HWND)parseUnsignedInt(opt->second);
  53. mIsChild = parentHWnd != 0;
  54. mWindowedStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  55. mWindowedStyleEx = 0;
  56. if (!mDesc.fullscreen || mIsChild)
  57. {
  58. if (parentHWnd)
  59. {
  60. if (mDesc.toolWindow)
  61. mWindowedStyleEx = WS_EX_TOOLWINDOW;
  62. else
  63. mWindowedStyle |= WS_CHILD;
  64. }
  65. if (!parentHWnd || mDesc.toolWindow)
  66. {
  67. if (mDesc.border == WindowBorder::None)
  68. mWindowedStyle |= WS_POPUP;
  69. else if (mDesc.border == WindowBorder::Fixed)
  70. mWindowedStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  71. WS_SYSMENU | WS_MINIMIZEBOX;
  72. else
  73. mWindowedStyle |= WS_OVERLAPPEDWINDOW;
  74. }
  75. }
  76. D3D9RenderWindowProperties& props = mProperties;
  77. if (!externalHandle)
  78. {
  79. MONITORINFO monitorInfo;
  80. RECT rc;
  81. HMONITOR hMonitor = NULL;
  82. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  83. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  84. if (numOutputs > 0)
  85. {
  86. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  87. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  88. hMonitor = outputInfo.getMonitorHandle();
  89. }
  90. // If we didn't specified the adapter index, or if it didn't find it
  91. if (hMonitor == NULL)
  92. {
  93. POINT windowAnchorPoint;
  94. // Fill in anchor point.
  95. windowAnchorPoint.x = mDesc.left;
  96. windowAnchorPoint.y = mDesc.top;
  97. // Get the nearest monitor to this window.
  98. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  99. }
  100. // Get the target monitor info
  101. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  102. monitorInfo.cbSize = sizeof(MONITORINFO);
  103. GetMonitorInfo(hMonitor, &monitorInfo);
  104. unsigned int winWidth, winHeight;
  105. winWidth = mDesc.videoMode.getWidth();
  106. winHeight = mDesc.videoMode.getHeight();
  107. UINT32 left = mDesc.left;
  108. UINT32 top = mDesc.top;
  109. // No specified top left -> Center the window in the middle of the monitor
  110. if (left == -1 || top == -1)
  111. {
  112. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  113. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  114. // clamp window dimensions to screen size
  115. int outerw = (int(winWidth) < screenw) ? int(winWidth) : screenw;
  116. int outerh = (int(winHeight) < screenh) ? int(winHeight) : screenh;
  117. if (left == -1)
  118. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  119. else if (hMonitor != NULL)
  120. left += monitorInfo.rcWork.left;
  121. if (top == -1)
  122. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  123. else if (hMonitor != NULL)
  124. top += monitorInfo.rcWork.top;
  125. }
  126. else if (hMonitor != NULL)
  127. {
  128. left += monitorInfo.rcWork.left;
  129. top += monitorInfo.rcWork.top;
  130. }
  131. props.mWidth = mDesc.videoMode.getWidth();
  132. props.mHeight = mDesc.videoMode.getHeight();
  133. props.mTop = top;
  134. props.mLeft = left;
  135. DWORD dwStyle = 0;
  136. DWORD dwStyleEx = 0;
  137. if (mDesc.fullscreen && !mIsChild)
  138. {
  139. dwStyle = WS_VISIBLE | WS_CLIPCHILDREN | WS_POPUP;
  140. props.mTop = monitorInfo.rcMonitor.top;
  141. props.mLeft = monitorInfo.rcMonitor.left;
  142. }
  143. else
  144. {
  145. dwStyle = mWindowedStyle;
  146. dwStyleEx = mWindowedStyleEx;
  147. getAdjustedWindowSize(mDesc.videoMode.getWidth(), mDesc.videoMode.getHeight(), dwStyle, &winWidth, &winHeight);
  148. if (!mDesc.outerDimensions)
  149. {
  150. // Calculate window dimensions required
  151. // to get the requested client area
  152. SetRect(&rc, 0, 0, props.mWidth, props.mHeight);
  153. AdjustWindowRect(&rc, dwStyle, false);
  154. props.mWidth = rc.right - rc.left;
  155. props.mHeight = rc.bottom - rc.top;
  156. // Clamp window rect to the nearest display monitor.
  157. if (props.mLeft < monitorInfo.rcWork.left)
  158. props.mLeft = monitorInfo.rcWork.left;
  159. if (props.mTop < monitorInfo.rcWork.top)
  160. props.mTop = monitorInfo.rcWork.top;
  161. if (static_cast<int>(winWidth) > monitorInfo.rcWork.right - props.mLeft)
  162. winWidth = monitorInfo.rcWork.right - props.mLeft;
  163. if (static_cast<int>(winHeight) > monitorInfo.rcWork.bottom - props.mTop)
  164. winHeight = monitorInfo.rcWork.bottom - props.mTop;
  165. }
  166. }
  167. // Register the window class
  168. WNDCLASS wc = { 0, Win32Platform::_win32WndProc, 0, 0, hInst,
  169. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  170. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D9Wnd" };
  171. RegisterClass(&wc);
  172. // Create our main window
  173. // Pass pointer to self
  174. mIsExternal = false;
  175. mHWnd = CreateWindowEx(dwStyleEx, "D3D9Wnd", mDesc.title.c_str(), dwStyle,
  176. props.mLeft, props.mTop, winWidth, winHeight, parentHWnd, 0, hInst, this);
  177. mStyle = dwStyle;
  178. }
  179. else
  180. {
  181. mHWnd = externalHandle;
  182. mIsExternal = true;
  183. }
  184. RECT rc;
  185. GetWindowRect(mHWnd, &rc);
  186. props.mTop = rc.top;
  187. props.mLeft = rc.left;
  188. GetClientRect(mHWnd, &rc);
  189. props.mWidth = rc.right;
  190. props.mHeight = rc.bottom;
  191. mIsDepthBuffered = mDesc.depthBuffer;
  192. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  193. props.mColorDepth = 32;
  194. props.mActive = true;
  195. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(RenderAPICore::instancePtr());
  196. rs->registerWindow(*this);
  197. }
  198. void D3D9RenderWindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  199. {
  200. THROW_IF_NOT_CORE_THREAD;
  201. if (mIsChild)
  202. return;
  203. const D3D9VideoModeInfo& videoModeInfo = static_cast<const D3D9VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  204. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  205. if (numOutputs == 0)
  206. return;
  207. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  208. const D3D9VideoOutputInfo& outputInfo = static_cast<const D3D9VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  209. D3D9RenderWindowProperties& props = mProperties;
  210. bool oldFullscreen = props.mIsFullScreen;
  211. props.mWidth = width;
  212. props.mHeight = height;
  213. mDisplayFrequency = Math::roundToInt(refreshRate);
  214. props.mIsFullScreen = true;
  215. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  216. MONITORINFO monitorInfo;
  217. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  218. monitorInfo.cbSize = sizeof(MONITORINFO);
  219. GetMonitorInfo(hMonitor, &monitorInfo);
  220. props.mTop = monitorInfo.rcMonitor.top;
  221. props.mLeft = monitorInfo.rcMonitor.left;
  222. // Invalidate device, which resets it
  223. mDevice->invalidate(this);
  224. mDevice->acquire();
  225. RenderWindowManager::instance().notifyMovedOrResized(this);
  226. }
  227. void D3D9RenderWindowCore::setFullscreen(const VideoMode& mode)
  228. {
  229. THROW_IF_NOT_CORE_THREAD;
  230. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  231. }
  232. void D3D9RenderWindowCore::setWindowed(UINT32 width, UINT32 height)
  233. {
  234. THROW_IF_NOT_CORE_THREAD;
  235. D3D9RenderWindowProperties& props = mProperties;
  236. if (!props.mIsFullScreen)
  237. return;
  238. props.mIsFullScreen = false;
  239. mStyle = mWindowedStyle;
  240. props.mWidth = width;
  241. props.mHeight = height;
  242. unsigned int winWidth, winHeight;
  243. getAdjustedWindowSize(props.mWidth, props.mHeight, mStyle, &winWidth, &winHeight);
  244. // Deal with centering when switching down to smaller resolution
  245. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  246. MONITORINFO monitorInfo;
  247. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  248. monitorInfo.cbSize = sizeof(MONITORINFO);
  249. GetMonitorInfo(hMonitor, &monitorInfo);
  250. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  251. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  252. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  253. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  254. SetWindowLong(mHWnd, GWL_STYLE, mStyle);
  255. SetWindowLong(mHWnd, GWL_EXSTYLE, mWindowedStyleEx);
  256. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  257. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  258. mDevice->invalidate(this);
  259. mDevice->acquire();
  260. RenderWindowManager::instance().notifyMovedOrResized(this);
  261. }
  262. void D3D9RenderWindowCore::setHidden(bool hidden)
  263. {
  264. THROW_IF_NOT_CORE_THREAD;
  265. D3D9RenderWindowProperties& props = mProperties;
  266. props.mHidden = hidden;
  267. if (!mIsExternal)
  268. {
  269. if (hidden)
  270. ShowWindow(mHWnd, SW_HIDE);
  271. else
  272. ShowWindow(mHWnd, SW_SHOWNORMAL);
  273. }
  274. RenderWindowManager::instance().notifyPropertiesDirty(this);
  275. }
  276. void D3D9RenderWindowCore::minimize()
  277. {
  278. THROW_IF_NOT_CORE_THREAD;
  279. if (mHWnd)
  280. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  281. }
  282. void D3D9RenderWindowCore::maximize()
  283. {
  284. THROW_IF_NOT_CORE_THREAD;
  285. if (mHWnd)
  286. SendMessage(mHWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  287. }
  288. void D3D9RenderWindowCore::restore()
  289. {
  290. THROW_IF_NOT_CORE_THREAD;
  291. if (mHWnd)
  292. SendMessage(mHWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  293. }
  294. void D3D9RenderWindowCore::move(INT32 top, INT32 left)
  295. {
  296. THROW_IF_NOT_CORE_THREAD;
  297. D3D9RenderWindowProperties& props = mProperties;
  298. if (mHWnd && !props.mIsFullScreen)
  299. {
  300. props.mLeft = left;
  301. props.mTop = top;
  302. SetWindowPos(mHWnd, 0, top, left, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  303. }
  304. }
  305. void D3D9RenderWindowCore::resize(UINT32 width, UINT32 height)
  306. {
  307. THROW_IF_NOT_CORE_THREAD;
  308. D3D9RenderWindowProperties& props = mProperties;
  309. if (mHWnd && !props.mIsFullScreen)
  310. {
  311. props.mWidth = width;
  312. props.mHeight = height;
  313. unsigned int winWidth, winHeight;
  314. getAdjustedWindowSize(width, height, mStyle, &winWidth, &winHeight);
  315. SetWindowPos(mHWnd, 0, 0, 0, winWidth, winHeight,
  316. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  317. }
  318. }
  319. void D3D9RenderWindowCore::getCustomAttribute(const String& name, void* pData) const
  320. {
  321. // Valid attributes and their equivalent native functions:
  322. // D3DDEVICE : getD3DDevice
  323. // WINDOW : getWindowHandle
  324. if( name == "D3DDEVICE" )
  325. {
  326. IDirect3DDevice9* *pDev = (IDirect3DDevice9**)pData;
  327. *pDev = _getD3D9Device();
  328. return;
  329. }
  330. else if( name == "WINDOW" )
  331. {
  332. UINT64 *pHwnd = (UINT64*)pData;
  333. *pHwnd = (UINT64)_getWindowHandle();
  334. return;
  335. }
  336. else if( name == "isTexture" )
  337. {
  338. bool *b = reinterpret_cast< bool * >( pData );
  339. *b = false;
  340. return;
  341. }
  342. else if( name == "D3DZBUFFER" )
  343. {
  344. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  345. *pSurf = mDevice->getDepthBuffer(this);
  346. return;
  347. }
  348. else if( name == "DDBACKBUFFER" )
  349. {
  350. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  351. *pSurf = mDevice->getBackBuffer(this);
  352. return;
  353. }
  354. else if( name == "DDFRONTBUFFER" )
  355. {
  356. IDirect3DSurface9* *pSurf = (IDirect3DSurface9**)pData;
  357. *pSurf = mDevice->getBackBuffer(this);
  358. return;
  359. }
  360. }
  361. void D3D9RenderWindowCore::swapBuffers()
  362. {
  363. THROW_IF_NOT_CORE_THREAD;
  364. if (mDeviceValid)
  365. mDevice->present(this);
  366. }
  367. void D3D9RenderWindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  368. {
  369. THROW_IF_NOT_CORE_THREAD;
  370. mDevice->copyContentsToMemory(this, dst, buffer);
  371. }
  372. void D3D9RenderWindowCore::_windowMovedOrResized()
  373. {
  374. THROW_IF_NOT_CORE_THREAD;
  375. D3D9RenderWindowProperties& props = mProperties;
  376. if (!mHWnd || IsIconic(mHWnd))
  377. return;
  378. updateWindowRect();
  379. RenderWindowCore::_windowMovedOrResized();
  380. }
  381. /************************************************************************/
  382. /* D3D9 IMPLEMENTATION SPECIFIC */
  383. /************************************************************************/
  384. void D3D9RenderWindowCore::getAdjustedWindowSize(UINT32 clientWidth, UINT32 clientHeight,
  385. DWORD style, UINT32* winWidth, UINT32* winHeight)
  386. {
  387. D3D9RenderWindowProperties& props = mProperties;
  388. RECT rc;
  389. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  390. AdjustWindowRect(&rc, style, false);
  391. *winWidth = rc.right - rc.left;
  392. *winHeight = rc.bottom - rc.top;
  393. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  394. MONITORINFO monitorInfo;
  395. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  396. monitorInfo.cbSize = sizeof(MONITORINFO);
  397. GetMonitorInfo(hMonitor, &monitorInfo);
  398. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  399. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  400. if (*winWidth > (unsigned int)maxW)
  401. *winWidth = maxW;
  402. if (*winHeight > (unsigned int)maxH)
  403. *winHeight = maxH;
  404. }
  405. void D3D9RenderWindowCore::_buildPresentParameters(D3DPRESENT_PARAMETERS* presentParams) const
  406. {
  407. const D3D9RenderWindowProperties& props = mProperties;
  408. IDirect3D9* pD3D = D3D9RenderAPI::getDirect3D9();
  409. D3DDEVTYPE devType = D3DDEVTYPE_HAL;
  410. if (mDevice != NULL)
  411. devType = mDevice->getDeviceType();
  412. ZeroMemory( presentParams, sizeof(D3DPRESENT_PARAMETERS) );
  413. presentParams->Windowed = !props.mIsFullScreen;
  414. presentParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
  415. presentParams->BackBufferCount = 1;
  416. presentParams->EnableAutoDepthStencil = mIsDepthBuffered;
  417. presentParams->hDeviceWindow = mHWnd;
  418. presentParams->BackBufferWidth = props.mWidth;
  419. presentParams->BackBufferHeight = props.mHeight;
  420. presentParams->FullScreen_RefreshRateInHz = props.mIsFullScreen ? mDisplayFrequency : 0;
  421. if (presentParams->BackBufferWidth == 0)
  422. presentParams->BackBufferWidth = 1;
  423. if (presentParams->BackBufferHeight == 0)
  424. presentParams->BackBufferHeight = 1;
  425. if (props.getVSync())
  426. {
  427. if (props.isFullScreen())
  428. {
  429. switch(mVSyncInterval)
  430. {
  431. case 1:
  432. default:
  433. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  434. break;
  435. case 2:
  436. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_TWO;
  437. break;
  438. case 3:
  439. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_THREE;
  440. break;
  441. case 4:
  442. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_FOUR;
  443. break;
  444. };
  445. D3DCAPS9 caps;
  446. pD3D->GetDeviceCaps(mDevice->getAdapterNumber(), devType, &caps);
  447. if (!(caps.PresentationIntervals & presentParams->PresentationInterval))
  448. {
  449. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  450. }
  451. }
  452. else
  453. {
  454. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  455. }
  456. }
  457. else
  458. {
  459. presentParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  460. }
  461. presentParams->BackBufferFormat = D3DFMT_X8R8G8B8;
  462. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  463. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  464. D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  465. {
  466. if (FAILED(pD3D->CheckDeviceFormat(mDevice->getAdapterNumber(),
  467. devType, presentParams->BackBufferFormat, D3DUSAGE_DEPTHSTENCIL,
  468. D3DRTYPE_SURFACE, D3DFMT_D32)))
  469. {
  470. presentParams->AutoDepthStencilFormat = D3DFMT_D16;
  471. }
  472. else
  473. {
  474. presentParams->AutoDepthStencilFormat = D3DFMT_D32;
  475. }
  476. }
  477. else
  478. {
  479. if (SUCCEEDED(pD3D->CheckDepthStencilMatch(mDevice->getAdapterNumber(), devType,
  480. presentParams->BackBufferFormat, presentParams->BackBufferFormat, D3DFMT_D24S8)))
  481. {
  482. presentParams->AutoDepthStencilFormat = D3DFMT_D24S8;
  483. }
  484. else
  485. {
  486. presentParams->AutoDepthStencilFormat = D3DFMT_D24X8;
  487. }
  488. }
  489. D3D9RenderAPI* rs = static_cast<D3D9RenderAPI*>(BansheeEngine::RenderAPICore::instancePtr());
  490. D3DMULTISAMPLE_TYPE multisampleType;
  491. DWORD multisampleQuality;
  492. rs->determineMultisampleSettings(mDevice->getD3D9Device(), props.getMultisampleCount(),
  493. presentParams->BackBufferFormat, props.isFullScreen(), &multisampleType, &multisampleQuality);
  494. presentParams->MultiSampleType = multisampleType;
  495. presentParams->MultiSampleQuality = (multisampleQuality == 0) ? 0 : multisampleQuality;
  496. }
  497. HWND D3D9RenderWindowCore::_getWindowHandle() const
  498. {
  499. return mHWnd;
  500. }
  501. IDirect3DDevice9* D3D9RenderWindowCore::_getD3D9Device() const
  502. {
  503. return mDevice->getD3D9Device();
  504. }
  505. IDirect3DSurface9* D3D9RenderWindowCore::_getRenderSurface() const
  506. {
  507. return mDevice->getBackBuffer(this);
  508. }
  509. D3D9Device* D3D9RenderWindowCore::_getDevice() const
  510. {
  511. return mDevice;
  512. }
  513. void D3D9RenderWindowCore::_setDevice(D3D9Device* device)
  514. {
  515. mDevice = device;
  516. mDeviceValid = false;
  517. }
  518. bool D3D9RenderWindowCore::_isDepthBuffered() const
  519. {
  520. return mIsDepthBuffered;
  521. }
  522. void D3D9RenderWindowCore::updateWindowRect()
  523. {
  524. RECT rc;
  525. BOOL result;
  526. D3D9RenderWindowProperties& props = mProperties;
  527. // Update top left parameters
  528. result = GetWindowRect(_getWindowHandle(), &rc);
  529. if (result == FALSE)
  530. {
  531. props.mTop = 0;
  532. props.mLeft = 0;
  533. props.mWidth = 0;
  534. props.mHeight = 0;
  535. return;
  536. }
  537. props.mTop = rc.top;
  538. props.mLeft = rc.left;
  539. // Width and height represent drawable area only
  540. result = GetClientRect(_getWindowHandle(), &rc);
  541. if (result == FALSE)
  542. {
  543. props.mTop = 0;
  544. props.mLeft = 0;
  545. props.mWidth = 0;
  546. props.mHeight = 0;
  547. return;
  548. }
  549. props.mWidth = rc.right - rc.left;
  550. props.mHeight = rc.bottom - rc.top;
  551. }
  552. UINT32 D3D9RenderWindowCore::getSyncData(UINT8* buffer)
  553. {
  554. UINT32 size = sizeof(mProperties);
  555. if (buffer != nullptr)
  556. memcpy(buffer, &mProperties, size);
  557. return size;
  558. }
  559. bool D3D9RenderWindowCore::_validateDevice()
  560. {
  561. mDeviceValid = mDevice->validate(this);
  562. return mDeviceValid;
  563. }
  564. D3D9RenderWindow::D3D9RenderWindow(const RENDER_WINDOW_DESC& desc, HINSTANCE instance)
  565. :RenderWindow(desc), mInstance(instance), mProperties(desc)
  566. {
  567. }
  568. void D3D9RenderWindow::getCustomAttribute(const String& name, void* pData) const
  569. {
  570. if (name == "WINDOW")
  571. {
  572. UINT64 *pHwnd = (UINT64*)pData;
  573. *pHwnd = (UINT64)getHWnd();
  574. return;
  575. }
  576. }
  577. Vector2I D3D9RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  578. {
  579. POINT pos;
  580. pos.x = screenPos.x;
  581. pos.y = screenPos.y;
  582. ScreenToClient(getHWnd(), &pos);
  583. return Vector2I(pos.x, pos.y);
  584. }
  585. Vector2I D3D9RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  586. {
  587. POINT pos;
  588. pos.x = windowPos.x;
  589. pos.y = windowPos.y;
  590. ClientToScreen(getHWnd(), &pos);
  591. return Vector2I(pos.x, pos.y);
  592. }
  593. void D3D9RenderWindow::setSyncData(UINT8* buffer, UINT32 size)
  594. {
  595. assert(size == sizeof(mProperties));
  596. memcpy(&mProperties, buffer, size);
  597. }
  598. HWND D3D9RenderWindow::getHWnd() const
  599. {
  600. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  601. // could be returned here if requested too soon after initialization.
  602. return getCore()->_getWindowHandle();
  603. }
  604. SPtr<D3D9RenderWindowCore> D3D9RenderWindow::getCore() const
  605. {
  606. return std::static_pointer_cast<D3D9RenderWindowCore>(mCoreSpecific);
  607. }
  608. }