CmD3D11RenderWindow.cpp 18 KB

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