12TEST(shader_tool, Array)
21float d[] = {a[0], a(0, 1)};
25float a[2] = ARRAY_T(float) ARRAY_V( 0, 1 );
26float b[2] = ARRAY_T(float) ARRAY_V(
30float d[2] = ARRAY_T(float) ARRAY_V( a[0], a(0, 1) );
32 auto [
output, _, error] = process_test_string(input);
40 auto [
output, _, error] = process_test_string(input);
41 EXPECT_EQ(error,
"Array size must be greater than zero.");
47 auto [
output, _, error] = process_test_string(input);
48 EXPECT_EQ(error,
"Array size must be greater than zero.");
52float2 c[2] = {{0, 1}, {0, 1}};
54 auto [
output, _, error] = process_test_string(input);
55 EXPECT_EQ(error,
"Nested initializer list is not supported.");
60float b[2] = {a[0], a[1],};
61float d[] = {a[0], a[1] + a[1]};
66float b[2] = {a[0], a[1] };
67float d[2] = {a[0], a[1] + a[1]};
69 auto [
output, _, error] = process_test_string(input, Language::BSL);
74 string input = R
"(float c[];)";
75 auto [
output, _, error] = process_test_string(input, Language::BSL);
77 "Definition of variable with array type needs an explicit size or an initializer");
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");
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");
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");
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");
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);
108TEST(shader_tool, Swizzle)
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);
119TEST(shader_tool, BinaryLiterals)
122 string input = R
"(0b1 0b10u 0b10001000100010001000100010001000)";
123 string expect = R
"(1 2u 2290649224)";
124 auto [
output, _, error] = process_test_local(input);
130TEST(shader_tool, MatrixConstructors)
133 GTEST_SKIP() <<
"This processing is only done for metal compatibility.";
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);
145TEST(shader_tool, CommaDeclaration)
159 A A_ctor_() {A r;r.a=0;r.b=0;return r;}
163 auto [
output, _, error] = process_test_string(input);
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);
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);
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);
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.");
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.");
203 string input = R
"(void func() { auto &a = b[0 + 1]; })";
204 auto [
output, _, error] = process_test_string(input);
206 "Array subscript inside reference declaration must be a single variable or a "
207 "constant, not an expression.");
210 string input = R
"(void func() { auto &a = b[c]; })";
211 auto [
output, _, error] = process_test_string(input);
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.");
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.");
223 string input = R
"(auto &a = b;)";
224 auto [
output, _, error] = process_test_string(input);
225 EXPECT_EQ(error,
"Unexpected token \"&\": Expecting declaration");
230int b[1]; const int i = 0; auto &a = b[i]; a = 0;)";
232int b[1]; const int i = 0; b[i]= 0;)";
233 auto [
output, _, error] = process_test_local(input, Language::BSL);
239int b(int){ return 0; }
240void f(){ auto &a = b(0); }
242 auto [
output, _, error] = process_test_string(input, Language::BSL);
243 EXPECT_EQ(error,
"Function calls are not allowed in reference definition");
247int b(int){ return 0; }
248void f(){ int c[1]; auto &a = c[b(0)]; }
250 auto [
output, _, error] = process_test_string(input, Language::BSL);
251 EXPECT_EQ(error,
"Function calls are not allowed in reference definition");
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");
261int b[1]; const int i = 0; auto &a = b[i + 1];)";
262 auto [
output, _, error] = process_test_local(input, Language::BSL);
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'");
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");
281 auto [
output, _, error] = process_test_local(input, Language::BSL);
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");
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");
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; })";
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);
327TEST(shader_tool, ConstexprVector)
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);
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);
340 auto [
output, _, error] = process_test_string(input, Language::BSL);
346static constexpr int3 j = int3(0, 1, 2) + int2(1);
348 auto [
output, _, error] = process_test_string(input, Language::BSL);
349 EXPECT_EQ(error,
"Invalid operands to binary expression ('int3' + 'int2')");
353static constexpr int3 j = int3(int2(0), 2);
355 auto [
output, _, error] = process_test_string(input, Language::BSL);
356 EXPECT_EQ(error,
"Constexpr variable 'j' must be initialized by a constant expression");
360static constexpr int3 j = int3(0, 1, 2) + false;
362 auto [
output, _, error] = process_test_string(input, Language::BSL);
363 EXPECT_EQ(error,
"Invalid operands to binary expression ('int3' + 'bool')");
367static constexpr int3 j = int2(0);
369 auto [
output, _, error] = process_test_string(input, Language::BSL);
371 "Constexpr variable 'j' must be initialized by a constant expression of type 'int3' "
377static constexpr float3 a = float3(2.5f);
378static constexpr int4 b = int4(-1);
381static constexpr float3 a = float3(2.5f, 2.5f, 2.5f);
382static constexpr int4 b = int4(-1, -1, -1, -1);
384 auto [
output, _, error] = process_test_string(input, Language::BSL);
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;
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);
404 auto [
output, _, error] = process_test_string(input, Language::BSL);
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);
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);
424 auto [
output, _, error] = process_test_string(input, Language::BSL);
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);
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);
444 auto [
output, _, error] = process_test_string(input, Language::BSL);
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));
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;
471 auto [
output, _, error] = process_test_string(input, Language::BSL);
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;
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;
488 auto [
output, _, error] = process_test_string(input, Language::BSL);
496static constexpr int3 v = int3(1, 2, 3) / int3(1, 0, 1);
498 auto [
output, _, error] = process_test_string(input, Language::BSL);
499 EXPECT_EQ(error,
"Division by zero during constexpr evaluation");
504static constexpr int3 j = float3(1.0f, 2.0f, 3.0f);
506 auto [
output, _, error] = process_test_string(input, Language::BSL);
508 "Constexpr variable 'j' must be initialized by a constant expression of type 'int3' "
513TEST(shader_tool, Auto)
519 "auto c{0.0f + 0};\n"
522 "auto f = 0.0f + 0;\n"
527 "auto k = i[0][0];\n"
528 "auto l = i[0][0][0];\n";
531 "float b=float(0.0f);\n"
532 "float c=float(0.0f + 0);\n"
535 "float f = 0.0f + 0;\n"
539 "float4x4 j = i[0];\n"
540 "float4 k = i[0][0];\n"
541 "float l = i[0][0][0];\n";
548TEST(shader_tool, ArrayAggregate)
551 string input =
"int a[4] = {0, 1, 2, 3};\n";
552 string expect =
"int a[4] = {0, 1, 2, 3};\n";
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";
566TEST(shader_tool, AttributeCapacity)
569 string input =
"[[capacity(my_const)]] int a[4];\n";
570 string expect =
" int a[SRT_CONSTANT_my_const];\n";
576 string input =
"struct A { [[capacity(my_const)]] int a[4]; };\n";
578 "struct A { int a[SRT_CONSTANT_my_const];\n"
582 "A A_ctor_() {A r;for(int a =0;a < SRT_CONSTANT_my_const;++a) {r.a[a]=0;}return r;}\n";
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)