BsD3D11RenderWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. #include "BsD3D11RenderWindow.h"
  2. #include "BsCoreThread.h"
  3. #include "Win32/BsPlatformWndProc.h"
  4. #include "BsD3D11RenderSystem.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsD3D11RenderTexture.h"
  7. #include "BsD3D11TextureView.h"
  8. #include "BsTextureManager.h"
  9. #include "BsD3D11DriverList.h"
  10. #include "BsD3D11Driver.h"
  11. #include "BsD3D11VideoModeInfo.h"
  12. #include "BsRenderStats.h"
  13. #include "BsInput.h"
  14. #include "BsException.h"
  15. namespace BansheeEngine
  16. {
  17. D3D11RenderWindowProperties::D3D11RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  18. :RenderWindowProperties(desc)
  19. { }
  20. D3D11RenderWindowCore::D3D11RenderWindowCore(const RENDER_WINDOW_DESC& desc, D3D11Device& device, IDXGIFactory* DXGIFactory)
  21. : RenderWindowCore(desc), mProperties(desc), mDevice(device), mDXGIFactory(DXGIFactory), mIsExternal(false), mSizing(false),
  22. mRenderTargetView(nullptr), mBackBuffer(nullptr), mSwapChain(nullptr), mDepthStencilView(nullptr), mIsChild(false),
  23. mRefreshRateNumerator(0), mRefreshRateDenominator(0), mHWnd(0)
  24. { }
  25. D3D11RenderWindowCore::~D3D11RenderWindowCore()
  26. { }
  27. void D3D11RenderWindowCore::initialize()
  28. {
  29. RenderWindowCore::initialize();
  30. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  31. D3D11RenderWindowProperties& props = mProperties;
  32. mMultisampleType.Count = 1;
  33. mMultisampleType.Quality = 0;
  34. HWND parentHWnd = 0;
  35. HWND externalHandle = 0;
  36. NameValuePairList::const_iterator opt;
  37. opt = mDesc.platformSpecific.find("parentWindowHandle");
  38. if (opt != mDesc.platformSpecific.end())
  39. parentHWnd = (HWND)parseUnsignedInt(opt->second);
  40. opt = mDesc.platformSpecific.find("externalWindowHandle");
  41. if (opt != mDesc.platformSpecific.end())
  42. externalHandle = (HWND)parseUnsignedInt(opt->second);
  43. mIsChild = parentHWnd != 0;
  44. props.mIsFullScreen = mDesc.fullscreen && !mIsChild;
  45. props.mColorDepth = 32;
  46. props.mActive = true;
  47. if (mDesc.videoMode.isCustom())
  48. {
  49. mRefreshRateNumerator = Math::roundToInt(mDesc.videoMode.getRefreshRate());
  50. mRefreshRateDenominator = 1;
  51. }
  52. else
  53. {
  54. const D3D11VideoMode& d3d11videoMode = static_cast<const D3D11VideoMode&>(mDesc.videoMode);
  55. mRefreshRateNumerator = d3d11videoMode.getRefreshRateNumerator();
  56. mRefreshRateDenominator = d3d11videoMode.getRefreshRateDenominator();
  57. }
  58. HMONITOR hMonitor = NULL;
  59. const D3D11VideoOutputInfo* outputInfo = nullptr;
  60. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  61. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  62. if (numOutputs > 0)
  63. {
  64. UINT32 actualMonitorIdx = std::min(mDesc.videoMode.getOutputIdx(), numOutputs - 1);
  65. outputInfo = static_cast<const D3D11VideoOutputInfo*>(&videoModeInfo.getOutputInfo(actualMonitorIdx));
  66. DXGI_OUTPUT_DESC desc;
  67. outputInfo->getDXGIOutput()->GetDesc(&desc);
  68. hMonitor = desc.Monitor;
  69. }
  70. if (!externalHandle)
  71. {
  72. DWORD dwStyle = (props.isHidden() ? 0 : WS_VISIBLE) | WS_CLIPCHILDREN;
  73. DWORD dwStyleEx = 0;
  74. RECT rc;
  75. MONITORINFO monitorInfo;
  76. // If we didn't specified the adapter index, or if it didn't find it
  77. if (hMonitor == NULL)
  78. {
  79. POINT windowAnchorPoint;
  80. // Fill in anchor point.
  81. windowAnchorPoint.x = mDesc.left;
  82. windowAnchorPoint.y = mDesc.top;
  83. // Get the nearest monitor to this window.
  84. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  85. }
  86. // Get the target monitor info
  87. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  88. monitorInfo.cbSize = sizeof(MONITORINFO);
  89. GetMonitorInfo(hMonitor, &monitorInfo);
  90. unsigned int winWidth, winHeight;
  91. winWidth = mDesc.videoMode.getWidth();
  92. winHeight = mDesc.videoMode.getHeight();
  93. UINT32 left = mDesc.left;
  94. UINT32 top = mDesc.top;
  95. // No specified top left -> Center the window in the middle of the monitor
  96. if (left == -1 || top == -1)
  97. {
  98. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  99. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  100. // clamp window dimensions to screen size
  101. int outerw = (int(winWidth) < screenw) ? int(winWidth) : screenw;
  102. int outerh = (int(winHeight) < screenh) ? int(winHeight) : screenh;
  103. if (left == -1)
  104. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  105. else if (hMonitor != NULL)
  106. left += monitorInfo.rcWork.left;
  107. if (top == -1)
  108. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  109. else if (hMonitor != NULL)
  110. top += monitorInfo.rcWork.top;
  111. }
  112. else if (hMonitor != NULL)
  113. {
  114. left += monitorInfo.rcWork.left;
  115. top += monitorInfo.rcWork.top;
  116. }
  117. props.mWidth = mDesc.videoMode.getWidth();
  118. props.mHeight = mDesc.videoMode.getHeight();
  119. props.mTop = top;
  120. props.mLeft = left;
  121. if (!mDesc.fullscreen)
  122. {
  123. if (parentHWnd)
  124. {
  125. if (mDesc.toolWindow)
  126. dwStyleEx = WS_EX_TOOLWINDOW;
  127. else
  128. dwStyle |= WS_CHILD;
  129. }
  130. if (!parentHWnd || mDesc.toolWindow)
  131. {
  132. if (mDesc.border == WindowBorder::None)
  133. dwStyle |= WS_POPUP;
  134. else if (mDesc.border == WindowBorder::Fixed)
  135. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  136. WS_SYSMENU | WS_MINIMIZEBOX;
  137. else
  138. dwStyle |= WS_OVERLAPPEDWINDOW;
  139. }
  140. if (!mDesc.outerDimensions)
  141. {
  142. // Calculate window dimensions required
  143. // to get the requested client area
  144. SetRect(&rc, 0, 0, props.mWidth, props.mHeight);
  145. AdjustWindowRect(&rc, dwStyle, false);
  146. props.mWidth = rc.right - rc.left;
  147. props.mHeight = rc.bottom - rc.top;
  148. // Clamp width and height to the desktop dimensions
  149. int screenw = GetSystemMetrics(SM_CXSCREEN);
  150. int screenh = GetSystemMetrics(SM_CYSCREEN);
  151. if ((int)props.mWidth > screenw)
  152. props.mWidth = screenw;
  153. if ((int)props.mHeight > screenh)
  154. props.mHeight = screenh;
  155. if (props.mLeft < 0)
  156. props.mLeft = (screenw - props.mWidth) / 2;
  157. if (props.mTop < 0)
  158. props.mTop = (screenh - props.mHeight) / 2;
  159. }
  160. }
  161. else
  162. {
  163. dwStyle |= WS_POPUP;
  164. props.mTop = 0;
  165. props.mLeft = 0;
  166. }
  167. UINT classStyle = 0;
  168. if (mDesc.enableDoubleClick)
  169. classStyle |= CS_DBLCLKS;
  170. HINSTANCE hInst = NULL;
  171. // Register the window class
  172. // Allow 4 bytes of window data for D3D11RenderWindow pointer
  173. WNDCLASS wc = { classStyle, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  174. LoadIcon(0, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  175. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "D3D11Wnd" };
  176. RegisterClass(&wc);
  177. // Create our main window
  178. // Pass pointer to self
  179. mIsExternal = false;
  180. mHWnd = CreateWindowEx(dwStyleEx, "D3D11Wnd", mDesc.title.c_str(), dwStyle,
  181. props.mLeft, props.mTop, props.mWidth, props.mHeight, parentHWnd, 0, hInst, this);
  182. }
  183. else
  184. {
  185. mHWnd = externalHandle;
  186. mIsExternal = true;
  187. }
  188. RECT rc;
  189. GetWindowRect(mHWnd, &rc);
  190. props.mTop = rc.top;
  191. props.mLeft = rc.left;
  192. GetClientRect(mHWnd, &rc);
  193. props.mWidth = rc.right;
  194. props.mHeight = rc.bottom;
  195. createSwapChain();
  196. if (props.isFullScreen())
  197. {
  198. if (outputInfo != nullptr)
  199. mSwapChain->SetFullscreenState(true, outputInfo->getDXGIOutput());
  200. else
  201. mSwapChain->SetFullscreenState(true, nullptr);
  202. }
  203. createSizeDependedD3DResources();
  204. mDXGIFactory->MakeWindowAssociation(mHWnd, NULL);
  205. setHidden(props.isHidden());
  206. }
  207. void D3D11RenderWindowCore::destroy()
  208. {
  209. D3D11RenderWindowProperties& props = mProperties;
  210. props.mActive = false;
  211. markCoreDirty();
  212. SAFE_RELEASE(mSwapChain);
  213. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_SwapChain);
  214. if (mHWnd && !mIsExternal)
  215. {
  216. DestroyWindow(mHWnd);
  217. }
  218. if (mDepthStencilView != nullptr)
  219. {
  220. TextureCore::releaseView(mDepthStencilView);
  221. mDepthStencilView = nullptr;
  222. }
  223. mHWnd = 0;
  224. destroySizeDependedD3DResources();
  225. RenderWindowCore::destroy();
  226. }
  227. void D3D11RenderWindowCore::swapBuffers()
  228. {
  229. THROW_IF_NOT_CORE_THREAD;
  230. if(mDevice.getD3D11Device() != nullptr)
  231. {
  232. HRESULT hr = mSwapChain->Present(getProperties().getVSync() ? getProperties().getVSyncInterval() : 0, 0);
  233. if( FAILED(hr) )
  234. BS_EXCEPT(RenderingAPIException, "Error Presenting surfaces");
  235. }
  236. }
  237. void D3D11RenderWindowCore::move(INT32 top, INT32 left)
  238. {
  239. THROW_IF_NOT_CORE_THREAD;
  240. D3D11RenderWindowProperties& props = mProperties;
  241. if (mHWnd && !props.mIsFullScreen)
  242. {
  243. props.mTop = top;
  244. props.mLeft = left;
  245. SetWindowPos(mHWnd, 0, top, left, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  246. markCoreDirty();
  247. }
  248. }
  249. void D3D11RenderWindowCore::resize(UINT32 width, UINT32 height)
  250. {
  251. THROW_IF_NOT_CORE_THREAD;
  252. D3D11RenderWindowProperties& props = mProperties;
  253. if (mHWnd && !props.mIsFullScreen)
  254. {
  255. props.mWidth = width;
  256. props.mHeight = height;
  257. RECT rc = { 0, 0, width, height };
  258. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  259. width = rc.right - rc.left;
  260. height = rc.bottom - rc.top;
  261. SetWindowPos(mHWnd, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  262. markCoreDirty();
  263. }
  264. }
  265. void D3D11RenderWindowCore::setActive(bool state)
  266. {
  267. THROW_IF_NOT_CORE_THREAD;
  268. D3D11RenderWindowProperties& props = mProperties;
  269. if (mHWnd && mSwapChain)
  270. {
  271. if (state)
  272. {
  273. ShowWindow(mHWnd, SW_RESTORE);
  274. mSwapChain->SetFullscreenState(props.mIsFullScreen, nullptr);
  275. }
  276. else
  277. {
  278. ShowWindow(mHWnd, SW_SHOWMINIMIZED);
  279. mSwapChain->SetFullscreenState(FALSE, nullptr);
  280. }
  281. }
  282. RenderWindowCore::setActive(state);
  283. }
  284. void D3D11RenderWindowCore::setHidden(bool hidden)
  285. {
  286. THROW_IF_NOT_CORE_THREAD;
  287. D3D11RenderWindowProperties& props = mProperties;
  288. props.mHidden = hidden;
  289. if (!mIsExternal)
  290. {
  291. if (hidden)
  292. ShowWindow(mHWnd, SW_HIDE);
  293. else
  294. ShowWindow(mHWnd, SW_SHOWNORMAL);
  295. }
  296. markCoreDirty();
  297. }
  298. void D3D11RenderWindowCore::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  299. {
  300. THROW_IF_NOT_CORE_THREAD;
  301. if (mIsChild)
  302. return;
  303. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  304. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  305. if (numOutputs == 0)
  306. return;
  307. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  308. const D3D11VideoOutputInfo& outputInfo = static_cast<const D3D11VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  309. DXGI_MODE_DESC modeDesc;
  310. ZeroMemory(&modeDesc, sizeof(modeDesc));
  311. modeDesc.Width = width;
  312. modeDesc.Height = height;
  313. modeDesc.RefreshRate.Numerator = Math::roundToInt(refreshRate);
  314. modeDesc.RefreshRate.Denominator = 1;
  315. modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  316. modeDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  317. modeDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  318. DXGI_MODE_DESC nearestMode;
  319. ZeroMemory(&nearestMode, sizeof(nearestMode));
  320. outputInfo.getDXGIOutput()->FindClosestMatchingMode(&modeDesc, &nearestMode, nullptr);
  321. mProperties.mIsFullScreen = true;
  322. mProperties.mWidth = width;
  323. mProperties.mHeight = height;
  324. mSwapChain->ResizeTarget(&nearestMode);
  325. mSwapChain->SetFullscreenState(true, outputInfo.getDXGIOutput());
  326. markCoreDirty();
  327. }
  328. void D3D11RenderWindowCore::setFullscreen(const VideoMode& mode)
  329. {
  330. THROW_IF_NOT_CORE_THREAD;
  331. if (mIsChild)
  332. return;
  333. if (mode.isCustom())
  334. {
  335. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  336. return;
  337. }
  338. const D3D11VideoModeInfo& videoModeInfo = static_cast<const D3D11VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  339. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  340. if (numOutputs == 0)
  341. return;
  342. UINT32 actualMonitorIdx = std::min(mode.getOutputIdx(), numOutputs - 1);
  343. const D3D11VideoOutputInfo& outputInfo = static_cast<const D3D11VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  344. const D3D11VideoMode& videoMode = static_cast<const D3D11VideoMode&>(mode);
  345. mProperties.mIsFullScreen = true;
  346. mProperties.mWidth = mode.getWidth();
  347. mProperties.mHeight = mode.getHeight();
  348. mSwapChain->ResizeTarget(&videoMode.getDXGIModeDesc());
  349. mSwapChain->SetFullscreenState(true, outputInfo.getDXGIOutput());
  350. markCoreDirty();
  351. }
  352. void D3D11RenderWindowCore::setWindowed(UINT32 width, UINT32 height)
  353. {
  354. THROW_IF_NOT_CORE_THREAD;
  355. mProperties.mWidth = width;
  356. mProperties.mHeight = height;
  357. mProperties.mIsFullScreen = false;
  358. mSwapChainDesc.Windowed = true;
  359. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  360. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  361. mSwapChainDesc.BufferDesc.Width = width;
  362. mSwapChainDesc.BufferDesc.Height = height;
  363. DXGI_MODE_DESC modeDesc;
  364. ZeroMemory(&modeDesc, sizeof(modeDesc));
  365. modeDesc.Width = width;
  366. modeDesc.Height = height;
  367. modeDesc.RefreshRate.Numerator = 0;
  368. modeDesc.RefreshRate.Denominator = 0;
  369. modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  370. mSwapChain->SetFullscreenState(false, nullptr);
  371. mSwapChain->ResizeTarget(&modeDesc);
  372. markCoreDirty();
  373. }
  374. HWND D3D11RenderWindowCore::_getWindowHandle() const
  375. {
  376. return mHWnd;
  377. }
  378. void D3D11RenderWindowCore::getCustomAttribute(const String& name, void* pData) const
  379. {
  380. if(name == "WINDOW")
  381. {
  382. HWND *pWnd = (HWND*)pData;
  383. *pWnd = mHWnd;
  384. return;
  385. }
  386. if(name == "RTV")
  387. {
  388. *static_cast<ID3D11RenderTargetView**>(pData) = mRenderTargetView;
  389. return;
  390. }
  391. else if(name == "DSV")
  392. {
  393. D3D11TextureView* d3d11TextureView = static_cast<D3D11TextureView*>(mDepthStencilView.get());
  394. *static_cast<ID3D11DepthStencilView**>(pData) = d3d11TextureView->getDSV();
  395. return;
  396. }
  397. RenderWindowCore::getCustomAttribute(name, pData);
  398. }
  399. void D3D11RenderWindowCore::copyToMemory(PixelData &dst, FrameBuffer buffer)
  400. {
  401. THROW_IF_NOT_CORE_THREAD;
  402. if(mBackBuffer == nullptr)
  403. return;
  404. // Get the backbuffer desc
  405. D3D11_TEXTURE2D_DESC BBDesc;
  406. mBackBuffer->GetDesc(&BBDesc);
  407. ID3D11Texture2D* backbuffer = nullptr;
  408. if(BBDesc.SampleDesc.Quality > 0)
  409. {
  410. D3D11_TEXTURE2D_DESC desc = BBDesc;
  411. desc.Usage = D3D11_USAGE_DEFAULT;
  412. desc.CPUAccessFlags = 0;
  413. desc.BindFlags = 0;
  414. desc.SampleDesc.Quality = 0;
  415. desc.SampleDesc.Count = 1;
  416. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(&desc, nullptr, &backbuffer);
  417. if (FAILED(hr) || mDevice.hasError())
  418. {
  419. String errorDescription = mDevice.getErrorDescription();
  420. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  421. }
  422. mDevice.getImmediateContext()->ResolveSubresource(backbuffer, D3D11CalcSubresource(0, 0, 1), mBackBuffer, D3D11CalcSubresource(0, 0, 1), desc.Format);
  423. }
  424. // Change the parameters of the texture so we can read it
  425. BBDesc.Usage = D3D11_USAGE_STAGING;
  426. BBDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  427. BBDesc.BindFlags = 0;
  428. BBDesc.SampleDesc.Quality = 0;
  429. BBDesc.SampleDesc.Count = 1;
  430. // Create a temp buffer to copy to
  431. ID3D11Texture2D* tempTexture;
  432. HRESULT hr = mDevice.getD3D11Device()->CreateTexture2D(&BBDesc, nullptr, &tempTexture);
  433. if (FAILED(hr) || mDevice.hasError())
  434. {
  435. String errorDescription = mDevice.getErrorDescription();
  436. BS_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
  437. }
  438. // Copy the back buffer
  439. mDevice.getImmediateContext()->CopyResource(tempTexture, backbuffer != NULL ? backbuffer : mBackBuffer);
  440. // Map the copied texture
  441. D3D11_MAPPED_SUBRESOURCE mappedTex2D;
  442. mDevice.getImmediateContext()->Map(tempTexture, 0,D3D11_MAP_READ, 0, &mappedTex2D);
  443. // Copy the the texture to the dest
  444. PixelData src(getProperties().getWidth(), getProperties().getHeight(), 1, PF_A8B8G8R8);
  445. src.setExternalBuffer((UINT8*)mappedTex2D.pData);
  446. PixelUtil::bulkPixelConversion(src, dst);
  447. // Unmap the temp buffer
  448. mDevice.getImmediateContext()->Unmap(tempTexture, 0);
  449. // Release the temp buffer
  450. SAFE_RELEASE(tempTexture);
  451. SAFE_RELEASE(backbuffer);
  452. }
  453. void D3D11RenderWindowCore::_windowMovedOrResized()
  454. {
  455. THROW_IF_NOT_CORE_THREAD;
  456. D3D11RenderWindowProperties& props = mProperties;
  457. if (!mHWnd || IsIconic(mHWnd))
  458. return;
  459. RECT rc;
  460. GetWindowRect(mHWnd, &rc);
  461. mProperties.mTop = rc.top;
  462. mProperties.mLeft = rc.left;
  463. markCoreDirty();
  464. GetClientRect(mHWnd, &rc);
  465. unsigned int width = rc.right - rc.left;
  466. unsigned int height = rc.bottom - rc.top;
  467. if (width == 0)
  468. width = 1;
  469. if (height == 0)
  470. height = 1;
  471. resizeSwapChainBuffers(width, height);
  472. RenderWindowCore::_windowMovedOrResized();
  473. }
  474. void D3D11RenderWindowCore::createSwapChain()
  475. {
  476. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  477. D3D11RenderWindowProperties& props = mProperties;
  478. IDXGIDevice* pDXGIDevice = queryDxgiDevice();
  479. ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  480. DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
  481. mSwapChainDesc.OutputWindow = mHWnd;
  482. mSwapChainDesc.BufferDesc.Width = props.mWidth;
  483. mSwapChainDesc.BufferDesc.Height = props.mHeight;
  484. mSwapChainDesc.BufferDesc.Format = format;
  485. if (props.mIsFullScreen)
  486. {
  487. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = mRefreshRateNumerator;
  488. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = mRefreshRateDenominator;
  489. }
  490. else
  491. {
  492. mSwapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  493. mSwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  494. }
  495. mSwapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  496. mSwapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  497. mSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH ;
  498. mSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  499. mSwapChainDesc.BufferCount = 1;
  500. mSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
  501. mSwapChainDesc.Windowed = true;
  502. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  503. rs->determineMultisampleSettings(props.mMultisampleCount, format, &mMultisampleType);
  504. mSwapChainDesc.SampleDesc.Count = mMultisampleType.Count;
  505. mSwapChainDesc.SampleDesc.Quality = mMultisampleType.Quality;
  506. HRESULT hr;
  507. // Create swap chain
  508. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  509. if (FAILED(hr))
  510. {
  511. // Try a second time, may fail the first time due to back buffer count,
  512. // which will be corrected by the runtime
  513. hr = mDXGIFactory->CreateSwapChain(pDXGIDevice, &mSwapChainDesc, &mSwapChain);
  514. }
  515. SAFE_RELEASE(pDXGIDevice);
  516. if (FAILED(hr))
  517. BS_EXCEPT(RenderingAPIException, "Unable to create swap chain");
  518. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_SwapChain);
  519. }
  520. void D3D11RenderWindowCore::createSizeDependedD3DResources()
  521. {
  522. SAFE_RELEASE(mBackBuffer);
  523. HRESULT hr = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBuffer);
  524. if(FAILED(hr))
  525. BS_EXCEPT(RenderingAPIException, "Unable to Get Back Buffer for swap chain");
  526. assert(mBackBuffer && !mRenderTargetView);
  527. D3D11_TEXTURE2D_DESC BBDesc;
  528. mBackBuffer->GetDesc(&BBDesc);
  529. D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
  530. ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
  531. RTVDesc.Format = BBDesc.Format;
  532. RTVDesc.ViewDimension = getProperties().getMultisampleCount() ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
  533. RTVDesc.Texture2D.MipSlice = 0;
  534. hr = mDevice.getD3D11Device()->CreateRenderTargetView(mBackBuffer, &RTVDesc, &mRenderTargetView);
  535. if(FAILED(hr))
  536. {
  537. String errorDescription = mDevice.getErrorDescription();
  538. BS_EXCEPT(RenderingAPIException, "Unable to create rendertagert view\nError Description:" + errorDescription);
  539. }
  540. mDepthStencilBuffer = TextureCoreManager::instance().createTexture(TEX_TYPE_2D,
  541. BBDesc.Width, BBDesc.Height, 0, 0, PF_D24S8, TU_DEPTHSTENCIL, false,
  542. getProperties().getMultisampleCount());
  543. if(mDepthStencilView != nullptr)
  544. {
  545. TextureCore::releaseView(mDepthStencilView);
  546. mDepthStencilView = nullptr;
  547. }
  548. mDepthStencilView = TextureCore::requestView(mDepthStencilBuffer, 0, 1, 0, 1, GVU_DEPTHSTENCIL);
  549. }
  550. void D3D11RenderWindowCore::destroySizeDependedD3DResources()
  551. {
  552. SAFE_RELEASE(mBackBuffer);
  553. SAFE_RELEASE(mRenderTargetView);
  554. mDepthStencilBuffer = nullptr;
  555. }
  556. void D3D11RenderWindowCore::resizeSwapChainBuffers(UINT32 width, UINT32 height)
  557. {
  558. destroySizeDependedD3DResources();
  559. UINT Flags = mProperties.isFullScreen() ? DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH : 0;
  560. HRESULT hr = mSwapChain->ResizeBuffers(mSwapChainDesc.BufferCount, width, height, mSwapChainDesc.BufferDesc.Format, Flags);
  561. if(hr != S_OK)
  562. BS_EXCEPT(InternalErrorException, "Call to ResizeBuffers failed.");
  563. mSwapChain->GetDesc(&mSwapChainDesc);
  564. mProperties.mWidth = mSwapChainDesc.BufferDesc.Width;
  565. mProperties.mHeight = mSwapChainDesc.BufferDesc.Height;
  566. mProperties.mIsFullScreen = (0 == mSwapChainDesc.Windowed); // Alt-Enter together with SetWindowAssociation() can change this state
  567. markCoreDirty();
  568. createSizeDependedD3DResources();
  569. mDevice.getImmediateContext()->OMSetRenderTargets(0, 0, 0);
  570. }
  571. IDXGIDevice* D3D11RenderWindowCore::queryDxgiDevice()
  572. {
  573. if (mDevice.getD3D11Device() == nullptr)
  574. {
  575. BS_EXCEPT(RenderingAPIException, "D3D11Device is null.");
  576. }
  577. IDXGIDevice* pDXGIDevice = nullptr;
  578. HRESULT hr = mDevice.getD3D11Device()->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
  579. if(FAILED(hr))
  580. BS_EXCEPT(RenderingAPIException, "Unable to query a DXGIDevice.");
  581. return pDXGIDevice;
  582. }
  583. D3D11RenderWindow::D3D11RenderWindow(const RENDER_WINDOW_DESC& desc, D3D11Device& device, IDXGIFactory* DXGIFactory)
  584. :RenderWindow(desc), mProperties(desc), mDevice(device), mDXGIFactory(DXGIFactory)
  585. {
  586. }
  587. void D3D11RenderWindow::getCustomAttribute(const String& name, void* pData) const
  588. {
  589. if (name == "WINDOW")
  590. {
  591. HWND *pHwnd = (HWND*)pData;
  592. *pHwnd = getHWnd();
  593. return;
  594. }
  595. }
  596. Vector2I D3D11RenderWindow::screenToWindowPos(const Vector2I& screenPos) const
  597. {
  598. POINT pos;
  599. pos.x = screenPos.x;
  600. pos.y = screenPos.y;
  601. ScreenToClient(getHWnd(), &pos);
  602. return Vector2I(pos.x, pos.y);
  603. }
  604. Vector2I D3D11RenderWindow::windowToScreenPos(const Vector2I& windowPos) const
  605. {
  606. POINT pos;
  607. pos.x = windowPos.x;
  608. pos.y = windowPos.y;
  609. ClientToScreen(getHWnd(), &pos);
  610. return Vector2I(pos.x, pos.y);
  611. }
  612. SPtr<D3D11RenderWindowCore> D3D11RenderWindow::getCore() const
  613. {
  614. return std::static_pointer_cast<D3D11RenderWindowCore>(mCoreSpecific);
  615. }
  616. HWND D3D11RenderWindow::getHWnd() const
  617. {
  618. // HACK: I'm accessing core method from sim thread, which means an invalid handle
  619. // could be returned here if requested too soon after initialization.
  620. return getCore()->_getWindowHandle();
  621. }
  622. }