Skip to content

Commit

Permalink
vtf: fix vtf loading for windows arm( and for other platforms )
Browse files Browse the repository at this point in the history
  • Loading branch information
nillerusr committed Aug 14, 2023
1 parent 3d0025b commit 7f26785
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 43 deletions.
86 changes: 61 additions & 25 deletions public/vtf/vtf.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,23 @@ bool GetVTFPreload360Data( const char *pDebugName, CUtlBuffer &fileBufferIn, CUt
// compiler pads, the 360 compiler does NOT.
//-----------------------------------------------------------------------------

struct VTFFileBaseHeader_t
// nillerusr: try to avoid problems with pragma pack, remove c++ inheritance to make this structs platform-independent

struct alignas(16) VTFFileBaseHeader_t
{
DECLARE_BYTESWAP_DATADESC();
char fileTypeString[4]; // "VTF" Valve texture file
int version[2]; // version[0].version[1]
int headerSize;
};

struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
struct alignas(16) VTFFileHeaderV7_1_t
{
DECLARE_BYTESWAP_DATADESC();
char fileTypeString[4]; // "VTF" Valve texture file
int version[2]; // version[0].version[1]
int headerSize;

unsigned short width;
unsigned short height;
unsigned int flags;
Expand All @@ -484,13 +490,65 @@ struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
unsigned char lowResImageHeight;
};

struct VTFFileHeaderV7_2_t : public VTFFileHeaderV7_1_t
struct alignas(16) VTFFileHeaderV7_2_t
{
DECLARE_BYTESWAP_DATADESC();

char fileTypeString[4]; // "VTF" Valve texture file
int version[2]; // version[0].version[1]
int headerSize;

unsigned short width;
unsigned short height;
unsigned int flags;
unsigned short numFrames;
unsigned short startFrame;

// must manually align in order to maintain pack(1) expected layout with existing binaries
char pad1[4];
VectorAligned reflectivity;

float bumpScale;
ImageFormat imageFormat;
unsigned char numMipLevels;
ImageFormat lowResImageFormat;
unsigned char lowResImageWidth;
unsigned char lowResImageHeight;
unsigned short depth;
};

struct alignas(16) VTFFileHeaderV7_3_t
{
DECLARE_BYTESWAP_DATADESC();

char fileTypeString[4]; // "VTF" Valve texture file
int version[2]; // version[0].version[1]
int headerSize;

unsigned short width;
unsigned short height;
unsigned int flags;
unsigned short numFrames;
unsigned short startFrame;

// must manually align in order to maintain pack(1) expected layout with existing binaries
char pad1[4];
VectorAligned reflectivity;

float bumpScale;
ImageFormat imageFormat;
unsigned char numMipLevels;
ImageFormat lowResImageFormat;
unsigned char lowResImageWidth;
unsigned char lowResImageHeight;
unsigned short depth;

char pad4[3];
unsigned int numResources;
};

typedef VTFFileHeaderV7_3_t VTFFileHeader_t;

#define BYTE_POS( byteVal, shft ) uint32( uint32(uint8(byteVal)) << uint8(shft * 8) )
#if !defined( _X360 )
#define MK_VTF_RSRC_ID(a, b, c) uint32( BYTE_POS(a, 0) | BYTE_POS(b, 1) | BYTE_POS(c, 2) )
Expand Down Expand Up @@ -535,28 +593,6 @@ struct ResourceEntryInfo
unsigned int resData; // Resource data or offset from the beginning of the file
};

struct VTFFileHeaderV7_3_t : public VTFFileHeaderV7_2_t
{
DECLARE_BYTESWAP_DATADESC();

char pad4[3];
unsigned int numResources;

#if defined( _X360 ) || defined( POSIX )
// must manually align in order to maintain pack(1) expected layout with existing binaries
char pad5[8];
#endif

// AFTER THE IMPLICIT PADDING CAUSED BY THE COMPILER....
// *** followed by *** ResourceEntryInfo resources[0];
// Array of resource entry infos sorted ascending by type
};

struct VTFFileHeader_t : public VTFFileHeaderV7_3_t
{
DECLARE_BYTESWAP_DATADESC();
};

