gfxTextureProfile.h 8.6 KB

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