gfxTextureProfile.cpp 7.8 KB

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