CmD3D11RenderWindow.cpp 22 KB

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