gfxTextureObject.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if defined(TORQUE_DEBUG)
  80. // Active object tracking.
  81. smActiveTOCount++;
  82. mDebugDescription = "Anonymous Texture Object";
  83. #if defined(TORQUE_ENABLE_PROFILE_PATH)
  84. mDebugCreationPath = gProfiler->getProfilePath();
  85. #endif
  86. mDebugNext = smHead;
  87. mDebugPrev = NULL;
  88. if(smHead)
  89. {
  90. AssertFatal(smHead->mDebugPrev == NULL, "GFXTextureObject::GFXTextureObject - found unexpected previous in current head!");
  91. smHead->mDebugPrev = this;
  92. }
  93. smHead = this;
  94. #endif
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Destructor
  98. //-----------------------------------------------------------------------------
  99. GFXTextureObject::~GFXTextureObject()
  100. {
  101. kill();
  102. #ifdef TORQUE_DEBUG
  103. if(smHead == this)
  104. smHead = this->mDebugNext;
  105. if(mDebugNext)
  106. mDebugNext->mDebugPrev = mDebugPrev;
  107. if(mDebugPrev)
  108. mDebugPrev->mDebugNext = mDebugNext;
  109. mDebugPrev = mDebugNext = NULL;
  110. smActiveTOCount--;
  111. #endif
  112. }
  113. void GFXTextureObject::destroySelf()
  114. {
  115. if (mDevice && mDevice->mTextureManager)
  116. mDevice->mTextureManager->requestDeleteTexture(this);
  117. }
  118. //-----------------------------------------------------------------------------
  119. // kill - this function clears out the data in texture object. It's done like
  120. // this because the texture object needs to release its pointers to textures
  121. // before the GFXDevice is shut down. The texture objects themselves get
  122. // deleted by the refcount structure - which may be after the GFXDevice has
  123. // been destroyed.
  124. //-----------------------------------------------------------------------------
  125. void GFXTextureObject::kill()
  126. {
  127. if( mDead )
  128. return;
  129. #ifdef TORQUE_DEBUG
  130. // This makes sure that nobody is forgetting to call kill from the derived
  131. // destructor. If they are, then we should get a pure virtual function
  132. // call here.
  133. pureVirtualCrash();
  134. #endif
  135. // If we're a dummy, don't do anything...
  136. if( !mDevice || !mDevice->mTextureManager )
  137. {
  138. mDead = true;
  139. return;
  140. }
  141. // Remove ourselves from the texture list and hash
  142. mDevice->mTextureManager->deleteTexture(this);
  143. // Delete the stored bitmap.
  144. SAFE_DELETE(mBitmap)
  145. SAFE_DELETE(mDDS);
  146. // Clean up linked list
  147. if(mNext)
  148. mNext->mPrev = mPrev;
  149. if(mPrev)
  150. mPrev->mNext = mNext;
  151. mDead = true;
  152. }
  153. const String GFXTextureObject::describeSelf() const
  154. {
  155. return String::ToString(" (width: %4d, height: %4d) profile: %s creation path: %s", getWidth(),
  156. #if defined(TORQUE_DEBUG) && defined(TORQUE_ENABLE_PROFILER)
  157. getHeight(), mProfile->getName().c_str(), mDebugCreationPath.c_str());
  158. #else
  159. getHeight(), mProfile->getName().c_str(), "");
  160. #endif
  161. }
  162. F32 GFXTextureObject::getMaxUCoord() const
  163. {
  164. return 1.0f;
  165. }
  166. F32 GFXTextureObject::getMaxVCoord() const
  167. {
  168. return 1.0f;
  169. }
  170. U32 GFXTextureObject::getEstimatedSizeInBytes() const
  171. {
  172. // We have to deal with DDS specially.
  173. if ( mFormat >= GFXFormatBC1 )
  174. return DDSFile::getSizeInBytes( mFormat, mTextureSize.x, mTextureSize.y, mMipLevels );
  175. // Else we need to calculate the size ourselves.
  176. S32 texSizeX = mTextureSize.x;
  177. S32 texSizeY = mTextureSize.y;
  178. S32 volDepth = getMax( 1, mTextureSize.z );
  179. U32 byteSize = GFXFormat_getByteSize( mFormat );
  180. U32 totalBytes = texSizeX * texSizeY * volDepth * byteSize;
  181. // Without mips we're done.
  182. if ( mProfile->noMip() )
  183. return totalBytes;
  184. // NOTE: While we have mMipLevels, at the time of this
  185. // comment it only stores the accessable mip levels and
  186. // not the count of the autogen mips.
  187. //
  188. // So we figure out the mip count ourselves assuming its
  189. // a complete mip chain.
  190. while ( texSizeX > 1 || texSizeY > 1 )
  191. {
  192. texSizeX = getMax( texSizeX >> 1, 1 );
  193. texSizeY = getMax( texSizeY >> 1, 1 );
  194. volDepth = getMax( volDepth >> 1, 1 );
  195. totalBytes += texSizeX * texSizeY * volDepth * byteSize;
  196. }
  197. return totalBytes;
  198. }
  199. bool GFXTextureObject::dumpToDisk( const String &bmType, const String &path )
  200. {
  201. if ( mBitmap )
  202. return mBitmap->writeBitmap( bmType, path);
  203. GBitmap bitmap( getWidth(), getHeight(), false, getFormat() );
  204. copyToBmp( &bitmap );
  205. return bitmap.writeBitmap( bmType, path);
  206. }