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.

502 lines
18 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef VTF_H
  8. #define VTF_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "bitmap/imageformat.h"
  13. #include "tier0/platform.h"
  14. #include "vtf/vtf_declarations.h"
  15. // #define VTF_FILE_FORMAT_ONLY to just include the vtf header and none of the code declaration
  16. #ifndef VTF_FILE_FORMAT_ONLY
  17. //-----------------------------------------------------------------------------
  18. // Interface to get at various bits of a VTF texture
  19. //-----------------------------------------------------------------------------
  20. class IVTFTexture
  21. {
  22. public:
  23. virtual ~IVTFTexture() { }
  24. // Initializes the texture and allocates space for the bits
  25. // In most cases, you shouldn't force the mip count.
  26. virtual bool Init( int nWidth, int nHeight, int nDepth, ImageFormat fmt, int nFlags, int iFrameCount, int nForceMipCount = -1 ) = 0;
  27. // Methods to set other texture fields
  28. virtual void SetBumpScale( float flScale ) = 0;
  29. virtual void SetReflectivity( const Vector &vecReflectivity ) = 0;
  30. // Methods to initialize the low-res image
  31. virtual void InitLowResImage( int nWidth, int nHeight, ImageFormat fmt ) = 0;
  32. // set the resource data (for writers). pass size=0 to delete data. if pdata is not null,
  33. // the resource data will be copied from *pData
  34. virtual void *SetResourceData( uint32 eType, void const *pData, size_t nDataSize ) = 0;
  35. // find the resource data and return a pointer to it. The data pointed to by this pointer will
  36. // go away when the ivtftexture does. retruns null if resource not present
  37. virtual void *GetResourceData( uint32 eType, size_t *pDataSize ) const = 0;
  38. // Locates the resource entry info if it's present, easier than crawling array types
  39. virtual bool HasResourceEntry( uint32 eType ) const = 0;
  40. // Retrieve available resource types of this IVTFTextures
  41. // arrTypesBuffer buffer to be filled with resource types available.
  42. // numTypesBufferElems how many resource types the buffer can accomodate.
  43. // Returns:
  44. // number of resource types available (can be greater than "numTypesBufferElems"
  45. // in which case only first "numTypesBufferElems" are copied to "arrTypesBuffer")
  46. virtual unsigned int GetResourceTypes( uint32 *arrTypesBuffer, int numTypesBufferElems ) const = 0;
  47. // When unserializing, we can skip a certain number of mip levels,
  48. // and we also can just load everything but the image data
  49. // NOTE: If you load only the buffer header, you'll need to use the
  50. // VTFBufferHeaderSize() method below to only read that much from the file
  51. // NOTE: If you skip mip levels, the height + width of the texture will
  52. // change to reflect the size of the largest read in mip level
  53. virtual bool Unserialize( CUtlBuffer &buf, bool bHeaderOnly = false, int nSkipMipLevels = 0 ) = 0;
  54. virtual bool Serialize( CUtlBuffer &buf ) = 0;
  55. // These are methods to help with optimization:
  56. // Once the header is read in, they indicate where to start reading
  57. // other data (measured from file start), and how many bytes to read....
  58. virtual void LowResFileInfo( int *pStartLocation, int *pSizeInBytes) const = 0;
  59. virtual void ImageFileInfo( int nFrame, int nFace, int nMip, int *pStartLocation, int *pSizeInBytes) const = 0;
  60. virtual int FileSize( int nMipSkipCount = 0 ) const = 0;
  61. // Attributes...
  62. virtual int Width() const = 0;
  63. virtual int Height() const = 0;
  64. virtual int Depth() const = 0;
  65. virtual int MipCount() const = 0;
  66. // returns the size of one row of a particular mip level
  67. virtual int RowSizeInBytes( int nMipLevel ) const = 0;
  68. // returns the size of one face of a particular mip level
  69. virtual int FaceSizeInBytes( int nMipLevel ) const = 0;
  70. virtual ImageFormat Format() const = 0;
  71. virtual int FaceCount() const = 0;
  72. virtual int FrameCount() const = 0;
  73. virtual int Flags() const = 0;
  74. virtual float BumpScale() const = 0;
  75. virtual int LowResWidth() const = 0;
  76. virtual int LowResHeight() const = 0;
  77. virtual ImageFormat LowResFormat() const = 0;
  78. // NOTE: reflectivity[0] = blue, [1] = greem, [2] = red
  79. virtual const Vector &Reflectivity() const = 0;
  80. virtual bool IsCubeMap() const = 0;
  81. virtual bool IsNormalMap() const = 0;
  82. virtual bool IsVolumeTexture() const = 0;
  83. // Computes the dimensions of a particular mip level
  84. virtual void ComputeMipLevelDimensions( int iMipLevel, int *pMipWidth, int *pMipHeight, int *pMipDepth ) const = 0;
  85. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  86. virtual int ComputeMipSize( int iMipLevel ) const = 0;
  87. // Computes the size of a subrect (specified at the top mip level) at a particular lower mip level
  88. virtual void ComputeMipLevelSubRect( Rect_t* pSrcRect, int nMipLevel, Rect_t *pSubRect ) const = 0;
  89. // Computes the size (in bytes) of a single face of a single frame
  90. // All mip levels starting at the specified mip level are included
  91. virtual int ComputeFaceSize( int iStartingMipLevel = 0 ) const = 0;
  92. // Computes the total size (in bytes) of all faces, all frames
  93. virtual int ComputeTotalSize() const = 0;
  94. // Returns the base address of the image data
  95. virtual uint8 *ImageData() = 0;
  96. // Returns a pointer to the data associated with a particular frame, face, and mip level
  97. virtual uint8 *ImageData( int iFrame, int iFace, int iMipLevel ) = 0;
  98. // Returns a pointer to the data associated with a particular frame, face, mip level, and offset
  99. virtual uint8 *ImageData( int iFrame, int iFace, int iMipLevel, int x, int y, int z = 0 ) = 0;
  100. // Returns the base address of the low-res image data
  101. virtual uint8 *LowResImageData() = 0;
  102. // Converts the textures image format. Use IMAGE_FORMAT_DEFAULT
  103. // if you want to be able to use various tool functions below
  104. virtual void ConvertImageFormat( ImageFormat fmt, bool bNormalToDUDV, bool bNormalToDXT5GA = false ) = 0;
  105. // NOTE: The following methods only work on textures using the
  106. // IMAGE_FORMAT_DEFAULT!
  107. // Generate spheremap based on the current cube faces (only works for cubemaps)
  108. // The look dir indicates the direction of the center of the sphere
  109. // NOTE: Only call this *after* cube faces have been correctly
  110. // oriented (using FixCubemapFaceOrientation)
  111. #if !defined (_PS3)
  112. virtual void GenerateSpheremap( LookDir_t lookDir = LOOK_DOWN_Z ) = 0;
  113. #endif
  114. // Generate spheremap based on the current cube faces (only works for cubemaps)
  115. // The look dir indicates the direction of the center of the sphere
  116. // NOTE: Only call this *after* cube faces have been correctly
  117. // oriented (using FixCubemapFaceOrientation)
  118. virtual void GenerateHemisphereMap( uint8 *pSphereMapBitsRGBA, int targetWidth,
  119. int targetHeight, LookDir_t lookDir, int iFrame ) = 0;
  120. // Fixes the cubemap faces orientation from our standard to the
  121. // standard the material system needs.
  122. virtual void FixCubemapFaceOrientation( ) = 0;
  123. // Generates mipmaps from the base mip levels
  124. virtual void GenerateMipmaps() = 0;
  125. // Put 1/miplevel (1..n) into alpha.
  126. virtual void PutOneOverMipLevelInAlpha() = 0;
  127. // Scale alpha by miplevel/mipcount
  128. virtual void PremultAlphaWithMipFraction() = 0;
  129. // Computes the reflectivity
  130. virtual void ComputeReflectivity( ) = 0;
  131. // Computes the alpha flags
  132. virtual void ComputeAlphaFlags() = 0;
  133. // Generate the low-res image bits
  134. virtual bool ConstructLowResImage() = 0;
  135. // Gets the texture all internally consistent assuming you've loaded
  136. // mip 0 of all faces of all frames
  137. virtual void PostProcess(bool bGenerateSpheremap, LookDir_t lookDir = LOOK_DOWN_Z, bool bAllowFixCubemapOrientation = true, bool bLoadedMiplevels = false) = 0;
  138. // Blends adjacent pixels on cubemap borders, since the card doesn't do it. If the texture
  139. // is S3TC compressed, then it has to do it AFTER the texture has been compressed to prevent
  140. // artifacts along the edges.
  141. //
  142. // If bSkybox is true, it assumes the faces are oriented in the way the engine draws the skybox
  143. // (which happens to be different from the way cubemaps have their faces).
  144. virtual void MatchCubeMapBorders( int iStage, ImageFormat finalFormat, bool bSkybox ) = 0;
  145. // Sets threshhold values for alphatest mipmapping
  146. virtual void SetAlphaTestThreshholds( float flBase, float flHighFreq ) = 0;
  147. virtual bool IsPreTiled() const = 0;
  148. #if defined( _GAMECONSOLE )
  149. virtual int UpdateOrCreate( const char *pFilename, const char *pPathID = NULL, bool bForce = false ) = 0;
  150. virtual bool UnserializeFromBuffer( CUtlBuffer &buf, bool bBufferIsVolatile, bool bHeaderOnly, bool bPreloadOnly, int nMipSkipCount ) = 0;
  151. virtual int FileSize( bool bPreloadOnly, int nMipSkipCount ) const = 0;
  152. virtual int MappingWidth() const = 0;
  153. virtual int MappingHeight() const = 0;
  154. virtual int MappingDepth() const = 0;
  155. virtual int MipSkipCount() const = 0;
  156. virtual uint8 *LowResImageSample() = 0;
  157. virtual void ReleaseImageMemory() = 0;
  158. #endif
  159. #if defined ( _PS3 )
  160. virtual int GetImageOffset() const = 0;
  161. #endif
  162. // Sets post-processing flags (settings are copied, pointer passed to distinguish between structure versions)
  163. virtual void SetPostProcessingSettings( VtfProcessingOptions const *pOptions ) = 0;
  164. };
  165. //-----------------------------------------------------------------------------
  166. // Class factory
  167. //-----------------------------------------------------------------------------
  168. IVTFTexture *CreateVTFTexture();
  169. void DestroyVTFTexture( IVTFTexture *pTexture );
  170. //-----------------------------------------------------------------------------
  171. // Allows us to only load in the first little bit of the VTF file to get info
  172. // Clients should read this much into a UtlBuffer and then pass it in to
  173. // Unserialize
  174. //-----------------------------------------------------------------------------
  175. int VTFFileHeaderSize( int nMajorVersion = -1, int nMinorVersion = -1 );
  176. //-----------------------------------------------------------------------------
  177. // 360 Conversion
  178. //-----------------------------------------------------------------------------
  179. typedef bool (*CompressFunc_t)( CUtlBuffer &inputBuffer, CUtlBuffer &outputBuffer );
  180. bool ConvertVTFTo360Format( const char *pDebugName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf, CompressFunc_t pCompressFunc, int nMaxMip = 0 );
  181. bool ConvertVTFToPS3Format( const char *pDebugName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf, CompressFunc_t pCompressFunc, int nMaxMip = 0 );
  182. bool SRGBCorrectImage( uint8 *pImage,int imageSize, ImageFormat imageFormat);
  183. //-----------------------------------------------------------------------------
  184. // 360 Preload
  185. //-----------------------------------------------------------------------------
  186. bool GetVTFPreload360Data( const char *pDebugName, CUtlBuffer &fileBufferIn, CUtlBuffer &preloadBufferOut );
  187. bool GetVTFPreloadPS3Data( const char *pDebugName, CUtlBuffer &fileBufferIn, CUtlBuffer &preloadBufferOut );
  188. #include "mathlib/vector.h"
  189. #endif // VTF_FILE_FORMAT_ONLY
  190. //-----------------------------------------------------------------------------
  191. // Disk format for VTF files ver. 7.2 and earlier
  192. //
  193. // NOTE: After the header is the low-res image data
  194. // Then follows image data, which is sorted in the following manner
  195. //
  196. // for each mip level (starting with 1x1, and getting larger)
  197. // for each animation frame
  198. // for each face
  199. // store the image data for the face
  200. //
  201. // NOTE: In memory, we store the data in the following manner:
  202. // for each animation frame
  203. // for each face
  204. // for each mip level (starting with the largest, and getting smaller)
  205. // store the image data for the face
  206. //
  207. // This is done because the various image manipulation function we have
  208. // expect this format
  209. //-----------------------------------------------------------------------------
  210. // Disk format for VTF files ver. 7.3
  211. //
  212. // NOTE: After the header is the array of ResourceEntryInfo structures,
  213. // number of elements in the array is defined by "numResources".
  214. // there are entries for:
  215. // eRsrcLowResImage = low-res image data
  216. // eRsrcSheet = sheet data
  217. // eRsrcImage = image data
  218. // {
  219. // for each mip level (starting with 1x1, and getting larger)
  220. // for each animation frame
  221. // for each face
  222. // store the image data for the face
  223. //
  224. // NOTE: In memory, we store the data in the following manner:
  225. // for each animation frame
  226. // for each face
  227. // for each mip level (starting with the largest, and getting smaller)
  228. // store the image data for the face
  229. // }
  230. //
  231. //-----------------------------------------------------------------------------
  232. #include "datamap.h"
  233. #pragma pack(1)
  234. // version number for the disk texture cache
  235. #define VTF_MAJOR_VERSION 7
  236. #define VTF_MINOR_VERSION 5
  237. //-----------------------------------------------------------------------------
  238. // !!!!CRITICAL!!!! BEFORE YOU CHANGE THE FORMAT
  239. //
  240. // The structure sizes ARE NOT what they appear, regardless of Pack(1).
  241. // The "VectorAligned" causes invisible padding in the FINAL derived structure.
  242. //
  243. // Each VTF format has been silently plagued by this.
  244. //
  245. // LOOK AT A 7.3 FILE. The 7.3 structure ends at 0x48 as you would expect by
  246. // counting structure bytes. But, the "Infos" start at 0x50! because the PC
  247. // compiler pads, the 360 compiler does NOT.
  248. //-----------------------------------------------------------------------------
  249. struct VTFFileBaseHeader_t
  250. {
  251. DECLARE_BYTESWAP_DATADESC();
  252. char fileTypeString[4]; // "VTF" Valve texture file
  253. int version[2]; // version[0].version[1]
  254. int headerSize;
  255. };
  256. struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
  257. {
  258. DECLARE_BYTESWAP_DATADESC();
  259. uint16 width;
  260. uint16 height;
  261. uint32 flags;
  262. uint16 numFrames;
  263. uint16 startFrame;
  264. #if !defined( POSIX ) && !defined( _X360 )
  265. VectorAligned reflectivity;
  266. #else
  267. // must manually align in order to maintain pack(1) expected layout with existing binaries
  268. char pad1[4];
  269. Vector reflectivity;
  270. char pad2[4];
  271. #endif
  272. float bumpScale;
  273. ImageFormat imageFormat;
  274. uint8 numMipLevels;
  275. ImageFormat lowResImageFormat;
  276. uint8 lowResImageWidth;
  277. uint8 lowResImageHeight;
  278. };
  279. struct VTFFileHeaderV7_2_t : public VTFFileHeaderV7_1_t
  280. {
  281. DECLARE_BYTESWAP_DATADESC();
  282. uint16 depth;
  283. };
  284. #define BYTE_POS( byteVal, shft ) uint32( uint32(uint8(byteVal)) << uint8(shft * 8) )
  285. #if !defined( _X360 ) && !defined ( _PS3 )
  286. #define MK_VTF_RSRC_ID(a, b, c) uint32( BYTE_POS(a, 0) | BYTE_POS(b, 1) | BYTE_POS(c, 2) )
  287. #define MK_VTF_RSRCF(d) BYTE_POS(d, 3)
  288. #else
  289. #define MK_VTF_RSRC_ID(a, b, c) uint32( BYTE_POS(a, 3) | BYTE_POS(b, 2) | BYTE_POS(c, 1) )
  290. #define MK_VTF_RSRCF(d) BYTE_POS(d, 0)
  291. #endif
  292. // Special section for stock resources types
  293. enum ResourceEntryType
  294. {
  295. // Legacy stock resources, readin/writing are handled differently (i.e. they do not have the length tag word!)
  296. VTF_LEGACY_RSRC_LOW_RES_IMAGE = MK_VTF_RSRC_ID( 0x01, 0, 0 ), // Low-res image data
  297. VTF_LEGACY_RSRC_IMAGE = MK_VTF_RSRC_ID( 0x30, 0, 0 ), // Image data
  298. // New extended resource
  299. VTF_RSRC_SHEET = MK_VTF_RSRC_ID( 0x10, 0, 0 ), // Sheet data
  300. };
  301. // Bytes with special meaning when set in a resource type
  302. enum ResourceEntryTypeFlag
  303. {
  304. RSRCF_HAS_NO_DATA_CHUNK = MK_VTF_RSRCF( 0x02 ), // Resource doesn't have a corresponding data chunk
  305. RSRCF_MASK = MK_VTF_RSRCF( 0xFF ) // Mask for all the flags
  306. };
  307. // Header details constants
  308. enum HeaderDetails
  309. {
  310. MAX_RSRC_DICTIONARY_ENTRIES = 32, // Max number of resources in dictionary
  311. MAX_X360_RSRC_DICTIONARY_ENTRIES = 4, // 360 needs this to be slim, otherwise preload size suffers
  312. };
  313. struct ResourceEntryInfo
  314. {
  315. union
  316. {
  317. uint32 eType; // Use MK_VTF_??? macros to be endian compliant with the type
  318. uint8 chTypeBytes[4];
  319. };
  320. uint32 resData; // Resource data or offset from the beginning of the file
  321. };
  322. struct VTFFileHeaderV7_3_t : public VTFFileHeaderV7_2_t
  323. {
  324. DECLARE_BYTESWAP_DATADESC();
  325. char pad4[3];
  326. uint32 numResources;
  327. #if defined( _X360 ) || defined( POSIX )
  328. // must manually align in order to maintain pack(1) expected layout with existing binaries
  329. char pad5[8];
  330. #endif
  331. // AFTER THE IMPLICIT PADDING CAUSED BY THE COMPILER....
  332. // *** followed by *** ResourceEntryInfo resources[0];
  333. // Array of resource entry infos sorted ascending by type
  334. };
  335. struct VTFFileHeader_t : public VTFFileHeaderV7_3_t
  336. {
  337. DECLARE_BYTESWAP_DATADESC();
  338. };
  339. #define VTF_X360_MAJOR_VERSION 0x0360
  340. #define VTF_X360_MINOR_VERSION 8
  341. struct VTFFileHeaderX360_t : public VTFFileBaseHeader_t
  342. {
  343. DECLARE_BYTESWAP_DATADESC();
  344. uint32 flags;
  345. uint16 width; // actual width of data in file
  346. uint16 height; // actual height of data in file
  347. uint16 depth; // actual depth of data in file
  348. uint16 numFrames;
  349. uint16 preloadDataSize; // exact size of preload data (may extend into image!)
  350. uint8 mipSkipCount; // used to resconstruct mapping dimensions
  351. uint8 numResources;
  352. Vector reflectivity; // Resides on 16 byte boundary!
  353. float bumpScale;
  354. ImageFormat imageFormat;
  355. uint8 lowResImageSample[4];
  356. uint32 compressedSize;
  357. // *** followed by *** ResourceEntryInfo resources[0];
  358. };
  359. // PS3 version of the vtf file header. This is just a copy of the 360 file header
  360. #define VTF_PS3_MAJOR_VERSION 0x0333
  361. #define VTF_PS3_MINOR_VERSION 8
  362. struct ALIGN16 VTFFileHeaderPS3_t : public VTFFileBaseHeader_t
  363. {
  364. DECLARE_BYTESWAP_DATADESC();
  365. uint32 flags;
  366. uint16 width; // actual width of data in file
  367. uint16 height; // actual height of data in file
  368. uint16 depth; // actual depth of data in file
  369. uint16 numFrames;
  370. uint16 preloadDataSize; // exact size of preload data (may extend into image!)
  371. uint8 mipSkipCount; // used to resconstruct mapping dimensions
  372. uint8 numResources;
  373. Vector reflectivity; // Resides on 16 byte boundary!
  374. float bumpScale;
  375. ImageFormat imageFormat;
  376. uint8 lowResImageSample[4];
  377. uint32 compressedSize;
  378. uint8 _padding_ps3[4]; // padding it to 64 bytes
  379. // *** followed by *** ResourceEntryInfo resources[0];
  380. }
  381. ALIGN16_POST;
  382. ///////////////////////////
  383. // Resource Extensions //
  384. ///////////////////////////
  385. // extended texture lod control:
  386. #define VTF_RSRC_TEXTURE_LOD_SETTINGS ( MK_VTF_RSRC_ID( 'L','O','D' ) )
  387. struct TextureLODControlSettings_t
  388. {
  389. // What to clamp the dimenstions to, mip-map wise, when at picmip 0. keeps texture from
  390. // exceeding (1<<m_ResolutionClamp) at picmip 0. at picmip 1, it won't exceed
  391. // (1<<(m_ResolutionClamp-1)), etc.
  392. uint8 m_ResolutionClampX;
  393. uint8 m_ResolutionClampY;
  394. uint8 m_ResolutionClampX_360;
  395. uint8 m_ResolutionClampY_360;
  396. };
  397. // Extended flags and settings:
  398. #define VTF_RSRC_TEXTURE_SETTINGS_EX ( MK_VTF_RSRC_ID( 'T','S','0' ) )
  399. struct TextureSettingsEx_t
  400. {
  401. enum Flags0 // flags0 byte mask
  402. {
  403. UNUSED = 0x01,
  404. };
  405. uint8 m_flags0; // a bitwise combination of Flags0
  406. uint8 m_flags1; // set to zero. for future expansion.
  407. uint8 m_flags2; // set to zero. for future expansion.
  408. uint8 m_flags3; // set to zero. for future expansion.
  409. };
  410. #define VTF_RSRC_TEXTURE_CRC ( MK_VTF_RSRC_ID( 'C','R','C' ) )
  411. #pragma pack()
  412. #endif // VTF_H