aboutsummaryrefslogtreecommitdiff
path: root/src/amberscript/parser_pipeline_test.cc
blob: 2b56e2ee5d64b7ad9844a3311745cadf0c1ca80c (plain)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2019 The Amber Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or parseried.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gtest/gtest.h"
#include "src/amberscript/parser.h"

namespace amber {
namespace amberscript {

using AmberScriptParserTest = testing::Test;

TEST_F(AmberScriptParserTest, Pipeline) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_TRUE(r.IsSuccess()) << r.Error();

  auto script = parser.GetScript();
  EXPECT_EQ(2U, script->GetShaders().size());

  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(1U, pipelines.size());

  const auto* pipeline = pipelines[0].get();
  EXPECT_EQ("my_pipeline", pipeline->GetName());
  EXPECT_EQ(PipelineType::kGraphics, pipeline->GetType());

  const auto& shaders = pipeline->GetShaders();
  ASSERT_EQ(2U, shaders.size());

  ASSERT_TRUE(shaders[0].GetShader() != nullptr);
  EXPECT_EQ("my_shader", shaders[0].GetShader()->GetName());
  EXPECT_EQ(kShaderTypeVertex, shaders[0].GetShader()->GetType());
  EXPECT_EQ(static_cast<uint32_t>(0),
            shaders[0].GetShaderOptimizations().size());