#define VTF_X360_MAJOR_VERSION 0x0360
#define VTF_X360_MINOR_VERSION 8
struct VTFFileHeaderX360_t : public VTFFileBaseHeader_t
Expand Down
55 changes: 37 additions & 18 deletions vtf/vtf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ BEGIN_BYTESWAP_DATADESC( VTFFileBaseHeader_t )
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
END_DATADESC()

BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_1_t, VTFFileBaseHeader_t )
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_1_t )
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
DEFINE_FIELD( width, FIELD_SHORT ),
DEFINE_FIELD( height, FIELD_SHORT ),
DEFINE_FIELD( flags, FIELD_INTEGER ),
Expand All @@ -42,17 +45,45 @@ BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_1_t, VTFFileBaseHeader_t )
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
END_DATADESC()

BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_2_t, VTFFileHeaderV7_1_t )
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_2_t )
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
DEFINE_FIELD( width, FIELD_SHORT ),
DEFINE_FIELD( height, FIELD_SHORT ),
DEFINE_FIELD( flags, FIELD_INTEGER ),
DEFINE_FIELD( numFrames, FIELD_SHORT ),
DEFINE_FIELD( startFrame, FIELD_SHORT ),
DEFINE_FIELD( reflectivity, FIELD_VECTOR ),
DEFINE_FIELD( bumpScale, FIELD_FLOAT ),
DEFINE_FIELD( imageFormat, FIELD_INTEGER ),
DEFINE_FIELD( numMipLevels, FIELD_CHARACTER ),
DEFINE_FIELD( lowResImageFormat, FIELD_INTEGER ),
DEFINE_FIELD( lowResImageWidth, FIELD_CHARACTER ),
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
DEFINE_FIELD( depth, FIELD_SHORT ),
END_DATADESC()

BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_3_t, VTFFileHeaderV7_2_t )
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_3_t )
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
DEFINE_FIELD( width, FIELD_SHORT ),
DEFINE_FIELD( height, FIELD_SHORT ),
DEFINE_FIELD( flags, FIELD_INTEGER ),
DEFINE_FIELD( numFrames, FIELD_SHORT ),
DEFINE_FIELD( startFrame, FIELD_SHORT ),
DEFINE_FIELD( reflectivity, FIELD_VECTOR ),
DEFINE_FIELD( bumpScale, FIELD_FLOAT ),
DEFINE_FIELD( imageFormat, FIELD_INTEGER ),
DEFINE_FIELD( numMipLevels, FIELD_CHARACTER ),
DEFINE_FIELD( lowResImageFormat, FIELD_INTEGER ),
DEFINE_FIELD( lowResImageWidth, FIELD_CHARACTER ),
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
DEFINE_FIELD( depth, FIELD_SHORT ),
DEFINE_FIELD( numResources, FIELD_INTEGER ),
END_DATADESC()

BEGIN_BYTESWAP_DATADESC_( VTFFileHeader_t, VTFFileHeaderV7_2_t )
END_DATADESC()

BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderX360_t, VTFFileBaseHeader_t )
DEFINE_FIELD( flags, FIELD_INTEGER ),
DEFINE_FIELD( width, FIELD_SHORT ),
Expand Down Expand Up @@ -903,23 +934,11 @@ static bool ReadHeaderFromBufferPastBaseHeader( CUtlBuffer &buf, VTFFileHeader_t
else if ( header.version[1] == 2 )
{
buf.Get( pBuf, sizeof(VTFFileHeaderV7_2_t) - sizeof(VTFFileBaseHeader_t) );

#if defined( _X360 ) || defined (POSIX)
// read 15 dummy bytes to be properly positioned with 7.2 PC data
byte dummy[15];
buf.Get( dummy, 15 );
#endif
}
else if ( header.version[1] == 1 || header.version[1] == 0 )
{
// previous version 7.0 or 7.1
buf.Get( pBuf, sizeof(VTFFileHeaderV7_1_t) - sizeof(VTFFileBaseHeader_t) );

#if defined( _X360 ) || defined (POSIX)
// read a dummy byte to be properly positioned with 7.0/1 PC data
byte dummy;
buf.Get( &dummy, 1 );
#endif
}
else
{
Expand Down

0 comments on commit 7f26785

Please sign in to comment.