CmD3D11RenderWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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);
  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);
  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. }
  291. void D3D11RenderWindow::setActive(bool state)
  292. {
  293. if (mHWnd && mSwapChain)
  294. {
  295. if (state)
  296. {
  297. ShowWindow(mHWnd, SW_RESTORE);
  298. mSwapChain->SetFullscreenState(mIsFullScreen, nullptr);
  299. }
  300. else
  301. {
  302. ShowWindow(mHWnd, SW_SHOWMINIMIZED);
  303. mSwapChain->SetFullscreenState(FALSE, nullptr);
  304. }
  305. }
  306. RenderWindow::setActive(state);
  307. }
  308. void D3D11RenderWindow::setHidden(bool hidden)
  309. {
  310. mHidden = hidden;
  311. if (!mIsExternal)
  312. {
  313. if (hidden)
  314. ShowWindow(mHWnd, SW_HIDE);
  315. else
  316. ShowWindow(mHWnd, SW_SHOWNORMAL);
  317. }
  318. }
  319. void D3D11RenderWindow::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
  320. {
  321. if (fullScreen != mIsFullScreen || width != mWidth || height != mHeight)
  322. {
  323. if (fullScreen != mIsFullScreen)
  324. mSwitchingFullscreen = true;
  325. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  326. bool oldFullscreen = mIsFullScreen;
  327. mIsFullScreen = fullScreen;
  328. if (fullScreen)
  329. {
  330. dwStyle |= WS_POPUP;
  331. mTop = mLeft = 0;
  332. mWidth = width;
  333. mHeight = height;
  334. // need different ordering here
  335. if (oldFullscreen)
  336. {
  337. // was previously fullscreen, just changing the resolution
  338. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, width, height, SWP_NOACTIVATE);
  339. }
  340. else
  341. {
  342. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, width, height, SWP_NOACTIVATE);
  343. //MoveWindow(mHWnd, mLeft, mTop, mWidth, mHeight, FALSE);
  344. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  345. SetWindowPos(mHWnd, 0, 0,0, 0,0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
  346. }
  347. }
  348. else
  349. {
  350. dwStyle |= WS_OVERLAPPEDWINDOW;
  351. // Calculate window dimensions required
  352. // to get the requested client area
  353. RECT rc;
  354. SetRect(&rc, 0, 0, width, height);
  355. AdjustWindowRect(&rc, dwStyle, false);
  356. unsigned int winWidth = rc.right - rc.left;
  357. unsigned int winHeight = rc.bottom - rc.top;
  358. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  359. SetWindowPos(mHWnd, HWND_NOTOPMOST, 0, 0, winWidth, winHeight,
  360. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE);
  361. // Note that we also set the position in the restoreLostDevice method
  362. // via _finishSwitchingFullScreen
  363. }
  364. mSwapChainDesc.Windowed = !fullScreen;
  365. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  366. mSwapChainDesc.BufferDesc.RefreshRate.Denominator=0;
  367. mSwapChainDesc.BufferDesc.Height = height;
  368. mSwapChainDesc.BufferDesc.Width = width;
  369. }
  370. }
  371. void D3D11RenderWindow::getCustomAttribute( const String& name, void* pData )
  372. {
  373. if(name == "WINDOW")
  374. {
  375. HWND *pWnd = (HWND*)pData;
  376. *pWnd = mHWnd;
  377. return;
  378. }
  379. if(name == "RTV")
  380. {
  381. *static_cast<ID3D11RenderTargetView**>(pData) = mRenderTargetView;
  382. return;
  383. }
  384. else if(name == "DSV")
  385. {
  386. D3D11TextureView* d3d11TextureView = static_cast<D3D11TextureView*>(mDepthStencilView.get());
  387. *static_cast<ID3D11DepthStencilView**>(pData) = d3d11TextureView->getDSV();
  388. return;
  389. }
  390. RenderWindow::getCustomAttribute(name, pData);
  391. }
  392. void D3D11RenderWindow::copyContentsToMemory(const PixelData &dst, FrameBuffer buffer)
  393. {
  394. if(mBackBuffer == nullptr)
  395. return;
  396. // get the backbuffer desc
  397. D3D11_TEXTURE2D_DESC BBDesc;
  398. mBackBuffer->GetDesc(&BBDesc);
  399. ID3D11Texture2D *backbuffer = nullptr;
  400. if(BBDesc.SampleDesc.Quality > 0)
  401. {
  402. D3D11_TEXTURE2D_DESC desc = BBDesc;
  403. desc.Usage = D3D11_USAGE_DEFAULT;
  404. desc.CPUAccessFlags = 0;
  405. desc.BindFlags = 0;
  406. desc.SampleDesc.Quality = 0;
  407. desc.SampleDesc.Count = 1;
  408. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(
  409. &desc,
  410. NULL,
  411. &backbuffer);
  412. if (FAILED(hr) || mDevice.hasError())
  413. {
  414. String errorDescription = mDevice.getErrorDescription();
  415. CM_EXCEPT(RenderingAPIException,
  416. "Error creating texture\nError Description:" + errorDescription);
  417. }
  418. mDevice.getImmediateContext()->ResolveSubresource(backbuffer, D3D11CalcSubresource(0, 0, 1), mBackBuffer, D3D11CalcSubresource(0, 0, 1), desc.Format);
  419. }
  420. // change the parameters of the texture so we can read it
  421. BBDesc.Usage = D3D11_USAGE_STAGING;
  422. BBDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  423. BBDesc.BindFlags = 0;
  424. BBDesc.SampleDesc.Quality = 0;
  425. BBDesc.SampleDesc.Count = 1;
  426. // create a temp buffer to copy to
  427. ID3D11Texture2D * pTempTexture2D;
  428. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(
  429. &BBDesc,
  430. NULL,
  431. &pTempTexture2D);
  432. if (FAILED(hr) || mDevice.hasError())
  433. {
  434. String errorDescription = mDevice.getErrorDescription();
  435. CM_EXCEPT(RenderingAPIException,
  436. "Error creating texture\nError Description:" + errorDescription);
  437. }
  438. // copy the back buffer
  439. mDevice.getImmediateContext()->CopyResource(pTempTexture2D, backbuffer != NULL ? backbuffer : mBackBuffer);
  440. // map the copied texture
  441. D3D11_MAPPED_SUBRESOURCE mappedTex2D;
  442. mDevice.getImmediateContext()->Map(pTempTexture2D, 0,D3D11_MAP_READ, 0, &mappedTex2D);
  443. // copy the the texture to the dest
  444. PixelUtil::bulkPixelConversion(PixelData(mWidth, mHeight, 1, PF_A8B8G8R8, mappedTex2D.pData), dst);
  445. // unmap the temp buffer
  446. mDevice.getImmediateContext()->Unmap(pTempTexture2D, 0);
  447. // Release the temp buffer
  448. SAFE_RELEASE(pTempTexture2D);
  449. SAFE_RELEASE(backbuffer);
  450. }
  451. void D3D11RenderWindow::_createSwapChain()
  452. {
  453. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  454. // get the dxgi device
  455. IDXGIDevice* pDXGIDevice = _queryDxgiDevice();
  456. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  457. DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  458. mSwapChainDesc.OutputWindow = mHWnd;
  459. mSwapChainDesc.BufferDesc.Width = mWidth;
  460. mSwapChainDesc.BufferDesc.Height = mHeight;
  461. mSwapChainDesc.BufferDesc.Format = format;
  462. mSwapChainDesc.BufferDesc.RefreshRate.Numerator=0;
  463. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  464. mSwapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  465. mSwapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  466. mSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH ;
  467. // triple buffer if VSync is on
  468. mSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  469. mSwapChainDesc.BufferCount = mVSync ? 2 : 1;
  470. mSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
  471. mSwapChainDesc.OutputWindow = mHWnd;
  472. mSwapChainDesc.Windowed = !mIsFullScreen;
  473. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  474. rs->determineFSAASettings(mFSAA, mFSAAHint, format, &mFSAAType);
  475. mSwapChainDesc.SampleDesc.Count = mFSAAType.Count;
  476. mSwapChainDesc.SampleDesc.Quality = mFSAAType.Quality;
  477. HRESULT hr;
  478. // Create swap chain
  479. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  480. if (FAILED(hr))
  481. {
  482. // Try a second time, may fail the first time due to back buffer count,
  483. // which will be corrected by the runtime
  484. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  485. }
  486. SAFE_RELEASE(pDXGIDevice);
  487. if (FAILED(hr))
  488. CM_EXCEPT(RenderingAPIException, "Unable to create swap chain");
  489. }
  490. void D3D11RenderWindow::_createSizeDependedD3DResources()
  491. {
  492. // obtain back buffer
  493. SAFE_RELEASE(mBackBuffer);
  494. HRESULT hr = mSwapChain->GetBuffer(0, __uuidof( ID3D11Texture2D ), (LPVOID*)&mBackBuffer);
  495. if( FAILED(hr) )
  496. CM_EXCEPT(RenderingAPIException, "Unable to Get Back Buffer for swap chain");
  497. // create all other size depended resources
  498. assert(mBackBuffer && !mRenderTargetView);
  499. // get the backbuffer desc
  500. D3D11_TEXTURE2D_DESC BBDesc;
  501. mBackBuffer->GetDesc(&BBDesc);
  502. // create the render target view
  503. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  504. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  505. RTVDesc.Format = BBDesc.Format;
  506. RTVDesc.ViewDimension = mFSAA ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  507. RTVDesc.Texture2D.MipSlice = 0;
  508. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  509. if( FAILED(hr) )
  510. {
  511. String errorDescription = mDevice.getErrorDescription();
  512. CM_EXCEPT(RenderingAPIException, "Unable to create rendertagert view\nError Description:" + errorDescription);
  513. }
  514. mDepthStencilBuffer = TextureManager::instance().createTexture(TEX_TYPE_2D,
  515. BBDesc.Width, BBDesc.Height, 0, PF_D24S8, TU_DEPTHSTENCIL, false, mFSAA, mFSAAHint);
  516. if(mDepthStencilView != nullptr)
  517. {
  518. Texture::releaseView(mDepthStencilView);
  519. mDepthStencilView = nullptr;
  520. }
  521. mDepthStencilView = Texture::requestView(mDepthStencilBuffer, 0, 1, 0, 1, GVU_DEPTHSTENCIL);
  522. }
  523. void D3D11RenderWindow::_destroySizeDependedD3DResources()
  524. {
  525. SAFE_RELEASE(mBackBuffer);
  526. SAFE_RELEASE(mRenderTargetView);
  527. mDepthStencilBuffer = nullptr;
  528. }
  529. void D3D11RenderWindow::_resizeSwapChainBuffers(unsigned width, unsigned height)
  530. {
  531. _destroySizeDependedD3DResources();
  532. // width and height can be zero to autodetect size, therefore do not rely on them
  533. UINT Flags = mIsFullScreen ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0;
  534. mSwapChain->ResizeBuffers(mSwapChainDesc.BufferCount, width, height, mSwapChainDesc.BufferDesc.Format, Flags);
  535. mSwapChain->GetDesc(&mSwapChainDesc);
  536. mWidth = mSwapChainDesc.BufferDesc.Width;
  537. mHeight = mSwapChainDesc.BufferDesc.Height;
  538. mIsFullScreen = (0 == mSwapChainDesc.Windowed); // Alt-Enter together with SetWindowAssociation() can change this state
  539. _createSizeDependedD3DResources();
  540. mDevice.getImmediateContext()->OMSetRenderTargets(0, 0, 0);
  541. // Additional swap chains need their own depth buffer
  542. // to support resizing them
  543. HRESULT hr = mSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&mBackBuffer );
  544. if( FAILED(hr) )
  545. {
  546. CM_EXCEPT(RenderingAPIException,
  547. "Unable to Get Back Buffer for swap chain");
  548. }
  549. // get the backbuffer desc
  550. D3D11_TEXTURE2D_DESC BBDesc;
  551. mBackBuffer->GetDesc(&BBDesc);
  552. // create the render target view
  553. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  554. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  555. RTVDesc.Format = BBDesc.Format;
  556. RTVDesc.ViewDimension = mFSAA ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  557. RTVDesc.Texture2D.MipSlice = 0;
  558. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  559. if(FAILED(hr))
  560. {
  561. String errorDescription = mDevice.getErrorDescription();
  562. CM_EXCEPT(RenderingAPIException,
  563. "Unable to create rendertagert view\nError Description:" + errorDescription);
  564. }
  565. }
  566. IDXGIDevice* D3D11RenderWindow::_queryDxgiDevice()
  567. {
  568. if (mDevice.getD3D11Device() == nullptr)
  569. {
  570. CM_EXCEPT(RenderingAPIException, "D3D11Device is NULL!");
  571. }
  572. IDXGIDevice* pDXGIDevice = nullptr;
  573. HRESULT hr = mDevice.getD3D11Device()->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
  574. if(FAILED(hr))
  575. CM_EXCEPT(RenderingAPIException, "Unable to query a DXGIDevice");
  576. return pDXGIDevice;
  577. }
  578. void D3D11RenderWindow::_finishSwitchingFullscreen()
  579. {
  580. if(mIsFullScreen)
  581. {
  582. // Need to reset the region on the window sometimes, when the
  583. // windowed mode was constrained by desktop
  584. HRGN hRgn = CreateRectRgn(0, 0, mSwapChainDesc.BufferDesc.Width, mSwapChainDesc.BufferDesc.Height);
  585. SetWindowRgn(mHWnd, hRgn, FALSE);
  586. }
  587. else
  588. {
  589. // When switching back to windowed mode, need to reset window size
  590. // after device has been restored
  591. RECT rc;
  592. SetRect(&rc, 0, 0, mSwapChainDesc.BufferDesc.Width, mSwapChainDesc.BufferDesc.Height);
  593. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  594. unsigned int winWidth = rc.right - rc.left;
  595. unsigned int winHeight = rc.bottom - rc.top;
  596. int screenw = GetSystemMetrics(SM_CXSCREEN);
  597. int screenh = GetSystemMetrics(SM_CYSCREEN);
  598. int left = (screenw - winWidth) / 2;
  599. int top = (screenh - winHeight) / 2;
  600. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  601. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  602. }
  603. mSwapChain->SetFullscreenState(mIsFullScreen, NULL);
  604. mSwitchingFullscreen = false;
  605. }
  606. bool D3D11RenderWindow::_checkMultiSampleQuality(UINT SampleCount, UINT *outQuality, DXGI_FORMAT format)
  607. {
  608. if (SUCCEEDED(mDevice.getD3D11Device()->CheckMultisampleQualityLevels(format, SampleCount, outQuality)))
  609. return true;
  610. else
  611. return false;
  612. }
  613. }