gfxD3D11TextureObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/gfxD3D11TextureObject.h"
  24. #include "platform/profiler.h"
  25. #include "console/console.h"
  26. #ifdef TORQUE_DEBUG
  27. U32 GFXD3D11TextureObject::mTexCount = 0;
  28. #endif
  29. // GFXFormatR8G8B8 has now the same behaviour as GFXFormatR8G8B8X8.
  30. // This is because 24 bit format are now deprecated by microsoft, for data alignment reason there's no changes beetween 24 and 32 bit formats.
  31. // DirectX 10-11 both have 24 bit format no longer.
  32. GFXD3D11TextureObject::GFXD3D11TextureObject( GFXDevice * d, GFXTextureProfile *profile) : GFXTextureObject( d, profile )
  33. {
  34. #ifdef D3D11_DEBUG_SPEW
  35. mTexCount++;
  36. Con::printf("+ texMake %d %x", mTexCount, this);
  37. #endif
  38. mD3DTexture = NULL;
  39. mLocked = false;
  40. mD3DSurface = NULL;
  41. dMemset(&mLockRect, 0, sizeof(mLockRect));
  42. dMemset(&mLockBox, 0, sizeof(mLockBox));
  43. mLockedSubresource = 0;
  44. mDSView = NULL;
  45. mRTView = NULL;
  46. mSRView = NULL;
  47. isManaged = false;
  48. }
  49. GFXD3D11TextureObject::~GFXD3D11TextureObject()
  50. {
  51. kill();
  52. #ifdef D3D11_DEBUG_SPEW
  53. mTexCount--;
  54. Con::printf("+ texkill %d %x", mTexCount, this);
  55. #endif
  56. }
  57. GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect /*= NULL*/)
  58. {
  59. AssertFatal( !mLocked, "GFXD3D11TextureObject::lock - The texture is already locked!" );
  60. if( !mStagingTex ||
  61. mStagingTex->getWidth() != getWidth() ||
  62. mStagingTex->getHeight() != getHeight() ||
  63. mStagingTex->getDepth() != getDepth())
  64. {
  65. if (getDepth() != 0)
  66. {
  67. mStagingTex.set(getWidth(), getHeight(), getDepth(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__, 0));
  68. }
  69. else
  70. {
  71. mStagingTex.set(getWidth(), getHeight(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__));
  72. }
  73. }
  74. ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT;
  75. D3D11_MAPPED_SUBRESOURCE mapInfo;
  76. U32 offset = 0;
  77. mLockedSubresource = D3D11CalcSubresource(mipLevel, 0, getMipLevels());
  78. GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex);
  79. //map staging texture
  80. HRESULT hr = pContext->Map(pD3DStagingTex->getResource(), mLockedSubresource, D3D11_MAP_WRITE, 0, &mapInfo);
  81. if (FAILED(hr))
  82. AssertFatal(false, "GFXD3D11TextureObject:lock - failed to map render target resource!");
  83. const bool is3D = mStagingTex->getDepth() != 0;
  84. const U32 width = mTextureSize.x >> mipLevel;
  85. const U32 height = mTextureSize.y >> mipLevel;
  86. const U32 depth = is3D ? mTextureSize.z >> mipLevel : 1;
  87. //calculate locked box region and offset
  88. if (inRect)
  89. {
  90. if ((inRect->point.x + inRect->extent.x > width) || (inRect->point.y + inRect->extent.y > height))
  91. AssertFatal(false, "GFXD3D11TextureObject::lock - Rectangle too big!");
  92. mLockBox.top = inRect->point.y;
  93. mLockBox.left = inRect->point.x;
  94. mLockBox.bottom = inRect->point.y + inRect->extent.y;
  95. mLockBox.right = inRect->point.x + inRect->extent.x;
  96. mLockBox.back = depth;
  97. mLockBox.front = 0;
  98. //calculate offset
  99. offset = inRect->point.x * getFormatByteSize() + inRect->point.y * mapInfo.RowPitch;
  100. }
  101. else
  102. {
  103. mLockBox.top = 0;
  104. mLockBox.left = 0;
  105. mLockBox.bottom = height;
  106. mLockBox.right = width;
  107. mLockBox.back = depth;
  108. mLockBox.front = 0;
  109. }
  110. mLocked = true;
  111. mLockRect.pBits = static_cast<U8*>(mapInfo.pData) + offset;
  112. mLockRect.Pitch = mapInfo.RowPitch;
  113. return (GFXLockedRect*)&mLockRect;
  114. }
  115. void GFXD3D11TextureObject::unlock(U32 mipLevel)
  116. {
  117. AssertFatal( mLocked, "GFXD3D11TextureObject::unlock - Attempting to unlock a surface that has not been locked" );
  118. //profile in the unlock function because all the heavy lifting is done here
  119. PROFILE_START(GFXD3D11TextureObject_lockRT);
  120. ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT;
  121. GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex);
  122. ID3D11Resource* pStagingResource = pD3DStagingTex->getResource();
  123. const bool is3D = mStagingTex->getDepth() != 0;
  124. //unmap staging texture
  125. pContext->Unmap(pStagingResource, mLockedSubresource);
  126. //copy lock box region from the staging texture to our regular texture
  127. pContext->CopySubresourceRegion(mD3DTexture, mLockedSubresource, mLockBox.left, mLockBox.top, is3D ? mLockBox.back : 0, pStagingResource, mLockedSubresource, &mLockBox);
  128. PROFILE_END();
  129. mLockedSubresource = 0;
  130. mLocked = false;
  131. }
  132. void GFXD3D11TextureObject::release()
  133. {
  134. SAFE_RELEASE(mSRView);
  135. SAFE_RELEASE(mRTView);
  136. SAFE_RELEASE(mDSView);
  137. SAFE_RELEASE(mD3DTexture);
  138. SAFE_RELEASE(mD3DSurface);
  139. }
  140. void GFXD3D11TextureObject::zombify()
  141. {
  142. // Managed textures are managed by D3D
  143. AssertFatal(!mLocked, "GFXD3D11TextureObject::zombify - Cannot zombify a locked texture!");
  144. if(isManaged)
  145. return;
  146. release();
  147. }
  148. void GFXD3D11TextureObject::resurrect()
  149. {
  150. // Managed textures are managed by D3D
  151. if(isManaged)
  152. return;
  153. static_cast<GFXD3D11TextureManager*>(TEXMGR)->refreshTexture(this);
  154. }
  155. bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
  156. {
  157. if (!bmp)
  158. return false;
  159. // check format limitations
  160. // at the moment we only support RGBA for the source (other 4 byte formats should
  161. // be easy to add though)
  162. AssertFatal(mFormat == GFXFormatR16G16B16A16F || mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB || mFormat == GFXFormatR8G8B8, "copyToBmp: invalid format");
  163. if (mFormat != GFXFormatR16G16B16A16F && mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB && mFormat != GFXFormatR8G8B8)
  164. return false;
  165. PROFILE_START(GFXD3D11TextureObject_copyToBmp);
  166. AssertFatal(bmp->getWidth() == getWidth(), avar("GFXGLTextureObject::copyToBmp - Width mismatch: %i vs %i", bmp->getWidth(), getWidth()));
  167. AssertFatal(bmp->getHeight() == getHeight(), avar("GFXGLTextureObject::copyToBmp - Height mismatch: %i vs %i", bmp->getHeight(), getHeight()));
  168. const U32 mipLevels = getMipLevels();
  169. bmp->setHasTransparency(mHasTransparency);
  170. // set some constants
  171. U32 sourceBytesPerPixel = 4;
  172. U32 destBytesPerPixel = 0;
  173. const GFXFormat fmt = bmp->getFormat();
  174. bool fp16 = false;//is rgba16f format?
  175. if (fmt == GFXFormatR16G16B16A16F)
  176. {
  177. destBytesPerPixel = 8;
  178. sourceBytesPerPixel = 8;
  179. fp16 = true;
  180. }
  181. else if (fmt == GFXFormatR8G8B8A8 || fmt == GFXFormatR8G8B8A8_LINEAR_FORCE || fmt == GFXFormatR8G8B8A8_SRGB)
  182. destBytesPerPixel = 4;
  183. else if(bmp->getFormat() == GFXFormatR8G8B8)
  184. destBytesPerPixel = 3;
  185. else
  186. // unsupported
  187. AssertFatal(false, "GFXD3D11TextureObject::copyToBmp - unsupported bitmap format");
  188. //create temp staging texture
  189. D3D11_TEXTURE2D_DESC desc;
  190. static_cast<ID3D11Texture2D*>(mD3DTexture)->GetDesc(&desc);
  191. desc.BindFlags = 0;
  192. desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
  193. desc.Usage = D3D11_USAGE_STAGING;
  194. desc.MiscFlags = 0;
  195. ID3D11Texture2D* pStagingTexture = NULL;
  196. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &pStagingTexture);
  197. if (FAILED(hr))
  198. {
  199. Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to create staging texture");
  200. return false;
  201. }
  202. //copy the classes texture to the staging texture
  203. D3D11DEVICECONTEXT->CopyResource(pStagingTexture, mD3DTexture);
  204. for (U32 mip = 0; mip < mipLevels; mip++)
  205. {
  206. const U32 width = bmp->getWidth(mip);
  207. const U32 height = bmp->getHeight(mip);
  208. //map the staging resource
  209. D3D11_MAPPED_SUBRESOURCE mappedRes;
  210. const U32 subResource = D3D11CalcSubresource(mip, 0, mipLevels);
  211. hr = D3D11DEVICECONTEXT->Map(pStagingTexture, subResource, D3D11_MAP_READ, 0, &mappedRes);
  212. if (FAILED(hr))
  213. {
  214. //cleanup
  215. SAFE_RELEASE(pStagingTexture);
  216. Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to map staging texture");
  217. return false;
  218. }
  219. // set pointers
  220. const U8* srcPtr = (U8*)mappedRes.pData;
  221. U8* destPtr = bmp->getWritableBits(mip);
  222. // we will want to skip over any D3D cache data in the source texture
  223. const S32 sourceCacheSize = mappedRes.RowPitch - width * sourceBytesPerPixel;
  224. AssertFatal(sourceCacheSize >= 0, "GFXD3D11TextureObject::copyToBmp - cache size is less than zero?");
  225. // copy data into bitmap
  226. for (U32 row = 0; row < height; ++row)
  227. {
  228. for (U32 col = 0; col < width; ++col)
  229. {
  230. //we can just copy data straight in with RGBA16F format
  231. if (fp16)
  232. {
  233. dMemcpy(destPtr, srcPtr, sizeof(U16) * 4);
  234. }
  235. else
  236. {
  237. destPtr[0] = srcPtr[2]; // red
  238. destPtr[1] = srcPtr[1]; // green
  239. destPtr[2] = srcPtr[0]; // blue
  240. if (destBytesPerPixel == 4)
  241. destPtr[3] = srcPtr[3]; // alpha
  242. }
  243. // go to next pixel in src
  244. srcPtr += sourceBytesPerPixel;
  245. // go to next pixel in dest
  246. destPtr += destBytesPerPixel;
  247. }
  248. // skip past the cache data for this row (if any)
  249. srcPtr += sourceCacheSize;
  250. }
  251. // assert if we stomped or underran memory
  252. AssertFatal(U32(destPtr - bmp->getWritableBits(mip)) == width * height * destBytesPerPixel, "GFXD3D11TextureObject::copyToBmp - memory error");
  253. AssertFatal(U32(srcPtr - (U8*)mappedRes.pData) == height * mappedRes.RowPitch, "GFXD3D11TextureObject::copyToBmp - memory error");
  254. D3D11DEVICECONTEXT->Unmap(pStagingTexture, subResource);
  255. }
  256. SAFE_RELEASE(pStagingTexture);
  257. PROFILE_END();
  258. return true;
  259. }
  260. ID3D11ShaderResourceView* GFXD3D11TextureObject::getSRView()
  261. {
  262. return mSRView;
  263. }
  264. ID3D11RenderTargetView* GFXD3D11TextureObject::getRTView()
  265. {
  266. return mRTView;
  267. }
  268. ID3D11DepthStencilView* GFXD3D11TextureObject::getDSView()
  269. {
  270. return mDSView;
  271. }
  272. ID3D11ShaderResourceView** GFXD3D11TextureObject::getSRViewPtr()
  273. {
  274. return &mSRView;
  275. }
  276. ID3D11RenderTargetView** GFXD3D11TextureObject::getRTViewPtr()
  277. {
  278. return &mRTView;
  279. }
  280. ID3D11DepthStencilView** GFXD3D11TextureObject::getDSViewPtr()
  281. {
  282. return &mDSView;
  283. }