forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenShader.cpp
198 lines (168 loc) · 7.36 KB
/
GenShader.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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXCore/Document.h>
#include <MaterialXCore/Observer.h>
#include <MaterialXFormat/XmlIo.h>
#include <MaterialXFormat/File.h>
#include <MaterialXGenShader/HwShaderGenerator.h>
#include <MaterialXGenShader/Nodes/SwizzleNode.h>
#include <MaterialXGenShader/TypeDesc.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXTest/GenShaderUtil.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <set>
namespace mx = MaterialX;
//
// Base tests
//
TEST_CASE("GenShader: Valid Libraries", "[genshader]")
{
mx::DocumentPtr doc = mx::createDocument();
mx::FilePath searchPath = mx::FilePath::getCurrentPath() / mx::FilePath("libraries");
GenShaderUtil::loadLibraries({ "stdlib", "pbrlib" }, searchPath, doc);
std::string validationErrors;
bool valid = doc->validate(&validationErrors);
if (!valid)
{
std::cout << validationErrors << std::endl;
}
REQUIRE(valid);
}
TEST_CASE("GenShader: TypeDesc Check", "[genshader]")
{
// Make sure the standard types are registered
const mx::TypeDesc* floatType = mx::TypeDesc::get("float");
REQUIRE(floatType != nullptr);
REQUIRE(floatType->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT);
const mx::TypeDesc* integerType = mx::TypeDesc::get("integer");
REQUIRE(integerType != nullptr);
REQUIRE(integerType->getBaseType() == mx::TypeDesc::BASETYPE_INTEGER);
const mx::TypeDesc* booleanType = mx::TypeDesc::get("boolean");
REQUIRE(booleanType != nullptr);
REQUIRE(booleanType->getBaseType() == mx::TypeDesc::BASETYPE_BOOLEAN);
const mx::TypeDesc* color2Type = mx::TypeDesc::get("color2");
REQUIRE(color2Type != nullptr);
REQUIRE(color2Type->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT);
REQUIRE(color2Type->getSemantic() == mx::TypeDesc::SEMANTIC_COLOR);
REQUIRE(color2Type->isFloat2());
const mx::TypeDesc* color3Type = mx::TypeDesc::get("color3");
REQUIRE(color3Type != nullptr);
REQUIRE(color3Type->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT);
REQUIRE(color3Type->getSemantic() == mx::TypeDesc::SEMANTIC_COLOR);
REQUIRE(color3Type->isFloat3());
const mx::TypeDesc* color4Type = mx::TypeDesc::get("color4");
REQUIRE(color4Type != nullptr);
REQUIRE(color4Type->getBaseType() == mx::TypeDesc::BASETYPE_FLOAT);
REQUIRE(color4Type->getSemantic() == mx::TypeDesc::SEMANTIC_COLOR);
REQUIRE(color4Type->isFloat4());
// Make sure we can register a new sutom type
const mx::TypeDesc* fooType = mx::TypeDesc::registerType("foo", mx::TypeDesc::BASETYPE_FLOAT, mx::TypeDesc::SEMANTIC_COLOR, 5);
REQUIRE(fooType != nullptr);
// Make sure we can't use a name already take
REQUIRE_THROWS(mx::TypeDesc::registerType("color3", mx::TypeDesc::BASETYPE_FLOAT));
// Make sure we can't request an unknown type
REQUIRE_THROWS(mx::TypeDesc::get("bar"));
}
TEST_CASE("GenShader: OSL Reference Implementation Check", "[genshader]")
{
mx::DocumentPtr doc = mx::createDocument();
mx::FilePath searchPath = mx::FilePath::getCurrentPath() / mx::FilePath("libraries");
GenShaderUtil::loadLibraries({ "stdlib" }, searchPath, doc);
// Set source code search path
mx::FileSearchPath sourceCodeSearchPath;
sourceCodeSearchPath.append(searchPath);
std::filebuf implDumpBuffer;
std::string fileName = "osl_vanilla_implementation_check.txt";
implDumpBuffer.open(fileName, std::ios::out);
std::ostream implDumpStream(&implDumpBuffer);
implDumpStream << "-----------------------------------------------------------------------" << std::endl;
implDumpStream << "Scanning language: osl. Target: reference" << std::endl;
implDumpStream << "-----------------------------------------------------------------------" << std::endl;
const std::string language("osl");
const std::string target("");
std::vector<mx::ImplementationPtr> impls = doc->getImplementations();
implDumpStream << "Existing implementations: " << std::to_string(impls.size()) << std::endl;
implDumpStream << "-----------------------------------------------------------------------" << std::endl;
for (auto impl : impls)
{
if (language == impl->getLanguage() && impl->getTarget().empty())
{
std::string msg("Impl: ");
msg += impl->getName();
mx::NodeDefPtr nodedef = impl->getNodeDef();
if (!nodedef)
{
std::string nodedefName = impl->getNodeDefString();
msg += ". Does NOT have a nodedef with name: " + nodedefName;
}
implDumpStream << msg << std::endl;
}
}
std::string nodeDefNode;
std::string nodeDefType;
unsigned int count = 0;
unsigned int missing = 0;
std::string missing_str;
std::string found_str;
// Scan through every nodedef defined
for (mx::NodeDefPtr nodeDef : doc->getNodeDefs())
{
count++;
std::string nodeDefName = nodeDef->getName();
std::string nodeName = nodeDef->getNodeString();
if (!mx::requiresImplementation(nodeDef))
{
found_str += "No implementation required for nodedef: " + nodeDefName + ", Node: " + nodeName + ".\n";
continue;
}
mx::InterfaceElementPtr inter = nodeDef->getImplementation(target, language);
if (!inter)
{
missing++;
missing_str += "Missing nodeDef implemenation: " + nodeDefName + ", Node: " + nodeName + ".\n";
}
else
{
mx::ImplementationPtr impl = inter->asA<mx::Implementation>();
if (impl)
{
// Scan for file and see if we can read in the contents
std::string sourceContents;
mx::FilePath sourcePath = impl->getFile();
mx::FilePath resolvedPath = sourceCodeSearchPath.find(sourcePath);
bool found = mx::readFile(resolvedPath.asString(), sourceContents);
if (!found)
{
missing++;
missing_str += "Missing source code: " + sourcePath.asString() + " for nodeDef: "
+ nodeDefName + ". Impl: " + impl->getName() + ".\n";
}
else
{
found_str += "Found impl and src for nodedef: " + nodeDefName + ", Node: "
+ nodeName + ". Impl: " + impl->getName() + ".\n";
}
}
else
{
mx::NodeGraphPtr graph = inter->asA<mx::NodeGraph>();
found_str += "Found NodeGraph impl for nodedef: " + nodeDefName + ", Node: "
+ nodeName + ". Impl: " + graph->getName() + ".\n";
}
}
}
implDumpStream << "-----------------------------------------------------------------------" << std::endl;
implDumpStream << "Missing: " << missing << " implementations out of: " << count << " nodedefs\n";
implDumpStream << missing_str << std::endl;
implDumpStream << found_str << std::endl;
implDumpStream << "-----------------------------------------------------------------------" << std::endl;
implDumpBuffer.close();
// To enable once this is true
//REQUIRE(missing == 0);
}