2
0

gfxD3D11TextureManager.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/gfxCardProfile.h"
  26. #include "gfx/gfxStringEnumTranslate.h"
  27. #include "core/strings/unicode.h"
  28. #include "core/util/swizzle.h"
  29. #include "core/util/safeDelete.h"
  30. #include "console/console.h"
  31. #include "core/resourceManager.h"
  32. GFXD3D11TextureManager::GFXD3D11TextureManager()
  33. {
  34. ZeroMemory(mCurTexSet, sizeof(mCurTexSet));
  35. }
  36. GFXD3D11TextureManager::~GFXD3D11TextureManager()
  37. {
  38. // Destroy texture table now so just in case some texture objects
  39. // are still left, we don't crash on a pure virtual method call.
  40. SAFE_DELETE_ARRAY( mHashTable );
  41. }
  42. void GFXD3D11TextureManager::_innerCreateTexture( GFXD3D11TextureObject *retTex,
  43. U32 height,
  44. U32 width,
  45. U32 depth,
  46. GFXFormat format,
  47. GFXTextureProfile *profile,
  48. U32 numMipLevels,
  49. bool forceMips,
  50. S32 antialiasLevel)
  51. {
  52. U32 usage = 0;
  53. U32 bindFlags = 0;
  54. U32 miscFlags = 0;
  55. if(!retTex->mProfile->isZTarget() && !retTex->mProfile->isSystemMemory())
  56. bindFlags = D3D11_BIND_SHADER_RESOURCE;
  57. U32 cpuFlags = 0;
  58. retTex->mProfile = profile;
  59. retTex->isManaged = false;
  60. DXGI_FORMAT d3dTextureFormat = GFXD3D11TextureFormat[format];
  61. if( retTex->mProfile->isDynamic() )
  62. {
  63. usage = D3D11_USAGE_DYNAMIC;
  64. cpuFlags |= D3D11_CPU_ACCESS_WRITE;
  65. retTex->isManaged = false;
  66. }
  67. else if ( retTex->mProfile->isSystemMemory() )
  68. {
  69. usage |= D3D11_USAGE_STAGING;
  70. cpuFlags |= D3D11_CPU_ACCESS_READ;
  71. }
  72. else
  73. {
  74. usage = D3D11_USAGE_DEFAULT;
  75. retTex->isManaged = true;
  76. }
  77. if( retTex->mProfile->isRenderTarget() )
  78. {
  79. bindFlags |= D3D11_BIND_RENDER_TARGET;
  80. //need to check to make sure this format supports render targets
  81. U32 supportFlag = 0;
  82. D3D11DEVICE->CheckFormatSupport(d3dTextureFormat, &supportFlag);
  83. //if it doesn't support render targets then default to R8G8B8A8
  84. if(!(supportFlag & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
  85. d3dTextureFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
  86. retTex->isManaged =false;
  87. }
  88. if( retTex->mProfile->isZTarget() )
  89. {
  90. bindFlags |= D3D11_BIND_DEPTH_STENCIL;
  91. retTex->isManaged = false;
  92. }
  93. if( !forceMips && !retTex->mProfile->isSystemMemory() &&
  94. numMipLevels == 0 &&
  95. !(depth > 0) )
  96. {
  97. miscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
  98. bindFlags |= D3D11_BIND_RENDER_TARGET; // in order to automatically generate mips. Resource needs to be a rendertarget and shader resource
  99. }
  100. if( depth > 0 )
  101. {
  102. D3D11_TEXTURE3D_DESC desc;
  103. ZeroMemory(&desc, sizeof(D3D11_TEXTURE3D_DESC));
  104. desc.BindFlags = bindFlags;
  105. desc.CPUAccessFlags = cpuFlags;
  106. desc.Depth = depth;
  107. desc.Width = width;
  108. desc.Height = height;
  109. desc.Format = d3dTextureFormat;
  110. desc.Usage = (D3D11_USAGE)usage;
  111. desc.MipLevels = numMipLevels;
  112. HRESULT hr = D3D11DEVICE->CreateTexture3D(&desc, NULL, retTex->get3DTexPtr());
  113. if(FAILED(hr))
  114. {
  115. AssertFatal(false, "GFXD3D11TextureManager::_createTexture - failed to create volume texture!");
  116. }
  117. retTex->mTextureSize.set(width, height, depth);
  118. retTex->get3DTex()->GetDesc(&desc);
  119. retTex->mMipLevels = numMipLevels;
  120. retTex->mFormat = format;
  121. }
  122. else
  123. {
  124. UINT numQualityLevels = 0;
  125. switch (antialiasLevel)
  126. {
  127. case 0:
  128. case AA_MATCH_BACKBUFFER:
  129. antialiasLevel = 1;
  130. break;
  131. default:
  132. {
  133. antialiasLevel = 0;
  134. UINT numQualityLevels;
  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 = aTexture->mFormat >= GFXFormatDXT1 && aTexture->mFormat <= GFXFormatDXT5;
  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. {
  271. PROFILE_SCOPE(Swizzle24_Upload);
  272. AssertFatal(pDL->getFormat() == GFXFormatR8G8B8, "Assumption failed");
  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. {
  285. PROFILE_SCOPE(Swizzle32_Upload);
  286. copyBuffer = new U8[pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel()];
  287. dev->getDeviceSwizzle32()->ToBuffer(copyBuffer, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  288. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subResource, NULL, copyBuffer, pDL->getWidth() * pDL->getBytesPerPixel(), pDL->getHeight() *pDL->getBytesPerPixel());
  289. break;
  290. }
  291. default:
  292. {
  293. // Just copy the bits in no swizzle or padding
  294. PROFILE_SCOPE(SwizzleNull_Upload);
  295. AssertFatal( pDL->getFormat() == texture->mFormat, "Format mismatch");
  296. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subResource, NULL, pDL->getBits(i), pDL->getWidth() *pDL->getBytesPerPixel(), pDL->getHeight() *pDL->getBytesPerPixel());
  297. }
  298. }
  299. SAFE_DELETE_ARRAY(copyBuffer);
  300. }
  301. else
  302. {
  303. D3D11_MAPPED_SUBRESOURCE mapping;
  304. HRESULT res = dev->getDeviceContext()->Map(texture->get2DTex(), subResource, D3D11_MAP_WRITE, 0, &mapping);
  305. AssertFatal(res, "tex2d map call failure");
  306. switch( texture->mFormat )
  307. {
  308. case GFXFormatR8G8B8:
  309. {
  310. PROFILE_SCOPE(Swizzle24_Upload);
  311. AssertFatal(pDL->getFormat() == GFXFormatR8G8B8, "Assumption failed");
  312. U8* Bits = new U8[pDL->getWidth(i) * pDL->getHeight(i) * 4];
  313. dMemcpy(Bits, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * 3);
  314. bitmapConvertRGB_to_RGBX(&Bits, pDL->getWidth(i) * pDL->getHeight(i));
  315. dev->getDeviceSwizzle32()->ToBuffer(mapping.pData, Bits, pDL->getWidth(i) * pDL->getHeight(i) * 4);
  316. SAFE_DELETE_ARRAY(Bits);
  317. }
  318. break;
  319. case GFXFormatR8G8B8A8:
  320. case GFXFormatR8G8B8X8:
  321. {
  322. PROFILE_SCOPE(Swizzle32_Upload);
  323. dev->getDeviceSwizzle32()->ToBuffer(mapping.pData, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  324. }
  325. break;
  326. default:
  327. {
  328. // Just copy the bits in no swizzle or padding
  329. PROFILE_SCOPE(SwizzleNull_Upload);
  330. AssertFatal( pDL->getFormat() == texture->mFormat, "Format mismatch");
  331. dMemcpy(mapping.pData, pDL->getBits(i), pDL->getWidth(i) * pDL->getHeight(i) * pDL->getBytesPerPixel());
  332. }
  333. }
  334. dev->getDeviceContext()->Unmap(texture->get2DTex(), subResource);
  335. }
  336. }
  337. D3D11_TEXTURE2D_DESC desc;
  338. // if the texture asked for mip generation. lets generate it.
  339. texture->get2DTex()->GetDesc(&desc);
  340. if (desc.MiscFlags &D3D11_RESOURCE_MISC_GENERATE_MIPS)
  341. {
  342. dev->getDeviceContext()->GenerateMips(texture->getSRView());
  343. //texture->mMipLevels = desc.MipLevels;
  344. }
  345. return true;
  346. }
  347. bool GFXD3D11TextureManager::_loadTexture(GFXTextureObject *inTex, void *raw)
  348. {
  349. PROFILE_SCOPE(GFXD3D11TextureManager_loadTextureRaw);
  350. GFXD3D11TextureObject *texture = (GFXD3D11TextureObject *) inTex;
  351. GFXD3D11Device* dev = static_cast<GFXD3D11Device *>(GFX);
  352. // currently only for volume textures...
  353. if(texture->getDepth() < 1) return false;
  354. U8* Bits = NULL;
  355. if(texture->mFormat == GFXFormatR8G8B8)
  356. {
  357. // convert 24 bit to 32 bit
  358. Bits = new U8[texture->getWidth() * texture->getHeight() * texture->getDepth() * 4];
  359. dMemcpy(Bits, raw, texture->getWidth() * texture->getHeight() * texture->getDepth() * 3);
  360. bitmapConvertRGB_to_RGBX(&Bits, texture->getWidth() * texture->getHeight() * texture->getDepth());
  361. }
  362. U32 bytesPerPix = 1;
  363. switch(texture->mFormat)
  364. {
  365. case GFXFormatR8G8B8:
  366. case GFXFormatR8G8B8A8:
  367. case GFXFormatR8G8B8X8:
  368. bytesPerPix = 4;
  369. break;
  370. }
  371. D3D11_BOX box;
  372. box.left = 0;
  373. box.right = texture->getWidth();
  374. box.front = 0;
  375. box.back = texture->getDepth();
  376. box.top = 0;
  377. box.bottom = texture->getHeight();
  378. if(texture->mFormat == GFXFormatR8G8B8) // converted format also for volume textures
  379. dev->getDeviceContext()->UpdateSubresource(texture->get3DTex(), 0, &box, Bits, texture->getWidth() * bytesPerPix, texture->getHeight() * bytesPerPix);
  380. else
  381. dev->getDeviceContext()->UpdateSubresource(texture->get3DTex(), 0, &box, raw, texture->getWidth() * bytesPerPix, texture->getHeight() * bytesPerPix);
  382. SAFE_DELETE_ARRAY(Bits);
  383. return true;
  384. }
  385. bool GFXD3D11TextureManager::_refreshTexture(GFXTextureObject *texture)
  386. {
  387. U32 usedStrategies = 0;
  388. GFXD3D11TextureObject *realTex = static_cast<GFXD3D11TextureObject *>(texture);
  389. if(texture->mProfile->doStoreBitmap())
  390. {
  391. if(texture->mBitmap)
  392. _loadTexture(texture, texture->mBitmap);
  393. if(texture->mDDS)
  394. _loadTexture(texture, texture->mDDS);
  395. usedStrategies++;
  396. }
  397. if(texture->mProfile->isRenderTarget() || texture->mProfile->isDynamic() || texture->mProfile->isZTarget())
  398. {
  399. realTex->release();
  400. _innerCreateTexture(realTex, texture->getHeight(), texture->getWidth(), texture->getDepth(), texture->mFormat, texture->mProfile, texture->mMipLevels, false, texture->mAntialiasLevel);
  401. usedStrategies++;
  402. }
  403. AssertFatal(usedStrategies < 2, "GFXD3D11TextureManager::_refreshTexture - Inconsistent profile flags!");
  404. return true;
  405. }
  406. bool GFXD3D11TextureManager::_freeTexture(GFXTextureObject *texture, bool zombify)
  407. {
  408. AssertFatal(dynamic_cast<GFXD3D11TextureObject *>(texture),"Not an actual d3d texture object!");
  409. GFXD3D11TextureObject *tex = static_cast<GFXD3D11TextureObject *>( texture );
  410. // If it's a managed texture and we're zombifying, don't blast it, D3D allows
  411. // us to keep it.
  412. if(zombify && tex->isManaged)
  413. return true;
  414. tex->release();
  415. return true;
  416. }
  417. /// Load a texture from a proper DDSFile instance.
  418. bool GFXD3D11TextureManager::_loadTexture(GFXTextureObject *aTexture, DDSFile *dds)
  419. {
  420. PROFILE_SCOPE(GFXD3D11TextureManager_loadTextureDDS);
  421. GFXD3D11TextureObject *texture = static_cast<GFXD3D11TextureObject*>(aTexture);
  422. GFXD3D11Device* dev = static_cast<GFXD3D11Device *>(GFX);
  423. // Fill the texture...
  424. for( U32 i = 0; i < aTexture->mMipLevels; i++ )
  425. {
  426. PROFILE_SCOPE(GFXD3DTexMan_loadSurface);
  427. AssertFatal( dds->mSurfaces.size() > 0, "Assumption failed. DDSFile has no surfaces." );
  428. U32 subresource = D3D11CalcSubresource(i, 0, aTexture->mMipLevels);
  429. dev->getDeviceContext()->UpdateSubresource(texture->get2DTex(), subresource, 0, dds->mSurfaces[0]->mMips[i], dds->getSurfacePitch(i), 0);
  430. }
  431. D3D11_TEXTURE2D_DESC desc;
  432. // if the texture asked for mip generation. lets generate it.
  433. texture->get2DTex()->GetDesc(&desc);
  434. if (desc.MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
  435. dev->getDeviceContext()->GenerateMips(texture->getSRView());
  436. return true;
  437. }
  438. void GFXD3D11TextureManager::createResourceView(U32 height, U32 width, U32 depth, DXGI_FORMAT format, U32 numMipLevels,U32 usageFlags, GFXTextureObject *inTex)
  439. {
  440. GFXD3D11TextureObject *tex = static_cast<GFXD3D11TextureObject*>(inTex);
  441. ID3D11Resource* resource = NULL;
  442. if(tex->get2DTex())
  443. resource = tex->get2DTex();
  444. else if(tex->getSurface())
  445. resource = tex->getSurface();
  446. else
  447. resource = tex->get3DTex();
  448. HRESULT hr;
  449. //TODO: add MSAA support later.
  450. if(usageFlags & D3D11_BIND_SHADER_RESOURCE)
  451. {
  452. D3D11_SHADER_RESOURCE_VIEW_DESC desc;
  453. if(usageFlags & D3D11_BIND_DEPTH_STENCIL)
  454. desc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; // reads the depth
  455. else
  456. desc.Format = format;
  457. if(depth > 0)
  458. {
  459. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  460. desc.Texture3D.MipLevels = -1;
  461. desc.Texture3D.MostDetailedMip = 0;
  462. }
  463. else
  464. {
  465. desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  466. desc.Texture2D.MipLevels = -1;
  467. desc.Texture2D.MostDetailedMip = 0;
  468. }
  469. hr = D3D11DEVICE->CreateShaderResourceView(resource,&desc, tex->getSRViewPtr());
  470. AssertFatal(SUCCEEDED(hr), "CreateShaderResourceView:: failed to create view!");
  471. }
  472. if(usageFlags & D3D11_BIND_RENDER_TARGET)
  473. {
  474. D3D11_RENDER_TARGET_VIEW_DESC desc;
  475. desc.Format = format;
  476. desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  477. desc.Texture2D.MipSlice = 0;
  478. hr = D3D11DEVICE->CreateRenderTargetView(resource, &desc, tex->getRTViewPtr());
  479. AssertFatal(SUCCEEDED(hr), "CreateRenderTargetView:: failed to create view!");
  480. }
  481. if(usageFlags & D3D11_BIND_DEPTH_STENCIL)
  482. {
  483. D3D11_DEPTH_STENCIL_VIEW_DESC desc;
  484. desc.Format = format;
  485. desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  486. desc.Texture2D.MipSlice = 0;
  487. desc.Flags = 0;
  488. hr = D3D11DEVICE->CreateDepthStencilView(resource,&desc, tex->getDSViewPtr());
  489. AssertFatal(SUCCEEDED(hr), "CreateDepthStencilView:: failed to create view!");
  490. }
  491. }