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.

450 lines
16 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef ISTUDIORENDER_H
  8. #define ISTUDIORENDER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/interface.h"
  13. #include "mathlib/vector.h"
  14. #include "mathlib/vector4d.h"
  15. #include "tier1/utlbuffer.h"
  16. #include "tier1/utlvector.h"
  17. #include "materialsystem/imaterial.h"
  18. #include "materialsystem/imaterialsystem.h"
  19. #include "appframework/iappsystem.h"
  20. #include "datacache/imdlcache.h"
  21. #include "tier0/fasttimer.h"
  22. #include "studio.h"
  23. //-----------------------------------------------------------------------------
  24. // forward declarations
  25. //-----------------------------------------------------------------------------
  26. struct studiohdr_t;
  27. struct studiomeshdata_t;
  28. class Vector;
  29. struct LightDesc_t;
  30. class IMaterial;
  31. struct studiohwdata_t;
  32. struct Ray_t;
  33. class Vector4D;
  34. class IMaterialSystem;
  35. struct matrix3x4_t;
  36. class IMesh;
  37. struct vertexFileHeader_t;
  38. struct FlashlightState_t;
  39. class VMatrix;
  40. namespace OptimizedModel { struct FileHeader_t; }
  41. class IPooledVBAllocator;
  42. struct MeshInstanceData_t;
  43. struct ShaderStencilState_t;
  44. // undone: what's the standard for function type naming?
  45. typedef void (*StudioRender_Printf_t)( PRINTF_FORMAT_STRING const char *fmt, ... );
  46. struct StudioRenderConfig_t
  47. {
  48. float fEyeShiftX; // eye X position
  49. float fEyeShiftY; // eye Y position
  50. float fEyeShiftZ; // eye Z position
  51. float fEyeSize; // adjustment to iris textures
  52. float fEyeGlintPixelWidthLODThreshold;
  53. int maxDecalsPerModel;
  54. int drawEntities;
  55. int skin;
  56. int fullbright;
  57. bool bEyeMove : 1; // look around
  58. bool bSoftwareSkin : 1;
  59. bool bNoHardware : 1;
  60. bool bNoSoftware : 1;
  61. bool bTeeth : 1;
  62. bool bEyes : 1;
  63. bool bFlex : 1;
  64. bool bWireframe : 1;
  65. bool bDrawNormals : 1;
  66. bool bDrawTangentFrame : 1;
  67. bool bDrawZBufferedWireframe : 1;
  68. bool bSoftwareLighting : 1;
  69. bool bShowEnvCubemapOnly : 1;
  70. bool bWireframeDecals : 1;
  71. // Reserved for future use
  72. int m_nReserved[4];
  73. };
  74. //-----------------------------------------------------------------------------
  75. // Studio render interface
  76. //-----------------------------------------------------------------------------
  77. DECLARE_POINTER_HANDLE( StudioDecalHandle_t );
  78. #define STUDIORENDER_DECAL_INVALID ( (StudioDecalHandle_t)0 )
  79. enum
  80. {
  81. ADDDECAL_TO_ALL_LODS = -1
  82. };
  83. //-----------------------------------------------------------------------------
  84. // DrawModel flags
  85. //-----------------------------------------------------------------------------
  86. enum
  87. {
  88. STUDIORENDER_DRAW_ENTIRE_MODEL = 0,
  89. STUDIORENDER_DRAW_OPAQUE_ONLY = 0x01,
  90. STUDIORENDER_DRAW_TRANSLUCENT_ONLY = 0x02,
  91. STUDIORENDER_DRAW_GROUP_MASK = 0x03,
  92. STUDIORENDER_DRAW_NO_FLEXES = 0x04,
  93. STUDIORENDER_DRAW_STATIC_LIGHTING = 0x08,
  94. STUDIORENDER_DRAW_ACCURATETIME = 0x10, // Use accurate timing when drawing the model.
  95. STUDIORENDER_DRAW_NO_SHADOWS = 0x20,
  96. STUDIORENDER_DRAW_GET_PERF_STATS = 0x40,
  97. STUDIORENDER_DRAW_WIREFRAME = 0x80,
  98. STUDIORENDER_DRAW_ITEM_BLINK = 0x100,
  99. STUDIORENDER_SHADOWDEPTHTEXTURE = 0x200,
  100. STUDIORENDER_UNUSED = 0x400,
  101. STUDIORENDER_SKIP_DECALS = 0x800,
  102. STUDIORENDER_MODEL_IS_CACHEABLE = 0x1000,
  103. STUDIORENDER_SHADOWDEPTHTEXTURE_INCLUDE_TRANSLUCENT_MATERIALS = 0x2000,
  104. STUDIORENDER_NO_PRIMARY_DRAW = 0x4000,
  105. STUDIORENDER_SSAODEPTHTEXTURE = 0x8000,
  106. };
  107. //-----------------------------------------------------------------------------
  108. // Standard model vertex formats
  109. //-----------------------------------------------------------------------------
  110. // FIXME: remove these (materials/shaders should drive vertex format). Need to
  111. // list required forcedmaterialoverrides in models/bsps (rather than
  112. // all models supporting all possible overrides, as they do currently).
  113. #define VERTEX_TEXCOORD0_2D ( ( (uint64) 2 ) << ( TEX_COORD_SIZE_BIT + ( 3*0 ) ) )
  114. enum MaterialVertexFormat_t
  115. {
  116. MATERIAL_VERTEX_FORMAT_MODEL = (VertexFormat_t) VERTEX_POSITION | VERTEX_COLOR_STREAM_1 | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D | VERTEX_USERDATA_SIZE(4),
  117. };
  118. //-----------------------------------------------------------------------------
  119. // What kind of material override is it?
  120. //-----------------------------------------------------------------------------
  121. enum OverrideType_t
  122. {
  123. OVERRIDE_NORMAL = 0,
  124. OVERRIDE_BUILD_SHADOWS,
  125. OVERRIDE_DEPTH_WRITE,
  126. OVERRIDE_SELECTIVE,
  127. OVERRIDE_SSAO_DEPTH_WRITE,
  128. };
  129. //-----------------------------------------------------------------------------
  130. // DrawModel info
  131. //-----------------------------------------------------------------------------
  132. // Special flag for studio models that have a compiled in shadow lod version
  133. // It's negative 2 since positive numbers == use a regular slot and -1 means
  134. // have studiorender compute a value instead
  135. enum
  136. {
  137. USESHADOWLOD = -2,
  138. };
  139. // beyond this number of materials, you won't get info back from DrawModel
  140. #define MAX_DRAW_MODEL_INFO_MATERIALS 8
  141. struct DrawModelResults_t
  142. {
  143. int m_ActualTriCount;
  144. int m_TextureMemoryBytes;
  145. int m_NumHardwareBones;
  146. int m_NumBatches;
  147. int m_NumMaterials;
  148. int m_nLODUsed;
  149. int m_flLODMetric;
  150. CFastTimer m_RenderTime;
  151. CUtlVectorFixed<IMaterial *,MAX_DRAW_MODEL_INFO_MATERIALS> m_Materials;
  152. };
  153. struct ColorMeshInfo_t
  154. {
  155. // A given color mesh can own a unique Mesh, or it can use a shared Mesh
  156. // (in which case it uses a sub-range defined by m_nVertOffset and m_nNumVerts)
  157. IMesh * m_pMesh;
  158. IPooledVBAllocator * m_pPooledVBAllocator;
  159. int m_nVertOffsetInBytes;
  160. int m_nNumVerts;
  161. };
  162. struct DrawModelInfo_t
  163. {
  164. studiohdr_t *m_pStudioHdr;
  165. studiohwdata_t *m_pHardwareData;
  166. StudioDecalHandle_t m_Decals;
  167. int m_Skin;
  168. int m_Body;
  169. int m_HitboxSet;
  170. void *m_pClientEntity;
  171. int m_Lod;
  172. ColorMeshInfo_t *m_pColorMeshes;
  173. bool m_bStaticLighting;
  174. MaterialLightingState_t m_LightingState;
  175. IMPLEMENT_OPERATOR_EQUAL( DrawModelInfo_t );
  176. };
  177. enum
  178. {
  179. // This is because we store which flashlights are on which model
  180. // in a 32-bit field (see ModelArrayInstanceData_t::m_nFlashlightUsage)
  181. MAX_FLASHLIGHTS_PER_INSTANCE_DRAW_CALL = 32
  182. };
  183. struct FlashlightInstance_t
  184. {
  185. IMaterial *m_pDebugMaterial;
  186. FlashlightState_t m_FlashlightState;
  187. VMatrix m_WorldToTexture;
  188. ITexture *m_pFlashlightDepthTexture;
  189. };
  190. struct StudioModelArrayInfo2_t
  191. {
  192. int m_nFlashlightCount;
  193. FlashlightInstance_t *m_pFlashlights; // NOTE: Can have at most MAX_FLASHLIGHTS_PER_INSTANCE_DRAW_CALL of these
  194. };
  195. struct StudioModelArrayInfo_t : public StudioModelArrayInfo2_t
  196. {
  197. studiohdr_t *m_pStudioHdr;
  198. studiohwdata_t *m_pHardwareData;
  199. };
  200. struct StudioArrayData_t
  201. {
  202. studiohdr_t *m_pStudioHdr;
  203. studiohwdata_t *m_pHardwareData;
  204. void *m_pInstanceData; // See StudioShadowArrayInstanceData_t or StudioArrayInstanceData_t
  205. int m_nCount;
  206. };
  207. struct StudioShadowArrayInstanceData_t
  208. {
  209. int m_nLOD;
  210. int m_nBody;
  211. int m_nSkin;
  212. matrix3x4a_t *m_pPoseToWorld;
  213. float *m_pFlexWeights;
  214. float *m_pDelayedFlexWeights;
  215. };
  216. struct StudioArrayInstanceData_t : public StudioShadowArrayInstanceData_t
  217. {
  218. MaterialLightingState_t *m_pLightingState;
  219. MaterialLightingState_t *m_pDecalLightingState;
  220. ITexture *m_pEnvCubemapTexture;
  221. StudioDecalHandle_t m_Decals;
  222. uint32 m_nFlashlightUsage; // Mask indicating which flashlights to use.
  223. ShaderStencilState_t *m_pStencilState;
  224. ColorMeshInfo_t *m_pColorMeshInfo;
  225. bool m_bColorMeshHasIndirectLightingOnly;
  226. Vector4D m_DiffuseModulation;
  227. };
  228. struct GetTriangles_Vertex_t
  229. {
  230. Vector m_Position;
  231. Vector m_Normal;
  232. Vector4D m_TangentS;
  233. Vector2D m_TexCoord;
  234. Vector4D m_BoneWeight;
  235. int m_BoneIndex[4];
  236. int m_NumBones;
  237. IMPLEMENT_OPERATOR_EQUAL( GetTriangles_Vertex_t );
  238. };
  239. struct GetTriangles_MaterialBatch_t
  240. {
  241. IMaterial *m_pMaterial;
  242. CUtlVector<GetTriangles_Vertex_t> m_Verts;
  243. CUtlVector<int> m_TriListIndices;
  244. };
  245. struct GetTriangles_Output_t
  246. {
  247. CUtlVector<GetTriangles_MaterialBatch_t> m_MaterialBatches;
  248. matrix3x4_t m_PoseToWorld[MAXSTUDIOBONES];
  249. DISALLOW_OPERATOR_EQUAL( GetTriangles_Output_t );
  250. };
  251. //-----------------------------------------------------------------------------
  252. // Cache Callback Function
  253. // implementation can either statically persist data (tools) or lru cache (engine) it.
  254. // caller returns base pointer to resident data.
  255. // code expectes data to be dynamic and invokes cache callback prior to iterative access.
  256. // virtualModel is member passed in via studiohdr_t and passed back for model identification.
  257. //-----------------------------------------------------------------------------
  258. #define STUDIO_DATA_CACHE_INTERFACE_VERSION "VStudioDataCache005"
  259. abstract_class IStudioDataCache : public IAppSystem
  260. {
  261. public:
  262. virtual bool VerifyHeaders( studiohdr_t *pStudioHdr ) = 0;
  263. virtual vertexFileHeader_t *CacheVertexData( studiohdr_t *pStudioHdr ) = 0;
  264. };
  265. //-----------------------------------------------------------------------------
  266. // Studio render interface
  267. //-----------------------------------------------------------------------------
  268. abstract_class IStudioRender : public IAppSystem
  269. {
  270. public:
  271. virtual void BeginFrame( void ) = 0;
  272. virtual void EndFrame( void ) = 0;
  273. // Used for the mat_stub console command.
  274. virtual void Mat_Stub( IMaterialSystem *pMatSys ) = 0;
  275. // Updates the rendering configuration
  276. virtual void UpdateConfig( const StudioRenderConfig_t& config ) = 0;
  277. virtual void GetCurrentConfig( StudioRenderConfig_t& config ) = 0;
  278. // Load, unload model data
  279. virtual bool LoadModel( studiohdr_t *pStudioHdr, void *pVtxData, studiohwdata_t *pHardwareData ) = 0;
  280. virtual void UnloadModel( studiohwdata_t *pHardwareData ) = 0;
  281. // Refresh the studiohdr since it was lost...
  282. virtual void RefreshStudioHdr( studiohdr_t* pStudioHdr, studiohwdata_t* pHardwareData ) = 0;
  283. // This is needed to do eyeglint and calculate the correct texcoords for the eyes.
  284. virtual void SetEyeViewTarget( const studiohdr_t *pStudioHdr, int nBodyIndex, const Vector& worldPosition ) = 0;
  285. // Methods related to lighting state
  286. // NOTE: SetAmbientLightColors assumes that the arraysize is the same as
  287. // returned from GetNumAmbientLightSamples
  288. virtual int GetNumAmbientLightSamples() = 0;
  289. virtual const Vector *GetAmbientLightDirections() = 0;
  290. virtual void SetAmbientLightColors( const Vector4D *pAmbientOnlyColors ) = 0;
  291. virtual void SetAmbientLightColors( const Vector *pAmbientOnlyColors ) = 0;
  292. virtual void SetLocalLights( int numLights, const LightDesc_t *pLights ) = 0;
  293. // Sets information about the camera location + orientation
  294. virtual void SetViewState( const Vector& viewOrigin, const Vector& viewRight,
  295. const Vector& viewUp, const Vector& viewPlaneNormal ) = 0;
  296. // LOD stuff
  297. virtual int GetNumLODs( const studiohwdata_t &hardwareData ) const = 0;
  298. virtual float GetLODSwitchValue( const studiohwdata_t &hardwareData, int lod ) const = 0;
  299. virtual void SetLODSwitchValue( studiohwdata_t &hardwareData, int lod, float switchValue ) = 0;
  300. // Sets the color/alpha modulation
  301. virtual void SetColorModulation( float const* pColor ) = 0;
  302. virtual void SetAlphaModulation( float flAlpha ) = 0;
  303. // Draws the model
  304. virtual void DrawModel( DrawModelResults_t *pResults, const DrawModelInfo_t& info,
  305. matrix3x4_t *pBoneToWorld, float *pFlexWeights, float *pFlexDelayedWeights, const Vector &modelOrigin, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  306. // Methods related to static prop rendering
  307. virtual void DrawModelStaticProp( const DrawModelInfo_t& drawInfo, const matrix3x4_t &modelToWorld, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  308. virtual void DrawStaticPropDecals( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld ) = 0;
  309. virtual void DrawStaticPropShadows( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld, int flags ) = 0;
  310. // Causes a material to be used instead of the materials the model was compiled with
  311. virtual void ForcedMaterialOverride( IMaterial *newMaterial, OverrideType_t nOverrideType = OVERRIDE_NORMAL, int nMaterialIndex = -1 ) = 0;
  312. virtual bool IsForcedMaterialOverride() = 0;
  313. // Create, destroy list of decals for a particular model
  314. virtual StudioDecalHandle_t CreateDecalList( studiohwdata_t *pHardwareData ) = 0;
  315. virtual void DestroyDecalList( StudioDecalHandle_t handle ) = 0;
  316. // Add decals to a decal list by doing a planar projection along the ray
  317. // The BoneToWorld matrices must be set before this is called
  318. virtual void AddDecal( StudioDecalHandle_t handle, studiohdr_t *pStudioHdr, matrix3x4_t *pBoneToWorld,
  319. const Ray_t & ray, const Vector& decalUp, IMaterial* pDecalMaterial, float radius, int body, bool noPokethru = false, int maxLODToDecal = ADDDECAL_TO_ALL_LODS, void *pvProxyUserData = NULL, int nAdditionalDecalFlags = 0 ) = 0;
  320. // Compute the lighting at a point and normal
  321. virtual void ComputeLighting( const Vector* pAmbient, int lightCount,
  322. LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting ) = 0;
  323. // Compute the lighting at a point, constant directional component is passed
  324. // as flDirectionalAmount
  325. virtual void ComputeLightingConstDirectional( const Vector* pAmbient, int lightCount,
  326. LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting, float flDirectionalAmount ) = 0;
  327. // Shadow state (affects the models as they are rendered)
  328. virtual void AddShadow( IMaterial* pMaterial, void* pProxyData, FlashlightState_t *m_pFlashlightState = NULL, VMatrix *pWorldToTexture = NULL, ITexture *pFlashlightDepthTexture = NULL ) = 0;
  329. virtual void ClearAllShadows() = 0;
  330. // Gets the model LOD; pass in the screen size in pixels of a sphere
  331. // of radius 1 that has the same origin as the model to get the LOD out...
  332. virtual int ComputeModelLod( studiohwdata_t* pHardwareData, float unitSphereSize, float *pMetric = NULL ) = 0;
  333. // Return a number that is usable for budgets, etc.
  334. // Things that we care about:
  335. // 1) effective triangle count (factors in batch sizes, state changes, etc)
  336. // 2) texture memory usage
  337. // Get Triangles returns the LOD used
  338. virtual void GetPerfStats( DrawModelResults_t *pResults, const DrawModelInfo_t &info, CUtlBuffer *pSpewBuf = NULL ) const = 0;
  339. virtual void GetTriangles( const DrawModelInfo_t& info, matrix3x4_t *pBoneToWorld, GetTriangles_Output_t &out ) = 0;
  340. // Returns materials used by a particular model
  341. virtual int GetMaterialList( studiohdr_t *pStudioHdr, int count, IMaterial** ppMaterials ) = 0;
  342. virtual int GetMaterialListFromBodyAndSkin( MDLHandle_t studio, int nSkin, int nBody, int nCountOutputMaterials, IMaterial** ppOutputMaterials ) = 0;
  343. // no debug modes, just fastest drawing path
  344. virtual void DrawModelArrayStaticProp( const DrawModelInfo_t& drawInfo, int nInstanceCount, const MeshInstanceData_t *pInstanceData, ColorMeshInfo_t **pColorMeshes ) = 0;
  345. // draw an array of models with the same state
  346. virtual void DrawModelArray( const StudioModelArrayInfo_t &drawInfo, int nCount,
  347. StudioArrayInstanceData_t *pInstanceData, int nInstanceStride, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  348. // draw an array of models with the same state
  349. virtual void DrawModelShadowArray( int nCount, StudioArrayData_t *pShadowData,
  350. int nInstanceStride, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  351. // draw an array of models with the same state
  352. virtual void DrawModelArray( const StudioModelArrayInfo2_t &info, int nCount, StudioArrayData_t *pArrayData,
  353. int nInstanceStride, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  354. #ifndef _CERT
  355. struct FacesRenderedInfo_t
  356. {
  357. studiohdr_t *pStudioHdr;
  358. unsigned int nFaceCount;
  359. unsigned int nRenderCount;
  360. };
  361. typedef void ( * FaceInfoCallbackFunc_t )( int nTopN, FacesRenderedInfo_t *pRenderedFaceInfo, int nTotalFaces );
  362. virtual void GatherRenderedFaceInfo( FaceInfoCallbackFunc_t pFunc ) = 0;
  363. #endif // !_CERT
  364. };
  365. DECLARE_TIER3_INTERFACE( IStudioRender, g_pStudioRender );
  366. #endif // ISTUDIORENDER_H