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.

445 lines
13 KiB

  1. //===== Copyright � 1996-2005, 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 Serialize( CUtlBuffer &buf );
  125. // Attributes...
  126. virtual int Width() const;
  127. virtual int Height() const;
  128. virtual int Depth() const;
  129. virtual int MipCount() const;
  130. virtual int RowSizeInBytes( int nMipLevel ) const;
  131. virtual int FaceSizeInBytes( int nMipLevel ) const;
  132. virtual ImageFormat Format() const;
  133. virtual int FaceCount() const;
  134. virtual int FrameCount() const;
  135. virtual int Flags() const;
  136. virtual float BumpScale() const;
  137. virtual const Vector &Reflectivity() const;
  138. virtual bool IsCubeMap() const;
  139. virtual bool IsNormalMap() const;
  140. virtual bool IsVolumeTexture() const;
  141. virtual int LowResWidth() const;
  142. virtual int LowResHeight() const;
  143. virtual ImageFormat LowResFormat() const;
  144. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  145. virtual int ComputeMipSize( int iMipLevel ) const;
  146. // Computes the size (in bytes) of a single face of a single frame
  147. // All mip levels starting at the specified mip level are included
  148. virtual int ComputeFaceSize( int iStartingMipLevel = 0 ) const;
  149. // Computes the total size of all faces, all frames
  150. virtual int ComputeTotalSize( ) const;
  151. // Computes the dimensions of a particular mip level
  152. virtual void ComputeMipLevelDimensions( int iMipLevel, int *pWidth, int *pHeight, int *pMipDepth ) const;
  153. // Computes the size of a subrect (specified at the top mip level) at a particular lower mip level
  154. virtual void ComputeMipLevelSubRect( Rect_t* pSrcRect, int nMipLevel, Rect_t *pSubRect ) const;
  155. // Returns the base address of the image data
  156. virtual unsigned char *ImageData();
  157. // Returns a pointer to the data associated with a particular frame, face, and mip level
  158. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel );
  159. // Returns a pointer to the data associated with a particular frame, face, mip level, and offset
  160. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel, int x, int y, int z );
  161. // Returns the base address of the low-res image data
  162. virtual unsigned char *LowResImageData();
  163. // Converts the texture's image format. Use IMAGE_FORMAT_DEFAULT
  164. virtual void ConvertImageFormat( ImageFormat fmt, bool bNormalToDUDV, bool bNormalToDXT5GA );
  165. // Generate spheremap based on the current cube faces (only works for cubemaps)
  166. // The look dir indicates the direction of the center of the sphere
  167. virtual void GenerateSpheremap( LookDir_t lookDir );
  168. virtual void GenerateHemisphereMap( unsigned char *pSphereMapBitsRGBA, int targetWidth,
  169. int targetHeight, LookDir_t lookDir, int iFrame );
  170. // Fixes the cubemap faces orientation from our standard to the
  171. // standard the material system needs.
  172. virtual void FixCubemapFaceOrientation( );
  173. // Normalize the top mip level if necessary
  174. virtual void NormalizeTopMipLevel();
  175. // Generates mipmaps from the base mip levels
  176. virtual void GenerateMipmaps();
  177. // Put 1/miplevel (1..n) into alpha.
  178. virtual void PutOneOverMipLevelInAlpha();
  179. // Scale alpha by miplevel/ mipcount
  180. virtual void PremultAlphaWithMipFraction();
  181. // Computes the reflectivity
  182. virtual void ComputeReflectivity( );
  183. // Computes the alpha flags
  184. virtual void ComputeAlphaFlags();
  185. virtual void Compute2DGradient();
  186. // Gets the texture all internally consistent assuming you've loaded
  187. // mip 0 of all faces of all frames
  188. virtual void PostProcess(bool bGenerateSpheremap, LookDir_t lookDir = LOOK_DOWN_Z, bool bAllowFixCubemapOrientation = true, bool bLoadedMiplevels = false);
  189. virtual void SetPostProcessingSettings( VtfProcessingOptions const *pOptions );
  190. // Generate the low-res image bits
  191. virtual bool ConstructLowResImage();
  192. virtual void MatchCubeMapBorders( int iStage, ImageFormat finalFormat, bool bSkybox );
  193. // Sets threshhold values for alphatest mipmapping
  194. virtual void SetAlphaTestThreshholds( float flBase, float flHighFreq );
  195. virtual bool IsPreTiled() const;
  196. #if defined( _GAMECONSOLE )
  197. virtual int UpdateOrCreate( const char *pFilename, const char *pPathID = NULL, bool bForce = false );
  198. virtual int FileSize( bool bPreloadOnly, int nMipSkipCount ) const;
  199. virtual bool UnserializeFromBuffer( CUtlBuffer &buf, bool bBufferIsVolatile, bool bHeaderOnly, bool bPreloadOnly, int nMipSkipCount );
  200. virtual int MappingWidth() const;
  201. virtual int MappingHeight() const;
  202. virtual int MappingDepth() const;
  203. virtual int MipSkipCount() const;
  204. virtual unsigned char *LowResImageSample();
  205. virtual void ReleaseImageMemory();
  206. #endif
  207. private:
  208. // Unserialization
  209. bool ReadHeader( CUtlBuffer &buf, VTFFileHeader_t &header );
  210. void BlendCubeMapEdgePalettes(
  211. int iFrame,
  212. int iMipLevel,
  213. const CEdgeMatch *pMatch );
  214. void BlendCubeMapCornerPalettes(
  215. int iFrame,
  216. int iMipLevel,
  217. const CCornerMatch *pMatch );
  218. void MatchCubeMapS3TCPalettes(
  219. CEdgeMatch edgeMatches[NUM_EDGE_MATCHES],
  220. CCornerMatch cornerMatches[NUM_CORNER_MATCHES]
  221. );
  222. void SetupFaceVert( int iMipLevel, int iVert, CEdgePos &out );
  223. void SetupEdgeIncrement( CEdgePos &start, CEdgePos &end, CEdgePos &inc );
  224. void SetupTextureEdgeIncrements(
  225. int iMipLevel,
  226. int iFace1Edge,
  227. int iFace2Edge,
  228. bool bFlipFace2Edge,
  229. CEdgeIncrements *incs );
  230. void BlendCubeMapFaceEdges(
  231. int iFrame,
  232. int iMipLevel,
  233. const CEdgeMatch *pMatch );
  234. void BlendCubeMapFaceCorners(
  235. int iFrame,
  236. int iMipLevel,
  237. const CCornerMatch *pMatch );
  238. void BuildCubeMapMatchLists( CEdgeMatch edgeMatches[NUM_EDGE_MATCHES], CCornerMatch cornerMatches[NUM_CORNER_MATCHES], bool bSkybox );
  239. // Allocate image data blocks with an eye toward re-using memory
  240. bool AllocateImageData( int nMemorySize );
  241. bool AllocateLowResImageData( int nMemorySize );
  242. // Compute the mip count based on the size + flags
  243. int ComputeMipCount( ) const;
  244. // Unserialization of low-res data
  245. bool LoadLowResData( CUtlBuffer &buf );
  246. // Unserialization of new resource data
  247. bool LoadNewResources( CUtlBuffer &buf );
  248. // Unserialization of image data
  249. bool LoadImageData( CUtlBuffer &buf, const VTFFileHeader_t &header, int nSkipMipLevels );
  250. // Shutdown
  251. void Shutdown();
  252. void ReleaseResources();
  253. // Makes a single frame of spheremap
  254. void ComputeSpheremapFrame( unsigned char **ppCubeFaces, unsigned char *pSpheremap, LookDir_t lookDir );
  255. // Makes a single frame of spheremap
  256. void ComputeHemispheremapFrame( unsigned char **ppCubeFaces, unsigned char *pSpheremap, LookDir_t lookDir );
  257. // Serialization of image data
  258. bool WriteImageData( CUtlBuffer &buf );
  259. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  260. int ComputeMipSize( int iMipLevel, ImageFormat fmt ) const;
  261. // Computes the size (in bytes) of a single face of a single frame
  262. // All mip levels starting at the specified mip level are included
  263. int ComputeFaceSize( int iStartingMipLevel, ImageFormat fmt ) const;
  264. // Computes the total size of all faces, all frames
  265. int ComputeTotalSize( ImageFormat fmt ) const;
  266. // Computes the location of a particular face, frame, and mip level
  267. int GetImageOffset( int iFrame, int iFace, int iMipLevel, ImageFormat fmt ) const;
  268. // Determines if the vtf or vtfx file needs to be swapped to the current platform
  269. bool SetupByteSwap( CUtlBuffer &buf );
  270. // Locates the resource entry info if it's present
  271. ResourceEntryInfo *FindResourceEntryInfo( unsigned int eType );
  272. ResourceEntryInfo const *FindResourceEntryInfo( unsigned int eType ) const;
  273. // Inserts the resource entry info if it's not present
  274. ResourceEntryInfo *FindOrCreateResourceEntryInfo( unsigned int eType );
  275. // Removes the resource entry info if it's present
  276. bool RemoveResourceEntryInfo( unsigned int eType );
  277. #if defined( _X360 )
  278. bool ReadHeader( CUtlBuffer &buf, VTFFileHeaderX360_t &header );
  279. bool LoadImageData( CUtlBuffer &buf, bool bBufferIsVolatile, int nMipSkipCount );
  280. #elif defined ( _PS3 )
  281. bool ReadHeader( CUtlBuffer &buf, VTFFileHeaderPS3_t &header );
  282. bool LoadImageData( CUtlBuffer &buf, bool bBufferIsVolatile, int nMipSkipCount );
  283. int GetImageOffset() const;
  284. #endif
  285. private:
  286. // This is to make sure old-format .vtf files are read properly
  287. int m_nVersion[2];
  288. int m_nWidth;
  289. int m_nHeight;
  290. int m_nDepth;
  291. ImageFormat m_Format;
  292. int m_nMipCount;
  293. int m_nFaceCount;
  294. int m_nFrameCount;
  295. int m_nImageAllocSize;
  296. int m_nFlags;
  297. unsigned char *m_pImageData;
  298. Vector m_vecReflectivity;
  299. float m_flBumpScale;
  300. // FIXME: Do I need this?
  301. int m_iStartFrame;
  302. // Low res data
  303. int m_nLowResImageAllocSize;
  304. ImageFormat m_LowResImageFormat;
  305. int m_nLowResImageWidth;
  306. int m_nLowResImageHeight;
  307. unsigned char *m_pLowResImageData;
  308. // Used while fixing mipmap edges.
  309. CUtlVector<S3RGBA> m_OriginalData;
  310. // Alpha threshholds
  311. float m_flAlphaThreshhold;
  312. float m_flAlphaHiFreqThreshhold;
  313. CByteswap m_Swap;
  314. #if defined( _X360 ) || defined ( _PS3 )
  315. int m_iPreloadDataSize;
  316. int m_iCompressedSize;
  317. // resolves actual dimensions to/from mapping dimensions due to pre-picmipping
  318. int m_nMipSkipCount;
  319. unsigned char m_LowResImageSample[4];
  320. #endif
  321. CUtlVector< ResourceEntryInfo > m_arrResourcesInfo;
  322. struct ResourceMemorySection
  323. {
  324. ResourceMemorySection() { memset( this, 0, sizeof( *this ) ); }
  325. int m_nDataAllocSize;
  326. int m_nDataLength;
  327. unsigned char *m_pData;
  328. bool AllocateData( int nMemorySize );
  329. bool LoadData( CUtlBuffer &buf, CByteswap &byteSwap );
  330. bool WriteData( CUtlBuffer &buf ) const;
  331. };
  332. CUtlVector< ResourceMemorySection > m_arrResourcesData;
  333. CUtlVector< ResourceMemorySection > m_arrResourcesData_ForReuse; // Maintained to keep allocated memory blocks when unserializing from files
  334. VtfProcessingOptions m_Options;
  335. };
  336. #endif // CVTF_H