forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderOsl.cpp
More file actions
401 lines (335 loc) · 15 KB
/
RenderOsl.cpp
File metadata and controls
401 lines (335 loc) · 15 KB
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
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXTest/External/Catch/catch.hpp>
#include <MaterialXTest/MaterialXRender/RenderUtil.h>
#include <MaterialXRenderOsl/OslRenderer.h>
#include <MaterialXRender/StbImageLoader.h>
#if defined(MATERIALX_BUILD_OIIO)
#include <MaterialXRender/OiioImageLoader.h>
#endif
#include <MaterialXGenOsl/OslShaderGenerator.h>
#include <MaterialXFormat/Util.h>
namespace mx = MaterialX;
namespace
{
//
// Define local overrides for the tangent frame in shader generation, aligning conventions
// between MaterialXRender and testrender.
//
class TangentOsl : public mx::ShaderNodeImpl
{
public:
static mx::ShaderNodeImplPtr create()
{
return std::make_shared<TangentOsl>();
}
void emitFunctionCall(const mx::ShaderNode& node, mx::GenContext& context, mx::ShaderStage& stage) const override
{
const mx::ShaderGenerator& shadergen = context.getShaderGenerator();
DEFINE_SHADER_STAGE(stage, mx::Stage::PIXEL)
{
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = normalize(vector(N[2], 0, -N[0]))", stage);
shadergen.emitLineEnd(stage);
}
}
};
class BitangentOsl : public mx::ShaderNodeImpl
{
public:
static mx::ShaderNodeImplPtr create()
{
return std::make_shared<BitangentOsl>();
}
void emitFunctionCall(const mx::ShaderNode& node, mx::GenContext& context, mx::ShaderStage& stage) const override
{
const mx::ShaderGenerator& shadergen = context.getShaderGenerator();
DEFINE_SHADER_STAGE(stage, mx::Stage::PIXEL)
{
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = normalize(cross(N, vector(N[2], 0, -N[0])))", stage);
shadergen.emitLineEnd(stage);
}
}
};
} // anonymous namespace
class OslShaderRenderTester : public RenderUtil::ShaderRenderTester
{
public:
explicit OslShaderRenderTester(mx::ShaderGeneratorPtr shaderGenerator, bool useOslCmdStr) :
RenderUtil::ShaderRenderTester(shaderGenerator)
{
// Preprocess to resolve to absolute image file names
// and all non-POSIX separators must be converted to POSIX ones (this only affects running on Windows)
_resolveImageFilenames = true;
_customFilenameResolver = mx::StringResolver::create();
_customFilenameResolver->setFilenameSubstitution("\\\\", "/");
_customFilenameResolver->setFilenameSubstitution("\\", "/");
_useOslCmdStr = useOslCmdStr;
}
protected:
void createRenderer(std::ostream& log) override;
bool runRenderer(const std::string& shaderName,
mx::TypedElementPtr element,
mx::GenContext& context,
mx::DocumentPtr doc,
std::ostream& log,
const GenShaderUtil::TestSuiteOptions& testOptions,
RenderUtil::RenderProfileTimes& profileTimes,
const mx::FileSearchPath& imageSearchPath,
const std::string& outputPath = ".",
mx::ImageVec* imageVec = nullptr) override;
void addSkipFiles() override
{
_skipFiles.insert("standard_surface_onyx_hextiled.mtlx");
_skipFiles.insert("hextiled.mtlx");
}
bool saveImage(const mx::FilePath& filePath, mx::ConstImagePtr image, bool /*verticalFlip*/) const override
{
return _renderer->getImageHandler()->saveImage(filePath, image, false);
}
mx::ImageLoaderPtr _imageLoader;
mx::OslRendererPtr _renderer;
bool _useOslCmdStr;
};
// Renderer setup
void OslShaderRenderTester::createRenderer(std::ostream& log)
{
bool initialized = false;
_renderer = mx::OslRenderer::create();
_imageLoader = mx::StbImageLoader::create();
// Set up additional utilities required to run OSL testing including
// oslc and testrender paths and OSL include path
//
const std::string oslcExecutable(MATERIALX_OSL_BINARY_OSLC);
_renderer->setOslCompilerExecutable(oslcExecutable);
const std::string testRenderExecutable(MATERIALX_OSL_BINARY_TESTRENDER);
_renderer->setOslTestRenderExecutable(testRenderExecutable);
mx::FilePath oslStandardIncludePath = mx::FilePath(MATERIALX_OSL_INCLUDE_PATH);
if (!oslStandardIncludePath.isEmpty())
{
_renderer->setOslIncludePath(mx::FileSearchPath(oslStandardIncludePath));
}
try
{
_renderer->initialize();
mx::StbImageLoaderPtr stbLoader = mx::StbImageLoader::create();
mx::ImageHandlerPtr imageHandler = mx::ImageHandler::create(stbLoader);
imageHandler->setSearchPath(mx::getDefaultDataSearchPath());
#if defined(MATERIALX_BUILD_OIIO)
mx::OiioImageLoaderPtr oiioLoader = mx::OiioImageLoader::create();
imageHandler->addLoader(oiioLoader);
#endif
_renderer->setImageHandler(imageHandler);
_renderer->setLightHandler(nullptr);
initialized = true;
// Pre-compile some required shaders for testrender
if (!oslcExecutable.empty() && !testRenderExecutable.empty())
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath shaderPath = searchPath.find("resources/Utilities/");
_renderer->setOslOutputFilePath(shaderPath);
const std::string OSL_EXTENSION("osl");
for (const mx::FilePath& filename : shaderPath.getFilesInDirectory(OSL_EXTENSION))
{
_renderer->compileOSL(shaderPath / filename);
}
// Set the search path for these compiled shaders.
_renderer->setOslUtilityOSOPath(shaderPath);
}
}
catch (mx::ExceptionRenderError& e)
{
for (const auto& error : e.errorLog())
{
log << e.what() << " " << error << std::endl;
}
}
REQUIRE(initialized);
}
// Renderer execution
bool OslShaderRenderTester::runRenderer(const std::string& shaderName,
mx::TypedElementPtr element,
mx::GenContext& context,
mx::DocumentPtr doc,
std::ostream& log,
const GenShaderUtil::TestSuiteOptions& testOptions,
RenderUtil::RenderProfileTimes& profileTimes,
const mx::FileSearchPath&,
const std::string& outputPath,
mx::ImageVec* imageVec)
{
std::cout << "Validating OSL rendering for: " << doc->getSourceUri() << std::endl;
mx::ScopedTimer totalOSLTime(&profileTimes.languageTimes.totalTime);
mx::ShaderGenerator& shadergen = context.getShaderGenerator();
std::vector<mx::GenOptions> optionsList;
getGenerationOptions(testOptions, context.getOptions(), optionsList);
if (element && doc)
{
log << "------------ Run OSL validation with element: " << element->getNamePath() << "-------------------" << std::endl;
for (const auto& options : optionsList)
{
profileTimes.elementsTested++;
mx::ShaderPtr shader;
try
{
mx::ScopedTimer genTimer(&profileTimes.languageTimes.generationTime);
mx::GenOptions& contextOptions = context.getOptions();
contextOptions = options;
contextOptions.targetColorSpaceOverride = "lin_rec709";
// Apply local overrides for shader generation.
shadergen.registerImplementation("IM_tangent_vector3_" + mx::OslShaderGenerator::TARGET, TangentOsl::create);
shadergen.registerImplementation("IM_bitangent_vector3_" + mx::OslShaderGenerator::TARGET, BitangentOsl::create);
shader = shadergen.generate(shaderName, element, context);
}
catch (mx::Exception& e)
{
log << ">> " << e.what() << "\n";
shader = nullptr;
}
CHECK(shader != nullptr);
if (shader == nullptr)
{
log << ">> Failed to generate shader\n";
return false;
}
CHECK(shader->getSourceCode().length() > 0);
std::string oslCmdStr = shader->getSourceCode();
/// TODO: this is a temp value.
oslCmdStr = "shader test_node test_node;\n";
oslCmdStr += "shader closure_passthrough closure_passthrough;\n";
oslCmdStr += "connect test_node.Out_Ci closure_passthrough.Cin;\n";
std::string shaderPath;
mx::FilePath outputFilePath = outputPath;
// Use separate directory for reduced output
if (options.shaderInterfaceType == mx::SHADER_INTERFACE_REDUCED)
{
outputFilePath = outputFilePath / mx::FilePath("reduced");
}
// Note: mkdir will fail if the directory already exists which is ok.
{
mx::ScopedTimer ioDir(&profileTimes.languageTimes.ioTime);
outputFilePath.createDirectory();
}
shaderPath = mx::FilePath(outputFilePath) / mx::FilePath(shaderName);
// Write out osl file
if (testOptions.dumpGeneratedCode)
{
mx::ScopedTimer ioTimer(&profileTimes.languageTimes.ioTime);
std::ofstream file;
file.open(shaderPath + (_useOslCmdStr ? ".oslcmd" : ".osl"));
file << shader->getSourceCode();
file.close();
}
// Validate
bool validated = false;
try
{
// Set renderer properties.
_renderer->setOslOutputFilePath(outputFilePath);
_renderer->setOslShaderName(shaderName);
_renderer->setRaysPerPixelLit(testOptions.enableReferenceQuality ? 32 : 4);
_renderer->setRaysPerPixelUnlit(testOptions.enableReferenceQuality ? 8 : 1);
// Validate compilation
if (!_useOslCmdStr)
{
mx::ScopedTimer compileTimer(&profileTimes.languageTimes.compileTime);
_renderer->createProgram(shader);
}
_renderer->setSize(static_cast<unsigned int>(testOptions.renderSize[0]), static_cast<unsigned int>(testOptions.renderSize[1]));
const mx::ShaderStage& stage = shader->getStage(mx::Stage::PIXEL);
// Bind IBL image name overrides.
mx::StringVec envOverrides;
std::string envmap_filename("string envmap_filename \"");
envmap_filename += testOptions.radianceIBLPath;
envmap_filename += "\";\n";
envOverrides.push_back(envmap_filename);
_renderer->setEnvShaderParameterOverrides(envOverrides);
const mx::VariableBlock& outputs = stage.getOutputBlock(mx::OSL::OUTPUTS);
if (outputs.size() > 0)
{
const mx::ShaderPort* output = outputs[0];
const mx::TypeSyntax& typeSyntax = shadergen.getSyntax().getTypeSyntax(output->getType());
const std::string& outputName = output->getVariable();
const std::string& outputType = typeSyntax.getTypeAlias().empty() ? typeSyntax.getName() : typeSyntax.getTypeAlias();
const std::string& sceneTemplateFile = (_useOslCmdStr ? "scene_template_oslcmd.xml": "scene_template.xml");
// Set shader output name and type to use
_renderer->setOslShaderOutput(outputName, outputType);
// Get oslcmdstr
_renderer->setOSLCmdStr(oslCmdStr);
_renderer->useOslCommandString(_useOslCmdStr);
// Set scene template file. For now we only have the constant color scene file
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath sceneTemplatePath = searchPath.find("resources/Utilities/");
sceneTemplatePath = sceneTemplatePath / sceneTemplateFile;
_renderer->setOslTestRenderSceneTemplateFile(sceneTemplatePath.asString());
// Validate rendering
{
mx::ScopedTimer renderTimer(&profileTimes.languageTimes.renderTime);
_renderer->render();
if (imageVec)
{
imageVec->push_back(_renderer->captureImage());
}
}
}
else
{
CHECK(false);
log << ">> Shader has no output to render from\n";
}
validated = true;
}
catch (mx::ExceptionRenderError& e)
{
// Always dump shader on error
std::ofstream file;
file.open(shaderPath + ".osl");
file << shader->getSourceCode();
file.close();
for (const auto& error : e.errorLog())
{
log << e.what() << " " << error << std::endl;
}
log << ">> Refer to shader code in dump file: " << shaderPath << ".osl file" << std::endl;
}
catch (mx::Exception& e)
{
log << e.what();
}
CHECK(validated);
}
}
return true;
}
TEST_CASE("Render: OSL TestSuite", "[renderosl]")
{
if (std::string(MATERIALX_OSL_BINARY_OSLC).empty() &&
std::string(MATERIALX_OSL_BINARY_TESTRENDER).empty())
{
INFO("Skipping the OSL test suite as its executable locations haven't been set.");
return;
}
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath optionsFilePath = searchPath.find("resources/Materials/TestSuite/_options.mtlx");
OslShaderRenderTester renderTester(mx::OslShaderGenerator::create(), false);
renderTester.validate(optionsFilePath);
}
TEST_CASE("Render: OSL Nodes TestSuite", "[oslNodes]")
{
if (std::string(MATERIALX_OSL_BINARY_OSLC).empty() &&
std::string(MATERIALX_OSL_BINARY_TESTRENDER).empty())
{
INFO("Skipping the OSL test suite as its executable locations haven't been set.");
return;
}
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath optionsFilePath = searchPath.find("resources/Materials/TestSuite/_options.mtlx");
/// TODO: change to chris' new shader generator
OslShaderRenderTester renderTester(mx::OslShaderGenerator::create(), true);
renderTester.validate(optionsFilePath);
}