  ASSERT_TRUE(shaders[1].GetShader() != nullptr);
  EXPECT_EQ("my_fragment", shaders[1].GetShader()->GetName());
  EXPECT_EQ(kShaderTypeFragment, shaders[1].GetShader()->GetType());
  EXPECT_EQ(static_cast<uint32_t>(0),
            shaders[1].GetShaderOptimizations().size());
}

TEST_F(AmberScriptParserTest, PipelineMissingEnd) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
PIPELINE graphics my_pipeline
  ATTACH my_shader
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("5: PIPELINE missing END command", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineWithExtraParams) {
  std::string in = R"(
PIPELINE graphics my_pipeline INVALID
  ATTACH my_shader
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: extra parameters after PIPELINE command: INVALID", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineInvalidType) {
  std::string in = "PIPELINE my_name\nEND";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("1: unknown pipeline type: my_name", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineMissingName) {
  std::string in = "PIPELINE compute\nEND";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: invalid token when looking for pipeline name", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineWithInvalidTokenType) {
  std::string in = "PIPELINE 123 my_pipeline\nEND";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("1: invalid token when looking for pipeline type", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineWithInvalidTokenName) {
  std::string in = "PIPELINE compute 123\nEND";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("1: invalid token when looking for pipeline name", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineEmpty) {
  std::string in = "PIPELINE compute my_pipeline\nEND";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("compute pipeline requires a compute shader", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineWithUnknownCommand) {
  std::string in = R"(
PIPELINE compute my_pipeline
  SHADER vertex my_shader PASSTHROUGH
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("3: unknown token in pipeline block: SHADER", r.Error());
}

TEST_F(AmberScriptParserTest, DuplicatePipelineName) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# Fragment shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END
PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("14: duplicate pipeline name provided", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineDefaultColorBuffer) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END
PIPELINE graphics my_pipeline2
  ATTACH my_shader
  ATTACH my_fragment
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_TRUE(r.IsSuccess()) << r.Error();

  auto script = parser.GetScript();
  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(2U, pipelines.size());

  ASSERT_EQ(1U, pipelines[0]->GetColorAttachments().size());
  const auto& buf1 = pipelines[0]->GetColorAttachments()[0];
  ASSERT_TRUE(buf1.buffer != nullptr);

  Buffer* buffer1 = buf1.buffer;
  EXPECT_EQ(FormatType::kB8G8R8A8_UNORM, buffer1->GetFormat()->GetFormatType());
  EXPECT_EQ(0u, buf1.location);
  EXPECT_EQ(250u * 250u, buffer1->ElementCount());
  EXPECT_EQ(250u * 250u * 4u, buffer1->ValueCount());
  EXPECT_EQ(250u * 250u * 4u * sizeof(uint8_t), buffer1->GetSizeInBytes());

  ASSERT_EQ(1U, pipelines[1]->GetColorAttachments().size());
  const auto& buf2 = pipelines[1]->GetColorAttachments()[0];
  ASSERT_TRUE(buf2.buffer != nullptr);
  ASSERT_EQ(buffer1, buf2.buffer);
  EXPECT_EQ(0u, buf2.location);
  EXPECT_EQ(FormatType::kB8G8R8A8_UNORM,
            buf2.buffer->GetFormat()->GetFormatType());
  EXPECT_EQ(250u * 250u, buf2.buffer->ElementCount());
  EXPECT_EQ(250u * 250u * 4u, buf2.buffer->ValueCount());
  EXPECT_EQ(250u * 250u * 4u * sizeof(uint8_t), buf2.buffer->GetSizeInBytes());
}

TEST_F(AmberScriptParserTest, PipelineDefaultColorBufferMismatchSize) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END
PIPELINE graphics my_pipeline2
  ATTACH my_shader
  ATTACH my_fragment
  FRAMEBUFFER_SIZE 256 256
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());

  EXPECT_EQ("shared framebuffer must have same size over all PIPELINES",
            r.Error());
}

TEST_F(AmberScriptParserTest, PipelinePolygonMode) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline_default
  ATTACH my_shader
  ATTACH my_fragment
  FRAMEBUFFER_SIZE 256 256
END
PIPELINE graphics my_pipeline_fill
  ATTACH my_shader
  ATTACH my_fragment
  POLYGON_MODE fill
  FRAMEBUFFER_SIZE 256 256
END
PIPELINE graphics my_pipeline_line
  ATTACH my_shader
  ATTACH my_fragment
  POLYGON_MODE line
  FRAMEBUFFER_SIZE 256 256
END
PIPELINE graphics my_pipeline_point
  ATTACH my_shader
  ATTACH my_fragment
  POLYGON_MODE point
  FRAMEBUFFER_SIZE 256 256
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_TRUE(r.IsSuccess());

  auto script = parser.GetScript();
  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(4U, pipelines.size());

  auto mode0 = pipelines[0]->GetPipelineData()->GetPolygonMode();
  ASSERT_EQ(mode0, PolygonMode::kFill);
  auto mode1 = pipelines[1]->GetPipelineData()->GetPolygonMode();
  ASSERT_EQ(mode1, PolygonMode::kFill);
  auto mode2 = pipelines[2]->GetPipelineData()->GetPolygonMode();
  ASSERT_EQ(mode2, PolygonMode::kLine);
  auto mode3 = pipelines[3]->GetPipelineData()->GetPolygonMode();
  ASSERT_EQ(mode3, PolygonMode::kPoint);
}

TEST_F(AmberScriptParserTest, PipelineMissingPolygonMode) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
  POLYGON_MODE
  FRAMEBUFFER_SIZE 256 256
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());

  EXPECT_EQ("11: missing mode in POLYGON_MODE command", r.Error());
}

TEST_F(AmberScriptParserTest, PipelineInvalidPolygonMode) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics my_pipeline
  ATTACH my_shader
  ATTACH my_fragment
  POLYGON_MODE foo
  FRAMEBUFFER_SIZE 256 256
END)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());

  EXPECT_EQ("10: invalid polygon mode: foo", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipeline) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END
SHADER fragment other_fragment GLSL
# GLSL Shader
END
BUFFER buf1 DATA_TYPE int32 SIZE 20 FILL 5
BUFFER buf2 DATA_TYPE int32 SIZE 20 FILL 7

PIPELINE graphics parent_pipeline
  ATTACH my_shader
  ATTACH my_fragment
  BIND BUFFER buf1 AS storage DESCRIPTOR_SET 1 BINDING 3
END

