gfxD3D11TextureObject.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. mLockedSubresource = 0;
  42. mDSView = NULL;
  43. mRTView = NULL;
  44. mSRView = NULL;
  45. }
  46. GFXD3D11TextureObject::~GFXD3D11TextureObject()
  47. {
  48. kill();
  49. #ifdef D3D11_DEBUG_SPEW
  50. mTexCount--;
  51. Con::printf("+ texkill %d %x", mTexCount, this);
  52. #endif
  53. }
  54. GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect /*= NULL*/)
  55. {
  56. AssertFatal( !mLocked, "GFXD3D11TextureObject::lock - The texture is already locked!" );
  57. if( !mStagingTex ||
  58. mStagingTex->getWidth() != getWidth() ||
  59. mStagingTex->getHeight() != getHeight() )
  60. {
  61. mStagingTex.set( getWidth(), getHeight(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__) );
  62. }
  63. ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT;
  64. D3D11_MAPPED_SUBRESOURCE mapInfo;
  65. U32 offset = 0;
  66. mLockedSubresource = D3D11CalcSubresource(mipLevel, 0, getMipLevels());
  67. GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex);
  68. //map staging texture
  69. HRESULT hr = pContext->Map(pD3DStagingTex->get2DTex(), mLockedSubresource, D3D11_MAP_READ, 0, &mapInfo);
  70. if (FAILED(hr))
  71. AssertFatal(false, "GFXD3D11TextureObject:lock - failed to map render target resource!");
  72. const U32 width = mTextureSize.x >> mipLevel;
  73. const U32 height = mTextureSize.y >> mipLevel;
  74. //calculate locked box region and offset
  75. if (inRect)
  76. {
  77. if ((inRect->point.x + inRect->extent.x > width) || (inRect->point.y + inRect->extent.y > height))
  78. AssertFatal(false, "GFXD3D11TextureObject::lock - Rectangle too big!");
  79. mLockBox.top = inRect->point.y;
  80. mLockBox.left = inRect->point.x;
  81. mLockBox.bottom = inRect->point.y + inRect->extent.y;
  82. mLockBox.right = inRect->point.x + inRect->extent.x;
  83. mLockBox.back = 1;
  84. mLockBox.front = 0;
  85. //calculate offset
  86. offset = inRect->point.x * getFormatByteSize() + inRect->point.y * mapInfo.RowPitch;
  87. }
  88. else
  89. {
  90. mLockBox.top = 0;
  91. mLockBox.left = 0;
  92. mLockBox.bottom = height;
  93. mLockBox.right = width;
  94. mLockBox.back = 1;
  95. mLockBox.front = 0;
  96. }
  97. mLocked = true;
  98. mLockRect.pBits = static_cast<U8*>(mapInfo.pData) + offset;
  99. mLockRect.Pitch = mapInfo.RowPitch;
  100. return (GFXLockedRect*)&mLockRect;
  101. }
  102. void GFXD3D11TextureObject::unlock(U32 mipLevel)
  103. {
  104. AssertFatal( mLocked, "GFXD3D11TextureObject::unlock - Attempting to unlock a surface that has not been locked" );
  105. //profile in the unlock function because all the heavy lifting is done here
  106. PROFILE_START(GFXD3D11TextureObject_lockRT);
  107. ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT;
  108. GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex);
  109. ID3D11Texture2D *pStagingTex = pD3DStagingTex->get2DTex();
  110. //unmap staging texture
  111. pContext->Unmap(pStagingTex, mLockedSubresource);
  112. //copy lock box region from the staging texture to our regular texture
  113. pContext->CopySubresourceRegion(mD3DTexture, mLockedSubresource, mLockBox.left, mLockBox.top, 0, pStagingTex, mLockedSubresource, &mLockBox);
  114. PROFILE_END();
  115. mLockedSubresource = 0;
  116. mLocked = false;
  117. }
  118. void GFXD3D11TextureObject::release()
  119. {
  120. SAFE_RELEASE(mSRView);
  121. SAFE_RELEASE(mRTView);
  122. SAFE_RELEASE(mDSView);
  123. SAFE_RELEASE(mD3DTexture);
  124. SAFE_RELEASE(mD3DSurface);
  125. }
  126. void GFXD3D11TextureObject::zombify()
  127. {
  128. // Managed textures are managed by D3D
  129. AssertFatal(!mLocked, "GFXD3D11TextureObject::zombify - Cannot zombify a locked texture!");
  130. if(isManaged)
  131. return;
  132. release();
  133. }
  134. void GFXD3D11TextureObject::resurrect()
  135. {
  136. // Managed textures are managed by D3D
  137. if(isManaged)
  138. return;
  139. static_cast<GFXD3D11TextureManager*>(TEXMGR)->refreshTexture(this);
  140. }
  141. bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
  142. {
  143. if (!bmp)
  144. return false;
  145. // check format limitations
  146. // at the moment we only support RGBA for the source (other 4 byte formats should
  147. // be easy to add though)
  148. AssertFatal(mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB, "copyToBmp: invalid format");
  149. if (mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB)
  150. return false;
  151. PROFILE_START(GFXD3D11TextureObject_copyToBmp);
  152. AssertFatal(bmp->getWidth() == getWidth(), "GFXD3D11TextureObject::copyToBmp - source/dest width does not match");
  153. AssertFatal(bmp->getHeight() == getHeight(), "GFXD3D11TextureObject::copyToBmp - source/dest height does not match");
  154. const U32 width = getWidth();
  155. const U32 height = getHeight();
  156. bmp->setHasTransparency(mHasTransparency);
  157. // set some constants
  158. const U32 sourceBytesPerPixel = 4;
  159. U32 destBytesPerPixel = 0;
  160. const GFXFormat fmt = bmp->getFormat();
  161. if (fmt == GFXFormatR8G8B8A8 || fmt == GFXFormatR8G8B8A8_LINEAR_FORCE || fmt == GFXFormatR8G8B8A8_SRGB)
  162. destBytesPerPixel = 4;
  163. else if(bmp->getFormat() == GFXFormatR8G8B8)
  164. destBytesPerPixel = 3;
  165. else
  166. // unsupported
  167. AssertFatal(false, "GFXD3D11TextureObject::copyToBmp - unsupported bitmap format");
  168. //create temp staging texture
  169. D3D11_TEXTURE2D_DESC desc;
  170. static_cast<ID3D11Texture2D*>(mD3DTexture)->GetDesc(&desc);
  171. desc.BindFlags = 0;
  172. desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
  173. desc.Usage = D3D11_USAGE_STAGING;
  174. ID3D11Texture2D* pStagingTexture = NULL;
  175. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &pStagingTexture);
  176. if (FAILED(hr))
  177. {
  178. Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to create staging texture");
  179. return false;
  180. }
  181. //copy the classes texture to the staging texture
  182. D3D11DEVICECONTEXT->CopyResource(pStagingTexture, mD3DTexture);
  183. //map the staging resource
  184. D3D11_MAPPED_SUBRESOURCE mappedRes;
  185. hr = D3D11DEVICECONTEXT->Map(pStagingTexture, 0, D3D11_MAP_READ, 0, &mappedRes);
  186. if (FAILED(hr))
  187. {
  188. //cleanup
  189. SAFE_RELEASE(pStagingTexture);
  190. Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to map staging texture");
  191. return false;
  192. }
  193. // set pointers
  194. const U8* srcPtr = (U8*)mappedRes.pData;
  195. U8* destPtr = bmp->getWritableBits();
  196. // we will want to skip over any D3D cache data in the source texture
  197. const S32 sourceCacheSize = mappedRes.RowPitch - width * sourceBytesPerPixel;
  198. AssertFatal(sourceCacheSize >= 0, "GFXD3D11TextureObject::copyToBmp - cache size is less than zero?");
  199. // copy data into bitmap
  200. for (U32 row = 0; row < height; ++row)
  201. {
  202. for (U32 col = 0; col < width; ++col)
  203. {
  204. destPtr[0] = srcPtr[2]; // red
  205. destPtr[1] = srcPtr[1]; // green
  206. destPtr[2] = srcPtr[0]; // blue
  207. if (destBytesPerPixel == 4)
  208. destPtr[3] = srcPtr[3]; // alpha
  209. // go to next pixel in src
  210. srcPtr += sourceBytesPerPixel;
  211. // go to next pixel in dest
  212. destPtr += destBytesPerPixel;
  213. }
  214. // skip past the cache data for this row (if any)
  215. srcPtr += sourceCacheSize;
  216. }
  217. // assert if we stomped or underran memory
  218. AssertFatal(U32(destPtr - bmp->getWritableBits()) == width * height * destBytesPerPixel, "GFXD3D11TextureObject::copyToBmp - memory error");
  219. AssertFatal(U32(srcPtr - (U8*)mappedRes.pData) == height * mappedRes.RowPitch, "GFXD3D11TextureObject::copyToBmp - memory error");
  220. D3D11DEVICECONTEXT->Unmap(pStagingTexture, 0);
  221. SAFE_RELEASE(pStagingTexture);
  222. PROFILE_END();
  223. return true;
  224. }
  225. ID3D11ShaderResourceView* GFXD3D11TextureObject::getSRView()
  226. {
  227. return mSRView;
  228. }
  229. ID3D11RenderTargetView* GFXD3D11TextureObject::getRTView()
  230. {
  231. return mRTView;
  232. }
  233. ID3D11DepthStencilView* GFXD3D11TextureObject::getDSView()
  234. {
  235. return mDSView;
  236. }
  237. ID3D11ShaderResourceView** GFXD3D11TextureObject::getSRViewPtr()
  238. {
  239. return &mSRView;
  240. }
  241. ID3D11RenderTargetView** GFXD3D11TextureObject::getRTViewPtr()
  242. {
  243. return &mRTView;
  244. }
  245. ID3D11DepthStencilView** GFXD3D11TextureObject::getDSViewPtr()
  246. {
  247. return &mDSView;
  248. }