Counter Strike : Global Offensive Source Code
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.

313 lines
9.1 KiB

  1. //========= Copyright c 1996-2011, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //====================================================================================//
  7. #ifndef __MDLCOMBINE_H
  8. #define __MDLCOMBINE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "utlbuffer.h"
  13. struct studiohdr2_t;
  14. struct studiodata_t;
  15. struct mstudiobone_t;
  16. struct mstudioanimdesc_t;
  17. struct mstudiomodel_t;
  18. class CModelCombine;
  19. class KeyValues;
  20. namespace OptimizedModel
  21. {
  22. struct StripGroupHeader_t;
  23. struct StripHeader_t;
  24. struct MeshHeader_t;
  25. struct ModelLODHeader_t;
  26. struct ModelHeader_t;
  27. struct BodyPartHeader_t;
  28. }
  29. //#define DEBUG_COMBINE 1
  30. #define COMBINER_MAX_STRINGS 2000
  31. #define COMBINER_MAX_BONES ( 53 * 2 )
  32. #define COMBINER_MAX_SUB_MODELS 20
  33. #define COMBINER_WORK_BUFFER_SIZE ( 2 * 1024 * 1024 )
  34. #define COMBINER_MAX_MATERIALS ( COMBINER_MAX_MATERIALS_PER_INPUT_MODEL * COMBINER_MAX_MODELS )
  35. #define COMBINER_MAX_BODYPARTS_PER_MODEL 5
  36. typedef struct SAtlasGroup
  37. {
  38. unsigned char *m_pCombinedTextures[ COMBINER_MAX_TEXTURES_PER_MATERIAL ];
  39. int m_nCombinedTextureSizes[ COMBINER_MAX_TEXTURES_PER_MATERIAL ];
  40. KeyValues *m_pCombinedMaterial;
  41. char m_szCombinedMaterialName[ MAX_PATH ];
  42. } TAtlasGroup;
  43. typedef struct SCombinedStudioData
  44. {
  45. studiodata_t *m_pPlaceholderStudioData;
  46. MDLHandle_t m_PlaceholderHandle;
  47. studiodata_t *m_pFinalStudioData;
  48. MDLHandle_t m_FinalHandle;
  49. void *m_pCombinedUserData;
  50. unsigned int m_nReferenceFlags;
  51. CombinedModelLoadedCallback m_CallbackFunc;
  52. CModelCombine *m_pCombineData;
  53. SCombinerModelInput_t m_ModelInputData[ COMBINER_MAX_MODELS ];
  54. int m_nNumModels;
  55. int m_nModelMaterialCounts[ COMBINER_MAX_MODELS ];
  56. int m_nModelMaterialIndices[ COMBINER_MAX_MODELS ][ COMBINER_MAX_MATERIALS ];
  57. int m_MeshToMaterialMap[ COMBINER_MAX_MODELS ][ COMBINER_MAX_BODYPARTS_PER_MODEL ][ COMBINER_MAX_MATERIALS_PER_INPUT_MODEL ];
  58. TAtlasGroup m_AtlasGroups[ COMBINER_MAX_ATLAS_GROUPS ];
  59. int m_nNumAtlasGroups;
  60. char m_szNonAtlasedMaterialPaths[ COMBINER_MAX_MODELS ][ MAX_PATH ];
  61. int m_nNumNonAtlasedMaterialPaths;
  62. KeyValues *m_pNonAtlasedMaterialKVs[ COMBINER_MAX_MODELS ];
  63. char m_szNonAtlasedMaterialBaseName[ COMBINER_MAX_MODELS ][ MAX_PATH ];
  64. int m_nNumNonAtlasedMaterialBaseNames;
  65. char m_szCombinedModelName[ MAX_PATH ];
  66. // returned results
  67. TCombinedResults m_Results;
  68. } TCombinedStudioData;
  69. enum
  70. {
  71. WRITE_AREA_MDL = 0,
  72. WRITE_AREA_VTX,
  73. WRITE_AREA_VVD,
  74. WRITE_AREA_VTF,
  75. MAX_WRITE_AREAS
  76. };
  77. class CCombinerMemoryWriter
  78. {
  79. public:
  80. CCombinerMemoryWriter( );
  81. ~CCombinerMemoryWriter( );
  82. void Init( );
  83. void InitWriteArea( int nArea, char *pPosition );
  84. void SetWriteArea( int nArea );
  85. char *AllocWrite( int nSize );
  86. char *WriteOffset( int &nOffsetIndex );
  87. char *WriteOffset( int &nOffsetIndex, void *pBasePtr );
  88. char *WriteOffset( short &nOffsetIndex, void *pBasePtr );
  89. char *WriteBuffer( const void *pData, int nSize );
  90. char *WriteBufferWithOffset( const void *pData, int nSize, int &nOffsetIndex );
  91. char *WriteString( const char *pszString ); // adds NULL terminator
  92. char *WriteText( const char *pszString ); // does not add NULL terminator
  93. void AlignWrite( int nAlignSize );
  94. char *GetSaveWritePos( int nArea ) { return m_pSaveWritePos[ nArea ]; }
  95. char *GetWritePos( ) { return m_pWritePos; }
  96. char *GetWriteArea( ) { return m_pWriteArea; }
  97. private:
  98. char *m_pWorkBuffer;
  99. char *m_pEndBuffer;
  100. char *m_pWriteArea;
  101. char *m_pSaveWriteArea[ MAX_WRITE_AREAS ];
  102. char *m_pWritePos;
  103. char *m_pSaveWritePos[ MAX_WRITE_AREAS ];
  104. int m_nWriteArea;
  105. #ifdef DEBUG_COMBINE
  106. char *m_pErrorPos;
  107. #endif
  108. };
  109. extern CCombinerMemoryWriter g_CombinerWriter;
  110. class CModelCombine
  111. {
  112. public:
  113. CModelCombine( );
  114. ~CModelCombine( );
  115. void Init( TCombinedStudioData *pCombinedStudioData );
  116. bool Resolve( );
  117. void *GetCombinedMDLPtr( ) { return ( void * )m_pCombinedStudioHdr; }
  118. int GetCombinedMDLSize( ) { return g_CombinerWriter.GetSaveWritePos( WRITE_AREA_MDL ) - ( char *)m_pCombinedStudioHdr; }
  119. bool GetCombinedMDLAvailability( ) { return GetCombinedMDLSize() != 0; }
  120. void *GetCombinedVTXPtr( ) { return ( void * )m_pCombinedHardwareHeader; }
  121. int GetCombinedVTXSize( ) { return g_CombinerWriter.GetSaveWritePos( WRITE_AREA_VTX ) - ( char *)m_pCombinedHardwareHeader; }
  122. bool GetCombinedVTXAvailability( ) { return GetCombinedVTXSize() != 0; }
  123. void *GetCombinedVVDPtr( ) { return ( void * )m_pCombinedVertex; }
  124. int GetCombinedVVDSize( ) { return g_CombinerWriter.GetSaveWritePos( WRITE_AREA_VVD ) - ( char *)m_pCombinedVertex; }
  125. bool GetCombinedVVDAvailability( ) { return GetCombinedVVDSize() != 0; }
  126. TCombinedResults *GetResults( ) { return &m_pCombinedStudioData->m_Results; }
  127. static int GetNextAssetID( ) { return ++m_nNextAssetID; }
  128. private:
  129. static int BoneNameCompare( const void *elem1, const void *elem2 );
  130. void BeginStringTable( );
  131. void AddToStringTable( void *base, int *ptr, const char *string );
  132. void WriteStringTable( );
  133. void VerifyField( int nField, const char *pszDescription );
  134. void VerifyField2( int nField, const char *pszDescription );
  135. void VerifyOffset( void *pPtr, const char *pszDescription, void *pWritePos = 0 );
  136. void DetermineMasterBoneList( );
  137. //
  138. void CombineMDL_PreintStrings( );
  139. void RemapBone( int nModel, int &nBone ) { nBone = m_nBoneRemap[ nModel ][ nBone ]; }
  140. void WriteBoneProc( int nSize, int nType1, int nType2 = -999 );
  141. void WriteBoneQuatInterp( );
  142. void WriteBoneTwist( );
  143. void WriteBoneConstraints( );
  144. void WriteBoneAttachments( );
  145. void WriteHitBoxes( );
  146. void WriteBoneTable( );
  147. void CombineMDL_Bones( );
  148. void WriteAnimation( mstudioanimdesc_t *pOrigAnim, void *pAnimData, int nFrameSize );
  149. void CombineMDL_Anims( );
  150. void CombineMDL_SequenceInfo( );
  151. void WriteModel( int nModel );
  152. void CombineMDL_Model( );
  153. void CombineMDL_Textures( );
  154. void CombineMDL_KeyValues( );
  155. void CombineMDL_BoneTransforms( );
  156. void CombineMDL_BoneFlexDrivers( );
  157. void CombineMDL_AssignMeshIDs( );
  158. void CombineMDL( bool bNoStringTable );
  159. #ifdef DEBUG_COMBINE
  160. void TestCombineMDL( );
  161. #endif // DEBUG_COMBINE
  162. void CalcVTXInfo();
  163. void WriteStrip( OptimizedModel::StripGroupHeader_t *pNewStripGroup, int nModel, OptimizedModel::StripGroupHeader_t *pOrigStripGroup, OptimizedModel::StripHeader_t *pOrigStrip );
  164. void MergeStripGroup( int nLOD, OptimizedModel::StripGroupHeader_t *pNewStripGroup, int nModel, OptimizedModel::StripGroupHeader_t *pOrigStripGroup );
  165. void WriteStripGroup( int nLOD, int nMaterialIndex, unsigned char nStripGroupFlags, OptimizedModel::MeshHeader_t *pNewMesh );
  166. void WriteMeshes( int nLOD, int nMaterialIndex, OptimizedModel::ModelLODHeader_t *pNewModelLOD, OptimizedModel::ModelLODHeader_t *pOrigModelLOD );
  167. void WriteModelLOD( int nLOD, OptimizedModel::ModelHeader_t *pNewModel, OptimizedModel::ModelHeader_t *pOrigModel );
  168. void WriteModel( int nModel, OptimizedModel::BodyPartHeader_t *pNewBodyPart, OptimizedModel::BodyPartHeader_t *pOrigBodyPart );
  169. void WriteBodyPart( int nBodyPart );
  170. void CombineVTX( );
  171. #ifdef DEBUG_COMBINE
  172. void TestCombineVTX( );
  173. #endif // DEBUG_COMBINE
  174. //
  175. void CombineVVD_OffsetVerts( );
  176. void CombineVVD( );
  177. //
  178. void CombineTextures( );
  179. int AddMaterialToTextureCombiner( int nTextureIndex, int nModel, int nModelMaterialIndex );
  180. void Test( );
  181. typedef struct SHardwareData
  182. {
  183. int m_nBodyParts;
  184. int m_nMaxBodyParts;
  185. int m_nModels;
  186. int m_nModelLODs;
  187. int m_nMeshes;
  188. int m_nStripGroups;
  189. int m_nStrips;
  190. int m_nVerts;
  191. int m_nIndices;
  192. int m_nBoneStateChanges;
  193. int m_nStringTable;
  194. int m_nTopology;
  195. int m_nMaterialReplacements;
  196. } THardwareData;
  197. TCombinedStudioData *m_pCombinedStudioData;
  198. CUtlBuffer *MDL_Data[ COMBINER_MAX_MODELS ];
  199. CUtlBuffer *VTX_Data[ COMBINER_MAX_MODELS ];
  200. CUtlBuffer *VVD_Data[ COMBINER_MAX_MODELS ];
  201. studiohdr_t *m_pStudioHdr[ COMBINER_MAX_MODELS ];
  202. studiohdr2_t *m_pStudioHdr2[ COMBINER_MAX_MODELS ];
  203. vertexFileHeader_t *m_pVertexFileHeader[ COMBINER_MAX_MODELS ];
  204. const mstudiobone_t *m_pMasterBoneList[ COMBINER_MAX_BONES ];
  205. int m_nBoneModelOwner[ COMBINER_MAX_BONES ];
  206. int m_nNumMasterBones;
  207. int m_nBoneRemap[ COMBINER_MAX_MODELS ][ COMBINER_MAX_BONES ];
  208. int m_nMasterToLocalBoneRemap[ COMBINER_MAX_MODELS ][ COMBINER_MAX_BONES ];
  209. mstudiomodel_t *m_pMasterModels[ COMBINER_MAX_MODELS ][ COMBINER_MAX_SUB_MODELS ];
  210. mstudiomodel_t *m_pMasterFlexModels[ COMBINER_MAX_SUB_MODELS ];
  211. int *m_nVertexRemap[ COMBINER_MAX_MODELS ];
  212. int m_nFlexModelSource;
  213. THardwareData m_MaxHardwareData;
  214. THardwareData m_CurrentHardwareData;
  215. THardwareData m_HardwareOffsets;
  216. studiohdr_t *m_pCombinedStudioHdr;
  217. studiohdr2_t *m_pCombinedStudioHdr2;
  218. OptimizedModel::FileHeader_t *m_pCombinedHardwareHeader;
  219. mstudiomodel_t *m_pCombinedModels;
  220. vertexFileHeader_t *m_pCombinedVertex;
  221. struct stringtable_t
  222. {
  223. byte *base;
  224. int *ptr;
  225. const char *string;
  226. int dupindex;
  227. byte *addr;
  228. };
  229. int numStrings;
  230. stringtable_t strings[ COMBINER_MAX_STRINGS ];
  231. static unsigned int m_nNextAssetID;
  232. };
  233. extern CModelCombine g_ModelCombiner;
  234. #endif // __MDLCOMBINE_H