gfxTextureProfile.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #pragma once
  23. #ifndef _GFXTEXTUREPROFILE_H_
  24. #define _GFXTEXTUREPROFILE_H_
  25. #ifndef _TORQUE_STRING_H_
  26. #include "core/util/str.h"
  27. #endif
  28. class GFXTextureObject;
  29. /// Helper struct for gathering profile stats.
  30. class GFXTextureProfileStats
  31. {
  32. public:
  33. /// Constructs and clears the stats.
  34. GFXTextureProfileStats() { clear(); }
  35. /// Zeros all the stats.
  36. void clear()
  37. {
  38. dMemset( this, 0, sizeof( GFXTextureProfileStats ) );
  39. }
  40. /// Adds stats together.
  41. GFXTextureProfileStats& operator += ( const GFXTextureProfileStats &stats )
  42. {
  43. activeCount += stats.activeCount;
  44. activeTexels += stats.activeTexels;
  45. activeBytes += stats.activeBytes;
  46. allocatedTextures += stats.allocatedTextures;
  47. allocatedTexels += stats.allocatedTexels;
  48. allocatedBytes += stats.allocatedBytes;
  49. return *this;
  50. }
  51. U32 activeCount; ///< Count of textures of this profile type allocated.
  52. U32 activeTexels; ///< Amount of texelspace currently allocated under this profile.
  53. U32 activeBytes; ///< Amount of storage currently allocated under this profile.
  54. U32 allocatedTextures; ///< Total number of textures allocated under this profile.
  55. U32 allocatedTexels; ///< Total number of texels allocated under this profile.
  56. U32 allocatedBytes; ///< Total number of bytes allocated under this profile.
  57. };
  58. class GFXTextureProfile
  59. {
  60. public:
  61. enum Types
  62. {
  63. DiffuseMap,
  64. NormalMap,
  65. AlphaMap,
  66. LuminanceMap
  67. };
  68. enum Flags
  69. {
  70. PreserveSize = BIT(0), ///< Never shrink this bitmap in low VRAM situations.
  71. NoMipmap = BIT(1), ///< Do not generate mipmap chain for this texture.
  72. SystemMemory = BIT(2), ///< System memory texture - isn't uploaded to card - useful as target for copying surface data out of video ram
  73. RenderTarget = BIT(3), ///< This texture will be used as a render target.
  74. Dynamic = BIT(4), ///< This texture may be refreshed. (Precludes Static)
  75. Static = BIT(5), ///< This texture will never be modified once loaded. (Precludes Dynamic)
  76. NoPadding = BIT(6), ///< Do not pad this texture if it's non pow2.
  77. KeepBitmap = BIT(7), ///< Always keep a copy of this texture's bitmap. (Potentially in addition to the API managed copy?)
  78. ZTarget = BIT(8), ///< This texture will be used as a Z target.
  79. SRGB = BIT(9), ///< sRGB texture
  80. /// Track and pool textures of this type for reuse.
  81. ///
  82. /// You should use this profile flag sparingly. Odd
  83. /// sized textures and spikes in allocation can cause
  84. /// the pool to contain unused textures which will remain
  85. /// in memory until a flush occurs.
  86. ///
  87. Pooled = BIT(10),
  88. /// A hint that the device is not allowed to discard the content
  89. /// of a target texture after presentation or deactivated.
  90. ///
  91. /// This is mainly a depth buffer optimization.
  92. NoDiscard = BIT(11),
  93. NoModify = BIT(11)
  94. };
  95. enum Compression
  96. {
  97. NONE,
  98. BC1,
  99. BC2,
  100. BC3,
  101. BC4,
  102. BC5,
  103. };
  104. GFXTextureProfile(const String &name, Types type, U32 flags, Compression compression = NONE);
  105. // Equality operators
  106. inline bool operator==(const GFXTextureProfile &in_Cmp) const { return (mName == in_Cmp.mName && mProfile == in_Cmp.mProfile); }
  107. inline bool operator!=(const GFXTextureProfile &in_Cmp) const { return !(*this == in_Cmp); }
  108. // Accessors
  109. String getName() const { return mName; };
  110. Types getType() const { return (Types)(mProfile & (BIT(TypeBits) - 1)); }
  111. const Compression getCompression() const { return (Compression)((mProfile >> (FlagBits + TypeBits)) & (BIT(CompressionBits + 1) - 1)); };
  112. bool testFlag(Flags flag) const
  113. {
  114. return (mProfile & (flag << TypeBits)) != 0;
  115. }
  116. // Mutators
  117. const U32 getDownscale() const { return mDownscale; }
  118. void setDownscale(const U32 shift) { mDownscale = shift; }
  119. void incActiveCopies() { mStats.activeCount++; }
  120. void decActiveCopies() { AssertFatal( mStats.activeCount != 0, "Ran out of extant copies!"); mStats.activeCount--; }
  121. // And static interface...
  122. static void init();
  123. static GFXTextureProfile *find(const String &name);
  124. static void updateStatsForCreation(GFXTextureObject *t);
  125. static void updateStatsForDeletion(GFXTextureObject *t);
  126. /// Collects the total stats for all the profiles which
  127. /// include any of the flag bits.
  128. static void collectStats( Flags flags, GFXTextureProfileStats *stats );
  129. /// Returns the total profile count in the list.
  130. static U32 getProfileCount() { return smProfileCount; }
  131. /// Returns the head of the profile list.
  132. static GFXTextureProfile* getHead() { return smHead; }
  133. /// Returns the next profile in the list.
  134. GFXTextureProfile* getNext() const { return mNext; }
  135. /// Returns the allocation stats for this texture profile.
  136. inline const GFXTextureProfileStats& getStats() const { return mStats; }
  137. // Helper functions...
  138. inline bool doStoreBitmap() const { return testFlag(KeepBitmap); }
  139. inline bool canDownscale() const { return !testFlag(PreserveSize); }
  140. inline bool isDynamic() const { return testFlag(Dynamic); }
  141. inline bool isRenderTarget() const { return testFlag(RenderTarget); }
  142. inline bool isZTarget() const { return testFlag(ZTarget); }
  143. inline bool isSystemMemory() const { return testFlag(SystemMemory); }
  144. inline bool noMip() const { return testFlag(NoMipmap); }
  145. inline bool isPooled() const { return testFlag(Pooled); }
  146. inline bool canDiscard() const { return !testFlag(NoDiscard); }
  147. inline bool isSRGB() const { return testFlag(SRGB); }
  148. //compare profile flags for equality
  149. inline bool compareFlags(const GFXTextureProfile& in_Cmp) const{ return (mProfile == in_Cmp.mProfile); }
  150. private:
  151. /// These constants control the packing for the profile; if you add flags, types, or
  152. /// compression info then make sure these are giving enough bits!
  153. enum Constants
  154. {
  155. TypeBits = 2,
  156. FlagBits = 12,
  157. CompressionBits = 3,
  158. };
  159. String mName; ///< Name of this profile...
  160. U32 mDownscale; ///< Amount to shift textures of this type down, if any.
  161. U32 mProfile; ///< Stores a munged version of the profile data.
  162. U32 mActiveCount; ///< Count of textures of this profile type allocated.
  163. U32 mActiveTexels; ///< Amount of texelspace currently allocated under this profile.
  164. U32 mActiveBytes; ///< Amount of storage currently allocated under this profile.
  165. U32 mAllocatedTextures; ///< Total number of textures allocated under this profile.
  166. U32 mAllocatedTexels; ///< Total number of texels allocated under this profile.
  167. U32 mAllocatedBytes; ///< Total number of bytes allocated under this profile.
  168. /// The texture profile stats.
  169. GFXTextureProfileStats mStats;
  170. /// The number of profiles in the system.
  171. static U32 smProfileCount;
  172. /// Keep a list of all the profiles.
  173. GFXTextureProfile *mNext;
  174. static GFXTextureProfile *smHead;
  175. };
  176. #define GFX_DeclareTextureProfile(name) extern GFXTextureProfile name
  177. #define GFX_ImplementTextureProfile(name, type, flags, compression) GFXTextureProfile name(#name, type, flags, compression)
  178. // Default Texture profiles
  179. // Texture we can render to.
  180. GFX_DeclareTextureProfile(GFXRenderTargetProfile);
  181. GFX_DeclareTextureProfile(GFXRenderTargetSRGBProfile);
  182. // Standard static diffuse textures
  183. GFX_DeclareTextureProfile(GFXStaticTextureProfile);
  184. GFX_DeclareTextureProfile(GFXStaticTextureSRGBProfile);
  185. // Standard static diffuse textures that are persistent in memory
  186. GFX_DeclareTextureProfile(GFXTexturePersistentProfile);
  187. GFX_DeclareTextureProfile(GFXTexturePersistentSRGBProfile);
  188. // Texture that resides in system memory - used to copy data to
  189. GFX_DeclareTextureProfile(GFXSystemMemTextureProfile);
  190. // normal map profiles
  191. GFX_DeclareTextureProfile(GFXNormalMapProfile);
  192. GFX_DeclareTextureProfile(GFXNormalMapBC3Profile);
  193. GFX_DeclareTextureProfile(GFXNormalMapBC5Profile);
  194. // Depth buffer texture
  195. GFX_DeclareTextureProfile(GFXZTargetProfile);
  196. // Dynamic Texure
  197. GFX_DeclareTextureProfile(GFXDynamicTextureProfile);
  198. GFX_DeclareTextureProfile(GFXDynamicTextureSRGBProfile);
  199. #endif