forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenOsl.cpp
134 lines (106 loc) · 5.44 KB
/
GenOsl.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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXTest/GenOsl.h>
#include <MaterialXCore/Document.h>
#include <MaterialXFormat/File.h>
#include <MaterialXGenOsl/OslShaderGenerator.h>
#include <MaterialXGenOsl/OslSyntax.h>
#include <MaterialXGenShader/DefaultColorManagementSystem.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/Util.h>
namespace mx = MaterialX;
TEST_CASE("GenShader: OSL Syntax", "[genosl]")
{
mx::SyntaxPtr syntax = mx::OslSyntax::create();
REQUIRE(syntax->getTypeName(mx::Type::FLOAT) == "float");
REQUIRE(syntax->getTypeName(mx::Type::COLOR3) == "color");
REQUIRE(syntax->getTypeName(mx::Type::VECTOR3) == "vector");
REQUIRE(syntax->getTypeName(mx::Type::FLOATARRAY) == "float");
REQUIRE(syntax->getTypeName(mx::Type::INTEGERARRAY) == "int");
REQUIRE(mx::Type::FLOATARRAY->isArray());
REQUIRE(mx::Type::INTEGERARRAY->isArray());
REQUIRE(syntax->getTypeName(mx::Type::BSDF) == "BSDF");
REQUIRE(syntax->getOutputTypeName(mx::Type::BSDF) == "output BSDF");
// Set fixed precision with one digit
mx::ScopedFloatFormatting format(mx::Value::FloatFormatFixed, 1);
std::string value;
value = syntax->getDefaultValue(mx::Type::FLOAT);
REQUIRE(value == "0.0");
value = syntax->getDefaultValue(mx::Type::COLOR3);
REQUIRE(value == "color(0.0)");
value = syntax->getDefaultValue(mx::Type::COLOR3, true);
REQUIRE(value == "color(0.0)");
value = syntax->getDefaultValue(mx::Type::COLOR4);
REQUIRE(value == "color4(color(0.0), 0.0)");
value = syntax->getDefaultValue(mx::Type::COLOR4, true);
REQUIRE(value == "{color(0.0), 0.0}");
value = syntax->getDefaultValue(mx::Type::FLOATARRAY, true);
REQUIRE(value.empty());
value = syntax->getDefaultValue(mx::Type::INTEGERARRAY, true);
REQUIRE(value.empty());
mx::ValuePtr floatValue = mx::Value::createValue<float>(42.0f);
value = syntax->getValue(mx::Type::FLOAT, *floatValue);
REQUIRE(value == "42.0");
value = syntax->getValue(mx::Type::FLOAT, *floatValue, true);
REQUIRE(value == "42.0");
mx::ValuePtr color3Value = mx::Value::createValue<mx::Color3>(mx::Color3(1.0f, 2.0f, 3.0f));
value = syntax->getValue(mx::Type::COLOR3, *color3Value);
REQUIRE(value == "color(1.0, 2.0, 3.0)");
value = syntax->getValue(mx::Type::COLOR3, *color3Value, true);
REQUIRE(value == "color(1.0, 2.0, 3.0)");
mx::ValuePtr color4Value = mx::Value::createValue<mx::Color4>(mx::Color4(1.0f, 2.0f, 3.0f, 4.0f));
value = syntax->getValue(mx::Type::COLOR4, *color4Value);
REQUIRE(value == "color4(color(1.0, 2.0, 3.0), 4.0)");
value = syntax->getValue(mx::Type::COLOR4, *color4Value, true);
REQUIRE(value == "{color(1.0, 2.0, 3.0), 4.0}");
std::vector<float> floatArray = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f };
mx::ValuePtr floatArrayValue = mx::Value::createValue<std::vector<float>>(floatArray);
value = syntax->getValue(mx::Type::FLOATARRAY, *floatArrayValue);
REQUIRE(value == "{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7}");
std::vector<int> intArray = { 1, 2, 3, 4, 5, 6, 7 };
mx::ValuePtr intArrayValue = mx::Value::createValue<std::vector<int>>(intArray);
value = syntax->getValue(mx::Type::INTEGERARRAY, *intArrayValue);
REQUIRE(value == "{1, 2, 3, 4, 5, 6, 7}");
}
TEST_CASE("GenShader: OSL Implementation Check", "[genosl]")
{
mx::GenContext context(mx::OslShaderGenerator::create());
mx::StringSet generatorSkipNodeTypes;
generatorSkipNodeTypes.insert("light");
mx::StringSet generatorSkipNodeDefs;
GenShaderUtil::checkImplementations(context, generatorSkipNodeTypes, generatorSkipNodeDefs, 65);
}
TEST_CASE("GenShader: OSL Unique Names", "[genosl]")
{
mx::GenContext context(mx::OslShaderGenerator::create());
mx::FilePath searchPath = mx::FilePath::getCurrentPath() / mx::FilePath("libraries");
context.registerSourceCodeSearchPath(searchPath);
// Add path to find OSL include files
context.registerSourceCodeSearchPath(searchPath / mx::FilePath("stdlib/osl"));
GenShaderUtil::testUniqueNames(context, mx::Stage::PIXEL);
}
static void generateOslCode()
{
const mx::FilePath testRootPath = mx::FilePath::getCurrentPath() / mx::FilePath("resources/Materials/TestSuite");
const mx::FilePath testRootPath2 = mx::FilePath::getCurrentPath() / mx::FilePath("resources/Materials/Examples/StandardSurface");
const mx::FilePath testRootPath3 = mx::FilePath::getCurrentPath() / mx::FilePath("resources/Materials/Examples/UsdPreviewSurface");
mx::FilePathVec testRootPaths;
testRootPaths.push_back(testRootPath);
testRootPaths.push_back(testRootPath2);
testRootPaths.push_back(testRootPath3);
const mx::FilePath libSearchPath = mx::FilePath::getCurrentPath() / mx::FilePath("libraries");
mx::FileSearchPath srcSearchPath(libSearchPath.asString());
srcSearchPath.append(libSearchPath / mx::FilePath("stdlib/osl"));
const mx::FilePath logPath("genosl_vanilla_generate_test.txt");
OslShaderGeneratorTester tester(mx::OslShaderGenerator::create(), testRootPaths, libSearchPath, srcSearchPath, logPath);
const mx::GenOptions genOptions;
mx::FilePath optionsFilePath = testRootPath / mx::FilePath("_options.mtlx");
tester.validate(genOptions, optionsFilePath);
}
TEST_CASE("GenShader: OSL Shader Generation", "[genosl]")
{
generateOslCode();
}