-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathRenderSlang.cpp
More file actions
415 lines (365 loc) · 17.3 KB
/
RenderSlang.cpp
File metadata and controls
415 lines (365 loc) · 17.3 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXTest/External/Catch/catch.hpp>
#include <MaterialXTest/MaterialXRender/RenderUtil.h>
#include <MaterialXGenSlang/SlangShaderGenerator.h>
#include <MaterialXRenderSlang/SlangRenderer.h>
#include <MaterialXRenderSlang/SlangProgram.h>
#include <MaterialXRender/GeometryHandler.h>
#include <MaterialXRender/StbImageLoader.h>
#if defined(MATERIALX_BUILD_OIIO)
#include <MaterialXRender/OiioImageLoader.h>
#endif
#include <MaterialXFormat/Util.h>
namespace mx = MaterialX;
//
// Render validation tester for the GLSL shading language
//
class SlangShaderRenderTester : public RenderUtil::ShaderRenderTester
{
public:
explicit SlangShaderRenderTester(mx::ShaderGeneratorPtr shaderGenerator) :
RenderUtil::ShaderRenderTester(shaderGenerator)
{
}
protected:
void loadAdditionalLibraries(mx::DocumentPtr document,
GenShaderUtil::TestSuiteOptions& options) override;
void registerLights(mx::DocumentPtr document, const GenShaderUtil::TestSuiteOptions& options,
mx::GenContext& context) override;
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;
bool saveImage(const mx::FilePath& filePath, mx::ConstImagePtr image, bool verticalFlip) const override;
mx::SlangRendererPtr _renderer;
mx::LightHandlerPtr _lightHandler;
};
// In addition to standard texture and shader definition libraries, additional lighting files
// are loaded in. If no files are specified in the input options, a sample
// compound light type and a set of lights in a "light rig" are loaded in to a given
// document.
void SlangShaderRenderTester::loadAdditionalLibraries(mx::DocumentPtr document,
GenShaderUtil::TestSuiteOptions& options)
{
mx::FilePath lightDir = mx::getDefaultDataSearchPath().find("resources/Materials/TestSuite/lights");
for (const auto& lightFile : options.lightFiles)
{
loadLibrary(lightDir / mx::FilePath(lightFile), document);
}
}
// Create a light handler and populate it based on lights found in a given document
void SlangShaderRenderTester::registerLights(mx::DocumentPtr document,
const GenShaderUtil::TestSuiteOptions& options,
mx::GenContext& context)
{
_lightHandler = mx::LightHandler::create();
// Scan for lights
if (options.enableDirectLighting)
{
std::vector<mx::NodePtr> lights;
_lightHandler->findLights(document, lights);
_lightHandler->registerLights(document, lights, context);
// Set the list of lights on the with the generator
_lightHandler->setLightSources(lights);
}
// Load environment lights.
mx::ImagePtr envRadiance = _renderer->getImageHandler()->acquireImage(options.radianceIBLPath);
mx::ImagePtr envIrradiance = _renderer->getImageHandler()->acquireImage(options.irradianceIBLPath);
REQUIRE(envRadiance);
REQUIRE(envIrradiance);
// Apply light settings for render tests.
_lightHandler->setEnvRadianceMap(envRadiance);
_lightHandler->setEnvIrradianceMap(envIrradiance);
_lightHandler->setEnvSampleCount(options.envSampleCount);
_lightHandler->setRefractionTwoSided(true);
}
//
// Create a renderer with the appropriate image, geometry and light handlers.
// The light handler on the renderer is cleared on initialization to indicate no lighting
// is required. During code generation, if the element to validate requires lighting then
// the handler _lightHandler will be used.
//
void SlangShaderRenderTester::createRenderer(std::ostream& log)
{
bool initialized = false;
try
{
_renderer = mx::SlangRenderer::create();
_renderer->initialize();
// Set image handler on renderer
mx::StbImageLoaderPtr stbLoader = mx::StbImageLoader::create();
mx::ImageHandlerPtr imageHandler = _renderer->createImageHandler(stbLoader);
imageHandler->setSearchPath(mx::getDefaultDataSearchPath());
#if defined(MATERIALX_BUILD_OIIO)
mx::OiioImageLoaderPtr oiioLoader = mx::OiioImageLoader::create();
imageHandler->addLoader(oiioLoader);
#endif
_renderer->setImageHandler(imageHandler);
// Set light handler.
_renderer->setLightHandler(nullptr);
initialized = true;
}
catch (mx::ExceptionRenderError& e)
{
for (const auto& error : e.errorLog())
{
log << e.what() << " " << error << std::endl;
}
}
catch (mx::Exception& e)
{
log << e.what() << std::endl;
}
REQUIRE(initialized);
}
bool SlangShaderRenderTester::saveImage(const mx::FilePath& filePath, mx::ConstImagePtr image, bool verticalFlip) const
{
return _renderer->getImageHandler()->saveImage(filePath, image, verticalFlip);
}
bool SlangShaderRenderTester::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)
{
std::cout << "Validating Slang rendering for: " << doc->getSourceUri() << std::endl;
mx::ScopedTimer totalGLSLTime(&profileTimes.languageTimes.totalTime);
const mx::ShaderGenerator& shadergen = context.getShaderGenerator();
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
std::vector<mx::GenOptions> optionsList;
getGenerationOptions(testOptions, context.getOptions(), optionsList);
if (element && doc)
{
log << "------------ Run Slang validation with element: " << element->getNamePath() << "-------------------" << std::endl;
for (auto options : optionsList)
{
profileTimes.elementsTested++;
mx::FilePath outputFilePath = outputPath;
// Note: mkdir will fail if the directory already exists which is ok.
{
mx::ScopedTimer ioDir(&profileTimes.languageTimes.ioTime);
outputFilePath.createDirectory();
// Use separate directory for reduced output
if (options.shaderInterfaceType == mx::SHADER_INTERFACE_REDUCED)
{
outputFilePath = outputFilePath / mx::FilePath("reduced");
outputFilePath.createDirectory();
}
}
std::string shaderPath = mx::FilePath(outputFilePath) / mx::FilePath(shaderName);
mx::ShaderPtr shader;
try
{
mx::ScopedTimer transpTimer(&profileTimes.languageTimes.transparencyTime);
options.hwTransparency = mx::isTransparentSurface(element, shadergen.getTarget());
transpTimer.endTimer();
mx::ScopedTimer generationTimer(&profileTimes.languageTimes.generationTime);
mx::GenOptions& contextOptions = context.getOptions();
contextOptions = options;
contextOptions.targetColorSpaceOverride = "lin_rec709";
shader = shadergen.generate(shaderName, element, context);
generationTimer.endTimer();
}
catch (mx::Exception& e)
{
log << ">> " << e.what() << "\n";
shader = nullptr;
}
CHECK(shader != nullptr);
if (shader == nullptr)
{
log << ">> Failed to generate shader\n";
return false;
}
const std::string& vertexSourceCode = shader->getSourceCode(mx::Stage::VERTEX);
const std::string& pixelSourceCode = shader->getSourceCode(mx::Stage::PIXEL);
CHECK(vertexSourceCode.length() > 0);
CHECK(pixelSourceCode.length() > 0);
if (testOptions.dumpGeneratedCode)
{
mx::ScopedTimer dumpTimer(&profileTimes.languageTimes.ioTime);
std::ofstream file;
file.open(shaderPath + "_vs.slang");
file << vertexSourceCode;
file.close();
file.open(shaderPath + "_ps.slang");
file << pixelSourceCode;
file.close();
}
// Validate
MaterialX::SlangProgramPtr program = _renderer->getProgram();
bool validated = false;
try
{
// Set geometry
mx::GeometryHandlerPtr geomHandler = _renderer->getGeometryHandler();
mx::FilePath geomPath;
if (!testOptions.renderGeometry.isEmpty())
{
if (!testOptions.renderGeometry.isAbsolute())
{
geomPath = searchPath.find("resources/Geometry") / testOptions.renderGeometry;
}
else
{
geomPath = testOptions.renderGeometry;
}
}
else
{
geomPath = searchPath.find("resources/Geometry/sphere.obj");
}
if (!geomHandler->hasGeometry(geomPath))
{
// For test sphere and plane geometry perform a V-flip of texture coordinates.
const std::string baseName = geomPath.getBaseName();
bool texcoordVerticalFlip = baseName == "sphere.obj" || baseName == "plane.obj";
geomHandler->clearGeometry();
geomHandler->loadGeometry(geomPath, texcoordVerticalFlip);
for (mx::MeshPtr mesh : geomHandler->getMeshes())
{
addAdditionalTestStreams(mesh);
}
}
bool isShader = mx::elementRequiresShading(element);
_renderer->setLightHandler(isShader ? _lightHandler : nullptr);
{
mx::ScopedTimer compileTimer(&profileTimes.languageTimes.compileTime);
_renderer->createProgram(shader);
_renderer->validateInputs();
}
if (testOptions.dumpUniformsAndAttributes)
{
mx::ScopedTimer printTimer(&profileTimes.languageTimes.ioTime);
log << "* Uniform:" << std::endl;
program->printUniforms(log);
log << "* Attributes:" << std::endl;
program->printVertexInputs(log);
log << "* Uniform UI Properties:" << std::endl;
const std::string& target = shadergen.getTarget();
const MaterialX::SlangProgram::UniformInputMap& uniforms = program->getUniformsList();
for (const auto& uniform : uniforms)
{
const std::string& path = uniform.second->mxElementPath;
if (path.empty())
{
continue;
}
mx::UIProperties uiProperties;
mx::ElementPtr pathElement = doc->getDescendant(path);
mx::InputPtr input = pathElement ? pathElement->asA<mx::Input>() : nullptr;
if (getUIProperties(input, target, uiProperties) > 0)
{
log << "Program Uniform: " << uniform.first << ". Path: " << path;
if (!uiProperties.uiName.empty())
log << ". UI Name: \"" << uiProperties.uiName << "\"";
if (!uiProperties.uiFolder.empty())
log << ". UI Folder: \"" << uiProperties.uiFolder << "\"";
if (!uiProperties.enumeration.empty())
{
log << ". Enumeration: {";
for (size_t i = 0; i < uiProperties.enumeration.size(); i++)
log << uiProperties.enumeration[i] << " ";
log << "}";
}
if (!uiProperties.enumerationValues.empty())
{
log << ". Enum Values: {";
for (size_t i = 0; i < uiProperties.enumerationValues.size(); i++)
log << uiProperties.enumerationValues[i]->getValueString() << "; ";
log << "}";
}
if (uiProperties.uiMin)
log << ". UI Min: " << uiProperties.uiMin->getValueString();
if (uiProperties.uiMax)
log << ". UI Max: " << uiProperties.uiMax->getValueString();
if (uiProperties.uiSoftMin)
log << ". UI Soft Min: " << uiProperties.uiSoftMin->getValueString();
if (uiProperties.uiSoftMax)
log << ". UI Soft Max: " << uiProperties.uiSoftMax->getValueString();
if (uiProperties.uiStep)
log << ". UI Step: " << uiProperties.uiStep->getValueString();
log << std::endl;
}
}
}
int supersampleFactor = testOptions.enableReferenceQuality ? 8 : 1;
{
mx::ScopedTimer renderTimer(&profileTimes.languageTimes.renderTime);
_renderer->getImageHandler()->setSearchPath(imageSearchPath);
unsigned int width = (unsigned int) testOptions.renderSize[0] * supersampleFactor;
unsigned int height = (unsigned int) testOptions.renderSize[1] * supersampleFactor;
_renderer->setSize(width, height);
for (unsigned int frame = 0; frame < testOptions.framesPerMaterial; frame++)
{
_renderer->render();
}
}
{
mx::ScopedTimer ioTimer(&profileTimes.languageTimes.imageSaveTime);
std::string fileName = shaderPath + "_slang.png";
mx::ImagePtr image = _renderer->captureImage();
if (image)
{
if (supersampleFactor > 1)
{
image = image->applyBoxDownsample(supersampleFactor);
}
_renderer->getImageHandler()->saveImage(fileName, image);
if (imageVec)
{
imageVec->push_back(image);
}
}
}
validated = true;
}
catch (mx::ExceptionRenderError& e)
{
// Always dump shader stages on error
std::ofstream file;
file.open(shaderPath + "_vs.gen.slang");
file << shader->getSourceCode(mx::Stage::VERTEX);
file.close();
file.open(shaderPath + "_ps.gen.slang");
file << shader->getSourceCode(mx::Stage::PIXEL);
file.close();
for (const auto& error : e.errorLog())
{
log << e.what() << " " << error << std::endl;
}
log << ">> Refer to shader code in dump files: " << shaderPath << "_ps.gen.slang and _vs.gen.slang files" << std::endl;
WARN(std::string(e.what()) + " in " + shaderPath);
}
catch (mx::Exception& e)
{
log << e.what() << std::endl;
WARN(std::string(e.what()) + " in " + shaderPath);
}
CHECK(validated);
}
}
return true;
}
TEST_CASE("Render: Slang TestSuite", "[renderslang]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath optionsFilePath = searchPath.find("resources/Materials/TestSuite/_options.mtlx");
SlangShaderRenderTester renderTester(mx::SlangShaderGenerator::create());
renderTester.validate(optionsFilePath);
}