CmD3D11RenderWindow.cpp 21 KB

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