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