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.

418 lines
11 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // A class representing a procedural texture
  4. //
  5. //=============================================================================
  6. #ifndef DMETEXTURE_H
  7. #define DMETEXTURE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "materialsystem/materialsystemutil.h"
  13. #include "materialobjects/dmeimage.h"
  14. #include "tier1/functors.h"
  15. //-----------------------------------------------------------------------------
  16. // Forward declarations
  17. //-----------------------------------------------------------------------------
  18. enum ImageFormat;
  19. //-----------------------------------------------------------------------------
  20. // Compression types
  21. //-----------------------------------------------------------------------------
  22. enum DmeTextureCompress_t
  23. {
  24. DMETEXTURE_COMPRESS_DEFAULT = 0,
  25. DMETEXTURE_COMPRESS_NONE,
  26. DMETEXTURE_COMPRESS_DXT1,
  27. DMETEXTURE_COMPRESS_DXT5,
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Filter types
  31. //-----------------------------------------------------------------------------
  32. enum DmeTextureFilter_t
  33. {
  34. DMETEXTURE_FILTER_DEFAULT = 0,
  35. DMETEXTURE_FILTER_ANISOTROPIC,
  36. DMETEXTURE_FILTER_TRILINEAR,
  37. DMETEXTURE_FILTER_BILINEAR,
  38. DMETEXTURE_FILTER_POINT,
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Mipmap types
  42. //-----------------------------------------------------------------------------
  43. enum DmeTextureMipmap_t
  44. {
  45. DMETEXTURE_MIPMAP_DEFAULT = 0,
  46. DMETEXTURE_MIPMAP_ALL_LEVELS,
  47. DMETEXTURE_MIPMAP_NONE,
  48. };
  49. //-----------------------------------------------------------------------------
  50. // Texture types
  51. //-----------------------------------------------------------------------------
  52. enum DmeTextureType_t
  53. {
  54. DMETEXTURE_TYPE_NORMAL = 0,
  55. DMETEXTURE_TYPE_CUBEMAP,
  56. };
  57. //-----------------------------------------------------------------------------
  58. // A class for textures
  59. //-----------------------------------------------------------------------------
  60. class CDmeTextureFrame : public CDmElement
  61. {
  62. DEFINE_ELEMENT( CDmeTextureFrame, CDmElement );
  63. public:
  64. int MipLevelCount() const;
  65. CDmeImageArray *GetMipLevel( int nIndex ) const;
  66. void AddMipLevel( CDmeImageArray *pImages );
  67. CDmeImageArray *AddMipLevel( );
  68. int ImageCount() const;
  69. private:
  70. // Mip levels for the texture
  71. CDmaElementArray< CDmeImageArray > m_MipLevels;
  72. };
  73. //-----------------------------------------------------------------------------
  74. // Helper functors
  75. //-----------------------------------------------------------------------------
  76. template< typename ObjectType_t > class CImageMemberFunctor
  77. {
  78. public:
  79. CImageMemberFunctor( ObjectType_t *pObject, bool (ObjectType_t::*pMemberFunc)( CDmeImage * ) )
  80. {
  81. m_pObject = pObject;
  82. m_pMemberFunc = pMemberFunc;
  83. }
  84. bool operator()( CDmeImage *pImage )
  85. {
  86. return m_pObject->*m_pMemberFunc( pImage );
  87. }
  88. private:
  89. ObjectType_t *m_pObject;
  90. bool (ObjectType_t::*m_pMemberFunc)( CDmeImage * );
  91. };
  92. class CImageFunctor
  93. {
  94. public:
  95. CImageFunctor( bool (*pMemberFunc)( CDmeImage * ) )
  96. {
  97. m_pFunc = pMemberFunc;
  98. }
  99. bool operator()( CDmeImage *pImage )
  100. {
  101. return m_pFunc( pImage );
  102. }
  103. private:
  104. bool (*m_pFunc)( CDmeImage * );
  105. };
  106. template< typename ObjectType_t > class CImageProcessorMemberFunctor
  107. {
  108. public:
  109. CImageProcessorMemberFunctor( ObjectType_t *pObject, void (ObjectType_t::*pMemberFunc)( CDmeImage *, CDmeImage * ) )
  110. {
  111. m_pObject = pObject;
  112. m_pMemberFunc = pMemberFunc;
  113. }
  114. void operator()( CDmeImage *pDstImage, CDmeImage *pSrcImage )
  115. {
  116. (m_pObject->*m_pMemberFunc)( pDstImage, pSrcImage );
  117. }
  118. private:
  119. ObjectType_t *m_pObject;
  120. void (ObjectType_t::*m_pMemberFunc)( CDmeImage *, CDmeImage * );
  121. };
  122. //-----------------------------------------------------------------------------
  123. // A class for textures
  124. //-----------------------------------------------------------------------------
  125. class CDmeTexture : public CDmElement
  126. {
  127. DEFINE_ELEMENT( CDmeTexture, CDmElement );
  128. public:
  129. // Compression type
  130. void SetCompressionType( DmeTextureCompress_t type );
  131. DmeTextureCompress_t GetCompressionType() const;
  132. // Filter type
  133. void SetFilterType( DmeTextureFilter_t type );
  134. DmeTextureFilter_t GetFilterType() const;
  135. // Mipmap type
  136. void SetMipmapType( DmeTextureMipmap_t type );
  137. DmeTextureMipmap_t GetMipmapType() const;
  138. // Texture type
  139. void SetTextureType( DmeTextureType_t type );
  140. DmeTextureType_t GetTextureType() const;
  141. CDmeTextureFrame *AddFrame();
  142. int FrameCount() const;
  143. CDmeTextureFrame *GetFrame( int nIndex ) const;
  144. void RemoveAllFrames();
  145. // Gets dimensions
  146. int Width() const;
  147. int Height() const;
  148. int Depth() const;
  149. int MipLevelCount() const;
  150. int ImageCount() const;
  151. // Methods related to image format
  152. ImageFormat Format() const;
  153. // Returns all images associated with a particular frame + face (mip count amount)
  154. void GetImages( int nFrame, int nImageIndex, CDmeImage **ppImages, int nSize );
  155. // Iterates over all images, processes them
  156. template< typename Functor > void ProcessTexture( CDmeTexture *pSrcTexture, Functor &func );
  157. template< typename ObjectType_t > void ProcessTexture( CDmeTexture *pSrcTexture, ObjectType_t *pObject, void (ObjectType_t::*pMemberFunc)( CDmeImage *, CDmeImage * ) );
  158. // Iterates over all images, runs a functor
  159. template< typename Functor > void ForEachImage( Functor &func );
  160. template< typename ObjectType_t > void ForEachImage( ObjectType_t *pObject, bool (ObjectType_t::*pMemberFunc)( CDmeImage * ) );
  161. void ForEachImage( bool (*pFunc)( CDmeImage * ) );
  162. void CompressTexture( CDmeTexture *pSrcTexture, ImageFormat fmt );
  163. //-------------------------------------
  164. // Ignore the macro! To use this nifty feature, use code that looks like this:
  165. // pTexture->ForEachImage( &CDmeImage::ConvertFormat, dstFormat );
  166. //-------------------------------------
  167. #define DEFINE_FOR_EACH_IMAGE(N) \
  168. template <typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N FUNC_TEMPLATE_ARG_PARAMS_##N> \
  169. void ForEachImage( FUNCTION_RETTYPE ( CDmeImage::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) FUNC_ARG_FORMAL_PARAMS_##N ) \
  170. { \
  171. int nFrameCount = FrameCount(); \
  172. for ( int f = 0; f < nFrameCount; ++f ) \
  173. { \
  174. CDmeTextureFrame *pFrame = GetFrame( f ); \
  175. if ( !pFrame ) \
  176. continue; \
  177. int nMipCount = pFrame->MipLevelCount(); \
  178. for ( int m = 0; m < nMipCount; ++m ) \
  179. { \
  180. CDmeImageArray *pImageArray = pFrame->GetMipLevel( m ); \
  181. if ( !pImageArray ) \
  182. continue; \
  183. int nImageCount = pImageArray->ImageCount(); \
  184. for ( int i = 0; i < nImageCount; ++i ) \
  185. { \
  186. CDmeImage *pImage = pImageArray->GetImage( i ); \
  187. if ( !pImage ) \
  188. continue; \
  189. FunctorDirectCall( pImage, pfnProxied FUNC_FUNCTOR_CALL_ARGS_##N ); \
  190. } \
  191. } \
  192. } \
  193. }
  194. FUNC_GENERATE_ALL( DEFINE_FOR_EACH_IMAGE );
  195. #undef DEFINE_FOR_EACH_IMAGE
  196. public:
  197. virtual void Resolve();
  198. public:
  199. CDmaVar<bool> m_bClampS;
  200. CDmaVar<bool> m_bClampT;
  201. CDmaVar<bool> m_bClampU;
  202. CDmaVar<bool> m_bNoDebugOverride;
  203. CDmaVar<bool> m_bNoLod;
  204. CDmaVar<bool> m_bNiceFiltered;
  205. CDmaVar<bool> m_bNormalMap;
  206. CDmaVar<float> m_flBumpScale;
  207. protected:
  208. // Computes texture flags
  209. int CalcTextureFlags( int nDepth ) const;
  210. // Computes the desired texture format based on flags
  211. ImageFormat ComputeDesiredImageFormat( ImageFormat srcFormat, int nWidth, int nHeight, int nDepth, int nFlags );
  212. CDmaVar<int> m_nCompressType;
  213. CDmaVar<int> m_nFilterType;
  214. CDmaVar<int> m_nMipmapType;
  215. CDmaVar<int> m_nTextureType;
  216. // Array of images in an animated texture
  217. CDmaElementArray< CDmeTextureFrame > m_Frames;
  218. // Computed values
  219. // CTextureReference m_Texture;
  220. // IVTFTexture *m_pVTFTexture;
  221. Vector m_vecReflectivity;
  222. };
  223. //-----------------------------------------------------------------------------
  224. // Inline methods
  225. //-----------------------------------------------------------------------------
  226. inline void CDmeTexture::SetCompressionType( DmeTextureCompress_t type )
  227. {
  228. m_nCompressType = type;
  229. }
  230. inline DmeTextureCompress_t CDmeTexture::GetCompressionType() const
  231. {
  232. return (DmeTextureCompress_t)m_nCompressType.Get();
  233. }
  234. inline void CDmeTexture::SetFilterType( DmeTextureFilter_t type )
  235. {
  236. m_nFilterType = type;
  237. }
  238. inline DmeTextureFilter_t CDmeTexture::GetFilterType() const
  239. {
  240. return (DmeTextureFilter_t)m_nFilterType.Get();
  241. }
  242. inline void CDmeTexture::SetMipmapType( DmeTextureMipmap_t type )
  243. {
  244. m_nMipmapType = type;
  245. }
  246. inline DmeTextureMipmap_t CDmeTexture::GetMipmapType() const
  247. {
  248. return (DmeTextureMipmap_t)m_nMipmapType.Get();
  249. }
  250. inline void CDmeTexture::SetTextureType( DmeTextureType_t type )
  251. {
  252. m_nTextureType = type;
  253. }
  254. inline DmeTextureType_t CDmeTexture::GetTextureType() const
  255. {
  256. return (DmeTextureType_t)m_nTextureType.Get();
  257. }
  258. inline int CDmeTexture::FrameCount() const
  259. {
  260. return m_Frames.Count();
  261. }
  262. inline CDmeTextureFrame *CDmeTexture::GetFrame( int nIndex ) const
  263. {
  264. return m_Frames[nIndex];
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Invokes a functor on all images in the texture
  268. //-----------------------------------------------------------------------------
  269. template< typename Functor > inline void CDmeTexture::ForEachImage( Functor &func )
  270. {
  271. int nFrameCount = FrameCount();
  272. for ( int f = 0; f < nFrameCount; ++f )
  273. {
  274. CDmeTextureFrame *pFrame = GetFrame( f );
  275. if ( !pFrame )
  276. continue;
  277. int nMipCount = pFrame->MipLevelCount();
  278. for ( int m = 0; m < nMipCount; ++m )
  279. {
  280. CDmeImageArray *pImageArray = pFrame->GetMipLevel( m );
  281. if ( !pImageArray )
  282. continue;
  283. int nImageCount = pImageArray->ImageCount();
  284. for ( int i = 0; i < nImageCount; ++i )
  285. {
  286. CDmeImage *pImage = pImageArray->GetImage( i );
  287. if ( !pImage )
  288. continue;
  289. if ( !func( pImage ) )
  290. break;
  291. }
  292. }
  293. }
  294. }
  295. template< typename ObjectType_t > inline void CDmeTexture::ForEachImage( ObjectType_t *pObject, bool (ObjectType_t::*pMemberFunc)( CDmeImage * ) )
  296. {
  297. CImageMemberFunctor< ObjectType_t > functor( pObject, pMemberFunc );
  298. ForEachImage( functor );
  299. }
  300. inline void CDmeTexture::ForEachImage( bool (*pFunc)( CDmeImage * ) )
  301. {
  302. CImageFunctor functor( pFunc );
  303. ForEachImage( functor );
  304. }
  305. // Iterates over all images, processes them
  306. template< typename Functor > inline void CDmeTexture::ProcessTexture( CDmeTexture *pSrcTexture, Functor &func )
  307. {
  308. // FIXME: This pattern should go into some templatized type thingy
  309. pSrcTexture->CopyAttributesTo( this, TD_NONE );
  310. // Copying will copy references to src texture frames. Remove them.
  311. RemoveAllFrames();
  312. int nFrameCount = pSrcTexture->FrameCount();
  313. for ( int f = 0; f < nFrameCount; ++f )
  314. {
  315. CDmeTextureFrame *pSrcFrame = pSrcTexture->GetFrame( f );
  316. CDmeTextureFrame *pDstFrame = AddFrame();
  317. int nMipCount = pSrcFrame->MipLevelCount();
  318. for ( int m = 0; m < nMipCount; ++m )
  319. {
  320. CDmeImageArray *pSrcImageArray = pSrcFrame->GetMipLevel( m );
  321. CDmeImageArray *pDstImageArray = pDstFrame->AddMipLevel();
  322. if ( !pSrcImageArray )
  323. continue;
  324. int nImageCount = pSrcImageArray->ImageCount();
  325. for ( int i = 0; i < nImageCount; ++i )
  326. {
  327. CDmeImage *pSrcImage = pSrcImageArray->GetImage( i );
  328. CDmeImage *pDstImage = pDstImageArray->AddImage( );
  329. if ( !pSrcImage )
  330. continue;
  331. func( pDstImage, pSrcImage );
  332. }
  333. }
  334. }
  335. }
  336. template< typename ObjectType_t > inline void CDmeTexture::ProcessTexture( CDmeTexture *pSrcTexture, ObjectType_t *pObject, void (ObjectType_t::*pMemberFunc)( CDmeImage *, CDmeImage * ) )
  337. {
  338. CImageProcessorMemberFunctor< ObjectType_t > functor( pObject, pMemberFunc );
  339. ProcessTexture( pSrcTexture, functor );
  340. }
  341. #endif // DMETEXTURE_H