aboutsummaryrefslogtreecommitdiff
path: root/src/vkscript/parser_test.cc
blob: 676e75ece6af68a2238054d6ff358755ce3e4f41 (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
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2018 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 "src/vkscript/parser.h"

#include <vector>

#include "gtest/gtest.h"
#include "src/format.h"

namespace amber {
namespace vkscript {

using VkScriptParserTest = testing::Test;

TEST_F(VkScriptParserTest, RequireBlockNoArgumentFeatures) {
  struct {
    const char* name;
  } features[] = {{"robustBufferAccess"},
                  {"fullDrawIndexUint32"},
                  {"imageCubeArray"},
                  {"independentBlend"},
                  {"geometryShader"},
                  {"tessellationShader"},
                  {"sampleRateShading"},
                  {"dualSrcBlend"},
                  {"logicOp"},
                  {"multiDrawIndirect"},
                  {"drawIndirectFirstInstance"},
                  {"depthClamp"},
                  {"depthBiasClamp"},
                  {"fillModeNonSolid"},
                  {"depthBounds"},
                  {"wideLines"},
                  {"largePoints"},
                  {"alphaToOne"},
                  {"multiViewport"},
                  {"samplerAnisotropy"},
                  {"textureCompressionETC2"},
                  {"textureCompressionASTC_LDR"},
                  {"textureCompressionBC"},
                  {"occlusionQueryPrecise"},
                  {"pipelineStatisticsQuery"},
                  {"vertexPipelineStoresAndAtomics"},
                  {"fragmentStoresAndAtomics"},
                  {"shaderTessellationAndGeometryPointSize"},
                  {"shaderImageGatherExtended"},
                  {"shaderStorageImageExtendedFormats"},
                  {"shaderStorageImageMultisample"},
                  {"shaderStorageImageReadWithoutFormat"},
                  {"shaderStorageImageWriteWithoutFormat"},
                  {"shaderUniformBufferArrayDynamicIndexing"},
                  {"shaderSampledImageArrayDynamicIndexing"},
                  {"shaderStorageBufferArrayDynamicIndexing"},
                  {"shaderStorageImageArrayDynamicIndexing"},
                  {"shaderClipDistance"},
                  {"shaderCullDistance"},
                  {"shaderFloat64"},
                  {"shaderInt64"},
                  {"shaderInt16"},
                  {"shaderResourceResidency"},
                  {"shaderResourceMinLod"},
                  {"sparseBinding"},
                  {"sparseResidencyBuffer"},
                  {"sparseResidencyImage2D"},
                  {"sparseResidencyImage3D"},
                  {"sparseResidency2Samples"},
                  {"sparseResidency4Samples"},
                  {"sparseResidency8Samples"},
                  {"sparseResidency16Samples"},
                  {"sparseResidencyAliased"},
                  {"variableMultisampleRate"},
                  {"inheritedQueries"},
                  {"VariablePointerFeatures.variablePointers"},
                  {"VariablePointerFeatures.variablePointersStorageBuffer"}};

  for (const auto& feature : features) {
    std::string in = std::string("[require]\n") + feature.name + "\n";

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

    auto script = parser.GetScript();
    auto feats = script->GetRequiredFeatures();
    ASSERT_EQ(1U, feats.size());
    EXPECT_EQ(feature.name, feats[0]);
  }
}

TEST_F(VkScriptParserTest, RequireBlockExtensions) {
  std::string block = R"([require]
VK_KHR_storage_buffer_storage_class
VK_KHR_variable_pointers
VK_KHR_get_physical_device_properties2)";

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

  auto script = parser.GetScript();
  auto device_exts = script->GetRequiredDeviceExtensions();
  ASSERT_EQ(2U, device_exts.size());
  EXPECT_EQ("VK_KHR_storage_buffer_storage_class", device_exts[0]);
  EXPECT_EQ("VK_KHR_variable_pointers", device_exts[1]);

  auto inst_exts = script->GetRequiredInstanceExtensions();
  ASSERT_EQ(1U, inst_exts.size());
  EXPECT_EQ("VK_KHR_get_physical_device_properties2", inst_exts[0]);
}

TEST_F(VkScriptParserTest, RequireBlockFramebuffer) {
  std::string block = "[require]\nframebuffer R32G32B32A32_SFLOAT";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(1U, buffers.size());
  EXPECT_EQ(BufferType::kColor, buffers[0]->GetBufferType());
  EXPECT_EQ(FormatType::kR32G32B32A32_SFLOAT,
            buffers[0]->GetFormat()->GetFormatType());
}

TEST_F(VkScriptParserTest, RequireBlockDepthStencil) {
  std::string block = "[require]\ndepthstencil D24_UNORM_S8_UINT";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(2U, buffers.size());
  EXPECT_EQ(BufferType::kDepth, buffers[1]->GetBufferType());
  EXPECT_EQ(FormatType::kD24_UNORM_S8_UINT,
            buffers[1]->GetFormat()->GetFormatType());
}

TEST_F(VkScriptParserTest, RequireFbSize) {
  std::string block = "[require]\nfbsize 300 400";

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

  auto script = parser.GetScript();
  const auto& pipelines = script->GetPipelines();
  ASSERT_EQ(1U, pipelines.size());
  EXPECT_EQ(300, pipelines[0]->GetFramebufferWidth());
  EXPECT_EQ(400, pipelines[0]->GetFramebufferHeight());
}

TEST_F(VkScriptParserTest, RequireFbSizeMissingSize) {
  std::string block = "[require]\nfbsize";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Missing width and height for fbsize command", r.Error());
}

TEST_F(VkScriptParserTest, RequireFbSizeMissingValue) {
  std::string block = "[require]\nfbsize 200";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Missing height for fbsize command", r.Error());
}

TEST_F(VkScriptParserTest, RequireFbSizeExtraParams) {
  std::string block = "[require]\nfbsize 200 300 EXTRA";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Failed to parse requirements block: invalid token: EXTRA",
            r.Error());
}

TEST_F(VkScriptParserTest, RequireFbSizeInvalidFirstParam) {
  std::string block = "[require]\nfbsize INVALID 200";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Invalid width for fbsize command", r.Error());
}

TEST_F(VkScriptParserTest, RequireFbSizeInvalidSecondParam) {
  std::string block = "[require]\nfbsize 200 INVALID";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Invalid height for fbsize command", r.Error());
}

TEST_F(VkScriptParserTest, RequireBlockMultipleLines) {
  std::string block = R"([require]
# Requirements block stuff.
depthstencil D24_UNORM_S8_UINT
sparseResidency4Samples
framebuffer R32G32B32A32_SFLOAT
# More comments
inheritedQueries # line comment
)";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(2U, buffers.size());
  EXPECT_EQ(BufferType::kColor, buffers[0]->GetBufferType());
  EXPECT_EQ(FormatType::kR32G32B32A32_SFLOAT,
            buffers[0]->GetFormat()->GetFormatType());

  EXPECT_EQ(BufferType::kDepth, buffers[1]->GetBufferType());
  EXPECT_EQ(FormatType::kD24_UNORM_S8_UINT,
            buffers[1]->GetFormat()->GetFormatType());

  auto feats = script->GetRequiredFeatures();
  EXPECT_EQ("sparseResidency4Samples", feats[0]);
  EXPECT_EQ("inheritedQueries", feats[1]);
}

