CmD3D11RenderWindow.cpp 21 KB

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