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.

220 lines
6.9 KiB

  1. //===== Copyright � 1996-2005, 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 "materialsystem/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 = 3
  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. CBasePerInstanceContextData *m_pInstanceData[MAX_RENDER_PASSES];
  79. };
  80. struct ShaderRenderState_t
  81. {
  82. // These are the same, regardless of whether alpha or color mod is used
  83. int m_Flags; // Can't shrink this to a short
  84. VertexFormat_t m_VertexFormat;
  85. VertexFormat_t m_VertexUsage;
  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 = 8,
  95. SNAPSHOT_COUNT_EDITOR = 16,
  96. SNAPSHOT_COUNT_GBUFFER = 64,
  97. };
  98. inline int SnapshotTypeCount()
  99. {
  100. int nMaterialUseFlags = MaterialSystem()->GetConfigurationFlags();
  101. if ( nMaterialUseFlags & MATCONFIG_FLAGS_SUPPORT_GBUFFER )
  102. return SNAPSHOT_COUNT_GBUFFER;
  103. if ( nMaterialUseFlags & MATCONFIG_FLAGS_SUPPORT_EDITOR )
  104. return SNAPSHOT_COUNT_EDITOR;
  105. return SNAPSHOT_COUNT_NORMAL;
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Utility methods
  109. //-----------------------------------------------------------------------------
  110. inline void SetFlags( IMaterialVar **params, MaterialVarFlags_t _flag )
  111. {
  112. params[FLAGS]->SetIntValue( params[FLAGS]->GetIntValueFast() | (_flag) );
  113. }
  114. inline void SetFlags2( IMaterialVar **params, MaterialVarFlags2_t _flag )
  115. {
  116. params[FLAGS2]->SetIntValue( params[FLAGS2]->GetIntValueFast() | (_flag) );
  117. }
  118. inline bool IsFlagSet( IMaterialVar **params, MaterialVarFlags_t _flag )
  119. {
  120. return ((params[FLAGS]->GetIntValueFast() & (_flag) ) != 0);
  121. }
  122. inline bool IsFlag2Set( IMaterialVar **params, MaterialVarFlags2_t _flag )
  123. {
  124. return ((params[FLAGS2]->GetIntValueFast() & (_flag) ) != 0);
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Poll params + renderstate
  128. //-----------------------------------------------------------------------------
  129. inline bool IsTranslucent( const ShaderRenderState_t* pRenderState )
  130. {
  131. return (pRenderState->m_Flags & SHADER_OPACITY_TRANSLUCENT) != 0;
  132. }
  133. inline bool IsAlphaTested( ShaderRenderState_t* pRenderState )
  134. {
  135. return (pRenderState->m_Flags & SHADER_OPACITY_ALPHATEST) != 0;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // The shader system (a singleton)
  139. //-----------------------------------------------------------------------------
  140. abstract_class IShaderSystemInternal : public IShaderInit, public IShaderSystem
  141. {
  142. public:
  143. // Initialization, shutdown
  144. virtual void Init() = 0;
  145. virtual void Shutdown() = 0;
  146. virtual void ModInit() = 0;
  147. virtual void ModShutdown() = 0;
  148. // Methods related to reading in shader DLLs
  149. virtual bool LoadShaderDLL( const char *pFullPath ) = 0;
  150. virtual void UnloadShaderDLL( const char *pFullPath ) = 0;
  151. // Find me a shader!
  152. virtual IShader* FindShader( char const* pShaderName ) = 0;
  153. // returns strings associated with the shader state flags...
  154. virtual char const* ShaderStateString( int i ) const = 0;
  155. virtual int ShaderStateCount( ) const = 0;
  156. // Rendering related methods
  157. // Create debugging materials
  158. virtual void CreateDebugMaterials() = 0;
  159. // Cleans up the debugging materials
  160. virtual void CleanUpDebugMaterials() = 0;
  161. // Call the SHADER_PARAM_INIT block of the shaders
  162. virtual void InitShaderParameters( IShader *pShader, IMaterialVar **params, const char *pMaterialName ) = 0;
  163. // Call the SHADER_INIT block of the shaders
  164. virtual void InitShaderInstance( IShader *pShader, IMaterialVar **params, const char *pMaterialName, const char *pTextureGroupName ) = 0;
  165. // go through each param and make sure it is the right type, load textures,
  166. // compute state snapshots and vertex types, etc.
  167. virtual bool InitRenderState( IShader *pShader, int numParams, IMaterialVar **params, ShaderRenderState_t* pRenderState, char const* pMaterialName ) = 0;
  168. // When you're done with the shader, be sure to call this to clean up
  169. virtual void CleanupRenderState( ShaderRenderState_t* pRenderState ) = 0;
  170. // Draws the shader
  171. virtual void DrawElements( IShader *pShader, IMaterialVar **params, ShaderRenderState_t* pShaderState, VertexCompressionType_t vertexCompression,
  172. uint32 nMaterialVarTimeStamp, uint32 nModulationFlags, bool bRenderingPreTessPatchMesh ) = 0;
  173. // Used to iterate over all shaders for editing purposes
  174. virtual int ShaderCount() const = 0;
  175. virtual int GetShaders( int nFirstShader, int nCount, IShader **ppShaderList ) const = 0;
  176. };
  177. #endif // SHADERSYSTEM_H