BsD3D11RenderWindow.cpp 21 KB

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