forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Material.cpp
760 lines (671 loc) · 25.1 KB
/
Material.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#include <MaterialXView/Material.h>
#include <MaterialXRenderGlsl/GLTextureHandler.h>
#include <MaterialXRender/Util.h>
#include <MaterialXGenShader/Shader.h>
#include <MaterialXFormat/Util.h>
#include <nanogui/messagedialog.h>
#include <iostream>
namespace {
using MatrixXfProxy = Eigen::Map<const ng::MatrixXf>;
using MatrixXuProxy = Eigen::Map<const ng::MatrixXu>;
const mx::Color4 IMAGE_DEFAULT_COLOR(0, 0, 0, 1);
const float PI = std::acos(-1.0f);
} // anonymous namespace
//
// Material methods
//
bool Material::loadSource(const mx::FilePath& vertexShaderFile, const mx::FilePath& pixelShaderFile, const std::string& shaderName, bool hasTransparency)
{
_hasTransparency = hasTransparency;
if (!_glShader)
{
_glShader = std::make_shared<ng::GLShader>();
}
std::string vertexShader = mx::readFile(vertexShaderFile);
if (vertexShader.empty())
{
return false;
}
std::string pixelShader = mx::readFile(pixelShaderFile);
if (pixelShader.empty())
{
return false;
}
bool initialized = _glShader->init(shaderName, vertexShader, pixelShader);
updateUniformsList();
return initialized;
}
void Material::updateUniformsList()
{
_uniformVariable.clear();
// Must bind to be able to inspect the uniforms
_glShader->bind();
int _programId = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &_programId);
int uniformCount = -1;
int uniformSize = -1;
GLenum uniformType = 0;
int maxNameLength = 0;
glGetProgramiv(_programId, GL_ACTIVE_UNIFORMS, &uniformCount);
glGetProgramiv(_programId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
char* uniformName = new char[maxNameLength];
for (int i = 0; i < uniformCount; i++)
{
glGetActiveUniform(_programId, GLuint(i), maxNameLength, nullptr, &uniformSize, &uniformType, uniformName);
GLint uniformLocation = glGetUniformLocation(_programId, uniformName);
if (uniformLocation >= 0)
{
_uniformVariable.insert(uniformName);
}
}
delete[] uniformName;
}
bool Material::generateShader(mx::GenContext& context)
{
if (!_elem)
{
return false;
}
_hasTransparency = mx::isTransparentSurface(_elem, context.getShaderGenerator());
mx::GenContext materialContext = context;
materialContext.getOptions().hwTransparency = _hasTransparency;
materialContext.getOptions().hwShadowMap = materialContext.getOptions().hwShadowMap && !_hasTransparency;
_hwShader = createShader("Shader", materialContext, _elem);
if (!_hwShader)
{
return false;
}
std::string vertexShader = _hwShader->getSourceCode(mx::Stage::VERTEX);
std::string pixelShader = _hwShader->getSourceCode(mx::Stage::PIXEL);
_glShader = std::make_shared<ng::GLShader>();
_glShader->init(_elem->getNamePath(), vertexShader, pixelShader);
updateUniformsList();
return true;
}
bool Material::generateShader(mx::ShaderPtr hwShader)
{
_hwShader = hwShader;
std::string vertexShader = _hwShader->getSourceCode(mx::Stage::VERTEX);
std::string pixelShader = _hwShader->getSourceCode(mx::Stage::PIXEL);
_glShader = std::make_shared<ng::GLShader>();
return _glShader->init(hwShader->getName(), vertexShader, pixelShader);
}
bool Material::generateEnvironmentShader(mx::GenContext& context,
const mx::FilePath& filename,
mx::DocumentPtr stdLib,
const mx::FilePath& imagePath)
{
// Read in the environment nodegraph.
mx::DocumentPtr doc = mx::createDocument();
doc->importLibrary(stdLib);
mx::DocumentPtr envDoc = mx::createDocument();
mx::readFromXmlFile(envDoc, filename);
doc->importLibrary(envDoc);
mx::NodeGraphPtr envGraph = doc->getNodeGraph("environmentDraw");
if (!envGraph)
{
return false;
}
mx::NodePtr image = envGraph->getNode("envImage");
if (!image)
{
return false;
}
image->setParameterValue("file", imagePath.asString(), mx::FILENAME_TYPE_STRING);
mx::OutputPtr output = envGraph->getOutput("out");
if (!output)
{
return false;
}
// Create the shader.
std::string shaderName = "__ENV_SHADER__";
try
{
_hwShader = createShader(shaderName, context, output);
}
catch (std::exception&)
{
return false;
}
return generateShader(_hwShader);
}
void Material::bindShader()
{
if (_glShader)
{
_glShader->bind();
}
}
void Material::bindMesh(mx::MeshPtr mesh) const
{
if (!mesh || !_glShader)
{
return;
}
_glShader->bind();
if (_glShader->attrib(mx::HW::IN_POSITION) != -1)
{
mx::MeshStreamPtr stream = mesh->getStream(mx::MeshStream::POSITION_ATTRIBUTE, 0);
mx::MeshFloatBuffer &buffer = stream->getData();
MatrixXfProxy positions(&buffer[0], stream->getStride(), buffer.size() / stream->getStride());
_glShader->uploadAttrib(mx::HW::IN_POSITION, positions);
}
if (_glShader->attrib(mx::HW::IN_NORMAL, false) != -1)
{
mx::MeshStreamPtr stream = mesh->getStream(mx::MeshStream::NORMAL_ATTRIBUTE, 0);
mx::MeshFloatBuffer &buffer = stream->getData();
MatrixXfProxy normals(&buffer[0], stream->getStride(), buffer.size() / stream->getStride());
_glShader->uploadAttrib(mx::HW::IN_NORMAL, normals);
}
if (_glShader->attrib(mx::HW::IN_TANGENT, false) != -1)
{
mx::MeshStreamPtr stream = mesh->getStream(mx::MeshStream::TANGENT_ATTRIBUTE, 0);
mx::MeshFloatBuffer &buffer = stream->getData();
MatrixXfProxy tangents(&buffer[0], stream->getStride(), buffer.size() / stream->getStride());
_glShader->uploadAttrib(mx::HW::IN_TANGENT, tangents);
}
const std::string texcoord = mx::HW::IN_TEXCOORD + "_0";
if (_glShader->attrib(texcoord, false) != -1)
{
mx::MeshStreamPtr stream = mesh->getStream(mx::MeshStream::TEXCOORD_ATTRIBUTE, 0);
mx::MeshFloatBuffer &buffer = stream->getData();
MatrixXfProxy texcoords(&buffer[0], stream->getStride(), buffer.size() / stream->getStride());
_glShader->uploadAttrib(texcoord, texcoords);
}
}
bool Material::bindPartition(mx::MeshPartitionPtr part) const
{
if (!_glShader)
{
return false;
}
_glShader->bind();
MatrixXuProxy indices(&part->getIndices()[0], 3, part->getIndices().size() / 3);
_glShader->uploadIndices(indices);
return true;
}
void Material::bindViewInformation(const mx::Matrix44& world, const mx::Matrix44& view, const mx::Matrix44& proj)
{
if (!_glShader)
{
return;
}
mx::Matrix44 viewProj = view * proj;
mx::Matrix44 invView = view.getInverse();
mx::Matrix44 invTransWorld = world.getInverse().getTranspose();
mx::Vector3 viewPosition(invView[3][0], invView[3][1], invView[3][2]);
// Bind view properties.
_glShader->setUniform(mx::HW::WORLD_MATRIX, ng::Matrix4f(world.data()), false);
_glShader->setUniform(mx::HW::VIEW_PROJECTION_MATRIX, ng::Matrix4f(viewProj.data()), false);
_glShader->setUniform(mx::HW::WORLD_INVERSE_TRANSPOSE_MATRIX, ng::Matrix4f(invTransWorld.data()), false);
_glShader->setUniform(mx::HW::VIEW_POSITION, ng::Vector3f(viewPosition.data()), false);
}
void Material::unbindImages(mx::ImageHandlerPtr imageHandler)
{
for (mx::ImagePtr image : _boundImages)
{
imageHandler->unbindImage(image);
}
}
void Material::bindImages(mx::ImageHandlerPtr imageHandler, const mx::FileSearchPath& searchPath)
{
if (!_glShader)
{
return;
}
_boundImages.clear();
const mx::VariableBlock* publicUniforms = getPublicUniforms();
for (const auto& uniform : publicUniforms->getVariableOrder())
{
if (uniform->getType() != mx::Type::FILENAME)
{
continue;
}
const std::string& uniformVariable = uniform->getVariable();
std::string filename;
if (uniform->getValue())
{
filename = searchPath.find(uniform->getValue()->getValueString());
}
// Extract out sampling properties
mx::ImageSamplingProperties samplingProperties;
samplingProperties.setProperties(uniformVariable, *publicUniforms);
mx::ImagePtr image = bindImage(filename, uniformVariable, imageHandler, samplingProperties, &IMAGE_DEFAULT_COLOR);
if (image)
{
_boundImages.push_back(image);
}
}
}
mx::ImagePtr Material::bindImage(const mx::FilePath& filePath, const std::string& uniformName, mx::ImageHandlerPtr imageHandler,
const mx::ImageSamplingProperties& samplingProperties, const mx::Color4* fallbackColor)
{
if (!_glShader)
{
return nullptr;
}
// Create a filename resolver for geometric properties.
mx::StringResolverPtr resolver = mx::StringResolver::create();
if (!getUdim().empty())
{
resolver->setUdimString(getUdim());
}
imageHandler->setFilenameResolver(resolver);
// Acquire the given image.
std::string error;
mx::ImagePtr image = imageHandler->acquireImage(filePath, true, fallbackColor, &error);
if (!error.empty())
{
std::cerr << error << std::endl;
}
if (!image)
{
return nullptr;
}
// Bind the image and set its sampling properties.
if (imageHandler->bindImage(image, samplingProperties))
{
mx::GLTextureHandlerPtr textureHandler = std::static_pointer_cast<mx::GLTextureHandler>(imageHandler);
int textureLocation = textureHandler->getBoundTextureLocation(image->getResourceId());
if (textureLocation >= 0)
{
_glShader->setUniform(uniformName, textureLocation, false);
return image;
}
}
return nullptr;
}
void Material::bindUniform(const std::string& name, mx::ConstValuePtr value)
{
if (!value)
{
return;
}
if (value->isA<float>())
{
float v = value->asA<float>();
_glShader->setUniform(name, v);
}
else if (value->isA<int>())
{
int v = value->asA<int>();
_glShader->setUniform(name, v);
}
else if (value->isA<bool>())
{
bool v = value->asA<bool>();
_glShader->setUniform(name, v);
}
else if (value->isA<mx::Color2>())
{
mx::Color2 v = value->asA<mx::Color2>();
_glShader->setUniform(name, ng::Vector2f(v.data()));
}
else if (value->isA<mx::Color3>())
{
mx::Color3 v = value->asA<mx::Color3>();
_glShader->setUniform(name, ng::Vector3f(v.data()));
}
else if (value->isA<mx::Color4>())
{
mx::Color4 v = value->asA<mx::Color4>();
_glShader->setUniform(name, ng::Vector4f(v.data()));
}
else if (value->isA<mx::Vector2>())
{
mx::Vector2 v = value->asA<mx::Vector2>();
_glShader->setUniform(name, ng::Vector2f(v.data()));
}
else if (value->isA<mx::Vector3>())
{
mx::Vector3 v = value->asA<mx::Vector3>();
_glShader->setUniform(name, ng::Vector3f(v.data()));
}
else if (value->isA<mx::Vector4>())
{
mx::Vector4 v = value->asA<mx::Vector4>();
_glShader->setUniform(name, ng::Vector4f(v.data()));
}
}
void Material::bindLights(const mx::GenContext& genContext, mx::LightHandlerPtr lightHandler, mx::ImageHandlerPtr imageHandler,
const LightingState& lightingState, const ShadowState& shadowState)
{
if (!_glShader)
{
return;
}
_glShader->bind();
// Bind environment lighting properties.
if (_glShader->uniform(mx::HW::ENV_MATRIX, false) != -1)
{
mx::Matrix44 envRotation = mx::Matrix44::createRotationY(PI) * lightingState.lightTransform;
_glShader->setUniform(mx::HW::ENV_MATRIX, ng::Matrix4f(envRotation.data()));
}
if (_glShader->uniform(mx::HW::ENV_RADIANCE_SAMPLES, false) != -1)
{
_glShader->setUniform(mx::HW::ENV_RADIANCE_SAMPLES, lightingState.envSamples);
}
mx::ImageMap envImages =
{
{ mx::HW::ENV_RADIANCE, lightingState.indirectLighting ? lightHandler->getEnvRadianceMap() : imageHandler->getZeroImage() },
{ mx::HW::ENV_IRRADIANCE, lightingState.indirectLighting ? lightHandler->getEnvIrradianceMap() : imageHandler->getZeroImage() }
};
for (const auto& env : envImages)
{
std::string uniform = env.first;
mx::ImagePtr image = env.second;
if (image && _glShader->uniform(env.first, false) != -1)
{
mx::ImageSamplingProperties samplingProperties;
samplingProperties.uaddressMode = mx::ImageSamplingProperties::AddressMode::PERIODIC;
samplingProperties.vaddressMode = mx::ImageSamplingProperties::AddressMode::CLAMP;
samplingProperties.filterType = mx::ImageSamplingProperties::FilterType::LINEAR;
// Bind the environment image.
if (imageHandler->bindImage(image, samplingProperties))
{
mx::GLTextureHandlerPtr textureHandler = std::static_pointer_cast<mx::GLTextureHandler>(imageHandler);
int textureLocation = textureHandler->getBoundTextureLocation(image->getResourceId());
if (textureLocation >= 0)
{
_glShader->setUniform(uniform, textureLocation, false);
}
// Bind any associated uniforms.
if (genContext.getOptions().hwSpecularEnvironmentMethod == mx::SPECULAR_ENVIRONMENT_FIS)
{
if (uniform == mx::HW::ENV_RADIANCE)
{
if (_glShader->uniform(mx::HW::ENV_RADIANCE_MIPS, false) != -1)
{
_glShader->setUniform(mx::HW::ENV_RADIANCE_MIPS, image->getMaxMipCount());
}
}
}
}
}
}
// Bind direct lighting properties.
if (_glShader->uniform(mx::HW::NUM_ACTIVE_LIGHT_SOURCES, false) != -1)
{
int lightCount = lightingState.directLighting ? (int) lightHandler->getLightSources().size() : 0;
_glShader->setUniform(mx::HW::NUM_ACTIVE_LIGHT_SOURCES, lightCount);
mx::LightIdMap idMap = lightHandler->computeLightIdMap(lightHandler->getLightSources());
size_t index = 0;
for (mx::NodePtr light : lightHandler->getLightSources())
{
auto nodeDef = light->getNodeDef();
if (!nodeDef)
{
continue;
}
const std::string prefix = mx::HW::LIGHT_DATA_INSTANCE + "[" + std::to_string(index) + "]";
// Set light type id
std::string lightType(prefix + ".type");
if (_glShader->uniform(lightType, false) != -1)
{
unsigned int lightTypeValue = idMap[nodeDef->getName()];
_glShader->setUniform(lightType, lightTypeValue);
}
// Set all inputs
for (const auto& input : light->getInputs())
{
// Make sure we have a value to set
if (input->hasValue())
{
std::string inputName(prefix + "." + input->getName());
if (_glShader->uniform(inputName, false) != -1)
{
if (input->getName() == "direction" && input->hasValue() && input->getValue()->isA<mx::Vector3>())
{
mx::Vector3 dir = input->getValue()->asA<mx::Vector3>();
dir = lightingState.lightTransform.transformVector(dir);
bindUniform(inputName, mx::Value::createValue(dir));
}
else
{
bindUniform(inputName, input->getValue());
}
}
}
}
// Set all parameters. Note that upstream node connections are not currently supported.
for (mx::ParameterPtr param : light->getParameters())
{
// Make sure we have a value to set
if (param->hasValue())
{
std::string paramName(prefix + "." + param->getName());
if (_glShader->uniform(paramName, false) != -1)
{
bindUniform(paramName, param->getValue());
}
}
}
++index;
}
}
// Bind shadow map properties
if (shadowState.shadowMap && _glShader->uniform(mx::HW::SHADOW_MAP, false) != -1)
{
mx::ImageSamplingProperties samplingProperties;
samplingProperties.uaddressMode = mx::ImageSamplingProperties::AddressMode::CLAMP;
samplingProperties.vaddressMode = mx::ImageSamplingProperties::AddressMode::CLAMP;
samplingProperties.filterType = mx::ImageSamplingProperties::FilterType::LINEAR;
// Bind the shadow map.
if (imageHandler->bindImage(shadowState.shadowMap, samplingProperties))
{
mx::GLTextureHandlerPtr textureHandler = std::static_pointer_cast<mx::GLTextureHandler>(imageHandler);
int textureLocation = textureHandler->getBoundTextureLocation(shadowState.shadowMap->getResourceId());
if (textureLocation >= 0)
{
_glShader->setUniform(mx::HW::SHADOW_MAP, textureLocation);
}
}
_glShader->setUniform(mx::HW::SHADOW_MATRIX, ng::Matrix4f(shadowState.shadowMatrix.data()), false);
}
// Bind ambient occlusion properties.
if (shadowState.ambientOcclusionMap && _glShader->uniform(mx::HW::AMB_OCC_MAP, false) != -1)
{
mx::ImageSamplingProperties samplingProperties;
samplingProperties.uaddressMode = mx::ImageSamplingProperties::AddressMode::PERIODIC;
samplingProperties.vaddressMode = mx::ImageSamplingProperties::AddressMode::PERIODIC;
samplingProperties.filterType = mx::ImageSamplingProperties::FilterType::LINEAR;
// Bind the ambient occlusion map.
if (imageHandler->bindImage(shadowState.ambientOcclusionMap, samplingProperties))
{
mx::GLTextureHandlerPtr textureHandler = std::static_pointer_cast<mx::GLTextureHandler>(imageHandler);
int textureLocation = textureHandler->getBoundTextureLocation(shadowState.ambientOcclusionMap->getResourceId());
if (textureLocation >= 0)
{
_glShader->setUniform(mx::HW::AMB_OCC_MAP, textureLocation);
}
}
_glShader->setUniform(mx::HW::AMB_OCC_GAIN, shadowState.ambientOcclusionGain, false);
}
// Bind the directional albedo table.
if (genContext.getOptions().hwDirectionalAlbedoMethod == mx::DIRECTIONAL_ALBEDO_TABLE)
{
if (_glShader->uniform(mx::HW::ALBEDO_TABLE, false) != -1)
{
mx::ImageSamplingProperties samplingProperties;
samplingProperties.uaddressMode = mx::ImageSamplingProperties::AddressMode::CLAMP;
samplingProperties.vaddressMode = mx::ImageSamplingProperties::AddressMode::CLAMP;
samplingProperties.filterType = mx::ImageSamplingProperties::FilterType::LINEAR;
mx::ImagePtr albedoTable = lightHandler->getAlbedoTable();
if (imageHandler->bindImage(albedoTable, samplingProperties))
{
mx::GLTextureHandlerPtr textureHandler = std::static_pointer_cast<mx::GLTextureHandler>(imageHandler);
int textureLocation = textureHandler->getBoundTextureLocation(albedoTable->getResourceId());
if (textureLocation >= 0)
{
_glShader->setUniform(mx::HW::ALBEDO_TABLE, textureLocation, false);
}
}
}
}
}
void Material::bindUnits(mx::UnitConverterRegistryPtr& registry, const mx::GenContext& context)
{
static std::string DISTANCE_UNIT_TARGET_NAME = "u_distanceUnitTarget";
mx::ShaderPort* port = nullptr;
mx::VariableBlock* publicUniforms = getPublicUniforms();
if (publicUniforms)
{
// Scan block based on unit name match predicate
port = publicUniforms->find(
[](mx::ShaderPort* port)
{
return (port && (port->getName() == DISTANCE_UNIT_TARGET_NAME));
});
// Check if the uniform exists in the shader program
if (port && !_uniformVariable.count(port->getVariable()))
{
port = nullptr;
}
}
if (port)
{
int intPortValue = registry->getUnitAsInteger(context.getOptions().targetDistanceUnit);
if (intPortValue >= 0)
{
port->setValue(mx::Value::createValue(intPortValue));
_glShader->bind();
if (_glShader->uniform(DISTANCE_UNIT_TARGET_NAME, false) != -1)
{
_glShader->setUniform(DISTANCE_UNIT_TARGET_NAME, intPortValue);
}
}
}
}
void Material::drawPartition(mx::MeshPartitionPtr part) const
{
if (!part || !bindPartition(part))
{
return;
}
_glShader->drawIndexed(GL_TRIANGLES, 0, (uint32_t) part->getFaceCount());
}
mx::VariableBlock* Material::getPublicUniforms() const
{
if (!_hwShader)
{
return nullptr;
}
try
{
mx::ShaderStage& stage = _hwShader->getStage(mx::Stage::PIXEL);
mx::VariableBlock& block = stage.getUniformBlock(mx::HW::PUBLIC_UNIFORMS);
return █
}
catch (mx::Exception& e)
{
new ng::MessageDialog(nullptr, ng::MessageDialog::Type::Warning, "Unable to find shader uniforms", e.what());
}
return nullptr;
}
mx::ShaderPort* Material::findUniform(const std::string& path) const
{
mx::ShaderPort* port = nullptr;
mx::VariableBlock* publicUniforms = getPublicUniforms();
if (publicUniforms)
{
// Scan block based on path match predicate
port = publicUniforms->find(
[path](mx::ShaderPort* port)
{
return (port && mx::stringEndsWith(port->getPath(), path));
});
// Check if the uniform exists in the shader program
if (port && !_uniformVariable.count(port->getVariable()))
{
port = nullptr;
}
}
return port;
}
void Material::changeUniformElement(mx::ShaderPort* uniform, const std::string& value)
{
if (!uniform)
{
throw std::runtime_error("Null ShaderPort");
}
uniform->setValue(mx::Value::createValueFromStrings(value, uniform->getType()->getName()));
mx::ElementPtr element = _doc->getDescendant(uniform->getPath());
if (element)
{
mx::ValueElementPtr valueElement = element->asA<mx::ValueElement>();
if (valueElement)
{
valueElement->setValueString(value);
}
}
}
void Material::setUniformInt(const std::string& path, int value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), value);
std::stringstream intValue;
intValue << value;
changeUniformElement(uniform, intValue.str());
}
}
void Material::setUniformFloat(const std::string& path, float value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), value);
std::stringstream floatValue;
floatValue << value;
changeUniformElement(uniform, floatValue.str());
}
}
void Material::setUniformVec2(const std::string& path, const ng::Vector2f& value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), value);
std::stringstream vec2Value;
vec2Value << value[0] << mx::ARRAY_VALID_SEPARATORS << value[1];
changeUniformElement(uniform, vec2Value.str());
}
}
void Material::setUniformVec3(const std::string& path, const ng::Vector3f& value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), value);
std::stringstream vec3Value;
vec3Value << value[0] << mx::ARRAY_VALID_SEPARATORS << value[1] << mx::ARRAY_VALID_SEPARATORS << value[2];
changeUniformElement(uniform, vec3Value.str());
}
}
void Material::setUniformVec4(const std::string& path, const ng::Vector4f& value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), value);
std::stringstream vec4Value;
vec4Value << value[0] << mx::ARRAY_VALID_SEPARATORS << value[1] << mx::ARRAY_VALID_SEPARATORS << value[2] << mx::ARRAY_VALID_SEPARATORS << value[3];
changeUniformElement(uniform, vec4Value.str());
}
}
void Material::setUniformEnum(const std::string& path, int index, const std::string& value)
{
mx::ShaderPort* uniform = findUniform(path);
if (uniform)
{
getShader()->bind();
getShader()->setUniform(uniform->getVariable(), index);
changeUniformElement(uniform, value);
}
}