CmD3D11RenderWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. #include "CmD3D11RenderWindow.h"
  2. #include "CmWindowEventUtilities.h"
  3. #include "CmD3D11RenderSystem.h"
  4. #include "CmD3D11Device.h"
  5. #include "CmDepthStencilBuffer.h"
  6. #include "CmTextureManager.h"
  7. #include "CmException.h"
  8. namespace CamelotEngine
  9. {
  10. D3D11RenderWindow::D3D11RenderWindow(D3D11Device& device, IDXGIFactory* DXGIFactory)
  11. : RenderWindow()
  12. , mDevice(device)
  13. , mDXGIFactory(DXGIFactory)
  14. , mIsExternal(false)
  15. , mSizing(false)
  16. , mClosed(false)
  17. , mHidden(false)
  18. , mSwitchingFullscreen(false)
  19. , mDisplayFrequency(0)
  20. , mRenderTargetView(nullptr)
  21. , mBackBuffer(nullptr)
  22. , mSwapChain(nullptr)
  23. , mHWnd(0)
  24. {
  25. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  26. }
  27. D3D11RenderWindow::~D3D11RenderWindow()
  28. {
  29. destroy();
  30. }
  31. void D3D11RenderWindow::initialize(const String& name, unsigned int width, unsigned int height,
  32. bool fullScreen, const NameValuePairList *miscParams)
  33. {
  34. mFSAAType.Count = 1;
  35. mFSAAType.Quality = 0;
  36. mFSAA = 0;
  37. mFSAAHint = "";
  38. mVSync = false;
  39. mVSyncInterval = 1;
  40. HWND parentHWnd = 0;
  41. HWND externalHandle = 0;
  42. String title = name;
  43. int left = -1; // Defaults to screen center
  44. int top = -1; // Defaults to screen center
  45. String border = "";
  46. bool outerSize = false;
  47. bool enableDoubleClick = false;
  48. unsigned int colourDepth = 32;
  49. bool depthBuffer = true;
  50. if(miscParams)
  51. {
  52. // Get variable-length params
  53. NameValuePairList::const_iterator opt;
  54. // vsync [parseBool]
  55. opt = miscParams->find("vsync");
  56. if(opt != miscParams->end())
  57. mVSync = parseBool(opt->second);
  58. // vsyncInterval [parseUnsignedInt]
  59. opt = miscParams->find("vsyncInterval");
  60. if(opt != miscParams->end())
  61. mVSyncInterval = parseUnsignedInt(opt->second);
  62. // hidden [parseBool]
  63. opt = miscParams->find("hidden");
  64. if(opt != miscParams->end())
  65. mHidden = parseBool(opt->second);
  66. // displayFrequency
  67. opt = miscParams->find("displayFrequency");
  68. if(opt != miscParams->end())
  69. mDisplayFrequency = parseUnsignedInt(opt->second);
  70. // colourDepth
  71. opt = miscParams->find("colourDepth");
  72. if(opt != miscParams->end())
  73. colourDepth = parseUnsignedInt(opt->second);
  74. // depthBuffer [parseBool]
  75. opt = miscParams->find("depthBuffer");
  76. if(opt != miscParams->end())
  77. depthBuffer = parseBool(opt->second);
  78. // FSAA type
  79. opt = miscParams->find("FSAA");
  80. if(opt != miscParams->end())
  81. mFSAA = parseUnsignedInt(opt->second);
  82. // FSAA quality
  83. opt = miscParams->find("FSAAHint");
  84. if(opt != miscParams->end())
  85. mFSAAHint = opt->second;
  86. // sRGB?
  87. opt = miscParams->find("gamma");
  88. if(opt != miscParams->end())
  89. mHwGamma = parseBool(opt->second);
  90. // left (x)
  91. opt = miscParams->find("left");
  92. if(opt != miscParams->end())
  93. left = parseInt(opt->second);
  94. // top (y)
  95. opt = miscParams->find("top");
  96. if(opt != miscParams->end())
  97. top = parseInt(opt->second);
  98. // Window title
  99. opt = miscParams->find("title");
  100. if(opt != miscParams->end())
  101. title = opt->second;
  102. // parentWindowHandle -> parentHWnd
  103. opt = miscParams->find("parentWindowHandle");
  104. if(opt != miscParams->end())
  105. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  106. // externalWindowHandle -> externalHandle
  107. opt = miscParams->find("externalWindowHandle");
  108. if(opt != miscParams->end())
  109. externalHandle = (HWND)parseUnsignedInt(opt->second);
  110. // window border style
  111. opt = miscParams->find("border");
  112. if(opt != miscParams->end())
  113. border = opt->second;
  114. // set outer dimensions?
  115. opt = miscParams->find("outerDimensions");
  116. if(opt != miscParams->end())
  117. outerSize = parseBool(opt->second);
  118. // enable double click messages
  119. opt = miscParams->find("enableDoubleClick");
  120. if(opt != miscParams->end())
  121. enableDoubleClick = parseBool(opt->second);
  122. }
  123. mName = name;
  124. mIsFullScreen = fullScreen;
  125. mColorDepth = colourDepth;
  126. mWidth = mHeight = mLeft = mTop = 0;
  127. mActive = true;
  128. mClosed = false;
  129. // Destroy current window if any
  130. if(mHWnd)
  131. destroy();
  132. if (!externalHandle)
  133. {
  134. DWORD dwStyle = (mHidden ? 0 : WS_VISIBLE) | WS_CLIPCHILDREN;
  135. RECT rc;
  136. mWidth = width;
  137. mHeight = height;
  138. mTop = top;
  139. mLeft = left;
  140. if (!fullScreen)
  141. {
  142. if (parentHWnd)
  143. {
  144. dwStyle |= WS_CHILD;
  145. }
  146. else
  147. {
  148. if (border == "none")
  149. dwStyle |= WS_POPUP;
  150. else if (border == "fixed")
  151. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  152. WS_SYSMENU | WS_MINIMIZEBOX;
  153. else
  154. dwStyle |= WS_OVERLAPPEDWINDOW;
  155. }
  156. if (!outerSize)
  157. {
  158. // Calculate window dimensions required
  159. // to get the requested client area
  160. SetRect(&rc, 0, 0, mWidth, mHeight);
  161. AdjustWindowRect(&rc, dwStyle, false);
  162. mWidth = rc.right - rc.left;
  163. mHeight = rc.bottom - rc.top;
  164. // Clamp width and height to the desktop dimensions
  165. int screenw = GetSystemMetrics(SM_CXSCREEN);
  166. int screenh = GetSystemMetrics(SM_CYSCREEN);
  167. if ((int)mWidth > screenw)
  168. mWidth = screenw;
  169. if ((int)mHeight > screenh)
  170. mHeight = screenh;
  171. if (mLeft < 0)
  172. mLeft = (screenw - mWidth) / 2;
  173. if (mTop < 0)
  174. mTop = (screenh - mHeight) / 2;
  175. }
  176. }
  177. else
  178. {
  179. dwStyle |= WS_POPUP;
  180. mTop = mLeft = 0;
  181. }
  182. UINT classStyle = 0;
  183. if (enableDoubleClick)
  184. classStyle |= CS_DBLCLKS;
  185. HINSTANCE hInst = NULL;
  186. // Register the window class
  187. // Allow 4 bytes of window data for D3D11RenderWindow pointer
  188. WNDCLASS wc = { classStyle, WindowEventUtilities::_WndProc, 0, 0, hInst,
  189. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  190. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D11Wnd" };
  191. RegisterClass(&wc);
  192. // Create our main window
  193. // Pass pointer to self
  194. mIsExternal = false;
  195. mHWnd = CreateWindow("D3D11Wnd", title.c_str(), dwStyle,
  196. mLeft, mTop, mWidth, mHeight, parentHWnd, 0, hInst, this);
  197. WindowEventUtilities::_addRenderWindow(this);
  198. }
  199. else
  200. {
  201. mHWnd = externalHandle;
  202. mIsExternal = true;
  203. }
  204. RECT rc;
  205. // top and left represent outer window coordinates
  206. GetWindowRect(mHWnd, &rc);
  207. mTop = rc.top;
  208. mLeft = rc.left;
  209. // width and height represent interior drawable area
  210. GetClientRect(mHWnd, &rc);
  211. mWidth = rc.right;
  212. mHeight = rc.bottom;
  213. _createSwapChain();
  214. _createSizeDependedD3DResources();
  215. mDXGIFactory->MakeWindowAssociation(mHWnd, NULL);
  216. setHidden(mHidden);
  217. }
  218. void D3D11RenderWindow::destroy()
  219. {
  220. _destroySizeDependedD3DResources();
  221. mActive = false;
  222. mClosed = true;
  223. SAFE_RELEASE(mSwapChain);
  224. if (mHWnd && !mIsExternal)
  225. {
  226. WindowEventUtilities::_removeRenderWindow(this);
  227. DestroyWindow(mHWnd);
  228. }
  229. mHWnd = nullptr;
  230. }
  231. void D3D11RenderWindow::swapBuffers(bool waitForVSync)
  232. {
  233. if(mDevice.getD3D11Device() != nullptr)
  234. {
  235. HRESULT hr = mSwapChain->Present(waitForVSync ? mVSyncInterval : 0, 0);
  236. if( FAILED(hr) )
  237. CM_EXCEPT(RenderingAPIException, "Error Presenting surfaces");
  238. }
  239. }
  240. void D3D11RenderWindow::reposition(int top, int left)
  241. {
  242. if (mHWnd && !mIsFullScreen)
  243. {
  244. SetWindowPos(mHWnd, 0, top, left, 0, 0,
  245. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  246. }
  247. }
  248. void D3D11RenderWindow::resize(unsigned int width, unsigned int height)
  249. {
  250. if (mHWnd && !mIsFullScreen)
  251. {
  252. RECT rc = { 0, 0, width, height };
  253. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  254. width = rc.right - rc.left;
  255. height = rc.bottom - rc.top;
  256. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  257. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  258. }
  259. }
  260. void D3D11RenderWindow::windowMovedOrResized()
  261. {
  262. if (!mHWnd || IsIconic(mHWnd))
  263. return;
  264. RECT rc;
  265. // top and left represent outer window position
  266. GetWindowRect(mHWnd, &rc);
  267. mTop = rc.top;
  268. mLeft = rc.left;
  269. // width and height represent drawable area only
  270. GetClientRect(mHWnd, &rc);
  271. unsigned int width = rc.right - rc.left;
  272. unsigned int height = rc.bottom - rc.top;
  273. if (width == 0)
  274. width = 1;
  275. if (height == 0)
  276. height = 1;
  277. if (mWidth == width && mHeight == height)
  278. return;
  279. _resizeSwapChainBuffers(width, height);
  280. }
  281. void D3D11RenderWindow::setActive(bool state)
  282. {
  283. if (mHWnd && mSwapChain)
  284. {
  285. if (state)
  286. {
  287. ShowWindow(mHWnd, SW_RESTORE);
  288. mSwapChain->SetFullscreenState(mIsFullScreen, nullptr);
  289. }
  290. else
  291. {
  292. ShowWindow(mHWnd, SW_SHOWMINIMIZED);
  293. mSwapChain->SetFullscreenState(FALSE, nullptr);
  294. }
  295. }
  296. RenderWindow::setActive(state);
  297. }
  298. void D3D11RenderWindow::setHidden(bool hidden)
  299. {
  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(bool fullScreen, unsigned int width, unsigned int height)
  310. {
  311. if (fullScreen != mIsFullScreen || width != mWidth || height != mHeight)
  312. {
  313. if (fullScreen != mIsFullScreen)
  314. mSwitchingFullscreen = true;
  315. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  316. bool oldFullscreen = mIsFullScreen;
  317. mIsFullScreen = fullScreen;
  318. if (fullScreen)
  319. {
  320. dwStyle |= WS_POPUP;
  321. mTop = mLeft = 0;
  322. mWidth = width;
  323. mHeight = height;
  324. // need different ordering here
  325. if (oldFullscreen)
  326. {
  327. // was previously fullscreen, just changing the resolution
  328. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, width, height, SWP_NOACTIVATE);
  329. }
  330. else
  331. {
  332. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, width, height, SWP_NOACTIVATE);
  333. //MoveWindow(mHWnd, mLeft, mTop, mWidth, mHeight, FALSE);
  334. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  335. SetWindowPos(mHWnd, 0, 0,0, 0,0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
  336. }
  337. }
  338. else
  339. {
  340. dwStyle |= WS_OVERLAPPEDWINDOW;
  341. // Calculate window dimensions required
  342. // to get the requested client area
  343. RECT rc;
  344. SetRect(&rc, 0, 0, width, height);
  345. AdjustWindowRect(&rc, dwStyle, false);
  346. unsigned int winWidth = rc.right - rc.left;
  347. unsigned int winHeight = rc.bottom - rc.top;
  348. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  349. SetWindowPos(mHWnd, HWND_NOTOPMOST, 0, 0, winWidth, winHeight,
  350. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE);
  351. // Note that we also set the position in the restoreLostDevice method
  352. // via _finishSwitchingFullScreen
  353. }
  354. mSwapChainDesc.Windowed = !fullScreen;
  355. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  356. mSwapChainDesc.BufferDesc.RefreshRate.Denominator=0;
  357. mSwapChainDesc.BufferDesc.Height = height;
  358. mSwapChainDesc.BufferDesc.Width = width;
  359. }
  360. }
  361. void D3D11RenderWindow::getCustomAttribute( const String& name, void* pData )
  362. {
  363. // Valid attributes and their equvalent native functions:
  364. // D3DDEVICE : getD3DDevice
  365. // WINDOW : getWindowHandle
  366. if(name == "WINDOW")
  367. {
  368. HWND *pWnd = (HWND*)pData;
  369. *pWnd = mHWnd;
  370. return;
  371. }
  372. if(name == "D3DDEVICE")
  373. {
  374. ID3D11Device **device = (ID3D11Device **)pData;
  375. *device = mDevice.getD3D11Device();
  376. return;
  377. }
  378. else if(name == "isTexture")
  379. {
  380. bool *b = reinterpret_cast< bool * >( pData );
  381. *b = false;
  382. return;
  383. }
  384. else if(name == "ID3D11RenderTargetView")
  385. {
  386. *static_cast<ID3D11RenderTargetView**>(pData) = mRenderTargetView;
  387. return;
  388. }
  389. else if(name == "ID3D11Texture2D")
  390. {
  391. ID3D11Texture2D **pBackBuffer = (ID3D11Texture2D**)pData;
  392. *pBackBuffer = mBackBuffer;
  393. return;
  394. }
  395. else if(name == "numberOfViews")
  396. {
  397. unsigned int* n = reinterpret_cast<unsigned int*>(pData);
  398. *n = 1;
  399. return;
  400. }
  401. else if(name == "DDBACKBUFFER")
  402. {
  403. ID3D11Texture2D **ppBackBuffer = (ID3D11Texture2D**) pData;
  404. ppBackBuffer[0] = NULL;
  405. return;
  406. }
  407. RenderWindow::getCustomAttribute_internal(name, pData);
  408. }
  409. void D3D11RenderWindow::copyContentsToMemory(const PixelData &dst, FrameBuffer buffer)
  410. {
  411. if(mBackBuffer == nullptr)
  412. return;
  413. // get the backbuffer desc
  414. D3D11_TEXTURE2D_DESC BBDesc;
  415. mBackBuffer->GetDesc(&BBDesc);
  416. ID3D11Texture2D *backbuffer = nullptr;
  417. if(BBDesc.SampleDesc.Quality > 0)
  418. {
  419. D3D11_TEXTURE2D_DESC desc = BBDesc;
  420. desc.Usage = D3D11_USAGE_DEFAULT;
  421. desc.CPUAccessFlags = 0;
  422. desc.BindFlags = 0;
  423. desc.SampleDesc.Quality = 0;
  424. desc.SampleDesc.Count = 1;
  425. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(
  426. &desc,
  427. NULL,
  428. &backbuffer);
  429. if (FAILED(hr) || mDevice.hasError())
  430. {
  431. String errorDescription = mDevice.getErrorDescription();
  432. CM_EXCEPT(RenderingAPIException,
  433. "Error creating texture\nError Description:" + errorDescription);
  434. }
  435. mDevice.getImmediateContext()->ResolveSubresource(backbuffer, D3D11CalcSubresource(0, 0, 1), mBackBuffer, D3D11CalcSubresource(0, 0, 1), desc.Format);
  436. }
  437. // change the parameters of the texture so we can read it
  438. BBDesc.Usage = D3D11_USAGE_STAGING;
  439. BBDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  440. BBDesc.BindFlags = 0;
  441. BBDesc.SampleDesc.Quality = 0;
  442. BBDesc.SampleDesc.Count = 1;
  443. // create a temp buffer to copy to
  444. ID3D11Texture2D * pTempTexture2D;
  445. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(
  446. &BBDesc,
  447. NULL,
  448. &pTempTexture2D);
  449. if (FAILED(hr) || mDevice.hasError())
  450. {
  451. String errorDescription = mDevice.getErrorDescription();
  452. CM_EXCEPT(RenderingAPIException,
  453. "Error creating texture\nError Description:" + errorDescription);
  454. }
  455. // copy the back buffer
  456. mDevice.getImmediateContext()->CopyResource(pTempTexture2D, backbuffer != NULL ? backbuffer : mBackBuffer);
  457. // map the copied texture
  458. D3D11_MAPPED_SUBRESOURCE mappedTex2D;
  459. mDevice.getImmediateContext()->Map(pTempTexture2D, 0,D3D11_MAP_READ, 0, &mappedTex2D);
  460. // copy the the texture to the dest
  461. PixelUtil::bulkPixelConversion(PixelData(mWidth, mHeight, 1, PF_A8B8G8R8, mappedTex2D.pData), dst);
  462. // unmap the temp buffer
  463. mDevice.getImmediateContext()->Unmap(pTempTexture2D, 0);
  464. // Release the temp buffer
  465. SAFE_RELEASE(pTempTexture2D);
  466. SAFE_RELEASE(backbuffer);
  467. }
  468. void D3D11RenderWindow::_createSwapChain()
  469. {
  470. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  471. // get the dxgi device
  472. IDXGIDevice* pDXGIDevice = _queryDxgiDevice();
  473. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  474. DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  475. mSwapChainDesc.OutputWindow = mHWnd;
  476. mSwapChainDesc.BufferDesc.Width = mWidth;
  477. mSwapChainDesc.BufferDesc.Height = mHeight;
  478. mSwapChainDesc.BufferDesc.Format = format;
  479. mSwapChainDesc.BufferDesc.RefreshRate.Numerator=0;
  480. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  481. mSwapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  482. mSwapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  483. mSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH ;
  484. // triple buffer if VSync is on
  485. mSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  486. mSwapChainDesc.BufferCount = mVSync ? 2 : 1;
  487. mSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
  488. mSwapChainDesc.OutputWindow = mHWnd;
  489. mSwapChainDesc.Windowed = !mIsFullScreen;
  490. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  491. rs->determineFSAASettings(mFSAA, mFSAAHint, format, &mFSAAType);
  492. mSwapChainDesc.SampleDesc.Count = mFSAAType.Count;
  493. mSwapChainDesc.SampleDesc.Quality = mFSAAType.Quality;
  494. HRESULT hr;
  495. // Create swap chain
  496. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  497. if (FAILED(hr))
  498. {
  499. // Try a second time, may fail the first time due to back buffer count,
  500. // which will be corrected by the runtime
  501. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  502. }
  503. SAFE_RELEASE(pDXGIDevice);
  504. if (FAILED(hr))
  505. CM_EXCEPT(RenderingAPIException, "Unable to create swap chain");
  506. }
  507. void D3D11RenderWindow::_createSizeDependedD3DResources()
  508. {
  509. // obtain back buffer
  510. SAFE_RELEASE(mBackBuffer);
  511. HRESULT hr = mSwapChain->GetBuffer(0, __uuidof( ID3D11Texture2D ), (LPVOID*)&mBackBuffer);
  512. if( FAILED(hr) )
  513. CM_EXCEPT(RenderingAPIException, "Unable to Get Back Buffer for swap chain");
  514. // create all other size depended resources
  515. assert(mBackBuffer && !mRenderTargetView);
  516. // get the backbuffer desc
  517. D3D11_TEXTURE2D_DESC BBDesc;
  518. mBackBuffer->GetDesc(&BBDesc);
  519. // create the render target view
  520. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  521. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  522. RTVDesc.Format = BBDesc.Format;
  523. RTVDesc.ViewDimension = mFSAA ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  524. RTVDesc.Texture2D.MipSlice = 0;
  525. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  526. if( FAILED(hr) )
  527. {
  528. String errorDescription = mDevice.getErrorDescription();
  529. CM_EXCEPT(RenderingAPIException, "Unable to create rendertagert view\nError Description:" + errorDescription);
  530. }
  531. mDepthStencilBuffer = TextureManager::instance().createDepthStencilBuffer(32, BBDesc.Width, BBDesc.Height, mFSAA, mFSAAHint);
  532. }
  533. void D3D11RenderWindow::_destroySizeDependedD3DResources()
  534. {
  535. SAFE_RELEASE(mBackBuffer);
  536. SAFE_RELEASE(mRenderTargetView);
  537. mDepthStencilBuffer = nullptr;
  538. }
  539. void D3D11RenderWindow::_resizeSwapChainBuffers(unsigned width, unsigned height)
  540. {
  541. _destroySizeDependedD3DResources();
  542. // width and height can be zero to autodetect size, therefore do not rely on them
  543. UINT Flags = mIsFullScreen ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0;
  544. mSwapChain->ResizeBuffers(mSwapChainDesc.BufferCount, width, height, mSwapChainDesc.BufferDesc.Format, Flags);
  545. mSwapChain->GetDesc(&mSwapChainDesc);
  546. mWidth = mSwapChainDesc.BufferDesc.Width;
  547. mHeight = mSwapChainDesc.BufferDesc.Height;
  548. mIsFullScreen = (0 == mSwapChainDesc.Windowed); // Alt-Enter together with SetWindowAssociation() can change this state
  549. _createSizeDependedD3DResources();
  550. mDevice.getImmediateContext()->OMSetRenderTargets(0, 0, 0);
  551. // Additional swap chains need their own depth buffer
  552. // to support resizing them
  553. HRESULT hr = mSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&mBackBuffer );
  554. if( FAILED(hr) )
  555. {
  556. CM_EXCEPT(RenderingAPIException,
  557. "Unable to Get Back Buffer for swap chain");
  558. }
  559. // get the backbuffer desc
  560. D3D11_TEXTURE2D_DESC BBDesc;
  561. mBackBuffer->GetDesc(&BBDesc);
  562. // create the render target view
  563. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  564. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  565. RTVDesc.Format = BBDesc.Format;
  566. RTVDesc.ViewDimension = mFSAA ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  567. RTVDesc.Texture2D.MipSlice = 0;
  568. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  569. if(FAILED(hr))
  570. {
  571. String errorDescription = mDevice.getErrorDescription();
  572. CM_EXCEPT(RenderingAPIException,
  573. "Unable to create rendertagert view\nError Description:" + errorDescription);
  574. }
  575. }
  576. IDXGIDevice* D3D11RenderWindow::_queryDxgiDevice()
  577. {
  578. if (mDevice.getD3D11Device() == nullptr)
  579. {
  580. CM_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. CM_EXCEPT(RenderingAPIException, "Unable to query a DXGIDevice");
  586. return pDXGIDevice;
  587. }
  588. void D3D11RenderWindow::_finishSwitchingFullscreen()
  589. {
  590. if(mIsFullScreen)
  591. {
  592. // Need to reset the region on the window sometimes, when the
  593. // windowed mode was constrained by desktop
  594. HRGN hRgn = CreateRectRgn(0, 0, mSwapChainDesc.BufferDesc.Width, mSwapChainDesc.BufferDesc.Height);
  595. SetWindowRgn(mHWnd, hRgn, FALSE);
  596. }
  597. else
  598. {
  599. // When switching back to windowed mode, need to reset window size
  600. // after device has been restored
  601. RECT rc;
  602. SetRect(&rc, 0, 0, mSwapChainDesc.BufferDesc.Width, mSwapChainDesc.BufferDesc.Height);
  603. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  604. unsigned int winWidth = rc.right - rc.left;
  605. unsigned int winHeight = rc.bottom - rc.top;
  606. int screenw = GetSystemMetrics(SM_CXSCREEN);
  607. int screenh = GetSystemMetrics(SM_CYSCREEN);
  608. int left = (screenw - winWidth) / 2;
  609. int top = (screenh - winHeight) / 2;
  610. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  611. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  612. }
  613. mSwapChain->SetFullscreenState(mIsFullScreen, NULL);
  614. mSwitchingFullscreen = false;
  615. }
  616. bool D3D11RenderWindow::_checkMultiSampleQuality(UINT SampleCount, UINT *outQuality, DXGI_FORMAT format)
  617. {
  618. if (SUCCEEDED(mDevice.getD3D11Device()->CheckMultisampleQualityLevels(format, SampleCount, outQuality)))
  619. return true;
  620. else
  621. return false;
  622. }
  623. }