BsD3D11RenderWindow.cpp 23 KB

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