/
Doom3ShaderSystem.cpp
543 lines (426 loc) · 14.6 KB
/
Doom3ShaderSystem.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
#include "Doom3ShaderSystem.h"
#include "ShaderFileLoader.h"
#include "i18n.h"
#include "iradiant.h"
#include "iregistry.h"
#include "ifilesystem.h"
#include "ipreferencesystem.h"
#include "imainframe.h"
#include "ieventmanager.h"
#include "iradiant.h"
#include "igame.h"
#include "iarchive.h"
#include "xmlutil/Node.h"
#include "xmlutil/MissingXMLNodeException.h"
#include "ShaderDefinition.h"
#include "ShaderExpression.h"
#include "debugging/ScopedDebugTimer.h"
#include "module/StaticModule.h"
#include "string/predicate.h"
#include "string/replace.h"
#include "parser/DefBlockTokeniser.h"
#include <functional>
namespace
{
const char* TEXTURE_PREFIX = "textures/";
const char* MISSING_BASEPATH_NODE =
"Failed to find \"/game/filesystem/shaders/basepath\" node \
in game descriptor";
const char* MISSING_EXTENSION_NODE =
"Failed to find \"/game/filesystem/shaders/extension\" node \
in game descriptor";
// Default image maps for optional material stages
const std::string IMAGE_FLAT = "_flat.bmp";
const std::string IMAGE_BLACK = "_black.bmp";
inline std::string getBitmapsPath()
{
return module::GlobalModuleRegistry().getApplicationContext().getBitmapsPath();
}
}
namespace shaders
{
// Constructor
Doom3ShaderSystem::Doom3ShaderSystem() :
_defLoader(std::bind(&Doom3ShaderSystem::loadMaterialFiles, this)),
_enableActiveUpdates(true),
_realised(false)
{}
void Doom3ShaderSystem::construct()
{
_library = std::make_shared<ShaderLibrary>();
_textureManager = std::make_shared<GLTextureManager>();
// Register this class as VFS observer
GlobalFileSystem().addObserver(*this);
}
void Doom3ShaderSystem::destroy()
{
// De-register this class as VFS Observer
GlobalFileSystem().removeObserver(*this);
// Free the shaders if we're in realised state
if (_realised)
{
freeShaders();
}
// Don't destroy the GLTextureManager, it's called from
// the CShader destructors.
}
ShaderLibraryPtr Doom3ShaderSystem::loadMaterialFiles()
{
// Get the shaders path and extension from the XML game file
xml::NodeList nlShaderPath =
GlobalGameManager().currentGame()->getLocalXPath("/filesystem/shaders/basepath");
if (nlShaderPath.empty())
throw xml::MissingXMLNodeException(MISSING_BASEPATH_NODE);
xml::NodeList nlShaderExt =
GlobalGameManager().currentGame()->getLocalXPath("/filesystem/shaders/extension");
if (nlShaderExt.empty())
throw xml::MissingXMLNodeException(MISSING_EXTENSION_NODE);
// Load the shader files from the VFS
std::string sPath = nlShaderPath[0].getContent();
if (!string::ends_with(sPath, "/"))
sPath += "/";
std::string extension = nlShaderExt[0].getContent();
ShaderLibraryPtr library = std::make_shared<ShaderLibrary>();
// Load each file from the global filesystem
{
ScopedDebugTimer timer("ShaderFiles parsed: ");
ShaderFileLoader<ShaderLibrary> loader(GlobalFileSystem(), *library,
sPath, extension);
loader.parseFiles();
}
rMessage() << library->getNumDefinitions() << " shader definitions found." << std::endl;
return library;
}
void Doom3ShaderSystem::realise()
{
if (!_realised)
{
// Start loading defs
_defLoader.start();
_signalDefsLoaded.emit();
_realised = true;
}
}
void Doom3ShaderSystem::unrealise()
{
if (_realised)
{
_signalDefsUnloaded.emit();
freeShaders();
_realised = false;
}
}
void Doom3ShaderSystem::ensureDefsLoaded()
{
// To avoid assigning the pointer everytime, check if the library is empty
if (_library->getNumDefinitions() == 0)
{
_library = _defLoader.get();
}
}
void Doom3ShaderSystem::onFileSystemInitialise()
{
realise();
}
void Doom3ShaderSystem::onFileSystemShutdown()
{
unrealise();
}
void Doom3ShaderSystem::freeShaders() {
_library->clear();
_defLoader.reset();
_textureManager->checkBindings();
activeShadersChangedNotify();
}
void Doom3ShaderSystem::refresh() {
unrealise();
realise();
}
// Is the shader system realised
bool Doom3ShaderSystem::isRealised()
{
// Don't report true until we have at least some definitions loaded
return _realised && _library->getNumDefinitions() > 0;
}
sigc::signal<void>& Doom3ShaderSystem::signal_DefsLoaded()
{
return _signalDefsLoaded;
}
sigc::signal<void>& Doom3ShaderSystem::signal_DefsUnloaded()
{
return _signalDefsUnloaded;
}
// Return a shader by name
MaterialPtr Doom3ShaderSystem::getMaterial(const std::string& name)
{
ensureDefsLoaded();
CShaderPtr shader = _library->findShader(name);
return shader;
}
bool Doom3ShaderSystem::materialExists(const std::string& name)
{
ensureDefsLoaded();
return _library->definitionExists(name);
}
bool Doom3ShaderSystem::materialCanBeModified(const std::string& name)
{
ensureDefsLoaded();
const auto& def = _library->getDefinition(name);
return def.file.getIsPhysicalFile();
}
void Doom3ShaderSystem::foreachShaderName(const ShaderNameCallback& callback)
{
ensureDefsLoaded();
// Pass the call to the Library
_library->foreachShaderName(callback);
}
void Doom3ShaderSystem::setLightingEnabled(bool enabled)
{
ensureDefsLoaded();
if (CShader::m_lightingEnabled != enabled)
{
// First unrealise the lighting of all shaders
_library->foreachShader([](const CShaderPtr& shader)
{
shader->unrealiseLighting();
});
// Set the global (greebo: Does this really need to be done this way?)
CShader::m_lightingEnabled = enabled;
// Now realise the lighting of all shaders
_library->foreachShader([](const CShaderPtr& shader)
{
shader->realiseLighting();
});
}
}
const char* Doom3ShaderSystem::getTexturePrefix() const
{
return TEXTURE_PREFIX;
}
GLTextureManager& Doom3ShaderSystem::getTextureManager()
{
return *_textureManager;
}
// Get default textures
TexturePtr Doom3ShaderSystem::getDefaultInteractionTexture(IShaderLayer::Type type)
{
TexturePtr defaultTex;
// Look up based on layer type
switch (type)
{
case IShaderLayer::DIFFUSE:
case IShaderLayer::SPECULAR:
defaultTex = _textureManager->getBinding(getBitmapsPath() + IMAGE_BLACK);
break;
case IShaderLayer::BUMP:
defaultTex = _textureManager->getBinding(getBitmapsPath() + IMAGE_FLAT);
break;
default:
break;
}
return defaultTex;
}
sigc::signal<void> Doom3ShaderSystem::signal_activeShadersChanged() const
{
return _signalActiveShadersChanged;
}
void Doom3ShaderSystem::activeShadersChangedNotify()
{
if (_enableActiveUpdates)
{
_signalActiveShadersChanged.emit();
}
}
void Doom3ShaderSystem::foreachMaterial(const std::function<void(const MaterialPtr&)>& func)
{
ensureDefsLoaded();
_library->foreachShader(func);
}
TexturePtr Doom3ShaderSystem::loadTextureFromFile(const std::string& filename)
{
// Remove any unused Textures before allocating new ones.
_textureManager->checkBindings();
// Get the binding (i.e. load the texture)
return _textureManager->getBinding(filename);
}
IShaderExpression::Ptr Doom3ShaderSystem::createShaderExpressionFromString(const std::string& exprStr)
{
return ShaderExpression::createFromString(exprStr);
}
MaterialPtr Doom3ShaderSystem::createDefaultMaterial(const std::string& name)
{
return std::make_shared<CShader>(name, _library->getEmptyDefinition(), true);
}
ITableDefinition::Ptr Doom3ShaderSystem::getTable(const std::string& name)
{
ensureDefsLoaded();
return _library->getTableForName(name);
}
const std::string& Doom3ShaderSystem::getName() const
{
static std::string _name(MODULE_SHADERSYSTEM);
return _name;
}
const StringSet& Doom3ShaderSystem::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_GAMEMANAGER);
}
return _dependencies;
}
void Doom3ShaderSystem::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called" << std::endl;
construct();
realise();
#if 0
testShaderExpressionParsing();
#endif
}
// Horrible evil macro to avoid assertion failures if expr is NULL
#define GET_EXPR_OR_RETURN expr = createShaderExpressionFromString(exprStr);\
if (!expr) return;
void Doom3ShaderSystem::testShaderExpressionParsing()
{
// Test a few things
std::string exprStr = "3";
IShaderExpression::Ptr expr;
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3+4";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "(3+4)";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "(4.2)";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3+5+6";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3+(5+6)";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 * 3+5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3+3*5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "(3+3)*5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "(3+3*7)-5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3-3*5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "blinktable[0]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "blinktable[1]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "blinktable[0.3]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "blinksnaptable[0.3]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "xianjittertable[0]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "xianjittertable[time]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3-3*xianjittertable[2]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3+xianjittertable[3]*7";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "(3+xianjittertable[3])*7";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "2.3 % 2";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "2.0 % 0.5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "2 == 2";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1 == 2";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1 != 2";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1.2 != 1.2";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1.2 == 1.2*3";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1.2*3 == 1.2*3";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 == 3 && 1 != 0";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "1 != 1 || 3 == 3";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "4 == 3 || 1 != 0";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "time";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(2) << std::endl;
exprStr = "-3 + 5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 * -5";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 * -5 + 4";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 + -5 * 4";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "3 * 5 * -6";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
exprStr = "decalFade[(time - Parm3)/(parm4 - parm3)]";
GET_EXPR_OR_RETURN;
rMessage() << "Expression " << exprStr << ": " << expr->getValue(0) << std::endl;
}
void Doom3ShaderSystem::shutdownModule()
{
rMessage() << "Doom3ShaderSystem::shutdownModule called" << std::endl;
destroy();
unrealise();
}
// Accessor function encapsulating the static shadersystem instance
Doom3ShaderSystemPtr GetShaderSystem()
{
// Acquire the moduleptr from the module registry
RegisterableModulePtr modulePtr(module::GlobalModuleRegistry().getModule(MODULE_SHADERSYSTEM));
// static_cast it onto our shadersystem type
return std::static_pointer_cast<Doom3ShaderSystem>(modulePtr);
}
GLTextureManager& GetTextureManager()
{
return GetShaderSystem()->getTextureManager();
}
// Static module instance
module::StaticModule<Doom3ShaderSystem> d3ShaderSystemModule;
} // namespace shaders