BsD3D11RenderWindow.cpp 20 KB

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