summaryrefslogtreecommitdiff
path: root/vtf/vtf_x360.cpp
blob: d4a8e56df8c1bc9d8653bbf09ead24c8633c6866 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: The 360 VTF file format I/O class to help simplify access to 360 VTF files.
// 360 Formatted VTF's are stored ascending 1x1 up to NxN. Disk format and unserialized
// formats are expected to be the same.
//
//=====================================================================================//

#include "bitmap/imageformat.h"
#include "cvtf.h"
#include "utlbuffer.h"
#include "tier0/dbg.h"
#include "tier0/mem.h"
#include "tier2/fileutils.h"
#include "byteswap.h"
#include "filesystem.h"
#include "mathlib/mathlib.h"
#include "tier1/lzmaDecoder.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Callback for UpdateOrCreate utility function - swaps a vtf file.
//-----------------------------------------------------------------------------
static bool VTFCreateCallback( const char *pSourceName, const char *pTargetName, const char *pPathID, void *pExtraData )
{
	// Generate the file
	CUtlBuffer sourceBuf;
	CUtlBuffer targetBuf;
	bool bOk = g_pFullFileSystem->ReadFile( pSourceName, pPathID, sourceBuf );
	if ( bOk )
	{
		bOk = ConvertVTFTo360Format( pSourceName, sourceBuf, targetBuf, NULL );
		if ( bOk )
		{
			bOk = g_pFullFileSystem->WriteFile( pTargetName, pPathID, targetBuf );
		}
	}

	if ( !bOk )
	{
		Warning( "Failed to create %s\n", pTargetName );
	}
	return bOk;
}

//-----------------------------------------------------------------------------
// Calls utility function to create .360 version of a vtf file.
//-----------------------------------------------------------------------------
int CVTFTexture::UpdateOrCreate( const char *pFilename, const char *pPathID, bool bForce )
{
	return ::UpdateOrCreate( pFilename, NULL, 0, pPathID, VTFCreateCallback, bForce, NULL );
}

//-----------------------------------------------------------------------------
// Determine size of file, possibly smaller if skipping top mip levels.
//-----------------------------------------------------------------------------
int CVTFTexture::FileSize( bool bPreloadOnly, int nMipSkipCount ) const
{
	if ( bPreloadOnly )
	{
		// caller wants size of preload
		return m_iPreloadDataSize;
	}

	const ResourceEntryInfo *pEntryInfo = FindResourceEntryInfo( VTF_LEGACY_RSRC_IMAGE );
	if ( !pEntryInfo )
	{
		// has to exist
		Assert( 0 );
		return 0;
	}
	int iImageDataOffset = pEntryInfo->resData;

	if ( m_iCompressedSize )
	{
		// file is compressed, mip skipping is non-applicable at this stage
		return iImageDataOffset + m_iCompressedSize;
	}

	// caller gets file size, possibly truncated due to mip skipping
	int nFaceSize = ComputeFaceSize( nMipSkipCount );
	return iImageDataOffset + m_nFrameCount * m_nFaceCount * nFaceSize;
}

//-----------------------------------------------------------------------------
// Unserialization of image data from buffer
//-----------------------------------------------------------------------------
bool CVTFTexture::LoadImageData( CUtlBuffer &buf, bool bBufferIsVolatile, int nMipSkipCount )
{
	ResourceEntryInfo *pEntryInfo = FindResourceEntryInfo( VTF_LEGACY_RSRC_IMAGE );
	if ( !pEntryInfo )
	{
		// has to exist
		Assert( 0 );
		return false;
	}
	int iImageDataOffset = pEntryInfo->resData;

	// Fix up the mip count + size based on how many mip levels we skip...
	if ( nMipSkipCount > 0 )
	{
		if ( nMipSkipCount >= m_nMipCount )
		{
			nMipSkipCount = 0;
		}
		ComputeMipLevelDimensions( nMipSkipCount, &m_nWidth, &m_nHeight, &m_nDepth );
		m_nMipCount -= nMipSkipCount;
		m_nMipSkipCount += nMipSkipCount;
	}

	int iImageSize = ComputeFaceSize();
	iImageSize = m_nFrameCount * m_nFaceCount * iImageSize;

	// seek to start of image data
	// The mip levels are stored on disk ascending from smallest (1x1) to largest (NxN) to allow for picmip truncated reads
	buf.SeekGet( CUtlBuffer::SEEK_HEAD, iImageDataOffset ); 

	CLZMA lzma;
	if ( m_iCompressedSize )
	{
		unsigned char *pCompressedData = (unsigned char *)buf.PeekGet();
		if ( !lzma.IsCompressed( pCompressedData ) )
		{
			// huh? header says it was compressed
			Assert( 0 );
			return false;
		}
	
		// have to decode entire image
		unsigned int originalSize = lzma.GetActualSize( pCompressedData );
		AllocateImageData( originalSize );
		unsigned int outputLength = lzma.Uncompress( pCompressedData, m_pImageData );
		return ( outputLength == originalSize );		
	}

	bool bOK;
	if ( bBufferIsVolatile )
	{
		AllocateImageData( iImageSize );
		buf.Get( m_pImageData, iImageSize );
		bOK = buf.IsValid();
	}
	else
	{
		// safe to alias
		m_pImageData = (unsigned char *)buf.PeekGet( iImageSize, 0 );
		bOK = ( m_pImageData != NULL );
	}

	return bOK;
}

