gfxTextureProfile.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. //-----------------------------------------------------------------------------
  59. GFXTextureProfile *GFXTextureProfile::smHead = NULL;
  60. U32 GFXTextureProfile::smProfileCount = 0;
  61. GFXTextureProfile::GFXTextureProfile(const String &name, Types type, U32 flag, Compression compression)
  62. : mName( name )
  63. {
  64. // Take type, flag, and compression and produce a munged profile word.
  65. mProfile = (type & (BIT(TypeBits + 1) - 1)) |
  66. ((flag & (BIT(FlagBits + 1) - 1)) << TypeBits) |
  67. ((compression & (BIT(CompressionBits + 1) - 1)) << (FlagBits + TypeBits));
  68. // Stick us on the linked list.
  69. mNext = smHead;
  70. smHead = this;
  71. ++smProfileCount;
  72. // Now do some sanity checking. (Ben is not proud of this code.)
  73. AssertFatal( (testFlag(Dynamic) && !testFlag(Static))
  74. || (!testFlag(Dynamic) && !testFlag(Static))
  75. || (!testFlag(Dynamic) && testFlag(Static)),
  76. "GFXTextureProfile::GFXTextureProfile - Cannot have a texture profile be both static and dynamic!");
  77. mDownscale = 0;
  78. }
  79. void GFXTextureProfile::init()
  80. {
  81. // Do something, anything?
  82. }
  83. GFXTextureProfile * GFXTextureProfile::find(const String &name)
  84. {
  85. // Not really necessary at this time.
  86. return NULL;
  87. }
  88. void GFXTextureProfile::collectStats( Flags flags, GFXTextureProfileStats *stats )
  89. {
  90. // Walk the profile list.
  91. GFXTextureProfile *curr = smHead;
  92. while ( curr )
  93. {
  94. if ( curr->testFlag( flags ) )
  95. (*stats) += curr->getStats();
  96. curr = curr->mNext;
  97. }
  98. }
  99. void GFXTextureProfile::updateStatsForCreation(GFXTextureObject *t)
  100. {
  101. if(t->mProfile)
  102. {
  103. t->mProfile->incActiveCopies();
  104. t->mProfile->mStats.allocatedTextures++;
  105. U32 texSize = t->getHeight() * t->getWidth();
  106. U32 byteSize = t->getEstimatedSizeInBytes();
  107. t->mProfile->mStats.allocatedTexels += texSize;
  108. t->mProfile->mStats.allocatedBytes += byteSize;
  109. t->mProfile->mStats.activeTexels += texSize;
  110. t->mProfile->mStats.activeBytes += byteSize;
  111. }
  112. }
  113. void GFXTextureProfile::updateStatsForDeletion(GFXTextureObject *t)
  114. {
  115. if(t->mProfile)
  116. {
  117. t->mProfile->decActiveCopies();
  118. U32 texSize = t->getHeight() * t->getWidth();
  119. U32 byteSize = t->getEstimatedSizeInBytes();
  120. t->mProfile->mStats.activeTexels -= texSize;
  121. t->mProfile->mStats.activeBytes -= byteSize;
  122. }
  123. }
  124. DefineEngineFunction( getTextureProfileStats, String, (),,
  125. "Returns a list of texture profiles in the format: ProfileName TextureCount TextureMB\n"
  126. "@ingroup GFX\n" )
  127. {
  128. StringBuilder result;
  129. GFXTextureProfile *profile = GFXTextureProfile::getHead();
  130. while ( profile )
  131. {
  132. const GFXTextureProfileStats &stats = profile->getStats();
  133. F32 mb = ( stats.activeBytes / 1024.0f ) / 1024.0f;
  134. result.format( "%s %d %0.2f\n",
  135. profile->getName().c_str(),
  136. stats.activeCount,
  137. mb );
  138. profile = profile->getNext();
  139. }
  140. return result.end();
  141. }