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.

235 lines
9.2 KiB

  1. //===== Copyright � 1996-2005, 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_declarations.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. class IMaterialVar;
  22. class CPerInstanceContextData;
  23. //-----------------------------------------------------------------------------
  24. // Base class for shaders, contains helper methods.
  25. //-----------------------------------------------------------------------------
  26. class CBaseShader : public IShader
  27. {
  28. public:
  29. // constructor
  30. CBaseShader();
  31. // Methods inherited from IShader
  32. virtual char const* GetFallbackShader( IMaterialVar** params ) const { return 0; }
  33. virtual int GetParamCount( ) const;
  34. virtual const ShaderParamInfo_t& GetParamInfo( int paramIndex ) const;
  35. virtual void InitShaderParams( IMaterialVar** ppParams, const char *pMaterialName );
  36. virtual void InitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName, const char *pTextureGroupName );
  37. virtual void DrawElements( IMaterialVar **params, int nModulationFlags, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI,
  38. VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContext, CBasePerInstanceContextData** pInstanceDataPtr );
  39. virtual int ComputeModulationFlags( IMaterialVar** params, IShaderDynamicAPI* pShaderAPI );
  40. virtual bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
  41. virtual bool NeedsFullFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame = true ) const;
  42. virtual bool IsTranslucent( IMaterialVar **params ) const;
  43. public:
  44. // These functions must be implemented by the shader
  45. virtual void OnInitShaderParams( IMaterialVar** ppParams, const char *pMaterialName ) {}
  46. virtual void OnInitShaderInstance( IMaterialVar** ppParams, IShaderInit *pShaderInit, const char *pMaterialName ) = 0;
  47. virtual void OnDrawElements( IMaterialVar **params, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr ) = 0;
  48. // Sets the default shadow state
  49. void SetInitialShadowState( );
  50. // Draws a snapshot
  51. void Draw( bool bMakeActualDrawCall = true );
  52. // Are we currently taking a snapshot?
  53. bool IsSnapshotting() const;
  54. // Methods related to building per-instance ("PI_") command buffers
  55. void PI_BeginCommandBuffer();
  56. void PI_EndCommandBuffer();
  57. void PI_SetPixelShaderAmbientLightCube( int nFirstRegister );
  58. void PI_SetPixelShaderLocalLighting( int nFirstRegister );
  59. void PI_SetPixelShaderAmbientLightCubeLuminance( int nFirstRegister );
  60. void PI_SetPixelShaderGlintDamping( int nFirstRegister );
  61. void PI_SetVertexShaderAmbientLightCube( /*int nFirstRegister*/ );
  62. void PI_SetModulationPixelShaderDynamicState( int nRegister );
  63. void PI_SetModulationPixelShaderDynamicState_LinearColorSpace_LinearScale( int nRegister, float scale );
  64. void PI_SetModulationPixelShaderDynamicState_LinearScale( int nRegister, float scale );
  65. void PI_SetModulationPixelShaderDynamicState_LinearScale_ScaleInW( int nRegister, float scale );
  66. void PI_SetModulationPixelShaderDynamicState_LinearColorSpace( int nRegister );
  67. void PI_SetModulationPixelShaderDynamicState_Identity( int nRegister );
  68. void PI_SetModulationVertexShaderDynamicState( void );
  69. void PI_SetModulationVertexShaderDynamicState_LinearScale( float flScale );
  70. // Gets at the current materialvar flags
  71. int CurrentMaterialVarFlags() const;
  72. // Gets at the current materialvar2 flags
  73. int CurrentMaterialVarFlags2() const;
  74. // Finds a particular parameter (works because the lowest parameters match the shader)
  75. int FindParamIndex( const char *pName ) const;
  76. // Are we using graphics?
  77. bool IsUsingGraphics();
  78. // Are we using editor materials?
  79. bool CanUseEditorMaterials() const;
  80. // Loads a texture
  81. void LoadTexture( int nTextureVar, int nAdditionalCreationFlags = 0 );
  82. // Loads a bumpmap
  83. void LoadBumpMap( int nTextureVar, int nAdditionalCreationFlags = 0 );
  84. // Loads a cubemap
  85. void LoadCubeMap( int nTextureVar, int nAdditionalCreationFlags = 0 );
  86. // get the shaderapi handle for a texture. BE CAREFUL WITH THIS.
  87. ShaderAPITextureHandle_t GetShaderAPITextureBindHandle( int nTextureVar, int nFrameVar, int nTextureChannel = 0 );
  88. ShaderAPITextureHandle_t GetShaderAPITextureBindHandle( ITexture *pTexture, int nFrame, int nTextureChannel = 0 );
  89. // Binds a texture
  90. void BindTexture( Sampler_t sampler1, Sampler_t sampler2, TextureBindFlags_t nBindFlags, int nTextureVar, int nFrameVar = -1 );
  91. void BindTexture( Sampler_t sampler1, TextureBindFlags_t nBindFlags, int nTextureVar, int nFrameVar = -1 );
  92. void BindTexture( Sampler_t sampler1, TextureBindFlags_t nBindFlags, ITexture *pTexture, int nFrame = 0 );
  93. void BindTexture( Sampler_t sampler1, Sampler_t sampler2, TextureBindFlags_t nBindFlags, ITexture *pTexture, int nFrame = 0 );
  94. // Bind vertex texture
  95. void BindVertexTexture( VertexTextureSampler_t vtSampler, int nTextureVar, int nFrame = 0 );
  96. // Is the texture translucent?
  97. bool TextureIsTranslucent( int textureVar, bool isBaseTexture );
  98. // Is the color var white?
  99. bool IsWhite( int colorVar );
  100. // Helper methods for fog
  101. void FogToOOOverbright( void );
  102. void FogToWhite( void );
  103. void FogToBlack( void );
  104. void FogToGrey( void );
  105. void FogToFogColor( void );
  106. void DisableFog( void );
  107. void DefaultFog( void );
  108. // Helpers for alpha blending
  109. void EnableAlphaBlending( ShaderBlendFactor_t src, ShaderBlendFactor_t dst );
  110. void DisableAlphaBlending();
  111. void SetBlendingShadowState( BlendType_t nMode );
  112. void SetNormalBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  113. void SetAdditiveBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  114. void SetDefaultBlendingShadowState( int textureVar = -1, bool isBaseTexture = true );
  115. void SingleTextureLightmapBlendMode( );
  116. // Helpers for color modulation
  117. bool IsAlphaModulating();
  118. // Helpers for HDR
  119. bool IsHDREnabled( void );
  120. bool UsingFlashlight( IMaterialVar **params ) const;
  121. bool UsingEditor( IMaterialVar **params ) const;
  122. bool IsRenderingPaint( IMaterialVar **params ) const;
  123. void ApplyColor2Factor( float *pColorOut, bool isLinearSpace = false ) const; // (*pColorOut) *= COLOR2
  124. static inline IMaterialVar **GetPPParams();
  125. void SetPPParams( IMaterialVar **params ) { s_ppParams = params; }
  126. void SetModulationFlags( int modulationFlags ) { s_nModulationFlags = modulationFlags; }
  127. private:
  128. // This is a per-instance state which is handled completely by the system
  129. void PI_SetSkinningMatrices();
  130. void PI_SetVertexShaderLocalLighting( );
  131. FORCEINLINE void SetFogMode( ShaderFogMode_t fogMode );
  132. protected:
  133. static IMaterialVar **s_ppParams;
  134. static const char *s_pTextureGroupName; // Current material's texture group name.
  135. static IShaderShadow *s_pShaderShadow;
  136. static IShaderDynamicAPI *s_pShaderAPI;
  137. static IShaderInit *s_pShaderInit;
  138. private:
  139. static int s_nPassCount;
  140. static int s_nModulationFlags;
  141. static CPerInstanceContextData** s_pInstanceDataPtr;
  142. template <class T> friend class CBaseCommandBufferBuilder;
  143. };
  144. //-----------------------------------------------------------------------------
  145. // Gets at the current materialvar flags
  146. //-----------------------------------------------------------------------------
  147. inline int CBaseShader::CurrentMaterialVarFlags() const
  148. {
  149. return s_ppParams[FLAGS]->GetIntValue();
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Gets at the current materialvar2 flags
  153. //-----------------------------------------------------------------------------
  154. inline int CBaseShader::CurrentMaterialVarFlags2() const
  155. {
  156. return s_ppParams[FLAGS2]->GetIntValue();
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Are we currently taking a snapshot?
  160. //-----------------------------------------------------------------------------
  161. inline bool CBaseShader::IsSnapshotting() const
  162. {
  163. return (s_pShaderShadow != NULL);
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Is the color var white?
  167. //-----------------------------------------------------------------------------
  168. inline bool CBaseShader::IsWhite( int colorVar )
  169. {
  170. if (colorVar < 0)
  171. return true;
  172. if (!s_ppParams[colorVar]->IsDefined())
  173. return true;
  174. float color[3];
  175. s_ppParams[colorVar]->GetVecValue( color, 3 );
  176. return (color[0] >= 1.0f) && (color[1] >= 1.0f) && (color[2] >= 1.0f);
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Returns the s_ppParams static member variable - for internal use only.
  180. //-----------------------------------------------------------------------------
  181. inline IMaterialVar **CBaseShader::GetPPParams()
  182. {
  183. return s_ppParams;
  184. }
  185. #endif // BASESHADER_H