gfxTextureObject.cpp 7.6 KB

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