gfxD3D11Target.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2015 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/D3D11/gfxD3D11Target.h"
  24. #include "gfx/D3D11/gfxD3D11Cubemap.h"
  25. #include "gfx/D3D11/gfxD3D11EnumTranslate.h"
  26. #include "gfx/gfxDebugEvent.h"
  27. #include "gfx/gfxStringEnumTranslate.h"
  28. #include "windowManager/win32/win32Window.h"
  29. GFXD3D11TextureTarget::GFXD3D11TextureTarget(bool genMips)
  30. : mTargetSize( Point2I::Zero ),
  31. mTargetFormat( GFXFormatR8G8B8A8 )
  32. {
  33. for(S32 i=0; i<MaxRenderSlotId; i++)
  34. {
  35. mTargets[i] = NULL;
  36. mResolveTargets[i] = NULL;
  37. mTargetViews[i] = NULL;
  38. mTargetSRViews[i] = NULL;
  39. }
  40. mGenMips = genMips;
  41. }
  42. GFXD3D11TextureTarget::~GFXD3D11TextureTarget()
  43. {
  44. // Release anything we might be holding.
  45. for(S32 i=0; i<MaxRenderSlotId; i++)
  46. {
  47. mResolveTargets[i] = NULL;
  48. SAFE_RELEASE(mTargetViews[i]);
  49. SAFE_RELEASE(mTargets[i]);
  50. SAFE_RELEASE(mTargetSRViews[i]);
  51. }
  52. zombify();
  53. }
  54. void GFXD3D11TextureTarget::attachTexture( RenderSlot slot, GFXTextureObject *tex, U32 mipLevel/*=0*/, U32 zOffset /*= 0*/ )
  55. {
  56. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_attachTexture, ColorI::RED );
  57. AssertFatal(slot < MaxRenderSlotId, "GFXD3D11TextureTarget::attachTexture - out of range slot.");
  58. // TODO: The way this is implemented... you can attach a texture
  59. // object multiple times and it will release and reset it.
  60. //
  61. // We should rework this to detect when no change has occured
  62. // and skip out early.
  63. // Mark state as dirty so device can know to update.
  64. invalidateState();
  65. // Release what we had, it's definitely going to change.
  66. SAFE_RELEASE(mTargetViews[slot]);
  67. SAFE_RELEASE(mTargets[slot]);
  68. SAFE_RELEASE(mTargetSRViews[slot]);
  69. mResolveTargets[slot] = NULL;
  70. if(slot == Color0)
  71. {
  72. mTargetSize = Point2I::Zero;
  73. mTargetFormat = GFXFormatR8G8B8A8;
  74. }
  75. // Are we clearing?
  76. if(!tex)
  77. {
  78. // Yup - just exit, it'll stay NULL.
  79. return;
  80. }
  81. // TODO: Mip map generation currently only supported on dynamic cubemaps
  82. mTargetSRViews[slot] = NULL;
  83. // Take care of default targets
  84. if( tex == GFXTextureTarget::sDefaultDepthStencil )
  85. {
  86. mTargets[slot] = D3D11->mDeviceDepthStencil;
  87. mTargetViews[slot] = D3D11->mDeviceDepthStencilView;
  88. mTargets[slot]->AddRef();
  89. mTargetViews[slot]->AddRef();
  90. }
  91. else
  92. {
  93. // Cast the texture object to D3D...
  94. AssertFatal(dynamic_cast<GFXD3D11TextureObject*>(tex), "GFXD3D11TextureTarget::attachTexture - invalid texture object.");
  95. GFXD3D11TextureObject *d3dto = dynamic_cast<GFXD3D11TextureObject*>(tex);
  96. // Grab the surface level.
  97. if( slot == DepthStencil )
  98. {
  99. mTargets[slot] = d3dto->getSurface();
  100. if ( mTargets[slot] )
  101. mTargets[slot]->AddRef();
  102. mTargetViews[slot] = d3dto->getDSView();
  103. if( mTargetViews[slot])
  104. mTargetViews[slot]->AddRef();
  105. }
  106. else
  107. {
  108. // getSurface will almost always return NULL. It will only return non-NULL
  109. // if the surface that it needs to render to is different than the mip level
  110. // in the actual texture. This will happen with MSAA.
  111. if( d3dto->getSurface() == NULL )
  112. {
  113. mTargets[slot] = d3dto->get2DTex();
  114. mTargets[slot]->AddRef();
  115. mTargetViews[slot] = d3dto->getRTView();
  116. mTargetViews[slot]->AddRef();
  117. }
  118. else
  119. {
  120. mTargets[slot] = d3dto->getSurface();
  121. mTargets[slot]->AddRef();
  122. mTargetViews[slot]->AddRef();
  123. mResolveTargets[slot] = d3dto;
  124. if ( tex && slot == Color0 )
  125. {
  126. mTargetSize.set( tex->getSize().x, tex->getSize().y );
  127. mTargetFormat = tex->getFormat();
  128. }
  129. }
  130. if (mGenMips)
  131. {
  132. mTargetSRViews[slot] = d3dto->getSRView();
  133. mTargetSRViews[slot]->AddRef();
  134. }
  135. }
  136. // Update surface size
  137. if(slot == Color0)
  138. {
  139. ID3D11Texture2D *surface = mTargets[Color0];
  140. if ( surface )
  141. {
  142. D3D11_TEXTURE2D_DESC sd;
  143. surface->GetDesc(&sd);
  144. mTargetSize = Point2I(sd.Width, sd.Height);
  145. S32 format = sd.Format;
  146. if (format == DXGI_FORMAT_R8G8B8A8_TYPELESS || format == DXGI_FORMAT_B8G8R8A8_TYPELESS)
  147. {
  148. mTargetFormat = GFXFormatR8G8B8A8;
  149. return;
  150. }
  151. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  152. mTargetFormat = (GFXFormat)format;
  153. }
  154. }
  155. }
  156. }
  157. void GFXD3D11TextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel/*=0*/ )
  158. {
  159. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_attachTexture_Cubemap, ColorI::RED );
  160. AssertFatal(slot < MaxRenderSlotId, "GFXD3D11TextureTarget::attachTexture - out of range slot.");
  161. // Mark state as dirty so device can know to update.
  162. invalidateState();
  163. SAFE_RELEASE(mTargetViews[slot]);
  164. SAFE_RELEASE(mTargets[slot]);
  165. SAFE_RELEASE(mTargetSRViews[slot]);
  166. mResolveTargets[slot] = NULL;
  167. mGenMips = false;
  168. // Cast the texture object to D3D...
  169. AssertFatal(!tex || static_cast<GFXD3D11Cubemap*>(tex), "GFXD3DTextureTarget::attachTexture - invalid cubemap object.");
  170. if(slot == Color0)
  171. {
  172. mTargetSize = Point2I::Zero;
  173. mTargetFormat = GFXFormatR8G8B8A8;
  174. }
  175. // Are we clearing?
  176. if(!tex)
  177. {
  178. // Yup - just exit, it'll stay NULL.
  179. return;
  180. }
  181. GFXD3D11Cubemap *cube = static_cast<GFXD3D11Cubemap*>(tex);
  182. // Grab the surface level.
  183. mTargets[slot] = cube->get2DTex();
  184. mTargets[slot]->AddRef();
  185. mTargetViews[slot] = cube->getRTView(face);
  186. mTargetViews[slot]->AddRef();
  187. /*mTargetSRViews[slot] = cube->getSRView();
  188. mTargetSRViews[slot]->AddRef();*/
  189. // Update surface size
  190. if(slot == Color0)
  191. {
  192. ID3D11Texture2D *surface = mTargets[Color0];
  193. if ( surface )
  194. {
  195. D3D11_TEXTURE2D_DESC sd;
  196. surface->GetDesc(&sd);
  197. mTargetSize = Point2I(sd.Width, sd.Height);
  198. S32 format = sd.Format;
  199. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  200. mTargetFormat = (GFXFormat)format;
  201. }
  202. }
  203. }
  204. void GFXD3D11TextureTarget::activate()
  205. {
  206. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_activate, ColorI::RED );
  207. AssertFatal( mTargets[GFXTextureTarget::Color0], "GFXD3D11TextureTarget::activate() - You can never have a NULL primary render target!" );
  208. // Clear the state indicator.
  209. stateApplied();
  210. // Now set all the new surfaces into the appropriate slots.
  211. ID3D11RenderTargetView* rtViews[MaxRenderSlotId] = { NULL, NULL, NULL, NULL, NULL, NULL };
  212. ID3D11DepthStencilView* dsView = (ID3D11DepthStencilView*)(mTargetViews[GFXTextureTarget::DepthStencil]);
  213. for (U32 i = 0; i < 6; i++)
  214. {
  215. rtViews[i] = (ID3D11RenderTargetView*)mTargetViews[GFXTextureTarget::Color0 + i];
  216. }
  217. D3D11DEVICECONTEXT->OMSetRenderTargets(MaxRenderSlotId, rtViews, dsView);
  218. }
  219. void GFXD3D11TextureTarget::deactivate()
  220. {
  221. if (!mGenMips)
  222. return;
  223. //re-gen mip maps
  224. for (U32 i = GFXTextureTarget::Color0; i < GFXTextureTarget::MaxRenderSlotId; i++)
  225. {
  226. GFXD3D11TextureObject* targ = mResolveTargets[i];
  227. ID3D11ShaderResourceView* pSRView = mTargetSRViews[i];
  228. if (targ && targ->getSurface() && pSRView)
  229. {
  230. ID3D11Texture2D* tex = dynamic_cast<ID3D11Texture2D*>(targ->getResource());
  231. if (tex)
  232. {
  233. D3D11_TEXTURE2D_DESC desc;
  234. tex->GetDesc(&desc);
  235. if (desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
  236. {
  237. D3D11DEVICECONTEXT->GenerateMips(pSRView);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. void GFXD3D11TextureTarget::resolve()
  244. {
  245. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_resolve, ColorI::RED );
  246. for (U32 i = 0; i < MaxRenderSlotId; i++)
  247. {
  248. // We use existance @ mResolveTargets as a flag that we need to copy
  249. // data from the rendertarget into the texture.
  250. if (mResolveTargets[i])
  251. {
  252. D3D11_TEXTURE2D_DESC desc;
  253. mTargets[i]->GetDesc(&desc);
  254. D3D11DEVICECONTEXT->CopySubresourceRegion(mResolveTargets[i]->get2DTex(), 0, 0, 0, 0, mTargets[i], 0, NULL);
  255. }
  256. }
  257. }
  258. void GFXD3D11TextureTarget::resolveTo( GFXTextureObject *tex )
  259. {
  260. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_resolveTo, ColorI::RED );
  261. if ( mTargets[Color0] == NULL )
  262. return;
  263. D3D11_TEXTURE2D_DESC desc;
  264. mTargets[Color0]->GetDesc(&desc);
  265. D3D11DEVICECONTEXT->CopySubresourceRegion(((GFXD3D11TextureObject*)(tex))->get2DTex(), 0, 0, 0, 0, mTargets[Color0], 0, NULL);
  266. }
  267. void GFXD3D11TextureTarget::zombify()
  268. {
  269. for(U32 i = 0; i < MaxRenderSlotId; i++)
  270. attachTexture(RenderSlot(i), NULL);
  271. }
  272. void GFXD3D11TextureTarget::resurrect()
  273. {
  274. }
  275. GFXD3D11WindowTarget::GFXD3D11WindowTarget()
  276. {
  277. mWindow = NULL;
  278. mBackBuffer = NULL;
  279. mDepthStencilView = NULL;
  280. mDepthStencil = NULL;
  281. mBackBufferView = NULL;
  282. mSwapChain = NULL;
  283. dMemset(&mPresentationParams, 0, sizeof(mPresentationParams));
  284. mSecondaryWindow = false;
  285. }
  286. GFXD3D11WindowTarget::~GFXD3D11WindowTarget()
  287. {
  288. SAFE_RELEASE(mDepthStencilView);
  289. SAFE_RELEASE(mDepthStencil);
  290. SAFE_RELEASE(mBackBufferView);
  291. SAFE_RELEASE(mBackBuffer);
  292. SAFE_RELEASE(mSwapChain);
  293. }
  294. void GFXD3D11WindowTarget::initPresentationParams()
  295. {
  296. // Get some video mode related info.
  297. const GFXVideoMode& vm = mWindow->getVideoMode();
  298. HWND hwnd = (HWND)mWindow->getSystemWindow(PlatformWindow::WindowSystem_Windows);
  299. // Do some validation...
  300. if (vm.fullScreen && mSecondaryWindow)
  301. {
  302. AssertFatal(false, "GFXD3D11WindowTarget::initPresentationParams - Cannot go fullscreen with secondary window!");
  303. }
  304. mPresentationParams = D3D11->setupPresentParams(vm, hwnd);
  305. }
  306. const Point2I GFXD3D11WindowTarget::getSize()
  307. {
  308. return mWindow->getVideoMode().resolution;
  309. }
  310. GFXFormat GFXD3D11WindowTarget::getFormat()
  311. {
  312. S32 format = mPresentationParams.BufferDesc.Format;
  313. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  314. return (GFXFormat)format;
  315. }
  316. bool GFXD3D11WindowTarget::present()
  317. {
  318. HRESULT hr = mSwapChain->Present(D3D11->smEnableVSync, 0);
  319. if (hr == DXGI_ERROR_DEVICE_REMOVED)
  320. {
  321. HRESULT result = D3D11->getDevice()->GetDeviceRemovedReason();
  322. if (result == DXGI_ERROR_DEVICE_HUNG)
  323. AssertFatal(false,"DXGI_ERROR_DEVICE_HUNG");
  324. else if (result == DXGI_ERROR_DEVICE_REMOVED)
  325. AssertFatal(false, "DXGI_ERROR_DEVICE_REMOVED");
  326. else if (result == DXGI_ERROR_DEVICE_RESET)
  327. AssertFatal(false, "DXGI_ERROR_DEVICE_RESET");
  328. else if (result == DXGI_ERROR_DRIVER_INTERNAL_ERROR)
  329. AssertFatal(false, "DXGI_ERROR_DRIVER_INTERNAL_ERROR");
  330. else if (result == DXGI_ERROR_INVALID_CALL)
  331. AssertFatal(false, "DXGI_ERROR_INVALID_CALL");
  332. }
  333. return (hr == S_OK);
  334. }
  335. void GFXD3D11WindowTarget::createSwapChain()
  336. {
  337. //create dxgi factory & swapchain
  338. IDXGIFactory1* DXGIFactory;
  339. HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&DXGIFactory));
  340. if (FAILED(hr))
  341. AssertFatal(false, "GFXD3D11WindowTarget::createSwapChain - couldn't create dxgi factory.");
  342. hr = DXGIFactory->CreateSwapChain(D3D11DEVICE, &mPresentationParams, &mSwapChain);
  343. if (FAILED(hr))
  344. AssertFatal(false, "GFXD3D11WindowTarget::createSwapChain - couldn't create swap chain.");
  345. SAFE_RELEASE(DXGIFactory);
  346. }
  347. void GFXD3D11WindowTarget::createBuffersAndViews()
  348. {
  349. //release old if they exist
  350. SAFE_RELEASE(mDepthStencilView);
  351. SAFE_RELEASE(mDepthStencil);
  352. SAFE_RELEASE(mBackBufferView);
  353. SAFE_RELEASE(mBackBuffer);
  354. //grab video mode
  355. const GFXVideoMode& vm = mWindow->getVideoMode();
  356. //create depth/stencil
  357. D3D11_TEXTURE2D_DESC desc;
  358. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  359. desc.CPUAccessFlags = 0;
  360. desc.Format = GFXD3D11TextureFormat[GFXFormatD24S8];
  361. desc.MipLevels = 1;
  362. desc.ArraySize = 1;
  363. desc.Usage = D3D11_USAGE_DEFAULT;
  364. desc.Width = vm.resolution.x;
  365. desc.Height = vm.resolution.y;
  366. desc.SampleDesc.Count = 1;
  367. desc.SampleDesc.Quality = 0;
  368. desc.MiscFlags = 0;
  369. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &mDepthStencil);
  370. if (FAILED(hr))
  371. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create device's depth-stencil surface.");
  372. D3D11_DEPTH_STENCIL_VIEW_DESC depthDesc;
  373. depthDesc.Format = GFXD3D11TextureFormat[GFXFormatD24S8];
  374. depthDesc.Flags = 0;
  375. depthDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  376. depthDesc.Texture2D.MipSlice = 0;
  377. hr = D3D11DEVICE->CreateDepthStencilView(mDepthStencil, &depthDesc, &mDepthStencilView);
  378. if (FAILED(hr))
  379. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create depth stencil view");
  380. setBackBuffer();
  381. //create back buffer view
  382. D3D11_RENDER_TARGET_VIEW_DESC RTDesc;
  383. RTDesc.Format = GFXD3D11TextureFormat[GFXFormatR8G8B8A8_SRGB];
  384. RTDesc.Texture2D.MipSlice = 0;
  385. RTDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  386. hr = D3D11DEVICE->CreateRenderTargetView(mBackBuffer, &RTDesc, &mBackBufferView);
  387. if (FAILED(hr))
  388. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create back buffer target view");
  389. //debug names
  390. #ifdef TORQUE_DEBUG
  391. if (!mSecondaryWindow)
  392. {
  393. String backBufferName = "MainBackBuffer";
  394. String depthSteniclName = "MainDepthStencil";
  395. String backBuffViewName = "MainBackBuffView";
  396. String depthStencViewName = "MainDepthView";
  397. mBackBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, backBufferName.size(), backBufferName.c_str());
  398. mDepthStencil->SetPrivateData(WKPDID_D3DDebugObjectName, depthSteniclName.size(), depthSteniclName.c_str());
  399. mDepthStencilView->SetPrivateData(WKPDID_D3DDebugObjectName, depthStencViewName.size(), depthStencViewName.c_str());
  400. mBackBufferView->SetPrivateData(WKPDID_D3DDebugObjectName, backBuffViewName.size(), backBuffViewName.c_str());
  401. }
  402. #endif
  403. }
  404. void GFXD3D11WindowTarget::resetMode()
  405. {
  406. HRESULT hr;
  407. if (mSwapChain)
  408. {
  409. // The current video settings.
  410. DXGI_SWAP_CHAIN_DESC desc;
  411. hr = mSwapChain->GetDesc(&desc);
  412. if (FAILED(hr))
  413. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to get swap chain description!");
  414. bool fullscreen = !desc.Windowed;
  415. Point2I backbufferSize(desc.BufferDesc.Width, desc.BufferDesc.Height);
  416. // The settings we are now applying.
  417. const GFXVideoMode& vm = mWindow->getVideoMode();
  418. // Early out if none of the settings which require a device reset
  419. // have changed.
  420. if (backbufferSize == vm.resolution &&
  421. fullscreen == vm.fullScreen)
  422. return;
  423. }
  424. //release old buffers and views
  425. SAFE_RELEASE(mDepthStencilView)
  426. SAFE_RELEASE(mDepthStencil);
  427. SAFE_RELEASE(mBackBufferView);
  428. SAFE_RELEASE(mBackBuffer);
  429. if (!mSecondaryWindow)
  430. D3D11->beginReset();
  431. mWindow->setSuppressReset(true);
  432. // Setup our presentation params.
  433. initPresentationParams();
  434. if (!mPresentationParams.Windowed)
  435. {
  436. mPresentationParams.BufferDesc.RefreshRate.Numerator = 0;
  437. mPresentationParams.BufferDesc.RefreshRate.Denominator = 0;
  438. hr = mSwapChain->ResizeTarget(&mPresentationParams.BufferDesc);
  439. if (FAILED(hr))
  440. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to resize target!");
  441. }
  442. hr = mSwapChain->ResizeBuffers(mPresentationParams.BufferCount, mPresentationParams.BufferDesc.Width, mPresentationParams.BufferDesc.Height,
  443. mPresentationParams.BufferDesc.Format, mPresentationParams.Windowed ? 0 : DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  444. if (FAILED(hr))
  445. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to resize back buffer!");
  446. hr = mSwapChain->SetFullscreenState(!mPresentationParams.Windowed, NULL);
  447. if (FAILED(hr))
  448. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to change screen states!");
  449. // Update our size, too.
  450. mSize = Point2I(mPresentationParams.BufferDesc.Width, mPresentationParams.BufferDesc.Height);
  451. mWindow->setSuppressReset(false);
  452. //re-create buffers and views
  453. createBuffersAndViews();
  454. if (!mSecondaryWindow)
  455. D3D11->endReset(this);
  456. }
  457. void GFXD3D11WindowTarget::zombify()
  458. {
  459. SAFE_RELEASE(mBackBuffer);
  460. }
  461. void GFXD3D11WindowTarget::resurrect()
  462. {
  463. setBackBuffer();
  464. }
  465. void GFXD3D11WindowTarget::setBackBuffer()
  466. {
  467. if (!mBackBuffer)
  468. mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)& mBackBuffer);
  469. }
  470. void GFXD3D11WindowTarget::activate()
  471. {
  472. GFXDEBUGEVENT_SCOPE(GFXPCD3D11WindowTarget_activate, ColorI::RED);
  473. //clear ther rendertargets first
  474. ID3D11RenderTargetView* rtViews[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  475. D3D11DEVICECONTEXT->OMSetRenderTargets(8, rtViews, NULL);
  476. D3D11DEVICECONTEXT->OMSetRenderTargets(1, &mBackBufferView, mDepthStencilView);
  477. DXGI_SWAP_CHAIN_DESC pp;
  478. mSwapChain->GetDesc(&pp);
  479. // Update our video mode here, too.
  480. GFXVideoMode vm;
  481. vm = mWindow->getVideoMode();
  482. vm.resolution.x = pp.BufferDesc.Width;
  483. vm.resolution.y = pp.BufferDesc.Height;
  484. vm.fullScreen = !pp.Windowed;
  485. mSize = vm.resolution;
  486. }
  487. void GFXD3D11WindowTarget::resolveTo(GFXTextureObject *tex)
  488. {
  489. GFXDEBUGEVENT_SCOPE(GFXPCD3D11WindowTarget_resolveTo, ColorI::RED);
  490. D3D11_TEXTURE2D_DESC desc;
  491. ID3D11Texture2D* surf = ((GFXD3D11TextureObject*)(tex))->get2DTex();
  492. surf->GetDesc(&desc);
  493. D3D11DEVICECONTEXT->ResolveSubresource(surf, 0, mBackBuffer, 0, desc.Format);
  494. }
  495. IDXGISwapChain* GFXD3D11WindowTarget::getSwapChain()
  496. {
  497. mSwapChain->AddRef();
  498. return mSwapChain;
  499. }
  500. ID3D11Texture2D* GFXD3D11WindowTarget::getBackBuffer()
  501. {
  502. mBackBuffer->AddRef();
  503. return mBackBuffer;
  504. }
  505. ID3D11Texture2D* GFXD3D11WindowTarget::getDepthStencil()
  506. {
  507. mDepthStencil->AddRef();
  508. return mDepthStencil;
  509. }
  510. ID3D11RenderTargetView* GFXD3D11WindowTarget::getBackBufferView()
  511. {
  512. mBackBufferView->AddRef();
  513. return mBackBufferView;
  514. }
  515. ID3D11DepthStencilView* GFXD3D11WindowTarget::getDepthStencilView()
  516. {
  517. mDepthStencilView->AddRef();
  518. return mDepthStencilView;
  519. }