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.

349 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef ISHADERDYNAMIC_H
  9. #define ISHADERDYNAMIC_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "shaderapi/shareddefs.h"
  14. #include "materialsystem/imaterial.h"
  15. #include "materialsystem/imaterialsystem.h"
  16. #include "tier0/basetypes.h"
  17. typedef int ShaderAPITextureHandle_t;
  18. //-----------------------------------------------------------------------------
  19. // forward declarations
  20. //-----------------------------------------------------------------------------
  21. class CMeshBuilder;
  22. class IMaterialVar;
  23. struct LightDesc_t;
  24. //-----------------------------------------------------------------------------
  25. // State from ShaderAPI used to select proper vertex and pixel shader combos
  26. //-----------------------------------------------------------------------------
  27. struct LightState_t
  28. {
  29. int m_nNumLights;
  30. bool m_bAmbientLight;
  31. bool m_bStaticLightVertex;
  32. bool m_bStaticLightTexel;
  33. inline int HasDynamicLight() { return (m_bAmbientLight || (m_nNumLights > 0)) ? 1 : 0; }
  34. };
  35. //-----------------------------------------------------------------------------
  36. // Color correction info
  37. //-----------------------------------------------------------------------------
  38. struct ShaderColorCorrectionInfo_t
  39. {
  40. bool m_bIsEnabled;
  41. int m_nLookupCount;
  42. float m_flDefaultWeight;
  43. float m_pLookupWeights[4];
  44. };
  45. //-----------------------------------------------------------------------------
  46. // the 3D shader API interface
  47. // This interface is all that shaders see.
  48. //-----------------------------------------------------------------------------
  49. enum StandardTextureId_t
  50. {
  51. // Lightmaps
  52. TEXTURE_LIGHTMAP = 0,
  53. TEXTURE_LIGHTMAP_FULLBRIGHT,
  54. TEXTURE_LIGHTMAP_BUMPED,
  55. TEXTURE_LIGHTMAP_BUMPED_FULLBRIGHT,
  56. // Flat colors
  57. TEXTURE_WHITE,
  58. TEXTURE_BLACK,
  59. TEXTURE_GREY,
  60. TEXTURE_GREY_ALPHA_ZERO,
  61. // Normalmaps
  62. TEXTURE_NORMALMAP_FLAT,
  63. // Normalization
  64. TEXTURE_NORMALIZATION_CUBEMAP,
  65. TEXTURE_NORMALIZATION_CUBEMAP_SIGNED,
  66. // Frame-buffer textures
  67. TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0,
  68. TEXTURE_FRAME_BUFFER_FULL_TEXTURE_1,
  69. // Color correction
  70. TEXTURE_COLOR_CORRECTION_VOLUME_0,
  71. TEXTURE_COLOR_CORRECTION_VOLUME_1,
  72. TEXTURE_COLOR_CORRECTION_VOLUME_2,
  73. TEXTURE_COLOR_CORRECTION_VOLUME_3,
  74. // An alias to the Back Frame Buffer
  75. TEXTURE_FRAME_BUFFER_ALIAS,
  76. // Noise for shadow mapping algorithm
  77. TEXTURE_SHADOW_NOISE_2D,
  78. // A texture in which morph data gets accumulated (vs30, fast vertex textures required)
  79. TEXTURE_MORPH_ACCUMULATOR,
  80. // A texture which contains morph weights
  81. TEXTURE_MORPH_WEIGHTS,
  82. // A snapshot of the frame buffer's depth. Currently only valid on the 360
  83. TEXTURE_FRAME_BUFFER_FULL_DEPTH,
  84. // A snapshot of the frame buffer's depth. Currently only valid on the 360
  85. TEXTURE_IDENTITY_LIGHTWARP,
  86. // Equivalent to the debug material for mat_luxels, in convenient texture form.
  87. TEXTURE_DEBUG_LUXELS,
  88. TEXTURE_MAX_STD_TEXTURES = 32
  89. };
  90. //-----------------------------------------------------------------------------
  91. // Viewport structure
  92. //-----------------------------------------------------------------------------
  93. #define SHADER_VIEWPORT_VERSION 1
  94. struct ShaderViewport_t
  95. {
  96. int m_nVersion;
  97. int m_nTopLeftX;
  98. int m_nTopLeftY;
  99. int m_nWidth;
  100. int m_nHeight;
  101. float m_flMinZ;
  102. float m_flMaxZ;
  103. ShaderViewport_t() : m_nVersion( SHADER_VIEWPORT_VERSION ) {}
  104. void Init()
  105. {
  106. memset( this, 0, sizeof(ShaderViewport_t) );
  107. m_nVersion = SHADER_VIEWPORT_VERSION;
  108. }
  109. void Init( int x, int y, int nWidth, int nHeight, float flMinZ = 0.0f, float flMaxZ = 1.0f )
  110. {
  111. m_nVersion = SHADER_VIEWPORT_VERSION;
  112. m_nTopLeftX = x; m_nTopLeftY = y; m_nWidth = nWidth; m_nHeight = nHeight;
  113. m_flMinZ = flMinZ;
  114. m_flMaxZ = flMaxZ;
  115. }
  116. };
  117. //-----------------------------------------------------------------------------
  118. // The Shader interface versions
  119. //-----------------------------------------------------------------------------
  120. #define SHADERDYNAMIC_INTERFACE_VERSION "ShaderDynamic001"
  121. abstract_class IShaderDynamicAPI
  122. {
  123. public:
  124. virtual void SetViewports( int nCount, const ShaderViewport_t* pViewports ) = 0;
  125. virtual int GetViewports( ShaderViewport_t* pViewports, int nMax ) const = 0;
  126. // returns the current time in seconds....
  127. virtual double CurrentTime() const = 0;
  128. // Gets the lightmap dimensions
  129. virtual void GetLightmapDimensions( int *w, int *h ) = 0;
  130. // Scene fog state.
  131. // This is used by the shaders for picking the proper vertex shader for fogging based on dynamic state.
  132. virtual MaterialFogMode_t GetSceneFogMode( ) = 0;
  133. virtual void GetSceneFogColor( unsigned char *rgb ) = 0;
  134. // stuff related to matrix stacks
  135. virtual void MatrixMode( MaterialMatrixMode_t matrixMode ) = 0;
  136. virtual void PushMatrix() = 0;
  137. virtual void PopMatrix() = 0;
  138. virtual void LoadMatrix( float *m ) = 0;
  139. virtual void MultMatrix( float *m ) = 0;
  140. virtual void MultMatrixLocal( float *m ) = 0;
  141. virtual void GetMatrix( MaterialMatrixMode_t matrixMode, float *dst ) = 0;
  142. virtual void LoadIdentity( void ) = 0;
  143. virtual void LoadCameraToWorld( void ) = 0;
  144. virtual void Ortho( double left, double right, double bottom, double top, double zNear, double zFar ) = 0;
  145. virtual void PerspectiveX( double fovx, double aspect, double zNear, double zFar ) = 0;
  146. virtual void PickMatrix( int x, int y, int width, int height ) = 0;
  147. virtual void Rotate( float angle, float x, float y, float z ) = 0;
  148. virtual void Translate( float x, float y, float z ) = 0;
  149. virtual void Scale( float x, float y, float z ) = 0;
  150. virtual void ScaleXY( float x, float y ) = 0;
  151. // Sets the color to modulate by
  152. virtual void Color3f( float r, float g, float b ) = 0;
  153. virtual void Color3fv( float const* pColor ) = 0;
  154. virtual void Color4f( float r, float g, float b, float a ) = 0;
  155. virtual void Color4fv( float const* pColor ) = 0;
  156. virtual void Color3ub( unsigned char r, unsigned char g, unsigned char b ) = 0;
  157. virtual void Color3ubv( unsigned char const* pColor ) = 0;
  158. virtual void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a ) = 0;
  159. virtual void Color4ubv( unsigned char const* pColor ) = 0;
  160. // Sets the constant register for vertex and pixel shaders
  161. virtual void SetVertexShaderConstant( int var, float const* pVec, int numConst = 1, bool bForce = false ) = 0;
  162. virtual void SetPixelShaderConstant( int var, float const* pVec, int numConst = 1, bool bForce = false ) = 0;
  163. // Sets the default *dynamic* state
  164. virtual void SetDefaultState() = 0;
  165. // Get the current camera position in world space.
  166. virtual void GetWorldSpaceCameraPosition( float* pPos ) const = 0;
  167. virtual int GetCurrentNumBones( void ) const = 0;
  168. virtual int GetCurrentLightCombo( void ) const = 0;
  169. virtual MaterialFogMode_t GetCurrentFogType( void ) const = 0;
  170. // fixme: move this to shadow state
  171. virtual void SetTextureTransformDimension( TextureStage_t textureStage, int dimension, bool projected ) = 0;
  172. virtual void DisableTextureTransform( TextureStage_t textureStage ) = 0;
  173. virtual void SetBumpEnvMatrix( TextureStage_t textureStage, float m00, float m01, float m10, float m11 ) = 0;
  174. // Sets the vertex and pixel shaders
  175. virtual void SetVertexShaderIndex( int vshIndex = -1 ) = 0;
  176. virtual void SetPixelShaderIndex( int pshIndex = 0 ) = 0;
  177. // Get the dimensions of the back buffer.
  178. virtual void GetBackBufferDimensions( int& width, int& height ) const = 0;
  179. // FIXME: The following 6 methods used to live in IShaderAPI
  180. // and were moved for stdshader_dx8. Let's try to move them back!
  181. // Get the lights
  182. virtual int GetMaxLights( void ) const = 0;
  183. virtual const LightDesc_t& GetLight( int lightNum ) const = 0;
  184. virtual void SetPixelShaderFogParams( int reg ) = 0;
  185. // Render state for the ambient light cube
  186. virtual void SetVertexShaderStateAmbientLightCube() = 0;
  187. virtual void SetPixelShaderStateAmbientLightCube( int pshReg, bool bForceToBlack = false ) = 0;
  188. virtual void CommitPixelShaderLighting( int pshReg ) = 0;
  189. // Use this to get the mesh builder that allows us to modify vertex data
  190. virtual CMeshBuilder* GetVertexModifyBuilder() = 0;
  191. virtual bool InFlashlightMode() const = 0;
  192. virtual const FlashlightState_t &GetFlashlightState( VMatrix &worldToTexture ) const = 0;
  193. virtual bool InEditorMode() const = 0;
  194. // Gets the bound morph's vertex format; returns 0 if no morph is bound
  195. virtual MorphFormat_t GetBoundMorphFormat() = 0;
  196. // Binds a standard texture
  197. virtual void BindStandardTexture( Sampler_t sampler, StandardTextureId_t id ) = 0;
  198. virtual ITexture *GetRenderTargetEx( int nRenderTargetID ) = 0;
  199. virtual void SetToneMappingScaleLinear( const Vector &scale ) = 0;
  200. virtual const Vector &GetToneMappingScaleLinear( void ) const = 0;
  201. virtual float GetLightMapScaleFactor( void ) const = 0;
  202. virtual void LoadBoneMatrix( int boneIndex, const float *m ) = 0;
  203. virtual void PerspectiveOffCenterX( double fovx, double aspect, double zNear, double zFar, double bottom, double top, double left, double right ) = 0;
  204. virtual void SetFloatRenderingParameter(int parm_number, float value) = 0;
  205. virtual void SetIntRenderingParameter(int parm_number, int value) = 0 ;
  206. virtual void SetVectorRenderingParameter(int parm_number, Vector const &value) = 0 ;
  207. virtual float GetFloatRenderingParameter(int parm_number) const = 0 ;
  208. virtual int GetIntRenderingParameter(int parm_number) const = 0 ;
  209. virtual Vector GetVectorRenderingParameter(int parm_number) const = 0 ;
  210. // stencil buffer operations.
  211. virtual void SetStencilEnable(bool onoff) = 0;
  212. virtual void SetStencilFailOperation(StencilOperation_t op) = 0;
  213. virtual void SetStencilZFailOperation(StencilOperation_t op) = 0;
  214. virtual void SetStencilPassOperation(StencilOperation_t op) = 0;
  215. virtual void SetStencilCompareFunction(StencilComparisonFunction_t cmpfn) = 0;
  216. virtual void SetStencilReferenceValue(int ref) = 0;
  217. virtual void SetStencilTestMask(uint32 msk) = 0;
  218. virtual void SetStencilWriteMask(uint32 msk) = 0;
  219. virtual void ClearStencilBufferRectangle( int xmin, int ymin, int xmax, int ymax,int value) = 0;
  220. virtual void GetDXLevelDefaults(uint &max_dxlevel,uint &recommended_dxlevel) = 0;
  221. virtual const FlashlightState_t &GetFlashlightStateEx( VMatrix &worldToTexture, ITexture **pFlashlightDepthTexture ) const = 0;
  222. virtual float GetAmbientLightCubeLuminance() = 0;
  223. virtual void GetDX9LightState( LightState_t *state ) const = 0;
  224. virtual int GetPixelFogCombo( ) = 0; //0 is either range fog, or no fog simulated with rigged range fog values. 1 is height fog
  225. virtual void BindStandardVertexTexture( VertexTextureSampler_t sampler, StandardTextureId_t id ) = 0;
  226. // Is hardware morphing enabled?
  227. virtual bool IsHWMorphingEnabled( ) const = 0;
  228. virtual void GetStandardTextureDimensions( int *pWidth, int *pHeight, StandardTextureId_t id ) = 0;
  229. virtual void SetBooleanVertexShaderConstant( int var, BOOL const* pVec, int numBools = 1, bool bForce = false ) = 0;
  230. virtual void SetIntegerVertexShaderConstant( int var, int const* pVec, int numIntVecs = 1, bool bForce = false ) = 0;
  231. virtual void SetBooleanPixelShaderConstant( int var, BOOL const* pVec, int numBools = 1, bool bForce = false ) = 0;
  232. virtual void SetIntegerPixelShaderConstant( int var, int const* pVec, int numIntVecs = 1, bool bForce = false ) = 0;
  233. //Are we in a configuration that needs access to depth data through the alpha channel later?
  234. virtual bool ShouldWriteDepthToDestAlpha( void ) const = 0;
  235. // deformations
  236. virtual void PushDeformation( DeformationBase_t const *Deformation ) = 0;
  237. virtual void PopDeformation( ) = 0;
  238. virtual int GetNumActiveDeformations() const =0;
  239. // for shaders to set vertex shader constants. returns a packed state which can be used to set
  240. // the dynamic combo. returns # of active deformations
  241. virtual int GetPackedDeformationInformation( int nMaskOfUnderstoodDeformations,
  242. float *pConstantValuesOut,
  243. int nBufferSize,
  244. int nMaximumDeformations,
  245. int *pNumDefsOut ) const = 0;
  246. // This lets the lower level system that certain vertex fields requested
  247. // in the shadow state aren't actually being read given particular state
  248. // known only at dynamic state time. It's here only to silence warnings.
  249. virtual void MarkUnusedVertexFields( unsigned int nFlags, int nTexCoordCount, bool *pUnusedTexCoords ) = 0;
  250. virtual void ExecuteCommandBuffer( uint8 *pCmdBuffer ) =0;
  251. // interface for mat system to tell shaderapi about standard texture handles
  252. virtual void SetStandardTextureHandle( StandardTextureId_t nId, ShaderAPITextureHandle_t nHandle ) =0;
  253. // Interface for mat system to tell shaderapi about color correction
  254. virtual void GetCurrentColorCorrection( ShaderColorCorrectionInfo_t* pInfo ) = 0;
  255. virtual void SetPSNearAndFarZ( int pshReg ) = 0;
  256. virtual void SetDepthFeatheringPixelShaderConstant( int iConstant, float fDepthBlendScale ) = 0;
  257. };
  258. // end class IShaderDynamicAPI
  259. //-----------------------------------------------------------------------------
  260. // Software vertex shaders
  261. //-----------------------------------------------------------------------------
  262. typedef void (*SoftwareVertexShader_t)( CMeshBuilder& meshBuilder, IMaterialVar **params, IShaderDynamicAPI *pShaderAPI );
  263. #endif // ISHADERDYNAMIC_H