gfxD3D11Target.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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(static_cast<GFXD3D11TextureObject*>(tex), "GFXD3D11TextureTarget::attachTexture - invalid texture object.");
  95. GFXD3D11TextureObject *d3dto = static_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. // Only assign resolve target if d3dto has a surface to give us.
  124. //
  125. // That usually means there is an MSAA target involved, which is why
  126. // the resolve is needed to get the data out of the target.
  127. mResolveTargets[slot] = d3dto;
  128. if ( tex && slot == Color0 )
  129. {
  130. mTargetSize.set( tex->getSize().x, tex->getSize().y );
  131. mTargetFormat = tex->getFormat();
  132. }
  133. }
  134. }
  135. // Update surface size
  136. if(slot == Color0)
  137. {
  138. ID3D11Texture2D *surface = mTargets[Color0];
  139. if ( surface )
  140. {
  141. D3D11_TEXTURE2D_DESC sd;
  142. surface->GetDesc(&sd);
  143. mTargetSize = Point2I(sd.Width, sd.Height);
  144. S32 format = sd.Format;
  145. if (format == DXGI_FORMAT_R8G8B8A8_TYPELESS || format == DXGI_FORMAT_B8G8R8A8_TYPELESS)
  146. {
  147. mTargetFormat = GFXFormatR8G8B8A8;
  148. return;
  149. }
  150. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  151. mTargetFormat = (GFXFormat)format;
  152. }
  153. }
  154. }
  155. }
  156. void GFXD3D11TextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32 face, U32 mipLevel/*=0*/ )
  157. {
  158. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_attachTexture_Cubemap, ColorI::RED );
  159. AssertFatal(slot < MaxRenderSlotId, "GFXD3D11TextureTarget::attachTexture - out of range slot.");
  160. // Mark state as dirty so device can know to update.
  161. invalidateState();
  162. SAFE_RELEASE(mTargetViews[slot]);
  163. SAFE_RELEASE(mTargets[slot]);
  164. SAFE_RELEASE(mTargetSRViews[slot]);
  165. mResolveTargets[slot] = NULL;
  166. mGenMips = false;
  167. // Cast the texture object to D3D...
  168. AssertFatal(!tex || static_cast<GFXD3D11Cubemap*>(tex), "GFXD3DTextureTarget::attachTexture - invalid cubemap object.");
  169. if(slot == Color0)
  170. {
  171. mTargetSize = Point2I::Zero;
  172. mTargetFormat = GFXFormatR8G8B8A8;
  173. }
  174. // Are we clearing?
  175. if(!tex)
  176. {
  177. // Yup - just exit, it'll stay NULL.
  178. return;
  179. }
  180. GFXD3D11Cubemap *cube = static_cast<GFXD3D11Cubemap*>(tex);
  181. // Grab the surface level.
  182. mTargets[slot] = cube->get2DTex();
  183. mTargets[slot]->AddRef();
  184. mTargetViews[slot] = cube->getRTView(face);
  185. mTargetViews[slot]->AddRef();
  186. /*mTargetSRViews[slot] = cube->getSRView();
  187. mTargetSRViews[slot]->AddRef();*/
  188. // Update surface size
  189. if(slot == Color0)
  190. {
  191. ID3D11Texture2D *surface = mTargets[Color0];
  192. if ( surface )
  193. {
  194. D3D11_TEXTURE2D_DESC sd;
  195. surface->GetDesc(&sd);
  196. mTargetSize = Point2I(sd.Width, sd.Height);
  197. S32 format = sd.Format;
  198. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  199. mTargetFormat = (GFXFormat)format;
  200. }
  201. }
  202. }
  203. void GFXD3D11TextureTarget::activate()
  204. {
  205. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_activate, ColorI::RED );
  206. AssertFatal( mTargets[GFXTextureTarget::Color0], "GFXD3D11TextureTarget::activate() - You can never have a NULL primary render target!" );
  207. // Clear the state indicator.
  208. stateApplied();
  209. // Now set all the new surfaces into the appropriate slots.
  210. ID3D11RenderTargetView* rtViews[MaxRenderSlotId] = { NULL, NULL, NULL, NULL, NULL, NULL };
  211. ID3D11DepthStencilView* dsView = (ID3D11DepthStencilView*)(mTargetViews[GFXTextureTarget::DepthStencil]);
  212. for (U32 i = 0; i < 6; i++)
  213. {
  214. rtViews[i] = (ID3D11RenderTargetView*)mTargetViews[GFXTextureTarget::Color0 + i];
  215. }
  216. D3D11DEVICECONTEXT->OMSetRenderTargets(MaxRenderSlotId, rtViews, dsView);
  217. }
  218. void GFXD3D11TextureTarget::deactivate()
  219. {
  220. if (!mGenMips)
  221. return;
  222. //re-gen mip maps
  223. for (U32 i = 0; i < 6; i++)
  224. {
  225. ID3D11ShaderResourceView* pSRView = mTargetSRViews[GFXTextureTarget::Color0 + i];
  226. if (pSRView)
  227. D3D11DEVICECONTEXT->GenerateMips(pSRView);
  228. }
  229. }
  230. void GFXD3D11TextureTarget::resolve()
  231. {
  232. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_resolve, ColorI::RED );
  233. for (U32 i = 0; i < MaxRenderSlotId; i++)
  234. {
  235. // We use existance @ mResolveTargets as a flag that we need to copy
  236. // data from the rendertarget into the texture.
  237. if (mResolveTargets[i])
  238. {
  239. D3D11_TEXTURE2D_DESC desc;
  240. mTargets[i]->GetDesc(&desc);
  241. D3D11DEVICECONTEXT->CopySubresourceRegion(mResolveTargets[i]->get2DTex(), 0, 0, 0, 0, mTargets[i], 0, NULL);
  242. }
  243. }
  244. }
  245. void GFXD3D11TextureTarget::resolveTo( GFXTextureObject *tex )
  246. {
  247. GFXDEBUGEVENT_SCOPE( GFXPCD3D11TextureTarget_resolveTo, ColorI::RED );
  248. if ( mTargets[Color0] == NULL )
  249. return;
  250. D3D11_TEXTURE2D_DESC desc;
  251. mTargets[Color0]->GetDesc(&desc);
  252. D3D11DEVICECONTEXT->CopySubresourceRegion(((GFXD3D11TextureObject*)(tex))->get2DTex(), 0, 0, 0, 0, mTargets[Color0], 0, NULL);
  253. }
  254. void GFXD3D11TextureTarget::zombify()
  255. {
  256. for(U32 i = 0; i < MaxRenderSlotId; i++)
  257. attachTexture(RenderSlot(i), NULL);
  258. }
  259. void GFXD3D11TextureTarget::resurrect()
  260. {
  261. }
  262. GFXD3D11WindowTarget::GFXD3D11WindowTarget()
  263. {
  264. mWindow = NULL;
  265. mBackBuffer = NULL;
  266. mDepthStencilView = NULL;
  267. mDepthStencil = NULL;
  268. mBackBufferView = NULL;
  269. mSwapChain = NULL;
  270. dMemset(&mPresentationParams, 0, sizeof(mPresentationParams));
  271. mSecondaryWindow = false;
  272. }
  273. GFXD3D11WindowTarget::~GFXD3D11WindowTarget()
  274. {
  275. SAFE_RELEASE(mDepthStencilView)
  276. SAFE_RELEASE(mDepthStencil);
  277. SAFE_RELEASE(mBackBufferView);
  278. SAFE_RELEASE(mBackBuffer);
  279. SAFE_RELEASE(mSwapChain);
  280. }
  281. void GFXD3D11WindowTarget::initPresentationParams()
  282. {
  283. // Get some video mode related info.
  284. const GFXVideoMode& vm = mWindow->getVideoMode();
  285. HWND hwnd = (HWND)mWindow->getSystemWindow(PlatformWindow::WindowSystem_Windows);
  286. // Do some validation...
  287. if (vm.fullScreen && mSecondaryWindow)
  288. {
  289. AssertFatal(false, "GFXD3D11WindowTarget::initPresentationParams - Cannot go fullscreen with secondary window!");
  290. }
  291. mPresentationParams = D3D11->setupPresentParams(vm, hwnd);
  292. }
  293. const Point2I GFXD3D11WindowTarget::getSize()
  294. {
  295. return mWindow->getVideoMode().resolution;
  296. }
  297. GFXFormat GFXD3D11WindowTarget::getFormat()
  298. {
  299. S32 format = mPresentationParams.BufferDesc.Format;
  300. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, format );
  301. return (GFXFormat)format;
  302. }
  303. bool GFXD3D11WindowTarget::present()
  304. {
  305. HRESULT hr = mSwapChain->Present(D3D11->smEnableVSync, 0);
  306. if (hr == DXGI_ERROR_DEVICE_REMOVED)
  307. {
  308. HRESULT result = D3D11->getDevice()->GetDeviceRemovedReason();
  309. if (result == DXGI_ERROR_DEVICE_HUNG)
  310. AssertFatal(false,"DXGI_ERROR_DEVICE_HUNG");
  311. else if (result == DXGI_ERROR_DEVICE_REMOVED)
  312. AssertFatal(false, "DXGI_ERROR_DEVICE_REMOVED");
  313. else if (result == DXGI_ERROR_DEVICE_RESET)
  314. AssertFatal(false, "DXGI_ERROR_DEVICE_RESET");
  315. else if (result == DXGI_ERROR_DRIVER_INTERNAL_ERROR)
  316. AssertFatal(false, "DXGI_ERROR_DRIVER_INTERNAL_ERROR");
  317. else if (result == DXGI_ERROR_INVALID_CALL)
  318. AssertFatal(false, "DXGI_ERROR_INVALID_CALL");
  319. }
  320. return (hr == S_OK);
  321. }
  322. void GFXD3D11WindowTarget::createSwapChain()
  323. {
  324. //create dxgi factory & swapchain
  325. IDXGIFactory1* DXGIFactory;
  326. HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&DXGIFactory));
  327. if (FAILED(hr))
  328. AssertFatal(false, "GFXD3D11WindowTarget::createSwapChain - couldn't create dxgi factory.");
  329. hr = DXGIFactory->CreateSwapChain(D3D11DEVICE, &mPresentationParams, &mSwapChain);
  330. if (FAILED(hr))
  331. AssertFatal(false, "GFXD3D11WindowTarget::createSwapChain - couldn't create swap chain.");
  332. SAFE_RELEASE(DXGIFactory);
  333. }
  334. void GFXD3D11WindowTarget::createBuffersAndViews()
  335. {
  336. //release old if they exist
  337. SAFE_RELEASE(mDepthStencilView);
  338. SAFE_RELEASE(mDepthStencil);
  339. SAFE_RELEASE(mBackBufferView);
  340. SAFE_RELEASE(mBackBuffer);
  341. //grab video mode
  342. const GFXVideoMode& vm = mWindow->getVideoMode();
  343. //create depth/stencil
  344. D3D11_TEXTURE2D_DESC desc;
  345. desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  346. desc.CPUAccessFlags = 0;
  347. desc.Format = GFXD3D11TextureFormat[GFXFormatD24S8];
  348. desc.MipLevels = 1;
  349. desc.ArraySize = 1;
  350. desc.Usage = D3D11_USAGE_DEFAULT;
  351. desc.Width = vm.resolution.x;
  352. desc.Height = vm.resolution.y;
  353. desc.SampleDesc.Count = 1;
  354. desc.SampleDesc.Quality = 0;
  355. desc.MiscFlags = 0;
  356. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &mDepthStencil);
  357. if (FAILED(hr))
  358. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create device's depth-stencil surface.");
  359. D3D11_DEPTH_STENCIL_VIEW_DESC depthDesc;
  360. depthDesc.Format = GFXD3D11TextureFormat[GFXFormatD24S8];
  361. depthDesc.Flags = 0;
  362. depthDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  363. depthDesc.Texture2D.MipSlice = 0;
  364. hr = D3D11DEVICE->CreateDepthStencilView(mDepthStencil, &depthDesc, &mDepthStencilView);
  365. if (FAILED(hr))
  366. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create depth stencil view");
  367. setBackBuffer();
  368. //create back buffer view
  369. D3D11_RENDER_TARGET_VIEW_DESC RTDesc;
  370. RTDesc.Format = GFXD3D11TextureFormat[GFXFormatR8G8B8A8_SRGB];
  371. RTDesc.Texture2D.MipSlice = 0;
  372. RTDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  373. hr = D3D11DEVICE->CreateRenderTargetView(mBackBuffer, &RTDesc, &mBackBufferView);
  374. if (FAILED(hr))
  375. AssertFatal(false, "GFXD3D11WindowTarget::createBuffersAndViews - couldn't create back buffer target view");
  376. //debug names
  377. #ifdef TORQUE_DEBUG
  378. if (!mSecondaryWindow)
  379. {
  380. String backBufferName = "MainBackBuffer";
  381. String depthSteniclName = "MainDepthStencil";
  382. String backBuffViewName = "MainBackBuffView";
  383. String depthStencViewName = "MainDepthView";
  384. mBackBuffer->SetPrivateData(WKPDID_D3DDebugObjectName, backBufferName.size(), backBufferName.c_str());
  385. mDepthStencil->SetPrivateData(WKPDID_D3DDebugObjectName, depthSteniclName.size(), depthSteniclName.c_str());
  386. mDepthStencilView->SetPrivateData(WKPDID_D3DDebugObjectName, depthStencViewName.size(), depthStencViewName.c_str());
  387. mBackBufferView->SetPrivateData(WKPDID_D3DDebugObjectName, backBuffViewName.size(), backBuffViewName.c_str());
  388. }
  389. #endif
  390. }
  391. void GFXD3D11WindowTarget::resetMode()
  392. {
  393. HRESULT hr;
  394. if (mSwapChain)
  395. {
  396. // The current video settings.
  397. DXGI_SWAP_CHAIN_DESC desc;
  398. hr = mSwapChain->GetDesc(&desc);
  399. if (FAILED(hr))
  400. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to get swap chain description!");
  401. bool fullscreen = !desc.Windowed;
  402. Point2I backbufferSize(desc.BufferDesc.Width, desc.BufferDesc.Height);
  403. // The settings we are now applying.
  404. const GFXVideoMode& vm = mWindow->getVideoMode();
  405. // Early out if none of the settings which require a device reset
  406. // have changed.
  407. if (backbufferSize == vm.resolution &&
  408. fullscreen == vm.fullScreen)
  409. return;
  410. }
  411. //release old buffers and views
  412. SAFE_RELEASE(mDepthStencilView)
  413. SAFE_RELEASE(mDepthStencil);
  414. SAFE_RELEASE(mBackBufferView);
  415. SAFE_RELEASE(mBackBuffer);
  416. if (!mSecondaryWindow)
  417. D3D11->beginReset();
  418. mWindow->setSuppressReset(true);
  419. // Setup our presentation params.
  420. initPresentationParams();
  421. if (!mPresentationParams.Windowed)
  422. {
  423. mPresentationParams.BufferDesc.RefreshRate.Numerator = 0;
  424. mPresentationParams.BufferDesc.RefreshRate.Denominator = 0;
  425. hr = mSwapChain->ResizeTarget(&mPresentationParams.BufferDesc);
  426. if (FAILED(hr))
  427. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to resize target!");
  428. }
  429. hr = mSwapChain->ResizeBuffers(mPresentationParams.BufferCount, mPresentationParams.BufferDesc.Width, mPresentationParams.BufferDesc.Height,
  430. mPresentationParams.BufferDesc.Format, mPresentationParams.Windowed ? 0 : DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  431. if (FAILED(hr))
  432. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to resize back buffer!");
  433. hr = mSwapChain->SetFullscreenState(!mPresentationParams.Windowed, NULL);
  434. if (FAILED(hr))
  435. AssertFatal(false, "GFXD3D11WindowTarget::resetMode - failed to change screen states!");
  436. // Update our size, too.
  437. mSize = Point2I(mPresentationParams.BufferDesc.Width, mPresentationParams.BufferDesc.Height);
  438. mWindow->setSuppressReset(false);
  439. //re-create buffers and views
  440. createBuffersAndViews();
  441. if (!mSecondaryWindow)
  442. D3D11->endReset(this);
  443. }
  444. void GFXD3D11WindowTarget::zombify()
  445. {
  446. SAFE_RELEASE(mBackBuffer);
  447. }
  448. void GFXD3D11WindowTarget::resurrect()
  449. {
  450. setBackBuffer();
  451. }
  452. void GFXD3D11WindowTarget::setBackBuffer()
  453. {
  454. if (!mBackBuffer)
  455. mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)& mBackBuffer);
  456. }
  457. void GFXD3D11WindowTarget::activate()
  458. {
  459. GFXDEBUGEVENT_SCOPE(GFXPCD3D11WindowTarget_activate, ColorI::RED);
  460. //clear ther rendertargets first
  461. ID3D11RenderTargetView* rtViews[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  462. D3D11DEVICECONTEXT->OMSetRenderTargets(8, rtViews, NULL);
  463. D3D11DEVICECONTEXT->OMSetRenderTargets(1, &mBackBufferView, mDepthStencilView);
  464. DXGI_SWAP_CHAIN_DESC pp;
  465. mSwapChain->GetDesc(&pp);
  466. // Update our video mode here, too.
  467. GFXVideoMode vm;
  468. vm = mWindow->getVideoMode();
  469. vm.resolution.x = pp.BufferDesc.Width;
  470. vm.resolution.y = pp.BufferDesc.Height;
  471. vm.fullScreen = !pp.Windowed;
  472. mSize = vm.resolution;
  473. }
  474. void GFXD3D11WindowTarget::resolveTo(GFXTextureObject *tex)
  475. {
  476. GFXDEBUGEVENT_SCOPE(GFXPCD3D11WindowTarget_resolveTo, ColorI::RED);
  477. D3D11_TEXTURE2D_DESC desc;
  478. ID3D11Texture2D* surf = ((GFXD3D11TextureObject*)(tex))->get2DTex();
  479. surf->GetDesc(&desc);
  480. D3D11DEVICECONTEXT->ResolveSubresource(surf, 0, mBackBuffer, 0, desc.Format);
  481. }
  482. IDXGISwapChain* GFXD3D11WindowTarget::getSwapChain()
  483. {
  484. mSwapChain->AddRef();
  485. return mSwapChain;
  486. }
  487. ID3D11Texture2D* GFXD3D11WindowTarget::getBackBuffer()
  488. {
  489. mBackBuffer->AddRef();
  490. return mBackBuffer;
  491. }
  492. ID3D11Texture2D* GFXD3D11WindowTarget::getDepthStencil()
  493. {
  494. mDepthStencil->AddRef();
  495. return mDepthStencil;
  496. }
  497. ID3D11RenderTargetView* GFXD3D11WindowTarget::getBackBufferView()
  498. {
  499. mBackBufferView->AddRef();
  500. return mBackBufferView;
  501. }
  502. ID3D11DepthStencilView* GFXD3D11WindowTarget::getDepthStencilView()
  503. {
  504. mDepthStencilView->AddRef();
  505. return mDepthStencilView;
  506. }