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.

247 lines
13 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef STUDIORENDERCONTEXT_H
  7. #define STUDIORENDERCONTEXT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "istudiorender.h"
  12. #include "tier3/tier3.h"
  13. #include "studio.h"
  14. #include "tier1/delegates.h"
  15. #include "tier1/memstack.h"
  16. #include "studiorender.h"
  17. //-----------------------------------------------------------------------------
  18. // Foward declarations
  19. //-----------------------------------------------------------------------------
  20. class IStudioDataCache;
  21. class CStudioRender;
  22. //-----------------------------------------------------------------------------
  23. // Global interfaces
  24. //-----------------------------------------------------------------------------
  25. extern IStudioDataCache *g_pStudioDataCache;
  26. extern CStudioRender *g_pStudioRenderImp;
  27. //-----------------------------------------------------------------------------
  28. // Internal config structure
  29. //-----------------------------------------------------------------------------
  30. struct StudioRenderConfigInternal_t : public StudioRenderConfig_t
  31. {
  32. bool m_bSupportsVertexAndPixelShaders : 1;
  33. bool m_bSupportsOverbright : 1;
  34. bool m_bEnableHWMorph : 1;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // All the data needed to render a studiomodel
  38. //-----------------------------------------------------------------------------
  39. struct FlexWeights_t
  40. {
  41. float *m_pFlexWeights;
  42. float *m_pFlexDelayedWeights;
  43. };
  44. struct StudioRenderContext_t
  45. {
  46. StudioRenderConfigInternal_t m_Config;
  47. Vector m_ViewTarget;
  48. Vector m_ViewOrigin;
  49. Vector m_ViewRight;
  50. Vector m_ViewUp;
  51. Vector m_ViewPlaneNormal;
  52. Vector4D m_LightBoxColors[6];
  53. LightDesc_t m_LocalLights[MAXLOCALLIGHTS];
  54. int m_NumLocalLights;
  55. float m_ColorMod[3];
  56. float m_AlphaMod;
  57. IMaterial* m_pForcedMaterial[MAX_MAT_OVERRIDES];
  58. OverrideType_t m_nForcedMaterialType;
  59. int m_nForcedMaterialIndex[MAX_MAT_OVERRIDES];
  60. int m_nForcedMaterialIndexCount;
  61. };
  62. //-----------------------------------------------------------------------------
  63. // Helper to queue up calls if necessary
  64. //-----------------------------------------------------------------------------
  65. #define QUEUE_STUDIORENDER_CALL( FuncName, ClassName, pObject, ... ) \
  66. CMatRenderContextPtr pRenderContext( g_pMaterialSystem ); \
  67. ICallQueue *pCallQueue = pRenderContext->GetCallQueue(); \
  68. if ( !pCallQueue || studio_queue_mode.GetInt() == 0 ) \
  69. { \
  70. pObject->FuncName( __VA_ARGS__ ); \
  71. } \
  72. else \
  73. { \
  74. pCallQueue->QueueCall( pObject, &ClassName::FuncName, ##__VA_ARGS__ ); \
  75. }
  76. #define QUEUE_STUDIORENDER_CALL_RC( FuncName, ClassName, pObject, pRenderContext, ... ) \
  77. ICallQueue *pCallQueue = pRenderContext->GetCallQueue(); \
  78. if ( !pCallQueue || studio_queue_mode.GetInt() == 0 ) \
  79. { \
  80. pObject->FuncName( __VA_ARGS__ ); \
  81. } \
  82. else \
  83. { \
  84. pCallQueue->QueueCall( pObject, &ClassName::FuncName, ##__VA_ARGS__ ); \
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Implementation of IStudioRender
  88. //-----------------------------------------------------------------------------
  89. class CStudioRenderContext : public CTier3AppSystem< IStudioRender >
  90. {
  91. typedef CTier3AppSystem< IStudioRender > BaseClass;
  92. // Methods of IAppSystem
  93. public:
  94. virtual bool Connect( CreateInterfaceFn factory );
  95. virtual void Disconnect();
  96. virtual void *QueryInterface( const char *pInterfaceName );
  97. virtual InitReturnVal_t Init();
  98. virtual void Shutdown();
  99. // Methods of IStudioRender
  100. public:
  101. virtual void BeginFrame( void );
  102. virtual void EndFrame( void );
  103. virtual void Mat_Stub( IMaterialSystem *pMatSys );
  104. virtual void UpdateConfig( const StudioRenderConfig_t& config );
  105. virtual void GetCurrentConfig( StudioRenderConfig_t& config );
  106. virtual bool LoadModel(studiohdr_t *pStudioHdr, void *pVtxData, studiohwdata_t *pHardwareData);
  107. virtual void UnloadModel( studiohwdata_t *pHardwareData );
  108. virtual void RefreshStudioHdr( studiohdr_t* pStudioHdr, studiohwdata_t* pHardwareData );
  109. virtual void SetEyeViewTarget( const studiohdr_t *pStudioHdr, int nBodyIndex, const Vector& worldPosition );
  110. virtual void SetAmbientLightColors( const Vector *pAmbientOnlyColors );
  111. virtual void SetAmbientLightColors( const Vector4D *pAmbientOnlyColors );
  112. virtual void SetLocalLights( int numLights, const LightDesc_t *pLights );
  113. virtual int GetNumAmbientLightSamples();
  114. virtual const Vector *GetAmbientLightDirections();
  115. virtual void SetViewState( const Vector& viewOrigin, const Vector& viewRight, const Vector& viewUp, const Vector& viewPlaneNormal );
  116. virtual int GetNumLODs( const studiohwdata_t &hardwareData ) const;
  117. virtual float GetLODSwitchValue( const studiohwdata_t &hardwareData, int lod ) const;
  118. virtual void SetLODSwitchValue( studiohwdata_t &hardwareData, int lod, float switchValue );
  119. virtual void SetColorModulation( const float* pColor );
  120. virtual void SetAlphaModulation( float alpha );
  121. virtual void DrawModel( DrawModelResults_t *pResults, const DrawModelInfo_t& info, matrix3x4_t *pCustomBoneToWorld, float *pFlexWeights, float *pFlexDelayedWeights, const Vector& origin, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL );
  122. virtual void DrawModelArray( const StudioModelArrayInfo_t &drawInfo, int arrayCount, StudioArrayInstanceData_t *pInstanceData, int instanceStride, int flags );
  123. virtual void DrawModelArray( const StudioModelArrayInfo2_t &drawInfo, int nArrayCount, StudioArrayData_t *pArrayData, int nInstanceStride, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL );
  124. virtual void DrawModelShadowArray( int nCount, StudioArrayData_t *pShadowData, int nInstanceStride, int flags );
  125. virtual void DrawModelStaticProp( const DrawModelInfo_t& info, const matrix3x4_t &modelToWorld, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL );
  126. virtual void DrawModelArrayStaticProp( const DrawModelInfo_t& info, int nInstanceCount, const MeshInstanceData_t *pInstanceData, ColorMeshInfo_t **pColorMeshes );
  127. virtual void DrawStaticPropDecals( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld );
  128. virtual void DrawStaticPropShadows( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld, int flags );
  129. virtual void ForcedMaterialOverride( IMaterial *newMaterial, OverrideType_t nOverrideType = OVERRIDE_NORMAL, int nMaterialIndex = -1 );
  130. virtual bool IsForcedMaterialOverride();
  131. DELEGATE_TO_OBJECT_1( StudioDecalHandle_t, CreateDecalList, studiohwdata_t *, g_pStudioRenderImp );
  132. virtual void DestroyDecalList( StudioDecalHandle_t handle );
  133. virtual void AddDecal( StudioDecalHandle_t handle, studiohdr_t *pStudioHdr, matrix3x4_t *pBoneToWorld, const Ray_t & ray, const Vector& decalUp, IMaterial* pDecalMaterial, float radius, int body, bool noPokethru, int maxLODToDecal = ADDDECAL_TO_ALL_LODS, void *pvProxyUserData = NULL, int nAdditionalDecalFlags = 0 );
  134. virtual void ComputeLighting( const Vector* pAmbient, int lightCount, LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting );
  135. virtual void ComputeLightingConstDirectional( const Vector* pAmbient, int lightCount, LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting, float flDirectionalAmount );
  136. virtual void AddShadow( IMaterial* pMaterial, void* pProxyData, FlashlightState_t *pFlashlightState, VMatrix *pWorldToTexture, ITexture *pFlashlightDepthTexture );
  137. virtual void ClearAllShadows();
  138. virtual int ComputeModelLod( studiohwdata_t* pHardwareData, float flUnitSphereSize, float *pMetric = NULL );
  139. virtual void GetPerfStats( DrawModelResults_t *pResults, const DrawModelInfo_t &info, CUtlBuffer *pSpewBuf = NULL ) const;
  140. virtual void GetTriangles( const DrawModelInfo_t& info, matrix3x4_t *pBoneToWorld, GetTriangles_Output_t &out );
  141. virtual int GetMaterialList( studiohdr_t *pStudioHdr, int count, IMaterial** ppMaterials );
  142. virtual int GetMaterialListFromBodyAndSkin( MDLHandle_t studio, int nSkin, int nBody, int nCountOutputMaterials, IMaterial** ppOutputMaterials );
  143. #ifndef _CERT
  144. // Gathers information about faces rendered this past frame and feeds them into the given callback function (presuambly to spew)
  145. // Callback may be invoked on mat queue thread!
  146. virtual void GatherRenderedFaceInfo( IStudioRender::FaceInfoCallbackFunc_t pFunc );
  147. #endif // _CERT
  148. // Other public methods
  149. public:
  150. CStudioRenderContext();
  151. virtual ~CStudioRenderContext();
  152. private:
  153. // Load, unload materials
  154. void LoadMaterials( studiohdr_t *phdr, OptimizedModel::FileHeader_t *, studioloddata_t &lodData, int lodID );
  155. // Determines material flags
  156. void ComputeMaterialFlags( studiohdr_t *phdr, IMaterial *pMaterial );
  157. // Creates, destroys static meshes
  158. void R_StudioCreateStaticMeshes( studiohdr_t *pStudioHdr, OptimizedModel::FileHeader_t* pVtxHdr,
  159. studiohwdata_t *pStudioHWData, int lodID, int *pColorMeshID );
  160. void R_StudioCreateSingleMesh( studiohdr_t *pStudioHdr, studioloddata_t *pStudioLodData,
  161. mstudiomesh_t* pMesh, OptimizedModel::MeshHeader_t* pVtxMesh, int numBones,
  162. studiomeshdata_t* pMeshData, int *pColorMeshID );
  163. void R_StudioDestroyStaticMeshes( int numStudioMeshes, studiomeshdata_t **ppStudioMeshes );
  164. // Determine if any strip groups shouldn't be morphed
  165. void DetermineHWMorphing( mstudiomodel_t *pModel, OptimizedModel::ModelLODHeader_t *pVtxLOD );
  166. // Count deltas affecting a particular stripgroup
  167. int CountDeltaFlexedStripGroups( mstudiomodel_t *pModel, OptimizedModel::ModelLODHeader_t *pVtxLOD );
  168. // Count vertices affected by deltas in a particular strip group
  169. int CountFlexedVertices( mstudiomesh_t* pMesh, OptimizedModel::StripGroupHeader_t* pStripGroup );
  170. // Builds morph data
  171. void R_StudioBuildMorph( studiohdr_t *pStudioHdr, studiomeshgroup_t* pMeshGroup, mstudiomesh_t* pMesh,
  172. OptimizedModel::StripGroupHeader_t *pStripGroup );
  173. // Builds the decal bone remap for a particular mesh
  174. void ComputeHWMorphDecalBoneRemap( studiohdr_t *pStudioHdr, OptimizedModel::FileHeader_t *pVtxHdr, studiohwdata_t *pStudioHWData, int nLOD );
  175. void BuildDecalBoneMap( studiohdr_t *pStudioHdr, int *pUsedBones, int *pBoneRemap, int *pMaxBoneCount, mstudiomesh_t* pMesh, OptimizedModel::StripGroupHeader_t* pStripGroup );
  176. // Helper methods used to construct static meshes
  177. int GetNumBoneWeights( const OptimizedModel::StripGroupHeader_t *pGroup );
  178. VertexFormat_t CalculateVertexFormat( const studiohdr_t *pStudioHdr, const studioloddata_t *pStudioLodData,
  179. const mstudiomesh_t* pMesh, OptimizedModel::StripGroupHeader_t *pGroup, bool bIsHwSkinned );
  180. VertexStreamSpec_t *CalculateStreamSpec( const studiohdr_t *pStudioHdr, const studioloddata_t *pStudioLodData,
  181. const mstudiomesh_t* pMesh, OptimizedModel::StripGroupHeader_t *pGroup, bool bIsHwSkinned, VertexFormat_t *pVertexFormat );
  182. bool MeshNeedsTangentSpace( studiohdr_t *pStudioHdr, studioloddata_t *pStudioLodData, mstudiomesh_t* pMesh );
  183. void R_StudioBuildMeshGroup( const char *pModelName, bool bNeedsTangentSpace, studioloddata_t *pStudioLodData,
  184. studiomeshgroup_t* pMeshGroup, OptimizedModel::StripGroupHeader_t *pStripGroup, mstudiomesh_t* pMesh,
  185. studiohdr_t *pStudioHdr, VertexFormat_t vertexFormat, VertexStreamSpec_t *pStreamSpec );
  186. void R_StudioBuildMeshStrips( studiomeshgroup_t* pMeshGroup,
  187. OptimizedModel::StripGroupHeader_t *pStripGroup );
  188. template <VertexCompressionType_t T> bool R_AddVertexToMesh( const char *pModelName, bool bNeedsTangentSpace, CMeshBuilder& meshBuilder,
  189. OptimizedModel::Vertex_t* pVertex, mstudiomesh_t* pMesh, const mstudio_meshvertexdata_t *vertData, bool hwSkin, bool bExtraUv );
  190. // This will generate random flex data that has a specified # of non-zero values
  191. void GenerateRandomFlexWeights( int nWeightCount, float* pWeights, float *pDelayedWeights );
  192. // Computes LOD
  193. int ComputeRenderLOD( IMatRenderContext *pRenderContext, const DrawModelInfo_t& info, const Vector &origin, float *pMetric );
  194. // This invokes proxies of all materials that are queued to be rendered
  195. void InvokeBindProxies( IMatRenderContext *pRenderContext, ICallQueue *pCallQueue, const DrawModelInfo_t &info );
  196. private:
  197. StudioRenderContext_t m_RC;
  198. // Used by the lighting computation methods,
  199. // this is only here to prevent constructors in lightpos_t from being repeatedly run
  200. lightpos_t m_pLightPos[MAXLIGHTCOMPUTE];
  201. };
  202. //-----------------------------------------------------------------------------
  203. // Inline methods
  204. //-----------------------------------------------------------------------------
  205. inline int CStudioRenderContext::ComputeModelLod( studiohwdata_t *pHardwareData, float flUnitSphereSize, float *pMetric )
  206. {
  207. return ComputeModelLODAndMetric( pHardwareData, flUnitSphereSize, pMetric );
  208. }
  209. #endif // STUDIORENDERCONTEXT_H