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.

216 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //
  8. // Shader system:
  9. // The shader system makes a few fundamental assumptions about when
  10. // certain types of state get set.
  11. //
  12. // 1) Anything that can potentially affect vertex format must be set up
  13. // during the shader shadow/snapshot phase
  14. // 2) Anything that we can dynamically mess with (through a material var)
  15. // should happen in the dynamic/render phase
  16. // 3) In general, we try to cache off expensive state pre-processing in
  17. // the shader shadow phase (like texture stage pipeline).
  18. //
  19. //=============================================================================//
  20. #ifndef SHADERSYSTEM_H
  21. #define SHADERSYSTEM_H
  22. #ifdef _WIN32
  23. #pragma once
  24. #endif
  25. #include "IShaderSystem.h"
  26. #include "shaderlib/BaseShader.h"
  27. #include "materialsystem/materialsystem_config.h"
  28. #include "shaderapi/ishaderapi.h"
  29. #include "materialsystem_global.h"
  30. //-----------------------------------------------------------------------------
  31. // forward declarations
  32. //-----------------------------------------------------------------------------
  33. class IMaterialVar;
  34. class TextureManager_t;
  35. class ITextureInternal;
  36. class ShaderSystem_t;
  37. class IMesh;
  38. class IVertexBuffer;
  39. class IIndexBuffer;
  40. class Vector;
  41. enum MaterialPrimitiveType_t;
  42. enum MaterialPropertyTypes_t;
  43. enum MaterialIndexFormat_t;
  44. enum ShaderParamType_t;
  45. //-----------------------------------------------------------------------------
  46. // for ShaderRenderState_t::m_flags
  47. //-----------------------------------------------------------------------------
  48. enum
  49. {
  50. // The flags up here are computed from the shaders themselves
  51. /*
  52. // lighting flags
  53. SHADER_UNLIT = 0x0000,
  54. SHADER_VERTEX_LIT = 0x0001,
  55. SHADER_NEEDS_LIGHTMAP = 0x0002,
  56. SHADER_NEEDS_BUMPED_LIGHTMAPS = 0x0004,
  57. SHADER_LIGHTING_MASK = 0x0007,
  58. */
  59. // opacity flags
  60. SHADER_OPACITY_ALPHATEST = 0x0010,
  61. SHADER_OPACITY_OPAQUE = 0x0020,
  62. SHADER_OPACITY_TRANSLUCENT = 0x0040,
  63. SHADER_OPACITY_MASK = 0x0070,
  64. };
  65. enum
  66. {
  67. MAX_RENDER_PASSES = 4
  68. };
  69. //-----------------------------------------------------------------------------
  70. // Information for a single render pass
  71. //-----------------------------------------------------------------------------
  72. struct RenderPassList_t
  73. {
  74. int m_nPassCount;
  75. StateSnapshot_t m_Snapshot[MAX_RENDER_PASSES];
  76. // per material shader-defined state
  77. CBasePerMaterialContextData *m_pContextData[MAX_RENDER_PASSES];
  78. };
  79. struct ShaderRenderState_t
  80. {
  81. // These are the same, regardless of whether alpha or color mod is used
  82. int m_Flags; // Can't shrink this to a short
  83. VertexFormat_t m_VertexFormat;
  84. VertexFormat_t m_VertexUsage;
  85. MorphFormat_t m_MorphFormat;
  86. // List of all snapshots
  87. RenderPassList_t *m_pSnapshots;
  88. };
  89. //-----------------------------------------------------------------------------
  90. // Used to get the snapshot count
  91. //-----------------------------------------------------------------------------
  92. enum
  93. {
  94. SNAPSHOT_COUNT_NORMAL = 16,
  95. SNAPSHOT_COUNT_EDITOR = 32,
  96. };
  97. inline int SnapshotTypeCount()
  98. {
  99. return MaterialSystem()->CanUseEditorMaterials() ? SNAPSHOT_COUNT_EDITOR : SNAPSHOT_COUNT_NORMAL;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Utility methods
  103. //-----------------------------------------------------------------------------
  104. inline void SetFlags( IMaterialVar **params, MaterialVarFlags_t _flag )
  105. {
  106. params[FLAGS]->SetIntValue( params[FLAGS]->GetIntValueFast() | (_flag) );
  107. }
  108. inline void SetFlags2( IMaterialVar **params, MaterialVarFlags2_t _flag )
  109. {
  110. params[FLAGS2]->SetIntValue( params[FLAGS2]->GetIntValueFast() | (_flag) );
  111. }
  112. inline bool IsFlagSet( IMaterialVar **params, MaterialVarFlags_t _flag )
  113. {
  114. return ((params[FLAGS]->GetIntValueFast() & (_flag) ) != 0);
  115. }
  116. inline bool IsFlag2Set( IMaterialVar **params, MaterialVarFlags2_t _flag )
  117. {
  118. return ((params[FLAGS2]->GetIntValueFast() & (_flag) ) != 0);
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Poll params + renderstate
  122. //-----------------------------------------------------------------------------
  123. inline bool IsTranslucent( const ShaderRenderState_t* pRenderState )
  124. {
  125. return (pRenderState->m_Flags & SHADER_OPACITY_TRANSLUCENT) != 0;
  126. }
  127. inline bool IsAlphaTested( ShaderRenderState_t* pRenderState )
  128. {
  129. return (pRenderState->m_Flags & SHADER_OPACITY_ALPHATEST) != 0;
  130. }
  131. //-----------------------------------------------------------------------------
  132. // The shader system (a singleton)
  133. //-----------------------------------------------------------------------------
  134. abstract_class IShaderSystemInternal : public IShaderInit, public IShaderSystem
  135. {
  136. public:
  137. // Initialization, shutdown
  138. virtual void Init() = 0;
  139. virtual void Shutdown() = 0;
  140. virtual void ModInit() = 0;
  141. virtual void ModShutdown() = 0;
  142. // Methods related to reading in shader DLLs
  143. virtual bool LoadShaderDLL( const char *pFullPath ) = 0;
  144. virtual void UnloadShaderDLL( const char *pFullPath ) = 0;
  145. // Find me a shader!
  146. virtual IShader* FindShader( char const* pShaderName ) = 0;
  147. // returns strings associated with the shader state flags...
  148. virtual char const* ShaderStateString( int i ) const = 0;
  149. virtual int ShaderStateCount( ) const = 0;
  150. // Rendering related methods
  151. // Create debugging materials
  152. virtual void CreateDebugMaterials() = 0;
  153. // Cleans up the debugging materials
  154. virtual void CleanUpDebugMaterials() = 0;
  155. // Call the SHADER_PARAM_INIT block of the shaders
  156. virtual void InitShaderParameters( IShader *pShader, IMaterialVar **params, const char *pMaterialName ) = 0;
  157. // Call the SHADER_INIT block of the shaders
  158. virtual void InitShaderInstance( IShader *pShader, IMaterialVar **params, const char *pMaterialName, const char *pTextureGroupName ) = 0;
  159. // go through each param and make sure it is the right type, load textures,
  160. // compute state snapshots and vertex types, etc.
  161. virtual bool InitRenderState( IShader *pShader, int numParams, IMaterialVar **params, ShaderRenderState_t* pRenderState, char const* pMaterialName ) = 0;
  162. // When you're done with the shader, be sure to call this to clean up
  163. virtual void CleanupRenderState( ShaderRenderState_t* pRenderState ) = 0;
  164. // Draws the shader
  165. virtual void DrawElements( IShader *pShader, IMaterialVar **params, ShaderRenderState_t* pShaderState, VertexCompressionType_t vertexCompression,
  166. uint32 nMaterialVarTimeStamp ) = 0;
  167. // Used to iterate over all shaders for editing purposes
  168. virtual int ShaderCount() const = 0;
  169. virtual int GetShaders( int nFirstShader, int nCount, IShader **ppShaderList ) const = 0;
  170. };
  171. #endif // SHADERSYSTEM_H