gfxTextureProfile.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/gfxTextureProfile.h"
  24. #include "gfx/gfxTextureObject.h"
  25. #include "gfx/bitmap/gBitmap.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "console/console.h"
  28. #include "console/engineAPI.h"
  29. // Set up defaults...
  30. GFX_ImplementTextureProfile(GFXDefaultRenderTargetProfile,
  31. GFXTextureProfile::DiffuseMap,
  32. GFXTextureProfile::PreserveSize | GFXTextureProfile::NoMipmap | GFXTextureProfile::RenderTarget,
  33. GFXTextureProfile::NONE);
  34. GFX_ImplementTextureProfile(GFXDefaultStaticDiffuseProfile,
  35. GFXTextureProfile::DiffuseMap,
  36. GFXTextureProfile::Static,
  37. GFXTextureProfile::NONE);
  38. GFX_ImplementTextureProfile(GFXDefaultStaticNormalMapProfile,
  39. GFXTextureProfile::NormalMap,
  40. GFXTextureProfile::Static,
  41. GFXTextureProfile::NONE);
  42. GFX_ImplementTextureProfile(GFXDefaultStaticDXT5nmProfile,
  43. GFXTextureProfile::NormalMap,
  44. GFXTextureProfile::Static,
  45. GFXTextureProfile::DXT5);
  46. GFX_ImplementTextureProfile(GFXDefaultPersistentProfile,
  47. GFXTextureProfile::DiffuseMap,
  48. GFXTextureProfile::PreserveSize | GFXTextureProfile::Static | GFXTextureProfile::KeepBitmap,
  49. GFXTextureProfile::NONE);
  50. GFX_ImplementTextureProfile(GFXSystemMemProfile,
  51. GFXTextureProfile::DiffuseMap,
  52. GFXTextureProfile::PreserveSize | GFXTextureProfile::NoMipmap | GFXTextureProfile::SystemMemory,
  53. GFXTextureProfile::NONE);
  54. GFX_ImplementTextureProfile(GFXDefaultZTargetProfile,
  55. GFXTextureProfile::DiffuseMap,
  56. GFXTextureProfile::PreserveSize | GFXTextureProfile::NoMipmap | GFXTextureProfile::ZTarget | GFXTextureProfile::NoDiscard,
  57. GFXTextureProfile::NONE);
  58. GFX_ImplementTextureProfile(GFXDynamicTextureProfile,
  59. GFXTextureProfile::DiffuseMap,
  60. GFXTextureProfile::Dynamic,
  61. GFXTextureProfile::NONE);
  62. //-----------------------------------------------------------------------------
  63. GFXTextureProfile *GFXTextureProfile::smHead = NULL;
  64. U32 GFXTextureProfile::smProfileCount = 0;
  65. GFXTextureProfile::GFXTextureProfile(const String &name, Types type, U32 flag, Compression compression)
  66. : mName( name )
  67. {
  68. // Take type, flag, and compression and produce a munged profile word.
  69. mProfile = (type & (BIT(TypeBits + 1) - 1)) |
  70. ((flag & (BIT(FlagBits + 1) - 1)) << TypeBits) |
  71. ((compression & (BIT(CompressionBits + 1) - 1)) << (FlagBits + TypeBits));
  72. // Stick us on the linked list.
  73. mNext = smHead;
  74. smHead = this;
  75. ++smProfileCount;
  76. // Now do some sanity checking. (Ben is not proud of this code.)
  77. AssertFatal( (testFlag(Dynamic) && !testFlag(Static))
  78. || (!testFlag(Dynamic) && !testFlag(Static))
  79. || (!testFlag(Dynamic) && testFlag(Static)),
  80. "GFXTextureProfile::GFXTextureProfile - Cannot have a texture profile be both static and dynamic!");
  81. mDownscale = 0;
  82. }
  83. void GFXTextureProfile::init()
  84. {
  85. // Do something, anything?
  86. }
  87. GFXTextureProfile * GFXTextureProfile::find(const String &name)
  88. {
  89. // Not really necessary at this time.
  90. return NULL;
  91. }
  92. void GFXTextureProfile::collectStats( Flags flags, GFXTextureProfileStats *stats )
  93. {
  94. // Walk the profile list.
  95. GFXTextureProfile *curr = smHead;
  96. while ( curr )
  97. {
  98. if ( curr->testFlag( flags ) )
  99. (*stats) += curr->getStats();
  100. curr = curr->mNext;
  101. }
  102. }
  103. void GFXTextureProfile::updateStatsForCreation(GFXTextureObject *t)
  104. {
  105. if(t->mProfile)
  106. {
  107. t->mProfile->incActiveCopies();
  108. t->mProfile->mStats.allocatedTextures++;
  109. U32 texSize = t->getHeight() * t->getWidth();
  110. U32 byteSize = t->getEstimatedSizeInBytes();
  111. t->mProfile->mStats.allocatedTexels += texSize;
  112. t->mProfile->mStats.allocatedBytes += byteSize;
  113. t->mProfile->mStats.activeTexels += texSize;
  114. t->mProfile->mStats.activeBytes += byteSize;
  115. }
  116. }
  117. void GFXTextureProfile::updateStatsForDeletion(GFXTextureObject *t)
  118. {
  119. if(t->mProfile)
  120. {
  121. t->mProfile->decActiveCopies();
  122. U32 texSize = t->getHeight() * t->getWidth();
  123. U32 byteSize = t->getEstimatedSizeInBytes();
  124. t->mProfile->mStats.activeTexels -= texSize;
  125. t->mProfile->mStats.activeBytes -= byteSize;
  126. }
  127. }
  128. DefineEngineFunction( getTextureProfileStats, String, (),,
  129. "Returns a list of texture profiles in the format: ProfileName TextureCount TextureMB\n"
  130. "@ingroup GFX\n" )
  131. {
  132. StringBuilder result;
  133. GFXTextureProfile *profile = GFXTextureProfile::getHead();
  134. while ( profile )
  135. {
  136. const GFXTextureProfileStats &stats = profile->getStats();
  137. F32 mb = ( stats.activeBytes / 1024.0f ) / 1024.0f;
  138. result.format( "%s %d %0.2f\n",
  139. profile->getName().c_str(),
  140. stats.activeCount,
  141. mb );
  142. profile = profile->getNext();
  143. }
  144. return result.end();
  145. }