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.

441 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Local header for CVTFTexture class declaration - allows platform-specific
  4. // implementation to be placed in separate cpp files.
  5. //
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef CVTF_H
  9. #define CVTF_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "s3tc_decode.h"
  14. #include "vtf/vtf.h"
  15. #include "byteswap.h"
  16. #include "filesystem.h"
  17. class CEdgePos
  18. {
  19. public:
  20. CEdgePos() {}
  21. CEdgePos( int ix, int iy )
  22. {
  23. x = ix;
  24. y = iy;
  25. }
  26. void operator +=( const CEdgePos &other )
  27. {
  28. x += other.x;
  29. y += other.y;
  30. }
  31. void operator /=( int val )
  32. {
  33. x /= val;
  34. y /= val;
  35. }
  36. CEdgePos operator >>( int shift )
  37. {
  38. return CEdgePos( x >> shift, y >> shift );
  39. }
  40. CEdgePos operator *( int shift )
  41. {
  42. return CEdgePos( x * shift, y * shift );
  43. }
  44. CEdgePos operator -( const CEdgePos &other )
  45. {
  46. return CEdgePos( x - other.x, y - other.y );
  47. }
  48. CEdgePos operator +( const CEdgePos &other )
  49. {
  50. return CEdgePos( x + other.x, y + other.y );
  51. }
  52. bool operator!=( const CEdgePos &other )
  53. {
  54. return !( *this == other );
  55. }
  56. bool operator==( const CEdgePos &other )
  57. {
  58. return x==other.x && y==other.y;
  59. }
  60. int x, y;
  61. };
  62. class CEdgeIncrements
  63. {
  64. public:
  65. CEdgePos iFace1Start, iFace1End;
  66. CEdgePos iFace1Inc, iFace2Inc;
  67. CEdgePos iFace2Start, iFace2End;
  68. };
  69. class CEdgeMatch
  70. {
  71. public:
  72. int m_iFaces[2]; // Which faces are touching.
  73. int m_iEdges[2]; // Which edge on each face is touching.
  74. int m_iCubeVerts[2];// Which of the cube's verts comprise this edge?
  75. bool m_bFlipFace2Edge;
  76. };
  77. class CCornerMatch
  78. {
  79. public:
  80. // The info for the 3 edges that match at this corner.
  81. int m_iFaces[3];
  82. int m_iFaceEdges[3];
  83. };
  84. class CEdgeFaceIndex
  85. {
  86. public:
  87. int m_iEdge;
  88. int m_iFace;
  89. };
  90. #define NUM_EDGE_MATCHES 12
  91. #define NUM_CORNER_MATCHES 8
  92. //-----------------------------------------------------------------------------
  93. // Implementation of the VTF Texture
  94. //-----------------------------------------------------------------------------
  95. class CVTFTexture : public IVTFTexture
  96. {
  97. public:
  98. CVTFTexture();
  99. virtual ~CVTFTexture();
  100. virtual bool Init( int nWidth, int nHeight, int nDepth, ImageFormat fmt, int iFlags, int iFrameCount, int nForceMipCount );
  101. // Methods to initialize the low-res image
  102. virtual void InitLowResImage( int nWidth, int nHeight, ImageFormat fmt );
  103. virtual void *SetResourceData( uint32 eType, void const *pData, size_t nDataSize );
  104. virtual void *GetResourceData( uint32 eType, size_t *pDataSize ) const;
  105. // Locates the resource entry info if it's present, easier than crawling array types
  106. virtual bool HasResourceEntry( uint32 eType ) const;
  107. // Retrieve available resource types of this IVTFTextures
  108. // arrTypesBuffer buffer to be filled with resource types available.
  109. // numTypesBufferElems how many resource types the buffer can accomodate.
  110. // Returns:
  111. // number of resource types available (can be greater than "numTypesBufferElems"
  112. // in which case only first "numTypesBufferElems" are copied to "arrTypesBuffer")
  113. virtual unsigned int GetResourceTypes( uint32 *arrTypesBuffer, int numTypesBufferElems ) const;
  114. // Methods to set other texture fields
  115. virtual void SetBumpScale( float flScale );
  116. virtual void SetReflectivity( const Vector &vecReflectivity );
  117. // These are methods to help with optimization of file access
  118. virtual void LowResFileInfo( int *pStartLocation, int *pSizeInBytes ) const;
  119. virtual void ImageFileInfo( int nFrame, int nFace, int nMip, int *pStartLocation, int *pSizeInBytes) const;
  120. virtual int FileSize( int nMipSkipCount = 0 ) const;
  121. // When unserializing, we can skip a certain number of mip levels,
  122. // and we also can just load everything but the image data
  123. virtual bool Unserialize( CUtlBuffer &buf, bool bBufferHeaderOnly = false, int nSkipMipLevels = 0 );
  124. virtual bool UnserializeEx( CUtlBuffer &buf, bool bHeaderOnly = false, int nForceFlags = 0, int nSkipMipLevels = 0 );
  125. virtual bool Serialize( CUtlBuffer &buf );
  126. virtual void GetMipmapRange( int* pOutFinest, int* pOutCoarsest );
  127. // Attributes...
  128. virtual int Width() const;
  129. virtual int Height() const;
  130. virtual int Depth() const;
  131. virtual int MipCount() const;
  132. virtual int RowSizeInBytes( int nMipLevel ) const;
  133. virtual int FaceSizeInBytes( int nMipLevel ) const;
  134. virtual ImageFormat Format() const;
  135. virtual int FaceCount() const;
  136. virtual int FrameCount() const;
  137. virtual int Flags() const;
  138. virtual float BumpScale() const;
  139. virtual const Vector &Reflectivity() const;
  140. virtual bool IsCubeMap() const;
  141. virtual bool IsNormalMap() const;
  142. virtual bool IsVolumeTexture() const;
  143. virtual int LowResWidth() const;
  144. virtual int LowResHeight() const;
  145. virtual ImageFormat LowResFormat() const;
  146. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  147. virtual int ComputeMipSize( int iMipLevel ) const;
  148. // Computes the size (in bytes) of a single face of a single frame
  149. // All mip levels starting at the specified mip level are included
  150. virtual int ComputeFaceSize( int iStartingMipLevel = 0 ) const;
  151. // Computes the total size of all faces, all frames
  152. virtual int ComputeTotalSize( ) const;
  153. // Computes the dimensions of a particular mip level
  154. virtual void ComputeMipLevelDimensions( int iMipLevel, int *pWidth, int *pHeight, int *pMipDepth ) const;
  155. // Computes the size of a subrect (specified at the top mip level) at a particular lower mip level
  156. virtual void ComputeMipLevelSubRect( Rect_t* pSrcRect, int nMipLevel, Rect_t *pSubRect ) const;
  157. // Returns the base address of the image data
  158. virtual unsigned char *ImageData();
  159. // Returns a pointer to the data associated with a particular frame, face, and mip level
  160. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel );
  161. // Returns a pointer to the data associated with a particular frame, face, mip level, and offset
  162. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel, int x, int y, int z );
  163. // Returns the base address of the low-res image data
  164. virtual unsigned char *LowResImageData();
  165. // Converts the texture's image format. Use IMAGE_FORMAT_DEFAULT
  166. virtual void ConvertImageFormat( ImageFormat fmt, bool bNormalToDUDV );
  167. // Generate spheremap based on the current cube faces (only works for cubemaps)
  168. // The look dir indicates the direction of the center of the sphere
  169. virtual void GenerateSpheremap( LookDir_t lookDir );
  170. virtual void GenerateHemisphereMap( unsigned char *pSphereMapBitsRGBA, int targetWidth,
  171. int targetHeight, LookDir_t lookDir, int iFrame );
  172. // Fixes the cubemap faces orientation from our standard to the
  173. // standard the material system needs.
  174. virtual void FixCubemapFaceOrientation( );
  175. // Normalize the top mip level if necessary
  176. virtual void NormalizeTopMipLevel();
  177. // Generates mipmaps from the base mip levels
  178. virtual void GenerateMipmaps();
  179. // Put 1/miplevel (1..n) into alpha.
  180. virtual void PutOneOverMipLevelInAlpha();
  181. // Computes the reflectivity
  182. virtual void ComputeReflectivity( );
  183. // Computes the alpha flags
  184. virtual void ComputeAlphaFlags();
  185. // Gets the texture all internally consistent assuming you've loaded
  186. // mip 0 of all faces of all frames
  187. virtual void PostProcess(bool bGenerateSpheremap, LookDir_t lookDir = LOOK_DOWN_Z, bool bAllowFixCubemapOrientation = true);
  188. virtual void SetPostProcessingSettings( VtfProcessingOptions const *pOptions );
  189. // Generate the low-res image bits
  190. virtual bool ConstructLowResImage();
  191. virtual void MatchCubeMapBorders( int iStage, ImageFormat finalFormat, bool bSkybox );
  192. // Sets threshhold values for alphatest mipmapping
  193. virtual void SetAlphaTestThreshholds( float flBase, float flHighFreq );
  194. #if defined( _X360 )
  195. virtual int UpdateOrCreate( const char *pFilename, const char *pPathID = NULL, bool bForce = false );
  196. virtual int FileSize( bool bPreloadOnly, int nMipSkipCount ) const;
  197. virtual bool UnserializeFromBuffer( CUtlBuffer &buf, bool bBufferIsVolatile, bool bHeaderOnly, bool bPreloadOnly, int nMipSkipCount );
  198. virtual bool IsPreTiled() const;
  199. virtual int MappingWidth() const;
  200. virtual int MappingHeight() const;
  201. virtual int MappingDepth() const;
  202. virtual int MipSkipCount() const;
  203. virtual unsigned char *LowResImageSample();
  204. virtual void ReleaseImageMemory();
  205. #endif
  206. private:
  207. // Unserialization
  208. bool ReadHeader( CUtlBuffer &buf, VTFFileHeader_t &header );
  209. void BlendCubeMapEdgePalettes(
  210. int iFrame,
  211. int iMipLevel,
  212. const CEdgeMatch *pMatch );
  213. void BlendCubeMapCornerPalettes(
  214. int iFrame,
  215. int iMipLevel,
  216. const CCornerMatch *pMatch );
  217. void MatchCubeMapS3TCPalettes(
  218. CEdgeMatch edgeMatches[NUM_EDGE_MATCHES],
  219. CCornerMatch cornerMatches[NUM_CORNER_MATCHES]
  220. );
  221. void SetupFaceVert( int iMipLevel, int iVert, CEdgePos &out );
  222. void SetupEdgeIncrement( CEdgePos &start, CEdgePos &end, CEdgePos &inc );
  223. void SetupTextureEdgeIncrements(
  224. int iMipLevel,
  225. int iFace1Edge,
  226. int iFace2Edge,
  227. bool bFlipFace2Edge,
  228. CEdgeIncrements *incs );
  229. void BlendCubeMapFaceEdges(
  230. int iFrame,
  231. int iMipLevel,
  232. const CEdgeMatch *pMatch );
  233. void BlendCubeMapFaceCorners(
  234. int iFrame,
  235. int iMipLevel,
  236. const CCornerMatch *pMatch );
  237. void BuildCubeMapMatchLists( CEdgeMatch edgeMatches[NUM_EDGE_MATCHES], CCornerMatch cornerMatches[NUM_CORNER_MATCHES], bool bSkybox );
  238. // Allocate image data blocks with an eye toward re-using memory
  239. bool AllocateImageData( int nMemorySize );
  240. bool AllocateLowResImageData( int nMemorySize );
  241. // Compute the mip count based on the size + flags
  242. int ComputeMipCount( ) const;
  243. // Unserialization of low-res data
  244. bool LoadLowResData( CUtlBuffer &buf );
  245. // Unserialization of new resource data
  246. bool LoadNewResources( CUtlBuffer &buf );
  247. // Unserialization of image data
  248. bool LoadImageData( CUtlBuffer &buf, const VTFFileHeader_t &header, int nSkipMipLevels );
  249. // Shutdown
  250. void Shutdown();
  251. void ReleaseResources();
  252. // Makes a single frame of spheremap
  253. void ComputeSpheremapFrame( unsigned char **ppCubeFaces, unsigned char *pSpheremap, LookDir_t lookDir );
  254. // Makes a single frame of spheremap
  255. void ComputeHemispheremapFrame( unsigned char **ppCubeFaces, unsigned char *pSpheremap, LookDir_t lookDir );
  256. // Serialization of image data
  257. bool WriteImageData( CUtlBuffer &buf );
  258. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  259. int ComputeMipSize( int iMipLevel, ImageFormat fmt ) const;
  260. // Computes the size (in bytes) of a single face of a single frame
  261. // All mip levels starting at the specified mip level are included
  262. int ComputeFaceSize( int iStartingMipLevel, ImageFormat fmt ) const;
  263. // Computes the total size of all faces, all frames
  264. int ComputeTotalSize( ImageFormat fmt ) const;
  265. // Computes the location of a particular face, frame, and mip level
  266. int GetImageOffset( int iFrame, int iFace, int iMipLevel, ImageFormat fmt ) const;
  267. // Determines if the vtf or vtfx file needs to be swapped to the current platform
  268. bool SetupByteSwap( CUtlBuffer &buf );
  269. // Locates the resource entry info if it's present
  270. ResourceEntryInfo *FindResourceEntryInfo( unsigned int eType );
  271. ResourceEntryInfo const *FindResourceEntryInfo( unsigned int eType ) const;
  272. // Inserts the resource entry info if it's not present
  273. ResourceEntryInfo *FindOrCreateResourceEntryInfo( unsigned int eType );
  274. // Removes the resource entry info if it's present
  275. bool RemoveResourceEntryInfo( unsigned int eType );
  276. #if defined( _X360 )
  277. bool ReadHeader( CUtlBuffer &buf, VTFFileHeaderX360_t &header );
  278. bool LoadImageData( CUtlBuffer &buf, bool bBufferIsVolatile, int nMipSkipCount );
  279. #endif
  280. private:
  281. // This is to make sure old-format .vtf files are read properly
  282. int m_nVersion[2];
  283. int m_nWidth;
  284. int m_nHeight;
  285. int m_nDepth;
  286. ImageFormat m_Format;
  287. int m_nMipCount;
  288. int m_nFaceCount;
  289. int m_nFrameCount;
  290. int m_nImageAllocSize;
  291. int m_nFlags;
  292. unsigned char *m_pImageData;
  293. Vector m_vecReflectivity;
  294. float m_flBumpScale;
  295. // FIXME: Do I need this?
  296. int m_iStartFrame;
  297. // Low res data
  298. int m_nLowResImageAllocSize;
  299. ImageFormat m_LowResImageFormat;
  300. int m_nLowResImageWidth;
  301. int m_nLowResImageHeight;
  302. unsigned char *m_pLowResImageData;
  303. // Used while fixing mipmap edges.
  304. CUtlVector<S3RGBA> m_OriginalData;
  305. // Alpha threshholds
  306. float m_flAlphaThreshhold;
  307. float m_flAlphaHiFreqThreshhold;
  308. CByteswap m_Swap;
  309. int m_nFinestMipmapLevel;
  310. int m_nCoarsestMipmapLevel;
  311. #if defined( _X360 )
  312. int m_iPreloadDataSize;
  313. int m_iCompressedSize;
  314. // resolves actual dimensions to/from mapping dimensions due to pre-picmipping
  315. int m_nMipSkipCount;
  316. unsigned char m_LowResImageSample[4];
  317. #endif
  318. CUtlVector< ResourceEntryInfo > m_arrResourcesInfo;
  319. struct ResourceMemorySection
  320. {
  321. ResourceMemorySection() { memset( this, 0, sizeof( *this ) ); }
  322. int m_nDataAllocSize;
  323. int m_nDataLength;
  324. unsigned char *m_pData;
  325. bool AllocateData( int nMemorySize );
  326. bool LoadData( CUtlBuffer &buf, CByteswap &byteSwap );
  327. bool WriteData( CUtlBuffer &buf ) const;
  328. };
  329. CUtlVector< ResourceMemorySection > m_arrResourcesData;
  330. CUtlVector< ResourceMemorySection > m_arrResourcesData_ForReuse; // Maintained to keep allocated memory blocks when unserializing from files
  331. VtfProcessingOptions m_Options;
  332. };
  333. #endif // CVTF_H