Skip to content

Commit 7f26785

Browse files
committed
vtf: fix vtf loading for windows arm( and for other platforms )
1 parent 3d0025b commit 7f26785

File tree

2 files changed

+98
-43
lines changed

2 files changed

+98
-43
lines changed

public/vtf/vtf.h

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,23 @@ bool GetVTFPreload360Data( const char *pDebugName, CUtlBuffer &fileBufferIn, CUt
455455
// compiler pads, the 360 compiler does NOT.
456456
//-----------------------------------------------------------------------------
457457

458-
struct VTFFileBaseHeader_t
458+
// nillerusr: try to avoid problems with pragma pack, remove c++ inheritance to make this structs platform-independent
459+
460+
struct alignas(16) VTFFileBaseHeader_t
459461
{
460462
DECLARE_BYTESWAP_DATADESC();
461463
char fileTypeString[4]; // "VTF" Valve texture file
462464
int version[2]; // version[0].version[1]
463465
int headerSize;
464466
};
465467

466-
struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
468+
struct alignas(16) VTFFileHeaderV7_1_t
467469
{
468470
DECLARE_BYTESWAP_DATADESC();
471+
char fileTypeString[4]; // "VTF" Valve texture file
472+
int version[2]; // version[0].version[1]
473+
int headerSize;
474+
469475
unsigned short width;
470476
unsigned short height;
471477
unsigned int flags;
@@ -484,13 +490,65 @@ struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
484490
unsigned char lowResImageHeight;
485491
};
486492

487-
struct VTFFileHeaderV7_2_t : public VTFFileHeaderV7_1_t
493+
struct alignas(16) VTFFileHeaderV7_2_t
488494
{
489495
DECLARE_BYTESWAP_DATADESC();
490496

497+
char fileTypeString[4]; // "VTF" Valve texture file
498+
int version[2]; // version[0].version[1]
499+
int headerSize;
500+
501+
unsigned short width;
502+
unsigned short height;
503+
unsigned int flags;
504+
unsigned short numFrames;
505+
unsigned short startFrame;
506+
507+
// must manually align in order to maintain pack(1) expected layout with existing binaries
508+
char pad1[4];
509+
VectorAligned reflectivity;
510+
511+
float bumpScale;
512+
ImageFormat imageFormat;
513+
unsigned char numMipLevels;
514+
ImageFormat lowResImageFormat;
515+
unsigned char lowResImageWidth;
516+
unsigned char lowResImageHeight;
491517
unsigned short depth;
492518
};
493519

520+
struct alignas(16) VTFFileHeaderV7_3_t
521+
{
522+
DECLARE_BYTESWAP_DATADESC();
523+
524+
char fileTypeString[4]; // "VTF" Valve texture file
525+
int version[2]; // version[0].version[1]
526+
int headerSize;
527+
528+
unsigned short width;
529+
unsigned short height;
530+
unsigned int flags;
531+
unsigned short numFrames;
532+
unsigned short startFrame;
533+
534+
// must manually align in order to maintain pack(1) expected layout with existing binaries
535+
char pad1[4];
536+
VectorAligned reflectivity;
537+
538+
float bumpScale;
539+
ImageFormat imageFormat;
540+
unsigned char numMipLevels;
541+
ImageFormat lowResImageFormat;
542+
unsigned char lowResImageWidth;
543+
unsigned char lowResImageHeight;
544+
unsigned short depth;
545+
546+
char pad4[3];
547+
unsigned int numResources;
548+
};
549+
550+
typedef VTFFileHeaderV7_3_t VTFFileHeader_t;
551+
494552
#define BYTE_POS( byteVal, shft ) uint32( uint32(uint8(byteVal)) << uint8(shft * 8) )
495553
#if !defined( _X360 )
496554
#define MK_VTF_RSRC_ID(a, b, c) uint32( BYTE_POS(a, 0) | BYTE_POS(b, 1) | BYTE_POS(c, 2) )
@@ -535,28 +593,6 @@ struct ResourceEntryInfo
535593
unsigned int resData; // Resource data or offset from the beginning of the file
536594
};
537595

538-
struct VTFFileHeaderV7_3_t : public VTFFileHeaderV7_2_t
539-
{
540-
DECLARE_BYTESWAP_DATADESC();
541-
542-
char pad4[3];
543-
unsigned int numResources;
544-
545-
#if defined( _X360 ) || defined( POSIX )
546-
// must manually align in order to maintain pack(1) expected layout with existing binaries
547-
char pad5[8];
548-
#endif
549-
550-
// AFTER THE IMPLICIT PADDING CAUSED BY THE COMPILER....
551-
// *** followed by *** ResourceEntryInfo resources[0];
552-
// Array of resource entry infos sorted ascending by type
553-
};
554-
555-
struct VTFFileHeader_t : public VTFFileHeaderV7_3_t
556-
{
557-
DECLARE_BYTESWAP_DATADESC();
558-
};
559-
560596
#define VTF_X360_MAJOR_VERSION 0x0360
561597
#define VTF_X360_MINOR_VERSION 8
562598
struct VTFFileHeaderX360_t : public VTFFileBaseHeader_t

