gfxTextureManager.h 13 KB

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