forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderUtil.cpp
330 lines (282 loc) · 12.5 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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/RenderUtil.h>
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXRender/Image.h>
#include <MaterialXGenShader/UnitConverter.h>
#include <MaterialXGenShader/Util.h>
namespace mx = MaterialX;
namespace RenderUtil
{
ShaderRenderTester::ShaderRenderTester(mx::ShaderGeneratorPtr shaderGenerator) :
_shaderGenerator(shaderGenerator),
_languageTargetString(shaderGenerator->getLanguage() + "_" + shaderGenerator->getTarget())
{
}
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);
//if (options.checkImplCount)
//{
// stream << "---------------------------------------" << std::endl;
// mx::StringSet whiteList;
// getImplementationWhiteList(whiteList);
// GenShaderUtil::checkImplementationUsage(language, usedImpls, whiteList, dependLib, context, stream);
//}
}
void ShaderRenderTester::loadDependentLibraries(GenShaderUtil::TestSuiteOptions options, mx::FilePath searchPath, mx::DocumentPtr& dependLib)
{
dependLib = mx::createDocument();
const mx::StringVec libraries = { "stdlib", "pbrlib", "lights" };
mx::loadLibraries(libraries, searchPath, dependLib, nullptr);
for (size_t i = 0; i < options.externalLibraryPaths.size(); i++)
{
const mx::FilePath& libraryPath = options.externalLibraryPaths[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 shader definitions used in the test suite.
loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/bxdf/standard_surface.mtlx"), dependLib);
loadLibrary(mx::FilePath::getCurrentPath() / mx::FilePath("libraries/bxdf/usd_preview_surface.mtlx"), dependLib);
// Load any addition per renderer libraries
loadAdditionalLibraries(dependLib, options);
}
bool ShaderRenderTester::validate(const mx::FilePathVec& testRootPaths, const mx::FilePath optionsFilePath)
{
// Test has been turned off so just do nothing.
// Check for an option file
GenShaderUtil::TestSuiteOptions options;
if (!options.readOptions(optionsFilePath))
{
std::cout << "Can't find options file. Skip test." << std::endl;
return false;
}
if (!runTest(options))
{
std::cout << "Language / target: " << _languageTargetString << " not set to run. Skip test." << std::endl;
return false;
}
// Profiling times
RenderUtil::RenderProfileTimes profileTimes;
// Global setup timer
RenderUtil::AdditiveScopedTimer totalTime(profileTimes.totalTime, "Global total time");
#ifdef LOG_TO_FILE
std::ofstream logfile(_languageTargetString + "_render_log.txt");
std::ostream& log(logfile);
std::string docValidLogFilename = _languageTargetString + "_render_doc_validation_log.txt";
std::ofstream docValidLogFile(docValidLogFilename);
std::ostream& docValidLog(docValidLogFile);
std::ofstream profilingLogfile(_languageTargetString + "__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
// 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);
}
RenderUtil::AdditiveScopedTimer ioTimer(profileTimes.ioTime, "Global I/O time");
mx::FilePathVec dirs;
if (options.externalTestPaths.size() == 0)
{
for (const auto& testRoot : testRootPaths)
{
mx::FilePathVec testRootDirs = testRoot.getSubDirectories();
dirs.insert(std::end(dirs), std::begin(testRootDirs), std::end(testRootDirs));
}
}
else
{
// Use test roots from options file
for (size_t i = 0; i < options.externalTestPaths.size(); i++)
{
std::cout << "Test root: " << options.externalTestPaths[i].asString() << std::endl;
dirs.push_back(options.externalTestPaths[i]);
}
}
ioTimer.endTimer();
// Add files to skip
addSkipFiles();
// Library search path
mx::FilePath searchPath = mx::FilePath::getCurrentPath() / mx::FilePath("libraries");
// 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
RenderUtil::AdditiveScopedTimer setupTime(profileTimes.languageTimes.setupTime, "Setup time");
createRenderer(log);
mx::ColorManagementSystemPtr colorManagementSystem = mx::DefaultColorManagementSystem::create(_shaderGenerator->getLanguage());
colorManagementSystem->loadLibrary(dependLib);
_shaderGenerator->setColorManagementSystem(colorManagementSystem);
// Setup Unit system and working space
mx::UnitSystemPtr unitSystem = mx::UnitSystem::create(_shaderGenerator->getLanguage());
_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(searchPath);
registerSourceCodeSearchPaths(context);
// Set target unit space
context.getOptions().targetDistanceUnit = "meter";
setupTime.endTimer();
registerLights(dependLib, options, context);
// Map to replace "/" in Element path names with "_".
mx::StringMap pathMap;
pathMap["/"] = "_";
RenderUtil::AdditiveScopedTimer validateTimer(profileTimes.validateTime, "Global validation time");
RenderUtil::AdditiveScopedTimer renderableSearchTimer(profileTimes.renderableSearchTime, "Global renderable search time");
mx::StringSet usedImpls;
mx::CopyOptions copyOptions;
copyOptions.skipConflictingElements = true;
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 std::string& 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.");
}
doc->importLibrary(dependLib, ©Options);
ioTimer.endTimer();
validateTimer.startTimer();
std::cout << "- Validating MTLX file: " << filename.asString() << std::endl;
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);
renderableSearchTimer.startTimer();
std::vector<mx::TypedElementPtr> elements;
try
{
mx::findRenderableElements(doc, elements);
}
catch (mx::Exception& e)
{
docValidLog << e.what() << std::endl;
WARN("Find renderable elements failed, see: " + docValidLogFilename + " for details.");
}
renderableSearchTimer.endTimer();
std::string outputPath = mx::FilePath(dir) / mx::FilePath(mx::removeExtension(file));
mx::FileSearchPath imageSearchPath(dir);
for (const auto& element : elements)
{
mx::OutputPtr output = element->asA<mx::Output>();
mx::ShaderRefPtr shaderRef = element->asA<mx::ShaderRef>();
mx::NodeDefPtr nodeDef = nullptr;
if (output)
{
nodeDef = output->getConnectedNode()->getNodeDef();
}
else if (shaderRef)
{
nodeDef = shaderRef->getNodeDef();
}
if (nodeDef)
{
mx::string elementName = mx::replaceSubstrings(element->getNamePath(), pathMap);
elementName = mx::createValidName(elementName);
{
renderableSearchTimer.startTimer();
mx::InterfaceElementPtr impl = nodeDef->getImplementation(_shaderGenerator->getTarget(), _shaderGenerator->getLanguage());
renderableSearchTimer.endTimer();
if (impl)
{
if (options.checkImplCount)
{
mx::NodeGraphPtr nodeGraph = impl->asA<mx::NodeGraph>();
mx::InterfaceElementPtr nodeGraphImpl = nodeGraph ? nodeGraph->getImplementation() : nullptr;
usedImpls.insert(nodeGraphImpl ? nodeGraphImpl->getName() : impl->getName());
}
runRenderer(elementName, element, context, doc, log, options, profileTimes, imageSearchPath, outputPath);
}
}
}
}
}
}
// Dump out profiling information
totalTime.endTimer();
printRunLog(profileTimes, options, profilingLog, dependLib);
return true;
}
} // namespace RenderUtil