-
Notifications
You must be signed in to change notification settings - Fork 324
/
RenderUtil.cpp
430 lines (370 loc) · 17.2 KB
/
RenderUtil.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
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXTest/External/Catch/catch.hpp>
#include <MaterialXTest/MaterialXRender/RenderUtil.h>
#include <MaterialXFormat/Util.h>
namespace mx = MaterialX;
namespace RenderUtil
{
ShaderRenderTester::ShaderRenderTester(mx::ShaderGeneratorPtr shaderGenerator) :
_shaderGenerator(shaderGenerator),
_resolveImageFilenames(false),
_emitColorTransforms(true)
{
}
ShaderRenderTester::~ShaderRenderTester()
{
}
// Create a list of generation options based on unit test options
// These options will override the original generation context options.
void ShaderRenderTester::getGenerationOptions(const GenShaderUtil::TestSuiteOptions& testOptions,
const mx::GenOptions& originalOptions,
std::vector<mx::GenOptions>& optionsList)
{
optionsList.clear();
if (testOptions.shaderInterfaces & 1)
{
mx::GenOptions reducedOption = originalOptions;
reducedOption.shaderInterfaceType = mx::SHADER_INTERFACE_REDUCED;
optionsList.push_back(reducedOption);
}
// Alway fallback to complete if no options specified.
if ((testOptions.shaderInterfaces & 2) || optionsList.empty())
{
mx::GenOptions completeOption = originalOptions;
completeOption.shaderInterfaceType = mx::SHADER_INTERFACE_COMPLETE;
optionsList.push_back(completeOption);
}
}
void ShaderRenderTester::printRunLog(const RenderProfileTimes &profileTimes,
const GenShaderUtil::TestSuiteOptions& options,
std::ostream& stream,
mx::DocumentPtr dependLib)
{
profileTimes.print(stream);
stream << "---------------------------------------" << std::endl;
options.print(stream);
}
void ShaderRenderTester::loadDependentLibraries(GenShaderUtil::TestSuiteOptions options, mx::FileSearchPath searchPath, mx::DocumentPtr& dependLib)
{
dependLib = mx::createDocument();
mx::loadLibraries({ "libraries" }, searchPath, dependLib);
for (size_t i = 0; i < options.extraLibraryPaths.size(); i++)
{
const mx::FilePath& libraryPath = options.extraLibraryPaths[i];
for (const mx::FilePath& libraryFile : libraryPath.getFilesInDirectory("mtlx"))
{
std::cout << "Extra library path: " << (libraryPath / libraryFile).asString() << std::endl;
mx::loadLibrary((libraryPath / libraryFile), dependLib);
}
}
// Load any addition per renderer libraries
loadAdditionalLibraries(dependLib, options);
}
void ShaderRenderTester::addSkipFiles()
{
_skipFiles.insert("_options.mtlx");
_skipFiles.insert("light_rig_test_1.mtlx");
_skipFiles.insert("light_rig_test_2.mtlx");
_skipFiles.insert("light_compound_test.mtlx");
_skipFiles.insert("xinclude_search_path.mtlx");
_skipFiles.insert("1_38_parameter_to_input.mtlx");
_skipFiles.insert("1_36_to_1_37.mtlx");
_skipFiles.insert("1_37_to_1_38.mtlx");
_skipFiles.insert("material_element_to_surface_material.mtlx");
}
bool ShaderRenderTester::validate(const mx::FilePath optionsFilePath)
{
#ifdef LOG_TO_FILE
std::ofstream logfile(_shaderGenerator->getTarget() + "_render_log.txt");
std::ostream& log(logfile);
std::string docValidLogFilename = _shaderGenerator->getTarget() + "_render_doc_validation_log.txt";
std::ofstream docValidLogFile(docValidLogFilename);
std::ostream& docValidLog(docValidLogFile);
std::ofstream profilingLogfile(_shaderGenerator->getTarget() + "_render_profiling_log.txt");
std::ostream& profilingLog(profilingLogfile);
#else
std::ostream& log(std::cout);
std::string docValidLogFilename = "std::cout";
std::ostream& docValidLog(std::cout);
std::ostream& profilingLog(std::cout);
#endif
// Test has been turned off so just do nothing.
// Check for an option file
GenShaderUtil::TestSuiteOptions options;
if (!options.readOptions(optionsFilePath))
{
log << "Can't find options file. Skip test." << std::endl;
return false;
}
if (!runTest(options))
{
log << "Target: " << _shaderGenerator->getTarget() << " not set to run. Skip test." << std::endl;
return false;
}
// Profiling times
RenderUtil::RenderProfileTimes profileTimes;
// Global setup timer
mx::ScopedTimer totalTime(&profileTimes.totalTime);
// Add files to override the files in the test suite to be tested.
mx::StringSet testfileOverride;
for (const auto& filterFile : options.overrideFiles)
{
testfileOverride.insert(filterFile);
}
mx::ScopedTimer ioTimer(&profileTimes.ioTime);
mx::FilePathVec dirs;
for (const auto& root : options.renderTestPaths)
{
mx::FilePathVec testRootDirs = root.getSubDirectories();
dirs.insert(std::end(dirs), std::begin(testRootDirs), std::end(testRootDirs));
}
ioTimer.endTimer();
// Add files to skip
addSkipFiles();
// Library search path
mx::FilePath currentPath = mx::FilePath::getCurrentPath();
mx::FileSearchPath searchPath;
searchPath.append(currentPath);
// Load in the library dependencies once
// This will be imported in each test document below
ioTimer.startTimer();
mx::DocumentPtr dependLib;
loadDependentLibraries(options, searchPath, dependLib);
ioTimer.endTimer();
// Create renderers and generators
mx::ScopedTimer setupTime(&profileTimes.languageTimes.setupTime);
createRenderer(log);
mx::ColorManagementSystemPtr colorManagementSystem = mx::DefaultColorManagementSystem::create(_shaderGenerator->getTarget());
colorManagementSystem->loadLibrary(dependLib);
_shaderGenerator->setColorManagementSystem(colorManagementSystem);
// Setup Unit system and working space
mx::UnitSystemPtr unitSystem = mx::UnitSystem::create(_shaderGenerator->getTarget());
_shaderGenerator->setUnitSystem(unitSystem);
mx::UnitConverterRegistryPtr registry = mx::UnitConverterRegistry::create();
mx::UnitTypeDefPtr distanceTypeDef = dependLib->getUnitTypeDef("distance");
registry->addUnitConverter(distanceTypeDef, mx::LinearUnitConverter::create(distanceTypeDef));
mx::UnitTypeDefPtr angleTypeDef = dependLib->getUnitTypeDef("angle");
registry->addUnitConverter(angleTypeDef, mx::LinearUnitConverter::create(angleTypeDef));
_shaderGenerator->getUnitSystem()->loadLibrary(dependLib);
_shaderGenerator->getUnitSystem()->setUnitConverterRegistry(registry);
mx::GenContext context(_shaderGenerator);
context.registerSourceCodeSearchPath(currentPath);
context.registerSourceCodeSearchPath(currentPath / mx::FilePath("libraries/stdlib/genosl/include"));
// Set target unit space
context.getOptions().targetDistanceUnit = "meter";
// Set whether to emit colorspace transforms
context.getOptions().emitColorTransforms = _emitColorTransforms;
// Register shader metadata defined in the libraries.
_shaderGenerator->registerShaderMetadata(dependLib, context);
setupTime.endTimer();
registerLights(dependLib, options, context);
// Map to replace "/" in Element path and ":" in namespaced names with "_".
mx::StringMap pathMap;
pathMap["/"] = "_";
pathMap[":"] = "_";
mx::ScopedTimer validateTimer(&profileTimes.validateTime);
mx::ScopedTimer renderableSearchTimer(&profileTimes.renderableSearchTime);
mx::StringSet usedImpls;
const std::string MTLX_EXTENSION("mtlx");
for (const auto& dir : dirs)
{
ioTimer.startTimer();
mx::FilePathVec files;
files = dir.getFilesInDirectory(MTLX_EXTENSION);
ioTimer.endTimer();
for (const mx::FilePath& file : files)
{
if (_skipFiles.count(file))
{
continue;
}
ioTimer.startTimer();
// Check if a file override set is used and ignore all files
// not part of the override set
if (testfileOverride.size() && testfileOverride.count(file) == 0)
{
ioTimer.endTimer();
continue;
}
const mx::FilePath filename = mx::FilePath(dir) / mx::FilePath(file);
mx::DocumentPtr doc = mx::createDocument();
try
{
mx::FileSearchPath readSearchPath(searchPath);
readSearchPath.append(dir);
mx::readFromXmlFile(doc, filename, readSearchPath);
}
catch (mx::Exception& e)
{
docValidLog << "Failed to load in file: " << filename.asString() << ". Error: " << e.what() << std::endl;
WARN("Failed to load in file: " + filename.asString() + "See: " + docValidLogFilename + " for details.");
}
// For each new file clear the implementation cache.
// Since the new file might contain implementations with names
// colliding with implementations in previous test cases.
context.clearNodeImplementations();
doc->importLibrary(dependLib);
ioTimer.endTimer();
validateTimer.startTimer();
log << "MTLX Filename: " << filename.asString() << std::endl;
// Validate the test document
std::string validationErrors;
bool validDoc = doc->validate(&validationErrors);
if (!validDoc)
{
docValidLog << filename.asString() << std::endl;
docValidLog << validationErrors << std::endl;
}
validateTimer.endTimer();
CHECK(validDoc);
mx::FileSearchPath imageSearchPath(dir);
imageSearchPath.append(searchPath);
// Resolve file names if specified
if (_resolveImageFilenames)
{
mx::flattenFilenames(doc, imageSearchPath, _customFilenameResolver);
}
mx::FilePath outputPath = mx::FilePath(dir) / file;
outputPath.removeExtension();
// Perform bake and use that file for rendering
if (canBake())
{
for (auto& doctoBake : options.bakeSettings)
{
mx::FilePath outputBakeFile = file;
if (doctoBake.bakeFile == outputBakeFile.asString())
{
outputBakeFile.removeExtension();
outputBakeFile = outputPath / (outputBakeFile.asString() + "_baked.mtlx");
runBake(doc, imageSearchPath, outputBakeFile, doctoBake, log);
break;
}
}
}
renderableSearchTimer.startTimer();
std::vector<mx::TypedElementPtr> elements;
try
{
elements = mx::findRenderableElements(doc);
}
catch (mx::Exception& e)
{
docValidLog << e.what() << std::endl;
WARN("Shader generation error in " + filename.asString() + ": " + e.what());
}
renderableSearchTimer.endTimer();
for (const auto& element : elements)
{
const mx::string elementName = mx::createValidName(mx::replaceSubstrings(element->getNamePath(), pathMap));
auto it = std::find_if(options.wedgeSettings.begin(), options.wedgeSettings.end(),
[&file] (const GenShaderUtil::TestSuiteOptions::WedgeSetting& setting) {
return (file.asString() == setting.wedgeFile);
});
const bool performWedge = (it != options.wedgeSettings.end()) ? true : false;
if (!performWedge)
{
runRenderer(elementName, element, context, doc, log, options, profileTimes, imageSearchPath, outputPath, nullptr);
}
else
{
for (auto &wedgesetting: options.wedgeSettings)
{
mx::ImageVec imageVec;
const std::string& wedgeFile = wedgesetting.wedgeFile;
if (wedgeFile != file.asString())
{
continue;
}
// Make this a utility
std::string parameterPath = wedgesetting.parameter;
mx::ElementPtr uniformElement = doc->getDescendant(parameterPath);
if (!uniformElement)
{
std::string nodePath = mx::parentNamePath(parameterPath);
mx::ElementPtr uniformParent = doc->getDescendant(nodePath);
if (uniformParent)
{
mx::NodePtr uniformNode = uniformParent->asA<mx::Node>();
if (uniformNode)
{
mx::StringVec pathVec = mx::splitNamePath(parameterPath);
uniformNode->addInputFromNodeDef(pathVec[pathVec.size() - 1]);
}
}
}
uniformElement = doc->getDescendant(parameterPath);
mx::ValueElementPtr valueElement = uniformElement ? uniformElement->asA<mx::ValueElement>() : nullptr;
if (!valueElement)
{
continue;
}
mx::ValuePtr origPropertyValue(valueElement ? valueElement->getValue() : nullptr);
mx::ValuePtr newValue = valueElement->getValue();
float wedgePropertyMin = wedgesetting.range[0];
float wedgePropertyMax = wedgesetting.range[1];
int wedgeImageCount = std::max(wedgesetting.steps, 2);
float wedgePropertyStep = (wedgePropertyMax - wedgePropertyMin) / (wedgeImageCount - 1);
for (int w = 0; w < wedgeImageCount; w++)
{
bool setValue = false;
float propertyValue = (w == wedgeImageCount - 1) ? wedgePropertyMax : wedgePropertyMin + wedgePropertyStep * w;
if (origPropertyValue->isA<int>())
{
valueElement->setValue(static_cast<int>(propertyValue));
setValue = true;
}
else if (origPropertyValue->isA<float>())
{
valueElement->setValue(propertyValue);
setValue = true;
}
else if (origPropertyValue->isA<mx::Vector2>())
{
mx::Vector2 val(propertyValue, propertyValue);
valueElement->setValue(val);
setValue = true;
}
else if (origPropertyValue->isA<mx::Color3>() ||
origPropertyValue->isA<mx::Vector3>())
{
mx::Vector3 val(propertyValue, propertyValue, propertyValue);
valueElement->setValue(val);
setValue = true;
}
else if (origPropertyValue->isA<mx::Color4>() ||
origPropertyValue->isA<mx::Vector4>())
{
mx::Vector4 val(propertyValue, propertyValue, propertyValue, origPropertyValue->isA<mx::Color4>() ? 1.0f : propertyValue);
valueElement->setValue(val);
setValue = true;
}
if (setValue)
{
runRenderer(elementName, element, context, doc, log, options, profileTimes, imageSearchPath, outputPath, &imageVec);
}
}
if (!imageVec.empty())
{
mx::ImagePtr wedgeImage = mx::createImageStrip(imageVec);
if (wedgeImage)
{
std::string wedgeFileName = mx::createValidName(mx::replaceSubstrings(parameterPath, pathMap));
wedgeFileName += "_" + _shaderGenerator->getTarget() + ".bmp";
mx::FilePath wedgePath = outputPath / wedgeFileName;
saveImage(wedgePath, wedgeImage, true);
}
}
}
}
}
}
}
// Dump out profiling information
totalTime.endTimer();
printRunLog(profileTimes, options, profilingLog, dependLib);
return true;
}
} // namespace RenderUtil