1 // Copyright 2020 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,DepthAllValues)23 TEST_F(AmberScriptParserTest, DepthAllValues) {
24 std::string in = R"(
25 SHADER vertex my_shader PASSTHROUGH
26 SHADER fragment my_fragment GLSL
27 # GLSL Shader
28 END
29 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
30 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
31
32 PIPELINE graphics my_pipeline
33 ATTACH my_shader
34 ATTACH my_fragment
35 BIND BUFFER my_fb AS color LOCATION 0
36 BIND BUFFER my_ds AS depth_stencil
37
38 DEPTH
39 TEST on
40 WRITE on
41 COMPARE_OP less_or_equal
42 CLAMP on
43 BOUNDS min 1.5 max 6.7
44 BIAS constant 2.1 clamp 3.5 slope 5.5
45 END
46 END)";
47
48 Parser parser;
49 Result r = parser.Parse(in);
50 ASSERT_TRUE(r.IsSuccess()) << r.Error();
51
52 auto script = parser.GetScript();
53 const auto& pipelines = script->GetPipelines();
54 ASSERT_EQ(1U, pipelines.size());
55
56 auto* pipeline = pipelines[0].get();
57 ASSERT_NE(nullptr, pipeline->GetDepthStencilBuffer().buffer);
58
59 ASSERT_TRUE(pipeline->GetPipelineData()->GetEnableDepthTest());
60 ASSERT_TRUE(pipeline->GetPipelineData()->GetEnableDepthWrite());
61 ASSERT_TRUE(pipeline->GetPipelineData()->GetEnableDepthClamp());
62 ASSERT_FLOAT_EQ(1.5f, pipeline->GetPipelineData()->GetMinDepthBounds());
63 ASSERT_FLOAT_EQ(6.7f, pipeline->GetPipelineData()->GetMaxDepthBounds());
64 ASSERT_FLOAT_EQ(2.1f,
65 pipeline->GetPipelineData()->GetDepthBiasConstantFactor());
66 ASSERT_FLOAT_EQ(3.5f, pipeline->GetPipelineData()->GetDepthBiasClamp());
67 ASSERT_FLOAT_EQ(5.5f, pipeline->GetPipelineData()->GetDepthBiasSlopeFactor());
68 }
69
TEST_F(AmberScriptParserTest,DepthTestMissingValue)70 TEST_F(AmberScriptParserTest, DepthTestMissingValue) {
71 std::string in = R"(
72 SHADER vertex my_shader PASSTHROUGH
73 SHADER fragment my_fragment GLSL
74 # GLSL Shader
75 END
76 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
77 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
78
79 PIPELINE graphics my_pipeline
80 ATTACH my_shader
81 ATTACH my_fragment
82 BIND BUFFER my_fb AS color LOCATION 0
83 BIND BUFFER my_ds AS depth_stencil
84
85 DEPTH
86 TEST
87 WRITE on
88 END
89 END)";
90
91 Parser parser;
92 Result r = parser.Parse(in);
93 ASSERT_FALSE(r.IsSuccess()) << r.Error();
94 EXPECT_EQ("17: invalid value for TEST", r.Error());
95 }
96
TEST_F(AmberScriptParserTest,DepthTestInvalidValue)97 TEST_F(AmberScriptParserTest, DepthTestInvalidValue) {
98 std::string in = R"(
99 SHADER vertex my_shader PASSTHROUGH
100 SHADER fragment my_fragment GLSL
101 # GLSL Shader
102 END
103 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
104 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
105
106 PIPELINE graphics my_pipeline
107 ATTACH my_shader
108 ATTACH my_fragment
109 BIND BUFFER my_fb AS color LOCATION 0
110 BIND BUFFER my_ds AS depth_stencil
111
112 DEPTH
113 TEST foo
114 WRITE on
115 END
116 END)";
117
118 Parser parser;
119 Result r = parser.Parse(in);
120 ASSERT_FALSE(r.IsSuccess()) << r.Error();
121 EXPECT_EQ("16: invalid value for TEST: foo", r.Error());
122 }
123
TEST_F(AmberScriptParserTest,DepthWriteMissingValue)124 TEST_F(AmberScriptParserTest, DepthWriteMissingValue) {
125 std::string in = R"(
126 SHADER vertex my_shader PASSTHROUGH
127 SHADER fragment my_fragment GLSL
128 # GLSL Shader
129 END
130 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
131 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
132
133 PIPELINE graphics my_pipeline
134 ATTACH my_shader
135 ATTACH my_fragment
136 BIND BUFFER my_fb AS color LOCATION 0
137 BIND BUFFER my_ds AS depth_stencil
138
139 DEPTH
140 TEST on
141 WRITE
142 END
143 END)";
144
145 Parser parser;
146 Result r = parser.Parse(in);
147 ASSERT_FALSE(r.IsSuccess()) << r.Error();
148 EXPECT_EQ("18: invalid value for WRITE", r.Error());
149 }
150
TEST_F(AmberScriptParserTest,DepthWriteInvalidValue)151 TEST_F(AmberScriptParserTest, DepthWriteInvalidValue) {
152 std::string in = R"(
153 SHADER vertex my_shader PASSTHROUGH
154 SHADER fragment my_fragment GLSL
155 # GLSL Shader
156 END
157 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
158 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
159
160 PIPELINE graphics my_pipeline
161 ATTACH my_shader
162 ATTACH my_fragment
163 BIND BUFFER my_fb AS color LOCATION 0
164 BIND BUFFER my_ds AS depth_stencil
165
166 DEPTH
167 TEST on
168 WRITE foo
169 END
170 END)";
171
172 Parser parser;
173 Result r = parser.Parse(in);
174 ASSERT_FALSE(r.IsSuccess()) << r.Error();
175 EXPECT_EQ("17: invalid value for WRITE: foo", r.Error());
176 }
177
TEST_F(AmberScriptParserTest,DepthClampMissingValue)178 TEST_F(AmberScriptParserTest, DepthClampMissingValue) {
179 std::string in = R"(
180 SHADER vertex my_shader PASSTHROUGH
181 SHADER fragment my_fragment GLSL
182 # GLSL Shader
183 END
184 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
185 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
186
187 PIPELINE graphics my_pipeline
188 ATTACH my_shader
189 ATTACH my_fragment
190 BIND BUFFER my_fb AS color LOCATION 0
191 BIND BUFFER my_ds AS depth_stencil
192
193 DEPTH
194 TEST on
195 CLAMP
196 END
197 END)";
198
199 Parser parser;
200 Result r = parser.Parse(in);
201 ASSERT_FALSE(r.IsSuccess()) << r.Error();
202 EXPECT_EQ("18: invalid value for CLAMP", r.Error());
203 }
204
TEST_F(AmberScriptParserTest,DepthClampInvalidValue)205 TEST_F(AmberScriptParserTest, DepthClampInvalidValue) {
206 std::string in = R"(
207 SHADER vertex my_shader PASSTHROUGH
208 SHADER fragment my_fragment GLSL
209 # GLSL Shader
210 END
211 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
212 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
213
214 PIPELINE graphics my_pipeline
215 ATTACH my_shader
216 ATTACH my_fragment
217 BIND BUFFER my_fb AS color LOCATION 0
218 BIND BUFFER my_ds AS depth_stencil
219
220 DEPTH
221 TEST on
222 CLAMP foo
223 END
224 END)";
225
226 Parser parser;
227 Result r = parser.Parse(in);
228 ASSERT_FALSE(r.IsSuccess()) << r.Error();
229 EXPECT_EQ("17: invalid value for CLAMP: foo", r.Error());
230 }
231
TEST_F(AmberScriptParserTest,DepthCompareMissingValue)232 TEST_F(AmberScriptParserTest, DepthCompareMissingValue) {
233 std::string in = R"(
234 SHADER vertex my_shader PASSTHROUGH
235 SHADER fragment my_fragment GLSL
236 # GLSL Shader
237 END
238 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
239 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
240
241 PIPELINE graphics my_pipeline
242 ATTACH my_shader
243 ATTACH my_fragment
244 BIND BUFFER my_fb AS color LOCATION 0
245 BIND BUFFER my_ds AS depth_stencil
246
247 DEPTH
248 TEST on
249 COMPARE_OP
250 END
251 END)";
252
253 Parser parser;
254 Result r = parser.Parse(in);
255 ASSERT_FALSE(r.IsSuccess()) << r.Error();
256 EXPECT_EQ("18: invalid value for COMPARE_OP", r.Error());
257 }
258
TEST_F(AmberScriptParserTest,DepthCompareInvalidValue)259 TEST_F(AmberScriptParserTest, DepthCompareInvalidValue) {
260 std::string in = R"(
261 SHADER vertex my_shader PASSTHROUGH
262 SHADER fragment my_fragment GLSL
263 # GLSL Shader
264 END
265 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
266 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
267
268 PIPELINE graphics my_pipeline
269 ATTACH my_shader
270 ATTACH my_fragment
271 BIND BUFFER my_fb AS color LOCATION 0
272 BIND BUFFER my_ds AS depth_stencil
273
274 DEPTH
275 TEST on
276 COMPARE_OP foo
277 END
278 END)";
279
280 Parser parser;
281 Result r = parser.Parse(in);
282 ASSERT_FALSE(r.IsSuccess()) << r.Error();
283 EXPECT_EQ("17: invalid value for COMPARE_OP: foo", r.Error());
284 }
285
TEST_F(AmberScriptParserTest,DepthBoundsExpectingMin)286 TEST_F(AmberScriptParserTest, DepthBoundsExpectingMin) {
287 std::string in = R"(
288 SHADER vertex my_shader PASSTHROUGH
289 SHADER fragment my_fragment GLSL
290 # GLSL Shader
291 END
292 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
293 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
294
295 PIPELINE graphics my_pipeline
296 ATTACH my_shader
297 ATTACH my_fragment
298 BIND BUFFER my_fb AS color LOCATION 0
299 BIND BUFFER my_ds AS depth_stencil
300
301 DEPTH
302 TEST on
303 BOUNDS
304 END
305 END)";
306
307 Parser parser;
308 Result r = parser.Parse(in);
309 ASSERT_FALSE(r.IsSuccess()) << r.Error();
310 EXPECT_EQ("18: BOUNDS expecting min", r.Error());
311 }
312
TEST_F(AmberScriptParserTest,DepthBoundsMinInvalidValue)313 TEST_F(AmberScriptParserTest, DepthBoundsMinInvalidValue) {
314 std::string in = R"(
315 SHADER vertex my_shader PASSTHROUGH
316 SHADER fragment my_fragment GLSL
317 # GLSL Shader
318 END
319 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
320 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
321
322 PIPELINE graphics my_pipeline
323 ATTACH my_shader
324 ATTACH my_fragment
325 BIND BUFFER my_fb AS color LOCATION 0
326 BIND BUFFER my_ds AS depth_stencil
327
328 DEPTH
329 TEST on
330 BOUNDS min foo
331 END
332 END)";
333
334 Parser parser;
335 Result r = parser.Parse(in);
336 ASSERT_FALSE(r.IsSuccess()) << r.Error();
337 EXPECT_EQ("17: BOUNDS invalid value for min", r.Error());
338 }
339
TEST_F(AmberScriptParserTest,DepthBoundsExpectingMax)340 TEST_F(AmberScriptParserTest, DepthBoundsExpectingMax) {
341 std::string in = R"(
342 SHADER vertex my_shader PASSTHROUGH
343 SHADER fragment my_fragment GLSL
344 # GLSL Shader
345 END
346 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
347 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
348
349 PIPELINE graphics my_pipeline
350 ATTACH my_shader
351 ATTACH my_fragment
352 BIND BUFFER my_fb AS color LOCATION 0
353 BIND BUFFER my_ds AS depth_stencil
354
355 DEPTH
356 TEST on
357 BOUNDS min 0.0 foo
358 END
359 END)";
360
361 Parser parser;
362 Result r = parser.Parse(in);
363 ASSERT_FALSE(r.IsSuccess()) << r.Error();
364 EXPECT_EQ("17: BOUNDS expecting max", r.Error());
365 }
366
TEST_F(AmberScriptParserTest,DepthBoundsMaxInvalidValue)367 TEST_F(AmberScriptParserTest, DepthBoundsMaxInvalidValue) {
368 std::string in = R"(
369 SHADER vertex my_shader PASSTHROUGH
370 SHADER fragment my_fragment GLSL
371 # GLSL Shader
372 END
373 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
374 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
375
376 PIPELINE graphics my_pipeline
377 ATTACH my_shader
378 ATTACH my_fragment
379 BIND BUFFER my_fb AS color LOCATION 0
380 BIND BUFFER my_ds AS depth_stencil
381
382 DEPTH
383 TEST on
384 BOUNDS min 0.0 max foo
385 END
386 END)";
387
388 Parser parser;
389 Result r = parser.Parse(in);
390 ASSERT_FALSE(r.IsSuccess()) << r.Error();
391 EXPECT_EQ("17: BOUNDS invalid value for max", r.Error());
392 }
393
TEST_F(AmberScriptParserTest,DepthBiasExpectingConstant)394 TEST_F(AmberScriptParserTest, DepthBiasExpectingConstant) {
395 std::string in = R"(
396 SHADER vertex my_shader PASSTHROUGH
397 SHADER fragment my_fragment GLSL
398 # GLSL Shader
399 END
400 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
401 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
402
403 PIPELINE graphics my_pipeline
404 ATTACH my_shader
405 ATTACH my_fragment
406 BIND BUFFER my_fb AS color LOCATION 0
407 BIND BUFFER my_ds AS depth_stencil
408
409 DEPTH
410 TEST on
411 BIAS
412 END
413 END)";
414
415 Parser parser;
416 Result r = parser.Parse(in);
417 ASSERT_FALSE(r.IsSuccess()) << r.Error();
418 EXPECT_EQ("18: BIAS expecting constant", r.Error());
419 }
420
TEST_F(AmberScriptParserTest,DepthBiasConstantInvalidValue)421 TEST_F(AmberScriptParserTest, DepthBiasConstantInvalidValue) {
422 std::string in = R"(
423 SHADER vertex my_shader PASSTHROUGH
424 SHADER fragment my_fragment GLSL
425 # GLSL Shader
426 END
427 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
428 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
429
430 PIPELINE graphics my_pipeline
431 ATTACH my_shader
432 ATTACH my_fragment
433 BIND BUFFER my_fb AS color LOCATION 0
434 BIND BUFFER my_ds AS depth_stencil
435
436 DEPTH
437 TEST on
438 BIAS constant foo
439 END
440 END)";
441
442 Parser parser;
443 Result r = parser.Parse(in);
444 ASSERT_FALSE(r.IsSuccess()) << r.Error();
445 EXPECT_EQ("17: BIAS invalid value for constant", r.Error());
446 }
447
TEST_F(AmberScriptParserTest,DepthBiasExpectingClamp)448 TEST_F(AmberScriptParserTest, DepthBiasExpectingClamp) {
449 std::string in = R"(
450 SHADER vertex my_shader PASSTHROUGH
451 SHADER fragment my_fragment GLSL
452 # GLSL Shader
453 END
454 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
455 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
456
457 PIPELINE graphics my_pipeline
458 ATTACH my_shader
459 ATTACH my_fragment
460 BIND BUFFER my_fb AS color LOCATION 0
461 BIND BUFFER my_ds AS depth_stencil
462
463 DEPTH
464 TEST on
465 BIAS constant 0.0 foo
466 END
467 END)";
468
469 Parser parser;
470 Result r = parser.Parse(in);
471 ASSERT_FALSE(r.IsSuccess()) << r.Error();
472 EXPECT_EQ("17: BIAS expecting clamp", r.Error());
473 }
474
TEST_F(AmberScriptParserTest,DepthBiasClampInvalidValue)475 TEST_F(AmberScriptParserTest, DepthBiasClampInvalidValue) {
476 std::string in = R"(
477 SHADER vertex my_shader PASSTHROUGH
478 SHADER fragment my_fragment GLSL
479 # GLSL Shader
480 END
481 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
482 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
483
484 PIPELINE graphics my_pipeline
485 ATTACH my_shader
486 ATTACH my_fragment
487 BIND BUFFER my_fb AS color LOCATION 0
488 BIND BUFFER my_ds AS depth_stencil
489
490 DEPTH
491 TEST on
492 BIAS constant 0.0 clamp foo
493 END
494 END)";
495
496 Parser parser;
497 Result r = parser.Parse(in);
498 ASSERT_FALSE(r.IsSuccess()) << r.Error();
499 EXPECT_EQ("17: BIAS invalid value for clamp", r.Error());
500 }
501
TEST_F(AmberScriptParserTest,DepthBiasExpectingSlope)502 TEST_F(AmberScriptParserTest, DepthBiasExpectingSlope) {
503 std::string in = R"(
504 SHADER vertex my_shader PASSTHROUGH
505 SHADER fragment my_fragment GLSL
506 # GLSL Shader
507 END
508 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
509 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
510
511 PIPELINE graphics my_pipeline
512 ATTACH my_shader
513 ATTACH my_fragment
514 BIND BUFFER my_fb AS color LOCATION 0
515 BIND BUFFER my_ds AS depth_stencil
516
517 DEPTH
518 TEST on
519 BIAS constant 0.0 clamp 0.0
520 END
521 END)";
522
523 Parser parser;
524 Result r = parser.Parse(in);
525 ASSERT_FALSE(r.IsSuccess()) << r.Error();
526 EXPECT_EQ("18: BIAS expecting slope", r.Error());
527 }
528
TEST_F(AmberScriptParserTest,DepthBiasSlopeInvalidValue)529 TEST_F(AmberScriptParserTest, DepthBiasSlopeInvalidValue) {
530 std::string in = R"(
531 SHADER vertex my_shader PASSTHROUGH
532 SHADER fragment my_fragment GLSL
533 # GLSL Shader
534 END
535 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
536 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
537
538 PIPELINE graphics my_pipeline
539 ATTACH my_shader
540 ATTACH my_fragment
541 BIND BUFFER my_fb AS color LOCATION 0
542 BIND BUFFER my_ds AS depth_stencil
543
544 DEPTH
545 TEST on
546 BIAS constant 0.0 clamp 0.0 slope foo
547 END
548 END)";
549
550 Parser parser;
551 Result r = parser.Parse(in);
552 ASSERT_FALSE(r.IsSuccess()) << r.Error();
553 EXPECT_EQ("17: BIAS invalid value for slope", r.Error());
554 }
555
556 } // namespace amberscript
557 } // namespace amber
558