gfxD3D11TextureManager.cpp 20 KB

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