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.

245 lines
6.3 KiB

  1. //========= Copyright 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 char const * GetName( void ) const = 0;
  70. virtual MaterialVarSym_t GetNameAsSymbol() const = 0;
  71. virtual void SetFloatValue( float val ) = 0;
  72. virtual void SetIntValue( int val ) = 0;
  73. virtual void SetStringValue( char const *val ) = 0;
  74. virtual char const * GetStringValue( void ) const = 0;
  75. // Use FourCC values to pass app-defined data structures between
  76. // the proxy and the shader. The shader should ignore the data if
  77. // its FourCC type not correct.
  78. virtual void SetFourCCValue( FourCC type, void *pData ) = 0;
  79. virtual void GetFourCCValue( FourCC *type, void **ppData ) = 0;
  80. // Vec (dim 2-4)
  81. virtual void SetVecValue( float const* val, int numcomps ) = 0;
  82. virtual void SetVecValue( float x, float y ) = 0;
  83. virtual void SetVecValue( float x, float y, float z ) = 0;
  84. virtual void SetVecValue( float x, float y, float z, float w ) = 0;
  85. virtual void GetLinearVecValue( float *val, int numcomps ) const = 0;
  86. // revisit: is this a good interface for textures?
  87. virtual void SetTextureValue( ITexture * ) = 0;
  88. virtual IMaterial * GetMaterialValue( void ) = 0;
  89. virtual void SetMaterialValue( IMaterial * ) = 0;
  90. virtual bool IsDefined() const = 0;
  91. virtual void SetUndefined() = 0;
  92. // Matrix
  93. virtual void SetMatrixValue( VMatrix const& matrix ) = 0;
  94. virtual const VMatrix &GetMatrixValue( ) = 0;
  95. virtual bool MatrixIsIdentity() const = 0;
  96. // Copy....
  97. virtual void CopyFrom( IMaterialVar *pMaterialVar ) = 0;
  98. virtual void SetValueAutodetectType( char const *val ) = 0;
  99. virtual IMaterial * GetOwningMaterial() = 0;
  100. //set just 1 component
  101. virtual void SetVecComponentValue( float fVal, int nComponent ) = 0;
  102. protected:
  103. virtual int GetIntValueInternal( void ) const = 0;
  104. virtual float GetFloatValueInternal( void ) const = 0;
  105. virtual float const* GetVecValueInternal( ) const = 0;
  106. virtual void GetVecValueInternal( float *val, int numcomps ) const = 0;
  107. virtual int VectorSizeInternal() const = 0;
  108. public:
  109. FORCEINLINE MaterialVarType_t GetType( void ) const
  110. {
  111. return ( MaterialVarType_t )m_Type;
  112. }
  113. FORCEINLINE bool IsTexture() const
  114. {
  115. return m_Type == MATERIAL_VAR_TYPE_TEXTURE;
  116. }
  117. FORCEINLINE operator ITexture*()
  118. {
  119. return GetTextureValue();
  120. }
  121. // NOTE: Fast methods should only be called in thread-safe situations
  122. FORCEINLINE int GetIntValueFast( void ) const
  123. {
  124. // Set methods for float and vector update this
  125. return m_intVal;
  126. }
  127. FORCEINLINE float GetFloatValueFast( void ) const
  128. {
  129. return m_VecVal[0];
  130. }
  131. FORCEINLINE float const* GetVecValueFast( ) const
  132. {
  133. return m_VecVal.Base();
  134. }
  135. FORCEINLINE void GetVecValueFast( float *val, int numcomps ) const
  136. {
  137. Assert( ( numcomps >0 ) && ( numcomps <= 4 ) );
  138. for( int i=0 ; i < numcomps; i++ )
  139. {
  140. val[i] = m_VecVal[ i ];
  141. }
  142. }
  143. FORCEINLINE int VectorSizeFast() const
  144. {
  145. return m_nNumVectorComps;
  146. }
  147. #ifdef FAST_MATERIALVAR_ACCESS
  148. FORCEINLINE int GetIntValue( void ) const
  149. {
  150. return GetIntValueFast();
  151. }
  152. FORCEINLINE float GetFloatValue( void ) const
  153. {
  154. return GetFloatValueFast();
  155. }
  156. FORCEINLINE float const* GetVecValue( ) const
  157. {
  158. return GetVecValueFast();
  159. }
  160. FORCEINLINE void GetVecValue( float *val, int numcomps ) const
  161. {
  162. GetVecValueFast( val, numcomps );
  163. }
  164. FORCEINLINE int VectorSize() const
  165. {
  166. return VectorSizeFast();
  167. }
  168. #else // !FAST_MATERIALVAR_ACCESS
  169. FORCEINLINE int GetIntValue( void ) const
  170. {
  171. return GetIntValueInternal();
  172. }
  173. FORCEINLINE float GetFloatValue( void ) const
  174. {
  175. return GetFloatValueInternal();
  176. }
  177. FORCEINLINE float const* GetVecValue( ) const
  178. {
  179. return GetVecValueInternal();
  180. }
  181. FORCEINLINE void GetVecValue( float *val, int numcomps ) const
  182. {
  183. return GetVecValueInternal( val, numcomps );
  184. }
  185. FORCEINLINE int VectorSize() const
  186. {
  187. return VectorSizeInternal();
  188. }
  189. #endif
  190. private:
  191. FORCEINLINE void SetTempIndex( int nIndex )
  192. {
  193. m_nTempIndex = nIndex;
  194. }
  195. friend void EnableThreadedMaterialVarAccess( bool bEnable, IMaterialVar **ppParams, int nVarCount );
  196. };
  197. #endif // IMATERIALVAR_H