TEST_F(VkScriptParserTest, IndicesBlock) {
  std::string block = "[indices]\n1 2 3";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(2U, buffers.size());
  ASSERT_EQ(BufferType::kIndex, buffers[1]->GetBufferType());

  auto buffer_ptr = buffers[1].get();
  auto buffer = buffer_ptr;
  EXPECT_TRUE(buffer->GetFormat()->IsUint32());
  EXPECT_EQ(3U, buffer->ElementCount());
  EXPECT_EQ(3U, buffer->ValueCount());
  EXPECT_EQ(3U * sizeof(uint32_t), buffer->GetSizeInBytes());

  const auto* data = buffer->GetValues<uint32_t>();
  EXPECT_EQ(1, data[0]);
  EXPECT_EQ(2, data[1]);
  EXPECT_EQ(3, data[2]);
}

TEST_F(VkScriptParserTest, IndicesBlockMultipleLines) {
  std::string block = R"([indices]
# comment line
1 2 3   4 5 6
# another comment
7 8 9  10 11 12
)";

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

  auto script = parser.GetScript();
  auto& buffers = script->GetBuffers();
  ASSERT_EQ(2U, buffers.size());
  ASSERT_EQ(buffers[1]->GetBufferType(), BufferType::kIndex);

  const auto* data = buffers[1]->GetValues<uint32_t>();
  std::vector<uint16_t> results = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  ASSERT_EQ(results.size(), buffers[1]->ValueCount());
  for (size_t i = 0; i < results.size(); ++i) {
    EXPECT_EQ(results[i], data[i]);
  }
}

