1 // Copyright 2021 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,NoViewport)23 TEST_F(AmberScriptParserTest, NoViewport) {
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 END)";
38
39 Parser parser;
40 Result r = parser.Parse(in);
41 ASSERT_TRUE(r.IsSuccess()) << r.Error();
42
43 auto script = parser.GetScript();
44 const auto& pipelines = script->GetPipelines();
45 ASSERT_EQ(1U, pipelines.size());
46
47 auto* pipeline = pipelines[0].get();
48 ASSERT_FALSE(pipeline->GetPipelineData()->HasViewportData());
49 }
50
TEST_F(AmberScriptParserTest,ViewportNoDepth)51 TEST_F(AmberScriptParserTest, ViewportNoDepth) {
52 std::string in = R"(
53 SHADER vertex my_shader PASSTHROUGH
54 SHADER fragment my_fragment GLSL
55 # GLSL Shader
56 END
57 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
58 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
59
60 PIPELINE graphics my_pipeline
61 ATTACH my_shader
62 ATTACH my_fragment
63 BIND BUFFER my_fb AS color LOCATION 0
64 BIND BUFFER my_ds AS depth_stencil
65
66 VIEWPORT 5.0 7.0 SIZE 10.0 12.0
67 END)";
68
69 Parser parser;
70 Result r = parser.Parse(in);
71 ASSERT_TRUE(r.IsSuccess()) << r.Error();
72
73 auto script = parser.GetScript();
74 const auto& pipelines = script->GetPipelines();
75 ASSERT_EQ(1U, pipelines.size());
76
77 auto* pipeline = pipelines[0].get();
78 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
79 ASSERT_FLOAT_EQ(5.0f, pipeline->GetPipelineData()->GetViewport().x);
80 ASSERT_FLOAT_EQ(7.0f, pipeline->GetPipelineData()->GetViewport().y);
81 ASSERT_FLOAT_EQ(10.0f, pipeline->GetPipelineData()->GetViewport().w);
82 ASSERT_FLOAT_EQ(12.0f, pipeline->GetPipelineData()->GetViewport().h);
83 ASSERT_FLOAT_EQ(0.0f, pipeline->GetPipelineData()->GetViewport().mind);
84 ASSERT_FLOAT_EQ(1.0f, pipeline->GetPipelineData()->GetViewport().maxd);
85 }
86
TEST_F(AmberScriptParserTest,ViewportMinDepth)87 TEST_F(AmberScriptParserTest, ViewportMinDepth) {
88 std::string in = R"(
89 SHADER vertex my_shader PASSTHROUGH
90 SHADER fragment my_fragment GLSL
91 # GLSL Shader
92 END
93 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
94 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
95
96 PIPELINE graphics my_pipeline
97 ATTACH my_shader
98 ATTACH my_fragment
99 BIND BUFFER my_fb AS color LOCATION 0
100 BIND BUFFER my_ds AS depth_stencil
101
102 VIEWPORT 12.2 9.7 SIZE 0.5 106.1 MIN_DEPTH 0.3
103 END)";
104
105 Parser parser;
106 Result r = parser.Parse(in);
107 ASSERT_TRUE(r.IsSuccess()) << r.Error();
108
109 auto script = parser.GetScript();
110 const auto& pipelines = script->GetPipelines();
111 ASSERT_EQ(1U, pipelines.size());
112
113 auto* pipeline = pipelines[0].get();
114 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
115 ASSERT_FLOAT_EQ(12.2f, pipeline->GetPipelineData()->GetViewport().x);
116 ASSERT_FLOAT_EQ(9.7f, pipeline->GetPipelineData()->GetViewport().y);
117 ASSERT_FLOAT_EQ(0.5f, pipeline->GetPipelineData()->GetViewport().w);
118 ASSERT_FLOAT_EQ(106.1f, pipeline->GetPipelineData()->GetViewport().h);
119 ASSERT_FLOAT_EQ(0.3f, pipeline->GetPipelineData()->GetViewport().mind);
120 ASSERT_FLOAT_EQ(1.0f, pipeline->GetPipelineData()->GetViewport().maxd);
121 }
122
TEST_F(AmberScriptParserTest,ViewportMaxDepth)123 TEST_F(AmberScriptParserTest, ViewportMaxDepth) {
124 std::string in = R"(
125 SHADER vertex my_shader PASSTHROUGH
126 SHADER fragment my_fragment GLSL
127 # GLSL Shader
128 END
129 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
130 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
131
132 PIPELINE graphics my_pipeline
133 ATTACH my_shader
134 ATTACH my_fragment
135 BIND BUFFER my_fb AS color LOCATION 0
136 BIND BUFFER my_ds AS depth_stencil
137
138 VIEWPORT 12.2 9.7 SIZE 0.5 106.1 MAX_DEPTH 0.456
139 END)";
140
141 Parser parser;
142 Result r = parser.Parse(in);
143 ASSERT_TRUE(r.IsSuccess()) << r.Error();
144
145 auto script = parser.GetScript();
146 const auto& pipelines = script->GetPipelines();
147 ASSERT_EQ(1U, pipelines.size());
148
149 auto* pipeline = pipelines[0].get();
150 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
151 ASSERT_FLOAT_EQ(12.2f, pipeline->GetPipelineData()->GetViewport().x);
152 ASSERT_FLOAT_EQ(9.7f, pipeline->GetPipelineData()->GetViewport().y);
153 ASSERT_FLOAT_EQ(0.5f, pipeline->GetPipelineData()->GetViewport().w);
154 ASSERT_FLOAT_EQ(106.1f, pipeline->GetPipelineData()->GetViewport().h);
155 ASSERT_FLOAT_EQ(0.0f, pipeline->GetPipelineData()->GetViewport().mind);
156 ASSERT_FLOAT_EQ(0.456f, pipeline->GetPipelineData()->GetViewport().maxd);
157 }
158
TEST_F(AmberScriptParserTest,ViewportAllValues)159 TEST_F(AmberScriptParserTest, ViewportAllValues) {
160 std::string in = R"(
161 SHADER vertex my_shader PASSTHROUGH
162 SHADER fragment my_fragment GLSL
163 # GLSL Shader
164 END
165 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
166 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
167
168 PIPELINE graphics my_pipeline
169 ATTACH my_shader
170 ATTACH my_fragment
171 BIND BUFFER my_fb AS color LOCATION 0
172 BIND BUFFER my_ds AS depth_stencil
173
174 VIEWPORT -0.6 5.2 SIZE 13.8 9.4 MIN_DEPTH 0.5 MAX_DEPTH 0.6
175 END)";
176
177 Parser parser;
178 Result r = parser.Parse(in);
179 ASSERT_TRUE(r.IsSuccess()) << r.Error();
180
181 auto script = parser.GetScript();
182 const auto& pipelines = script->GetPipelines();
183 ASSERT_EQ(1U, pipelines.size());
184
185 auto* pipeline = pipelines[0].get();
186 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
187 ASSERT_FLOAT_EQ(-0.6f, pipeline->GetPipelineData()->GetViewport().x);
188 ASSERT_FLOAT_EQ(5.2f, pipeline->GetPipelineData()->GetViewport().y);
189 ASSERT_FLOAT_EQ(13.8f, pipeline->GetPipelineData()->GetViewport().w);
190 ASSERT_FLOAT_EQ(9.4f, pipeline->GetPipelineData()->GetViewport().h);
191 ASSERT_FLOAT_EQ(0.5f, pipeline->GetPipelineData()->GetViewport().mind);
192 ASSERT_FLOAT_EQ(0.6f, pipeline->GetPipelineData()->GetViewport().maxd);
193 }
194
TEST_F(AmberScriptParserTest,ViewportIntegers)195 TEST_F(AmberScriptParserTest, ViewportIntegers) {
196 std::string in = R"(
197 SHADER vertex my_shader PASSTHROUGH
198 SHADER fragment my_fragment GLSL
199 # GLSL Shader
200 END
201 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
202 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
203
204 PIPELINE graphics my_pipeline
205 ATTACH my_shader
206 ATTACH my_fragment
207 BIND BUFFER my_fb AS color LOCATION 0
208 BIND BUFFER my_ds AS depth_stencil
209
210 VIEWPORT -2 7 SIZE 15 20 MIN_DEPTH 1 MAX_DEPTH 2
211 END)";
212
213 Parser parser;
214 Result r = parser.Parse(in);
215 ASSERT_TRUE(r.IsSuccess()) << r.Error();
216
217 auto script = parser.GetScript();
218 const auto& pipelines = script->GetPipelines();
219 ASSERT_EQ(1U, pipelines.size());
220
221 auto* pipeline = pipelines[0].get();
222 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
223 ASSERT_FLOAT_EQ(-2.0f, pipeline->GetPipelineData()->GetViewport().x);
224 ASSERT_FLOAT_EQ(7.0f, pipeline->GetPipelineData()->GetViewport().y);
225 ASSERT_FLOAT_EQ(15.0f, pipeline->GetPipelineData()->GetViewport().w);
226 ASSERT_FLOAT_EQ(20.0f, pipeline->GetPipelineData()->GetViewport().h);
227 ASSERT_FLOAT_EQ(1.0f, pipeline->GetPipelineData()->GetViewport().mind);
228 ASSERT_FLOAT_EQ(2.0f, pipeline->GetPipelineData()->GetViewport().maxd);
229 }
230
TEST_F(AmberScriptParserTest,ViewportMixedIntegers)231 TEST_F(AmberScriptParserTest, ViewportMixedIntegers) {
232 std::string in = R"(
233 SHADER vertex my_shader PASSTHROUGH
234 SHADER fragment my_fragment GLSL
235 # GLSL Shader
236 END
237 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
238 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
239
240 PIPELINE graphics my_pipeline
241 ATTACH my_shader
242 ATTACH my_fragment
243 BIND BUFFER my_fb AS color LOCATION 0
244 BIND BUFFER my_ds AS depth_stencil
245
246 VIEWPORT -2 13.1 SIZE 15.9 20
247 END)";
248
249 Parser parser;
250 Result r = parser.Parse(in);
251 ASSERT_TRUE(r.IsSuccess()) << r.Error();
252
253 auto script = parser.GetScript();
254 const auto& pipelines = script->GetPipelines();
255 ASSERT_EQ(1U, pipelines.size());
256
257 auto* pipeline = pipelines[0].get();
258 ASSERT_TRUE(pipeline->GetPipelineData()->HasViewportData());
259 ASSERT_FLOAT_EQ(-2.0f, pipeline->GetPipelineData()->GetViewport().x);
260 ASSERT_FLOAT_EQ(13.1f, pipeline->GetPipelineData()->GetViewport().y);
261 ASSERT_FLOAT_EQ(15.9f, pipeline->GetPipelineData()->GetViewport().w);
262 ASSERT_FLOAT_EQ(20.0f, pipeline->GetPipelineData()->GetViewport().h);
263 ASSERT_FLOAT_EQ(0.0f, pipeline->GetPipelineData()->GetViewport().mind);
264 ASSERT_FLOAT_EQ(1.0f, pipeline->GetPipelineData()->GetViewport().maxd);
265 }
266
TEST_F(AmberScriptParserTest,ViewportInvalidMissingSize)267 TEST_F(AmberScriptParserTest, ViewportInvalidMissingSize) {
268 std::string in = R"(
269 SHADER vertex my_shader PASSTHROUGH
270 SHADER fragment my_fragment GLSL
271 # GLSL Shader
272 END
273 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
274 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
275
276 PIPELINE graphics my_pipeline
277 ATTACH my_shader
278 ATTACH my_fragment
279 BIND BUFFER my_fb AS color LOCATION 0
280 BIND BUFFER my_ds AS depth_stencil
281
282 VIEWPORT 0.0 2.0 12.0 24.0
283 END)";
284
285 Parser parser;
286 Result r = parser.Parse(in);
287 ASSERT_FALSE(r.IsSuccess()) << r.Error();
288 EXPECT_EQ("15: missing SIZE for VIEWPORT command", r.Error());
289 }
290
TEST_F(AmberScriptParserTest,ViewportInvalidSizeNotOptional)291 TEST_F(AmberScriptParserTest, ViewportInvalidSizeNotOptional) {
292 std::string in = R"(
293 SHADER vertex my_shader PASSTHROUGH
294 SHADER fragment my_fragment GLSL
295 # GLSL Shader
296 END
297 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
298 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
299
300 PIPELINE graphics my_pipeline
301 ATTACH my_shader
302 ATTACH my_fragment
303 BIND BUFFER my_fb AS color LOCATION 0
304 BIND BUFFER my_ds AS depth_stencil
305
306 VIEWPORT 0.0 2.0
307 END)";
308
309 Parser parser;
310 Result r = parser.Parse(in);
311 ASSERT_FALSE(r.IsSuccess()) << r.Error();
312 EXPECT_EQ("16: missing SIZE for VIEWPORT command", r.Error());
313 }
314
TEST_F(AmberScriptParserTest,ViewportInvalidMissingOffset)315 TEST_F(AmberScriptParserTest, ViewportInvalidMissingOffset) {
316 std::string in = R"(
317 SHADER vertex my_shader PASSTHROUGH
318 SHADER fragment my_fragment GLSL
319 # GLSL Shader
320 END
321 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
322 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
323
324 PIPELINE graphics my_pipeline
325 ATTACH my_shader
326 ATTACH my_fragment
327 BIND BUFFER my_fb AS color LOCATION 0
328 BIND BUFFER my_ds AS depth_stencil
329
330 VIEWPORT 0.0 SIZE 12.0 24.0
331 END)";
332
333 Parser parser;
334 Result r = parser.Parse(in);
335 ASSERT_FALSE(r.IsSuccess()) << r.Error();
336 EXPECT_EQ("15: invalid offset for VIEWPORT command", r.Error());
337 }
338
TEST_F(AmberScriptParserTest,ViewportInvalidMissingSizeValue)339 TEST_F(AmberScriptParserTest, ViewportInvalidMissingSizeValue) {
340 std::string in = R"(
341 SHADER vertex my_shader PASSTHROUGH
342 SHADER fragment my_fragment GLSL
343 # GLSL Shader
344 END
345 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
346 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
347
348 PIPELINE graphics my_pipeline
349 ATTACH my_shader
350 ATTACH my_fragment
351 BIND BUFFER my_fb AS color LOCATION 0
352 BIND BUFFER my_ds AS depth_stencil
353
354 VIEWPORT 0.0 2.0 SIZE 12.0
355 END)";
356
357 Parser parser;
358 Result r = parser.Parse(in);
359 ASSERT_FALSE(r.IsSuccess()) << r.Error();
360 EXPECT_EQ("16: missing size for VIEWPORT command", r.Error());
361 }
362
TEST_F(AmberScriptParserTest,ViewportInvalidMissingDepthValue)363 TEST_F(AmberScriptParserTest, ViewportInvalidMissingDepthValue) {
364 std::string in = R"(
365 SHADER vertex my_shader PASSTHROUGH
366 SHADER fragment my_fragment GLSL
367 # GLSL Shader
368 END
369 BUFFER my_fb FORMAT R32G32B32A32_SFLOAT
370 BUFFER my_ds FORMAT D32_SFLOAT_S8_UINT
371
372 PIPELINE graphics my_pipeline
373 ATTACH my_shader
374 ATTACH my_fragment
375 BIND BUFFER my_fb AS color LOCATION 0
376 BIND BUFFER my_ds AS depth_stencil
377
378 VIEWPORT 0.0 2.0 SIZE 12.0 24.0 MIN_DEPTH MAX_DEPTH 1.0
379 END)";
380
381 Parser parser;
382 Result r = parser.Parse(in);
383 ASSERT_FALSE(r.IsSuccess()) << r.Error();
384 EXPECT_EQ("15: invalid min_depth for VIEWPORT command", r.Error());
385 }
386
387 } // namespace amberscript
388 } // namespace amber
389