Blender V5.3
syntax_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7namespace blender::gpu::tests {
8
9using namespace shader;
10using namespace std;
11
12TEST(shader_tool, Array)
13{
14 {
15 string input = R"(
16float a[] = {0, 1};
17float b[2] = {
18 a[0],
19 a(0, 1),
20};
21float d[] = {a[0], a(0, 1)};
22)";
23 string expect =
24 R"(
25float a[2] = ARRAY_T(float) ARRAY_V( 0, 1 );
26float b[2] = ARRAY_T(float) ARRAY_V(
27 a[0],
28 a(0, 1)
29 );
30float d[2] = ARRAY_T(float) ARRAY_V( a[0], a(0, 1) );
31)";
32 auto [output, _, error] = process_test_string(input);
33 EXPECT_EQ(expect, output);
34 EXPECT_EQ(error, "");
35 }
36 {
37 string input = R"(
38float c[] = {};
39)";
40 auto [output, _, error] = process_test_string(input);
41 EXPECT_EQ(error, "Array size must be greater than zero.");
42 }
43 {
44 string input = R"(
45float c[0] = {};
46)";
47 auto [output, _, error] = process_test_string(input);
48 EXPECT_EQ(error, "Array size must be greater than zero.");
49 }
50 {
51 string input = R"(
52float2 c[2] = {{0, 1}, {0, 1}};
53)";
54 auto [output, _, error] = process_test_string(input);
55 EXPECT_EQ(error, "Nested initializer list is not supported.");
56 }
57 {
58 string input = R"(
59float a[] = {0, 1};
60float b[2] = {a[0], a[1],};
61float d[] = {a[0], a[1] + a[1]};
62)";
63 string expect =
64 R"(
65float a[2] = {0, 1};
66float b[2] = {a[0], a[1] };
67float d[2] = {a[0], a[1] + a[1]};
68)";
69 auto [output, _, error] = process_test_string(input, Language::BSL);
70 EXPECT_EQ(expect, output);
71 EXPECT_EQ(error, "");
72 }
73 {
74 string input = R"(float c[];)";
75 auto [output, _, error] = process_test_string(input, Language::BSL);
76 EXPECT_EQ(error,
77 "Definition of variable with array type needs an explicit size or an initializer");
78 }
79 {
80 string input = R"(float c[] = {};)";
81 auto [output, _, error] = process_test_string(input, Language::BSL);
82 EXPECT_EQ(error, "Array size must be greater than zero");
83 }
84 {
85 string input = R"(float c[][] = {};)";
86 auto [output, _, error] = process_test_string(input, Language::BSL);
87 EXPECT_EQ(error, "Multi-dimensional arrays cannot have implicit size");
88 }
89 {
90 string input = R"(float c[0] = {};)";
91 auto [output, _, error] = process_test_string(input, Language::BSL);
92 EXPECT_EQ(error, "Array size must be greater than zero");
93 }
94 {
95 string input = R"(float c[1][0] = {};)";
96 auto [output, _, error] = process_test_string(input, Language::BSL);
97 EXPECT_EQ(error, "Array size must be greater than zero");
98 }
99 {
100 string input = R"(float2 c[2] = {{0, 1}, {0, 1}};)";
101 string expect = R"(float2 c[2] = {float2(0, 1), float2(0, 1)};)";
102 auto [output, _, error] = process_test_string(input, Language::BSL);
103 EXPECT_EQ(expect, output);
104 EXPECT_EQ(error, "");
105 }
106}
107
108TEST(shader_tool, Swizzle)
109{
110 {
111 string input = R"(a.xyzw().aaa().xxx().grba().yzww; aaaa();)";
112 string expect = R"(a.xyzw .aaa .xxx .grba .yzww; aaaa();)";
113 auto [output, _, error] = process_test_local(input);
114 EXPECT_EQ(output, expect);
115 EXPECT_EQ(error, "");
116 }
117}
118
119TEST(shader_tool, BinaryLiterals)
120{
121 {
122 string input = R"(0b1 0b10u 0b10001000100010001000100010001000)";
123 string expect = R"(1 2u 2290649224)";
124 auto [output, _, error] = process_test_local(input);
125 EXPECT_EQ(output, expect);
126 EXPECT_EQ(error, "");
127 }
128}
129
130TEST(shader_tool, MatrixConstructors)
131{
132#ifndef __APPLE__ /* This processing is only done for metal compatibility. */
133 GTEST_SKIP() << "This processing is only done for metal compatibility.";
134 return;
135#endif
136 {
137 string input = R"(mat3(a); mat3 a; my_mat4x4(a); mat2x2(a); mat3x2(a);)";
138 string expect = R"(__mat3x3(a); mat3 a; my_mat4x4(a); __mat2x2(a); mat3x2(a);)";
139 auto [output, _, error] = process_test_string(input, Language::GLSL);
140 EXPECT_EQ(output, expect);
141 EXPECT_EQ(error, "");
142 }
143}
144
145TEST(shader_tool, CommaDeclaration)
146{
147 {
148 string input = R"(
149struct A {
150 int a, b;
151};
152)";
153 string expect =
154 R"(
155struct A {
156 int a;int b;
157};
158#line 2
159 A A_ctor_() {A r;r.a=0;r.b=0;return r;}
160
161
162)";
163 auto [output, _, error] = process_test_string(input);
164 EXPECT_EQ(expect, output);
165 EXPECT_EQ(error, "");
166 }
167}
168
169TEST(shader_tool, Reference)
170{
171 {
172 string input = R"(void func() { auto &a = b; a.a = 0; c = a(a); a_c_a = a; })";
173 string expect = R"(void func() { b.a = 0; c = a(b); a_c_a = b; })";
174 auto [output, _, error] = process_test_string(input);
175 EXPECT_EQ(output, expect);
176 EXPECT_EQ(error, "");
177 }
178 {
179 string input = R"(void func() { const int &a = b; a.a = 0; c = a(a); })";
180 string expect = R"(void func() { b.a = 0; c = a(b); })";
181 auto [output, _, error] = process_test_string(input);
182 EXPECT_EQ(output, expect);
183 EXPECT_EQ(error, "");
184 }
185 {
186 string input = R"(void func() { const int i = 0; auto &a = b[i]; a.a = 0; })";
187 string expect = R"(void func() { const int i = 0; b[i].a = 0; })";
188 auto [output, _, error] = process_test_string(input);
189 EXPECT_EQ(output, expect);
190 EXPECT_EQ(error, "");
191 }
192 {
193 string input = R"(void func() { auto &a = b(0); })";
194 auto [output, _, error] = process_test_string(input);
195 EXPECT_EQ(error, "Reference definitions cannot contain function calls.");
196 }
197 {
198 string input = R"(void func() { int i = 0; auto &a = b[i++]; })";
199 auto [output, _, error] = process_test_string(input);
200 EXPECT_EQ(error, "Reference definitions cannot have side effects.");
201 }
202 {
203 string input = R"(void func() { auto &a = b[0 + 1]; })";
204 auto [output, _, error] = process_test_string(input);
205 EXPECT_EQ(error,
206 "Array subscript inside reference declaration must be a single variable or a "
207 "constant, not an expression.");
208 }
209 {
210 string input = R"(void func() { auto &a = b[c]; })";
211 auto [output, _, error] = process_test_string(input);
212 EXPECT_EQ(error,
213 "Cannot locate array subscript variable declaration. "
214 "If it is a global variable, assign it to a temporary const variable for "
215 "indexing inside the reference.");
216 }
217 {
218 string input = R"(void func() { int c = 0; auto &a = b[c]; })";
219 auto [output, _, error] = process_test_string(input);
220 EXPECT_EQ(error, "Array subscript variable must be declared as const qualified.");
221 }
222 {
223 string input = R"(auto &a = b;)";
224 auto [output, _, error] = process_test_string(input);
225 EXPECT_EQ(error, "Unexpected token \"&\": Expecting declaration");
226 }
227
228 {
229 string input = R"(
230int b[1]; const int i = 0; auto &a = b[i]; a = 0;)";
231 string expect = R"(
232int b[1]; const int i = 0; b[i]= 0;)";
233 auto [output, _, error] = process_test_local(input, Language::BSL);
234 EXPECT_EQ(output, expect);
235 EXPECT_EQ(error, "");
236 }
237 {
238 string input = R"(
239int b(int){ return 0; }
240void f(){ auto &a = b(0); }
241)";
242 auto [output, _, error] = process_test_string(input, Language::BSL);
243 EXPECT_EQ(error, "Function calls are not allowed in reference definition");
244 }
245 {
246 string input = R"(
247int b(int){ return 0; }
248void f(){ int c[1]; auto &a = c[b(0)]; }
249)";
250 auto [output, _, error] = process_test_string(input, Language::BSL);
251 EXPECT_EQ(error, "Function calls are not allowed in reference definition");
252 }
253 {
254 string input = R"(
255int b[1]; int i = 0; auto &a = b[i + 1];)";
256 auto [output, _, error] = process_test_local(input, Language::BSL);
257 EXPECT_EQ(error, "Reference cannot bind to expression of non-const subscript index");
258 }
259 {
260 string input = R"(
261int b[1]; const int i = 0; auto &a = b[i + 1];)";
262 auto [output, _, error] = process_test_local(input, Language::BSL);
263 EXPECT_EQ(error, "");
264 }
265 {
266 string input = R"(
267int b; float &a = b;)";
268 auto [output, _, error] = process_test_local(input, Language::BSL);
269 EXPECT_EQ(error, "Reference to type 'float' cannot bind to a value of unrelated type 'int'");
270 }
271 {
272 string input = R"(
273int b; int &a = b + 1;)";
274 auto [output, _, error] = process_test_local(input, Language::BSL);
275 EXPECT_EQ(error, "Reference cannot bind to a temporary");
276 }
277 {
278 /* Only allowed in C++ if ref is const. */
279 string input = R"(
280const int &a = 1;)";
281 auto [output, _, error] = process_test_local(input, Language::BSL);
282 EXPECT_EQ(error, "");
283 }
284 {
285 string input = R"(
286int b; int &a = b++;)";
287 auto [output, _, error] = process_test_local(input, Language::BSL);
288 EXPECT_EQ(error, "Reference cannot bind to an expression with side effects");
289 }
290 {
291 string input = R"(
292int b; int &a = ++b;)";
293 auto [output, _, error] = process_test_local(input, Language::BSL);
294 EXPECT_EQ(error, "Reference cannot bind to an expression with side effects");
295 }
296}
297
298TEST(shader_tool, Constexpr)
299{
300 {
301 string input = R"(
302static constexpr int a{1};
303static constexpr int b = {3};
304static constexpr int c = int(3);
305static constexpr int d = 4;
306static constexpr int e = {{4}};
307static constexpr int f = (a + b / 2) * d - e;
308static constexpr int g = floatBitsToInt(24);
309static constexpr float h = intBitsToFloat(floatBitsToInt(24));
310int func() { return f; })";
311 string expect = R"(
312static constexpr int a= 1;
313static constexpr int b = 3;
314static constexpr int c = 3;
315static constexpr int d = 4;
316static constexpr int e = 4;
317static constexpr int f = 4;
318static constexpr int g = 1103101952;
319static constexpr float h = 24.0f;
320int func() { return 4; })";
321 auto [output, _, error] = process_test_string(input, Language::BSL);
322 EXPECT_EQ(output, expect);
323 EXPECT_EQ(error, "");
324 }
325}
326
327TEST(shader_tool, ConstexprVector)
328{
329 {
330 string input = R"(
331static constexpr float3 i = float3(0, 1.5f, 2u);
332static constexpr int3 j = int3(i + int3(0, 1, 2)) + 1;
333static constexpr float3 k = i + int3(0, 1, 2);
334)";
335 string expect = R"(
336static constexpr float3 i = float3(0.0f, 1.5f, 2.0f);
337static constexpr int3 j = int3(1, 3, 5);
338static constexpr float3 k = float3(0.0f, 2.5f, 4.0f);
339)";
340 auto [output, _, error] = process_test_string(input, Language::BSL);
341 EXPECT_EQ(output, expect);
342 EXPECT_EQ(error, "");
343 }
344 {
345 string input = R"(
346static constexpr int3 j = int3(0, 1, 2) + int2(1);
347)";
348 auto [output, _, error] = process_test_string(input, Language::BSL);
349 EXPECT_EQ(error, "Invalid operands to binary expression ('int3' + 'int2')");
350 }
351 {
352 string input = R"(
353static constexpr int3 j = int3(int2(0), 2);
354)";
355 auto [output, _, error] = process_test_string(input, Language::BSL);
356 EXPECT_EQ(error, "Constexpr variable 'j' must be initialized by a constant expression");
357 }
358 {
359 string input = R"(
360static constexpr int3 j = int3(0, 1, 2) + false;
361)";
362 auto [output, _, error] = process_test_string(input, Language::BSL);
363 EXPECT_EQ(error, "Invalid operands to binary expression ('int3' + 'bool')");
364 }
365 {
366 string input = R"(
367static constexpr int3 j = int2(0);
368)";
369 auto [output, _, error] = process_test_string(input, Language::BSL);
370 EXPECT_EQ(error,
371 "Constexpr variable 'j' must be initialized by a constant expression of type 'int3' "
372 "but got 'int2'");
373 }
374
375 {
376 string input = R"(
377static constexpr float3 a = float3(2.5f);
378static constexpr int4 b = int4(-1);
379)";
380 string expect = R"(
381static constexpr float3 a = float3(2.5f, 2.5f, 2.5f);
382static constexpr int4 b = int4(-1, -1, -1, -1);
383)";
384 auto [output, _, error] = process_test_string(input, Language::BSL);
385 EXPECT_EQ(output, expect);
386 EXPECT_EQ(error, "");
387 }
388
389 {
390 string input = R"(
391static constexpr float3 v = float3(1.0f, 2.0f, 3.0f);
392static constexpr float3 mul_right = v * 2.0f;
393static constexpr float3 mul_left = 2.0f * v;
394static constexpr float3 sub_scalar = 10.0f - v;
395static constexpr float3 div_scalar = v / 2.0f;
396)";
397 string expect = R"(
398static constexpr float3 v = float3(1.0f, 2.0f, 3.0f);
399static constexpr float3 mul_right = float3(2.0f, 4.0f, 6.0f);
400static constexpr float3 mul_left = float3(2.0f, 4.0f, 6.0f);
401static constexpr float3 sub_scalar = float3(9.0f, 8.0f, 7.0f);
402static constexpr float3 div_scalar = float3(0.5f, 1.0f, 1.5f);
403)";
404 auto [output, _, error] = process_test_string(input, Language::BSL);
405 EXPECT_EQ(output, expect);
406 EXPECT_EQ(error, "");
407 }
408
409 {
410 string input = R"(
411static constexpr int3 a = int3(1, -2, 3);
412static constexpr int3 neg = -a;
413static constexpr int3 bit_and = int3(7, 3, 1) & int3(3, 2, 1);
414static constexpr int3 bit_or = int3(4, 2, 0) | int3(1, 1, 0);
415static constexpr int3 shift = int3(1, 2, 4) << int3(1, 2, 0);
416)";
417 string expect = R"(
418static constexpr int3 a = int3(1, -2, 3);
419static constexpr int3 neg = int3(-1, 2, -3);
420static constexpr int3 bit_and = int3(3, 2, 1);
421static constexpr int3 bit_or = int3(5, 3, 0);
422static constexpr int3 shift = int3(2, 8, 4);
423)";
424 auto [output, _, error] = process_test_string(input, Language::BSL);
425 EXPECT_EQ(output, expect);
426 EXPECT_EQ(error, "");
427 }
428
429 {
430 string input = R"(
431static constexpr float3 v1 = float3(1.0f, 2.0f, 3.0f);
432static constexpr float3 v2 = float3(2.0f, 2.0f, 1.0f);
433static constexpr bool3 eq = equal(v1, v2);
434static constexpr bool3 lt = lessThan(v1, v2);
435static constexpr bool3 gte = greaterThanEqual(v1, v2);
436)";
437 string expect = R"(
438static constexpr float3 v1 = float3(1.0f, 2.0f, 3.0f);
439static constexpr float3 v2 = float3(2.0f, 2.0f, 1.0f);
440static constexpr bool3 eq = bool3(false, true, false);
441static constexpr bool3 lt = bool3(true, false, false);
442static constexpr bool3 gte = bool3(false, true, true);
443)";
444 auto [output, _, error] = process_test_string(input, Language::BSL);
445 EXPECT_EQ(output, expect);
446 EXPECT_EQ(error, "");
447 }
448
449#if 0 /* Would be nice to support at some points. */
450 {
451 string input = R"(
452static constexpr float3 a = float3(-1.0f, 2.0f, -5.0f);
453static constexpr float3 b_abs = abs(a);
454static constexpr float3 b_min = min(a, float3(0.0f));
455static constexpr float3 b_clamp = clamp(a, float3(-2.0f), float3(1.0f));
456static constexpr float d = dot(float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f));
457static constexpr float3 c = cross(float3(1, 0, 0), float3(0, 1, 0));
458static constexpr bool is_all = all(bool3(true, true, true));
459static constexpr bool is_any = any(bool3(false, true, false));
460)";
461 string expect = R"(
462static constexpr float3 a = float3(-1.0f, 2.0f, -5.0f);
463static constexpr float3 b_abs = float3(1.0f, 2.0f, 5.0f);
464static constexpr float3 b_min = float3(-1.0f, 0.0f, -5.0f);
465static constexpr float3 b_clamp = float3(-1.0f, 1.0f, -2.0f);
466static constexpr float d = 32.0f;
467static constexpr float3 c = float3(0.0f, 0.0f, 1.0f);
468static constexpr bool is_all = true;
469static constexpr bool is_any = true;
470)";
471 auto [output, _, error] = process_test_string(input, Language::BSL);
472 EXPECT_EQ(output, expect);
473 EXPECT_EQ(error, "");
474 }
475 {
476 string input = R"(
477static constexpr float4 v = float4(1.0f, 2.0f, 3.0f, 4.0f);
478static constexpr float2 s1 = v.xz;
479static constexpr float3 s2 = v.wzy + 1.0f;
480static constexpr float s3 = v.x + v.w;
481)";
482 string expect = R"(
483static constexpr float4 v = float4(1.0f, 2.0f, 3.0f, 4.0f);
484static constexpr float2 s1 = float2(1.0f, 3.0f);
485static constexpr float3 s2 = float3(5.0f, 4.0f, 3.0f);
486static constexpr float s3 = 5.0f;
487)";
488 auto [output, _, error] = process_test_string(input, Language::BSL);
489 EXPECT_EQ(output, expect);
490 EXPECT_EQ(error, "");
491 }
492#endif
493
494 {
495 string input = R"(
496static constexpr int3 v = int3(1, 2, 3) / int3(1, 0, 1);
497)";
498 auto [output, _, error] = process_test_string(input, Language::BSL);
499 EXPECT_EQ(error, "Division by zero during constexpr evaluation");
500 }
501
502 {
503 string input = R"(
504static constexpr int3 j = float3(1.0f, 2.0f, 3.0f);
505)";
506 auto [output, _, error] = process_test_string(input, Language::BSL);
507 EXPECT_EQ(error,
508 "Constexpr variable 'j' must be initialized by a constant expression of type 'int3' "
509 "but got 'float3'");
510 }
511}
512
513TEST(shader_tool, Auto)
514{
515 {
516 string input =
517 "auto a{0};\n"
518 "auto b{0.0f};\n"
519 "auto c{0.0f + 0};\n"
520 "auto d = 0;\n"
521 "auto e = 0.0f;\n"
522 "auto f = 0.0f + 0;\n"
523 "auto g = a * b;\n"
524 "auto h = a * a;\n"
525 "float4x4 i[4];\n"
526 "auto j = i[0];\n"
527 "auto k = i[0][0];\n"
528 "auto l = i[0][0][0];\n";
529 string expect =
530 "int a=int(0);\n"
531 "float b=float(0.0f);\n"
532 "float c=float(0.0f + 0);\n"
533 "int d = 0;\n"
534 "float e = 0.0f;\n"
535 "float f = 0.0f + 0;\n"
536 "float g = a * b;\n"
537 "int h = a * a;\n"
538 "float4x4 i[4];\n"
539 "float4x4 j = i[0];\n"
540 "float4 k = i[0][0];\n"
541 "float l = i[0][0][0];\n";
542 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
543 EXPECT_EQ(output, expect);
544 EXPECT_EQ(error, "");
545 }
546}
547
548TEST(shader_tool, ArrayAggregate)
549{
550 {
551 string input = "int a[4] = {0, 1, 2, 3};\n";
552 string expect = "int a[4] = {0, 1, 2, 3};\n";
553 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
554 EXPECT_EQ(output, expect);
555 EXPECT_EQ(error, "");
556 }
557 {
558 string input = "int a[4][2] = {{0,0}, {1,1}, {int{2},{2}}, {3,3}};\n";
559 string expect = "int a[4][2] = {{0,0}, {1,1}, {int(2),int(2)}, {3,3}};\n";
560 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
561 EXPECT_EQ(output, expect);
562 EXPECT_EQ(error, "");
563 }
564}
565
566TEST(shader_tool, AttributeCapacity)
567{
568 {
569 string input = "[[capacity(my_const)]] int a[4];\n";
570 string expect = " int a[SRT_CONSTANT_my_const];\n";
571 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
572 EXPECT_EQ(output, expect);
573 EXPECT_EQ(error, "");
574 }
575 {
576 string input = "struct A { [[capacity(my_const)]] int a[4]; };\n";
577 string expect =
578 "struct A { int a[SRT_CONSTANT_my_const];\n"
579 "#line 1\n"
580 " };\n"
581 "#line 1\n"
582 "A A_ctor_() {A r;for(int a =0;a < SRT_CONSTANT_my_const;++a) {r.a[a]=0;}return r;}\n";
583 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
584 EXPECT_EQ(output, expect);
585 EXPECT_EQ(error, "");
586 }
587}
588
589} // namespace blender::gpu::tests
#define output
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
TEST(array_store, Nop)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
@ Constexpr
Definition types.hh:86