/
CShader.cpp
690 lines (545 loc) · 15 KB
/
CShader.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#include "CShader.h"
#include "MaterialManager.h"
#include "MapExpression.h"
#include "iregistry.h"
#include "ishaders.h"
#include "texturelib.h"
#include "gamelib.h"
#include "materials/ParseLib.h"
#include "parser/DefTokeniser.h"
/* CONSTANTS */
namespace {
// Registry path for default light shader
const std::string DEFAULT_LIGHT_PATH = "/defaults/lightShader";
}
namespace shaders
{
/* Constructor. Sets the name and the ShaderTemplate to use.
*/
CShader::CShader(const std::string& name, const ShaderTemplate::Ptr& declaration) :
CShader(name, declaration, false)
{}
CShader::CShader(const std::string& name, const ShaderTemplate::Ptr& declaration, bool isInternal) :
_isInternal(isInternal),
_originalTemplate(declaration),
_template(declaration),
_name(name),
m_bInUse(false),
_visible(true)
{
subscribeToTemplateChanges();
// Realise the shader
realise();
}
CShader::~CShader()
{
_templateChanged.disconnect();
unrealise();
GetTextureManager().checkBindings();
}
float CShader::getSortRequest() const
{
return _template->getSortRequest();
}
void CShader::setSortRequest(float sortRequest)
{
ensureTemplateCopy();
_template->setSortRequest(sortRequest);
}
void CShader::resetSortRequest()
{
ensureTemplateCopy();
_template->resetSortRequest();
}
float CShader::getPolygonOffset() const
{
return _template->getPolygonOffset();
}
void CShader::setPolygonOffset(float offset)
{
ensureTemplateCopy();
_template->setPolygonOffset(offset);
}
TexturePtr CShader::getEditorImage()
{
if (!_editorTexture)
{
auto editorTex = _template->getEditorTexture();
if (!editorTex)
{
// If there is no editor expression defined, use the an image from a layer, but no Bump or speculars
for (const auto& layer : _layers)
{
if (layer->getType() != IShaderLayer::BUMP && layer->getType() != IShaderLayer::SPECULAR &&
std::dynamic_pointer_cast<MapExpression>(layer->getMapExpression()))
{
editorTex = std::static_pointer_cast<MapExpression>(layer->getMapExpression());
break;
}
}
}
// Pass the call to the GLTextureManager to realise this image
_editorTexture = GetTextureManager().getBinding(editorTex);
}
return _editorTexture;
}
IMapExpression::Ptr CShader::getEditorImageExpression()
{
return _template->getEditorTexture();
}
void CShader::setEditorImageExpressionFromString(const std::string& editorImagePath)
{
ensureTemplateCopy();
_editorTexture.reset();
_template->setEditorImageExpressionFromString(editorImagePath);
}
bool CShader::isEditorImageNoTex()
{
return (getEditorImage() == GetTextureManager().getShaderNotFound());
}
IMapExpression::Ptr CShader::getLightFalloffExpression()
{
return _template->getLightFalloff();
}
void CShader::setLightFalloffExpressionFromString(const std::string& expressionString)
{
ensureTemplateCopy();
_template->setLightFalloffExpressionFromString(expressionString);
}
IShaderLayer::MapType CShader::getLightFalloffCubeMapType()
{
return _template->getLightFalloffCubeMapType();
}
void CShader::setLightFalloffCubeMapType(IShaderLayer::MapType type)
{
ensureTemplateCopy();
_template->setLightFalloffCubeMapType(type);
}
/*
* Return the light falloff texture (Z dimension).
*/
TexturePtr CShader::lightFalloffImage() {
// Construct the texture if necessary
if (!_texLightFalloff) {
// Create image. If there is no falloff image defined, use the
// default.
if (_template->getLightFalloff()) {
// create the image
_texLightFalloff = GetTextureManager().getBinding(_template->getLightFalloff());
}
else {
// Find the default light shader in the ShaderSystem and query its
// falloff texture name.
std::string defLight = game::current::getValue<std::string>(DEFAULT_LIGHT_PATH);
MaterialPtr defLightShader = GetShaderSystem()->getMaterial(defLight);
// Cast to a CShader so we can call getFalloffName().
CShaderPtr cshaderPtr = std::static_pointer_cast<CShader>(defLightShader);
// create the image
_texLightFalloff = GetTextureManager().getBinding(cshaderPtr->_template->getLightFalloff());
}
}
// Return the texture
return _texLightFalloff;
}
std::string CShader::getName() const
{
return _name;
}
std::string CShader::getDescription() const
{
return _template->getDescription();
}
void CShader::setDescription(const std::string& description)
{
ensureTemplateCopy();
_template->setDescription(description);
}
bool CShader::IsInUse() const {
return m_bInUse;
}
void CShader::SetInUse(bool bInUse) {
m_bInUse = bInUse;
GetShaderSystem()->activeShadersChangedNotify();
}
int CShader::getMaterialFlags() const
{
return _template->getMaterialFlags();
}
void CShader::setMaterialFlag(Flags flag)
{
ensureTemplateCopy();
_template->setMaterialFlag(flag);
}
void CShader::clearMaterialFlag(Flags flag)
{
ensureTemplateCopy();
_template->clearMaterialFlag(flag);
}
bool CShader::IsDefault() const
{
return _isInternal || _template->getBlockSyntax().fileInfo.name.empty();
}
// get the cull type
Material::CullType CShader::getCullType() const
{
return _template->getCullType();
}
void CShader::setCullType(CullType type)
{
ensureTemplateCopy();
_template->setCullType(type);
}
ClampType CShader::getClampType() const
{
return _template->getClampType();
}
void CShader::setClampType(ClampType type)
{
ensureTemplateCopy();
_template->setClampType(type);
}
int CShader::getSurfaceFlags() const
{
return _template->getSurfaceFlags();
}
void CShader::setSurfaceFlag(Material::SurfaceFlags flag)
{
ensureTemplateCopy();
_template->setSurfaceFlag(flag);
}
void CShader::clearSurfaceFlag(Material::SurfaceFlags flag)
{
ensureTemplateCopy();
_template->clearSurfaceFlag(flag);
}
Material::SurfaceType CShader::getSurfaceType() const
{
return _template->getSurfaceType();
}
void CShader::setSurfaceType(SurfaceType type)
{
ensureTemplateCopy();
_template->setSurfaceType(type);
}
Material::DeformType CShader::getDeformType() const
{
return _template->getDeformType();
}
IShaderExpression::Ptr CShader::getDeformExpression(std::size_t index)
{
return _template->getDeformExpression(index);
}
std::string CShader::getDeformDeclName()
{
return _template->getDeformDeclName();
}
int CShader::getSpectrum() const
{
return _template->getSpectrum();
}
void CShader::setSpectrum(int spectrum)
{
ensureTemplateCopy();
_template->setSpectrum(spectrum);
}
Material::DecalInfo CShader::getDecalInfo() const
{
return _template->getDecalInfo();
}
Material::Coverage CShader::getCoverage() const
{
return _template->getCoverage();
}
// get shader file name (ie the file where this one is defined)
const char* CShader::getShaderFileName() const
{
return _template->getBlockSyntax().fileInfo.name.c_str();
}
void CShader::setShaderFileName(const std::string& fullPath)
{
std::string path = fullPath;
if (path_is_absolute(path.c_str()))
{
auto rootPath = GlobalFileSystem().findRoot(path);
if (rootPath.empty())
{
throw std::invalid_argument("The path " + path + " is not located in the current mod file structure");
}
path = os::getRelativePath(path, rootPath);
}
auto materialsFolder = getMaterialsFolderName();
auto pathRelativeToMaterialsFolder = os::getRelativePath(path, materialsFolder);
// Check if the path starts with a "materials/" folder
// getRelativePath will return the unchanged path if this is not the case
if (pathRelativeToMaterialsFolder == path)
{
throw std::invalid_argument("The path " + path + " does not point to a " + materialsFolder + " folder");
}
auto extension = getMaterialFileExtension();
if (os::getExtension(pathRelativeToMaterialsFolder) != extension)
{
throw std::invalid_argument("The file extension must be " + extension);
}
_template->setFileInfo(vfs::FileInfo(materialsFolder, pathRelativeToMaterialsFolder, vfs::Visibility::NORMAL));
}
const vfs::FileInfo& CShader::getShaderFileInfo() const
{
return _template->getBlockSyntax().fileInfo;
}
std::string CShader::getDefinition()
{
return _template->getBlockContents();
}
int CShader::getParseFlags() const
{
return _template->getParseFlags();
}
bool CShader::isModified()
{
return _template != _originalTemplate;
}
void CShader::setIsModified()
{
ensureTemplateCopy();
}
void CShader::revertModifications()
{
_template = _originalTemplate;
subscribeToTemplateChanges();
// We need to update that layer reference vector on change
unrealise();
realise();
_sigMaterialModified.emit();
}
sigc::signal<void>& CShader::sig_materialChanged()
{
return _sigMaterialModified;
}
std::string CShader::getRenderBumpArguments()
{
return _template->getRenderBumpArguments();
}
std::string CShader::getRenderBumpFlatArguments()
{
return _template->getRenderBumpFlatArguments();
}
const std::string& CShader::getGuiSurfArgument()
{
return _template->getGuiSurfArgument();
}
// -----------------------------------------
void CShader::realise() {
realiseLighting();
}
void CShader::unrealise()
{
_editorTexture.reset();
_texLightFalloff.reset();
unrealiseLighting();
}
// Parse and load image maps for this shader
void CShader::realiseLighting()
{
for (const auto& layer : _template->getLayers())
{
_layers.push_back(layer);
}
}
void CShader::unrealiseLighting()
{
_layers.clear();
}
void CShader::setName(const std::string& name)
{
_name = name;
_sigMaterialModified.emit();
}
IShaderLayer* CShader::firstLayer() const
{
if (_layers.empty())
{
return nullptr;
}
return _layers.front().get();
}
const IShaderLayerVector& CShader::getAllLayers() const
{
return _layers;
}
std::size_t CShader::addLayer(IShaderLayer::Type type)
{
ensureTemplateCopy();
auto newIndex = _template->addLayer(type);
unrealiseLighting();
realiseLighting();
// We need another signal after the realiseLighting call
_sigMaterialModified.emit();
return newIndex;
}
void CShader::removeLayer(std::size_t index)
{
ensureTemplateCopy();
_template->removeLayer(index);
unrealiseLighting();
realiseLighting();
// We need another signal after the realiseLighting call
_sigMaterialModified.emit();
}
void CShader::swapLayerPosition(std::size_t first, std::size_t second)
{
ensureTemplateCopy();
_template->swapLayerPosition(first, second);
unrealiseLighting();
realiseLighting();
// We need another signal after the realiseLighting call
_sigMaterialModified.emit();
}
std::size_t CShader::duplicateLayer(std::size_t index)
{
ensureTemplateCopy();
auto newIndex = _template->duplicateLayer(index);
unrealiseLighting();
realiseLighting();
// We need another signal after the realiseLighting call
_sigMaterialModified.emit();
return newIndex;
}
IEditableShaderLayer::Ptr CShader::getEditableLayer(std::size_t index)
{
ensureTemplateCopy();
const auto& layers = _template->getLayers();
assert(index >= 0 && index < layers.size());
return layers[index];
}
/* Required Material light type predicates */
bool CShader::isAmbientLight() const {
return _template->isAmbientLight();
}
bool CShader::isBlendLight() const {
return _template->isBlendLight();
}
bool CShader::isFogLight() const {
return _template->isFogLight();
}
bool CShader::isCubicLight() const
{
return _template->isCubicLight();
}
void CShader::setIsAmbientLight(bool newValue)
{
ensureTemplateCopy();
_template->setIsAmbientLight(newValue);
}
void CShader::setIsBlendLight(bool newValue)
{
ensureTemplateCopy();
_template->setIsBlendLight(newValue);
}
void CShader::setIsFogLight(bool newValue)
{
ensureTemplateCopy();
_template->setIsFogLight(newValue);
}
void CShader::setIsCubicLight(bool newValue)
{
ensureTemplateCopy();
_template->setIsCubicLight(newValue);
}
bool CShader::lightCastsShadows() const
{
int flags = getMaterialFlags();
return (flags & FLAG_FORCESHADOWS) ||
(!isFogLight() && !isAmbientLight() && !isBlendLight() && !(flags & FLAG_NOSHADOWS));
}
bool CShader::surfaceCastsShadow() const
{
int flags = getMaterialFlags();
return (flags & FLAG_FORCESHADOWS) || !(flags & FLAG_NOSHADOWS);
}
bool CShader::isDrawn() const
{
return _template->getLayers().size() > 0 || (getSurfaceFlags() & SURF_ENTITYGUI) /*|| gui != NULL*/;
}
bool CShader::isDiscrete() const
{
int flags = getSurfaceFlags();
return (flags & SURF_ENTITYGUI) /*|| gui*/ || getDeformType() != DEFORM_NONE ||
getSortRequest() == SORT_SUBVIEW || (flags & SURF_DISCRETE);
}
bool CShader::isVisible() const {
return _visible;
}
void CShader::setVisible(bool visible)
{
_visible = visible;
}
void CShader::ensureTemplateCopy()
{
if (_template != _originalTemplate)
{
return; // copy is already in place
}
// Create a clone of the original template
_template = _originalTemplate->clone();
subscribeToTemplateChanges();
// We need to update that layer reference vector
// as long as it's there
unrealise();
realise();
}
void CShader::commitModifications()
{
if (_template == _originalTemplate) return;
// Replace the contents with our working copy
_originalTemplate->setBlockSyntax(_template->getBlockSyntax());
// Overwrite the original template reference, the material is now unmodified again
_originalTemplate = _template;
}
const ShaderTemplate::Ptr& CShader::getTemplate()
{
return _template;
}
void CShader::subscribeToTemplateChanges()
{
// Disconnect from any signal first
_templateChanged.disconnect();
_templateChanged = _template->sig_TemplateChanged().connect([this]()
{
_sigMaterialModified.emit();
// Check if the editor image needs an update
updateEditorImage();
});
}
void CShader::updateEditorImage()
{
// In case the editor tex is pointing to the fallback "shader not found"
// check if we have some possible replacements
if (!_editorTexture) return;
if (isEditorImageNoTex() || !_template->getEditorTexture())
{
// The editor image is "shader not found", but we might have layers and/or an qer_editorImage defined
// In case we don't have an editor texture expression, remove it from the slot
// we will update it again as soon as it will be requested
_editorTexture.reset();
}
}
void CShader::refreshImageMaps()
{
if (_template->getEditorTexture())
{
GetTextureManager().clearCacheForBindable(_template->getEditorTexture());
}
if (_template->getLightFalloff())
{
GetTextureManager().clearCacheForBindable(_template->getLightFalloff());
}
for (const auto& layer : _template->getLayers())
{
layer->refreshImageMaps();
}
_editorTexture.reset();
_texLightFalloff.reset();
_sigMaterialModified.emit();
}
bool CShader::m_lightingEnabled = false;
} // namespace shaders