gfxD3D11TextureManager.cpp 20 KB

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