gfxTextureManager.h 14 KB

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