forked from lua9520/source-engine-2018-cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmeprecompiledtexture.cpp
More file actions
380 lines (315 loc) · 11.5 KB
/
dmeprecompiledtexture.cpp
File metadata and controls
380 lines (315 loc) · 11.5 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
//====== Copyright © 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "materialobjects/dmeprecompiledtexture.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "materialobjects/dmetexture.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmePrecompiledTexture, CDmePrecompiledTexture );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmePrecompiledTexture::OnConstruction()
{
m_ImageFileName.Init( this, "imageFileName" );
m_Processors.Init( this, "processors" );
m_pSourceTexture.Init( this, "sourceTexture" );
m_nStartFrame.InitAndSet( this, "startFrame", -1 );
m_nEndFrame.InitAndSet( this, "endFrame", -1 );
m_nVolumeTextureDepth.InitAndSet( this, "volumeTextureDepth", 1 );
m_nTextureArraySize.InitAndSet( this, "textureArraySize", 1 );
m_nTextureType.Init( this, "textureType" );
m_flBumpScale.InitAndSet( this, "bumpScale", 1.0f );
m_flPFMScale.InitAndSet( this, "pfmScale", 1.0f );
m_nFilterType.Init( this, "filterType" );
m_bClampS.Init( this, "clamps" );
m_bClampT.Init( this, "clampt" );
m_bClampU.Init( this, "clampu" );
m_bLoadMipLevels.Init( this, "loadMipLevels" );
m_bBorder.Init( this, "border" );
m_bNormalMap.Init( this, "normalMap" );
m_bSSBump.Init( this, "ssBump" );
m_bNoMip.Init( this, "noMip" );
m_bAllMips.Init( this, "allMips" );
m_bNoLod.Init( this, "noLod" );
m_bNoDebugOverride.Init( this, "noDebugOverride" );
m_bNoCompression.Init( this, "noCompression" );
m_bHintDxt5Compression.Init( this, "hintDxt5Compression" );
}
void CDmePrecompiledTexture::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Ensures all settings are internally consistent
//-----------------------------------------------------------------------------
bool CDmePrecompiledTexture::ValidateValues()
{
if ( ( m_nTextureType == DMETEXTURE_TYPE_CUBEMAP ) && ( m_nTextureArraySize != 1 ) )
{
// Cubemaps are packed into a single texture in a cross shape
Warning( "Specified a cubemap with a texture array size != 1!\n" );
return false;
}
if ( ( m_nVolumeTextureDepth > 1 ) && ( m_nTextureArraySize != 1 ) )
{
Warning( "Specified a volume texture with a texture array size != 1!\n" );
return false;
}
if ( m_bLoadMipLevels )
{
char pBaseName[MAX_PATH];
Q_FileBase( GetName(), pBaseName, sizeof( pBaseName ) );
char pRight[16];
V_StrRight( pBaseName, 5, pRight, sizeof( pRight ) );
if ( !Q_stristr( pRight, "_mip0" ) )
{
Warning( "Invalid texture name (\"%s\") for explicitly loading mip levels - the top mip file should end in '_mip0'\n", GetName() );
}
}
return true;
}
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT( CDmeTextureProcessor );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmeTextureProcessor::OnConstruction()
{
}
void CDmeTextureProcessor::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTP_ComputeMipmaps, CDmeTP_ComputeMipmaps );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmeTP_ComputeMipmaps::OnConstruction()
{
m_bNoNiceFiltering.Init( this, "noNiceFiltering" );
m_bAlphaTestDownsampling.Init( this, "alphaTestDownsampling" );
m_flAlphaTestDownsampleThreshhold.Init( this, "alphaTestDownsampleThreshhold" );
m_flAlphaTestDownsampleHiFreqThreshhold.Init( this, "alphaTestDownsampleHiFreqThreshhold" );
}
void CDmeTP_ComputeMipmaps::OnDestruction()
{
}
void CDmeTP_ComputeMipmaps::ProcessTexture( CDmeTexture *pSrcTexture, CDmeTexture *pDstTexture )
{
int nWidth = pSrcTexture->Width();
int nHeight = pSrcTexture->Height();
int nDepth = pSrcTexture->Depth();
int nTotalMipCount = ImageLoader::GetNumMipMapLevels( nWidth, nHeight, nDepth );
pSrcTexture->CopyAttributesTo( pDstTexture, TD_NONE );
// Copying will copy references to src texture frames. Remove them.
pDstTexture->RemoveAllFrames();
DownsampleInfo_t info;
info.m_flAlphaThreshhold = m_flAlphaTestDownsampleThreshhold;
info.m_flAlphaHiFreqThreshhold = m_flAlphaTestDownsampleHiFreqThreshhold;
info.m_nFlags = 0;
if ( pSrcTexture->m_bClampS )
{
info.m_nFlags |= DOWNSAMPLE_CLAMPS;
}
if ( pSrcTexture->m_bClampT )
{
info.m_nFlags |= DOWNSAMPLE_CLAMPT;
}
if ( pSrcTexture->m_bClampU )
{
info.m_nFlags |= DOWNSAMPLE_CLAMPU;
}
if ( m_bAlphaTestDownsampling )
{
info.m_nFlags |= DOWNSAMPLE_ALPHATEST;
}
int nFrameCount = pSrcTexture->FrameCount();
CDmeImage **ppSrcImages = (CDmeImage**)stackalloc( nTotalMipCount * sizeof(CDmeImage*) );
CDmeImage **ppDstImages = (CDmeImage**)stackalloc( nTotalMipCount * sizeof(CDmeImage*) );
for ( int f = 0; f < nFrameCount; ++f )
{
CDmeTextureFrame *pSrcFrame = pSrcTexture->GetFrame( f );
CDmeTextureFrame *pDstFrame = pDstTexture->AddFrame();
// Add mip level 0 for this frame
CDmeImageArray *pSrcArray = pSrcFrame->GetMipLevel( 0 );
if ( !pSrcArray )
continue;
for ( int m = 0; m < nTotalMipCount; ++m )
{
pDstFrame->AddMipLevel();
}
int nImageCount = pSrcFrame->ImageCount();
for ( int i = 0; i < nImageCount; ++i )
{
pSrcTexture->GetImages( f, i, ppSrcImages, nTotalMipCount );
CDmeImage *pSrcImage = ppSrcImages[0];
if ( !pSrcImage )
continue;
int nMipWidth = nWidth;
int nMipHeight = nHeight;
int nMipDepth = nDepth;
for ( int m = 0; m < nTotalMipCount; ++m )
{
CDmeImageArray *pDstArray = pDstFrame->GetMipLevel( m );
CDmeImage *pSrcImage = ppSrcImages[m];
CDmeImage *pDstImage = pDstArray->AddImage();
ppDstImages[m] = pDstImage;
if ( pSrcImage )
{
Assert( pSrcImage->Width() == nMipWidth );
Assert( pSrcImage->Height() == nMipHeight );
Assert( pSrcImage->Depth() == nMipDepth );
// FIXME: Just assign the src image into the dest texture
// and remove it from the src texture
// For mips where we have data, just copy src -> dest.
pDstImage->CopyFrom( pSrcImage );
}
else
{
// For mips where don't have data, generate a mip from the src data of the previous level
Assert( m != 0 && ppDstImages[m-1] );
if ( m_bNoNiceFiltering )
{
pDstImage->QuarterSize( ppDstImages[m-1] );
}
else
{
int nSrcMip = clamp( m-3, 0, nTotalMipCount );
pDstImage->Init( nMipWidth, nMipHeight, nMipDepth, ppDstImages[nSrcMip]->Format(), ppDstImages[nSrcMip]->Gamma() );
pDstImage->DownsampleNiceFiltered( info, ppDstImages[nSrcMip] );
}
}
nMipWidth >>= 1; nMipHeight >>= 1; nMipDepth >>= 1;
nMipWidth = MAX( 1, nMipWidth ); nMipHeight = MAX( 1, nMipHeight ); nMipDepth = MAX( 1, nMipDepth );
}
}
}
}
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeTP_ChangeColorChannels, CDmeTP_ChangeColorChannels );
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
void CDmeTP_ChangeColorChannels::OnConstruction()
{
m_nMaxChannels.Init( this, "maxChannels" );
}
void CDmeTP_ChangeColorChannels::OnDestruction()
{
}
static ImageFormat ComputeDestFormat( ImageFormat fmt, int nChannelCount )
{
switch( fmt )
{
case IMAGE_FORMAT_RGBA8888:
case IMAGE_FORMAT_ABGR8888:
case IMAGE_FORMAT_ARGB8888:
case IMAGE_FORMAT_BGRA8888:
Assert( nChannelCount != 2 );
return ( nChannelCount == 3 ) ? IMAGE_FORMAT_RGB888 : IMAGE_FORMAT_I8;
case IMAGE_FORMAT_RGB888:
case IMAGE_FORMAT_BGR888:
case IMAGE_FORMAT_BGRX8888:
case IMAGE_FORMAT_RGBX8888:
Assert( nChannelCount == 1 );
return IMAGE_FORMAT_I8;
case IMAGE_FORMAT_IA88:
Assert( nChannelCount == 1 );
return IMAGE_FORMAT_I8;
case IMAGE_FORMAT_DXT5:
Assert( nChannelCount == 3 );
return IMAGE_FORMAT_DXT1;
case IMAGE_FORMAT_RGBA32323232F:
if ( nChannelCount == 3 )
return IMAGE_FORMAT_RGB323232F;
// fall through
case IMAGE_FORMAT_RGB323232F:
if ( nChannelCount == 2 )
return IMAGE_FORMAT_RG3232F;
// fall through
case IMAGE_FORMAT_RG3232F:
Assert( nChannelCount == 1 );
return IMAGE_FORMAT_R32F;
case IMAGE_FORMAT_RGBA16161616F:
Assert( nChannelCount != 3 );
if ( nChannelCount == 2 )
return IMAGE_FORMAT_RG1616F;
// fall through
case IMAGE_FORMAT_RG1616F:
Assert( nChannelCount == 1 );
return IMAGE_FORMAT_R16F;
default:
Assert( 0 );
break;
}
return fmt;
}
void CDmeTP_ChangeColorChannels::ProcessImage( CDmeImage *pDstImage, CDmeImage *pSrcImage )
{
ImageFormat fmt = pSrcImage->Format();
ImageFormat dstFmt = fmt;
const ImageFormatInfo_t &info = ImageLoader::ImageFormatInfo( fmt );
int nChannelCount = 0;
if ( info.m_nNumRedBits > 0 ) ++nChannelCount;
if ( info.m_nNumGreenBits > 0 ) ++nChannelCount;
if ( info.m_nNumBlueBits > 0 ) ++nChannelCount;
if ( info.m_nNumAlphaBits > 0 ) ++nChannelCount;
if ( nChannelCount > m_nMaxChannels )
{
dstFmt = ComputeDestFormat( fmt, m_nMaxChannels );
}
pDstImage->CopyFrom( pSrcImage, dstFmt );
}
void CDmeTP_ChangeColorChannels::ProcessTexture( CDmeTexture *pSrcTexture, CDmeTexture *pDstTexture )
{
pDstTexture->ProcessTexture( pSrcTexture, this, &CDmeTP_ChangeColorChannels::ProcessImage );
}
/*
class CDmeTP_OneOverMipLevelInAlpha : public CDmeTextureProcessor
{
DEFINE_ELEMENT( CDmeTP_OneOverMipLevelInAlpha, CDmeTextureProcessor );
public:
};
IMPLEMENT_ELEMENT_FACTORY( DmeTP_OneOverMipLevelInAlpha, CDmeTP_OneOverMipLevelInAlpha );
class CDmeTP_PreMultiplyColorByOneOverMipLevel : public CDmeTextureProcessor
{
DEFINE_ELEMENT( CDmeTP_PreMultiplyColorByOneOverMipLevel, CDmeTextureProcessor );
public:
};
IMPLEMENT_ELEMENT_FACTORY( DmeTP_PreMultiplyColorByOneOverMipLevel, CDmeTP_PreMultiplyColorByOneOverMipLevel );
class CDmeTP_NormalToDuDv : public CDmeTextureProcessor
{
DEFINE_ELEMENT( CDmeTP_NormalToDuDv, CDmeTextureProcessor );
public:
};
IMPLEMENT_ELEMENT_FACTORY( DmeTP_NormalToDuDv, CDmeTP_NormalToDuDv );
class CDmeTP_CompressTexture : public CDmeTextureProcessor
{
DEFINE_ELEMENT( CDmeTP_CompressTexture, CDmeTextureProcessor );
public:
CDmaVar< bool > m_bNormalGAMap;
CDmaVar< bool > m_bDXT5; // FIXME: Should nocompress/dxt5 be combined?
CDmaVar< bool > m_bNoCompress;
};
IMPLEMENT_ELEMENT_FACTORY( DmeTP_CompressTexture, CDmeTP_CompressTexture );
class CDmeTP_GenerateMipmaps : public CDmeTextureProcessor
{
DEFINE_ELEMENT( CDmeTP_GenerateMipmaps, CDmeTextureProcessor );
public:
CDmaVar< bool > m_bNoNice;
CDmaVar< bool > m_bAlphaTest;
CDmaVar< float > m_flAlphaThreshhold;
CDmaVar< float > m_flAlphaThreshholdHigh;
};
IMPLEMENT_ELEMENT_FACTORY( DmeTP_GenerateMipmaps, CDmeTP_GenerateMipmaps );
*/