Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // model loading and caching
  12. //
  13. //=============================================================================
  14. #ifndef IMDLCACHE_H
  15. #define IMDLCACHE_H
  16. #ifdef _WIN32
  17. #pragma once
  18. #endif
  19. #include "appframework/IAppSystem.h"
  20. //-----------------------------------------------------------------------------
  21. // Forward declarations
  22. //-----------------------------------------------------------------------------
  23. struct studiohdr_t;
  24. struct studiohwdata_t;
  25. struct vcollide_t;
  26. struct virtualmodel_t;
  27. struct vertexFileHeader_t;
  28. namespace OptimizedModel
  29. {
  30. struct FileHeader_t;
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Reference to a loaded studiomdl
  34. //-----------------------------------------------------------------------------
  35. typedef unsigned short MDLHandle_t;
  36. enum
  37. {
  38. MDLHANDLE_INVALID = (MDLHandle_t)~0
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Cache data types
  42. //-----------------------------------------------------------------------------
  43. enum MDLCacheDataType_t
  44. {
  45. // Callbacks to get called when data is loaded or unloaded for these:
  46. MDLCACHE_STUDIOHDR = 0,
  47. MDLCACHE_STUDIOHWDATA,
  48. MDLCACHE_VCOLLIDE,
  49. // Callbacks NOT called when data is loaded or unloaded for these:
  50. MDLCACHE_ANIMBLOCK,
  51. MDLCACHE_VIRTUALMODEL,
  52. MDLCACHE_VERTEXES,
  53. MDLCACHE_DECODEDANIMBLOCK,
  54. };
  55. abstract_class IMDLCacheNotify
  56. {
  57. public:
  58. // Called right after the data is loaded
  59. virtual void OnDataLoaded( MDLCacheDataType_t type, MDLHandle_t handle ) = 0;
  60. // Called right before the data is unloaded
  61. virtual void OnDataUnloaded( MDLCacheDataType_t type, MDLHandle_t handle ) = 0;
  62. };
  63. //-----------------------------------------------------------------------------
  64. // Flags for flushing
  65. //-----------------------------------------------------------------------------
  66. enum MDLCacheFlush_t
  67. {
  68. MDLCACHE_FLUSH_STUDIOHDR = 0x01,
  69. MDLCACHE_FLUSH_STUDIOHWDATA = 0x02,
  70. MDLCACHE_FLUSH_VCOLLIDE = 0x04,
  71. MDLCACHE_FLUSH_ANIMBLOCK = 0x08,
  72. MDLCACHE_FLUSH_VIRTUALMODEL = 0x10,
  73. MDLCACHE_FLUSH_AUTOPLAY = 0x20,
  74. MDLCACHE_FLUSH_VERTEXES = 0x40,
  75. MDLCACHE_FLUSH_IGNORELOCK = 0x80000000,
  76. MDLCACHE_FLUSH_ALL = 0xFFFFFFFF
  77. };
  78. /*
  79. #define MDLCACHE_INTERFACE_VERSION_4 "MDLCache004"
  80. namespace MDLCacheV4
  81. {
  82. abstract_class IMDLCache : public IAppSystem
  83. {
  84. public:
  85. // Used to install callbacks for when data is loaded + unloaded
  86. virtual void SetCacheNotify( IMDLCacheNotify *pNotify ) = 0;
  87. // NOTE: This assumes the "GAME" path if you don't use
  88. // the UNC method of specifying files. This will also increment
  89. // the reference count of the MDL
  90. virtual MDLHandle_t FindMDL( const char *pMDLRelativePath ) = 0;
  91. // Reference counting
  92. virtual int AddRef( MDLHandle_t handle ) = 0;
  93. virtual int Release( MDLHandle_t handle ) = 0;
  94. // Gets at the various data associated with a MDL
  95. virtual studiohdr_t *GetStudioHdr( MDLHandle_t handle ) = 0;
  96. virtual studiohwdata_t *GetHardwareData( MDLHandle_t handle ) = 0;
  97. virtual vcollide_t *GetVCollide( MDLHandle_t handle ) = 0;
  98. virtual unsigned char *GetAnimBlock( MDLHandle_t handle, int nBlock ) = 0;
  99. virtual virtualmodel_t *GetVirtualModel( MDLHandle_t handle ) = 0;
  100. virtual int GetAutoplayList( MDLHandle_t handle, unsigned short **pOut ) = 0;
  101. virtual vertexFileHeader_t *GetVertexData( MDLHandle_t handle ) = 0;
  102. // Brings all data associated with an MDL into memory
  103. virtual void TouchAllData( MDLHandle_t handle ) = 0;
  104. // Gets/sets user data associated with the MDL
  105. virtual void SetUserData( MDLHandle_t handle, void* pData ) = 0;
  106. virtual void *GetUserData( MDLHandle_t handle ) = 0;
  107. // Is this MDL using the error model?
  108. virtual bool IsErrorModel( MDLHandle_t handle ) = 0;
  109. // Flushes the cache, force a full discard
  110. virtual void Flush( int nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  111. // Flushes a particular model out of memory
  112. virtual void Flush( MDLHandle_t handle, int nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  113. // Returns the name of the model (its relative path)
  114. virtual const char *GetModelName( MDLHandle_t handle ) = 0;
  115. // faster access when you already have the studiohdr
  116. virtual virtualmodel_t *GetVirtualModelFast( const studiohdr_t *pStudioHdr, MDLHandle_t handle ) = 0;
  117. // all cache entries that subsequently allocated or successfully checked
  118. // are considered "locked" and will not be freed when additional memory is needed
  119. virtual void BeginLock() = 0;
  120. // reset all protected blocks to normal
  121. virtual void EndLock() = 0;
  122. // returns a pointer to a counter that is incremented every time the cache has been out of the locked state (EVIL)
  123. virtual int *GetFrameUnlockCounterPtr() = 0;
  124. // Finish all pending async operations
  125. virtual void FinishPendingLoads() = 0;
  126. };
  127. }
  128. */
  129. //-----------------------------------------------------------------------------
  130. // The main MDL cacher
  131. //-----------------------------------------------------------------------------
  132. #define MDLCACHE_INTERFACE_VERSION "MDLCache004"
  133. abstract_class IMDLCache : public IAppSystem
  134. {
  135. public:
  136. // Used to install callbacks for when data is loaded + unloaded
  137. // Returns the prior notify
  138. virtual void SetCacheNotify( IMDLCacheNotify *pNotify ) = 0;
  139. // NOTE: This assumes the "GAME" path if you don't use
  140. // the UNC method of specifying files. This will also increment
  141. // the reference count of the MDL
  142. virtual MDLHandle_t FindMDL( const char *pMDLRelativePath ) = 0;
  143. // Reference counting
  144. virtual int AddRef( MDLHandle_t handle ) = 0;
  145. virtual int Release( MDLHandle_t handle ) = 0;
  146. virtual int GetRef( MDLHandle_t handle ) = 0;
  147. // Gets at the various data associated with a MDL
  148. virtual studiohdr_t *GetStudioHdr( MDLHandle_t handle ) = 0;
  149. virtual studiohwdata_t *GetHardwareData( MDLHandle_t handle ) = 0;
  150. virtual vcollide_t *GetVCollide( MDLHandle_t handle ) = 0;
  151. virtual unsigned char *GetAnimBlock( MDLHandle_t handle, int nBlock ) = 0;
  152. virtual virtualmodel_t *GetVirtualModel( MDLHandle_t handle ) = 0;
  153. virtual int GetAutoplayList( MDLHandle_t handle, unsigned short **pOut ) = 0;
  154. virtual vertexFileHeader_t *GetVertexData( MDLHandle_t handle ) = 0;
  155. // Brings all data associated with an MDL into memory
  156. virtual void TouchAllData( MDLHandle_t handle ) = 0;
  157. // Gets/sets user data associated with the MDL
  158. virtual void SetUserData( MDLHandle_t handle, void* pData ) = 0;
  159. virtual void *GetUserData( MDLHandle_t handle ) = 0;
  160. // Is this MDL using the error model?
  161. virtual bool IsErrorModel( MDLHandle_t handle ) = 0;
  162. // Flushes the cache, force a full discard
  163. virtual void Flush( MDLCacheFlush_t nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  164. // Flushes a particular model out of memory
  165. virtual void Flush( MDLHandle_t handle, int nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  166. // Returns the name of the model (its relative path)
  167. virtual const char *GetModelName( MDLHandle_t handle ) = 0;
  168. // faster access when you already have the studiohdr
  169. virtual virtualmodel_t *GetVirtualModelFast( const studiohdr_t *pStudioHdr, MDLHandle_t handle ) = 0;
  170. // all cache entries that subsequently allocated or successfully checked
  171. // are considered "locked" and will not be freed when additional memory is needed
  172. virtual void BeginLock() = 0;
  173. // reset all protected blocks to normal
  174. virtual void EndLock() = 0;
  175. // returns a pointer to a counter that is incremented every time the cache has been out of the locked state (EVIL)
  176. virtual int *GetFrameUnlockCounterPtrOLD() = 0;
  177. // Finish all pending async operations
  178. virtual void FinishPendingLoads() = 0;
  179. virtual vcollide_t *GetVCollideEx( MDLHandle_t handle, bool synchronousLoad = true ) = 0;
  180. virtual bool GetVCollideSize( MDLHandle_t handle, int *pVCollideSize ) = 0;
  181. virtual bool GetAsyncLoad( MDLCacheDataType_t type ) = 0;
  182. virtual bool SetAsyncLoad( MDLCacheDataType_t type, bool bAsync ) = 0;
  183. virtual void BeginMapLoad() = 0;
  184. virtual void EndMapLoad() = 0;
  185. virtual void MarkAsLoaded( MDLHandle_t handle ) = 0;
  186. virtual void InitPreloadData( bool rebuild ) = 0;
  187. virtual void ShutdownPreloadData() = 0;
  188. virtual bool IsDataLoaded( MDLHandle_t handle, MDLCacheDataType_t type ) = 0;
  189. virtual int *GetFrameUnlockCounterPtr( MDLCacheDataType_t type ) = 0;
  190. virtual studiohdr_t *LockStudioHdr( MDLHandle_t handle ) = 0;
  191. virtual void UnlockStudioHdr( MDLHandle_t handle ) = 0;
  192. virtual bool PreloadModel( MDLHandle_t handle ) = 0;
  193. // Hammer uses this. If a model has an error loading in GetStudioHdr, then it is flagged
  194. // as an error model and any further attempts to load it will just get the error model.
  195. // That is, until you call this function. Then it will load the correct model.
  196. virtual void ResetErrorModelStatus( MDLHandle_t handle ) = 0;
  197. virtual void MarkFrame() = 0;
  198. };
  199. //-----------------------------------------------------------------------------
  200. // Critical section helper code
  201. //-----------------------------------------------------------------------------
  202. class CMDLCacheCriticalSection
  203. {
  204. public:
  205. CMDLCacheCriticalSection( IMDLCache *pCache ) : m_pCache( pCache )
  206. {
  207. m_pCache->BeginLock();
  208. }
  209. ~CMDLCacheCriticalSection()
  210. {
  211. m_pCache->EndLock();
  212. }
  213. private:
  214. IMDLCache *m_pCache;
  215. };
  216. #define MDCACHE_FINE_GRAINED 1
  217. #if defined(MDCACHE_FINE_GRAINED)
  218. #define MDLCACHE_CRITICAL_SECTION_( pCache ) CMDLCacheCriticalSection cacheCriticalSection(pCache)
  219. #define MDLCACHE_COARSE_LOCK_( pCache ) ((void)(0))
  220. #elif defined(MDLCACHE_LEVEL_LOCKED)
  221. #define MDLCACHE_CRITICAL_SECTION_( pCache ) ((void)(0))
  222. #define MDLCACHE_COARSE_LOCK_( pCache ) ((void)(0))
  223. #else
  224. #define MDLCACHE_CRITICAL_SECTION_( pCache ) ((void)(0))
  225. #define MDLCACHE_COARSE_LOCK_( pCache ) CMDLCacheCriticalSection cacheCriticalSection(pCache)
  226. #endif
  227. #define MDLCACHE_CRITICAL_SECTION() MDLCACHE_CRITICAL_SECTION_(mdlcache)
  228. #define MDLCACHE_COARSE_LOCK() MDLCACHE_COARSE_LOCK_(mdlcache)
  229. #endif // IMDLCACHE_H