forked from lua9520/source-engine-2018-cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmetexture.cpp
More file actions
394 lines (331 loc) · 9.8 KB
/
dmetexture.cpp
File metadata and controls
394 lines (331 loc) · 9.8 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//====== Copyright © 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "materialobjects/dmetexture.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "materialsystem/IMaterial.h"
#include "materialsystem/IMaterialSystem.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTextureFrame, CDmeTextureFrame );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmeTextureFrame::OnConstruction()
{
m_MipLevels.Init( this, "mipLevels" );
}
void CDmeTextureFrame::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// List manipulation
//-----------------------------------------------------------------------------
int CDmeTextureFrame::MipLevelCount() const
{
return m_MipLevels.Count();
}
CDmeImageArray *CDmeTextureFrame::GetMipLevel( int nIndex ) const
{
return m_MipLevels[ nIndex ];
}
void CDmeTextureFrame::AddMipLevel( CDmeImageArray *pImages )
{
m_MipLevels.AddToTail( pImages );
}
CDmeImageArray *CDmeTextureFrame::AddMipLevel( )
{
CDmeImageArray *pImages = CreateElement< CDmeImageArray >( "mip", GetFileId() );
AddMipLevel( pImages );
return pImages;
}
int CDmeTextureFrame::ImageCount() const
{
int nImageCount = 0;
int nMipCount = MipLevelCount();
for ( int m = 0; m < nMipCount; ++m )
{
CDmeImageArray *pMipLevel = GetMipLevel( m );
nImageCount = MAX( nImageCount, pMipLevel->ImageCount() );
}
return nImageCount;
}
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTexture, CDmeTexture );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmeTexture::OnConstruction()
{
// m_pVTFTexture = CreateVTFTexture();
m_bClampS.Init( this, "clampS" );
m_bClampT.Init( this, "clampT" );
m_bClampU.Init( this, "clampU" );
m_bNoDebugOverride.Init( this, "noDebugOverride" );
m_bNoLod.Init( this, "noMipmapLOD" );
m_bNiceFiltered.Init( this, "niceFiltered" );
m_bNormalMap.Init( this, "normalMap" );
m_flBumpScale.Init( this, "bumpScale" );
m_nCompressType.Init( this, "compressType" );
m_nFilterType.Init( this, "filterType" );
m_nMipmapType.Init( this, "mipmapType" );
m_nTextureType.Init( this, "textureType" );
m_Frames.Init( this, "frames" );
}
void CDmeTexture::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Gets dimensions
//-----------------------------------------------------------------------------
int CDmeTexture::Width() const
{
int nFrameCount = m_Frames.Count();
int nWidth = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Width() : 0;
#ifdef _DEBUG
for ( int f = 1; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
Assert( pFrame->GetMipLevel( 0 )->Width() == nWidth );
}
#endif
return nWidth;
}
int CDmeTexture::Height() const
{
int nFrameCount = m_Frames.Count();
int nHeight = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Height() : 0;
#ifdef _DEBUG
for ( int f = 1; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
Assert( pFrame->GetMipLevel( 0 )->Height() == nHeight );
}
#endif
return nHeight;
}
int CDmeTexture::Depth() const
{
int nFrameCount = m_Frames.Count();
int nDepth = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Depth() : 0;
#ifdef _DEBUG
for ( int f = 1; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
Assert( pFrame->GetMipLevel( 0 )->Depth() == nDepth );
}
#endif
return nDepth;
}
ImageFormat CDmeTexture::Format() const
{
int nFrameCount = m_Frames.Count();
ImageFormat nFormat = nFrameCount ? m_Frames[0]->GetMipLevel( 0 )->Format() : IMAGE_FORMAT_UNKNOWN;
#ifdef _DEBUG
for ( int f = 0; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
int nMipCount = pFrame->MipLevelCount();
for ( int m = 0; m < nMipCount; ++m )
{
CDmeImageArray *pMipLevel = pFrame->GetMipLevel( m );
Assert( pMipLevel->Format() == nFormat );
}
}
#endif
return nFormat;
}
int CDmeTexture::MipLevelCount() const
{
int nFrameCount = m_Frames.Count();
int nMipCount = nFrameCount ? m_Frames[0]->MipLevelCount( ) : 0;
#ifdef _DEBUG
for ( int f = 0; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
Assert( nMipCount == pFrame->MipLevelCount() );
}
#endif
return nMipCount;
}
int CDmeTexture::ImageCount() const
{
int nImageCount = 0;
int nFrameCount = m_Frames.Count();
for ( int f = 0; f < nFrameCount; ++f )
{
CDmeTextureFrame *pFrame = m_Frames[f];
nImageCount = MAX( nImageCount, pFrame->ImageCount() );
}
return nImageCount;
}
//-----------------------------------------------------------------------------
// Computes texture flags
//-----------------------------------------------------------------------------
int CDmeTexture::CalcTextureFlags( int nDepth ) const
{
int nFlags = 0;
if ( m_bClampS )
{
nFlags |= TEXTUREFLAGS_CLAMPS;
}
if ( m_bClampT )
{
nFlags |= TEXTUREFLAGS_CLAMPT;
}
if ( m_bClampU )
{
nFlags |= TEXTUREFLAGS_CLAMPU;
}
if ( m_bNoLod )
{
nFlags |= TEXTUREFLAGS_NOLOD;
}
if ( m_bNormalMap )
{
nFlags |= TEXTUREFLAGS_NORMAL;
}
if ( m_bNormalMap )
{
nFlags |= TEXTUREFLAGS_NORMAL;
}
if ( m_bNoDebugOverride )
{
nFlags |= TEXTUREFLAGS_NODEBUGOVERRIDE;
}
switch ( m_nCompressType )
{
case DMETEXTURE_COMPRESS_DEFAULT:
case DMETEXTURE_COMPRESS_DXT1:
break;
case DMETEXTURE_COMPRESS_DXT5:
nFlags |= TEXTUREFLAGS_HINT_DXT5;
break;
}
switch ( m_nFilterType )
{
case DMETEXTURE_FILTER_DEFAULT:
case DMETEXTURE_FILTER_BILINEAR:
break;
case DMETEXTURE_FILTER_ANISOTROPIC:
nFlags |= TEXTUREFLAGS_ANISOTROPIC;
break;
case DMETEXTURE_FILTER_TRILINEAR:
nFlags |= TEXTUREFLAGS_TRILINEAR;
break;
case DMETEXTURE_FILTER_POINT:
nFlags |= TEXTUREFLAGS_POINTSAMPLE;
break;
}
switch ( m_nMipmapType )
{
case DMETEXTURE_MIPMAP_DEFAULT:
case DMETEXTURE_MIPMAP_ALL_LEVELS:
break;
case DMETEXTURE_MIPMAP_NONE:
nFlags |= TEXTUREFLAGS_NOMIP;
break;
}
if ( nDepth > 1 )
{
// FIXME: Volume textures don't currently support DXT compression
nFlags &= ~TEXTUREFLAGS_HINT_DXT5;
}
return nFlags;
}
//-----------------------------------------------------------------------------
// Computes the desired texture format based on flags
//-----------------------------------------------------------------------------
ImageFormat CDmeTexture::ComputeDesiredImageFormat( ImageFormat srcFormat, int nWidth, int nHeight, int nDepth, int nFlags )
{
// HDRFIXME: Need to figure out what format to use here.
if ( srcFormat == IMAGE_FORMAT_RGB323232F )
return IMAGE_FORMAT_RGBA16161616F;
/*
if( bDUDVTarget)
{
if ( bCopyAlphaToLuminance && ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) ) )
return IMAGE_FORMAT_UVLX8888;
return IMAGE_FORMAT_UV88;
}
*/
// can't compress textures that are smaller than 4x4
if ( (nFlags & TEXTUREFLAGS_PROCEDURAL) ||
( nWidth < 4 ) || ( nHeight < 4 ) || ( nDepth > 1 ) )
{
if ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) )
return IMAGE_FORMAT_BGRA8888;
return IMAGE_FORMAT_BGR888;
}
if( nFlags & TEXTUREFLAGS_HINT_DXT5 )
return IMAGE_FORMAT_DXT5;
// compressed with alpha blending
if ( nFlags & TEXTUREFLAGS_EIGHTBITALPHA )
return IMAGE_FORMAT_DXT5;
if ( nFlags & TEXTUREFLAGS_ONEBITALPHA )
return IMAGE_FORMAT_DXT5; // IMAGE_FORMAT_DXT1_ONEBITALPHA
return IMAGE_FORMAT_DXT1;
}
//-----------------------------------------------------------------------------
// Adds a frame
//-----------------------------------------------------------------------------
CDmeTextureFrame *CDmeTexture::AddFrame()
{
CDmeTextureFrame *pImageArray = CreateElement< CDmeTextureFrame >( "frame", GetFileId() );
m_Frames.AddToTail( pImageArray );
return pImageArray;
}
void CDmeTexture::RemoveAllFrames()
{
m_Frames.RemoveAll();
}
//-----------------------------------------------------------------------------
// Returns all images associated with a particular frame + face (mip count amount)
//-----------------------------------------------------------------------------
void CDmeTexture::GetImages( int nFrame, int nImageIndex, CDmeImage **ppImages, int nSize )
{
memset( ppImages, 0, nSize * sizeof(CDmeImage*) );
int nFrameCount = FrameCount();
if ( nFrame >= nFrameCount )
return;
CDmeTextureFrame *pFrame = GetFrame( nFrame );
if ( !pFrame )
return;
int nMipCount = MIN( pFrame->MipLevelCount(), nSize );
for ( int m = 0; m < nMipCount; ++m )
{
CDmeImageArray *pImageArray = pFrame->GetMipLevel( m );
if ( !pImageArray )
continue;
int nImageCount = pImageArray->ImageCount();
if ( nImageIndex >= nImageCount )
continue;
ppImages[m] = pImageArray->GetImage( nImageIndex );
}
}
void CDmeTexture::CompressTexture( CDmeTexture *pSrcTexture, ImageFormat fmt )
{
class CCompressionFunctor
{
public:
CCompressionFunctor( ImageFormat fmt ) { m_Format = fmt; }
void operator()( CDmeImage *pDstImage, CDmeImage *pSrcImage )
{
pDstImage->CompressImage( pSrcImage, m_Format );
}
ImageFormat m_Format;
};
CCompressionFunctor compress( fmt );
ProcessTexture( pSrcTexture, compress );
}
//-----------------------------------------------------------------------------
// resolve
//-----------------------------------------------------------------------------
void CDmeTexture::Resolve()
{
}