TEST_F(VkScriptParserTest, IndicesBlockBadValue) {
  std::string block = "[indices]\n1 a 3";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("1: Invalid value in indices block: a", r.Error());
}

TEST_F(VkScriptParserTest, IndicesBlockValueTooLarge) {
  std::string block = "[indices]\n100000000000 3";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("1: Value too large in indices block: 100000000000", r.Error());
}

TEST_F(VkScriptParserTest, VertexDataEmpty) {
  std::string block = "[vertex data]\n#comment\n";

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

  auto script = parser.GetScript();
  EXPECT_EQ(1U, script->GetBuffers().size());
}

TEST_F(VkScriptParserTest, VertexDataHeaderFormatString) {
  std::string block = "[vertex data]\n0/R32G32_SFLOAT 1/A8B8G8R8_UNORM_PACK32";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(3U, buffers.size());

  ASSERT_EQ(1U, script->GetPipelines().size());
  const auto* pipeline = script->GetPipelines()[0].get();

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

  ASSERT_EQ(BufferType::kVertex, buffers[1]->GetBufferType());
  EXPECT_EQ(static_cast<uint8_t>(0U), pipeline_buffers[0].location);
  EXPECT_EQ(FormatType::kR32G32_SFLOAT,
            buffers[1]->GetFormat()->GetFormatType());
  EXPECT_EQ(static_cast<uint32_t>(0), buffers[1]->ElementCount());

  ASSERT_EQ(BufferType::kVertex, buffers[2]->GetBufferType());
  EXPECT_EQ(1U, pipeline_buffers[1].location);
  EXPECT_EQ(FormatType::kA8B8G8R8_UNORM_PACK32,
            buffers[2]->GetFormat()->GetFormatType());
  EXPECT_EQ(static_cast<uint32_t>(0), buffers[2]->ElementCount());
}

TEST_F(VkScriptParserTest, VertexDataHeaderGlslString) {
  std::string block = "[vertex data]\n0/float/vec2 1/int/vec3";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(3U, buffers.size());

  ASSERT_EQ(1U, script->GetPipelines().size());
  const auto* pipeline = script->GetPipelines()[0].get();

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

  ASSERT_EQ(BufferType::kVertex, buffers[1]->GetBufferType());
  EXPECT_EQ(static_cast<uint8_t>(0U), pipeline_buffers[0].location);

  EXPECT_EQ(FormatType::kR32G32_SFLOAT,
            buffers[1]->GetFormat()->GetFormatType());

  auto& segs1 = buffers[1]->GetFormat()->GetSegments();
  ASSERT_EQ(2U, segs1.size());
  EXPECT_EQ(FormatMode::kSFloat, segs1[0].GetFormatMode());
  EXPECT_EQ(FormatMode::kSFloat, segs1[1].GetFormatMode());
  EXPECT_EQ(static_cast<uint32_t>(0), buffers[1]->ElementCount());

  ASSERT_EQ(BufferType::kVertex, buffers[2]->GetBufferType());
  EXPECT_EQ(1U, pipeline_buffers[1].location);
  EXPECT_EQ(FormatType::kR32G32B32_SINT,
            buffers[2]->GetFormat()->GetFormatType());

  auto& segs2 = buffers[2]->GetFormat()->GetSegments();
  ASSERT_EQ(4, segs2.size());
  EXPECT_EQ(FormatMode::kSInt, segs2[0].GetFormatMode());
  EXPECT_EQ(FormatMode::kSInt, segs2[1].GetFormatMode());
  EXPECT_EQ(FormatMode::kSInt, segs2[2].GetFormatMode());
  EXPECT_TRUE(segs2[3].IsPadding());
  EXPECT_EQ(static_cast<uint32_t>(0), buffers[2]->ElementCount());
}

