gfxTextureManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 _GFXTEXTUREMANAGER_H_
  23. #define _GFXTEXTUREMANAGER_H_
  24. #ifndef _GFXTEXTUREOBJECT_H_
  25. #include "gfx/gfxTextureObject.h"
  26. #endif
  27. #ifndef _GBITMAP_H_
  28. #include "gfx/bitmap/gBitmap.h"
  29. #endif
  30. #ifndef _DDSFILE_H_
  31. #include "gfx/bitmap/ddsFile.h"
  32. #endif
  33. #ifndef _RESOURCEMANAGER_H_
  34. #include "core/resourceManager.h"
  35. #endif
  36. #ifndef _TDICTIONARY_H_
  37. #include "core/util/tDictionary.h"
  38. #endif
  39. #ifndef _TSIGNAL_H_
  40. #include "core/util/tSignal.h"
  41. #endif
  42. namespace Torque
  43. {
  44. class Path;
  45. }
  46. class GFXCubemap;
  47. class GFXTextureManager
  48. {
  49. public:
  50. enum
  51. {
  52. AA_MATCH_BACKBUFFER = -1
  53. };
  54. GFXTextureManager();
  55. virtual ~GFXTextureManager();
  56. /// Set up some global script interface stuff.
  57. static void init();
  58. /// Update width and height based on available resources.
  59. ///
  60. /// We provide a simple interface for managing texture memory usage. Specifically,
  61. /// if the total video memory is below a certain threshold, we scale all texture
  62. /// resolutions down by a specific factor (you can specify different scale factors
  63. /// for different types of textures).
  64. ///
  65. /// @note The base GFXTextureManager class provides all the logic to do this scaling.
  66. /// Subclasses need only implement getTotalVideoMemory().
  67. ///
  68. /// @param type Type of the requested texture. This is used to determine scaling factors.
  69. /// @param width Requested width - is changed to the actual width that should be used.
  70. /// @param height Requested height - is changed to the actual height that should be used.
  71. /// @return True if the texture request should be granted, false otherwise.
  72. virtual bool validateTextureQuality(GFXTextureProfile *profile, U32 &width, U32 &height);
  73. ///
  74. static U32 getTextureDownscalePower( GFXTextureProfile *profile );
  75. virtual GFXTextureObject *createTexture( GBitmap *bmp,
  76. const String &resourceName,
  77. GFXTextureProfile *profile,
  78. bool deleteBmp);
  79. virtual GFXTextureObject *createTexture( DDSFile *dds,
  80. GFXTextureProfile *profile,
  81. bool deleteDDS);
  82. virtual GFXTextureObject *createTexture( const Torque::Path &path,
  83. GFXTextureProfile *profile );
  84. virtual GFXTextureObject *createTexture( U32 width,
  85. U32 height,
  86. void *pixels,
  87. GFXFormat format,
  88. GFXTextureProfile *profile);
  89. virtual GFXTextureObject *createTexture( U32 width,
  90. U32 height,
  91. U32 depth,
  92. void *pixels,
  93. GFXFormat format,
  94. GFXTextureProfile *profile );
  95. virtual GFXTextureObject *createTexture( U32 width,
  96. U32 height,
  97. GFXFormat format,
  98. GFXTextureProfile *profile,
  99. U32 numMipLevels,
  100. S32 antialiasLevel);
  101. void deleteTexture( GFXTextureObject *texture );
  102. void reloadTexture( GFXTextureObject *texture );
  103. /// Request that the texture be deleted which will
  104. /// either occur immediately or delayed if its cached.
  105. void requestDeleteTexture( GFXTextureObject *texture );
  106. /// @name Texture Necromancy
  107. ///
  108. /// Texture necromancy in three easy steps:
  109. /// - If you want to destroy the texture manager, call kill().
  110. /// - If you want to switch resolutions, or otherwise reset the device, call zombify().
  111. /// - When you want to bring the manager back from zombie state, call resurrect().
  112. /// @{
  113. ///
  114. void kill();
  115. void zombify();
  116. void resurrect();
  117. /// This releases any pooled textures which are
  118. /// currently unused freeing up video memory.
  119. void cleanupPool();
  120. ///
  121. void reloadTextures();
  122. /// This releases cached textures that have not
  123. /// been referenced for a period of time.
  124. void cleanupCache( U32 secondsToLive = 0 );
  125. /// Registers a callback for texture zombify and resurrect events.
  126. /// @see GFXTexCallbackCode
  127. /// @see removeEventDelegate
  128. template <class T,class U>
  129. static void addEventDelegate( T obj, U func );
  130. /// Unregisteres a texture event callback.
  131. /// @see addEventDelegate
  132. template <class T,class U>
  133. static void removeEventDelegate( T obj, U func ) { smEventSignal.remove( obj, func ); }
  134. /// @}
  135. /// Load a cubemap from a texture file.
  136. GFXCubemap* createCubemap( const Torque::Path &path );
  137. /// Used to remove a cubemap from the cache.
  138. void releaseCubemap( GFXCubemap *cubemap );
  139. protected:
  140. /// The amount of texture mipmaps to skip when loading a
  141. /// texture that allows downscaling.
  142. ///
  143. /// Exposed to script via $pref::Video::textureReductionLevel.
  144. ///
  145. /// @see GFXTextureProfile::PreserveSize
  146. ///
  147. static S32 smTextureReductionLevel;
  148. GFXTextureObject *mListHead;
  149. GFXTextureObject *mListTail;
  150. // We have a hash table for fast texture lookups
  151. GFXTextureObject **mHashTable;
  152. U32 mHashCount;
  153. GFXTextureObject *hashFind( const String &name );
  154. void hashInsert(GFXTextureObject *object);
  155. void hashRemove(GFXTextureObject *object);
  156. // The cache of loaded cubemap textures.
  157. typedef HashTable<String,GFXCubemap*> CubemapTable;
  158. CubemapTable mCubemapTable;
  159. /// The textures waiting to be deleted.
  160. Vector<GFXTextureObject*> mToDelete;
  161. enum TextureManagerState
  162. {
  163. Living,
  164. Zombie,
  165. Dead
  166. } mTextureManagerState;
  167. /// The texture pool collection type.
  168. typedef HashTable<GFXTextureProfile*,StrongRefPtr<GFXTextureObject> > TexturePoolMap;
  169. /// All the allocated texture pool textures.
  170. TexturePoolMap mTexturePool;
  171. //-----------------------------------------------------------------------
  172. // Protected methods
  173. //-----------------------------------------------------------------------
  174. /// Returns a free texture of the requested attributes from
  175. /// from the shared texture pool. It returns NULL if no match
  176. /// is found.
  177. GFXTextureObject* _findPooledTexure( U32 width,
  178. U32 height,
  179. GFXFormat format,
  180. GFXTextureProfile *profile,
  181. U32 numMipLevels,
  182. S32 antialiasLevel );
  183. GFXTextureObject *_createTexture( GBitmap *bmp,
  184. const String &resourceName,
  185. GFXTextureProfile *profile,
  186. bool deleteBmp,
  187. GFXTextureObject *inObj );
  188. GFXTextureObject *_createTexture( DDSFile *dds,
  189. GFXTextureProfile *profile,
  190. bool deleteDDS,
  191. GFXTextureObject *inObj );
  192. /// Frees the API handles to the texture, for D3D this is a release call
  193. ///
  194. /// @note freeTexture MUST NOT DELETE THE TEXTURE OBJECT
  195. virtual void freeTexture( GFXTextureObject *texture, bool zombify = false );
  196. virtual void refreshTexture( GFXTextureObject *texture );
  197. /// @group Internal Texture Manager Interface
  198. ///
  199. /// These pure virtual functions are overloaded by each API-specific
  200. /// subclass.
  201. ///
  202. /// The order of calls is:
  203. /// @code
  204. /// _createTexture()
  205. /// _loadTexture
  206. /// _refreshTexture()
  207. /// _refreshTexture()
  208. /// _refreshTexture()
  209. /// ...
  210. /// _freeTexture()
  211. /// @endcode
  212. ///
  213. /// @{
  214. /// Allocate a texture with the internal API.
  215. ///
  216. /// @param height Height of the texture.
  217. /// @param width Width of the texture.
  218. /// @param depth Depth of the texture. (Will normally be 1 unless
  219. /// we are doing a cubemap or volumetexture.)
  220. /// @param format Pixel format of the texture.
  221. /// @param profile Profile for the texture.
  222. /// @param numMipLevels If not-NULL, then use that many mips.
  223. /// If NULL create the full mip chain
  224. /// @param antialiasLevel, Use GFXTextureManager::AA_MATCH_BACKBUFFER to match the backbuffer settings (for render targets that want to share
  225. /// the backbuffer z buffer. 0 for no antialiasing, > 0 for levels that match the GFXVideoMode struct.
  226. virtual GFXTextureObject *_createTextureObject( U32 height,
  227. U32 width,
  228. U32 depth,
  229. GFXFormat format,
  230. GFXTextureProfile *profile,
  231. U32 numMipLevels,
  232. bool forceMips = false,
  233. S32 antialiasLevel = 0,
  234. GFXTextureObject *inTex = NULL ) = 0;
  235. /// Load a texture from a proper DDSFile instance.
  236. virtual bool _loadTexture(GFXTextureObject *texture, DDSFile *dds)=0;
  237. /// Load data into a texture from a GBitmap using the internal API.
  238. virtual bool _loadTexture(GFXTextureObject *texture, GBitmap *bmp)=0;
  239. /// Load data into a texture from a raw buffer using the internal API.
  240. ///
  241. /// Note that the size of the buffer is assumed from the parameters used
  242. /// for this GFXTextureObject's _createTexture call.
  243. virtual bool _loadTexture(GFXTextureObject *texture, void *raw)=0;
  244. /// Refresh a texture using the internal API.
  245. virtual bool _refreshTexture(GFXTextureObject *texture)=0;
  246. /// Free a texture (but do not delete the GFXTextureObject) using the internal
  247. /// API.
  248. ///
  249. /// This is only called during zombification for textures which need it, so you
  250. /// don't need to do any internal safety checks.
  251. virtual bool _freeTexture(GFXTextureObject *texture, bool zombify=false)=0;
  252. /// @}
  253. /// Store texture into the hash table cache and linked list.
  254. void _linkTexture( GFXTextureObject *obj );
  255. /// Validate the parameters for creating a texture.
  256. void _validateTexParams( const U32 width, const U32 height, const GFXTextureProfile *profile,
  257. U32 &inOutNumMips, GFXFormat &inOutFormat );
  258. // New texture manager methods for the cleanup work:
  259. GFXTextureObject *_lookupTexture( const char *filename, const GFXTextureProfile *profile );
  260. GFXTextureObject *_lookupTexture( const DDSFile *ddsFile, const GFXTextureProfile *profile );
  261. void _onFileChanged( const Torque::Path &path );
  262. /// The texture event signal type.
  263. typedef Signal<void(GFXTexCallbackCode code)> EventSignal;
  264. /// The texture event signal.
  265. static EventSignal smEventSignal;
  266. };
  267. template <class T,class U>
  268. inline void GFXTextureManager::addEventDelegate( T obj, U func )
  269. {
  270. EventSignal::DelegateSig d( obj, func );
  271. AssertFatal( !smEventSignal.contains( d ),
  272. "GFXTextureManager::addEventDelegate() - This is already registered!" );
  273. smEventSignal.notify( d );
  274. }
  275. inline void GFXTextureManager::reloadTexture( GFXTextureObject *texture )
  276. {
  277. refreshTexture( texture );
  278. }
  279. /// Returns the GFXTextureManager singleton. Should only be
  280. /// called after the GFX device has been initialized.
  281. #define TEXMGR GFXDevice::get()->getTextureManager()
  282. #endif // _GFXTEXTUREMANAGER_H_