DERIVE_PIPELINE child_pipeline FROM parent_pipeline
  ATTACH other_fragment
  BIND BUFFER buf2 AS storage DESCRIPTOR_SET 1 BINDING 3
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_TRUE(r.IsSuccess()) << r.Error();

  auto script = parser.GetScript();
  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(2U, pipelines.size());

  const auto* pipeline1 = pipelines[0].get();
  auto buffers1 = pipeline1->GetBuffers();
  ASSERT_EQ(1U, buffers1.size());
  EXPECT_EQ("buf1", buffers1[0].buffer->GetName());
  EXPECT_EQ(1u, buffers1[0].descriptor_set);
  EXPECT_EQ(3u, buffers1[0].binding);

  auto shaders1 = pipeline1->GetShaders();
  ASSERT_EQ(2U, shaders1.size());
  EXPECT_EQ("my_shader", shaders1[0].GetShader()->GetName());
  EXPECT_EQ("my_fragment", shaders1[1].GetShader()->GetName());

  const auto* pipeline2 = pipelines[1].get();
  EXPECT_EQ("child_pipeline", pipeline2->GetName());

  auto buffers2 = pipeline2->GetBuffers();
  ASSERT_EQ(1U, buffers2.size());
  EXPECT_EQ("buf2", buffers2[0].buffer->GetName());
  EXPECT_EQ(1u, buffers2[0].descriptor_set);
  EXPECT_EQ(3u, buffers2[0].binding);

  auto shaders2 = pipeline2->GetShaders();
  ASSERT_EQ(2U, shaders2.size());
  EXPECT_EQ("my_shader", shaders2[0].GetShader()->GetName());
  EXPECT_EQ("other_fragment", shaders2[1].GetShader()->GetName());
}

TEST_F(AmberScriptParserTest, DerivePipelineMissingEnd) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics parent_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END

DERIVE_PIPELINE derived_pipeline FROM parent_pipeline
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("13: DERIVE_PIPELINE missing END command", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineMissingPipelineName) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics parent_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END

DERIVE_PIPELINE FROM parent_pipeline
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("12: missing pipeline name for DERIVE_PIPELINE command", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineMissingFrom) {
  std::string in = R"(
DERIVE_PIPELINE derived_pipeline parent_pipeline
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: missing FROM in DERIVE_PIPELINE command", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineMissingParentPipelineName) {
  std::string in = R"(
DERIVE_PIPELINE derived_pipeline FROM
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("3: missing parent pipeline name in DERIVE_PIPELINE command",
            r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineUnknownParentPipeline) {
  std::string in = R"(
DERIVE_PIPELINE derived_pipeline FROM parent_pipeline
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: unknown parent pipeline in DERIVE_PIPELINE command", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineDuplicatePipelineName) {
  std::string in = R"(
SHADER vertex my_shader PASSTHROUGH
SHADER fragment my_fragment GLSL
# GLSL Shader
END

PIPELINE graphics parent_pipeline
  ATTACH my_shader
  ATTACH my_fragment
END

DERIVE_PIPELINE parent_pipeline FROM parent_pipeline
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("12: duplicate pipeline name for DERIVE_PIPELINE command",
            r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineNoParams) {
  std::string in = R"(
DERIVE_PIPELINE
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("3: missing pipeline name for DERIVE_PIPELINE command", r.Error());
}

TEST_F(AmberScriptParserTest, DerivePipelineSpecialized) {
  std::string in = R"(
SHADER compute my_shader GLSL
#shaders
END
PIPELINE compute p1
  ATTACH my_shader SPECIALIZE 3 AS uint32 4
END
DERIVE_PIPELINE p2 FROM p1
END
)";

  Parser parser;
  Result r = parser.Parse(in);
  EXPECT_EQ("", r.Error());
  ASSERT_TRUE(r.IsSuccess());

  auto script = parser.GetScript();
  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(2U, pipelines.size());

  const auto* p1 = pipelines[0].get();
  const auto& s1 = p1->GetShaders();
  ASSERT_EQ(1U, s1.size());

  EXPECT_EQ(1u, s1[0].GetSpecialization().size());
  EXPECT_EQ(4u, s1[0].GetSpecialization().at(3));

  const auto* p2 = pipelines[1].get();
  const auto& s2 = p2->GetShaders();
  ASSERT_EQ(1U, s2.size());

  EXPECT_EQ(1u, s2[0].GetSpecialization().size());
  EXPECT_EQ(4u, s2[0].GetSpecialization().at(3));
}

}  // namespace amberscript
}  // namespace amber