Debian Codegraph @

tools/matc/tests/test_matc.cpp

raw
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "MockConfig.h"
20
21 #include <matc/MaterialCompiler.h>
22 #include <matc/MaterialLexer.h>
23 #include <matc/JsonishLexer.h>
24 #include <matc/JsonishParser.h>
25
26 class MaterialLexer: public ::testing::Test {
27 protected:
28 MaterialLexer() = default;
29 ~MaterialLexer() = default;
30 };
31
32 class TestMaterialCompiler {
33 public:
34 explicit TestMaterialCompiler(const matc::MaterialCompiler& materialCompiler) :
35 mMaterialCompiler(materialCompiler) {}
36
37 bool parseMaterial(const char* buffer, size_t size, filamat::MaterialBuilder& builder)
38 const noexcept{
39 return mMaterialCompiler.parseMaterial(buffer, size, builder);
40 }
41
42 bool parseMaterialAsJSON(const char* buffer, size_t size, filamat::MaterialBuilder& builder)
43 const noexcept{
44 return mMaterialCompiler.parseMaterialAsJSON(buffer, size, builder);
45 }
46
47 private:
48 const matc::MaterialCompiler& mMaterialCompiler;
49 };
50
51 static std::string materialSource(R"(
52 material {
53 name : "Filament Default Material",
54 shadingModel : unlit
55 }
56
57 fragment {
58 void material(inout MaterialInputs material) {
59 prepareMaterial(material);
60 material.baseColor.rgb = vec3(0.8);
61 }
62 }
63 )");
64
65 static std::string materialSourceWithTool(R"(
66 material {
67 name : "Filament Default Material",
68 shadingModel : unlit
69 }
70
71 fragment {
72 void material(inout MaterialInputs material) {
73 prepareMaterial(material);
74 material.baseColor.rgb = vec3(0.8);
75 }
76 }
77
78 tool {
79 all of this should be ignored
80 matching braces are fine
81 { } { { } } { }
82 }
83 )");
84
85 static std::string materialSourceWithCommentedBraces(R"(
86 material {
87 name : "Filament Default Material",
88 shadingModel : unlit
89 }
90
91 fragment {
92 void material(inout MaterialInputs material) {
93 prepareMaterial(material);
94 // if (test) {
95 material.baseColor.rgb = vec3(0.8); // if (test) {
96 }
97 }
98 )");
99
100 TEST_F(MaterialLexer, NormalMaterialLexing) {
101 matc::MaterialLexer materialLexer;
102 materialLexer.lex(materialSource.c_str(), materialSource.size(), 1);
103 auto lexemes = materialLexer.getLexemes();
104 // A material grammar is made of identifiers and blocks. The source provided has one identifier,
105 // followed by block,identifier, and block. Total expected is four.
106 EXPECT_EQ(lexemes.size(), 4);
107 }
108
109 TEST_F(MaterialLexer, MaterialCompiler) {
110 matc::MaterialCompiler rawCompiler;
111 TestMaterialCompiler compiler(rawCompiler);
112 filamat::MaterialBuilder unused;
113 bool result = compiler.parseMaterial(materialSource.c_str(), materialSource.size(), unused);
114 EXPECT_EQ(result, true);
115 }
116
117 TEST_F(MaterialLexer, MaterialCompilerWithToolSection) {
118 matc::MaterialCompiler rawCompiler;
119 TestMaterialCompiler compiler(rawCompiler);
120 filamat::MaterialBuilder unused;
121 bool result = compiler.parseMaterial(materialSourceWithTool.c_str(), materialSourceWithTool.size(), unused);
122 EXPECT_EQ(result, true);
123 }
124
125 TEST_F(MaterialLexer, MaterialCompilerWithCommentedBraces) {
126 matc::MaterialCompiler rawCompiler;
127 TestMaterialCompiler compiler(rawCompiler);
128 filamat::MaterialBuilder unused;
129 bool result = compiler.parseMaterial(materialSourceWithCommentedBraces.c_str(), materialSourceWithCommentedBraces.size(), unused);
130 EXPECT_EQ(result, true);
131 }
132
133 TEST_F(MaterialLexer, MaterialParserErrorOnlyIdentifier) {
134 std::string sourceMissingIdentifier(R"(
135 singleIdentifier
136 )");
137 matc::MaterialCompiler rawCompiler;
138 TestMaterialCompiler compiler(rawCompiler);
139 filamat::MaterialBuilder unused;
140 bool result = compiler.parseMaterial(
141 sourceMissingIdentifier.c_str(), sourceMissingIdentifier.size(), unused);
142 EXPECT_EQ(result, false);
143 }
144
145 TEST_F(MaterialLexer, MaterialParserErrorMissingBlock) {
146 std::string sourceMissingBlock(R"(
147 identifier1 {}
148 identifier1
149 )");
150 matc::MaterialCompiler rawCompiler;
151 TestMaterialCompiler compiler(rawCompiler);
152 filamat::MaterialBuilder unused;
153 bool result = compiler.parseMaterial(
154 sourceMissingBlock.c_str(), sourceMissingBlock.size(), unused);
155 EXPECT_EQ(result, false);
156 }
157
158 TEST_F(MaterialLexer, MaterialParserErrorTwoBlock) {
159 std::string sourceTwoBlock(R"(
160 identifier1 {} {}
161 )");
162 matc::MaterialCompiler rawCompiler;
163 TestMaterialCompiler compiler(rawCompiler);
164 filamat::MaterialBuilder unused;
165 bool result = compiler.parseMaterial(sourceTwoBlock.c_str(), sourceTwoBlock.size(), unused);
166 EXPECT_EQ(result, false);
167 }
168
169 TEST_F(MaterialLexer, MaterialParserSyntaxError) {
170 std::string sourceSyntaxError(R"(
171 identifier1 {} # identified2 {}
172 )");
173 matc::MaterialCompiler rawCompiler;
174 TestMaterialCompiler compiler(rawCompiler);
175 filamat::MaterialBuilder unused;
176 bool result = compiler.parseMaterial(sourceSyntaxError.c_str(), sourceSyntaxError.size(), unused);
177 EXPECT_EQ(result, false);
178 }
179
180 static std::string jsonMaterialSource(R"(
181 {
182 "fragment": "void material(inout MaterialInputs material) {
183 vec3 normal = vec3(0.0, 0.0, 1.0);
184 material.normal = normal;
185 prepareMaterial(material);
186 material.baseColor = vec4(1.0, 1.0, 1.0, 1.0) * materialParams.baseColorFactor;
187 material.metallic = materialParams.metallicFactor;
188 material.roughness = materialParams.roughnessFactor;
189 material.ambientOcclusion = 1.0;
190 material.emissive = vec4(0.0);
191 material.reflectance = 0.5;
192 // material.baseColor.xyz = normal;\n}",
193 "material": {
194 "blending": "opaque",
195 "name": "Gltf 2 Metallic-Roughness Material",
196 "parameters": [
197 {
198 "name": "baseColorFactor",
199 "type": "float4"
200 },
201 {
202 "name": "metallicFactor",
203 "type": "float"
204 },
205 {
206 "name": "roughnessFactor",
207 "type": "float"
208 },
209 {
210 "name": "emissiveFactor",
211 "type": "float4"
212 },
213 {
214 "name": "weights2d",
215 "type": "float[9]"
216 }
217 ],
218 "requires": [
219 "position",
220 "tangents",
221 "uv0"
222 ],
223 "shadingModel": "lit"
224 }
225 }
226 )");
227
228 static std::string jsonMaterialSourceNoQuotes(R"({
229
230 material: {
231 blending: opaque,
232 name: albertCamus,
233 parameters: [
234 {
235 name: baseColorFactor,
236 type: float4
237 },
238 {
239 name: metallicFactor,
240 type: float
241 },
242 {
243 name: roughnessFactor,
244 type: float
245 },
246 {
247 name: emissiveFactor,
248 type: float4
249 },
250 {
251 name: weights2d,
252 type: float[9]
253 }
254 ],
255 requires: [
256 position,
257 tangents,
258 uv0
259 ],
260 shadingModel: lit
261 }
262
263 })");
264
265 TEST_F(MaterialLexer, JsonMaterialLexingAndParsing) {
266 matc::JsonishLexer jsonishLexer;
267 jsonishLexer.lex(jsonMaterialSource.c_str(), jsonMaterialSource.size(), 1);
268 auto lexemes = jsonishLexer.getLexemes();
269
270 EXPECT_TRUE(!lexemes.empty());
271
272 matc::JsonishParser jsonishParser(lexemes);
273 std::unique_ptr<matc::JsonishObject> root = jsonishParser.parse();
274
275 auto entries = root->getEntries();
276 EXPECT_TRUE(entries.size() == 2);
277 EXPECT_TRUE(entries.find("fragment") != entries.end());
278 EXPECT_TRUE(entries.find("material") != entries.end());
279 }
280
281 TEST_F(MaterialLexer, JsonMaterialLexingAndParsingNoQuotes) {
282 matc::JsonishLexer jsonishLexer;
283 jsonishLexer.lex(jsonMaterialSourceNoQuotes.c_str(), jsonMaterialSourceNoQuotes.size(), 1);
284 auto lexemes = jsonishLexer.getLexemes();
285
286 EXPECT_TRUE(!lexemes.empty());
287
288 matc::JsonishParser jsonishParser(lexemes);
289 std::unique_ptr<matc::JsonishObject> root = jsonishParser.parse();
290
291 auto entries = root->getEntries();
292 EXPECT_TRUE(entries.size() == 1);
293 EXPECT_TRUE(entries.find("material") != entries.end());
294 }
295
296 TEST_F(MaterialLexer, JsonMaterialCompiler) {
297 matc::MaterialCompiler rawCompiler;
298 TestMaterialCompiler compiler(rawCompiler);
299 filamat::MaterialBuilder unused;
300 bool result = compiler.parseMaterialAsJSON(jsonMaterialSource.c_str(), jsonMaterialSource.size(), unused);
301 EXPECT_EQ(result, true);
302 }
303
304 int main(int argc, char** argv) {
305 ::testing::InitGoogleTest(&argc, argv);
306 return RUN_ALL_TESTS();
307 }