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.

341 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. // This is what all shaders inherit from.
  7. //===========================================================================//
  8. #ifndef BASESHADER_H
  9. #define BASESHADER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "materialsystem/IShader.h"
  14. #include "materialsystem/imaterialvar.h"
  15. #include "materialsystem/ishaderapi.h"
  16. #include "materialsystem/imaterialsystemhardwareconfig.h"
  17. #include "shaderlib/BaseShader.h"
  18. #include "convar.h"
  19. //-----------------------------------------------------------------------------
  20. // Forward declarations
  21. //-----------------------------------------------------------------------------
  22. class IMaterialVar;
  23. //-----------------------------------------------------------------------------
  24. // Standard material vars
  25. //-----------------------------------------------------------------------------
  26. // Note: if you add to these, add to s_StandardParams in CBaseShader.cpp
  27. enum ShaderMaterialVars_t
  28. {
  29. FLAGS = 0,
  30. FLAGS_DEFINED, // mask indicating if the flag was specified
  31. FLAGS2,
  32. FLAGS_DEFINED2,
  33. COLOR,
  34. ALPHA,
  35. BASETEXTURE,
  36. FRAME,
  37. BASETEXTURETRANSFORM,
  38. FLASHLIGHTTEXTURE,
  39. FLASHLIGHTTEXTUREFRAME,
  40. COLOR2,
  41. SRGBTINT,
  42. NUM_SHADER_MATERIAL_VARS
  43. };
  44. // Alpha belnd mode enums. Moved from basevsshader
  45. enum BlendType_t
  46. {
  47. // no alpha blending
  48. BT_NONE = 0,
  49. // src * srcAlpha + dst * (1-srcAlpha)
  50. // two passes for HDR:
  51. // pass 1:
  52. // color: src * srcAlpha + dst * (1-srcAlpha)
  53. // alpha: srcAlpha * zero + dstAlpha * (1-srcAlpha)
  54. // pass 2:
  55. // color: none
  56. // alpha: srcAlpha * one + dstAlpha * one
  57. //
  58. BT_BLEND,
  59. // src * one + dst * one
  60. // one pass for HDR
  61. BT_ADD,
  62. // Why do we ever use this instead of using premultiplied alpha?
  63. // src * srcAlpha + dst * one
  64. // two passes for HDR
  65. // pass 1:
  66. // color: src * srcAlpha + dst * one
  67. // alpha: srcAlpha * one + dstAlpha * one
  68. // pass 2:
  69. // color: none
  70. // alpha: srcAlpha * one + dstAlpha * one
  71. BT_BLENDADD
  72. };
  73. //-----------------------------------------------------------------------------
  74. // Base class for shaders, contains helper methods.
  75. //-----------------------------------------------------------------------------
  76. class CBaseShader : public IShader
  77. {
  78. public:
  79. // constructor
  80. CBaseShader();
  81. // Methods inherited from IShader
  82. virtual char const* GetFallbackShader( IMaterialVar** params ) const { return 0; }
  83. virtual int GetNumParams( ) const;
  84. virtual char const* GetParamName( int paramIndex ) const;
  85. virtual char const* GetParamHelp( int paramIndex ) const;
  86. virtual ShaderParamType_t GetParamType( int paramIndex ) const;
  87. virtual char const* GetParamDefault( int paramIndex ) const;
  88. virtual int GetParamFlags( int nParamIndex ) const;
  89. virtual void InitShaderParams( IMaterialVar** ppParams, const char *pMaterialName );
  90. virtual void InitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName, const char *pTextureGroupName );
  91. virtual void DrawElements( IMaterialVar **params, int nModulationFlags, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI,
  92. VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContext );
  93. virtual const SoftwareVertexShader_t GetSoftwareVertexShader() const { return m_SoftwareVertexShader; }
  94. virtual int ComputeModulationFlags( IMaterialVar** params, IShaderDynamicAPI* pShaderAPI );
  95. virtual bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
  96. virtual bool NeedsFullFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
  97. virtual bool IsTranslucent( IMaterialVar **params ) const;
  98. public:
  99. // These functions must be implemented by the shader
  100. virtual void OnInitShaderParams( IMaterialVar** ppParams, const char *pMaterialName ) {}
  101. virtual void OnInitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName ) = 0;
  102. virtual void OnDrawElements( IMaterialVar **params, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr ) = 0;
  103. // Sets the default shadow state
  104. void SetInitialShadowState( );
  105. // Draws a snapshot
  106. void Draw( bool bMakeActualDrawCall = true );
  107. // Are we currently taking a snapshot?
  108. bool IsSnapshotting() const;
  109. // Gets at the current materialvar flags
  110. int CurrentMaterialVarFlags() const;
  111. // Finds a particular parameter (works because the lowest parameters match the shader)
  112. int FindParamIndex( const char *pName ) const;
  113. // Are we using graphics?
  114. bool IsUsingGraphics();
  115. // Are we using editor materials?
  116. bool CanUseEditorMaterials();
  117. // Gets the builder...
  118. CMeshBuilder* MeshBuilder();
  119. // Loads a texture
  120. void LoadTexture( int nTextureVar, int nAdditionalCreationFlags = 0 );
  121. // Loads a bumpmap
  122. void LoadBumpMap( int nTextureVar );
  123. // Loads a cubemap
  124. void LoadCubeMap( int nTextureVar, int nAdditionalCreationFlags = 0 );
  125. // get the shaderapi handle for a texture. BE CAREFUL WITH THIS.
  126. ShaderAPITextureHandle_t GetShaderAPITextureBindHandle( int nTextureVar, int nFrameVar, int nTextureChannel = 0 );
  127. // Binds a texture
  128. void BindTexture( Sampler_t sampler1, Sampler_t sampler2, int nTextureVar, int nFrameVar = -1 );
  129. void BindTexture( Sampler_t sampler1, int nTextureVar, int nFrameVar = -1 );
  130. void BindTexture( Sampler_t sampler1, ITexture *pTexture, int nFrame = 0 );
  131. void BindTexture( Sampler_t sampler1, Sampler_t sampler2, ITexture *pTexture, int nFrame = 0 );
  132. void GetTextureDimensions( float* pOutWidth, float* pOutHeight, int nTextureVar );
  133. // Is the texture translucent?
  134. bool TextureIsTranslucent( int textureVar, bool isBaseTexture );
  135. // Returns the translucency...
  136. float GetAlpha( IMaterialVar** params = NULL );
  137. // Is the color var white?
  138. bool IsWhite( int colorVar );
  139. // Helper methods for fog
  140. void FogToOOOverbright( void );
  141. void FogToWhite( void );
  142. void FogToBlack( void );
  143. void FogToGrey( void );
  144. void FogToFogColor( void );
  145. void DisableFog( void );
  146. void DefaultFog( void );
  147. // Helpers for alpha blending
  148. void EnableAlphaBlending( ShaderBlendFactor_t src, ShaderBlendFactor_t dst );
  149. void DisableAlphaBlending();
  150. void SetBlendingShadowState( BlendType_t nMode );
  151. void SetNormalBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  152. void SetAdditiveBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  153. void SetDefaultBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  154. void SingleTextureLightmapBlendMode( );
  155. // Helpers for color modulation
  156. void SetColorState( int colorVar, bool setAlpha = false );
  157. bool IsAlphaModulating();
  158. bool IsColorModulating();
  159. void ComputeModulationColor( float* color );
  160. void SetModulationShadowState( int tintVar = -1 );
  161. void SetModulationDynamicState( int tintVar = -1 );
  162. // Helpers for HDR
  163. bool IsHDREnabled( void );
  164. // Loads the identity matrix into the texture
  165. void LoadIdentity( MaterialMatrixMode_t matrixMode );
  166. // Loads the camera to world transform
  167. void LoadCameraToWorldTransform( MaterialMatrixMode_t matrixMode );
  168. void LoadCameraSpaceSphereMapTransform( MaterialMatrixMode_t matrixMode );
  169. // Sets a texture translation transform in fixed function
  170. void SetFixedFunctionTextureTranslation( MaterialMatrixMode_t mode, int translationVar );
  171. void SetFixedFunctionTextureScale( MaterialMatrixMode_t mode, int scaleVar );
  172. void SetFixedFunctionTextureScaledTransform( MaterialMatrixMode_t textureTransform, int transformVar, int scaleVar );
  173. void SetFixedFunctionTextureTransform( MaterialMatrixMode_t textureTransform, int transformVar );
  174. void CleanupDynamicStateFixedFunction( );
  175. // Fixed function Base * detail pass
  176. void FixedFunctionBaseTimesDetailPass( int baseTextureVar, int frameVar,
  177. int baseTextureTransformVar, int detailVar, int detailScaleVar );
  178. // Fixed function Self illumination pass
  179. void FixedFunctionSelfIlluminationPass( Sampler_t sampler,
  180. int baseTextureVar, int frameVar, int baseTextureTransformVar, int selfIllumTintVar );
  181. // Masked environment map
  182. void FixedFunctionMaskedEnvmapPass( int envMapVar, int envMapMaskVar,
  183. int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
  184. int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
  185. // Additive masked environment map
  186. void FixedFunctionAdditiveMaskedEnvmapPass( int envMapVar, int envMapMaskVar,
  187. int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
  188. int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
  189. // Modulate by detail texture pass
  190. void FixedFunctionMultiplyByDetailPass( int baseTextureVar, int frameVar,
  191. int textureOffsetVar, int detailVar, int detailScaleVar );
  192. // Multiply by lightmap pass
  193. void FixedFunctionMultiplyByLightmapPass( int baseTextureVar, int frameVar,
  194. int baseTextureTransformVar, float alphaOverride = -1 );
  195. // Helper methods for environment mapping
  196. int SetShadowEnvMappingState( int envMapMaskVar, int tintVar = -1 );
  197. void SetDynamicEnvMappingState( int envMapVar, int envMapMaskVar,
  198. int baseTextureVar, int envMapFrameVar, int envMapMaskFrameVar,
  199. int frameVar, int maskOffsetVar, int maskScaleVar, int tintVar = -1 );
  200. bool UsingFlashlight( IMaterialVar **params ) const;
  201. bool UsingEditor( IMaterialVar **params ) const;
  202. void DrawFlashlight_dx70( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  203. IShaderShadow* pShaderShadow,
  204. int flashlightTextureVar, int flashlightTextureFrameVar,
  205. bool suppress_lighting = false );
  206. void SetFlashlightFixedFunctionTextureTransform( MaterialMatrixMode_t matrix );
  207. void GetColorParameter( IMaterialVar** params, float *pColorOut ) const; // return tint color (color*color2)
  208. void ApplyColor2Factor( float *pColorOut ) const; // (*pColorOut) *= COLOR2
  209. static IMaterialVar **s_ppParams;
  210. protected:
  211. SoftwareVertexShader_t m_SoftwareVertexShader;
  212. static const char *s_pTextureGroupName; // Current material's texture group name.
  213. static IShaderShadow *s_pShaderShadow;
  214. static IShaderDynamicAPI *s_pShaderAPI;
  215. static IShaderInit *s_pShaderInit;
  216. private:
  217. static int s_nModulationFlags;
  218. static CMeshBuilder *s_pMeshBuilder;
  219. };
  220. //-----------------------------------------------------------------------------
  221. // Gets at the current materialvar flags
  222. //-----------------------------------------------------------------------------
  223. inline int CBaseShader::CurrentMaterialVarFlags() const
  224. {
  225. return s_ppParams[FLAGS]->GetIntValue();
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Are we currently taking a snapshot?
  229. //-----------------------------------------------------------------------------
  230. inline bool CBaseShader::IsSnapshotting() const
  231. {
  232. return (s_pShaderShadow != NULL);
  233. }
  234. //-----------------------------------------------------------------------------
  235. // Is the color var white?
  236. //-----------------------------------------------------------------------------
  237. inline bool CBaseShader::IsWhite( int colorVar )
  238. {
  239. if (colorVar < 0)
  240. return true;
  241. if (!s_ppParams[colorVar]->IsDefined())
  242. return true;
  243. float color[3];
  244. s_ppParams[colorVar]->GetVecValue( color, 3 );
  245. return (color[0] >= 1.0f) && (color[1] >= 1.0f) && (color[2] >= 1.0f);
  246. }
  247. class CBasePerMaterialContextData // shaders can keep per material data in classes descended from this
  248. {
  249. public:
  250. uint32 m_nVarChangeID;
  251. bool m_bMaterialVarsChanged; // set by mat system when material vars change. shader should rehtink and then clear the var
  252. FORCEINLINE CBasePerMaterialContextData( void )
  253. {
  254. m_bMaterialVarsChanged = true;
  255. m_nVarChangeID = 0xffffffff;
  256. }
  257. // virtual destructor so that derived classes can have their own data to be cleaned up on
  258. // delete of material
  259. virtual ~CBasePerMaterialContextData( void )
  260. {
  261. }
  262. };
  263. #endif // BASESHADER_H