gfxTextureManager.h 14 KB

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