BsD3D11RenderWindow.cpp 20 KB

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