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.

385 lines
14 KiB

  1. //========= Copyright 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 "studio.h"
  22. //-----------------------------------------------------------------------------
  23. // forward declarations
  24. //-----------------------------------------------------------------------------
  25. struct studiohdr_t;
  26. struct studiomeshdata_t;
  27. class Vector;
  28. struct LightDesc_t;
  29. class IMaterial;
  30. struct studiohwdata_t;
  31. struct Ray_t;
  32. class Vector4D;
  33. class IMaterialSystem;
  34. struct matrix3x4_t;
  35. class IMesh;
  36. struct vertexFileHeader_t;
  37. struct FlashlightState_t;
  38. class VMatrix;
  39. namespace OptimizedModel { struct FileHeader_t; }
  40. class IPooledVBAllocator;
  41. // undone: what's the standard for function type naming?
  42. typedef void (*StudioRender_Printf_t)( PRINTF_FORMAT_STRING const char *fmt, ... );
  43. struct StudioRenderConfig_t
  44. {
  45. float fEyeShiftX; // eye X position
  46. float fEyeShiftY; // eye Y position
  47. float fEyeShiftZ; // eye Z position
  48. float fEyeSize; // adjustment to iris textures
  49. float fEyeGlintPixelWidthLODThreshold;
  50. int maxDecalsPerModel;
  51. int drawEntities;
  52. int skin;
  53. int fullbright;
  54. bool bEyeMove : 1; // look around
  55. bool bSoftwareSkin : 1;
  56. bool bNoHardware : 1;
  57. bool bNoSoftware : 1;
  58. bool bTeeth : 1;
  59. bool bEyes : 1;
  60. bool bFlex : 1;
  61. bool bWireframe : 1;
  62. bool bDrawNormals : 1;
  63. bool bDrawTangentFrame : 1;
  64. bool bDrawZBufferedWireframe : 1;
  65. bool bSoftwareLighting : 1;
  66. bool bShowEnvCubemapOnly : 1;
  67. bool bWireframeDecals : 1;
  68. // Reserved for future use
  69. int m_nReserved[4];
  70. };
  71. //-----------------------------------------------------------------------------
  72. // Studio render interface
  73. //-----------------------------------------------------------------------------
  74. DECLARE_POINTER_HANDLE( StudioDecalHandle_t );
  75. #define STUDIORENDER_DECAL_INVALID ( (StudioDecalHandle_t)0 )
  76. enum
  77. {
  78. ADDDECAL_TO_ALL_LODS = -1
  79. };
  80. //-----------------------------------------------------------------------------
  81. // DrawModel flags
  82. //-----------------------------------------------------------------------------
  83. enum
  84. {
  85. STUDIORENDER_DRAW_ENTIRE_MODEL = 0,
  86. STUDIORENDER_DRAW_OPAQUE_ONLY = 0x01,
  87. STUDIORENDER_DRAW_TRANSLUCENT_ONLY = 0x02,
  88. STUDIORENDER_DRAW_GROUP_MASK = 0x03,
  89. STUDIORENDER_DRAW_NO_FLEXES = 0x04,
  90. STUDIORENDER_DRAW_STATIC_LIGHTING = 0x08,
  91. STUDIORENDER_DRAW_ACCURATETIME = 0x10, // Use accurate timing when drawing the model.
  92. STUDIORENDER_DRAW_NO_SHADOWS = 0x20,
  93. STUDIORENDER_DRAW_GET_PERF_STATS = 0x40,
  94. STUDIORENDER_DRAW_WIREFRAME = 0x80,
  95. STUDIORENDER_DRAW_ITEM_BLINK = 0x100,
  96. STUDIORENDER_SHADOWDEPTHTEXTURE = 0x200,
  97. STUDIORENDER_SSAODEPTHTEXTURE = 0x1000,
  98. STUDIORENDER_GENERATE_STATS = 0x8000,
  99. };
  100. //-----------------------------------------------------------------------------
  101. // Standard model vertex formats
  102. //-----------------------------------------------------------------------------
  103. // FIXME: remove these (materials/shaders should drive vertex format). Need to
  104. // list required forcedmaterialoverrides in models/bsps (rather than
  105. // all models supporting all possible overrides, as they do currently).
  106. #define VERTEX_TEXCOORD0_2D ( ( (uint64) 2 ) << ( TEX_COORD_SIZE_BIT + ( 3*0 ) ) )
  107. enum MaterialVertexFormat_t
  108. {
  109. MATERIAL_VERTEX_FORMAT_MODEL_SKINNED = (VertexFormat_t) VERTEX_POSITION | VERTEX_COLOR | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D | VERTEX_BONEWEIGHT(2) | VERTEX_BONE_INDEX | VERTEX_USERDATA_SIZE(4),
  110. MATERIAL_VERTEX_FORMAT_MODEL_SKINNED_DX7 = (VertexFormat_t) VERTEX_POSITION | VERTEX_COLOR | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D | VERTEX_BONEWEIGHT(2) | VERTEX_BONE_INDEX,
  111. MATERIAL_VERTEX_FORMAT_MODEL = (VertexFormat_t) VERTEX_POSITION | VERTEX_COLOR | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D | VERTEX_USERDATA_SIZE(4),
  112. MATERIAL_VERTEX_FORMAT_MODEL_DX7 = (VertexFormat_t) VERTEX_POSITION | VERTEX_COLOR | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D,
  113. MATERIAL_VERTEX_FORMAT_COLOR = (VertexFormat_t) VERTEX_SPECULAR
  114. };
  115. //-----------------------------------------------------------------------------
  116. // What kind of material override is it?
  117. //-----------------------------------------------------------------------------
  118. enum OverrideType_t
  119. {
  120. OVERRIDE_NORMAL = 0,
  121. OVERRIDE_BUILD_SHADOWS,
  122. OVERRIDE_DEPTH_WRITE,
  123. OVERRIDE_SSAO_DEPTH_WRITE,
  124. };
  125. //-----------------------------------------------------------------------------
  126. // DrawModel info
  127. //-----------------------------------------------------------------------------
  128. // Special flag for studio models that have a compiled in shadow lod version
  129. // It's negative 2 since positive numbers == use a regular slot and -1 means
  130. // have studiorender compute a value instead
  131. enum
  132. {
  133. USESHADOWLOD = -2,
  134. };
  135. // beyond this number of materials, you won't get info back from DrawModel
  136. #define MAX_DRAW_MODEL_INFO_MATERIALS 8
  137. struct DrawModelResults_t
  138. {
  139. int m_ActualTriCount;
  140. int m_TextureMemoryBytes;
  141. int m_NumHardwareBones;
  142. int m_NumBatches;
  143. int m_NumMaterials;
  144. int m_nLODUsed;
  145. int m_flLODMetric;
  146. CFastTimer m_RenderTime;
  147. CUtlVectorFixed<IMaterial *,MAX_DRAW_MODEL_INFO_MATERIALS> m_Materials;
  148. };
  149. struct ColorTexelsInfo_t
  150. {
  151. int m_nWidth;
  152. int m_nHeight;
  153. int m_nMipmapCount;
  154. ImageFormat m_ImageFormat;
  155. int m_nByteCount;
  156. byte* m_pTexelData;
  157. };
  158. struct ColorMeshInfo_t
  159. {
  160. // A given color mesh can own a unique Mesh, or it can use a shared Mesh
  161. // (in which case it uses a sub-range defined by m_nVertOffset and m_nNumVerts)
  162. IMesh * m_pMesh;
  163. IPooledVBAllocator * m_pPooledVBAllocator;
  164. int m_nVertOffsetInBytes;
  165. int m_nNumVerts;
  166. ITexture * m_pLightmap;
  167. ColorTexelsInfo_t * m_pLightmapData;
  168. };
  169. struct DrawModelInfo_t
  170. {
  171. studiohdr_t *m_pStudioHdr;
  172. studiohwdata_t *m_pHardwareData;
  173. StudioDecalHandle_t m_Decals;
  174. int m_Skin;
  175. int m_Body;
  176. int m_HitboxSet;
  177. void *m_pClientEntity;
  178. int m_Lod;
  179. ColorMeshInfo_t *m_pColorMeshes;
  180. bool m_bStaticLighting;
  181. Vector m_vecAmbientCube[6]; // ambient, and lights that aren't in locallight[]
  182. int m_nLocalLightCount;
  183. LightDesc_t m_LocalLightDescs[4];
  184. };
  185. struct GetTriangles_Vertex_t
  186. {
  187. Vector m_Position;
  188. Vector m_Normal;
  189. Vector4D m_TangentS;
  190. Vector2D m_TexCoord;
  191. Vector4D m_BoneWeight;
  192. int m_BoneIndex[4];
  193. int m_NumBones;
  194. };
  195. struct GetTriangles_MaterialBatch_t
  196. {
  197. IMaterial *m_pMaterial;
  198. CUtlVector<GetTriangles_Vertex_t> m_Verts;
  199. CUtlVector<int> m_TriListIndices;
  200. };
  201. struct GetTriangles_Output_t
  202. {
  203. CUtlVector<GetTriangles_MaterialBatch_t> m_MaterialBatches;
  204. matrix3x4_t m_PoseToWorld[MAXSTUDIOBONES];
  205. };
  206. struct model_array_instance_t
  207. {
  208. matrix3x4_t modelToWorld;
  209. // UNDONE: Per instance lighting values?
  210. };
  211. //-----------------------------------------------------------------------------
  212. // Cache Callback Function
  213. // implementation can either statically persist data (tools) or lru cache (engine) it.
  214. // caller returns base pointer to resident data.
  215. // code expectes data to be dynamic and invokes cache callback prior to iterative access.
  216. // virtualModel is member passed in via studiohdr_t and passed back for model identification.
  217. //-----------------------------------------------------------------------------
  218. #define STUDIO_DATA_CACHE_INTERFACE_VERSION "VStudioDataCache005"
  219. abstract_class IStudioDataCache : public IAppSystem
  220. {
  221. public:
  222. virtual bool VerifyHeaders( studiohdr_t *pStudioHdr ) = 0;
  223. virtual vertexFileHeader_t *CacheVertexData( studiohdr_t *pStudioHdr ) = 0;
  224. };
  225. //-----------------------------------------------------------------------------
  226. // Studio render interface
  227. //-----------------------------------------------------------------------------
  228. #define STUDIO_RENDER_INTERFACE_VERSION "VStudioRender025"
  229. abstract_class IStudioRender : public IAppSystem
  230. {
  231. public:
  232. virtual void BeginFrame( void ) = 0;
  233. virtual void EndFrame( void ) = 0;
  234. // Used for the mat_stub console command.
  235. virtual void Mat_Stub( IMaterialSystem *pMatSys ) = 0;
  236. // Updates the rendering configuration
  237. virtual void UpdateConfig( const StudioRenderConfig_t& config ) = 0;
  238. virtual void GetCurrentConfig( StudioRenderConfig_t& config ) = 0;
  239. // Load, unload model data
  240. virtual bool LoadModel( studiohdr_t *pStudioHdr, void *pVtxData, studiohwdata_t *pHardwareData ) = 0;
  241. virtual void UnloadModel( studiohwdata_t *pHardwareData ) = 0;
  242. // Refresh the studiohdr since it was lost...
  243. virtual void RefreshStudioHdr( studiohdr_t* pStudioHdr, studiohwdata_t* pHardwareData ) = 0;
  244. // This is needed to do eyeglint and calculate the correct texcoords for the eyes.
  245. virtual void SetEyeViewTarget( const studiohdr_t *pStudioHdr, int nBodyIndex, const Vector& worldPosition ) = 0;
  246. // Methods related to lighting state
  247. // NOTE: SetAmbientLightColors assumes that the arraysize is the same as
  248. // returned from GetNumAmbientLightSamples
  249. virtual int GetNumAmbientLightSamples() = 0;
  250. virtual const Vector *GetAmbientLightDirections() = 0;
  251. virtual void SetAmbientLightColors( const Vector4D *pAmbientOnlyColors ) = 0;
  252. virtual void SetAmbientLightColors( const Vector *pAmbientOnlyColors ) = 0;
  253. virtual void SetLocalLights( int numLights, const LightDesc_t *pLights ) = 0;
  254. // Sets information about the camera location + orientation
  255. virtual void SetViewState( const Vector& viewOrigin, const Vector& viewRight,
  256. const Vector& viewUp, const Vector& viewPlaneNormal ) = 0;
  257. // Allocates flex weights for use in rendering
  258. // NOTE: Pass in a non-null second parameter to lock delayed flex weights
  259. virtual void LockFlexWeights( int nWeightCount, float **ppFlexWeights, float **ppFlexDelayedWeights = NULL ) = 0;
  260. virtual void UnlockFlexWeights() = 0;
  261. // Used to allocate bone matrices to be used to pass into DrawModel
  262. virtual matrix3x4_t* LockBoneMatrices( int nBoneCount ) = 0;
  263. virtual void UnlockBoneMatrices() = 0;
  264. // LOD stuff
  265. virtual int GetNumLODs( const studiohwdata_t &hardwareData ) const = 0;
  266. virtual float GetLODSwitchValue( const studiohwdata_t &hardwareData, int lod ) const = 0;
  267. virtual void SetLODSwitchValue( studiohwdata_t &hardwareData, int lod, float switchValue ) = 0;
  268. // Sets the color/alpha modulation
  269. virtual void SetColorModulation( float const* pColor ) = 0;
  270. virtual void SetAlphaModulation( float flAlpha ) = 0;
  271. // Draws the model
  272. virtual void DrawModel( DrawModelResults_t *pResults, const DrawModelInfo_t& info,
  273. matrix3x4_t *pBoneToWorld, float *pFlexWeights, float *pFlexDelayedWeights, const Vector &modelOrigin, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  274. // Methods related to static prop rendering
  275. virtual void DrawModelStaticProp( const DrawModelInfo_t& drawInfo, const matrix3x4_t &modelToWorld, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  276. virtual void DrawStaticPropDecals( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld ) = 0;
  277. virtual void DrawStaticPropShadows( const DrawModelInfo_t &drawInfo, const matrix3x4_t &modelToWorld, int flags ) = 0;
  278. // Causes a material to be used instead of the materials the model was compiled with
  279. virtual void ForcedMaterialOverride( IMaterial *newMaterial, OverrideType_t nOverrideType = OVERRIDE_NORMAL ) = 0;
  280. // Create, destroy list of decals for a particular model
  281. virtual StudioDecalHandle_t CreateDecalList( studiohwdata_t *pHardwareData ) = 0;
  282. virtual void DestroyDecalList( StudioDecalHandle_t handle ) = 0;
  283. // Add decals to a decal list by doing a planar projection along the ray
  284. // The BoneToWorld matrices must be set before this is called
  285. virtual void AddDecal( StudioDecalHandle_t handle, studiohdr_t *pStudioHdr, matrix3x4_t *pBoneToWorld,
  286. const Ray_t & ray, const Vector& decalUp, IMaterial* pDecalMaterial, float radius, int body, bool noPokethru = false, int maxLODToDecal = ADDDECAL_TO_ALL_LODS ) = 0;
  287. // Compute the lighting at a point and normal
  288. virtual void ComputeLighting( const Vector* pAmbient, int lightCount,
  289. LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting ) = 0;
  290. // Compute the lighting at a point, constant directional component is passed
  291. // as flDirectionalAmount
  292. virtual void ComputeLightingConstDirectional( const Vector* pAmbient, int lightCount,
  293. LightDesc_t* pLights, const Vector& pt, const Vector& normal, Vector& lighting, float flDirectionalAmount ) = 0;
  294. // Shadow state (affects the models as they are rendered)
  295. virtual void AddShadow( IMaterial* pMaterial, void* pProxyData, FlashlightState_t *m_pFlashlightState = NULL, VMatrix *pWorldToTexture = NULL, ITexture *pFlashlightDepthTexture = NULL ) = 0;
  296. virtual void ClearAllShadows() = 0;
  297. // Gets the model LOD; pass in the screen size in pixels of a sphere
  298. // of radius 1 that has the same origin as the model to get the LOD out...
  299. virtual int ComputeModelLod( studiohwdata_t* pHardwareData, float unitSphereSize, float *pMetric = NULL ) = 0;
  300. // Return a number that is usable for budgets, etc.
  301. // Things that we care about:
  302. // 1) effective triangle count (factors in batch sizes, state changes, etc)
  303. // 2) texture memory usage
  304. // Get Triangles returns the LOD used
  305. virtual void GetPerfStats( DrawModelResults_t *pResults, const DrawModelInfo_t &info, CUtlBuffer *pSpewBuf = NULL ) const = 0;
  306. virtual void GetTriangles( const DrawModelInfo_t& info, matrix3x4_t *pBoneToWorld, GetTriangles_Output_t &out ) = 0;
  307. // Returns materials used by a particular model
  308. virtual int GetMaterialList( studiohdr_t *pStudioHdr, int count, IMaterial** ppMaterials ) = 0;
  309. virtual int GetMaterialListFromBodyAndSkin( MDLHandle_t studio, int nSkin, int nBody, int nCountOutputMaterials, IMaterial** ppOutputMaterials ) = 0;
  310. // draw an array of models with the same state
  311. virtual void DrawModelArray( const DrawModelInfo_t &drawInfo, int arrayCount, model_array_instance_t *pInstanceData, int instanceStride, int flags = STUDIORENDER_DRAW_ENTIRE_MODEL ) = 0;
  312. virtual void GetMaterialOverride( IMaterial** ppOutForcedMaterial, OverrideType_t* pOutOverrideType ) = 0;
  313. };
  314. extern IStudioRender *g_pStudioRender;
  315. #endif // ISTUDIORENDER_H