TEST_F(VkScriptParserTest, TestBlock) {
  std::string block = R"([test]
clear color 255 255 255 0
clear depth 10
clear stencil 2
clear)";

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

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

  ASSERT_TRUE(cmds[0]->IsClearColor());
  auto* color_cmd = cmds[0]->AsClearColor();
  EXPECT_FLOAT_EQ(255.f, color_cmd->GetR());
  EXPECT_FLOAT_EQ(255.f, color_cmd->GetG());
  EXPECT_FLOAT_EQ(255.f, color_cmd->GetB());
  EXPECT_FLOAT_EQ(0.0f, color_cmd->GetA());

  ASSERT_TRUE(cmds[1]->IsClearDepth());
  EXPECT_EQ(10U, cmds[1]->AsClearDepth()->GetValue());

  ASSERT_TRUE(cmds[2]->IsClearStencil());
  EXPECT_EQ(2U, cmds[2]->AsClearStencil()->GetValue());

  EXPECT_TRUE(cmds[3]->IsClear());
}

TEST_F(VkScriptParserTest, VertexDataRows) {
  std::string block = R"([vertex data]
# Vertex data
0/R32G32B32_SFLOAT  1/R8G8B8_UNORM
-1    -1 0.25       255 128 1  # ending comment
# Another Row
0.25  -1 0.25       255 128 255
)";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(3U, buffers.size());

  ASSERT_EQ(BufferType::kVertex, buffers[1]->GetBufferType());

  std::vector<float> seg_0 = {-1.f, -1.f, 0.25f, 0, 0.25f, -1.f, 0.25f, 0};
  const auto* values_0 = buffers[1]->GetValues<float>();
  for (size_t i = 0; i < seg_0.size(); ++i) {
    EXPECT_FLOAT_EQ(seg_0[i], values_0[i]);
  }

  ASSERT_EQ(BufferType::kVertex, buffers[2]->GetBufferType());

  std::vector<uint8_t> seg_1 = {255, 128, 1, 0, 255, 128, 255, 0};
  const auto* values_1 = buffers[2]->GetValues<uint8_t>();
  for (size_t i = 0; i < seg_1.size(); ++i) {
    EXPECT_EQ(seg_1[i], values_1[i]);
  }
}

TEST_F(VkScriptParserTest, VertexDataShortRow) {
  std::string block = R"([vertex data]
0/R32G32B32_SFLOAT  1/R8G8B8_UNORM
-1    -1 0.25       255 0 0
0.25  -1 0.25       255 0
)";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("3: Too few cells in given vertex data row", r.Error());
}

TEST_F(VkScriptParserTest, VertexDataIncorrectValue) {
  std::string block = R"([vertex data]
0/R32G32B32_SFLOAT  1/R8G8B8_UNORM
-1    -1 0.25       255 StringValue 0
0.25  -1 0.25       255 0 0
)";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Invalid vertex data value: StringValue", r.Error());
}

TEST_F(VkScriptParserTest, VertexDataRowsWithHex) {
  std::string block = R"([vertex data]
0/A8B8G8R8_UNORM_PACK32
0xff0000ff
0xffff0000
)";

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

  auto script = parser.GetScript();
  const auto& buffers = script->GetBuffers();
  ASSERT_EQ(2U, buffers.size());
  ASSERT_EQ(BufferType::kVertex, buffers[1]->GetBufferType());

  std::vector<uint32_t> seg_0 = {0xff0000ff, 0xffff0000};
  const auto* values_0 = buffers[1]->GetValues<uint32_t>();
  ASSERT_EQ(seg_0.size(), buffers[1]->ValueCount());

  for (size_t i = 0; i < seg_0.size(); ++i) {
    EXPECT_EQ(seg_0[i], values_0[i]);
  }
}

TEST_F(VkScriptParserTest, VertexDataRowsWithHexWrongColumn) {
  std::string block = R"([vertex data]
0/R32G32B32_SFLOAT  1/R8G8B8_UNORM
-1    -1 0.25       0xffff0000
0.25  -1 0.25       255 0
)";

  Parser parser;
  Result r = parser.Parse(block);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("2: Invalid vertex data value: 0xffff0000", r.Error());
}

TEST_F(VkScriptParserTest, ErrorLineNumberBug195) {
  std::string input = R"([compute shader]
#version 430

void main() {
}

[test]
# Error must report "9: Unknown command: unknown"
unknown
})";

  Parser parser;
  Result r = parser.Parse(input);
  ASSERT_FALSE(r.IsSuccess());
  EXPECT_EQ("9: Unknown command: unknown", r.Error());
}

}  // namespace vkscript
}  // namespace amber