gfxTextureManager.h 14 KB

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