2
0

CmD3D11RenderWindow.cpp 22 KB

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