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.

246 lines
6.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef IMATERIALVAR_H
  9. #define IMATERIALVAR_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier0/platform.h"
  14. #include "tier1/utlsymbol.h"
  15. #include "mathlib/vector4d.h"
  16. class IMaterial;
  17. class VMatrix;
  18. class ITexture;
  19. #define MAKE_MATERIALVAR_FOURCC(ch0, ch1, ch2, ch3) \
  20. ((unsigned long)(ch0) | ((unsigned long)(ch1) << 8) | \
  21. ((unsigned long)(ch2) << 16) | ((unsigned long)(ch3) << 24 ))
  22. // This fourcc is reserved.
  23. #define FOURCC_UNKNOWN MAKE_MATERIALVAR_FOURCC('U','N','K','N')
  24. //-----------------------------------------------------------------------------
  25. // Various material var types
  26. //-----------------------------------------------------------------------------
  27. enum MaterialVarType_t
  28. {
  29. MATERIAL_VAR_TYPE_FLOAT = 0,
  30. MATERIAL_VAR_TYPE_STRING,
  31. MATERIAL_VAR_TYPE_VECTOR,
  32. MATERIAL_VAR_TYPE_TEXTURE,
  33. MATERIAL_VAR_TYPE_INT,
  34. MATERIAL_VAR_TYPE_FOURCC,
  35. MATERIAL_VAR_TYPE_UNDEFINED,
  36. MATERIAL_VAR_TYPE_MATRIX,
  37. MATERIAL_VAR_TYPE_MATERIAL,
  38. };
  39. typedef unsigned short MaterialVarSym_t;
  40. class IMaterialVar
  41. {
  42. public:
  43. typedef unsigned long FourCC;
  44. protected:
  45. // base data and accessors
  46. char* m_pStringVal;
  47. int m_intVal;
  48. Vector4D m_VecVal;
  49. // member data. total = 4 bytes
  50. uint8 m_Type : 4;
  51. uint8 m_nNumVectorComps : 3;
  52. uint8 m_bFakeMaterialVar : 1;
  53. uint8 m_nTempIndex;
  54. CUtlSymbol m_Name;
  55. public:
  56. // class factory methods
  57. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey, VMatrix const& matrix );
  58. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey, char const* pVal );
  59. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey, float* pVal, int numcomps );
  60. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey, float val );
  61. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey, int val );
  62. static IMaterialVar* Create( IMaterial* pMaterial, char const* pKey );
  63. static void Destroy( IMaterialVar* pVar );
  64. static MaterialVarSym_t GetSymbol( char const* pName );
  65. static MaterialVarSym_t FindSymbol( char const* pName );
  66. static bool SymbolMatches( char const* pName, MaterialVarSym_t symbol );
  67. static void DeleteUnreferencedTextures( bool enable );
  68. virtual ITexture *GetTextureValue( void ) = 0;
  69. virtual bool IsTextureValueInternalEnvCubemap( void ) const = 0;
  70. virtual char const * GetName( void ) const = 0;
  71. virtual MaterialVarSym_t GetNameAsSymbol() const = 0;
  72. virtual void SetFloatValue( float val ) = 0;
  73. virtual void SetIntValue( int val ) = 0;
  74. virtual void SetStringValue( char const *val ) = 0;
  75. virtual char const * GetStringValue( void ) const = 0;
  76. // Use FourCC values to pass app-defined data structures between
  77. // the proxy and the shader. The shader should ignore the data if
  78. // its FourCC type not correct.
  79. virtual void SetFourCCValue( FourCC type, void *pData ) = 0;
  80. virtual void GetFourCCValue( FourCC *type, void **ppData ) = 0;
  81. // Vec (dim 2-4)
  82. virtual void SetVecValue( float const* val, int numcomps ) = 0;
  83. virtual void SetVecValue( float x, float y ) = 0;
  84. virtual void SetVecValue( float x, float y, float z ) = 0;
  85. virtual void SetVecValue( float x, float y, float z, float w ) = 0;
  86. virtual void GetLinearVecValue( float *val, int numcomps ) const = 0;
  87. // revisit: is this a good interface for textures?
  88. virtual void SetTextureValue( ITexture * ) = 0;
  89. virtual IMaterial * GetMaterialValue( void ) = 0;
  90. virtual void SetMaterialValue( IMaterial * ) = 0;
  91. virtual bool IsDefined() const = 0;
  92. virtual void SetUndefined() = 0;
  93. // Matrix
  94. virtual void SetMatrixValue( VMatrix const& matrix ) = 0;
  95. virtual const VMatrix &GetMatrixValue( ) = 0;
  96. virtual bool MatrixIsIdentity() const = 0;
  97. // Copy....
  98. virtual void CopyFrom( IMaterialVar *pMaterialVar ) = 0;
  99. virtual void SetValueAutodetectType( char const *val ) = 0;
  100. virtual IMaterial * GetOwningMaterial() = 0;
  101. //set just 1 component
  102. virtual void SetVecComponentValue( float fVal, int nComponent ) = 0;
  103. protected:
  104. virtual int GetIntValueInternal( void ) const = 0;
  105. virtual float GetFloatValueInternal( void ) const = 0;
  106. virtual float const* GetVecValueInternal( ) const = 0;
  107. virtual void GetVecValueInternal( float *val, int numcomps ) const = 0;
  108. virtual int VectorSizeInternal() const = 0;
  109. public:
  110. FORCEINLINE MaterialVarType_t GetType( void ) const
  111. {
  112. return ( MaterialVarType_t )m_Type;
  113. }
  114. FORCEINLINE bool IsTexture() const
  115. {
  116. return m_Type == MATERIAL_VAR_TYPE_TEXTURE;
  117. }
  118. FORCEINLINE operator ITexture*()
  119. {
  120. return GetTextureValue();
  121. }
  122. // NOTE: Fast methods should only be called in thread-safe situations
  123. FORCEINLINE int GetIntValueFast( void ) const
  124. {
  125. // Set methods for float and vector update this
  126. return m_intVal;
  127. }
  128. FORCEINLINE float GetFloatValueFast( void ) const
  129. {
  130. return m_VecVal[0];
  131. }
  132. FORCEINLINE float const* GetVecValueFast( ) const
  133. {
  134. return m_VecVal.Base();
  135. }
  136. FORCEINLINE void GetVecValueFast( float *val, int numcomps ) const
  137. {
  138. Assert( ( numcomps >0 ) && ( numcomps <= 4 ) );
  139. for( int i=0 ; i < numcomps; i++ )
  140. {
  141. val[i] = m_VecVal[ i ];
  142. }
  143. }
  144. FORCEINLINE int VectorSizeFast() const
  145. {
  146. return m_nNumVectorComps;
  147. }
  148. #ifdef FAST_MATERIALVAR_ACCESS
  149. FORCEINLINE int GetIntValue( void ) const
  150. {
  151. return GetIntValueFast();
  152. }
  153. FORCEINLINE float GetFloatValue( void ) const
  154. {
  155. return GetFloatValueFast();
  156. }
  157. FORCEINLINE float const* GetVecValue( ) const
  158. {
  159. return GetVecValueFast();
  160. }
  161. FORCEINLINE void GetVecValue( float *val, int numcomps ) const
  162. {
  163. GetVecValueFast( val, numcomps );
  164. }
  165. FORCEINLINE int VectorSize() const
  166. {
  167. return VectorSizeFast();
  168. }
  169. #else // !FAST_MATERIALVAR_ACCESS
  170. FORCEINLINE int GetIntValue( void ) const
  171. {
  172. return GetIntValueInternal();
  173. }
  174. FORCEINLINE float GetFloatValue( void ) const
  175. {
  176. return GetFloatValueInternal();
  177. }
  178. FORCEINLINE float const* GetVecValue( ) const
  179. {
  180. return GetVecValueInternal();
  181. }
  182. FORCEINLINE void GetVecValue( float *val, int numcomps ) const
  183. {
  184. return GetVecValueInternal( val, numcomps );
  185. }
  186. FORCEINLINE int VectorSize() const
  187. {
  188. return VectorSizeInternal();
  189. }
  190. #endif
  191. private:
  192. FORCEINLINE void SetTempIndex( int nIndex )
  193. {
  194. m_nTempIndex = nIndex;
  195. }
  196. friend void EnableThreadedMaterialVarAccess( bool bEnable, IMaterialVar **ppParams, int nVarCount );
  197. };
  198. #endif // IMATERIALVAR_H