BsD3D11RenderWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. #include "BsD3D11RenderWindow.h"
  2. #include "BsCoreThread.h"
  3. #include "Win32/BsPlatformWndProc.h"
  4. #include "BsD3D11RenderAPI.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsD3D11RenderTexture.h"
  7. #include "BsD3D11TextureView.h"
  8. #include "BsTextureManager.h"
  9. #include "BsD3D11DriverList.h"
  10. #include "BsD3D11Driver.h"
  11. #include "BsD3D11VideoModeInfo.h"
  12. #include "BsRenderStats.h"
  13. #include "BsInput.h"
  14. #include "BsException.h"
  15. namespace BansheeEngine
  16. {
  17. D3D11RenderWindowProperties::D3D11RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  18. :RenderWindowProperties(desc)
  19. { }
  20. D3D11RenderWindowCore::D3D11RenderWindowCore(const RENDER_WINDOW_DESC& desc, D3D11Device& device, IDXGIFactory* DXGIFactory)
  21. : RenderWindowCore(desc), mProperties(desc), mDevice(device), mDXGIFactory(DXGIFactory), mIsExternal(false), mSizing(false),
  22. mRenderTargetView(nullptr), mBackBuffer(nullptr), mSwapChain(nullptr), mDepthStencilView(nullptr), mIsChild(false),
  23. mRefreshRateNumerator(0), mRefreshRateDenominator(0), mHWnd(0)
  24. { }
  25. D3D11RenderWindowCore::~D3D11RenderWindowCore()
  26. {
  27. D3D11RenderWindowProperties& props = mProperties;
  28. props.mActive = false;
  29. markCoreDirty();
  30. SAFE_RELEASE(mSwapChain);
  31. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_SwapChain);
  32. if (mHWnd && !mIsExternal)
  33. {
  34. DestroyWindow(mHWnd);
  35. }
  36. if (mDepthStencilView != nullptr)
  37. {
  38. TextureCore::releaseView(mDepthStencilView);
  39. mDepthStencilView = nullptr;
  40. }
  41. mHWnd = 0;
  42. destroySizeDependedD3DResources();
  43. }
  44. void D3D11RenderWindowCore::initialize()
  45. {
  46. RenderWindowCore::initialize();
  47. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  48. D3D11RenderWindowProperties& props = mProperties;
  49. mMultisampleType.Count = 1;
  50. mMultisampleType.Quality = 0;
  51. HWND parentHWnd = 0;
  52. HWND externalHandle = 0;
  53. NameValuePairList::const_iterator opt;
  54. opt = mDesc.platformSpecific.find("parentWindowHandle");
  55. if (opt != mDesc.platformSpecific.end())
  56. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  57. opt = mDesc.platformSpecific.find("externalWindowHandle");
  58. if (opt != mDesc.platformSpecific.end())
  59. externalHandle = (HWND)parseUnsignedInt(opt->second);
  60. mIsChild = parentHWnd != 0;
  61. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  62. props.mColorDepth = 32;
  63. props.mActive = true;
  64. if (mDesc.videoMode.isCustom())
  65. {
  66. mRefreshRateNumerator = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  67. mRefreshRateDenominator = 1;
  68. }
  69. else
  70. {
  71. const D3D11VideoMode& d3d11videoMode = static_cast<const D3D11VideoMode&>(mDesc.videoMode);
  72. mRefreshRateNumerator = d3d11videoMode.getRefreshRateNumerator();
  73. mRefreshRateDenominator = d3d11videoMode.getRefreshRateDenominator();
  74. }
  75. HMONITOR hMonitor = NULL;
  76. const D3D11VideoOutputInfo* outputInfo = nullptr;
  77. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  78. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  79. if (numOutputs > 0)
  80. {
  81. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  82. outputInfo = static_cast<const D3D11VideoOutputInfo*>(&videoModeInfo.getOutputInfo(actualMonitorIdx));
  83. DXGI_OUTPUT_DESC desc;
  84. outputInfo->getDXGIOutput()->GetDesc(&desc);
  85. hMonitor = desc.Monitor;
  86. }
  87. if (!externalHandle)
  88. {
  89. DWORD dwStyle = (props.isHidden() ? 0 : WS_VISIBLE) | WS_CLIPCHILDREN;
  90. DWORD dwStyleEx = 0;
  91. RECT rc;
  92. MONITORINFO monitorInfo;
  93. // If we didn't specified the adapter index, or if it didn't find it
  94. if (hMonitor == NULL)
  95. {
  96. POINT windowAnchorPoint;
  97. // Fill in anchor point.
  98. windowAnchorPoint.x = mDesc.left;
  99. windowAnchorPoint.y = mDesc.top;
  100. // Get the nearest monitor to this window.
  101. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  102. }
  103. // Get the target monitor info
  104. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  105. monitorInfo.cbSize = sizeof(MONITORINFO);
  106. GetMonitorInfo(hMonitor, &monitorInfo);
  107. unsigned int winWidth, winHeight;
  108. winWidth = mDesc.videoMode.getWidth();
  109. winHeight = mDesc.videoMode.getHeight();
  110. UINT32 left = mDesc.left;
  111. UINT32 top = mDesc.top;
  112. // No specified top left -> Center the window in the middle of the monitor
  113. if (left == -1 || top == -1)
  114. {
  115. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  116. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  117. // clamp window dimensions to screen size
  118. int outerw = (int(winWidth) < screenw) ? int(winWidth) : screenw;
  119. int outerh = (int(winHeight) < screenh) ? int(winHeight) : screenh;
  120. if (left == -1)
  121. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  122. else if (hMonitor != NULL)
  123. left += monitorInfo.rcWork.left;
  124. if (top == -1)
  125. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  126. else if (hMonitor != NULL)
  127. top += monitorInfo.rcWork.top;
  128. }
  129. else if (hMonitor != NULL)
  130. {
  131. left += monitorInfo.rcWork.left;
  132. top += monitorInfo.rcWork.top;
  133. }
  134. props.mWidth = mDesc.videoMode.getWidth();
  135. props.mHeight = mDesc.videoMode.getHeight();
  136. props.mTop = top;
  137. props.mLeft = left;
  138. if (!mDesc.fullscreen)
  139. {
  140. if (parentHWnd)
  141. {
  142. if (mDesc.toolWindow)
  143. dwStyleEx = WS_EX_TOOLWINDOW;
  144. else
  145. dwStyle |= WS_CHILD;
  146. }
  147. if (!parentHWnd || mDesc.toolWindow)
  148. {
  149. if (mDesc.border == WindowBorder::None)
  150. dwStyle |= WS_POPUP;
  151. else if (mDesc.border == WindowBorder::Fixed)
  152. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  153. WS_SYSMENU | WS_MINIMIZEBOX;
  154. else
  155. dwStyle |= WS_OVERLAPPEDWINDOW;
  156. }
  157. if (!mDesc.outerDimensions)
  158. {
  159. // Calculate window dimensions required
  160. // to get the requested client area
  161. SetRect(&rc, 0, 0, props.mWidth, props.mHeight);
  162. AdjustWindowRect(&rc, dwStyle, false);
  163. props.mWidth = rc.right - rc.left;
  164. props.mHeight = rc.bottom - rc.top;
  165. // Clamp width and height to the desktop dimensions
  166. int screenw = GetSystemMetrics(SM_CXSCREEN);
  167. int screenh = GetSystemMetrics(SM_CYSCREEN);
  168. if ((int)props.mWidth > screenw)
  169. props.mWidth = screenw;
  170. if ((int)props.mHeight > screenh)
  171. props.mHeight = screenh;
  172. if (props.mLeft < 0)
  173. props.mLeft = (screenw - props.mWidth) / 2;
  174. if (props.mTop < 0)
  175. props.mTop = (screenh - props.mHeight) / 2;
  176. }
  177. }
  178. else
  179. {
  180. dwStyle |= WS_POPUP;
  181. props.mTop = 0;
  182. props.mLeft = 0;
  183. }
  184. UINT classStyle = 0;
  185. if (mDesc.enableDoubleClick)
  186. classStyle |= CS_DBLCLKS;
  187. HINSTANCE hInst = NULL;
  188. // Register the window class
  189. // Allow 4 bytes of window data for D3D11RenderWindow pointer
  190. WNDCLASS wc = { classStyle, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  191. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  192. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D11Wnd" };
  193. RegisterClass(&wc);
  194. // Create our main window
  195. // Pass pointer to self
  196. mIsExternal = false;
  197. mHWnd = CreateWindowEx(dwStyleEx, "D3D11Wnd", mDesc.title.c_str(), dwStyle,
  198. props.mLeft, props.mTop, props.mWidth, props.mHeight, parentHWnd, 0, hInst, this);
  199. }
  200. else
  201. {
  202. mHWnd = externalHandle;
  203. mIsExternal = true;
  204. }
  205. RECT rc;
  206. GetWindowRect(mHWnd, &rc);
  207. props.mTop = rc.top;
  208. props.mLeft = rc.left;
  209. GetClientRect(mHWnd, &rc);
  210. props.mWidth = rc.right;
  211. props.mHeight = rc.bottom;
  212. createSwapChain();
  213. if (props.isFullScreen())
  214. {
  215. if (outputInfo != nullptr)
  216. mSwapChain->SetFullscreenState(true, outputInfo->getDXGIOutput());
  217. else
  218. mSwapChain->SetFullscreenState(true, nullptr);
  219. }
  220. createSizeDependedD3DResources();
  221. mDXGIFactory->MakeWindowAssociation(mHWnd, NULL);
  222. setHidden(props.isHidden());
  223. }
  224. void D3D11RenderWindowCore::swapBuffers()
  225. {
  226. THROW_IF_NOT_CORE_THREAD;
  227. if(mDevice.getD3D11Device() != nullptr)
  228. {
  229. HRESULT hr = mSwapChain->Present(getProperties().getVSync() ? getProperties().getVSyncInterval() : 0, 0);
  230. if( FAILED(hr) )
  231. BS_EXCEPT(RenderingAPIException, "Error Presenting surfaces");
  232. }
  233. }
  234. void D3D11RenderWindowCore::move(INT32 top, INT32 left)
  235. {
  236. THROW_IF_NOT_CORE_THREAD;
  237. D3D11RenderWindowProperties& props = mProperties;
  238. if (mHWnd && !props.mIsFullScreen)
  239. {
  240. props.mTop = top;
  241. props.mLeft = left;
  242. SetWindowPos(mHWnd, 0, top, left, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  243. }
  244. }
  245. void D3D11RenderWindowCore::resize(UINT32 width, UINT32 height)
  246. {
  247. THROW_IF_NOT_CORE_THREAD;
  248. D3D11RenderWindowProperties& props = mProperties;
  249. if (mHWnd && !props.mIsFullScreen)
  250. {
  251. props.mWidth = width;
  252. props.mHeight = height;
  253. RECT rc = { 0, 0, width, height };
  254. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  255. width = rc.right - rc.left;
  256. height = rc.bottom - rc.top;
  257. SetWindowPos(mHWnd, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  258. }
  259. }
  260. void D3D11RenderWindowCore::setActive(bool state)
  261. {
  262. THROW_IF_NOT_CORE_THREAD;
  263. D3D11RenderWindowProperties& props = mProperties;
  264. if (mHWnd && mSwapChain)
  265. {
  266. if (state)
  267. {
  268. ShowWindow(mHWnd, SW_RESTORE);
  269. mSwapChain->SetFullscreenState(props.mIsFullScreen, nullptr);
  270. }
  271. else
  272. {
  273. ShowWindow(mHWnd, SW_SHOWMINIMIZED);
  274. mSwapChain->SetFullscreenState(FALSE, nullptr);
  275. }
  276. }
  277. RenderWindowCore::setActive(state);
  278. }
  279. void D3D11RenderWindowCore::setHidden(bool hidden)
  280. {
  281. THROW_IF_NOT_CORE_THREAD;
  282. D3D11RenderWindowProperties& props = mProperties;
  283. props.mHidden = hidden;
  284. if (!mIsExternal)
  285. {
  286. if (hidden)
  287. ShowWindow(mHWnd, SW_HIDE);
  288. else
  289. ShowWindow(mHWnd, SW_SHOWNORMAL);
  290. }
  291. markCoreDirty();
  292. }
  293. void D3D11RenderWindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  294. {
  295. THROW_IF_NOT_CORE_THREAD;
  296. if (mIsChild)
  297. return;
  298. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  299. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  300. if (numOutputs == 0)
  301. return;
  302. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  303. const D3D11VideoOutputInfo& outputInfo = static_cast<const D3D11VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  304. DXGI_MODE_DESC modeDesc;
  305. ZeroMemory(&modeDesc, sizeof(modeDesc));
  306. modeDesc.Width = width;
  307. modeDesc.Height = height;
  308. modeDesc.RefreshRate.Numerator = Math::roundToInt(refreshRate);
  309. modeDesc.RefreshRate.Denominator = 1;
  310. modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  311. modeDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  312. modeDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  313. DXGI_MODE_DESC nearestMode;
  314. ZeroMemory(&nearestMode, sizeof(nearestMode));
  315. outputInfo.getDXGIOutput()->FindClosestMatchingMode(&modeDesc, &nearestMode, nullptr);
  316. mProperties.mIsFullScreen = true;
  317. mProperties.mWidth = width;
  318. mProperties.mHeight = height;
  319. mSwapChain->ResizeTarget(&nearestMode);
  320. mSwapChain->SetFullscreenState(true, outputInfo.getDXGIOutput());
  321. markCoreDirty();
  322. }
  323. void D3D11RenderWindowCore::setFullscreen(const VideoMode& mode)
  324. {
  325. THROW_IF_NOT_CORE_THREAD;
  326. if (mIsChild)
  327. return;
  328. if (mode.isCustom())
  329. {
  330. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  331. return;
  332. }
  333. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderAPICore::instance().getVideoModeInfo());
  334. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  335. if (numOutputs == 0)
  336. return;
  337. UINT32 actualMonitorIdx = std::min(mode.getOutputIdx(), numOutputs - 1);
  338. const D3D11VideoOutputInfo& outputInfo = static_cast<const D3D11VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  339. const D3D11VideoMode& videoMode = static_cast<const D3D11VideoMode&>(mode);
  340. mProperties.mIsFullScreen = true;
  341. mProperties.mWidth = mode.getWidth();
  342. mProperties.mHeight = mode.getHeight();
  343. mSwapChain->ResizeTarget(&videoMode.getDXGIModeDesc());
  344. mSwapChain->SetFullscreenState(true, outputInfo.getDXGIOutput());
  345. markCoreDirty();
  346. }
  347. void D3D11RenderWindowCore::setWindowed(UINT32 width, UINT32 height)
  348. {
  349. THROW_IF_NOT_CORE_THREAD;
  350. mProperties.mWidth = width;
  351. mProperties.mHeight = height;
  352. mProperties.mIsFullScreen = false;
  353. mSwapChainDesc.Windowed = true;
  354. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  355. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  356. mSwapChainDesc.BufferDesc.Width = width;
  357. mSwapChainDesc.BufferDesc.Height = height;
  358. DXGI_MODE_DESC modeDesc;
  359. ZeroMemory(&modeDesc, sizeof(modeDesc));
  360. modeDesc.Width = width;
  361. modeDesc.Height = height;
  362. modeDesc.RefreshRate.Numerator = 0;
  363. modeDesc.RefreshRate.Denominator = 0;
  364. modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  365. mSwapChain->SetFullscreenState(false, nullptr);
  366. mSwapChain->ResizeTarget(&modeDesc);
  367. markCoreDirty();
  368. }
  369. HWND D3D11RenderWindowCore::_getWindowHandle() const
  370. {
  371. return mHWnd;
  372. }
  373. void D3D11RenderWindowCore::getCustomAttribute(const String& name, void* pData) const
  374. {
  375. if(name == "WINDOW")
  376. {
  377. UINT64 *pWnd = (UINT64*)pData;
  378. *pWnd = (UINT64)mHWnd;
  379. return;
  380. }
  381. if(name == "RTV")
  382. {
  383. *static_cast<ID3D11RenderTargetView**>(pData) = mRenderTargetView;
  384. return;
  385. }
  386. else if(name == "DSV")
  387. {
  388. D3D11TextureView* d3d11TextureView = static_cast<D3D11TextureView*>(mDepthStencilView.get());
  389. *static_cast<ID3D11DepthStencilView**>(pData) = d3d11TextureView->getDSV();
  390. return;
  391. }
  392. RenderWindowCore::getCustomAttribute(name, pData);
  393. }
  394. void D3D11RenderWindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  395. {
  396. THROW_IF_NOT_CORE_THREAD;
  397. if(mBackBuffer == nullptr)
  398. return;
  399. // Get the backbuffer desc
  400. D3D11_TEXTURE2D_DESC BBDesc;
  401. mBackBuffer->GetDesc(&BBDesc);
  402. ID3D11Texture2D* backbuffer = nullptr;
  403. if(BBDesc.SampleDesc.Quality > 0)
  404. {
  405. D3D11_TEXTURE2D_DESC desc = BBDesc;
  406. desc.Usage = D3D11_USAGE_DEFAULT;
  407. desc.CPUAccessFlags = 0;
  408. desc.BindFlags = 0;
  409. desc.SampleDesc.Quality = 0;
  410. desc.SampleDesc.Count = 1;
  411. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(&desc, nullptr, &backbuffer);
  412. if (FAILED(hr) || mDevice.hasError())
  413. {
  414. String errorDescription = mDevice.getErrorDescription();
  415. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  416. }
  417. mDevice.getImmediateContext()->ResolveSubresource(backbuffer, D3D11CalcSubresource(0, 0, 1), mBackBuffer, D3D11CalcSubresource(0, 0, 1), desc.Format);
  418. }
  419. // Change the parameters of the texture so we can read it
  420. BBDesc.Usage = D3D11_USAGE_STAGING;
  421. BBDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  422. BBDesc.BindFlags = 0;
  423. BBDesc.SampleDesc.Quality = 0;
  424. BBDesc.SampleDesc.Count = 1;
  425. // Create a temp buffer to copy to
  426. ID3D11Texture2D* tempTexture;
  427. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(&BBDesc, nullptr, &tempTexture);
  428. if (FAILED(hr) || mDevice.hasError())
  429. {
  430. String errorDescription = mDevice.getErrorDescription();
  431. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  432. }
  433. // Copy the back buffer
  434. mDevice.getImmediateContext()->CopyResource(tempTexture, backbuffer != NULL ? backbuffer : mBackBuffer);
  435. // Map the copied texture
  436. D3D11_MAPPED_SUBRESOURCE mappedTex2D;
  437. mDevice.getImmediateContext()->Map(tempTexture, 0,D3D11_MAP_READ, 0, &mappedTex2D);
  438. // Copy the the texture to the dest
  439. PixelData src(getProperties().getWidth(), getProperties().getHeight(), 1, PF_A8B8G8R8);
  440. src.setExternalBuffer((UINT8*)mappedTex2D.pData);
  441. PixelUtil::bulkPixelConversion(src, dst);
  442. // Unmap the temp buffer
  443. mDevice.getImmediateContext()->Unmap(tempTexture, 0);
  444. // Release the temp buffer
  445. SAFE_RELEASE(tempTexture);
  446. SAFE_RELEASE(backbuffer);
  447. }
  448. void D3D11RenderWindowCore::_windowMovedOrResized()
  449. {
  450. THROW_IF_NOT_CORE_THREAD;
  451. D3D11RenderWindowProperties& props = mProperties;
  452. if (!mHWnd || IsIconic(mHWnd))
  453. return;
  454. RECT rc;
  455. GetWindowRect(mHWnd, &rc);
  456. mProperties.mTop = rc.top;
  457. mProperties.mLeft = rc.left;
  458. markCoreDirty();
  459. GetClientRect(mHWnd, &rc);
  460. unsigned int width = rc.right - rc.left;
  461. unsigned int height = rc.bottom - rc.top;
  462. if (width == 0)
  463. width = 1;
  464. if (height == 0)
  465. height = 1;
  466. resizeSwapChainBuffers(width, height);
  467. RenderWindowCore::_windowMovedOrResized();
  468. }
  469. void D3D11RenderWindowCore::createSwapChain()
  470. {
  471. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  472. D3D11RenderWindowProperties& props = mProperties;
  473. IDXGIDevice* pDXGIDevice = queryDxgiDevice();
  474. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  475. DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  476. mSwapChainDesc.OutputWindow = mHWnd;
  477. mSwapChainDesc.BufferDesc.Width = props.mWidth;
  478. mSwapChainDesc.BufferDesc.Height = props.mHeight;
  479. mSwapChainDesc.BufferDesc.Format = format;
  480. if (props.mIsFullScreen)
  481. {
  482. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = mRefreshRateNumerator;
  483. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = mRefreshRateDenominator;
  484. }
  485. else
  486. {
  487. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  488. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  489. }
  490. mSwapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  491. mSwapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  492. mSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH ;
  493. mSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  494. mSwapChainDesc.BufferCount = 1;
  495. mSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
  496. mSwapChainDesc.Windowed = true;
  497. D3D11RenderAPI* rs = static_cast<D3D11RenderAPI*>(RenderAPICore::instancePtr());
  498. rs->determineMultisampleSettings(props.mMultisampleCount, format, &mMultisampleType);
  499. mSwapChainDesc.SampleDesc.Count = mMultisampleType.Count;
  500. mSwapChainDesc.SampleDesc.Quality = mMultisampleType.Quality;
  501. HRESULT hr;
  502. // Create swap chain
  503. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  504. if (FAILED(hr))
  505. {
  506. // Try a second time, may fail the first time due to back buffer count,
  507. // which will be corrected by the runtime
  508. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  509. }
  510. SAFE_RELEASE(pDXGIDevice);
  511. if (FAILED(hr))
  512. BS_EXCEPT(RenderingAPIException, "Unable to create swap chain");
  513. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_SwapChain);
  514. }
  515. void D3D11RenderWindowCore::createSizeDependedD3DResources()
  516. {
  517. SAFE_RELEASE(mBackBuffer);
  518. HRESULT hr = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBuffer);
  519. if(FAILED(hr))
  520. BS_EXCEPT(RenderingAPIException, "Unable to Get Back Buffer for swap chain");
  521. assert(mBackBuffer && !mRenderTargetView);
  522. D3D11_TEXTURE2D_DESC BBDesc;
  523. mBackBuffer->GetDesc(&BBDesc);
  524. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  525. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  526. RTVDesc.Format = BBDesc.Format;
  527. RTVDesc.ViewDimension = getProperties().getMultisampleCount() ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  528. RTVDesc.Texture2D.MipSlice = 0;
  529. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  530. if(FAILED(hr))
  531. {
  532. String errorDescription = mDevice.getErrorDescription();
  533. BS_EXCEPT(RenderingAPIException, "Unable to create rendertagert view\nError Description:" + errorDescription);
  534. }
  535. mDepthStencilBuffer = TextureCoreManager::instance().createTexture(TEX_TYPE_2D,
  536. BBDesc.Width, BBDesc.Height, 0, 0, PF_D24S8, TU_DEPTHSTENCIL, false,
  537. getProperties().getMultisampleCount());
  538. if(mDepthStencilView != nullptr)
  539. {
  540. TextureCore::releaseView(mDepthStencilView);
  541. mDepthStencilView = nullptr;
  542. }
  543. mDepthStencilView = TextureCore::requestView(mDepthStencilBuffer, 0, 1, 0, 1, GVU_DEPTHSTENCIL);
  544. }
  545. void D3D11RenderWindowCore::destroySizeDependedD3DResources()
  546. {
  547. SAFE_RELEASE(mBackBuffer);
  548. SAFE_RELEASE(mRenderTargetView);
  549. mDepthStencilBuffer = nullptr;
  550. }
  551. void D3D11RenderWindowCore::resizeSwapChainBuffers(UINT32 width, UINT32 height)
  552. {
  553. destroySizeDependedD3DResources();
  554. UINT Flags = mProperties.isFullScreen() ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0;
  555. HRESULT hr = mSwapChain->ResizeBuffers(mSwapChainDesc.BufferCount, width, height, mSwapChainDesc.BufferDesc.Format, Flags);
  556. if(hr != S_OK)
  557. BS_EXCEPT(InternalErrorException, "Call to ResizeBuffers failed.");
  558. mSwapChain->GetDesc(&mSwapChainDesc);
  559. mProperties.mWidth = mSwapChainDesc.BufferDesc.Width;
  560. mProperties.mHeight = mSwapChainDesc.BufferDesc.Height;
  561. mProperties.mIsFullScreen = (0 == mSwapChainDesc.Windowed); // Alt-Enter together with SetWindowAssociation() can change this state
  562. markCoreDirty();
  563. createSizeDependedD3DResources();
  564. mDevice.getImmediateContext()->OMSetRenderTargets(0, 0, 0);
  565. }
  566. IDXGIDevice* D3D11RenderWindowCore::queryDxgiDevice()
  567. {
  568. if (mDevice.getD3D11Device() == nullptr)
  569. {
  570. BS_EXCEPT(RenderingAPIException, "D3D11Device is null.");
  571. }
  572. IDXGIDevice* pDXGIDevice = nullptr;
  573. HRESULT hr = mDevice.getD3D11Device()->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
  574. if(FAILED(hr))
  575. BS_EXCEPT(RenderingAPIException, "Unable to query a DXGIDevice.");
  576. return pDXGIDevice;
  577. }
  578. D3D11RenderWindow::D3D11RenderWindow(const RENDER_WINDOW_DESC& desc, D3D11Device& device, IDXGIFactory* DXGIFactory)
  579. :RenderWindow(desc), mProperties(desc), mDevice(device), mDXGIFactory(DXGIFactory)
  580. {
  581. }
  582. void D3D11RenderWindow::getCustomAttribute(const String& name, void* pData) const
  583. {
  584. if (name == "WINDOW")
  585. {
  586. UINT64 *pHwnd = (UINT64*)pData;
  587. *pHwnd = (UINT64)getHWnd();
  588. return;
  589. }
  590. }
  591. Vector2I D3D11RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  592. {
  593. POINT pos;
  594. pos.x = screenPos.x;
  595. pos.y = screenPos.y;
  596. ScreenToClient(getHWnd(), &pos);
  597. return Vector2I(pos.x, pos.y);
  598. }
  599. Vector2I D3D11RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  600. {
  601. POINT pos;
  602. pos.x = windowPos.x;
  603. pos.y = windowPos.y;
  604. ClientToScreen(getHWnd(), &pos);
  605. return Vector2I(pos.x, pos.y);
  606. }
  607. SPtr<D3D11RenderWindowCore> D3D11RenderWindow::getCore() const
  608. {
  609. return std::static_pointer_cast<D3D11RenderWindowCore>(mCoreSpecific);
  610. }
  611. HWND D3D11RenderWindow::getHWnd() const
  612. {
  613. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  614. // could be returned here if requested too soon after initialization.
  615. return getCore()->_getWindowHandle();
  616. }
  617. }