gfxTextureProfile.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. GFX_ImplementTextureProfile(GFXDynamicCubemapTextureProfile,
  82. GFXTextureProfile::DiffuseMap,
  83. GFXTextureProfile::Dynamic | GFXTextureProfile::CubeMap,
  84. GFXTextureProfile::NONE);
  85. GFX_ImplementTextureProfile(GFXCubemapRenderTargetProfile,
  86. GFXTextureProfile::DiffuseMap,
  87. GFXTextureProfile::PreserveSize | GFXTextureProfile::RenderTarget | GFXTextureProfile::CubeMap,
  88. GFXTextureProfile::NONE);
  89. GFX_ImplementTextureProfile(GFXCubemapStaticTextureProfile, GFXTextureProfile::DiffuseMap,
  90. GFXTextureProfile::Static | GFXTextureProfile::CubeMap,
  91. GFXTextureProfile::NONE);
  92. GFX_ImplementTextureProfile(GFXCubemapTexturePersistentProfile,
  93. GFXTextureProfile::DiffuseMap,
  94. GFXTextureProfile::PreserveSize | GFXTextureProfile::Static | GFXTextureProfile::KeepBitmap | GFXTextureProfile::CubeMap,
  95. GFXTextureProfile::NONE);
  96. //-----------------------------------------------------------------------------
  97. GFXTextureProfile *GFXTextureProfile::smHead = NULL;
  98. U32 GFXTextureProfile::smProfileCount = 0;
  99. GFXTextureProfile::GFXTextureProfile(const String &name, Types type, U32 flag, Compression compression)
  100. : mName( name )
  101. {
  102. // Take type, flag, and compression and produce a munged profile word.
  103. mProfile = (type & (BIT(TypeBits + 1) - 1)) |
  104. ((flag & (BIT(FlagBits + 1) - 1)) << TypeBits) |
  105. ((compression & (BIT(CompressionBits + 1) - 1)) << (FlagBits + TypeBits));
  106. // Stick us on the linked list.
  107. mNext = smHead;
  108. smHead = this;
  109. ++smProfileCount;
  110. // Now do some sanity checking. (Ben is not proud of this code.)
  111. AssertFatal( (testFlag(Dynamic) && !testFlag(Static))
  112. || (!testFlag(Dynamic) && !testFlag(Static))
  113. || (!testFlag(Dynamic) && testFlag(Static)),
  114. "GFXTextureProfile::GFXTextureProfile - Cannot have a texture profile be both static and dynamic!");
  115. mDownscale = 0;
  116. }
  117. void GFXTextureProfile::init()
  118. {
  119. // Do something, anything?
  120. }
  121. GFXTextureProfile * GFXTextureProfile::find(const String &name)
  122. {
  123. // Not really necessary at this time.
  124. return NULL;
  125. }
  126. void GFXTextureProfile::collectStats( Flags flags, GFXTextureProfileStats *stats )
  127. {
  128. // Walk the profile list.
  129. GFXTextureProfile *curr = smHead;
  130. while ( curr )
  131. {
  132. if ( curr->testFlag( flags ) )
  133. (*stats) += curr->getStats();
  134. curr = curr->mNext;
  135. }
  136. }
  137. void GFXTextureProfile::updateStatsForCreation(GFXTextureObject *t)
  138. {
  139. if(t->mProfile)
  140. {
  141. t->mProfile->incActiveCopies();
  142. t->mProfile->mStats.allocatedTextures++;
  143. U32 texSize = t->getHeight() * t->getWidth();
  144. U32 byteSize = t->getEstimatedSizeInBytes();
  145. t->mProfile->mStats.allocatedTexels += texSize;
  146. t->mProfile->mStats.allocatedBytes += byteSize;
  147. t->mProfile->mStats.activeTexels += texSize;
  148. t->mProfile->mStats.activeBytes += byteSize;
  149. }
  150. }
  151. void GFXTextureProfile::updateStatsForDeletion(GFXTextureObject *t)
  152. {
  153. if(t->mProfile)
  154. {
  155. t->mProfile->decActiveCopies();
  156. U32 texSize = t->getHeight() * t->getWidth();
  157. U32 byteSize = t->getEstimatedSizeInBytes();
  158. t->mProfile->mStats.activeTexels -= texSize;
  159. t->mProfile->mStats.activeBytes -= byteSize;
  160. }
  161. }
  162. DefineEngineFunction( getTextureProfileStats, String, (),,
  163. "Returns a list of texture profiles in the format: ProfileName TextureCount TextureMB\n"
  164. "@ingroup GFX\n" )
  165. {
  166. StringBuilder result;
  167. GFXTextureProfile *profile = GFXTextureProfile::getHead();
  168. while ( profile )
  169. {
  170. const GFXTextureProfileStats &stats = profile->getStats();
  171. F32 mb = ( stats.activeBytes / 1024.0f ) / 1024.0f;
  172. result.format( "%s %d %0.2f\n",
  173. profile->getName().c_str(),
  174. stats.activeCount,
  175. mb );
  176. profile = profile->getNext();
  177. }
  178. return result.end();
  179. }