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.

2996 lines
80 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include <string.h>
  11. #include "tier1/strtools.h"
  12. #include <sys/stat.h>
  13. #include "bitmap/bitmap.h"
  14. #include "bitmap/tgaloader.h"
  15. #include "bitmap/psd.h"
  16. #include "bitmap/float_bm.h"
  17. #include "bitmap/imageformat.h"
  18. #include "mathlib/mathlib.h"
  19. #ifdef POSIX
  20. #include <sys/stat.h>
  21. #define _stat stat
  22. #endif
  23. #ifdef WIN32
  24. #include "conio.h"
  25. #include <direct.h>
  26. #include <io.h>
  27. #endif
  28. #include "vtf/vtf.h"
  29. #include "utlbuffer.h"
  30. #include "tier0/dbg.h"
  31. #include "cmdlib.h"
  32. #include "tier0/icommandline.h"
  33. #ifdef WIN32
  34. #include "windows.h"
  35. #endif
  36. #include "ilaunchabledll.h"
  37. #include "ivtex.h"
  38. #include "appframework/IAppSystemGroup.h"
  39. #include "tier2/tier2.h"
  40. #include "tier2/p4helpers.h"
  41. #include "p4lib/ip4.h"
  42. #include "tier1/checksum_crc.h"
  43. #define FF_TRYAGAIN 1
  44. #define FF_DONTPROCESS 2
  45. #define LOWRESIMAGE_DIM 16
  46. #ifdef POSIX
  47. #define LOWRES_IMAGE_FORMAT IMAGE_FORMAT_RGBA8888
  48. #else
  49. #define LOWRES_IMAGE_FORMAT IMAGE_FORMAT_DXT1
  50. #endif
  51. //#define DEBUG_NO_COMPRESSION
  52. static bool g_NoPause = false;
  53. static bool g_Quiet = false;
  54. static const char *g_ShaderName = NULL;
  55. static bool g_CreateDir = true;
  56. static bool g_UseGameDir = true;
  57. static bool g_bUseStandardError = false;
  58. static bool g_bWarningsAsErrors = false;
  59. static bool g_bUsedAsLaunchableDLL = false;
  60. static bool g_bNoTga = false;
  61. static bool g_bNoPsd = false;
  62. static char g_ForcedOutputDir[MAX_PATH];
  63. #define MAX_VMT_PARAMS 16
  64. struct VTexVMTParam_t
  65. {
  66. const char *m_szParam;
  67. const char *m_szValue;
  68. };
  69. class SmartIVtfTexture
  70. {
  71. public:
  72. explicit SmartIVtfTexture( IVTFTexture *pVtf ) : m_p( pVtf ) {}
  73. ~SmartIVtfTexture() { if ( m_p ) DestroyVTFTexture( m_p ); }
  74. private:
  75. SmartIVtfTexture( SmartIVtfTexture const &x );
  76. SmartIVtfTexture & operator = ( SmartIVtfTexture const &x );
  77. private:
  78. SmartIVtfTexture & operator = ( IVTFTexture *pVtf ) { m_p = pVtf; return *this; }
  79. operator IVTFTexture * () const { return m_p; }
  80. public:
  81. IVTFTexture * Assign( IVTFTexture *pVtfNew ) { IVTFTexture *pOld = m_p; m_p = pVtfNew; return pOld; }
  82. IVTFTexture * Get() const { return m_p; }
  83. IVTFTexture * operator->() const { return m_p; }
  84. protected:
  85. IVTFTexture *m_p;
  86. };
  87. static VTexVMTParam_t g_VMTParams[MAX_VMT_PARAMS];
  88. static int g_NumVMTParams = 0;
  89. static enum Mode { eModePSD, eModeTGA, eModePFM } g_eMode = eModePSD;
  90. // NOTE: these must stay in the same order as CubeMapFaceIndex_t.
  91. static const char *g_CubemapFacingNames[7] = { "rt", "lf", "bk", "ft", "up", "dn", "sph" };
  92. static void Pause( void )
  93. {
  94. if( !g_NoPause )
  95. {
  96. printf( "Hit a key to continue\n" );
  97. #ifdef WIN32
  98. getch();
  99. #endif
  100. }
  101. }
  102. static bool VTexErrorAborts()
  103. {
  104. if ( CommandLine()->FindParm( "-crcvalidate" ) )
  105. return false;
  106. return true;
  107. }
  108. static void VTexError( const char *pFormat, ... )
  109. {
  110. char str[4096];
  111. va_list marker;
  112. va_start( marker, pFormat );
  113. Q_vsnprintf( str, sizeof( str ), pFormat, marker );
  114. va_end( marker );
  115. if ( !VTexErrorAborts() )
  116. {
  117. fprintf( stderr, "ERROR: %s", str );
  118. return;
  119. }
  120. if ( g_bUseStandardError )
  121. {
  122. Error( "ERROR: %s", str );
  123. }
  124. else
  125. {
  126. fprintf( stderr, "ERROR: %s", str );
  127. Pause();
  128. exit( 1 );
  129. }
  130. }
  131. static void VTexWarning( const char *pFormat, ... )
  132. {
  133. char str[4096];
  134. va_list marker;
  135. va_start( marker, pFormat );
  136. Q_vsnprintf( str, sizeof( str ), pFormat, marker );
  137. va_end( marker );
  138. if ( g_bWarningsAsErrors )
  139. {
  140. VTexError( "%s", str );
  141. }
  142. else
  143. {
  144. fprintf( stderr, "WARN: %s", str );
  145. Pause();
  146. }
  147. }
  148. struct VTexConfigInfo_t
  149. {
  150. int m_nStartFrame;
  151. int m_nEndFrame;
  152. unsigned int m_nFlags;
  153. float m_flBumpScale;
  154. LookDir_t m_LookDir;
  155. bool m_bNormalToDuDv;
  156. bool m_bAlphaToLuminance;
  157. bool m_bDuDv;
  158. float m_flAlphaThreshhold;
  159. float m_flAlphaHiFreqThreshhold;
  160. bool m_bSkyBox;
  161. int m_nVolumeTextureDepth;
  162. float m_pfmscale;
  163. bool m_bStripAlphaChannel;
  164. bool m_bStripColorChannel;
  165. bool m_bIsCubeMap;
  166. // scaling parameters
  167. int m_nReduceX;
  168. int m_nReduceY;
  169. int m_nMaxDimensionX, m_nMaxDimensionX_360;
  170. int m_nMaxDimensionY, m_nMaxDimensionY_360;
  171. // may restrict the texture to reading only 3 channels
  172. int m_numChannelsMax;
  173. bool m_bAlphaToDistance;
  174. float m_flDistanceSpread; // how far to stretch out distance range in pixels
  175. CRC32_t m_uiInputHash; // Sources hash
  176. TextureSettingsEx_t m_exSettings0;
  177. VtfProcessingOptions m_vtfProcOptions;
  178. enum
  179. {
  180. // CRC of input files:
  181. // txt + tga/pfm
  182. // or
  183. // psd
  184. VTF_INPUTSRC_CRC = MK_VTF_RSRC_ID( 'C','R','C' )
  185. };
  186. char m_SrcName[MAX_PATH];
  187. VTexConfigInfo_t( void )
  188. {
  189. m_nStartFrame = -1;
  190. m_nEndFrame = -1;
  191. m_nFlags = 0;
  192. m_bNormalToDuDv = false;
  193. m_bAlphaToLuminance = false;
  194. m_flBumpScale = 1.0f;
  195. m_bDuDv = false;
  196. m_flAlphaThreshhold = -1.0f;
  197. m_flAlphaHiFreqThreshhold = -1.0f;
  198. m_bSkyBox = false;
  199. m_nVolumeTextureDepth = 1;
  200. m_pfmscale=1.0;
  201. m_bStripAlphaChannel = false;
  202. m_bStripColorChannel = false;
  203. m_bIsCubeMap = false;
  204. m_nReduceX = 1;
  205. m_nReduceY = 1;
  206. m_SrcName[0]=0;
  207. m_numChannelsMax = 4;
  208. m_bAlphaToDistance = 0;
  209. m_flDistanceSpread = 1.0;
  210. m_nMaxDimensionX = -1;
  211. m_nMaxDimensionX_360 = -1;
  212. m_nMaxDimensionY = -1;
  213. m_nMaxDimensionY_360 = -1;
  214. memset( &m_exSettings0, 0, sizeof( m_exSettings0 ) );
  215. memset( &m_vtfProcOptions, 0, sizeof( m_vtfProcOptions ) );
  216. m_vtfProcOptions.cbSize = sizeof( m_vtfProcOptions );
  217. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_FILTER_NICE;
  218. CRC32_Init( &m_uiInputHash );
  219. }
  220. bool IsSettings0Valid( void ) const
  221. {
  222. TextureSettingsEx_t exSettingsEmpty;
  223. memset( &exSettingsEmpty, 0, sizeof( exSettingsEmpty ) );
  224. Assert( sizeof( m_exSettings0 ) == sizeof( exSettingsEmpty ) );
  225. return !!memcmp( &m_exSettings0, &exSettingsEmpty, sizeof( m_exSettings0 ) );
  226. }
  227. // returns false if unrecognized option
  228. void ParseOptionKey( const char *pKeyName, const char *pKeyValue );
  229. };
  230. template < typename T >
  231. static inline T& SetFlagValueT( T &field, T const &flag, int bSetFlag )
  232. {
  233. if ( bSetFlag )
  234. field |= flag;
  235. else
  236. field &=~flag;
  237. return field;
  238. }
  239. static inline uint32& SetFlagValue( uint32 &field, uint32 const &flag, int bSetFlag )
  240. {
  241. return SetFlagValueT<uint32>( field, flag, bSetFlag );
  242. }
  243. void VTexConfigInfo_t::ParseOptionKey( const char *pKeyName, const char *pKeyValue )
  244. {
  245. int iValue = atoi( pKeyValue ); // To properly have "clamps 0" and not enable the clamping
  246. if ( !stricmp( pKeyName, "skybox" ) )
  247. {
  248. // We're going to treat it like a cubemap until the very end, so it'll load the other skybox faces and
  249. // match their edges with the texture compression and mipmapping.
  250. m_bSkyBox = iValue ? true : false;
  251. m_bIsCubeMap = iValue ? true : false;
  252. if ( !g_Quiet && iValue )
  253. Msg( "'skybox' detected. Treating skybox like a cubemap for edge-matching purposes.\n" );
  254. }
  255. else if( !stricmp( pKeyName, "startframe" ) )
  256. {
  257. m_nStartFrame = atoi( pKeyValue );
  258. }
  259. else if( !stricmp( pKeyName, "endframe" ) )
  260. {
  261. m_nEndFrame = atoi( pKeyValue );
  262. }
  263. else if( !stricmp( pKeyName, "volumetexture" ) )
  264. {
  265. m_nVolumeTextureDepth = atoi( pKeyValue );
  266. // FIXME: Volume textures don't currently support DXT compression
  267. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_NOCOMPRESS;
  268. // FIXME: Volume textures don't currently support NICE filtering
  269. m_vtfProcOptions.flags0 &= ~VtfProcessingOptions::OPT_FILTER_NICE;
  270. }
  271. else if( !stricmp( pKeyName, "spheremap_x" ) )
  272. {
  273. if ( iValue )
  274. m_LookDir = LOOK_DOWN_X;
  275. }
  276. else if( !stricmp( pKeyName, "spheremap_negx" ) )
  277. {
  278. if ( iValue )
  279. m_LookDir = LOOK_DOWN_NEGX;
  280. }
  281. else if( !stricmp( pKeyName, "spheremap_y" ) )
  282. {
  283. if ( iValue )
  284. m_LookDir = LOOK_DOWN_Y;
  285. }
  286. else if( !stricmp( pKeyName, "spheremap_negy" ) )
  287. {
  288. if ( iValue )
  289. m_LookDir = LOOK_DOWN_NEGY;
  290. }
  291. else if( !stricmp( pKeyName, "spheremap_z" ) )
  292. {
  293. if ( iValue )
  294. m_LookDir = LOOK_DOWN_Z;
  295. }
  296. else if( !stricmp( pKeyName, "spheremap_negz" ) )
  297. {
  298. if ( iValue )
  299. m_LookDir = LOOK_DOWN_NEGZ;
  300. }
  301. else if( !stricmp( pKeyName, "bumpscale" ) )
  302. {
  303. m_flBumpScale = atof( pKeyValue );
  304. }
  305. else if( !stricmp( pKeyName, "pointsample" ) )
  306. {
  307. SetFlagValue( m_nFlags, TEXTUREFLAGS_POINTSAMPLE, iValue );
  308. }
  309. else if( !stricmp( pKeyName, "trilinear" ) )
  310. {
  311. SetFlagValue( m_nFlags, TEXTUREFLAGS_TRILINEAR, iValue );
  312. }
  313. else if( !stricmp( pKeyName, "clamps" ) )
  314. {
  315. SetFlagValue( m_nFlags, TEXTUREFLAGS_CLAMPS, iValue );
  316. }
  317. else if( !stricmp( pKeyName, "clampt" ) )
  318. {
  319. SetFlagValue( m_nFlags, TEXTUREFLAGS_CLAMPT, iValue );
  320. }
  321. else if( !stricmp( pKeyName, "clampu" ) )
  322. {
  323. SetFlagValue( m_nFlags, TEXTUREFLAGS_CLAMPU, iValue );
  324. }
  325. else if( !stricmp( pKeyName, "border" ) )
  326. {
  327. SetFlagValue( m_nFlags, TEXTUREFLAGS_BORDER, iValue );
  328. // Gets applied to s, t and u We currently assume black border color
  329. }
  330. else if( !stricmp( pKeyName, "anisotropic" ) )
  331. {
  332. SetFlagValue( m_nFlags, TEXTUREFLAGS_ANISOTROPIC, iValue );
  333. }
  334. else if( !stricmp( pKeyName, "dxt5" ) )
  335. {
  336. SetFlagValue( m_nFlags, TEXTUREFLAGS_HINT_DXT5, iValue );
  337. }
  338. else if( !stricmp( pKeyName, "nocompress" ) )
  339. {
  340. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_NOCOMPRESS, iValue );
  341. }
  342. else if( !stricmp( pKeyName, "normal" ) )
  343. {
  344. SetFlagValue( m_nFlags, TEXTUREFLAGS_NORMAL, iValue );
  345. }
  346. else if( !stricmp( pKeyName, "ssbump" ) )
  347. {
  348. SetFlagValue( m_nFlags, TEXTUREFLAGS_SSBUMP, iValue );
  349. }
  350. else if( !stricmp( pKeyName, "nomip" ) )
  351. {
  352. SetFlagValue( m_nFlags, TEXTUREFLAGS_NOMIP, iValue );
  353. }
  354. else if( !stricmp( pKeyName, "allmips" ) )
  355. {
  356. SetFlagValue( m_nFlags, TEXTUREFLAGS_ALL_MIPS, iValue );
  357. }
  358. else if( !stricmp( pKeyName, "nonice" ) )
  359. {
  360. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_FILTER_NICE, !iValue );
  361. }
  362. else if( !stricmp( pKeyName, "nolod" ) )
  363. {
  364. SetFlagValue( m_nFlags, TEXTUREFLAGS_NOLOD, iValue );
  365. }
  366. else if( !stricmp( pKeyName, "procedural" ) )
  367. {
  368. SetFlagValue( m_nFlags, TEXTUREFLAGS_PROCEDURAL, iValue );
  369. }
  370. else if( !stricmp( pKeyName, "alphatest" ) )
  371. {
  372. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_MIP_ALPHATEST, iValue );
  373. }
  374. else if( !stricmp( pKeyName, "alphatest_threshhold" ) )
  375. {
  376. m_flAlphaThreshhold = atof( pKeyValue );
  377. }
  378. else if( !stricmp( pKeyName, "alphatest_hifreq_threshhold" ) )
  379. {
  380. m_flAlphaHiFreqThreshhold = atof( pKeyValue );
  381. }
  382. else if( !stricmp( pKeyName, "rendertarget" ) )
  383. {
  384. SetFlagValue( m_nFlags, TEXTUREFLAGS_RENDERTARGET, iValue );
  385. }
  386. else if ( !stricmp( pKeyName, "numchannels" ) )
  387. {
  388. m_numChannelsMax = atoi( pKeyValue );
  389. }
  390. else if ( !stricmp( pKeyName, "nodebug" ) )
  391. {
  392. SetFlagValue( m_nFlags, TEXTUREFLAGS_NODEBUGOVERRIDE, iValue );
  393. }
  394. else if ( !stricmp( pKeyName, "singlecopy" ) )
  395. {
  396. SetFlagValue( m_nFlags, TEXTUREFLAGS_SINGLECOPY, iValue );
  397. }
  398. else if( !stricmp( pKeyName, "oneovermiplevelinalpha" ) )
  399. {
  400. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_SET_ALPHA_ONEOVERMIP, iValue );
  401. }
  402. else if( !stricmp( pKeyName, "premultcolorbyoneovermiplevel" ) )
  403. {
  404. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_PREMULT_COLOR_ONEOVERMIP, iValue );
  405. }
  406. else if ( !stricmp( pKeyName, "normaltodudv" ) )
  407. {
  408. m_bNormalToDuDv = iValue ? true : false;
  409. SetFlagValue( m_vtfProcOptions.flags0, VtfProcessingOptions::OPT_NORMAL_DUDV, iValue );
  410. }
  411. else if ( !stricmp( pKeyName, "stripalphachannel" ) )
  412. {
  413. m_bStripAlphaChannel = iValue ? true : false;
  414. }
  415. else if ( !stricmp( pKeyName, "stripcolorchannel" ) )
  416. {
  417. m_bStripColorChannel = iValue ? true : false;
  418. }
  419. else if ( !stricmp( pKeyName, "normalalphatodudvluminance" ) )
  420. {
  421. m_bAlphaToLuminance = iValue ? true : false;
  422. }
  423. else if ( !stricmp( pKeyName, "dudv" ) )
  424. {
  425. m_bDuDv = iValue ? true : false;
  426. }
  427. else if( !stricmp( pKeyName, "reduce" ) )
  428. {
  429. m_nReduceX = atoi(pKeyValue);
  430. m_nReduceY = m_nReduceX;
  431. }
  432. else if( !stricmp( pKeyName, "reducex" ) )
  433. {
  434. m_nReduceX = atoi(pKeyValue);
  435. }
  436. else if( !stricmp( pKeyName, "reducey" ) )
  437. {
  438. m_nReduceY = atoi(pKeyValue);
  439. }
  440. else if( !stricmp( pKeyName, "maxwidth" ) )
  441. {
  442. m_nMaxDimensionX = atoi(pKeyValue);
  443. }
  444. else if( !stricmp( pKeyName, "maxwidth_360" ) )
  445. {
  446. m_nMaxDimensionX_360 = atoi(pKeyValue);
  447. }
  448. else if( !stricmp( pKeyName, "maxheight" ) )
  449. {
  450. m_nMaxDimensionY = atoi(pKeyValue);
  451. }
  452. else if( !stricmp( pKeyName, "maxheight_360" ) )
  453. {
  454. m_nMaxDimensionY_360 = atoi(pKeyValue);
  455. }
  456. else if( !stricmp( pKeyName, "alphatodistance" ) )
  457. {
  458. m_bAlphaToDistance = iValue ? true : false;
  459. }
  460. else if( !stricmp( pKeyName, "distancespread" ) )
  461. {
  462. m_flDistanceSpread = atof(pKeyValue);
  463. }
  464. else if( !stricmp( pKeyName, "pfmscale" ) )
  465. {
  466. m_pfmscale=atof(pKeyValue);
  467. printf("******pfm scale=%f\n",m_pfmscale);
  468. }
  469. else if ( !stricmp( pKeyName, "pfm" ) )
  470. {
  471. if ( iValue )
  472. g_eMode = eModePFM;
  473. }
  474. else if ( !stricmp( pKeyName, "specvar" ) )
  475. {
  476. int iDecayChannel = -1;
  477. if ( !stricmp( pKeyValue, "red" ) || !stricmp( pKeyValue, "r" ) )
  478. iDecayChannel = 0;
  479. if ( !stricmp( pKeyValue, "green" ) || !stricmp( pKeyValue, "g" ) )
  480. iDecayChannel = 1;
  481. if ( !stricmp( pKeyValue, "blue" ) || !stricmp( pKeyValue, "b" ) )
  482. iDecayChannel = 2;
  483. if ( !stricmp( pKeyValue, "alpha" ) || !stricmp( pKeyValue, "a" ) )
  484. iDecayChannel = 3;
  485. if ( iDecayChannel >= 0 && iDecayChannel < 4 )
  486. {
  487. m_vtfProcOptions.flags0 |= ( VtfProcessingOptions::OPT_DECAY_R | VtfProcessingOptions::OPT_DECAY_EXP_R ) << iDecayChannel;
  488. m_vtfProcOptions.numNotDecayMips[iDecayChannel] = 0;
  489. m_vtfProcOptions.clrDecayGoal[iDecayChannel] = 0;
  490. m_vtfProcOptions.fDecayExponentBase[iDecayChannel] = 0.75;
  491. SetFlagValue( m_nFlags, TEXTUREFLAGS_ALL_MIPS, 1 );
  492. }
  493. }
  494. else if ( !stricmp( pKeyName, "mipblend" ) )
  495. {
  496. SetFlagValue( m_nFlags, TEXTUREFLAGS_ALL_MIPS, 1 );
  497. // Possible values
  498. if ( !stricmp( pKeyValue, "detail" ) ) // Skip 2 mips and fade to gray -> (128, 128, 128, -)
  499. {
  500. for( int ch = 0; ch < 3; ++ ch )
  501. {
  502. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_DECAY_R << ch;
  503. // m_vtfProcOptions.flags0 &= ~(VtfProcessingOptions::OPT_DECAY_EXP_R << ch);
  504. m_vtfProcOptions.numNotDecayMips[ch] = 2;
  505. m_vtfProcOptions.clrDecayGoal[ch] = 128;
  506. }
  507. }
  508. /*
  509. else if ( !stricmp( pKeyValue, "additive" ) ) // Skip 2 mips and fade to black -> (0, 0, 0, -)
  510. {
  511. for( int ch = 0; ch < 3; ++ ch )
  512. {
  513. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_DECAY_R << ch;
  514. m_vtfProcOptions.flags0 &= ~(VtfProcessingOptions::OPT_DECAY_EXP_R << ch);
  515. m_vtfProcOptions.numDecayMips[ch] = 2;
  516. m_vtfProcOptions.clrDecayGoal[ch] = 0;
  517. }
  518. }
  519. else if ( !stricmp( pKeyValue, "alphablended" ) ) // Skip 2 mips and fade out alpha to 0
  520. {
  521. for( int ch = 3; ch < 4; ++ ch )
  522. {
  523. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_DECAY_R << ch;
  524. m_vtfProcOptions.flags0 &= ~(VtfProcessingOptions::OPT_DECAY_EXP_R << ch);
  525. m_vtfProcOptions.numDecayMips[ch] = 2;
  526. m_vtfProcOptions.clrDecayGoal[ch] = 0;
  527. }
  528. }
  529. */
  530. else
  531. {
  532. // Parse the given value:
  533. // skip=3:r=255:g=255:b=255:a=255 - linear decay
  534. // r=0e.75 - exponential decay targeting 0 with exponent base 0.75
  535. int nSteps = 0; // default
  536. for ( char const *szParse = pKeyValue; szParse; szParse = strchr( szParse, ':' ), szParse ? ++ szParse : 0 )
  537. {
  538. if ( char const *sz = StringAfterPrefix( szParse, "skip=" ) )
  539. {
  540. szParse = sz;
  541. nSteps = atoi(sz);
  542. }
  543. else if ( StringHasPrefix( szParse, "r=" ) ||
  544. StringHasPrefix( szParse, "g=" ) ||
  545. StringHasPrefix( szParse, "b=" ) ||
  546. StringHasPrefix( szParse, "a=" ) )
  547. {
  548. int ch = 0;
  549. switch ( *szParse )
  550. {
  551. case 'g': case 'G': ch = 1; break;
  552. case 'b': case 'B': ch = 2; break;
  553. case 'a': case 'A': ch = 3; break;
  554. }
  555. szParse += 2;
  556. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_DECAY_R << ch;
  557. m_vtfProcOptions.flags0 &= ~(VtfProcessingOptions::OPT_DECAY_EXP_R << ch);
  558. m_vtfProcOptions.numNotDecayMips[ch] = nSteps;
  559. m_vtfProcOptions.clrDecayGoal[ch] = atoi( szParse );
  560. while ( isdigit( *szParse ) )
  561. ++ szParse;
  562. // Exponential decay
  563. if ( ( *szParse == 'e' || *szParse == 'E' ) && ( szParse[1] == '.' ) )
  564. {
  565. m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_DECAY_EXP_R << ch;
  566. m_vtfProcOptions.fDecayExponentBase[ch] = ( float ) atof( szParse + 1 );
  567. }
  568. }
  569. else
  570. {
  571. printf( "Warning: invalid mipblend setting \"%s\"\n", pKeyValue );
  572. }
  573. }
  574. }
  575. }
  576. else if( !stricmp( pKeyName, "srgb" ) )
  577. {
  578. SetFlagValue( m_nFlags, TEXTUREFLAGS_SRGB, iValue );
  579. }
  580. else
  581. {
  582. VTexError("unrecognized option in text file - %s\n", pKeyName );
  583. }
  584. }
  585. static const char *GetSourceExtension( void )
  586. {
  587. switch ( g_eMode )
  588. {
  589. case eModePSD:
  590. return ".psd";
  591. case eModeTGA:
  592. return ".tga";
  593. case eModePFM:
  594. return ".pfm";
  595. default:
  596. return ".tga";
  597. }
  598. }
  599. //-----------------------------------------------------------------------------
  600. // Computes the desired texture format based on flags
  601. //-----------------------------------------------------------------------------
  602. static ImageFormat ComputeDesiredImageFormat( IVTFTexture *pTexture, VTexConfigInfo_t &info )
  603. {
  604. bool bDUDVTarget = info.m_bNormalToDuDv || info.m_bDuDv;
  605. bool bCopyAlphaToLuminance = info.m_bNormalToDuDv && info.m_bAlphaToLuminance;
  606. ImageFormat targetFormat;
  607. int nFlags = pTexture->Flags();
  608. if ( info.m_bStripAlphaChannel )
  609. {
  610. nFlags &= ~( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA );
  611. }
  612. // HDRFIXME: Need to figure out what format to use here.
  613. if ( pTexture->Format() == IMAGE_FORMAT_RGB323232F )
  614. {
  615. #ifndef DEBUG_NO_COMPRESSION
  616. if ( g_bUsedAsLaunchableDLL && !( info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_NOCOMPRESS ) )
  617. {
  618. return IMAGE_FORMAT_BGRA8888;
  619. }
  620. else
  621. #endif // #ifndef DEBUG_NO_COMPRESSION
  622. {
  623. return IMAGE_FORMAT_RGBA16161616F;
  624. }
  625. }
  626. if ( bDUDVTarget )
  627. {
  628. if ( bCopyAlphaToLuminance && ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) ) )
  629. return IMAGE_FORMAT_UVLX8888;
  630. return IMAGE_FORMAT_UV88;
  631. }
  632. if ( info.m_bStripColorChannel )
  633. {
  634. return IMAGE_FORMAT_A8;
  635. }
  636. // can't compress textures that are smaller than 4x4
  637. if( (nFlags & TEXTUREFLAGS_PROCEDURAL) ||
  638. (info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_NOCOMPRESS) ||
  639. (pTexture->Width() < 4) || (pTexture->Height() < 4) )
  640. {
  641. if ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) )
  642. {
  643. targetFormat = IMAGE_FORMAT_BGRA8888;
  644. }
  645. else
  646. {
  647. targetFormat = IMAGE_FORMAT_BGR888;
  648. }
  649. }
  650. else if( nFlags & TEXTUREFLAGS_HINT_DXT5 )
  651. {
  652. #ifdef DEBUG_NO_COMPRESSION
  653. targetFormat = IMAGE_FORMAT_BGRA8888;
  654. #else
  655. targetFormat = IsPosix() ? IMAGE_FORMAT_BGRA8888 : IMAGE_FORMAT_DXT5; // No DXT compressor on Posix
  656. #endif
  657. }
  658. else if( nFlags & TEXTUREFLAGS_EIGHTBITALPHA )
  659. {
  660. // compressed with alpha blending
  661. #ifdef DEBUG_NO_COMPRESSION
  662. targetFormat = IMAGE_FORMAT_BGRA8888;
  663. #else
  664. targetFormat = IsPosix() ? IMAGE_FORMAT_BGRA8888 : IMAGE_FORMAT_DXT5; // No DXT compressor on Posix
  665. #endif
  666. }
  667. else if ( nFlags & TEXTUREFLAGS_ONEBITALPHA )
  668. {
  669. // garymcthack - fixme IMAGE_FORMAT_DXT1_ONEBITALPHA doesn't work yet.
  670. #ifdef DEBUG_NO_COMPRESSION
  671. targetFormat = IMAGE_FORMAT_BGRA8888;
  672. #else
  673. // targetFormat = IMAGE_FORMAT_DXT1_ONEBITALPHA;
  674. targetFormat = IsPosix() ? IMAGE_FORMAT_BGRA8888 : IMAGE_FORMAT_DXT5; // No DXT compressor on Posix
  675. #endif
  676. }
  677. else
  678. {
  679. #ifdef DEBUG_NO_COMPRESSION
  680. targetFormat = IMAGE_FORMAT_BGR888;
  681. #else
  682. targetFormat = IsPosix() ? IMAGE_FORMAT_BGR888 : IMAGE_FORMAT_DXT1; // No DXT compressor on Posix
  683. #endif
  684. }
  685. return targetFormat;
  686. }
  687. //-----------------------------------------------------------------------------
  688. // Computes the low res image size
  689. //-----------------------------------------------------------------------------
  690. void VTFGetLowResImageInfo( int cacheWidth, int cacheHeight, int *lowResImageWidth, int *lowResImageHeight,
  691. ImageFormat *imageFormat )
  692. {
  693. if (cacheWidth > cacheHeight)
  694. {
  695. int factor = cacheWidth / LOWRESIMAGE_DIM;
  696. if (factor > 0)
  697. {
  698. *lowResImageWidth = LOWRESIMAGE_DIM;
  699. *lowResImageHeight = cacheHeight / factor;
  700. }
  701. else
  702. {
  703. *lowResImageWidth = cacheWidth;
  704. *lowResImageHeight = cacheHeight;
  705. }
  706. }
  707. else
  708. {
  709. int factor = cacheHeight / LOWRESIMAGE_DIM;
  710. if (factor > 0)
  711. {
  712. *lowResImageHeight = LOWRESIMAGE_DIM;
  713. *lowResImageWidth = cacheWidth / factor;
  714. }
  715. else
  716. {
  717. *lowResImageWidth = cacheWidth;
  718. *lowResImageHeight = cacheHeight;
  719. }
  720. }
  721. // Can end up with a dimension of zero for high aspect ration images.
  722. if( *lowResImageWidth < 1 )
  723. {
  724. *lowResImageWidth = 1;
  725. }
  726. if( *lowResImageHeight < 1 )
  727. {
  728. *lowResImageHeight = 1;
  729. }
  730. *imageFormat = LOWRES_IMAGE_FORMAT;
  731. }
  732. //-----------------------------------------------------------------------------
  733. // This method creates the low-res image and hooks it into the VTF Texture
  734. //-----------------------------------------------------------------------------
  735. static void CreateLowResImage( IVTFTexture *pVTFTexture )
  736. {
  737. int iWidth, iHeight;
  738. ImageFormat imageFormat;
  739. VTFGetLowResImageInfo( pVTFTexture->Width(), pVTFTexture->Height(), &iWidth, &iHeight, &imageFormat );
  740. // Allocate the low-res image data
  741. pVTFTexture->InitLowResImage( iWidth, iHeight, imageFormat );
  742. // Generate the low-res image bits
  743. if (!pVTFTexture->ConstructLowResImage())
  744. {
  745. VTexError( "Can't convert image from %s to %s in CalcLowResImage\n",
  746. ImageLoader::GetName(IMAGE_FORMAT_RGBA8888), ImageLoader::GetName(imageFormat) );
  747. }
  748. }
  749. //-----------------------------------------------------------------------------
  750. // Computes the source file name
  751. //-----------------------------------------------------------------------------
  752. void MakeSrcFileName( char *pSrcName, unsigned int flags, const char *pFullNameWithoutExtension, int frameID,
  753. int faceID, int z, bool isCubeMap, int startFrame, int endFrame, bool bNormalToDUDV )
  754. {
  755. bool bAnimated = !( startFrame == -1 || endFrame == -1 );
  756. char tempBuf[512];
  757. if( bNormalToDUDV )
  758. {
  759. char *pNormalString = Q_stristr( ( char * )pFullNameWithoutExtension, "_dudv" );
  760. if( pNormalString )
  761. {
  762. Q_strncpy( tempBuf, pFullNameWithoutExtension, sizeof(tempBuf) );
  763. char *pNormalString = Q_stristr( tempBuf, "_dudv" );
  764. Q_strcpy( pNormalString, "_normal" );
  765. pFullNameWithoutExtension = tempBuf;
  766. }
  767. else
  768. {
  769. Assert( Q_stristr( ( char * )pFullNameWithoutExtension, "_dudv" ) );
  770. }
  771. }
  772. if( bAnimated )
  773. {
  774. if( isCubeMap )
  775. {
  776. Assert( z == -1 );
  777. sprintf( pSrcName, "%s%s%03d%s", pFullNameWithoutExtension, g_CubemapFacingNames[faceID], frameID + startFrame, GetSourceExtension() );
  778. }
  779. else
  780. {
  781. if ( z == -1 )
  782. {
  783. sprintf( pSrcName, "%s%03d%s", pFullNameWithoutExtension, frameID + startFrame, GetSourceExtension() );
  784. }
  785. else
  786. {
  787. sprintf( pSrcName, "%s%03d_z%03d%s", pFullNameWithoutExtension, z, frameID + startFrame, GetSourceExtension() );
  788. }
  789. }
  790. }
  791. else
  792. {
  793. if( isCubeMap )
  794. {
  795. Assert( z == -1 );
  796. sprintf( pSrcName, "%s%s%s", pFullNameWithoutExtension, g_CubemapFacingNames[faceID], GetSourceExtension() );
  797. }
  798. else
  799. {
  800. if ( z == -1 )
  801. {
  802. sprintf( pSrcName, "%s%s", pFullNameWithoutExtension, GetSourceExtension() );
  803. }
  804. else
  805. {
  806. sprintf( pSrcName, "%s_z%03d%s", pFullNameWithoutExtension, z, GetSourceExtension() );
  807. }
  808. }
  809. }
  810. }
  811. static void ComputeBufferHash( void const *pvBuffer, size_t numBytes, CRC32_t &uiHashUpdate )
  812. {
  813. CRC32_ProcessBuffer( &uiHashUpdate, pvBuffer, numBytes );
  814. }
  815. //-----------------------------------------------------------------------------
  816. // Loads a file into a UTLBuffer,
  817. // also computes the hash of the buffer.
  818. //-----------------------------------------------------------------------------
  819. static bool LoadFile( const char *pFileName, CUtlBuffer &buf, bool bFailOnError, CRC32_t *puiHash )
  820. {
  821. FILE *fp = fopen( pFileName, "rb" );
  822. if (!fp)
  823. {
  824. if ( bFailOnError )
  825. VTexError( "Can't open: \"%s\"\n", pFileName );
  826. return false;
  827. }
  828. fseek( fp, 0, SEEK_END );
  829. int nFileLength = ftell( fp );
  830. fseek( fp, 0, SEEK_SET );
  831. buf.EnsureCapacity( nFileLength );
  832. int nBytesRead = fread( buf.Base(), 1, nFileLength, fp );
  833. fclose( fp );
  834. buf.SeekPut( CUtlBuffer::SEEK_HEAD, nBytesRead );
  835. { CP4AutoAddFile autop4( pFileName ); /* add loaded file to P4 */ }
  836. // Auto-compute buffer hash if necessary
  837. if ( puiHash )
  838. ComputeBufferHash( buf.Base(), nBytesRead, *puiHash );
  839. return true;
  840. }
  841. //-----------------------------------------------------------------------------
  842. // Creates a texture the size of the PSD image stored in the buffer
  843. //-----------------------------------------------------------------------------
  844. static void InitializeSrcTexture_PSD( IVTFTexture *pTexture, const char *pInputFileName,
  845. CUtlBuffer &psdBuffer, int nDepth, int nFrameCount,
  846. const VTexConfigInfo_t &info )
  847. {
  848. int nWidth, nHeight;
  849. ImageFormat imageFormat;
  850. float flSrcGamma;
  851. bool ok = PSDGetInfo( psdBuffer, &nWidth, &nHeight, &imageFormat, &flSrcGamma );
  852. if (!ok)
  853. {
  854. Error( "PSD %s is bogus!\n", pInputFileName );
  855. }
  856. nWidth /= info.m_nReduceX;
  857. nHeight /= info.m_nReduceY;
  858. if (!pTexture->Init( nWidth, nHeight, nDepth, IMAGE_FORMAT_DEFAULT, info.m_nFlags, nFrameCount ))
  859. {
  860. Error( "Error initializing texture %s\n", pInputFileName );
  861. }
  862. }
  863. //-----------------------------------------------------------------------------
  864. // Creates a texture the size of the TGA image stored in the buffer
  865. //-----------------------------------------------------------------------------
  866. static void InitializeSrcTexture_TGA( IVTFTexture *pTexture, const char *pInputFileName,
  867. CUtlBuffer &tgaBuffer, int nDepth, int nFrameCount,
  868. const VTexConfigInfo_t &info )
  869. {
  870. int nWidth, nHeight;
  871. ImageFormat imageFormat;
  872. float flSrcGamma;
  873. bool ok = TGALoader::GetInfo( tgaBuffer, &nWidth, &nHeight, &imageFormat, &flSrcGamma );
  874. if (!ok)
  875. {
  876. Error( "TGA %s is bogus!\n", pInputFileName );
  877. }
  878. nWidth /= info.m_nReduceX;
  879. nHeight /= info.m_nReduceY;
  880. if (!pTexture->Init( nWidth, nHeight, nDepth, IMAGE_FORMAT_DEFAULT, info.m_nFlags, nFrameCount ))
  881. {
  882. Error( "Error initializing texture %s\n", pInputFileName );
  883. }
  884. }
  885. // HDRFIXME: Put this somewhere better than this.
  886. // This reads an integer from a binary CUtlBuffer.
  887. static int ReadIntFromUtlBuffer( CUtlBuffer &buf )
  888. {
  889. int val = 0;
  890. int c;
  891. while( buf.IsValid() )
  892. {
  893. c = buf.GetChar();
  894. if( c >= '0' && c <= '9' )
  895. {
  896. val = val * 10 + ( c - '0' );
  897. }
  898. else
  899. {
  900. buf.SeekGet( CUtlBuffer::SEEK_CURRENT, -1 );
  901. break;
  902. }
  903. }
  904. return val;
  905. }
  906. static inline bool IsWhitespace( char c )
  907. {
  908. return c == ' ' || c == '\t' || c == 10;
  909. }
  910. static void EatWhiteSpace( CUtlBuffer &buf )
  911. {
  912. while( buf.IsValid() )
  913. {
  914. int c = buf.GetChar();
  915. if( !IsWhitespace( c ) )
  916. {
  917. buf.SeekGet( CUtlBuffer::SEEK_CURRENT, -1 );
  918. return;
  919. }
  920. }
  921. return;
  922. }
  923. //-----------------------------------------------------------------------------
  924. // Creates a texture the size of the PFM image stored in the buffer
  925. //-----------------------------------------------------------------------------
  926. static void InitializeSrcTexture_PFM( IVTFTexture *pTexture, const char *pInputFileName,
  927. CUtlBuffer &fileBuffer, int nDepth, int nFrameCount,
  928. const VTexConfigInfo_t &info )
  929. {
  930. fileBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  931. if( fileBuffer.GetChar() != 'P' )
  932. {
  933. Assert( 0 );
  934. return;
  935. }
  936. if( fileBuffer.GetChar() != 'F' )
  937. {
  938. Assert( 0 );
  939. return;
  940. }
  941. if( fileBuffer.GetChar() != 0xa )
  942. {
  943. Assert( 0 );
  944. return;
  945. }
  946. int nWidth, nHeight;
  947. nWidth = ReadIntFromUtlBuffer( fileBuffer );
  948. EatWhiteSpace( fileBuffer );
  949. nHeight = ReadIntFromUtlBuffer( fileBuffer );
  950. // // eat crap until the next newline
  951. // while( fileBuffer.GetChar() != 0xa )
  952. // {
  953. // }
  954. nWidth /= info.m_nReduceX;
  955. nHeight /= info.m_nReduceY;
  956. if (!pTexture->Init( nWidth, nHeight, nDepth, IMAGE_FORMAT_RGB323232F, info.m_nFlags, nFrameCount ))
  957. {
  958. Error( "Error initializing texture %s\n", pInputFileName );
  959. }
  960. }
  961. static void InitializeSrcTexture( IVTFTexture *pTexture, const char *pInputFileName,
  962. CUtlBuffer &tgaBuffer, int nDepth, int nFrameCount,
  963. const VTexConfigInfo_t &info )
  964. {
  965. switch ( g_eMode )
  966. {
  967. case eModePSD:
  968. InitializeSrcTexture_PSD( pTexture, pInputFileName, tgaBuffer, nDepth, nFrameCount, info );
  969. break;
  970. case eModeTGA:
  971. InitializeSrcTexture_TGA( pTexture, pInputFileName, tgaBuffer, nDepth, nFrameCount, info );
  972. break;
  973. case eModePFM:
  974. InitializeSrcTexture_PFM( pTexture, pInputFileName, tgaBuffer, nDepth, nFrameCount, info );
  975. break;
  976. }
  977. }
  978. #define DISTANCE_CODE_ALPHA_INOUT_THRESHOLD 10
  979. //-----------------------------------------------------------------------------
  980. // Loads a face from a PSD image
  981. //-----------------------------------------------------------------------------
  982. static bool LoadFaceFromPSD( IVTFTexture *pTexture, CUtlBuffer &psdBuffer, int z, int nFrame, int nFace, float flGamma, const VTexConfigInfo_t &info )
  983. {
  984. // NOTE: This only works because all mip levels are stored sequentially
  985. // in memory, starting with the highest mip level. It also only works
  986. // because the VTF Texture store *all* mip levels down to 1x1
  987. // Get the information from the file...
  988. int nWidth, nHeight;
  989. ImageFormat imageFormat;
  990. float flSrcGamma;
  991. bool ok = PSDGetInfo( psdBuffer, &nWidth, &nHeight, &imageFormat, &flSrcGamma );
  992. if (!ok)
  993. return false;
  994. // Seek back so PSDLoader can see the psd header...
  995. psdBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  996. // Load the psd and create all mipmap levels
  997. unsigned char *pDestBits = pTexture->ImageData( nFrame, nFace, 0, 0, 0, z );
  998. if ( ( info.m_bAlphaToDistance ) ||
  999. ( nWidth != pTexture->Width() ) ||
  1000. ( nHeight != pTexture->Height() ) )
  1001. {
  1002. // Load into temp
  1003. Bitmap_t bmPsdData;
  1004. ok = PSDReadFileRGBA8888( psdBuffer, bmPsdData );
  1005. if ( !ok )
  1006. return false;
  1007. CUtlMemory<uint8> tmpDest( 0, pTexture->Width() * pTexture->Height() * 4 );
  1008. ImageLoader::ResampleInfo_t resInfo;
  1009. resInfo.m_pSrc = bmPsdData.GetBits();
  1010. resInfo.m_pDest = tmpDest.Base();
  1011. resInfo.m_nSrcWidth = nWidth;
  1012. resInfo.m_nSrcHeight = nHeight;
  1013. resInfo.m_nDestWidth = pTexture->Width();
  1014. resInfo.m_nDestHeight = pTexture->Height();
  1015. resInfo.m_flSrcGamma = flGamma;
  1016. resInfo.m_flDestGamma = flGamma;
  1017. if (info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_FILTER_NICE )
  1018. {
  1019. resInfo.m_nFlags |= ImageLoader::RESAMPLE_NICE_FILTER;
  1020. }
  1021. ResampleRGBA8888( resInfo );
  1022. if ( info.m_bAlphaToDistance )
  1023. {
  1024. float flMaxRad=info.m_flDistanceSpread*2.0*max(info.m_nReduceX,info.m_nReduceY);
  1025. int nSearchRad=ceil(flMaxRad);
  1026. bool bWarnEdges = false;
  1027. // now, do alpha to distance coded stuff
  1028. ImageFormatInfo_t fmtInfo=ImageLoader::ImageFormatInfo( pTexture->Format() );
  1029. if ( fmtInfo.m_NumAlphaBits == 0 )
  1030. {
  1031. VTexWarning( "%s: alpha to distance asked for but no alpha channel.\n", info.m_SrcName );
  1032. }
  1033. else
  1034. {
  1035. for(int x=0; x < pTexture->Width(); x++ )
  1036. {
  1037. for(int y=0; y < pTexture->Height(); y++ )
  1038. {
  1039. // map to original image coords
  1040. int nOrig_x=FLerp(0,nWidth-1,0,pTexture->Width()-1,x);
  1041. int nOrig_y=FLerp(0,nHeight-1,0,pTexture->Height()-1,y);
  1042. uint8 nOrigAlpha = bmPsdData.GetColor(nOrig_x, nOrig_y).a();
  1043. bool bInOrOut=nOrigAlpha > DISTANCE_CODE_ALPHA_INOUT_THRESHOLD;
  1044. float flClosest_Dist=1.0e23;
  1045. for(int iy=-nSearchRad; iy <= nSearchRad; iy++ )
  1046. {
  1047. for(int ix=-nSearchRad; ix <= nSearchRad; ix++ )
  1048. {
  1049. int cx=max( 0, min( nWidth-1, ix + nOrig_x ) );
  1050. int cy=max( 0, min( nHeight-1, iy + nOrig_y ) );
  1051. uint8 alphaValue = bmPsdData.GetColor(cx, cy).a();
  1052. bool bIn =( alphaValue > DISTANCE_CODE_ALPHA_INOUT_THRESHOLD );
  1053. if ( bInOrOut != bIn ) // transition?
  1054. {
  1055. float flTryDist = sqrt( (float) (ix*ix+iy*iy) );
  1056. flClosest_Dist = min( flClosest_Dist, flTryDist );
  1057. }
  1058. }
  1059. }
  1060. // now, map signed distance to alpha value
  1061. float flOutDist = min( 0.5f, FLerp( 0, .5, 0, flMaxRad, flClosest_Dist ) );
  1062. if ( ! bInOrOut )
  1063. {
  1064. // negative distance
  1065. flOutDist = -flOutDist;
  1066. }
  1067. uint8 &nOutAlpha= tmpDest[3+4*(x+pTexture->Width()*y )];
  1068. nOutAlpha = min( 255.0, 255.0*( 0.5+flOutDist ) );
  1069. if ( ( nOutAlpha != 0 ) &&
  1070. (
  1071. ( x == 0 ) ||
  1072. ( y == 0 ) ||
  1073. ( x == pTexture->Width()-1 ) ||
  1074. ( y == pTexture->Height()-1 ) ) )
  1075. {
  1076. bWarnEdges = true;
  1077. nOutAlpha = 0; // force it.
  1078. }
  1079. }
  1080. }
  1081. }
  1082. if ( bWarnEdges )
  1083. {
  1084. VTexWarning( "%s: There are non-zero distance pixels along the image edge. You may need"
  1085. " to reduce your distance spread or reduce the image less"
  1086. " or add a border to the image.\n",
  1087. info.m_SrcName );
  1088. }
  1089. }
  1090. // now, store in dest
  1091. ImageLoader::ConvertImageFormat( tmpDest.Base(), IMAGE_FORMAT_RGBA8888, pDestBits,
  1092. pTexture->Format(), pTexture->Width(), pTexture->Height(),
  1093. 0, 0 );
  1094. return true;
  1095. }
  1096. else
  1097. {
  1098. // Read the PSD file into a bitmap
  1099. Bitmap_t bmPsdData;
  1100. ok = PSDReadFileRGBA8888( psdBuffer, bmPsdData );
  1101. if ( ok )
  1102. {
  1103. memcpy( pDestBits, bmPsdData.GetBits(), bmPsdData.Height() * bmPsdData.Stride() );
  1104. }
  1105. return ok;
  1106. }
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. // Loads a face from a TGA image
  1110. //-----------------------------------------------------------------------------
  1111. static bool LoadFaceFromTGA( IVTFTexture *pTexture, CUtlBuffer &tgaBuffer, int z, int nFrame, int nFace, float flGamma, const VTexConfigInfo_t &info )
  1112. {
  1113. // NOTE: This only works because all mip levels are stored sequentially
  1114. // in memory, starting with the highest mip level. It also only works
  1115. // because the VTF Texture store *all* mip levels down to 1x1
  1116. // Get the information from the file...
  1117. int nWidth, nHeight;
  1118. ImageFormat imageFormat;
  1119. float flSrcGamma;
  1120. bool ok = TGALoader::GetInfo( tgaBuffer, &nWidth, &nHeight, &imageFormat, &flSrcGamma );
  1121. if (!ok)
  1122. return false;
  1123. // Seek back so TGALoader::Load can see the tga header...
  1124. tgaBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  1125. // Load the tga and create all mipmap levels
  1126. unsigned char *pDestBits = pTexture->ImageData( nFrame, nFace, 0, 0, 0, z );
  1127. if ( ( info.m_bAlphaToDistance ) ||
  1128. ( nWidth != pTexture->Width() ) ||
  1129. ( nHeight != pTexture->Height() ) )
  1130. {
  1131. // load into temp and resample
  1132. CUtlMemory<uint8> tmpImage( 0, nWidth*nHeight*4 );
  1133. if ( ! TGALoader::Load( tmpImage.Base(), tgaBuffer, nWidth,
  1134. nHeight, IMAGE_FORMAT_RGBA8888, flGamma, false ) )
  1135. {
  1136. return false;
  1137. }
  1138. CUtlMemory<uint8> tmpDest( 0, pTexture->Width() * pTexture->Height() *4 );
  1139. ImageLoader::ResampleInfo_t resInfo;
  1140. resInfo.m_pSrc = tmpImage.Base();
  1141. resInfo.m_pDest = tmpDest.Base();
  1142. resInfo.m_nSrcWidth = nWidth;
  1143. resInfo.m_nSrcHeight = nHeight;
  1144. resInfo.m_nDestWidth = pTexture->Width();
  1145. resInfo.m_nDestHeight = pTexture->Height();
  1146. resInfo.m_flSrcGamma = flGamma;
  1147. resInfo.m_flDestGamma = flGamma;
  1148. if (info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_FILTER_NICE )
  1149. {
  1150. resInfo.m_nFlags |= ImageLoader::RESAMPLE_NICE_FILTER;
  1151. }
  1152. ResampleRGBA8888( resInfo );
  1153. if ( info.m_bAlphaToDistance )
  1154. {
  1155. float flMaxRad=info.m_flDistanceSpread*2.0*max(info.m_nReduceX,info.m_nReduceY);
  1156. int nSearchRad=ceil(flMaxRad);
  1157. bool bWarnEdges = false;
  1158. // now, do alpha to distance coded stuff
  1159. ImageFormatInfo_t fmtInfo=ImageLoader::ImageFormatInfo( pTexture->Format() );
  1160. if ( fmtInfo.m_NumAlphaBits == 0 )
  1161. {
  1162. VTexWarning( "%s: alpha to distance asked for but no alpha channel.\n", info.m_SrcName );
  1163. }
  1164. else
  1165. {
  1166. for(int x=0; x < pTexture->Width(); x++ )
  1167. {
  1168. for(int y=0; y < pTexture->Height(); y++ )
  1169. {
  1170. // map to original image coords
  1171. int nOrig_x=FLerp(0,nWidth-1,0,pTexture->Width()-1,x);
  1172. int nOrig_y=FLerp(0,nHeight-1,0,pTexture->Height()-1,y);
  1173. uint8 nOrigAlpha=tmpImage[3+4*(nOrig_x+nWidth*nOrig_y)];
  1174. bool bInOrOut=nOrigAlpha > DISTANCE_CODE_ALPHA_INOUT_THRESHOLD;
  1175. float flClosest_Dist=1.0e23;
  1176. for(int iy=-nSearchRad; iy <= nSearchRad; iy++ )
  1177. {
  1178. for(int ix=-nSearchRad; ix <= nSearchRad; ix++ )
  1179. {
  1180. int cx=max( 0, min( nWidth-1, ix + nOrig_x ) );
  1181. int cy=max( 0, min( nHeight-1, iy + nOrig_y ) );
  1182. int nOffset = 3+ 4 * ( cx + cy * nWidth );
  1183. uint8 alphaValue = tmpImage[nOffset];
  1184. bool bIn =( alphaValue > DISTANCE_CODE_ALPHA_INOUT_THRESHOLD );
  1185. if ( bInOrOut != bIn ) // transition?
  1186. {
  1187. float flTryDist = sqrt( (float) (ix*ix+iy*iy) );
  1188. flClosest_Dist = min( flClosest_Dist, flTryDist );
  1189. }
  1190. }
  1191. }
  1192. // now, map signed distance to alpha value
  1193. float flOutDist = min( 0.5f, FLerp( 0, .5, 0, flMaxRad, flClosest_Dist ) );
  1194. if ( ! bInOrOut )
  1195. {
  1196. // negative distance
  1197. flOutDist = -flOutDist;
  1198. }
  1199. uint8 &nOutAlpha= tmpDest[3+4*(x+pTexture->Width()*y )];
  1200. nOutAlpha = min( 255.0, 255.0*( 0.5+flOutDist ) );
  1201. if ( ( nOutAlpha != 0 ) &&
  1202. (
  1203. ( x == 0 ) ||
  1204. ( y == 0 ) ||
  1205. ( x == pTexture->Width()-1 ) ||
  1206. ( y == pTexture->Height()-1 ) ) )
  1207. {
  1208. bWarnEdges = true;
  1209. nOutAlpha = 0; // force it.
  1210. }
  1211. }
  1212. }
  1213. }
  1214. if ( bWarnEdges )
  1215. {
  1216. VTexWarning( "%s: There are non-zero distance pixels along the image edge. You may need"
  1217. " to reduce your distance spread or reduce the image less"
  1218. " or add a border to the image.\n",
  1219. info.m_SrcName );
  1220. }
  1221. }
  1222. // now, store in dest
  1223. ImageLoader::ConvertImageFormat( tmpDest.Base(), IMAGE_FORMAT_RGBA8888, pDestBits,
  1224. pTexture->Format(), pTexture->Width(), pTexture->Height(),
  1225. 0, 0 );
  1226. return true;
  1227. }
  1228. else
  1229. {
  1230. return TGALoader::Load( pDestBits, tgaBuffer, pTexture->Width(),
  1231. pTexture->Height(), pTexture->Format(), flGamma, false );
  1232. }
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. // Loads a face from a PFM image
  1236. //-----------------------------------------------------------------------------
  1237. // HDRFIXME: How is this different from InitializeSrcTexture_PFM?
  1238. static bool LoadFaceFromPFM( IVTFTexture *pTexture, CUtlBuffer &fileBuffer, int z, int nFrame,
  1239. int nFace, float flGamma, const VTexConfigInfo_t &info )
  1240. {
  1241. fileBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  1242. if( fileBuffer.GetChar() != 'P' )
  1243. {
  1244. Assert( 0 );
  1245. return false;
  1246. }
  1247. if( fileBuffer.GetChar() != 'F' )
  1248. {
  1249. Assert( 0 );
  1250. return false;
  1251. }
  1252. if( fileBuffer.GetChar() != 0xa )
  1253. {
  1254. Assert( 0 );
  1255. return false;
  1256. }
  1257. int nWidth, nHeight;
  1258. nWidth = ReadIntFromUtlBuffer( fileBuffer );
  1259. EatWhiteSpace( fileBuffer );
  1260. nHeight = ReadIntFromUtlBuffer( fileBuffer );
  1261. // eat crap until the next newline
  1262. while( fileBuffer.IsValid() && fileBuffer.GetChar() != 0xa )
  1263. {
  1264. }
  1265. // eat crap until the next newline
  1266. while( fileBuffer.IsValid() && fileBuffer.GetChar() != 0xa )
  1267. {
  1268. }
  1269. Assert( ImageLoader::SizeInBytes( pTexture->Format() ) == 3 * sizeof( float ) );
  1270. // Load the pfm and create all mipmap levels
  1271. float *pDestBits = ( float * )pTexture->ImageData( nFrame, nFace, 0, 0, 0, z );
  1272. int y;
  1273. for( y = nHeight-1; y >= 0; y-- )
  1274. {
  1275. Assert( fileBuffer.IsValid() );
  1276. fileBuffer.Get( pDestBits + y * nWidth * 3, nWidth * 3 * sizeof( float ) );
  1277. for(int x=0;x<nWidth*3;x++)
  1278. pDestBits[x+y*nWidth*3]*=info.m_pfmscale;
  1279. }
  1280. return true;
  1281. }
  1282. static bool LoadFaceFromX( IVTFTexture *pTexture, CUtlBuffer &tgaBuffer, int z, int nFrame, int nFace,
  1283. float flGamma, const VTexConfigInfo_t &info )
  1284. {
  1285. switch ( g_eMode )
  1286. {
  1287. case eModePSD:
  1288. return LoadFaceFromPSD( pTexture, tgaBuffer, z, nFrame, nFace, flGamma, info );
  1289. break;
  1290. case eModeTGA:
  1291. return LoadFaceFromTGA( pTexture, tgaBuffer, z, nFrame, nFace, flGamma, info );
  1292. break;
  1293. case eModePFM:
  1294. return LoadFaceFromPFM( pTexture, tgaBuffer, z, nFrame, nFace, flGamma, info );
  1295. break;
  1296. default:
  1297. return false;
  1298. }
  1299. }
  1300. static bool LoadFace( IVTFTexture *pTexture, CUtlBuffer &tgaBuffer, int z, int nFrame, int nFace,
  1301. float flGamma, const VTexConfigInfo_t &info )
  1302. {
  1303. if ( !LoadFaceFromX( pTexture, tgaBuffer, z, nFrame, nFace, flGamma, info ) )
  1304. return false;
  1305. // Restricting number of channels by painting white into the rest
  1306. if ( info.m_numChannelsMax < 1 || info.m_numChannelsMax > 4 )
  1307. {
  1308. VTexWarning( "%s: Invalid setting restricting number of channels to %d, discarded!\n", info.m_SrcName, info.m_numChannelsMax );
  1309. }
  1310. else if ( info.m_numChannelsMax < 4 )
  1311. {
  1312. if ( 4 != ImageLoader::SizeInBytes( pTexture->Format() ) )
  1313. {
  1314. VTexWarning( "%s: Channels restricted to %d, but cannot fill white"
  1315. " because pixel format is %d (size in bytes %d)!"
  1316. " Proceeding with unmodified channels.\n",
  1317. info.m_SrcName,
  1318. info.m_numChannelsMax, pTexture->Format(), ImageLoader::SizeInBytes( pTexture->Format() ) );
  1319. Assert( 0 );
  1320. }
  1321. else
  1322. {
  1323. // Fill other channels with white
  1324. unsigned char *pDestBits = pTexture->ImageData( nFrame, nFace, 0, 0, 0, z );
  1325. int nWidth = pTexture->Width();
  1326. int nHeight = pTexture->Height();
  1327. int nPaintOff = info.m_numChannelsMax;
  1328. int nPaintBytes = 4 - nPaintOff;
  1329. pDestBits += nPaintOff;
  1330. for( int j = 0; j < nHeight; ++ j )
  1331. {
  1332. for ( int k = 0; k < nWidth; ++ k, pDestBits += 4 )
  1333. {
  1334. memset( pDestBits, 0xFF, nPaintBytes );
  1335. }
  1336. }
  1337. }
  1338. }
  1339. return true;
  1340. }
  1341. //-----------------------------------------------------------------------------
  1342. // Loads source image data
  1343. //-----------------------------------------------------------------------------
  1344. static bool LoadSourceImages( IVTFTexture *pTexture, const char *pFullNameWithoutExtension,
  1345. bool *pbGenerateSphereMaps,
  1346. VTexConfigInfo_t &info )
  1347. {
  1348. static char pSrcName[1024];
  1349. bool bGenerateSpheremaps = false;
  1350. // The input file name here is simply for error reporting
  1351. char *pInputFileName = ( char * )stackalloc( strlen( pFullNameWithoutExtension ) + strlen( GetSourceExtension() ) + 1 );
  1352. strcpy( pInputFileName, pFullNameWithoutExtension );
  1353. strcat( pInputFileName, GetSourceExtension() );
  1354. int nFrameCount;
  1355. bool bAnimated = !( info.m_nStartFrame == -1 || info.m_nEndFrame == -1 );
  1356. if( !bAnimated )
  1357. {
  1358. nFrameCount = 1;
  1359. }
  1360. else
  1361. {
  1362. nFrameCount = info.m_nEndFrame - info.m_nStartFrame + 1;
  1363. }
  1364. bool bIsCubeMap = (info.m_nFlags & TEXTUREFLAGS_ENVMAP) != 0;
  1365. bool bIsVolumeTexture = ( info.m_nVolumeTextureDepth > 1 );
  1366. // Iterate over all faces of all frames
  1367. int nFaceCount = bIsCubeMap ? CUBEMAP_FACE_COUNT : 1;
  1368. for( int iFrame = 0; iFrame < nFrameCount; ++iFrame )
  1369. {
  1370. for( int iFace = 0; iFace < nFaceCount; ++iFace )
  1371. {
  1372. for ( int z = 0; z < info.m_nVolumeTextureDepth; ++z )
  1373. {
  1374. // Generate the filename to load....
  1375. MakeSrcFileName( pSrcName, info.m_nFlags, pFullNameWithoutExtension,
  1376. iFrame, iFace, bIsVolumeTexture ? z : -1, bIsCubeMap, info.m_nStartFrame, info.m_nEndFrame, info.m_bNormalToDuDv );
  1377. // Don't fail if the 7th iFace of a cubemap isn't loaded...
  1378. // that just means that we're gonna have to build the spheremap ourself.
  1379. bool bFailOnError = !bIsCubeMap || (iFace != CUBEMAP_FACE_SPHEREMAP);
  1380. // Load the TGA from disk...
  1381. CUtlBuffer tgaBuffer;
  1382. if ( !LoadFile( pSrcName, tgaBuffer, bFailOnError,
  1383. ( g_eMode != eModePSD ) ? &info.m_uiInputHash : NULL ) )
  1384. {
  1385. // If we want to fail on error and VTexError didn't abort then
  1386. // simply notify the caller that we failed
  1387. if ( bFailOnError )
  1388. return false;
  1389. // The only other way we can get here is if LoadFile tried to load a spheremap and failed
  1390. bGenerateSpheremaps = true;
  1391. continue;
  1392. }
  1393. // Initialize the VTF Texture here if we haven't already....
  1394. // Note that we have to do it here because we have to get the width + height from the file
  1395. if (!pTexture->ImageData())
  1396. {
  1397. InitializeSrcTexture( pTexture, pSrcName, tgaBuffer, info.m_nVolumeTextureDepth, nFrameCount, info );
  1398. // Re-seek back to the front of the buffer so LoadFaceFromTGA works
  1399. tgaBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  1400. }
  1401. strcpy( info.m_SrcName, pSrcName );
  1402. // NOTE: This here will generate all mip levels of the source image
  1403. if (!LoadFace( pTexture, tgaBuffer, z, iFrame, iFace, 2.2, info ))
  1404. {
  1405. Error( "Error loading texture %s\n", pInputFileName );
  1406. }
  1407. }
  1408. }
  1409. }
  1410. if ( pbGenerateSphereMaps )
  1411. {
  1412. *pbGenerateSphereMaps = bGenerateSpheremaps;
  1413. }
  1414. return true;
  1415. }
  1416. void PreprocessSkyBox( char *pFullNameWithoutExtension, int *iSkyboxFace )
  1417. {
  1418. // When we get here, it means that we're processing one face of a skybox, but we're going to
  1419. // load all the faces and treat it as a cubemap so we can do the edge matching.
  1420. // Since they passed in only one face of the skybox, there's a 2 letter extension we want to get rid of.
  1421. int len = strlen( pFullNameWithoutExtension );
  1422. if ( len >= 3 )
  1423. {
  1424. // Make sure there really is a 2 letter extension.
  1425. char *pEnd = &pFullNameWithoutExtension[ len - 2 ];
  1426. *iSkyboxFace = -1;
  1427. for ( int i=0; i < ARRAYSIZE( g_CubemapFacingNames ); i++ )
  1428. {
  1429. if ( stricmp( pEnd, g_CubemapFacingNames[i] ) == 0 )
  1430. {
  1431. *iSkyboxFace = i;
  1432. break;
  1433. }
  1434. }
  1435. // Cut off the 2 letter extension.
  1436. if ( *iSkyboxFace != -1 )
  1437. {
  1438. pEnd[0] = 0;
  1439. return;
  1440. }
  1441. }
  1442. Error( "PreprocessSkyBox: filename %s doesn't have a proper extension (bk, dn, rt, etc..)\n", pFullNameWithoutExtension );
  1443. }
  1444. // Right now, we've got a full cubemap, and we want to return the one face of the
  1445. // skybox that we're supposed to be processing.
  1446. IVTFTexture* PostProcessSkyBox( IVTFTexture *pTexture, int iSkyboxFace )
  1447. {
  1448. int nFlags = pTexture->Flags();
  1449. Assert( nFlags & TEXTUREFLAGS_ENVMAP ); // Should have been treated as an envmap till now.
  1450. nFlags &= ~TEXTUREFLAGS_ENVMAP; // But it ends now!
  1451. IVTFTexture *pRet = CreateVTFTexture();
  1452. if ( !pRet->Init( pTexture->Width(), pTexture->Height(), 1, pTexture->Format(), nFlags, pTexture->FrameCount() ) )
  1453. Error( "PostProcessSkyBox: IVTFTexture::Init() failed.\n" );
  1454. // Now just dump the data for the face we want to keep.
  1455. int nMips = min( pTexture->MipCount(), pRet->MipCount() );
  1456. for ( int iMip=0; iMip < nMips; iMip++ )
  1457. {
  1458. int mipSize = pTexture->ComputeMipSize( iMip );
  1459. if ( pRet->ComputeMipSize( iMip ) != mipSize )
  1460. {
  1461. Error( "PostProcessSkyBox: ComputeMipSize differs (src=%d, dest=%d)\n", mipSize, pRet->ComputeMipSize( iMip ) );
  1462. }
  1463. for ( int iFrame=0; iFrame < pTexture->FrameCount(); iFrame++ )
  1464. {
  1465. unsigned char *pDest = pRet->ImageData( iFrame, 0, iMip );
  1466. const unsigned char *pSrc = pTexture->ImageData( iFrame, iSkyboxFace, iMip );
  1467. memcpy( pDest, pSrc, mipSize );
  1468. }
  1469. }
  1470. // Note: there are a few things that don't get copied here, like alpha test threshold
  1471. // and bumpscale, but we shouldn't need those for skyboxes anyway.
  1472. // Get rid of the full cubemap one and return the single-face one.
  1473. DestroyVTFTexture( pTexture );
  1474. return pRet;
  1475. }
  1476. void MakeDirHier( const char *pPath )
  1477. {
  1478. #ifdef POSIX
  1479. #define mkdir(s) mkdir(s, S_IRWXU | S_IRWXG | S_IRWXO )
  1480. #endif
  1481. char temp[1024];
  1482. Q_strncpy( temp, pPath, 1024 );
  1483. int i;
  1484. for( i = 0; i < strlen( temp ); i++ )
  1485. {
  1486. if( temp[i] == '/' || temp[i] == '\\' )
  1487. {
  1488. temp[i] = '\0';
  1489. // DebugOut( "mkdir( %s )\n", temp );
  1490. mkdir( temp );
  1491. temp[i] = CORRECT_PATH_SEPARATOR;
  1492. }
  1493. }
  1494. // DebugOut( "mkdir( %s )\n", temp );
  1495. mkdir( temp );
  1496. }
  1497. static uint8 GetClampingValue( int nClampSize )
  1498. {
  1499. if ( nClampSize <= 0 )
  1500. return 30; // ~1 billion
  1501. int nRet = 0;
  1502. while ( nClampSize > 1 )
  1503. {
  1504. nClampSize >>= 1;
  1505. nRet++;
  1506. }
  1507. return nRet;
  1508. }
  1509. static void SetTextureLodData( IVTFTexture *pTexture, VTexConfigInfo_t const &info )
  1510. {
  1511. if (
  1512. ( info.m_nMaxDimensionX > 0 && info.m_nMaxDimensionX < pTexture->Width() ) ||
  1513. ( info.m_nMaxDimensionY > 0 && info.m_nMaxDimensionY < pTexture->Height() ) ||
  1514. ( info.m_nMaxDimensionX_360 > 0 && info.m_nMaxDimensionX_360 < pTexture->Width() ) ||
  1515. ( info.m_nMaxDimensionY_360 > 0 && info.m_nMaxDimensionY_360 < pTexture->Height() )
  1516. )
  1517. {
  1518. TextureLODControlSettings_t lodChunk;
  1519. memset( &lodChunk, 0, sizeof( lodChunk ) );
  1520. lodChunk.m_ResolutionClampX = GetClampingValue( info.m_nMaxDimensionX );
  1521. lodChunk.m_ResolutionClampY = GetClampingValue( info.m_nMaxDimensionY );
  1522. lodChunk.m_ResolutionClampX_360 = GetClampingValue( info.m_nMaxDimensionX_360 );
  1523. lodChunk.m_ResolutionClampY_360 = GetClampingValue( info.m_nMaxDimensionY_360 );
  1524. pTexture->SetResourceData( VTF_RSRC_TEXTURE_LOD_SETTINGS, &lodChunk, sizeof( lodChunk ) );
  1525. }
  1526. }
  1527. static void AttachShtFile( const char *pFullNameWithoutExtension, IVTFTexture *pTexture, CRC32_t *puiHash )
  1528. {
  1529. char shtName[MAX_PATH];
  1530. Q_strncpy( shtName, pFullNameWithoutExtension, sizeof(shtName) );
  1531. Q_SetExtension( shtName, ".sht", sizeof(shtName) );
  1532. struct _stat statBuf;
  1533. if( _stat( shtName, &statBuf ) == -1 )
  1534. return;
  1535. printf( "Attaching .sht file %s.\n", shtName );
  1536. // Ok, the file exists. Read it.
  1537. CUtlBuffer buf;
  1538. if ( !LoadFile( shtName, buf, false, puiHash ) )
  1539. return;
  1540. pTexture->SetResourceData( VTF_RSRC_SHEET, buf.Base(), buf.TellPut() );
  1541. }
  1542. //-----------------------------------------------------------------------------
  1543. // Does the dirty deed and generates a VTF file
  1544. //-----------------------------------------------------------------------------
  1545. bool ProcessFiles( const char *pFullNameWithoutExtension,
  1546. const char *pOutputDir, const char *pBaseName,
  1547. bool isCubeMap, VTexConfigInfo_t &info )
  1548. {
  1549. // force clamps/clampt for cube maps
  1550. if( isCubeMap )
  1551. {
  1552. info.m_nFlags |= TEXTUREFLAGS_ENVMAP;
  1553. info.m_nFlags |= TEXTUREFLAGS_CLAMPS;
  1554. info.m_nFlags |= TEXTUREFLAGS_CLAMPT;
  1555. }
  1556. // Create the texture we're gonna store out
  1557. SmartIVtfTexture pVTFTexture( CreateVTFTexture() );
  1558. int iSkyboxFace = 0;
  1559. char fullNameTemp[512];
  1560. if ( info.m_bSkyBox )
  1561. {
  1562. Q_strncpy( fullNameTemp, pFullNameWithoutExtension, sizeof( fullNameTemp ) );
  1563. pFullNameWithoutExtension = fullNameTemp;
  1564. PreprocessSkyBox( fullNameTemp, &iSkyboxFace );
  1565. }
  1566. // Load the source images into the texture
  1567. bool bGenerateSpheremaps = false;
  1568. bool bLoadedSourceImages = LoadSourceImages( pVTFTexture.Get(),
  1569. pFullNameWithoutExtension, &bGenerateSpheremaps, info );
  1570. if ( !bLoadedSourceImages )
  1571. {
  1572. VTexError( "Can't load source images for \"%s\"\n", pFullNameWithoutExtension );
  1573. return false;
  1574. }
  1575. // Attach a sheet file if present
  1576. AttachShtFile( pFullNameWithoutExtension, pVTFTexture.Get(), &info.m_uiInputHash );
  1577. // No more file loads, finalize the sources hash
  1578. CRC32_Final( &info.m_uiInputHash );
  1579. pVTFTexture->SetResourceData( VTexConfigInfo_t::VTF_INPUTSRC_CRC, &info.m_uiInputHash, sizeof( info.m_uiInputHash ) );
  1580. CRC32_t crcWritten = info.m_uiInputHash;
  1581. // Name of the destination file
  1582. char dstFileName[1024];
  1583. sprintf( dstFileName, "%s/%s%s.vtf", pOutputDir, pBaseName, ( ( eModePFM == g_eMode ) && isCubeMap ) ? ".hdr" : "" );
  1584. // Now if we are only validating the CRC
  1585. if( CommandLine()->FindParm( "-crcvalidate" ) )
  1586. {
  1587. CUtlBuffer bufFile;
  1588. bool bLoad = LoadFile( dstFileName, bufFile, false, NULL );
  1589. if ( !bLoad )
  1590. {
  1591. fprintf( stderr, "LOAD ERROR: %s\n", dstFileName );
  1592. return false;
  1593. }
  1594. SmartIVtfTexture spExistingVtf( CreateVTFTexture() );
  1595. bLoad = spExistingVtf->Unserialize( bufFile );
  1596. if ( !bLoad )
  1597. {
  1598. fprintf( stderr, "UNSERIALIZE ERROR: %s\n", dstFileName );
  1599. return false;
  1600. }
  1601. size_t numDataBytes;
  1602. void *pCrcData = spExistingVtf->GetResourceData( VTexConfigInfo_t::VTF_INPUTSRC_CRC, &numDataBytes );
  1603. if ( !pCrcData || numDataBytes != sizeof( CRC32_t ) )
  1604. {
  1605. fprintf( stderr, "OLD TEXTURE FORMAT: %s\n", dstFileName );
  1606. return false;
  1607. }
  1608. CRC32_t crcFile = * reinterpret_cast< CRC32_t const * >( pCrcData );
  1609. if ( crcFile != crcWritten )
  1610. {
  1611. fprintf( stderr, "CRC MISMATCH: %s\n", dstFileName );
  1612. return false;
  1613. }
  1614. fprintf( stderr, "OK: %s\n", dstFileName );
  1615. return true;
  1616. }
  1617. // Now if we are not forcing the CRC
  1618. if( !CommandLine()->FindParm( "-crcforce" ) )
  1619. {
  1620. CUtlBuffer bufFile;
  1621. if ( LoadFile( dstFileName, bufFile, false, NULL ) )
  1622. {
  1623. SmartIVtfTexture spExistingVtf( CreateVTFTexture() );
  1624. if ( spExistingVtf->Unserialize( bufFile ) )
  1625. {
  1626. size_t numDataBytes;
  1627. void *pCrcData = spExistingVtf->GetResourceData( VTexConfigInfo_t::VTF_INPUTSRC_CRC, &numDataBytes );
  1628. if ( pCrcData && numDataBytes == sizeof( CRC32_t ) )
  1629. {
  1630. CRC32_t crcFile = * reinterpret_cast< CRC32_t const * >( pCrcData );
  1631. if ( crcFile == crcWritten )
  1632. {
  1633. if( !g_Quiet )
  1634. printf( "SUCCESS: %s is up-to-date\n", dstFileName );
  1635. if( !CommandLine()->FindParm( "-crcforce" ) )
  1636. return true;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. }
  1642. // Bumpmap scale..
  1643. pVTFTexture->SetBumpScale( info.m_flBumpScale );
  1644. // Alphatest threshhold
  1645. pVTFTexture->SetAlphaTestThreshholds( info.m_flAlphaThreshhold, info.m_flAlphaHiFreqThreshhold );
  1646. // Set texture lod data
  1647. SetTextureLodData( pVTFTexture.Get(), info );
  1648. // Get the texture all internally consistent and happy
  1649. bool bAllowFixCubemapOrientation = !info.m_bSkyBox; // Don't let it rotate our pseudo-cubemap faces around if it's a skybox.
  1650. pVTFTexture->SetPostProcessingSettings( &info.m_vtfProcOptions );
  1651. pVTFTexture->PostProcess( bGenerateSpheremaps, info.m_LookDir, bAllowFixCubemapOrientation );
  1652. // Compute the preferred image format
  1653. ImageFormat vtfImageFormat = ComputeDesiredImageFormat( pVTFTexture.Get(), info );
  1654. // Set up the low-res image
  1655. if (pVTFTexture->IsCubeMap())
  1656. {
  1657. // "Stage 1" of matching cubemap borders. Sometimes, it has to store off the original image.
  1658. pVTFTexture->MatchCubeMapBorders( 1, vtfImageFormat, info.m_bSkyBox );
  1659. }
  1660. else
  1661. {
  1662. CreateLowResImage( pVTFTexture.Get() );
  1663. }
  1664. // Convert to the final format
  1665. pVTFTexture->ConvertImageFormat( vtfImageFormat, info.m_bNormalToDuDv );
  1666. // Stage 2 of matching cubemap borders.
  1667. pVTFTexture->MatchCubeMapBorders( 2, vtfImageFormat, info.m_bSkyBox );
  1668. if ( info.m_bSkyBox )
  1669. {
  1670. pVTFTexture.Assign( PostProcessSkyBox( pVTFTexture.Get(), iSkyboxFace ) );
  1671. }
  1672. if ( info.IsSettings0Valid() )
  1673. {
  1674. pVTFTexture->SetResourceData( VTF_RSRC_TEXTURE_SETTINGS_EX, &info.m_exSettings0, sizeof( info.m_exSettings0 ) );
  1675. }
  1676. // Write it!
  1677. if ( g_CreateDir == true )
  1678. MakeDirHier( pOutputDir ); //It'll create it if it doesn't exist.
  1679. // Make sure the CRC hasn't been modified since finalized
  1680. Assert( crcWritten == info.m_uiInputHash );
  1681. CUtlBuffer outputBuf;
  1682. if (!pVTFTexture->Serialize( outputBuf ))
  1683. {
  1684. VTexError( "ERROR: \"%s\": Unable to serialize the VTF file!\n", dstFileName );
  1685. }
  1686. {
  1687. CP4AutoEditAddFile autop4( dstFileName );
  1688. FILE *fp = fopen( dstFileName, "wb" );
  1689. if( !fp )
  1690. {
  1691. VTexError( "Can't open: %s\n", dstFileName );
  1692. }
  1693. fwrite( outputBuf.Base(), 1, outputBuf.TellPut(), fp );
  1694. fclose( fp );
  1695. }
  1696. printf("SUCCESS: Vtf file created\n");
  1697. return true;
  1698. }
  1699. const char *GetPossiblyQuotedWord( const char *pInBuf, char *pOutbuf )
  1700. {
  1701. pInBuf += strspn( pInBuf, " \t" ); // skip whitespace
  1702. const char *pWordEnd;
  1703. bool bQuote = false;
  1704. if (pInBuf[0]=='"')
  1705. {
  1706. pInBuf++;
  1707. pWordEnd=strchr(pInBuf,'"');
  1708. bQuote = true;
  1709. }
  1710. else
  1711. {
  1712. pWordEnd=strchr(pInBuf,' ');
  1713. if (! pWordEnd )
  1714. pWordEnd = strchr(pInBuf,'\t' );
  1715. if (! pWordEnd )
  1716. pWordEnd = pInBuf+strlen(pInBuf);
  1717. }
  1718. if ((! pWordEnd ) || (pWordEnd == pInBuf ) )
  1719. return NULL; // no word found
  1720. memcpy( pOutbuf, pInBuf, pWordEnd-pInBuf );
  1721. pOutbuf[pWordEnd-pInBuf]=0;
  1722. pInBuf = pWordEnd;
  1723. if ( bQuote )
  1724. pInBuf++;
  1725. return pInBuf;
  1726. }
  1727. // GetKeyValueFromBuffer:
  1728. // fills in "key" and "val" respectively and returns "true" if succeeds.
  1729. // returns false if:
  1730. // a) end-of-buffer is reached (then "val" is empty)
  1731. // b) error occurs (then "val" is the error message)
  1732. //
  1733. static bool GetKeyValueFromBuffer( CUtlBuffer &buffer, char *key, char *val )
  1734. {
  1735. char buf[2048];
  1736. while( buffer.GetBytesRemaining() )
  1737. {
  1738. buffer.GetLine( buf, sizeof( buf ) );
  1739. // Scanning algorithm
  1740. char *pComment = strpbrk( buf, "#\n\r" );
  1741. if ( pComment )
  1742. *pComment = 0;
  1743. pComment = strstr( buf, "//" );
  1744. if ( pComment)
  1745. *pComment = 0;
  1746. const char *scan = buf;
  1747. scan=GetPossiblyQuotedWord( scan, key );
  1748. if ( scan )
  1749. {
  1750. scan=GetPossiblyQuotedWord( scan, val );
  1751. if ( scan )
  1752. return true;
  1753. else
  1754. {
  1755. sprintf( val, "parameter %s has no value", key );
  1756. return false;
  1757. }
  1758. }
  1759. }
  1760. val[0] = 0;
  1761. return false;
  1762. }
  1763. //-----------------------------------------------------------------------------
  1764. // Loads the .psd file or .txt file associated with the .tga and gets out various data
  1765. //-----------------------------------------------------------------------------
  1766. static bool LoadConfigFile( const char *pFileBaseName, VTexConfigInfo_t &info, bool *isCubeMap )
  1767. {
  1768. // Tries to load .txt, then .psd
  1769. int lenBaseName = strlen( pFileBaseName );
  1770. char *pFileName = ( char * )stackalloc( lenBaseName + strlen( ".tga" ) + 1 );
  1771. strcpy( pFileName, pFileBaseName );
  1772. strcat( pFileName, ".tga" );
  1773. bool bOK = false;
  1774. info.m_LookDir = LOOK_DOWN_Z;
  1775. // Try TGA file with config
  1776. memcpy( pFileName + lenBaseName, ".tga", 4 );
  1777. if ( !bOK && !g_bNoTga && ( 00 == access( pFileName, 00 ) ) ) // TGA file exists
  1778. {
  1779. g_eMode = eModeTGA;
  1780. memcpy( pFileName + lenBaseName, ".txt", 4 );
  1781. CUtlBuffer bufFile( 0, 0, CUtlBuffer::TEXT_BUFFER );
  1782. bOK = LoadFile( pFileName, bufFile, false, &info.m_uiInputHash );
  1783. if ( bOK )
  1784. {
  1785. printf("config file %s\n",pFileName);
  1786. {
  1787. char key[2048];
  1788. char val[2048];
  1789. while( GetKeyValueFromBuffer( bufFile, key, val ) )
  1790. {
  1791. info.ParseOptionKey( key, val );
  1792. }
  1793. if ( val[0] )
  1794. {
  1795. VTexError( "%s: %s\n", pFileName, val );
  1796. return false;
  1797. }
  1798. }
  1799. }
  1800. else
  1801. {
  1802. memcpy( pFileName + lenBaseName, ".tga", 4 );
  1803. printf("no config file for %s\n",pFileName);
  1804. bOK = true;
  1805. }
  1806. }
  1807. memcpy( pFileName + lenBaseName, ".tga", 4 );
  1808. if ( g_bNoTga && ( 00 == access( pFileName, 00 ) ) )
  1809. {
  1810. printf( "Warning: -notga disables \"%s\"\n", pFileName );
  1811. }
  1812. // PSD file attempt
  1813. memcpy( pFileName + lenBaseName, ".psd", 4 );
  1814. if ( !bOK && !g_bNoPsd ) // If PSD mode was not disabled
  1815. {
  1816. g_eMode = eModePSD;
  1817. CUtlBuffer bufFile;
  1818. bOK = LoadFile( pFileName, bufFile, false, &info.m_uiInputHash );
  1819. if ( bOK )
  1820. {
  1821. printf("config file %s\n", pFileName);
  1822. bOK = IsPSDFile( bufFile );
  1823. if ( !bOK )
  1824. {
  1825. VTexError( "%s is not a valid PSD file!\n", pFileName );
  1826. return false;
  1827. }
  1828. PSDImageResources imgres = PSDGetImageResources( bufFile );
  1829. PSDResFileInfo resFileInfo( imgres.FindElement( PSDImageResources::eResFileInfo ) );
  1830. PSDResFileInfo::ResFileInfoElement descr = resFileInfo.FindElement( PSDResFileInfo::eDescription );
  1831. if ( descr.m_pvData )
  1832. {
  1833. CUtlBuffer bufDescr( 0, 0, CUtlBuffer::TEXT_BUFFER );
  1834. bufDescr.EnsureCapacity( descr.m_numBytes );
  1835. bufDescr.Put( descr.m_pvData, descr.m_numBytes );
  1836. {
  1837. char key[2048];
  1838. char val[2048];
  1839. while( GetKeyValueFromBuffer( bufDescr, key, val ) )
  1840. {
  1841. info.ParseOptionKey( key, val );
  1842. }
  1843. if ( val[0] )
  1844. {
  1845. VTexError( "%s: %s\n", pFileName, val );
  1846. return false;
  1847. }
  1848. }
  1849. }
  1850. }
  1851. }
  1852. else if ( 00 == access( pFileName, 00 ) )
  1853. {
  1854. if ( !bOK )
  1855. printf( "Warning: -nopsd disables \"%s\"\n", pFileName );
  1856. else
  1857. printf( "Warning: psd file \"%s\" exists, but not used, delete tga and txt files to use psd file directly\n", pFileName );
  1858. }
  1859. // Try TXT file as config again for TGA cubemap / PFM
  1860. memcpy( pFileName + lenBaseName, ".txt", 4 );
  1861. if ( !bOK )
  1862. {
  1863. g_eMode = eModeTGA;
  1864. CUtlBuffer bufFile( 0, 0, CUtlBuffer::TEXT_BUFFER );
  1865. bOK = LoadFile( pFileName, bufFile, false, &info.m_uiInputHash );
  1866. if ( bOK )
  1867. {
  1868. printf("config file %s\n",pFileName);
  1869. {
  1870. char key[2048];
  1871. char val[2048];
  1872. while( GetKeyValueFromBuffer( bufFile, key, val ) )
  1873. {
  1874. info.ParseOptionKey( key, val );
  1875. }
  1876. if ( val[0] )
  1877. {
  1878. VTexError( "%s: %s\n", pFileName, val );
  1879. return false;
  1880. }
  1881. }
  1882. if ( g_eMode == eModePFM )
  1883. {
  1884. if ( g_bUsedAsLaunchableDLL && !( info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_NOCOMPRESS ) )
  1885. {
  1886. info.m_nFlags |= TEXTUREFLAGS_NOMIP;
  1887. }
  1888. }
  1889. }
  1890. }
  1891. if ( !bOK )
  1892. {
  1893. VTexError( "\"%s\" does not specify valid %s%sPFM+TXT files!\n",
  1894. pFileBaseName,
  1895. g_bNoPsd ? "" : "PSD or ",
  1896. g_bNoTga ? "" : "TGA or "
  1897. );
  1898. return false;
  1899. }
  1900. if ( info.m_bIsCubeMap )
  1901. *isCubeMap = true;
  1902. if( ( info.m_bNormalToDuDv || ( info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_NORMAL_DUDV ) ) &&
  1903. !( info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_PREMULT_COLOR_ONEOVERMIP ) )
  1904. {
  1905. printf( "Implicitly setting premultcolorbyoneovermiplevel since you are generating a dudv map\n" );
  1906. info.m_vtfProcOptions.flags0 |= VtfProcessingOptions::OPT_PREMULT_COLOR_ONEOVERMIP;
  1907. }
  1908. if( ( info.m_bNormalToDuDv || ( info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_NORMAL_DUDV ) ) )
  1909. {
  1910. printf( "Implicitly setting trilinear since you are generating a dudv map\n" );
  1911. info.m_nFlags |= TEXTUREFLAGS_TRILINEAR;
  1912. }
  1913. if( Q_stristr( pFileBaseName, "_normal" ) )
  1914. {
  1915. if( !( info.m_nFlags & TEXTUREFLAGS_NORMAL ) )
  1916. {
  1917. if( !g_Quiet )
  1918. {
  1919. fprintf( stderr, "implicitly setting:\n" );
  1920. fprintf( stderr, "\t\"normal\" \"1\"\n" );
  1921. fprintf( stderr, "since filename ends in \"_normal\"\n" );
  1922. }
  1923. info.m_nFlags |= TEXTUREFLAGS_NORMAL;
  1924. }
  1925. }
  1926. if( Q_stristr( pFileBaseName, "ssbump" ) )
  1927. {
  1928. if( !( info.m_nFlags & TEXTUREFLAGS_SSBUMP ) )
  1929. {
  1930. if( !g_Quiet )
  1931. {
  1932. fprintf( stderr, "implicitly setting:\n" );
  1933. fprintf( stderr, "\t\"ssbump\" \"1\"\n" );
  1934. fprintf( stderr, "since filename includes \"ssbump\"\n" );
  1935. }
  1936. info.m_nFlags |= TEXTUREFLAGS_SSBUMP;
  1937. }
  1938. }
  1939. if( Q_stristr( pFileBaseName, "_dudv" ) )
  1940. {
  1941. if( !info.m_bNormalToDuDv && !info.m_bDuDv )
  1942. {
  1943. if( !g_Quiet )
  1944. {
  1945. fprintf( stderr, "Implicitly setting:\n" );
  1946. fprintf( stderr, "\t\"dudv\" \"1\"\n" );
  1947. fprintf( stderr, "since filename ends in \"_dudv\"\n" );
  1948. fprintf( stderr, "If you are trying to convert from a normal map to a dudv map, put \"normaltodudv\" \"1\" in description.\n" );
  1949. }
  1950. info.m_bDuDv = true;
  1951. }
  1952. }
  1953. // turn off nice filtering if we are a cube map (takes too long with buildcubemaps) or
  1954. // if we are a normal map (looks like terd.)
  1955. if( ( info.m_nFlags & TEXTUREFLAGS_NORMAL ) || *isCubeMap )
  1956. {
  1957. if (info.m_vtfProcOptions.flags0 & VtfProcessingOptions::OPT_FILTER_NICE )
  1958. {
  1959. if ( !g_Quiet )
  1960. {
  1961. fprintf( stderr, "implicity disabling nice filtering\n" );
  1962. }
  1963. }
  1964. info.m_vtfProcOptions.flags0 &= ~VtfProcessingOptions::OPT_FILTER_NICE;
  1965. }
  1966. return true;
  1967. }
  1968. void Usage( void )
  1969. {
  1970. VTexError(
  1971. "Usage: vtex [-outdir dir] [-quiet] [-nopause] [-mkdir] [-shader ShaderName] [-vmtparam Param Value] tex1.txt tex2.txt . . .\n"
  1972. "-quiet : don't print anything out, don't pause for input\n"
  1973. "-warningsaserrors : treat warnings as errors\n"
  1974. "-nopause : don't pause for input\n"
  1975. "-nomkdir : don't create destination folder if it doesn't exist\n"
  1976. "-vmtparam : adds parameter and value to the .vmt file\n"
  1977. "-outdir <dir> : write output to the specified dir regardless of source filename and vproject\n"
  1978. "-deducepath : deduce path of sources by target file names\n"
  1979. "-quickconvert : use with \"-nop4 -dontusegamedir -quickconvert\" to upgrade old .vmt files\n"
  1980. "-crcvalidate : validate .vmt against the sources\n"
  1981. "-crcforce : generate a new .vmt even if sources crc matches\n"
  1982. "\teg: -vmtparam $ignorez 1 -vmtparam $translucent 1\n"
  1983. "Note that you can use wildcards and that you can also chain them\n"
  1984. "e.g. materialsrc/monster1/*.tga materialsrc/monster2/*.tga\n" );
  1985. }
  1986. bool GetOutputDir( const char *inputName, char *outputDir )
  1987. {
  1988. if ( g_ForcedOutputDir[0] )
  1989. {
  1990. strcpy( outputDir, g_ForcedOutputDir );
  1991. }
  1992. else
  1993. {
  1994. // Is inputName a relative path?
  1995. char buf[MAX_PATH];
  1996. Q_MakeAbsolutePath( buf, sizeof( buf ), inputName, NULL );
  1997. Q_FixSlashes( buf );
  1998. char szSearch[MAX_PATH] = { 0 };
  1999. V_snprintf( szSearch, sizeof( szSearch ), "materialsrc%c", CORRECT_PATH_SEPARATOR );
  2000. const char *pTmp = Q_stristr( buf, szSearch );
  2001. if( !pTmp )
  2002. {
  2003. return false;
  2004. }
  2005. pTmp += strlen( "materialsrc/" );
  2006. strcpy( outputDir, gamedir );
  2007. strcat( outputDir, "materials/" );
  2008. strcat( outputDir, pTmp );
  2009. Q_StripFilename( outputDir );
  2010. }
  2011. if( !g_Quiet )
  2012. {
  2013. printf( "output directory: %s\n", outputDir );
  2014. }
  2015. return true;
  2016. }
  2017. bool IsCube( const char *inputName )
  2018. {
  2019. char tgaName[MAX_PATH];
  2020. // Do Strcmp for ".hdr" to make sure we aren't ripping too much stuff off.
  2021. Q_StripExtension( inputName, tgaName, MAX_PATH );
  2022. const char *pInputExtension = inputName + Q_strlen( tgaName );
  2023. Q_strncat( tgaName, "rt", MAX_PATH, COPY_ALL_CHARACTERS );
  2024. Q_strncat( tgaName, pInputExtension, MAX_PATH, COPY_ALL_CHARACTERS );
  2025. Q_strncat( tgaName, GetSourceExtension(), MAX_PATH, COPY_ALL_CHARACTERS );
  2026. struct _stat buf;
  2027. if( _stat( tgaName, &buf ) != -1 )
  2028. {
  2029. return true;
  2030. }
  2031. else
  2032. {
  2033. return false;
  2034. }
  2035. }
  2036. #ifdef WIN32
  2037. int Find_Files( WIN32_FIND_DATA &wfd, HANDLE &hResult, const char *basedir, const char *extension )
  2038. {
  2039. char filename[MAX_PATH] = {0};
  2040. BOOL bMoreFiles = FindNextFile( hResult, &wfd);
  2041. if ( bMoreFiles )
  2042. {
  2043. // Skip . and ..
  2044. if ( wfd.cFileName[0] == '.' )
  2045. {
  2046. return FF_TRYAGAIN;
  2047. }
  2048. // If it's a subdirectory, just recurse down it
  2049. if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
  2050. {
  2051. char subdir[MAX_PATH];
  2052. sprintf( subdir, "%s\\%s", basedir, wfd.cFileName );
  2053. // Recurse
  2054. Find_Files( wfd, hResult, basedir, extension );
  2055. return FF_TRYAGAIN;
  2056. }
  2057. // Check that it's a tga
  2058. //
  2059. char fname[_MAX_FNAME] = {0};
  2060. char ext[_MAX_EXT] = {0};
  2061. _splitpath( wfd.cFileName, NULL, NULL, fname, ext );
  2062. // Not the type we want.
  2063. if ( stricmp( ext, extension ) )
  2064. return FF_DONTPROCESS;
  2065. // Check for .vmt
  2066. sprintf( filename, "%s\\%s.vmt", basedir, fname );
  2067. // Exists, so don't overwrite it
  2068. if ( access( filename, 0 ) != -1 )
  2069. return FF_TRYAGAIN;
  2070. char texturename[ _MAX_PATH ] = {0};
  2071. char *p = ( char * )basedir;
  2072. // Skip over the base path to get a material system relative path
  2073. // p += strlen( wfd.cFileName ) + 1;
  2074. // Construct texture name
  2075. sprintf( texturename, "%s\\%s", p, fname );
  2076. // Convert all to lower case
  2077. strlwr( texturename );
  2078. strlwr( filename );
  2079. }
  2080. return bMoreFiles;
  2081. }
  2082. #endif
  2083. bool Process_File( char *pInputBaseName, int maxlen )
  2084. {
  2085. char outputDir[1024];
  2086. Q_FixSlashes( pInputBaseName, '/' );
  2087. Q_StripExtension( pInputBaseName, pInputBaseName, maxlen );
  2088. if ( CommandLine()->FindParm( "-deducepath" ) )
  2089. {
  2090. strcpy( outputDir, pInputBaseName );
  2091. // If it is not a full path, try making it a full path
  2092. if ( pInputBaseName[0] != '/' &&
  2093. pInputBaseName[1] != ':' )
  2094. {
  2095. // Convert to full path
  2096. getcwd( outputDir, sizeof( outputDir ) );
  2097. Q_FixSlashes( outputDir, '/' );
  2098. Q_strncat( outputDir, "/", sizeof( outputDir ) );
  2099. Q_strncat( outputDir, pInputBaseName, sizeof( outputDir ) );
  2100. }
  2101. // If it is pointing inside "/materials/" make it go for "/materialsrc/"
  2102. char *pGame = strstr( outputDir, "/game/" );
  2103. char *pMaterials = strstr( outputDir, "/materials/" );
  2104. if ( pGame && pMaterials && ( pGame < pMaterials ) )
  2105. {
  2106. // "u:/data/game/tf/materials/" -> "u:/data/content/tf/materialsrc/"
  2107. int numExtraBytes = strlen( "/content/.../materialsrc/" ) - strlen( "/game/.../materials/" );
  2108. int numConvertBytes = pMaterials + strlen( "/materials/" ) - outputDir;
  2109. memmove( outputDir + numConvertBytes + numExtraBytes, outputDir + numConvertBytes, strlen( outputDir ) - numConvertBytes + 1 );
  2110. int numMidBytes = pMaterials - pGame - strlen( "/game" );
  2111. memmove( pGame + strlen( "/content" ), pGame + strlen( "/game" ), numMidBytes );
  2112. memmove( pGame, "/content", strlen( "/content" ) );
  2113. memmove( pGame + strlen( "/content" ) + numMidBytes, "/materialsrc/", strlen( "/materialsrc/" ) );
  2114. }
  2115. Q_strncpy( pInputBaseName, outputDir, maxlen );
  2116. }
  2117. if( !g_Quiet )
  2118. {
  2119. printf( "input file: %s\n", pInputBaseName );
  2120. }
  2121. if(g_UseGameDir && !GetOutputDir( pInputBaseName, outputDir ) )
  2122. {
  2123. VTexError( "Problem figuring out outputdir for %s\n", pInputBaseName );
  2124. return FALSE;
  2125. }
  2126. else if (!g_UseGameDir)
  2127. {
  2128. strcpy(outputDir, pInputBaseName);
  2129. Q_StripFilename(outputDir);
  2130. }
  2131. // Usage:
  2132. // vtex -nop4 -dontusegamedir -quickconvert u:\data\game\tf\texture.vtf
  2133. // Will read the old texture format and write the new texture format
  2134. //
  2135. if ( CommandLine()->FindParm( "-quickconvert" ) )
  2136. {
  2137. printf( "Quick convert of '%s'...\n", pInputBaseName );
  2138. char chFileNameConvert[ 512 ];
  2139. sprintf( chFileNameConvert, "%s.vtf", pInputBaseName );
  2140. IVTFTexture *pVtf = CreateVTFTexture();
  2141. CUtlBuffer bufFile;
  2142. LoadFile( chFileNameConvert, bufFile, true, NULL );
  2143. bool bRes = pVtf->Unserialize( bufFile );
  2144. if ( !bRes )
  2145. VTexError( "Failed to read '%s'!\n", chFileNameConvert );
  2146. // Determine the CRC if it was there
  2147. // CRC32_t uiDataHash = 0;
  2148. // CRC32_t *puiDataHash = &uiDataHash;
  2149. // Assert( sizeof( uiDataHash ) == sizeof( int ) );
  2150. // if ( !pVtf->GetResourceData( VTexConfigInfo_t::VTF_INPUTSRC_CRC, ... ) )
  2151. AttachShtFile( pInputBaseName, pVtf, NULL );
  2152. // Update the CRC
  2153. // if ( puiDataHash )
  2154. // {
  2155. // pVtf->InitResourceDataSection( VTexConfigInfo_t::VTF_INPUTSRC_CRC, *puiDataHash );
  2156. // }
  2157. // Remove the CRC when quick-converting
  2158. pVtf->SetResourceData( VTexConfigInfo_t::VTF_INPUTSRC_CRC, NULL, 0 );
  2159. bufFile.Clear();
  2160. bRes = pVtf->Serialize( bufFile );
  2161. if ( !bRes )
  2162. VTexError( "Failed to write '%s'!\n", chFileNameConvert );
  2163. DestroyVTFTexture( pVtf );
  2164. if ( FILE *fw = fopen( chFileNameConvert, "wb" ) )
  2165. {
  2166. fwrite( bufFile.Base(), 1, bufFile.TellPut(), fw );
  2167. fclose( fw );
  2168. }
  2169. else
  2170. VTexError( "Failed to open '%s' for writing!\n", chFileNameConvert );
  2171. printf( "... succeeded.\n" );
  2172. return TRUE;
  2173. }
  2174. VTexConfigInfo_t info;
  2175. bool isCubeMap = false;
  2176. if ( !LoadConfigFile( pInputBaseName, info, &isCubeMap ) )
  2177. return FALSE;
  2178. if( !isCubeMap )
  2179. {
  2180. isCubeMap = IsCube( pInputBaseName );
  2181. }
  2182. if( ( info.m_nStartFrame == -1 && info.m_nEndFrame != -1 ) ||
  2183. ( info.m_nStartFrame != -1 && info.m_nEndFrame == -1 ) )
  2184. {
  2185. VTexError( "%s: If you use startframe, you must use endframe, and vice versa.\n", pInputBaseName );
  2186. return FALSE;
  2187. }
  2188. const char *pBaseName = &pInputBaseName[strlen( pInputBaseName ) - 1];
  2189. while( (pBaseName >= pInputBaseName) && *pBaseName != '\\' && *pBaseName != '/' )
  2190. {
  2191. pBaseName--;
  2192. }
  2193. pBaseName++;
  2194. bool bProcessedFilesOK = ProcessFiles( pInputBaseName, outputDir, pBaseName, isCubeMap, info );
  2195. if ( !bProcessedFilesOK )
  2196. return FALSE;
  2197. // create vmts if necessary
  2198. if( g_ShaderName )
  2199. {
  2200. char buf[1024];
  2201. sprintf( buf, "%s/%s.vmt", outputDir, pBaseName );
  2202. const char *tmp = Q_stristr( outputDir, "materials" );
  2203. FILE *fp;
  2204. if( tmp )
  2205. {
  2206. // check if the file already exists.
  2207. fp = fopen( buf, "r" );
  2208. if( fp )
  2209. {
  2210. if ( !g_Quiet )
  2211. fprintf( stderr, "vmt file \"%s\" already exists\n", buf );
  2212. fclose( fp );
  2213. }
  2214. else
  2215. {
  2216. fp = fopen( buf, "w" );
  2217. if( fp )
  2218. {
  2219. if ( !g_Quiet )
  2220. fprintf( stderr, "Creating vmt file: %s/%s\n", tmp, pBaseName );
  2221. tmp += strlen( "materials/" );
  2222. fprintf( fp, "\"%s\"\n", g_ShaderName );
  2223. fprintf( fp, "{\n" );
  2224. fprintf( fp, "\t\"$baseTexture\" \"%s/%s\"\n", tmp, pBaseName );
  2225. int i;
  2226. for( i=0;i<g_NumVMTParams;i++ )
  2227. {
  2228. fprintf( fp, "\t\"%s\" \"%s\"\n", g_VMTParams[i].m_szParam, g_VMTParams[i].m_szValue );
  2229. }
  2230. fprintf( fp, "}\n" );
  2231. fclose( fp );
  2232. CP4AutoAddFile autop4( buf );
  2233. }
  2234. else
  2235. {
  2236. VTexWarning( "Couldn't open \"%s\" for writing\n", buf );
  2237. }
  2238. }
  2239. }
  2240. else
  2241. {
  2242. VTexWarning( "Couldn't find \"materials/\" in output path\n", buf );
  2243. }
  2244. }
  2245. return TRUE;
  2246. }
  2247. static SpewRetval_t VTexOutputFunc( SpewType_t spewType, char const *pMsg )
  2248. {
  2249. printf( "%s", pMsg );
  2250. if (spewType == SPEW_ERROR)
  2251. {
  2252. Pause();
  2253. return SPEW_ABORT;
  2254. }
  2255. return (spewType == SPEW_ASSERT) ? SPEW_DEBUGGER : SPEW_CONTINUE;
  2256. }
  2257. class CVTex : public CTier2AppSystem< IVTex >, public ILaunchableDLL
  2258. {
  2259. public:
  2260. int VTex( int argc, char **argv );
  2261. // ILaunchableDLL, used by vtex.exe.
  2262. virtual int main( int argc, char **argv )
  2263. {
  2264. g_bUsedAsLaunchableDLL = true;
  2265. // Being used as a launchable DLL, we don't want to blow away the host app's command line
  2266. CUtlString strOrigCmdLine( CommandLine()->GetCmdLine() );
  2267. // Run the vtex logic
  2268. int iResult = VTex( argc, argv );
  2269. // Restore command line
  2270. CommandLine()->CreateCmdLine( strOrigCmdLine.Get() );
  2271. return iResult;
  2272. }
  2273. virtual int VTex( CreateInterfaceFn fsFactory, const char *pGameDir, int argc, char **argv )
  2274. {
  2275. g_pFileSystem = g_pFullFileSystem = (IFileSystem*)fsFactory( FILESYSTEM_INTERFACE_VERSION, NULL );
  2276. if ( !g_pFileSystem )
  2277. {
  2278. Error( "IVTex3::VTex - fsFactory can't get '%s' interface.", FILESYSTEM_INTERFACE_VERSION );
  2279. return 0;
  2280. }
  2281. Q_strncpy( gamedir, pGameDir, sizeof( gamedir ) );
  2282. Q_AppendSlash( gamedir, sizeof( gamedir ) );
  2283. // When being used embedded in a host app, we don't want to blow away the host app's command line
  2284. CUtlString strOrigCmdLine( CommandLine()->GetCmdLine() );
  2285. int iResult = VTex( argc, argv );
  2286. // Restore command line
  2287. CommandLine()->CreateCmdLine( strOrigCmdLine.Get() );
  2288. return iResult;
  2289. }
  2290. };
  2291. static class CSuggestGameDirHelper
  2292. {
  2293. public:
  2294. static bool SuggestFn( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories );
  2295. bool MySuggestFn( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories );
  2296. public:
  2297. CSuggestGameDirHelper() : m_pszInputFiles( NULL ), m_numInputFiles( 0 ) {}
  2298. public:
  2299. char const * const *m_pszInputFiles;
  2300. size_t m_numInputFiles;
  2301. } g_suggestGameDirHelper;
  2302. bool CSuggestGameDirHelper::SuggestFn( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories )
  2303. {
  2304. return g_suggestGameDirHelper.MySuggestFn( pFsSteamSetupInfo, pchPathBuffer, nBufferLength, pbBubbleDirectories );
  2305. }
  2306. bool CSuggestGameDirHelper::MySuggestFn( CFSSteamSetupInfo const *pFsSteamSetupInfo, char *pchPathBuffer, int nBufferLength, bool *pbBubbleDirectories )
  2307. {
  2308. if ( !m_numInputFiles || !m_pszInputFiles )
  2309. return false;
  2310. if ( pbBubbleDirectories )
  2311. *pbBubbleDirectories = true;
  2312. for ( int k = 0; k < m_numInputFiles; ++ k )
  2313. {
  2314. Q_MakeAbsolutePath( pchPathBuffer, nBufferLength, m_pszInputFiles[ k ] );
  2315. return true;
  2316. }
  2317. return false;
  2318. }
  2319. int CVTex::VTex( int argc, char **argv )
  2320. {
  2321. CommandLine()->CreateCmdLine( argc, argv );
  2322. if ( g_bUsedAsLaunchableDLL )
  2323. {
  2324. SpewOutputFunc( VTexOutputFunc );
  2325. }
  2326. MathLib_Init( 2.2f, 2.2f, 0.0f, 1.0f, false, false, false, false );
  2327. if( argc < 2 )
  2328. {
  2329. Usage();
  2330. return -1;
  2331. }
  2332. g_UseGameDir = true; // make sure this is initialized to true.
  2333. const char *p4ChangelistLabel = "VTex Auto Checkout";
  2334. bool bCreatedFilesystem = false;
  2335. int i;
  2336. i = 1;
  2337. while( i < argc )
  2338. {
  2339. if( stricmp( argv[i], "-quiet" ) == 0 )
  2340. {
  2341. i++;
  2342. g_Quiet = true;
  2343. g_NoPause = true; // no point in pausing if we aren't going to print anything out.
  2344. }
  2345. else if( stricmp( argv[i], "-nopause" ) == 0 )
  2346. {
  2347. i++;
  2348. g_NoPause = true;
  2349. }
  2350. else if ( stricmp( argv[i], "-WarningsAsErrors" ) == 0 )
  2351. {
  2352. i++;
  2353. g_bWarningsAsErrors = true;
  2354. }
  2355. else if ( stricmp( argv[i], "-UseStandardError" ) == 0 )
  2356. {
  2357. i++;
  2358. g_bUseStandardError = true;
  2359. }
  2360. else if ( stricmp( argv[i], "-nopsd" ) == 0 )
  2361. {
  2362. i++;
  2363. g_bNoPsd = true;
  2364. }
  2365. else if ( stricmp( argv[i], "-notga" ) == 0 )
  2366. {
  2367. i++;
  2368. g_bNoTga = true;
  2369. }
  2370. else if ( stricmp( argv[i], "-nomkdir" ) == 0 )
  2371. {
  2372. i++;
  2373. g_CreateDir = false;
  2374. }
  2375. else if ( stricmp( argv[i], "-mkdir" ) == 0 )
  2376. {
  2377. i++;
  2378. g_CreateDir = true;
  2379. }
  2380. else if ( stricmp( argv[i], "-game" ) == 0 )
  2381. {
  2382. i += 2;
  2383. }
  2384. else if ( stricmp( argv[i], "-outdir" ) == 0 )
  2385. {
  2386. V_strcpy_safe( g_ForcedOutputDir, argv[i+1] );
  2387. i += 2;
  2388. }
  2389. else if ( stricmp( argv[i], "-p4changelistlabel" ) == 0 )
  2390. {
  2391. p4ChangelistLabel = argv[i+1];
  2392. i += 2;
  2393. }
  2394. else if ( stricmp( argv[i], "-p4skipchangelistlabel" ) == 0 )
  2395. {
  2396. p4ChangelistLabel = NULL;
  2397. i++;
  2398. }
  2399. else if ( stricmp( argv[i], "-dontusegamedir" ) == 0)
  2400. {
  2401. ++i;
  2402. g_UseGameDir = false;
  2403. }
  2404. else if( stricmp( argv[i], "-shader" ) == 0 )
  2405. {
  2406. i++;
  2407. if( i < argc )
  2408. {
  2409. g_ShaderName = argv[i];
  2410. i++;
  2411. }
  2412. }
  2413. else if( stricmp( argv[i], "-vproject" ) == 0 )
  2414. {
  2415. // skip this one. . we dont' use it internally.
  2416. i += 2;
  2417. }
  2418. else if( stricmp( argv[i], "-allowdebug" ) == 0 )
  2419. {
  2420. // skip this one. . we dont' use it internally.
  2421. i++;
  2422. }
  2423. else if( stricmp( argv[i], "-vmtparam" ) == 0 )
  2424. {
  2425. if( g_NumVMTParams < MAX_VMT_PARAMS )
  2426. {
  2427. i++;
  2428. if( i < argc - 1 )
  2429. {
  2430. g_VMTParams[g_NumVMTParams].m_szParam = argv[i];
  2431. i++;
  2432. if( i < argc - 1 )
  2433. {
  2434. g_VMTParams[g_NumVMTParams].m_szValue = argv[i];
  2435. i++;
  2436. }
  2437. else
  2438. {
  2439. g_VMTParams[g_NumVMTParams].m_szValue = "";
  2440. }
  2441. if( !g_Quiet )
  2442. {
  2443. fprintf( stderr, "Adding .vmt parameter: \"%s\"\t\"%s\"\n",
  2444. g_VMTParams[g_NumVMTParams].m_szParam,
  2445. g_VMTParams[g_NumVMTParams].m_szValue );
  2446. }
  2447. g_NumVMTParams++;
  2448. }
  2449. }
  2450. else
  2451. {
  2452. fprintf( stderr, "Exceeded max number of vmt parameters, extra ignored ( max %d )\n", MAX_VMT_PARAMS );
  2453. }
  2454. }
  2455. else if( stricmp( argv[i], "-nop4" ) == 0 )
  2456. {
  2457. // Just here to signify that -nop4 is a valid flag
  2458. ++ i;
  2459. }
  2460. else if( stricmp( argv[i], "-deducepath" ) == 0 )
  2461. {
  2462. // Just here to signify that -deducepath is a valid flag
  2463. ++ i;
  2464. }
  2465. else if( stricmp( argv[i], "-quickconvert" ) == 0 )
  2466. {
  2467. // Just here to signify that -quickconvert is a valid flag
  2468. ++ i;
  2469. }
  2470. else if( stricmp( argv[i], "-crcvalidate" ) == 0 )
  2471. {
  2472. // Just here to signify that -crcvalidate is a valid flag
  2473. ++ i;
  2474. }
  2475. else if( stricmp( argv[i], "-crcforce" ) == 0 )
  2476. {
  2477. // Just here to signify that -crcforce is a valid flag
  2478. ++ i;
  2479. }
  2480. else if( stricmp( argv[i], "-p4skip" ) == 0 )
  2481. {
  2482. // Just here to signify that -p4skip is a valid flag
  2483. ++ i;
  2484. }
  2485. else
  2486. {
  2487. break;
  2488. }
  2489. }
  2490. // Set the suggest game info directory helper
  2491. g_suggestGameDirHelper.m_pszInputFiles = argv + i;
  2492. g_suggestGameDirHelper.m_numInputFiles = argc - i;
  2493. SetSuggestGameInfoDirFn( CSuggestGameDirHelper::SuggestFn );
  2494. // g_pFileSystem may have been inherited with -inherit_filesystem.
  2495. if (g_UseGameDir && !g_pFileSystem)
  2496. {
  2497. FileSystem_Init( argv[i] );
  2498. bCreatedFilesystem = true;
  2499. Q_FixSlashes( gamedir, '/' );
  2500. }
  2501. if ( !CommandLine()->FindParm( "-p4skip" ) )
  2502. {
  2503. // Initialize P4
  2504. bool bP4DLLExists = false;
  2505. if ( g_pFullFileSystem )
  2506. {
  2507. bP4DLLExists = g_pFullFileSystem->FileExists( "p4lib.dll", "EXECUTABLE_PATH" );
  2508. }
  2509. if ( g_bUsedAsLaunchableDLL && !CommandLine()->FindParm( "-nop4" ) && bP4DLLExists )
  2510. {
  2511. const char *pModuleName = "p4lib.dll";
  2512. CSysModule *pModule = Sys_LoadModule( pModuleName );
  2513. if ( !pModule )
  2514. {
  2515. printf( "Can't load %s.\n", pModuleName );
  2516. return -1;
  2517. }
  2518. CreateInterfaceFn fn = Sys_GetFactory( pModule );
  2519. if ( !fn )
  2520. {
  2521. printf( "Can't get factory from %s.\n", pModuleName );
  2522. Sys_UnloadModule( pModule );
  2523. return -1;
  2524. }
  2525. p4 = (IP4 *)fn( P4_INTERFACE_VERSION, NULL );
  2526. if ( !p4 )
  2527. {
  2528. printf( "Can't get IP4 interface from %s, proceeding with -nop4.\n", pModuleName );
  2529. g_p4factory->SetDummyMode( true );
  2530. }
  2531. else
  2532. {
  2533. p4->Connect( FileSystem_GetFactory() );
  2534. p4->Init();
  2535. }
  2536. }
  2537. else
  2538. {
  2539. g_p4factory->SetDummyMode( true );
  2540. }
  2541. // Setup p4 factory
  2542. if ( p4ChangelistLabel && p4ChangelistLabel[0] != '\000' )
  2543. {
  2544. // Set the named changelist
  2545. g_p4factory->SetOpenFileChangeList( p4ChangelistLabel );
  2546. }
  2547. }
  2548. // Parse args
  2549. for( ; i < argc; i++ )
  2550. {
  2551. if ( argv[i][0] == '-' )
  2552. continue; // Assuming flags
  2553. char pInputBaseName[MAX_PATH];
  2554. Q_strncpy( pInputBaseName, argv[i], sizeof(pInputBaseName) );
  2555. // int maxlen = Q_strlen( pInputBaseName ) + 1;
  2556. if ( !Q_strstr( pInputBaseName, "*." ) )
  2557. {
  2558. Process_File( pInputBaseName, sizeof(pInputBaseName) );
  2559. continue;
  2560. }
  2561. #ifdef WIN32
  2562. char search[ 128 ];
  2563. char basedir[MAX_PATH];
  2564. char ext[_MAX_EXT];
  2565. char filename[_MAX_FNAME];
  2566. _splitpath( pInputBaseName, NULL, NULL, NULL, ext ); //find extension wanted
  2567. if ( !Q_ExtractFilePath ( pInputBaseName, basedir, sizeof( basedir ) ) )
  2568. strcpy( basedir, ".\\" );
  2569. sprintf( search, "%s\\*.*", basedir );
  2570. WIN32_FIND_DATA wfd;
  2571. HANDLE hResult;
  2572. memset(&wfd, 0, sizeof(WIN32_FIND_DATA));
  2573. hResult = FindFirstFile( search, &wfd );
  2574. if ( hResult != INVALID_HANDLE_VALUE )
  2575. {
  2576. sprintf( filename, "%s%s", basedir, wfd.cFileName );
  2577. if ( wfd.cFileName[0] != '.' )
  2578. Process_File( filename, sizeof( filename ) );
  2579. int iFFType = Find_Files( wfd, hResult, basedir, ext );
  2580. while ( iFFType )
  2581. {
  2582. sprintf( filename, "%s%s", basedir, wfd.cFileName );
  2583. if ( wfd.cFileName[0] != '.' && iFFType != FF_DONTPROCESS )
  2584. Process_File( filename, sizeof( filename ) );
  2585. iFFType = Find_Files( wfd, hResult, basedir, ext );
  2586. }
  2587. if ( iFFType == 0 )
  2588. {
  2589. FindClose( hResult );
  2590. }
  2591. }
  2592. #endif
  2593. }
  2594. // Shutdown P4
  2595. if ( g_bUsedAsLaunchableDLL && p4 && !CommandLine()->FindParm( "-p4skip" ) )
  2596. {
  2597. p4->Shutdown();
  2598. p4->Disconnect();
  2599. }
  2600. if ( bCreatedFilesystem )
  2601. {
  2602. FileSystem_Term();
  2603. }
  2604. if ( g_bUsedAsLaunchableDLL )
  2605. {
  2606. // Make sure any further spew doesn't call the function in this module (which will be unloaded shortly)
  2607. SpewOutputFunc( NULL );
  2608. }
  2609. Pause();
  2610. return 0;
  2611. }
  2612. CVTex g_VTex;
  2613. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CVTex, IVTex, IVTEX_VERSION_STRING, g_VTex );
  2614. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CVTex, ILaunchableDLL, LAUNCHABLE_DLL_INTERFACE_VERSION, g_VTex );