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.

394 lines
9.8 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "materialobjects/dmetexture.h"
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "materialsystem/IMaterial.h"
  9. #include "materialsystem/IMaterialSystem.h"
  10. //-----------------------------------------------------------------------------
  11. // Expose this class to the scene database
  12. //-----------------------------------------------------------------------------
  13. IMPLEMENT_ELEMENT_FACTORY( DmeTextureFrame, CDmeTextureFrame );
  14. //-----------------------------------------------------------------------------
  15. // Constructor, destructor
  16. //-----------------------------------------------------------------------------
  17. void CDmeTextureFrame::OnConstruction()
  18. {
  19. m_MipLevels.Init( this, "mipLevels" );
  20. }
  21. void CDmeTextureFrame::OnDestruction()
  22. {
  23. }
  24. //-----------------------------------------------------------------------------
  25. // List manipulation
  26. //-----------------------------------------------------------------------------
  27. int CDmeTextureFrame::MipLevelCount() const
  28. {
  29. return m_MipLevels.Count();
  30. }
  31. CDmeImageArray *CDmeTextureFrame::GetMipLevel( int nIndex ) const
  32. {
  33. return m_MipLevels[ nIndex ];
  34. }
  35. void CDmeTextureFrame::AddMipLevel( CDmeImageArray *pImages )
  36. {
  37. m_MipLevels.AddToTail( pImages );
  38. }
  39. CDmeImageArray *CDmeTextureFrame::AddMipLevel( )
  40. {
  41. CDmeImageArray *pImages = CreateElement< CDmeImageArray >( "mip", GetFileId() );
  42. AddMipLevel( pImages );
  43. return pImages;
  44. }
  45. int CDmeTextureFrame::ImageCount() const
  46. {
  47. int nImageCount = 0;
  48. int nMipCount = MipLevelCount();
  49. for ( int m = 0; m < nMipCount; ++m )
  50. {
  51. CDmeImageArray *pMipLevel = GetMipLevel( m );
  52. nImageCount = MAX( nImageCount, pMipLevel->ImageCount() );
  53. }
  54. return nImageCount;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Expose this class to the scene database
  58. //-----------------------------------------------------------------------------
  59. IMPLEMENT_ELEMENT_FACTORY( DmeTexture, CDmeTexture );
  60. //-----------------------------------------------------------------------------
  61. // Constructor, destructor
  62. //-----------------------------------------------------------------------------
  63. void CDmeTexture::OnConstruction()
  64. {
  65. // m_pVTFTexture = CreateVTFTexture();
  66. m_bClampS.Init( this, "clampS" );
  67. m_bClampT.Init( this, "clampT" );
  68. m_bClampU.Init( this, "clampU" );
  69. m_bNoDebugOverride.Init( this, "noDebugOverride" );
  70. m_bNoLod.Init( this, "noMipmapLOD" );
  71. m_bNiceFiltered.Init( this, "niceFiltered" );
  72. m_bNormalMap.Init( this, "normalMap" );
  73. m_flBumpScale.Init( this, "bumpScale" );
  74. m_nCompressType.Init( this, "compressType" );
  75. m_nFilterType.Init( this, "filterType" );
  76. m_nMipmapType.Init( this, "mipmapType" );
  77. m_nTextureType.Init( this, "textureType" );
  78. m_Frames.Init( this, "frames" );
  79. }
  80. void CDmeTexture::OnDestruction()
  81. {
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Gets dimensions
  85. //-----------------------------------------------------------------------------
  86. int CDmeTexture::Width() const
  87. {
  88. int nFrameCount = m_Frames.Count();
  89. int nWidth = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Width() : 0;
  90. #ifdef _DEBUG
  91. for ( int f = 1; f < nFrameCount; ++f )
  92. {
  93. CDmeTextureFrame *pFrame = m_Frames[f];
  94. Assert( pFrame->GetMipLevel( 0 )->Width() == nWidth );
  95. }
  96. #endif
  97. return nWidth;
  98. }
  99. int CDmeTexture::Height() const
  100. {
  101. int nFrameCount = m_Frames.Count();
  102. int nHeight = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Height() : 0;
  103. #ifdef _DEBUG
  104. for ( int f = 1; f < nFrameCount; ++f )
  105. {
  106. CDmeTextureFrame *pFrame = m_Frames[f];
  107. Assert( pFrame->GetMipLevel( 0 )->Height() == nHeight );
  108. }
  109. #endif
  110. return nHeight;
  111. }
  112. int CDmeTexture::Depth() const
  113. {
  114. int nFrameCount = m_Frames.Count();
  115. int nDepth = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Depth() : 0;
  116. #ifdef _DEBUG
  117. for ( int f = 1; f < nFrameCount; ++f )
  118. {
  119. CDmeTextureFrame *pFrame = m_Frames[f];
  120. Assert( pFrame->GetMipLevel( 0 )->Depth() == nDepth );
  121. }
  122. #endif
  123. return nDepth;
  124. }
  125. ImageFormat CDmeTexture::Format() const
  126. {
  127. int nFrameCount = m_Frames.Count();
  128. ImageFormat nFormat = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Format() : IMAGE_FORMAT_UNKNOWN;
  129. #ifdef _DEBUG
  130. for ( int f = 0; f < nFrameCount; ++f )
  131. {
  132. CDmeTextureFrame *pFrame = m_Frames[f];
  133. int nMipCount = pFrame->MipLevelCount();
  134. for ( int m = 0; m < nMipCount; ++m )
  135. {
  136. CDmeImageArray *pMipLevel = pFrame->GetMipLevel( m );
  137. Assert( pMipLevel->Format() == nFormat );
  138. }
  139. }
  140. #endif
  141. return nFormat;
  142. }
  143. int CDmeTexture::MipLevelCount() const
  144. {
  145. int nFrameCount = m_Frames.Count();
  146. int nMipCount = nFrameCount ? m_Frames[0]->MipLevelCount( ) : 0;
  147. #ifdef _DEBUG
  148. for ( int f = 0; f < nFrameCount; ++f )
  149. {
  150. CDmeTextureFrame *pFrame = m_Frames[f];
  151. Assert( nMipCount == pFrame->MipLevelCount() );
  152. }
  153. #endif
  154. return nMipCount;
  155. }
  156. int CDmeTexture::ImageCount() const
  157. {
  158. int nImageCount = 0;
  159. int nFrameCount = m_Frames.Count();
  160. for ( int f = 0; f < nFrameCount; ++f )
  161. {
  162. CDmeTextureFrame *pFrame = m_Frames[f];
  163. nImageCount = MAX( nImageCount, pFrame->ImageCount() );
  164. }
  165. return nImageCount;
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Computes texture flags
  169. //-----------------------------------------------------------------------------
  170. int CDmeTexture::CalcTextureFlags( int nDepth ) const
  171. {
  172. int nFlags = 0;
  173. if ( m_bClampS )
  174. {
  175. nFlags |= TEXTUREFLAGS_CLAMPS;
  176. }
  177. if ( m_bClampT )
  178. {
  179. nFlags |= TEXTUREFLAGS_CLAMPT;
  180. }
  181. if ( m_bClampU )
  182. {
  183. nFlags |= TEXTUREFLAGS_CLAMPU;
  184. }
  185. if ( m_bNoLod )
  186. {
  187. nFlags |= TEXTUREFLAGS_NOLOD;
  188. }
  189. if ( m_bNormalMap )
  190. {
  191. nFlags |= TEXTUREFLAGS_NORMAL;
  192. }
  193. if ( m_bNormalMap )
  194. {
  195. nFlags |= TEXTUREFLAGS_NORMAL;
  196. }
  197. if ( m_bNoDebugOverride )
  198. {
  199. nFlags |= TEXTUREFLAGS_NODEBUGOVERRIDE;
  200. }
  201. switch ( m_nCompressType )
  202. {
  203. case DMETEXTURE_COMPRESS_DEFAULT:
  204. case DMETEXTURE_COMPRESS_DXT1:
  205. break;
  206. case DMETEXTURE_COMPRESS_DXT5:
  207. nFlags |= TEXTUREFLAGS_HINT_DXT5;
  208. break;
  209. }
  210. switch ( m_nFilterType )
  211. {
  212. case DMETEXTURE_FILTER_DEFAULT:
  213. case DMETEXTURE_FILTER_BILINEAR:
  214. break;
  215. case DMETEXTURE_FILTER_ANISOTROPIC:
  216. nFlags |= TEXTUREFLAGS_ANISOTROPIC;
  217. break;
  218. case DMETEXTURE_FILTER_TRILINEAR:
  219. nFlags |= TEXTUREFLAGS_TRILINEAR;
  220. break;
  221. case DMETEXTURE_FILTER_POINT:
  222. nFlags |= TEXTUREFLAGS_POINTSAMPLE;
  223. break;
  224. }
  225. switch ( m_nMipmapType )
  226. {
  227. case DMETEXTURE_MIPMAP_DEFAULT:
  228. case DMETEXTURE_MIPMAP_ALL_LEVELS:
  229. break;
  230. case DMETEXTURE_MIPMAP_NONE:
  231. nFlags |= TEXTUREFLAGS_NOMIP;
  232. break;
  233. }
  234. if ( nDepth > 1 )
  235. {
  236. // FIXME: Volume textures don't currently support DXT compression
  237. nFlags &= ~TEXTUREFLAGS_HINT_DXT5;
  238. }
  239. return nFlags;
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Computes the desired texture format based on flags
  243. //-----------------------------------------------------------------------------
  244. ImageFormat CDmeTexture::ComputeDesiredImageFormat( ImageFormat srcFormat, int nWidth, int nHeight, int nDepth, int nFlags )
  245. {
  246. // HDRFIXME: Need to figure out what format to use here.
  247. if ( srcFormat == IMAGE_FORMAT_RGB323232F )
  248. return IMAGE_FORMAT_RGBA16161616F;
  249. /*
  250. if( bDUDVTarget)
  251. {
  252. if ( bCopyAlphaToLuminance && ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) ) )
  253. return IMAGE_FORMAT_UVLX8888;
  254. return IMAGE_FORMAT_UV88;
  255. }
  256. */
  257. // can't compress textures that are smaller than 4x4
  258. if ( (nFlags & TEXTUREFLAGS_PROCEDURAL) ||
  259. ( nWidth < 4 ) || ( nHeight < 4 ) || ( nDepth > 1 ) )
  260. {
  261. if ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) )
  262. return IMAGE_FORMAT_BGRA8888;
  263. return IMAGE_FORMAT_BGR888;
  264. }
  265. if( nFlags & TEXTUREFLAGS_HINT_DXT5 )
  266. return IMAGE_FORMAT_DXT5;
  267. // compressed with alpha blending
  268. if ( nFlags & TEXTUREFLAGS_EIGHTBITALPHA )
  269. return IMAGE_FORMAT_DXT5;
  270. if ( nFlags & TEXTUREFLAGS_ONEBITALPHA )
  271. return IMAGE_FORMAT_DXT5; // IMAGE_FORMAT_DXT1_ONEBITALPHA
  272. return IMAGE_FORMAT_DXT1;
  273. }
  274. //-----------------------------------------------------------------------------
  275. // Adds a frame
  276. //-----------------------------------------------------------------------------
  277. CDmeTextureFrame *CDmeTexture::AddFrame()
  278. {
  279. CDmeTextureFrame *pImageArray = CreateElement< CDmeTextureFrame >( "frame", GetFileId() );
  280. m_Frames.AddToTail( pImageArray );
  281. return pImageArray;
  282. }
  283. void CDmeTexture::RemoveAllFrames()
  284. {
  285. m_Frames.RemoveAll();
  286. }
  287. //-----------------------------------------------------------------------------
  288. // Returns all images associated with a particular frame + face (mip count amount)
  289. //-----------------------------------------------------------------------------
  290. void CDmeTexture::GetImages( int nFrame, int nImageIndex, CDmeImage **ppImages, int nSize )
  291. {
  292. memset( ppImages, 0, nSize * sizeof(CDmeImage*) );
  293. int nFrameCount = FrameCount();
  294. if ( nFrame >= nFrameCount )
  295. return;
  296. CDmeTextureFrame *pFrame = GetFrame( nFrame );
  297. if ( !pFrame )
  298. return;
  299. int nMipCount = MIN( pFrame->MipLevelCount(), nSize );
  300. for ( int m = 0; m < nMipCount; ++m )
  301. {
  302. CDmeImageArray *pImageArray = pFrame->GetMipLevel( m );
  303. if ( !pImageArray )
  304. continue;
  305. int nImageCount = pImageArray->ImageCount();
  306. if ( nImageIndex >= nImageCount )
  307. continue;
  308. ppImages[m] = pImageArray->GetImage( nImageIndex );
  309. }
  310. }
  311. void CDmeTexture::CompressTexture( CDmeTexture *pSrcTexture, ImageFormat fmt )
  312. {
  313. class CCompressionFunctor
  314. {
  315. public:
  316. CCompressionFunctor( ImageFormat fmt ) { m_Format = fmt; }
  317. void operator()( CDmeImage *pDstImage, CDmeImage *pSrcImage )
  318. {
  319. pDstImage->CompressImage( pSrcImage, m_Format );
  320. }
  321. ImageFormat m_Format;
  322. };
  323. CCompressionFunctor compress( fmt );
  324. ProcessTexture( pSrcTexture, compress );
  325. }
  326. //-----------------------------------------------------------------------------
  327. // resolve
  328. //-----------------------------------------------------------------------------
  329. void CDmeTexture::Resolve()
  330. {
  331. }