gfxTextureObject.cpp 7.6 KB

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