vtf/vtf.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ BEGIN_BYTESWAP_DATADESC( VTFFileBaseHeader_t )
2727
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
2828
END_DATADESC()
2929

30-
BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_1_t, VTFFileBaseHeader_t )
30+
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_1_t )
31+
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
32+
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
33+
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
3134
DEFINE_FIELD( width, FIELD_SHORT ),
3235
DEFINE_FIELD( height, FIELD_SHORT ),
3336
DEFINE_FIELD( flags, FIELD_INTEGER ),
@@ -42,17 +45,45 @@ BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_1_t, VTFFileBaseHeader_t )
4245
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
4346
END_DATADESC()
4447

45-
BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_2_t, VTFFileHeaderV7_1_t )
48+
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_2_t )
49+
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
50+
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
51+
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
52+
DEFINE_FIELD( width, FIELD_SHORT ),
53+
DEFINE_FIELD( height, FIELD_SHORT ),
54+
DEFINE_FIELD( flags, FIELD_INTEGER ),
55+
DEFINE_FIELD( numFrames, FIELD_SHORT ),
56+
DEFINE_FIELD( startFrame, FIELD_SHORT ),
57+
DEFINE_FIELD( reflectivity, FIELD_VECTOR ),
58+
DEFINE_FIELD( bumpScale, FIELD_FLOAT ),
59+
DEFINE_FIELD( imageFormat, FIELD_INTEGER ),
60+
DEFINE_FIELD( numMipLevels, FIELD_CHARACTER ),
61+
DEFINE_FIELD( lowResImageFormat, FIELD_INTEGER ),
62+
DEFINE_FIELD( lowResImageWidth, FIELD_CHARACTER ),
63+
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
4664
DEFINE_FIELD( depth, FIELD_SHORT ),
4765
END_DATADESC()
4866

49-
BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderV7_3_t, VTFFileHeaderV7_2_t )
67+
BEGIN_BYTESWAP_DATADESC( VTFFileHeaderV7_3_t )
68+
DEFINE_ARRAY( fileTypeString, FIELD_CHARACTER, 4 ),
69+
DEFINE_ARRAY( version, FIELD_INTEGER, 2 ),
70+
DEFINE_FIELD( headerSize, FIELD_INTEGER ),
71+
DEFINE_FIELD( width, FIELD_SHORT ),
72+
DEFINE_FIELD( height, FIELD_SHORT ),
73+
DEFINE_FIELD( flags, FIELD_INTEGER ),
74+
DEFINE_FIELD( numFrames, FIELD_SHORT ),
75+
DEFINE_FIELD( startFrame, FIELD_SHORT ),
76+
DEFINE_FIELD( reflectivity, FIELD_VECTOR ),
77+
DEFINE_FIELD( bumpScale, FIELD_FLOAT ),
78+
DEFINE_FIELD( imageFormat, FIELD_INTEGER ),
79+
DEFINE_FIELD( numMipLevels, FIELD_CHARACTER ),
80+
DEFINE_FIELD( lowResImageFormat, FIELD_INTEGER ),
81+
DEFINE_FIELD( lowResImageWidth, FIELD_CHARACTER ),
82+
DEFINE_FIELD( lowResImageHeight, FIELD_CHARACTER ),
83+
DEFINE_FIELD( depth, FIELD_SHORT ),
5084
DEFINE_FIELD( numResources, FIELD_INTEGER ),
5185
END_DATADESC()
5286

53-
BEGIN_BYTESWAP_DATADESC_( VTFFileHeader_t, VTFFileHeaderV7_2_t )
54-
END_DATADESC()
55-
5687
BEGIN_BYTESWAP_DATADESC_( VTFFileHeaderX360_t, VTFFileBaseHeader_t )
5788
DEFINE_FIELD( flags, FIELD_INTEGER ),
5889
DEFINE_FIELD( width, FIELD_SHORT ),
@@ -903,23 +934,11 @@ static bool ReadHeaderFromBufferPastBaseHeader( CUtlBuffer &buf, VTFFileHeader_t
903934
else if ( header.version[1] == 2 )
904935
{
905936
buf.Get( pBuf, sizeof(VTFFileHeaderV7_2_t) - sizeof(VTFFileBaseHeader_t) );
906-
907-
#if defined( _X360 ) || defined (POSIX)
908-
// read 15 dummy bytes to be properly positioned with 7.2 PC data
909-
byte dummy[15];
910-
buf.Get( dummy, 15 );
911-
#endif
912937
}
913938
else if ( header.version[1] == 1 || header.version[1] == 0 )
914939
{
915940
// previous version 7.0 or 7.1
916941
buf.Get( pBuf, sizeof(VTFFileHeaderV7_1_t) - sizeof(VTFFileBaseHeader_t) );
917-
918-
#if defined( _X360 ) || defined (POSIX)
919-
// read a dummy byte to be properly positioned with 7.0/1 PC data
920-
byte dummy;
921-
buf.Get( &dummy, 1 );
922-
#endif
923942
}
924943
else
925944
{

0 commit comments

Comments
 (0)