gfxTextureObject.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gfx/gfxTextureObject.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/gfxTextureManager.h"
  26. #include "core/util/safeDelete.h"
  27. #include "core/strings/stringFunctions.h"
  28. #include "core/stream/fileStream.h"
  29. #include "console/console.h"
  30. #include "console/engineAPI.h"
  31. // TODO: Change this to be in non-shipping builds maybe?
  32. #ifdef TORQUE_DEBUG
  33. GFXTextureObject *GFXTextureObject::smHead = NULL;
  34. U32 GFXTextureObject::smActiveTOCount = 0;
  35. U32 GFXTextureObject::dumpActiveTOs()
  36. {
  37. if(!smActiveTOCount)
  38. {
  39. Con::printf( "GFXTextureObject::dumpActiveTOs - no active TOs to dump." );
  40. return 0;
  41. }
  42. Con::printf("GFXTextureObject Usage Report - %d active TOs", smActiveTOCount);
  43. Con::printf("---------------------------------------------------------------");
  44. Con::printf(" Addr Dim. GFXTextureProfile ProfilerPath DebugDescription");
  45. for(GFXTextureObject *walk = smHead; walk; walk=walk->mDebugNext)
  46. Con::printf(" %x (%4d, %4d) %s %s %s", walk, walk->getWidth(),
  47. walk->getHeight(), walk->mProfile->getName().c_str(), walk->mDebugCreationPath.c_str(), walk->mDebugDescription.c_str());
  48. Con::printf("----- dump complete -------------------------------------------");
  49. return smActiveTOCount;
  50. }
  51. #endif // TORQUE_DEBUG
  52. DefineEngineFunction( dumpTextureObjects, void, (),,
  53. "Dumps a list of all active texture objects to the console.\n"
  54. "@note This function is only available in debug builds.\n"
  55. "@ingroup GFX\n" )
  56. {
  57. #ifdef TORQUE_DEBUG
  58. GFXTextureObject::dumpActiveTOs();
  59. #endif
  60. }
  61. //-----------------------------------------------------------------------------
  62. // GFXTextureObject
  63. //-----------------------------------------------------------------------------
  64. GFXTextureObject::GFXTextureObject(GFXDevice *aDevice, GFXTextureProfile *aProfile)
  65. {
  66. mHashNext = mNext = mPrev = NULL;
  67. mDevice = aDevice;
  68. mProfile = aProfile;
  69. mBitmap = NULL;
  70. mMipLevels = 1;
  71. mAntialiasLevel = 0;
  72. mTextureSize.set( 0, 0, 0 );
  73. mDead = false;
  74. mDeleteTime = 0;
  75. mBitmap = NULL;
  76. mDDS = NULL;
  77. mFormat = GFXFormatR8G8B8;
  78. mHasTransparency = false;
  79. mArraySize = 1;
  80. #if defined(TORQUE_DEBUG)
  81. // Active object tracking.
  82. smActiveTOCount++;
  83. mDebugDescription = "Anonymous Texture Object";
  84. #if defined(TORQUE_ENABLE_PROFILE_PATH)
  85. mDebugCreationPath = gProfiler->getProfilePath();
  86. #endif
  87. mDebugNext = smHead;
  88. mDebugPrev = NULL;
  89. if(smHead)
  90. {
  91. AssertFatal(smHead->mDebugPrev == NULL, "GFXTextureObject::GFXTextureObject - found unexpected previous in current head!");
  92. smHead->mDebugPrev = this;
  93. }
  94. smHead = this;
  95. #endif
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Destructor
  99. //-----------------------------------------------------------------------------
  100. GFXTextureObject::~GFXTextureObject()
  101. {
  102. kill();
  103. #ifdef TORQUE_DEBUG
  104. if(smHead == this)
  105. smHead = this->mDebugNext;
  106. if(mDebugNext)
  107. mDebugNext->mDebugPrev = mDebugPrev;
  108. if(mDebugPrev)
  109. mDebugPrev->mDebugNext = mDebugNext;
  110. mDebugPrev = mDebugNext = NULL;
  111. smActiveTOCount--;
  112. #endif
  113. }
  114. void GFXTextureObject::destroySelf()
  115. {
  116. if (mDevice && mDevice->mTextureManager)
  117. mDevice->mTextureManager->requestDeleteTexture(this);
  118. }
  119. //-----------------------------------------------------------------------------
  120. // kill - this function clears out the data in texture object. It's done like
  121. // this because the texture object needs to release its pointers to textures
  122. // before the GFXDevice is shut down. The texture objects themselves get
  123. // deleted by the refcount structure - which may be after the GFXDevice has
  124. // been destroyed.
  125. //-----------------------------------------------------------------------------
  126. void GFXTextureObject::kill()
  127. {
  128. if( mDead )
  129. return;
  130. #ifdef TORQUE_DEBUG
  131. // This makes sure that nobody is forgetting to call kill from the derived
  132. // destructor. If they are, then we should get a pure virtual function
  133. // call here.
  134. pureVirtualCrash();
  135. #endif
  136. // If we're a dummy, don't do anything...
  137. if( !mDevice || !mDevice->mTextureManager )
  138. {
  139. mDead = true;
  140. return;
  141. }
  142. // Remove ourselves from the texture list and hash
  143. mDevice->mTextureManager->deleteTexture(this);
  144. // Delete the stored bitmap.
  145. SAFE_DELETE(mBitmap)
  146. SAFE_DELETE(mDDS);
  147. // Clean up linked list
  148. if(mNext)
  149. mNext->mPrev = mPrev;
  150. if(mPrev)
  151. mPrev->mNext = mNext;
  152. mDead = true;
  153. }
  154. const String GFXTextureObject::describeSelf() const
  155. {
  156. return String::ToString(" (width: %4d, height: %4d) profile: %s creation path: %s", getWidth(),
  157. #if defined(TORQUE_DEBUG) && defined(TORQUE_ENABLE_PROFILER)
  158. getHeight(), mProfile->getName().c_str(), mDebugCreationPath.c_str());
  159. #else
  160. getHeight(), mProfile->getName().c_str(), "");
  161. #endif
  162. }
  163. F32 GFXTextureObject::getMaxUCoord() const
  164. {
  165. return 1.0f;
  166. }
  167. F32 GFXTextureObject::getMaxVCoord() const
  168. {
  169. return 1.0f;
  170. }
  171. U32 GFXTextureObject::getEstimatedSizeInBytes() const
  172. {
  173. // We have to deal with DDS specially.
  174. if ( mFormat >= GFXFormatBC1 )
  175. return DDSFile::getSizeInBytes( mFormat, mTextureSize.x, mTextureSize.y, mMipLevels );
  176. // Else we need to calculate the size ourselves.
  177. S32 texSizeX = mTextureSize.x;
  178. S32 texSizeY = mTextureSize.y;
  179. S32 volDepth = getMax( 1, mTextureSize.z );
  180. U32 byteSize = GFXFormat_getByteSize( mFormat );
  181. U32 totalBytes = texSizeX * texSizeY * volDepth * byteSize;
  182. // Without mips we're done.
  183. if ( mProfile->noMip() )
  184. return totalBytes;
  185. // NOTE: While we have mMipLevels, at the time of this
  186. // comment it only stores the accessable mip levels and
  187. // not the count of the autogen mips.
  188. //
  189. // So we figure out the mip count ourselves assuming its
  190. // a complete mip chain.
  191. while ( texSizeX > 1 || texSizeY > 1 )
  192. {
  193. texSizeX = getMax( texSizeX >> 1, 1 );
  194. texSizeY = getMax( texSizeY >> 1, 1 );
  195. volDepth = getMax( volDepth >> 1, 1 );
  196. totalBytes += texSizeX * texSizeY * volDepth * byteSize;
  197. }
  198. return totalBytes;
  199. }
  200. bool GFXTextureObject::dumpToDisk( const String &bmType, const String &path )
  201. {
  202. if ( mBitmap )
  203. return mBitmap->writeBitmap( bmType, path);
  204. GBitmap bitmap( getWidth(), getHeight(), false, getFormat() );
  205. copyToBmp( &bitmap );
  206. return bitmap.writeBitmap( bmType, path);
  207. }