//-----------------------------------------------------------------------------
// Unserialization
//-----------------------------------------------------------------------------
bool CVTFTexture::ReadHeader( CUtlBuffer &buf, VTFFileHeaderX360_t &header )
{
	memset( &header, 0, sizeof( VTFFileHeaderX360_t ) );
	buf.GetObjects( &header );
	if ( !buf.IsValid() )
	{
		Warning( "*** Error getting header from a X360 VTF file.\n" );
		return false;
	}

	// Validity check
	if ( Q_strncmp( header.fileTypeString, "VTFX", 4 ) )
	{
		Warning( "*** Tried to load a PC VTF file as a X360 VTF file!\n" );
		return false;
	}

	if ( header.version[0] != VTF_X360_MAJOR_VERSION && header.version[1] != VTF_X360_MINOR_VERSION )
	{
		Warning( "*** Encountered X360 VTF file with an invalid version!\n" );
		return false;
	}

	if ( ( header.flags & TEXTUREFLAGS_ENVMAP ) && ( header.width != header.height ) )
	{
		Warning( "*** Encountered X360 VTF non-square cubemap!\n" );
		return false;
	}

	if ( ( header.flags & TEXTUREFLAGS_ENVMAP ) && ( header.depth != 1 ) )
	{
		Warning( "*** Encountered X360 VTF volume texture cubemap!\n" );
		return false;
	}

	if ( header.width <= 0 || header.height <= 0 || header.depth <= 0 )
	{
		Warning( "*** Encountered X360 VTF invalid texture size!\n" );
		return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Unserialization. Can optionally alias image components to a non-volatile buffer,
// which prevents unecessary copies.  Disk format and memory format of the image
// components are explicitly the same.
//-----------------------------------------------------------------------------
bool CVTFTexture::UnserializeFromBuffer( CUtlBuffer &buf, bool bBufferIsVolatile, bool bHeaderOnly, bool bPreloadOnly, int nMipSkipCount )
{
	VTFFileHeaderX360_t header;
	ResourceEntryInfo	*pEntryInfo;

	if ( !ReadHeader( buf, header ) )
	{
		return false;
	}

	// must first release any prior owned memory or reset aliases, otherwise corruption if types intermingled
	ReleaseImageMemory();
	ReleaseResources();

	m_nVersion[0] = header.version[0];
	m_nVersion[1] = header.version[1];

	m_nWidth = header.width;
	m_nHeight = header.height;
	m_nDepth = header.depth;
	m_Format = header.imageFormat;
	m_nFlags = header.flags;
	m_nFrameCount = header.numFrames;
	m_nFaceCount = ( m_nFlags & TEXTUREFLAGS_ENVMAP ) ? CUBEMAP_FACE_COUNT-1 : 1;
	m_nMipCount = ComputeMipCount();
	m_nMipSkipCount = header.mipSkipCount;
	m_vecReflectivity = header.reflectivity;
	m_flBumpScale = header.bumpScale;
	m_iPreloadDataSize = header.preloadDataSize;
	m_iCompressedSize = header.compressedSize;

	m_LowResImageFormat = IMAGE_FORMAT_RGB888;
	if ( header.lowResImageSample[3] )
	{
		// nonzero denotes validity of color value
		m_nLowResImageWidth = 1;
		m_nLowResImageHeight = 1;
		*(unsigned int *)m_LowResImageSample = *(unsigned int *)header.lowResImageSample;
	}
	else
	{
		m_nLowResImageWidth = 0;
		m_nLowResImageHeight = 0;
		*(unsigned int *)m_LowResImageSample = 0;
	}

	// 360 always has the image resource
	Assert( header.numResources >= 1 );
	m_arrResourcesInfo.SetCount( header.numResources );
	m_arrResourcesData.SetCount( header.numResources );

	// Read the dictionary of resources info
	buf.Get( m_arrResourcesInfo.Base(), m_arrResourcesInfo.Count() * sizeof( ResourceEntryInfo ) );
	if ( !buf.IsValid() )
	{
		return false;
	}

	pEntryInfo = FindResourceEntryInfo( VTF_LEGACY_RSRC_IMAGE );
	if ( !pEntryInfo )
	{
		// not optional, has to be present
		Assert( 0 );
		return false;
	}

	if ( bHeaderOnly )
	{
		// caller wants header components only
		// resource data chunks are NOT unserialized!
		return true;
	}

	if ( !LoadNewResources( buf ) )
	{
		return false;
	}

	if ( bPreloadOnly )
	{
		// caller wants preload portion only, everything up to the image
		return true;
	}

	if ( !LoadImageData( buf, bBufferIsVolatile, nMipSkipCount ) )
	{
		return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Discard image data to free up memory.
//-----------------------------------------------------------------------------
void CVTFTexture::ReleaseImageMemory()
{
	// valid sizes identify locally owned memory
	if ( m_nImageAllocSize )
	{
		delete [] m_pImageData;
		m_nImageAllocSize = 0;
	}

	// block pointers could be owned or aliased, always clear
	// ensures other caller's don't free an aliased pointer
	m_pImageData = NULL;
}

//-----------------------------------------------------------------------------
// Attributes...
//-----------------------------------------------------------------------------
bool CVTFTexture::IsPreTiled() const
{
	return false; 
}

int CVTFTexture::MappingWidth() const
{
	return m_nWidth << m_nMipSkipCount;
}

int CVTFTexture::MappingHeight() const
{
	return m_nHeight << m_nMipSkipCount;
}

int CVTFTexture::MappingDepth() const
{
	return m_nDepth << m_nMipSkipCount;
}

int CVTFTexture::MipSkipCount() const
{
	return m_nMipSkipCount;
}

unsigned char *CVTFTexture::LowResImageSample()
{
	return &m_LowResImageSample[0];
}