-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmatInstance.cpp
599 lines (498 loc) · 17.5 KB
/
matInstance.cpp
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "materials/matInstance.h"
#include "materials/materialManager.h"
#include "materials/customMaterialDefinition.h"
#include "materials/processedMaterial.h"
#include "materials/processedFFMaterial.h"
#include "materials/processedShaderMaterial.h"
#include "materials/processedCustomMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "shaderGen/featureMgr.h"
#include "gfx/gfxDevice.h"
#include "gfx/sim/cubemapData.h"
#include "gfx/gfxCubemap.h"
#include "core/util/safeDelete.h"
#include "ts/tsShape.h"
class MatInstParameters;
class MatInstanceParameterHandle : public MaterialParameterHandle
{
public:
virtual ~MatInstanceParameterHandle() {}
MatInstanceParameterHandle(const String& name);
void loadHandle(ProcessedMaterial* pmat);
// MaterialParameterHandle interface
const String& getName() const { return mName; }
virtual bool isValid() const;
virtual S32 getSamplerRegister( U32 pass ) const;
private:
friend class MatInstParameters;
String mName;
MaterialParameterHandle* mProcessedHandle;
};
MatInstanceParameterHandle::MatInstanceParameterHandle(const String& name)
{
mName = name;
mProcessedHandle = NULL;
}
bool MatInstanceParameterHandle::isValid() const
{
return mProcessedHandle && mProcessedHandle->isValid();
}
S32 MatInstanceParameterHandle::getSamplerRegister( U32 pass ) const
{
if ( !mProcessedHandle )
return -1;
return mProcessedHandle->getSamplerRegister( pass );
}
void MatInstanceParameterHandle::loadHandle(ProcessedMaterial* pmat)
{
mProcessedHandle = pmat->getMaterialParameterHandle(mName);
}
MatInstParameters::MatInstParameters()
{
mOwnParameters = false;
mParameters = NULL;
}
MatInstParameters::MatInstParameters(MaterialParameters* matParams)
{
mOwnParameters = false;
mParameters = matParams;
}
void MatInstParameters::loadParameters(ProcessedMaterial* pmat)
{
mOwnParameters = true;
mParameters = pmat->allocMaterialParameters();
}
MatInstParameters::~MatInstParameters()
{
if (mOwnParameters)
SAFE_DELETE(mParameters);
}
const Vector<GFXShaderConstDesc>& MatInstParameters::getShaderConstDesc() const
{
return mParameters->getShaderConstDesc();
}
U32 MatInstParameters::getAlignmentValue(const GFXShaderConstType constType)
{
return mParameters->getAlignmentValue(constType);
}
#define MATINSTPARAMSET(handle, f) \
if (!mParameters) \
return; \
AssertFatal(dynamic_cast<MatInstanceParameterHandle*>(handle), "Invalid handle type!"); \
MatInstanceParameterHandle* mph = static_cast<MatInstanceParameterHandle*>(handle); \
mParameters->set(mph->mProcessedHandle, f); \
void MatInstParameters::set(MaterialParameterHandle* handle, const F32 f)
{
MATINSTPARAMSET(handle, f);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point2F& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point3F& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point4F& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const LinearColorF& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const S32 f)
{
MATINSTPARAMSET(handle, f);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point2I& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point3I& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const Point4I& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv)
{
MATINSTPARAMSET(handle, fv);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType)
{
AssertFatal(dynamic_cast<MatInstanceParameterHandle*>(handle), "Invalid handle type!");
MatInstanceParameterHandle* mph = static_cast<MatInstanceParameterHandle*>(handle);
mParameters->set(mph->mProcessedHandle, mat, matrixType);
}
void MatInstParameters::set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
{
AssertFatal(dynamic_cast<MatInstanceParameterHandle*>(handle), "Invalid handle type!");
MatInstanceParameterHandle* mph = static_cast<MatInstanceParameterHandle*>(handle);
mParameters->set(mph->mProcessedHandle, mat, arraySize, matrixType);
}
#undef MATINSTPARAMSET
//****************************************************************************
// Material Instance
//****************************************************************************
MatInstance::MatInstance( Material &mat )
{
VECTOR_SET_ASSOCIATION( mCurrentHandles );
VECTOR_SET_ASSOCIATION( mCurrentParameters );
mMaterial = &mat;
mCreatedFromCustomMaterial = (dynamic_cast<CustomMaterial *>(&mat) != NULL);
construct();
}
//----------------------------------------------------------------------------
// Construct
//----------------------------------------------------------------------------
void MatInstance::construct()
{
mUserObject = NULL;
mCurPass = -1;
mProcessedMaterial = NULL;
mVertexFormat = NULL;
mMaxStages = 1;
mActiveParameters = NULL;
mDefaultParameters = NULL;
mHasNormalMaps = false;
mUsesHardwareSkinning = false;
mIsForwardLit = false;
mIsValid = false;
mIsHardwareSkinned = false;
needsHighlighting = false;
MATMGR->_track(this);
}
//----------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------
MatInstance::~MatInstance()
{
SAFE_DELETE(mProcessedMaterial);
SAFE_DELETE(mDefaultParameters);
for (U32 i = 0; i < mCurrentHandles.size(); i++)
SAFE_DELETE(mCurrentHandles[i]);
MATMGR->_untrack(this);
}
//----------------------------------------------------------------------------
// Init
//----------------------------------------------------------------------------
bool MatInstance::init( const FeatureSet &features,
const GFXVertexFormat *vertexFormat )
{
AssertFatal( vertexFormat, "MatInstance::init - Got null vertex format!" );
mFeatureList = features;
mVertexFormat = vertexFormat;
SAFE_DELETE(mProcessedMaterial);
mIsValid = processMaterial();
return mIsValid;
}
//----------------------------------------------------------------------------
// reInitialize
//----------------------------------------------------------------------------
bool MatInstance::reInit()
{
if (!mVertexFormat)
{
mIsValid = false;
return mIsValid;
}
SAFE_DELETE(mProcessedMaterial);
deleteAllHooks();
mIsValid = processMaterial();
if ( mIsValid )
{
for (U32 i = 0; i < mCurrentHandles.size(); i++)
mCurrentHandles[i]->loadHandle(mProcessedMaterial);
for (U32 i = 0; i < mCurrentParameters.size(); i++)
mCurrentParameters[i]->loadParameters(mProcessedMaterial);
}
return mIsValid;
}
//----------------------------------------------------------------------------
// Process stages
//----------------------------------------------------------------------------
bool MatInstance::processMaterial()
{
AssertFatal( mMaterial, "MatInstance::processMaterial - Got null material!" );
//AssertFatal( mVertexFormat, "MatInstance::processMaterial - Got null vertex format!" );
if ( !mMaterial || !mVertexFormat )
return false;
SAFE_DELETE(mDefaultParameters);
CustomMaterial *custMat = NULL;
if( dynamic_cast<CustomMaterial*>(mMaterial) )
{
F32 pixVersion = GFX->getPixelShaderVersion();
custMat = static_cast<CustomMaterial*>(mMaterial);
if ((custMat->mVersion > pixVersion) || (custMat->mVersion == 0.0))
{
if(custMat->mFallback)
{
mMaterial = custMat->mFallback;
return processMaterial();
}
else
{
AssertWarn(custMat->mVersion == 0.0f, avar("Can't load CustomMaterial %s for %s, using generic FF fallback",
String(mMaterial->getName()).isEmpty() ? "Unknown" : mMaterial->getName(), custMat->mMapTo.c_str()));
mProcessedMaterial = new ProcessedFFMaterial(*mMaterial);
}
}
else
mProcessedMaterial = new ProcessedCustomMaterial(*mMaterial);
}
else if(GFX->getPixelShaderVersion() > 0.001)
mProcessedMaterial = getShaderMaterial();
else
mProcessedMaterial = new ProcessedFFMaterial(*mMaterial);
if (mProcessedMaterial)
{
mProcessedMaterial->addStateBlockDesc( mUserDefinedState );
mProcessedMaterial->setShaderMacros( mUserMacros );
mProcessedMaterial->setUserObject( mUserObject );
FeatureSet features( mFeatureList );
features.exclude( MATMGR->getExclusionFeatures() );
if (mVertexFormat->hasBlendIndices() && TSShape::smUseHardwareSkinning)
{
features.addFeature( MFT_HardwareSkinning );
}
if( !mProcessedMaterial->init(features, mVertexFormat, mFeaturesDelegate) )
{
Con::errorf( "Failed to initialize material '%s'", getMaterial()->getName() );
SAFE_DELETE( mProcessedMaterial );
return false;
}
mDefaultParameters = new MatInstParameters(mProcessedMaterial->getDefaultMaterialParameters());
mActiveParameters = mDefaultParameters;
const FeatureSet &finalFeatures = mProcessedMaterial->getFeatures();
mHasNormalMaps = finalFeatures.hasFeature( MFT_NormalMap );
mUsesHardwareSkinning = finalFeatures.hasFeature( MFT_HardwareSkinning );
mIsForwardLit = ( custMat && custMat->mForwardLit ) ||
( !finalFeatures.hasFeature( MFT_IsEmissive ) &&
finalFeatures.hasFeature( MFT_ForwardShading ) );
mIsHardwareSkinned = finalFeatures.hasFeature( MFT_HardwareSkinning );
return true;
}
return false;
}
const MatStateHint& MatInstance::getStateHint() const
{
if ( mProcessedMaterial )
return mProcessedMaterial->getStateHint();
else
return MatStateHint::Default;
}
ProcessedMaterial* MatInstance::getShaderMaterial()
{
return new ProcessedShaderMaterial(*mMaterial);
}
void MatInstance::addStateBlockDesc(const GFXStateBlockDesc& desc)
{
mUserDefinedState = desc;
}
void MatInstance::updateStateBlocks()
{
if ( mProcessedMaterial )
mProcessedMaterial->updateStateBlocks();
}
void MatInstance::addShaderMacro( const String &name, const String &value )
{
// Check to see if we already have this macro.
Vector<GFXShaderMacro>::iterator iter = mUserMacros.begin();
for ( ; iter != mUserMacros.end(); iter++ )
{
if ( iter->name == name )
{
iter->value = value;
return;
}
}
// Add a new macro.
mUserMacros.increment();
mUserMacros.last().name = name;
mUserMacros.last().value = value;
}
//----------------------------------------------------------------------------
// Setup pass - needs scenegraph data because the lightmap will change across
// several materials.
//----------------------------------------------------------------------------
bool MatInstance::setupPass(SceneRenderState * state, const SceneData &sgData )
{
PROFILE_SCOPE( MatInstance_SetupPass );
if( !mProcessedMaterial )
return false;
++mCurPass;
if ( !mProcessedMaterial->setupPass( state, sgData, mCurPass ) )
{
mCurPass = -1;
return false;
}
return true;
}
void MatInstance::setTransforms(const MatrixSet &matrixSet, SceneRenderState *state)
{
PROFILE_SCOPE(MatInstance_setTransforms);
mProcessedMaterial->setTransforms(matrixSet, state, getCurPass());
}
void MatInstance::setNodeTransforms(const MatrixF *address, const U32 numTransforms)
{
PROFILE_SCOPE(MatInstance_setNodeTransforms);
mProcessedMaterial->setNodeTransforms(address, numTransforms, getCurPass());
}
void MatInstance::setCustomShaderData(Vector<CustomShaderBindingData> &shaderData)
{
PROFILE_SCOPE(MatInstance_setCustomShaderData);
mProcessedMaterial->setCustomShaderData(shaderData, getCurPass());
}
void MatInstance::setSceneInfo(SceneRenderState * state, const SceneData& sgData)
{
PROFILE_SCOPE(MatInstance_setSceneInfo);
mProcessedMaterial->setSceneInfo(state, sgData, getCurPass());
}
void MatInstance::setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer)
{
mProcessedMaterial->setBuffers(vertBuffer, primBuffer);
}
void MatInstance::setTextureStages(SceneRenderState * state, const SceneData &sgData )
{
PROFILE_SCOPE(MatInstance_setTextureStages);
mProcessedMaterial->setTextureStages(state, sgData, getCurPass());
}
bool MatInstance::isInstanced() const
{
return mProcessedMaterial->getFeatures().hasFeature( MFT_UseInstancing );
}
bool MatInstance::stepInstance()
{
AssertFatal( isInstanced(), "MatInstance::stepInstance - This material isn't instanced!" );
AssertFatal( mCurPass >= 0, "MatInstance::stepInstance - Must be within material setup pass!" );
return mProcessedMaterial->stepInstance();
}
U32 MatInstance::getCurStageNum()
{
return mProcessedMaterial->getStageFromPass(getCurPass());
}
RenderPassData* MatInstance::getPass(U32 pass)
{
return mProcessedMaterial->getPass(pass);
}
bool MatInstance::hasGlow()
{
if( mProcessedMaterial )
return mProcessedMaterial->hasGlow();
else
return false;
}
bool MatInstance::hasAccumulation()
{
if( mProcessedMaterial )
return mProcessedMaterial->hasAccumulation();
else
return false;
}
const FeatureSet& MatInstance::getFeatures() const
{
return mProcessedMaterial->getFeatures();
}
MaterialParameterHandle* MatInstance::getMaterialParameterHandle(const String& name)
{
AssertFatal(mProcessedMaterial, "Not init'ed!");
for (U32 i = 0; i < mCurrentHandles.size(); i++)
{
if (mCurrentHandles[i]->getName().equal(name))
{
return mCurrentHandles[i];
}
}
MatInstanceParameterHandle* mph = new MatInstanceParameterHandle(name);
mph->loadHandle(mProcessedMaterial);
mCurrentHandles.push_back(mph);
return mph;
}
MaterialParameters* MatInstance::allocMaterialParameters()
{
AssertFatal(mProcessedMaterial, "Not init'ed!");
MatInstParameters* mip = new MatInstParameters();
mip->loadParameters(mProcessedMaterial);
mCurrentParameters.push_back(mip);
return mip;
}
void MatInstance::setMaterialParameters(MaterialParameters* param)
{
AssertFatal(mProcessedMaterial, "Not init'ed!");
mProcessedMaterial->setMaterialParameters(param, mCurPass);
AssertFatal(dynamic_cast<MatInstParameters*>(param), "Incorrect param type!");
mActiveParameters = static_cast<MatInstParameters*>(param);
}
MaterialParameters* MatInstance::getMaterialParameters()
{
AssertFatal(mProcessedMaterial, "Not init'ed!");
return mActiveParameters;
}
void MatInstance::dumpShaderInfo() const
{
if ( mMaterial == NULL )
{
Con::errorf( "Trying to get Material information on an invalid MatInstance" );
return;
}
Con::printf( "Material Info for object %s - %s", mMaterial->getName(), mMaterial->mMapTo.c_str() );
if ( mProcessedMaterial == NULL )
{
Con::printf( " [no processed material!]" );
return;
}
mProcessedMaterial->dumpMaterialInfo();
}