gfxD3D11TextureManager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 "gfx/D3D11/gfxD3D11Device.h"
  23. #include "gfx/D3D11/gfxD3D11EnumTranslate.h"
  24. #include "gfx/bitmap/bitmapUtils.h"
  25. #include "gfx/bitmap/imageUtils.h"
  26. #include "gfx/gfxCardProfile.h"
  27. #include "gfx/gfxStringEnumTranslate.h"
  28. #include "core/strings/unicode.h"
  29. #include "core/util/swizzle.h"
  30. #include "core/util/safeDelete.h"
  31. #include "console/console.h"
  32. #include "core/resourceManager.h"
  33. GFXD3D11TextureManager::GFXD3D11TextureManager()
  34. {
  35. ZeroMemory(mCurTexSet, sizeof(mCurTexSet));
  36. }
  37. GFXD3D11TextureManager::~GFXD3D11TextureManager()
  38. {
  39. // Destroy texture table now so just in case some texture objects
  40. // are still left, we don't crash on a pure virtual method call.
  41. SAFE_DELETE_ARRAY( mHashTable );
  42. }
  43. void GFXD3D11TextureManager::_innerCreateTexture( GFXD3D11TextureObject *retTex,
  44. U32 height,
  45. U32 width,
  46. U32 depth,
  47. GFXFormat format,
  48. GFXTextureProfile *profile,
  49. U32 numMipLevels,
  50. bool forceMips,
  51. S32 antialiasLevel)
  52. {
  53. U32 usage = 0;
  54. U32 bindFlags = 0;
  55. U32 miscFlags = 0;
  56. if(!retTex->mProfile->isZTarget() && !retTex->mProfile->isSystemMemory())
  57. bindFlags = D3D11_BIND_SHADER_RESOURCE;
  58. U32 cpuFlags = 0;
  59. retTex->mProfile = profile;
  60. retTex->isManaged = false;
  61. DXGI_FORMAT d3dTextureFormat = GFXD3D11TextureFormat[format];
  62. if( retTex->mProfile->isDynamic() )
  63. {
  64. usage = D3D11_USAGE_DYNAMIC;
  65. cpuFlags |= D3D11_CPU_ACCESS_WRITE;
  66. retTex->isManaged = false;
  67. }
  68. else if ( retTex->mProfile->isSystemMemory() )
  69. {
  70. usage |= D3D11_USAGE_STAGING;
  71. cpuFlags |= D3D11_CPU_ACCESS_READ;
  72. }
  73. else
  74. {
  75. usage = D3D11_USAGE_DEFAULT;
  76. retTex->isManaged = true;
  77. }
  78. if( retTex->mProfile->isRenderTarget() )
  79. {
  80. bindFlags |= D3D11_BIND_RENDER_TARGET;
  81. //need to check to make sure this format supports render targets
  82. U32 supportFlag = 0;
  83. D3D11DEVICE->CheckFormatSupport(d3dTextureFormat, &supportFlag);
  84. //if it doesn't support render targets then default to R8G8B8A8
  85. if(!(supportFlag & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
  86. d3dTextureFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
  87. retTex->isManaged =false;
  88. }
  89. if( retTex->mProfile->isZTarget() )
  90. {
  91. bindFlags |= D3D11_BIND_DEPTH_STENCIL;
  92. retTex->isManaged = false;
  93. }
  94. if( !forceMips && !retTex->mProfile->isSystemMemory() &&
  95. numMipLevels == 0 &&
  96. !(depth > 0) )
  97. {
  98. miscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
  99. bindFlags |= D3D11_BIND_RENDER_TARGET; // in order to automatically generate mips. Resource needs to be a rendertarget and shader resource
  100. }
  101. if( depth > 0 )
  102. {
  103. D3D11_TEXTURE3D_DESC desc;
  104. ZeroMemory(&desc, sizeof(D3D11_TEXTURE3D_DESC));
  105. desc.BindFlags = bindFlags;
  106. desc.CPUAccessFlags = cpuFlags;
  107. desc.Depth = depth;
  108. desc.Width = width;
  109. desc.Height = height;
  110. desc.Format = d3dTextureFormat;
  111. desc.Usage = (D3D11_USAGE)usage;
  112. desc.MipLevels = numMipLevels;
  113. HRESULT hr = D3D11DEVICE->CreateTexture3D(&desc, NULL, retTex->get3DTexPtr());
  114. if(FAILED(hr))
  115. {
  116. AssertFatal(false, "GFXD3D11TextureManager::_createTexture - failed to create volume texture!");
  117. }
  118. retTex->mTextureSize.set(width, height, depth);
  119. retTex->get3DTex()->GetDesc(&desc);
  120. retTex->mMipLevels = numMipLevels;
  121. retTex->mFormat = format;
  122. }
  123. else
  124. {
  125. U32 numQualityLevels = 0;
  126. switch (antialiasLevel)
  127. {
  128. case 0:
  129. case AA_MATCH_BACKBUFFER:
  130. antialiasLevel = 1;
  131. break;
  132. default:
  133. {
  134. antialiasLevel = 0;
  135. D3D11DEVICE->CheckMultisampleQualityLevels(d3dTextureFormat, antialiasLevel, &numQualityLevels);
  136. AssertFatal(numQualityLevels, "Invalid AA level!");
  137. break;
  138. }
  139. }
  140. if(retTex->mProfile->isZTarget())
  141. {
  142. D3D11_TEXTURE2D_DESC desc;
  143. ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
  144. desc.ArraySize = 1;
  145. desc.BindFlags = bindFlags;
  146. desc.CPUAccessFlags = cpuFlags;
  147. //depth stencil must be a typeless format if it is bound on render target and shader resource simultaneously
  148. // we'll send the real format for the creation of the views
  149. desc.Format = DXGI_FORMAT_R24G8_TYPELESS;
  150. desc.MipLevels = numMipLevels;
  151. desc.SampleDesc.Count = antialiasLevel;
  152. desc.SampleDesc.Quality = numQualityLevels;
  153. desc.Height = height;
  154. desc.Width = width;
  155. desc.Usage = (D3D11_USAGE)usage;
  156. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, retTex->getSurfacePtr());
  157. if(FAILED(hr))
  158. {
  159. AssertFatal(false, "Failed to create Zbuffer texture");
  160. }
  161. retTex->mFormat = format; // Assigning format like this should be fine.
  162. }
  163. else
  164. {
  165. D3D11_TEXTURE2D_DESC desc;
  166. ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
  167. desc.ArraySize = 1;
  168. desc.BindFlags = bindFlags;
  169. desc.CPUAccessFlags = cpuFlags;
  170. desc.Format = d3dTextureFormat;
  171. desc.MipLevels = numMipLevels;
  172. desc.SampleDesc.Count = antialiasLevel;
  173. desc.SampleDesc.Quality = numQualityLevels;
  174. desc.Height = height;
  175. desc.Width = width;
  176. desc.Usage = (D3D11_USAGE)usage;
  177. desc.MiscFlags = miscFlags;
  178. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, retTex->get2DTexPtr());
  179. if(FAILED(hr))
  180. {
  181. AssertFatal(false, "GFXD3D11TextureManager::_createTexture - failed to create texture!");
  182. }
  183. retTex->get2DTex()->GetDesc(&desc);
  184. retTex->mMipLevels = desc.MipLevels;
  185. }
  186. // start creating the resource views...
  187. // don't bother creating views for system memory/staging textures
  188. // they are just used for copying
  189. if (!retTex->mProfile->isSystemMemory())
  190. {
  191. createResourceView(height, width, depth, d3dTextureFormat, numMipLevels, bindFlags, retTex);
  192. }
  193. // Get the actual size of the texture...
  194. D3D11_TEXTURE2D_DESC probeDesc;
  195. ZeroMemory(&probeDesc, sizeof(D3D11_TEXTURE2D_DESC));
  196. if( retTex->get2DTex() != NULL )
  197. {
  198. retTex->get2DTex()->GetDesc(&probeDesc);
  199. }
  200. else if( retTex->getSurface() != NULL )
  201. {
  202. retTex->getSurface()->GetDesc(&probeDesc);
  203. }
  204. retTex->mTextureSize.set(probeDesc.Width, probeDesc.Height, 0);
  205. S32 fmt = 0;
  206. if(!profile->isZTarget())
  207. fmt = probeDesc.Format;
  208. else
  209. fmt = DXGI_FORMAT_D24_UNORM_S8_UINT; // we need to assign this manually.
  210. GFXREVERSE_LOOKUP( GFXD3D11TextureFormat, GFXFormat, fmt );
  211. retTex->mFormat = (GFXFormat)fmt;
  212. }
  213. }
  214. //-----------------------------------------------------------------------------
  215. // createTexture
  216. //-----------------------------------------------------------------------------
  217. GFXTextureObject *GFXD3D11TextureManager::_createTextureObject( U32 height,
  218. U32 width,
  219. U32 depth,
  220. GFXFormat format,
  221. GFXTextureProfile *profile,
  222. U32 numMipLevels,
  223. bool forceMips,
  224. S32 antialiasLevel,
  225. GFXTextureObject *inTex )
  226. {
  227. GFXD3D11TextureObject *retTex;
  228. if ( inTex )
  229. {
  230. AssertFatal(static_cast<GFXD3D11TextureObject*>( inTex ), "GFXD3D11TextureManager::_createTexture() - Bad inTex type!");
  231. retTex = static_cast<GFXD3D11TextureObject*>( inTex );
  232. retTex->release();
  233. }
  234. else
  235. {
  236. retTex = new GFXD3D11TextureObject(GFX, profile);
  237. retTex->registerResourceWithDevice(GFX);
  238. }
  239. _innerCreateTexture(retTex, height, width, depth, format, profile, numMipLevels, forceMips, antialiasLevel);
  240. return retTex;
  241. }
  242. bool GFXD3D11TextureManager::_loadTexture(GFXTextureObject *aTexture, GBitmap *pDL)
  243. {
  244. PROFILE_SCOPE(GFXD3D11TextureManager_loadTexture);
  245. GFXD3D11TextureObject *texture = static_cast<GFXD3D11TextureObject*>(aTexture);
  246. // Check with profiler to see if we can do automatic mipmap generation.
  247. const bool supportsAutoMips = GFX->getCardProfiler()->queryProfile("autoMipMapLevel", true);
  248. // Helper bool
  249. const bool isCompressedTexFmt = ImageUtil::isCompressedFormat(aTexture->mFormat);
  250. // Settings for mipmap generation
  251. U32 maxDownloadMip = pDL->getNumMipLevels();
  252. U32 nbMipMapLevel = pDL->getNumMipLevels();
  253. if( supportsAutoMips && !isCompressedTexFmt )
  254. {
  255. maxDownloadMip = 1;
  256. nbMipMapLevel = aTexture->mMipLevels;
  257. }
  258. GFXD3D11Device* dev = D3D11;
  259. bool isDynamic = texture->mProfile->isDynamic();
  260. // Fill the texture...
  261. for( U32 i = 0; i < maxDownloadMip; i++ )
  262. {
  263. U32 subResource = D3D11CalcSubresource(i, 0, aTexture->mMipLevels);
  264. if(!isDynamic)
  265. {
  266. U8* copyBuffer = NULL;
  267. switch(texture->mFormat)
  268. {
  269. case GFXFormatR8G8B8:
  270. case GFXFormatR8G8B8_SRGB:
  271. {
  272. PROFILE_SCOPE(Swizzle24_Upload);
  273. U8* Bits = new U8[pDL->getWidth(i) * pDL->getHeight(i) * 4];
  274. dMemcpy(Bits, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * 3);
  275. bitmapConvertRGB_to_RGBX(&Bits, pDL->getWidth(i) * pDL->getHeight(i));
  276. copyBuffer = new U8[pDL->getWidth(i) * pDL->getHeight(i) * 4];
  277. dev->getDeviceSwizzle32()->ToBuffer(copyBuffer, Bits, pDL->getWidth(i) * pDL->getHeight(i) * 4);
  278. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subResource, NULL, copyBuffer, pDL->getWidth() * 4, pDL->getHeight() *4);
  279. SAFE_DELETE_ARRAY(Bits);
  280. break;
  281. }
  282. case GFXFormatR8G8B8A8:
  283. case GFXFormatR8G8B8X8:
  284. case GFXFormatR8G8B8A8_SRGB:
  285. {
  286. PROFILE_SCOPE(Swizzle32_Upload);
  287. copyBuffer = new U8[pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel()];
  288. dev->getDeviceSwizzle32()->ToBuffer(copyBuffer, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  289. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subResource, NULL, copyBuffer, pDL->getWidth() * pDL->getBytesPerPixel(), pDL->getHeight() *pDL->getBytesPerPixel());
  290. break;
  291. }
  292. default:
  293. {
  294. // Just copy the bits in no swizzle or padding
  295. PROFILE_SCOPE(SwizzleNull_Upload);
  296. AssertFatal( pDL->getFormat() == texture->mFormat, "Format mismatch");
  297. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subResource, NULL, pDL->getBits(i), pDL->getWidth() *pDL->getBytesPerPixel(), pDL->getHeight() *pDL->getBytesPerPixel());
  298. }
  299. }
  300. SAFE_DELETE_ARRAY(copyBuffer);
  301. }
  302. else
  303. {
  304. D3D11_MAPPED_SUBRESOURCE mapping;
  305. HRESULT res = dev->getDeviceContext()->Map(texture->get2DTex(), subResource, D3D11_MAP_WRITE, 0, &mapping);
  306. AssertFatal(res, "tex2d map call failure");
  307. switch( texture->mFormat )
  308. {
  309. case GFXFormatR8G8B8:
  310. case GFXFormatR8G8B8_SRGB:
  311. {
  312. PROFILE_SCOPE(Swizzle24_Upload);
  313. U8* Bits = new U8[pDL->getWidth(i) * pDL->getHeight(i) * 4];
  314. dMemcpy(Bits, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * 3);
  315. bitmapConvertRGB_to_RGBX(&Bits, pDL->getWidth(i) * pDL->getHeight(i));
  316. dev->getDeviceSwizzle32()->ToBuffer(mapping.pData, Bits, pDL->getWidth(i) * pDL->getHeight(i) * 4);
  317. SAFE_DELETE_ARRAY(Bits);
  318. }
  319. break;
  320. case GFXFormatR8G8B8A8:
  321. case GFXFormatR8G8B8X8:
  322. case GFXFormatR8G8B8A8_SRGB:
  323. {
  324. PROFILE_SCOPE(Swizzle32_Upload);
  325. dev->getDeviceSwizzle32()->ToBuffer(mapping.pData, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  326. }
  327. break;
  328. default:
  329. {
  330. // Just copy the bits in no swizzle or padding
  331. PROFILE_SCOPE(SwizzleNull_Upload);
  332. AssertFatal( pDL->getFormat() == texture->mFormat, "Format mismatch");
  333. dMemcpy(mapping.pData, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  334. }
  335. }
  336. dev->getDeviceContext()->Unmap(texture->get2DTex(), subResource);
  337. }
  338. }
  339. D3D11_TEXTURE2D_DESC desc;
  340. // if the texture asked for mip generation. lets generate it.
  341. texture->get2DTex()->GetDesc(&desc);
  342. if (desc.MiscFlags &D3D11_RESOURCE_MISC_GENERATE_MIPS)
  343. {
  344. dev->getDeviceContext()->GenerateMips(texture->getSRView());
  345. //texture->mMipLevels = desc.MipLevels;
  346. }
  347. return true;
  348. }
  349. bool GFXD3D11TextureManager::_loadTexture(GFXTextureObject *inTex, void *raw)
  350. {
  351. PROFILE_SCOPE(GFXD3D11TextureManager_loadTextureRaw);
  352. GFXD3D11TextureObject *texture = (GFXD3D11TextureObject *) inTex;
  353. GFXD3D11Device* dev = static_cast<GFXD3D11Device *>(GFX);
  354. // currently only for volume textures...
  355. if(texture->getDepth() < 1) return false;
  356. U8* Bits = NULL;
  357. if(texture->mFormat == GFXFormatR8G8B8 || texture->mFormat == GFXFormatR8G8B8_SRGB)
  358. {
  359. // convert 24 bit to 32 bit
  360. Bits = new U8[texture->getWidth() * texture->getHeight() * texture->getDepth() * 4];
  361. dMemcpy(Bits, raw, texture->getWidth() * texture->getHeight() * texture->getDepth() * 3);
  362. bitmapConvertRGB_to_RGBX(&Bits, texture->getWidth() * texture->getHeight() * texture->getDepth());
  363. }
  364. U32 bytesPerPix = 1;
  365. switch(texture->mFormat)
  366. {
  367. case GFXFormatR8G8B8:
  368. case GFXFormatR8G8B8_SRGB:
  369. case GFXFormatR8G8B8A8:
  370. case GFXFormatR8G8B8X8:
  371. case GFXFormatR8G8B8A8_SRGB:
  372. bytesPerPix = 4;
  373. break;
  374. }
  375. D3D11_BOX box;
  376. box.left = 0;
  377. box.right = texture->getWidth();
  378. box.front = 0;
  379. box.back = texture->getDepth();
  380. box.top = 0;
  381. box.bottom = texture->getHeight();
  382. if(texture->mFormat == GFXFormatR8G8B8 || texture->mFormat == GFXFormatR8G8B8_SRGB) // converted format also for volume textures
  383. dev->getDeviceContext()->UpdateSubresource(texture->get3DTex(), 0, &box, Bits, texture->getWidth() * bytesPerPix, texture->getHeight() * bytesPerPix);
  384. else
  385. dev->getDeviceContext()->UpdateSubresource(texture->get3DTex(), 0, &box, raw, texture->getWidth() * bytesPerPix, texture->getHeight() * bytesPerPix);
  386. SAFE_DELETE_ARRAY(Bits);
  387. return true;
  388. }
  389. bool GFXD3D11TextureManager::_refreshTexture(GFXTextureObject *texture)
  390. {
  391. U32 usedStrategies = 0;
  392. GFXD3D11TextureObject *realTex = static_cast<GFXD3D11TextureObject *>(texture);
  393. if(texture->mProfile->doStoreBitmap())
  394. {
  395. if(texture->mBitmap)
  396. _loadTexture(texture, texture->mBitmap);
  397. if(texture->mDDS)
  398. _loadTexture(texture, texture->mDDS);
  399. usedStrategies++;
  400. }
  401. if(texture->mProfile->isRenderTarget() || texture->mProfile->isDynamic() || texture->mProfile->isZTarget())
  402. {
  403. realTex->release();
  404. _innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels, false, texture->mAntialiasLevel);
  405. usedStrategies++;
  406. }
  407. AssertFatal(usedStrategies < 2, "GFXD3D11TextureManager::_refreshTexture - Inconsistent profile flags!");
  408. return true;
  409. }
  410. bool GFXD3D11TextureManager::_freeTexture(GFXTextureObject *texture, bool zombify)
  411. {
  412. AssertFatal(dynamic_cast<GFXD3D11TextureObject *>(texture),"Not an actual d3d texture object!");
  413. GFXD3D11TextureObject *tex = static_cast<GFXD3D11TextureObject *>( texture );
  414. // If it's a managed texture and we're zombifying, don't blast it, D3D allows
  415. // us to keep it.
  416. if(zombify && tex->isManaged)
  417. return true;
  418. tex->release();
  419. return true;
  420. }
  421. /// Load a texture from a proper DDSFile instance.
  422. bool GFXD3D11TextureManager::_loadTexture(GFXTextureObject *aTexture, DDSFile *dds)
  423. {
  424. PROFILE_SCOPE(GFXD3D11TextureManager_loadTextureDDS);
  425. GFXD3D11TextureObject *texture = static_cast<GFXD3D11TextureObject*>(aTexture);
  426. GFXD3D11Device* dev = static_cast<GFXD3D11Device *>(GFX);
  427. // Fill the texture...
  428. for( U32 i = 0; i < aTexture->mMipLevels; i++ )
  429. {
  430. PROFILE_SCOPE(GFXD3DTexMan_loadSurface);
  431. AssertFatal( dds->mSurfaces.size() > 0, "Assumption failed. DDSFile has no surfaces." );
  432. U32 subresource = D3D11CalcSubresource(i, 0, aTexture->mMipLevels);
  433. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subresource, 0, dds->mSurfaces[0]->mMips[i], dds->getSurfacePitch(i), 0);
  434. }
  435. D3D11_TEXTURE2D_DESC desc;
  436. // if the texture asked for mip generation. lets generate it.
  437. texture->get2DTex()->GetDesc(&desc);
  438. if (desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
  439. dev->getDeviceContext()->GenerateMips(texture->getSRView());
  440. return true;
  441. }
  442. void GFXD3D11TextureManager::createResourceView(U32 height, U32 width, U32 depth, DXGI_FORMAT format, U32 numMipLevels,U32 usageFlags, GFXTextureObject *inTex)
  443. {
  444. GFXD3D11TextureObject *tex = static_cast<GFXD3D11TextureObject*>(inTex);
  445. ID3D11Resource* resource = NULL;
  446. if(tex->get2DTex())
  447. resource = tex->get2DTex();
  448. else if(tex->getSurface())
  449. resource = tex->getSurface();
  450. else
  451. resource = tex->get3DTex();
  452. HRESULT hr;
  453. //TODO: add MSAA support later.
  454. if(usageFlags & D3D11_BIND_SHADER_RESOURCE)
  455. {
  456. D3D11_SHADER_RESOURCE_VIEW_DESC desc;
  457. if(usageFlags & D3D11_BIND_DEPTH_STENCIL)
  458. desc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; // reads the depth
  459. else
  460. desc.Format = format;
  461. if(depth > 0)
  462. {
  463. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  464. desc.Texture3D.MipLevels = -1;
  465. desc.Texture3D.MostDetailedMip = 0;
  466. }
  467. else
  468. {
  469. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  470. desc.Texture2D.MipLevels = -1;
  471. desc.Texture2D.MostDetailedMip = 0;
  472. }
  473. hr = D3D11DEVICE->CreateShaderResourceView(resource,&desc, tex->getSRViewPtr());
  474. AssertFatal(SUCCEEDED(hr), "CreateShaderResourceView:: failed to create view!");
  475. }
  476. if(usageFlags & D3D11_BIND_RENDER_TARGET)
  477. {
  478. D3D11_RENDER_TARGET_VIEW_DESC desc;
  479. desc.Format = format;
  480. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  481. desc.Texture2D.MipSlice = 0;
  482. hr = D3D11DEVICE->CreateRenderTargetView(resource, &desc, tex->getRTViewPtr());
  483. AssertFatal(SUCCEEDED(hr), "CreateRenderTargetView:: failed to create view!");
  484. }
  485. if(usageFlags & D3D11_BIND_DEPTH_STENCIL)
  486. {
  487. D3D11_DEPTH_STENCIL_VIEW_DESC desc;
  488. desc.Format = format;
  489. desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  490. desc.Texture2D.MipSlice = 0;
  491. desc.Flags = 0;
  492. hr = D3D11DEVICE->CreateDepthStencilView(resource,&desc, tex->getDSViewPtr());
  493. AssertFatal(SUCCEEDED(hr), "CreateDepthStencilView:: failed to create view!");
  494. }
  495. }