9using namespace shader::parser;
10using namespace shader;
50A A_ctor_() {A r;for(int a =0;a < 2;++a) {r.a[a]=float2(0);}return r;}
56B B_ctor_() {B r;r.b=float2(0);return r;}
64C C_ctor_() {C r;r.b=B_ctor_();r.c=float2(0);r.d=float2(0);return r;}
72U U_ctor_() {U r;r._0=float4(0);r._1=float2(0);return r;}
80void _a_set_(_ref(U ,this_), A v) {
89void _b_set_(_ref(U ,this_), B v) {
99void _c_set_(_ref(U ,this_), C v) {
112 auto [
output, _, error] = process_test_string(input, Language::BSL);
125 auto [
output, _, error] = process_test_string(input, Language::BSL);
126 EXPECT_EQ(error,
"Multi-dimensional arrays are not supported");
132 float2 a[SOME_CONST];
137 auto [
output, _, error] = process_test_string(input, Language::BSL);
138 EXPECT_EQ(error,
"Array size is not a constant expression");
146 auto [
output, _, error] = process_test_string(input, Language::BSL);
147 EXPECT_EQ(error,
"Unsupported array at union base scope; wrap it inside a named struct");
151struct [[host_shared]] T {
163#define T_union0_host_shared_ T_union0
164#define T_union0_host_shared_uniform_ T_union0
171 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
174#define T_host_shared_ T
175#define T_host_shared_uniform_ T
184uint4 _a(const T this_);
185void _a_set_(_ref(T ,this_), uint4 value);
186int4 _b(const T this_);
187void _b_set_(_ref(T ,this_), int4 value);
188float4 _c(const T this_);
189void _c_set_(_ref(T ,this_), float4 value);
192 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
194uint4 _a(const T this_) {
196 val = floatBitsToUint(this_.union0.data0);
200void _a_set_(_ref(T ,this_), uint4 value) {
201 this_.union0.data0 = uintBitsToFloat(value);
204int4 _b(const T this_) {
206 val = floatBitsToInt(this_.union0.data0);
210void _b_set_(_ref(T ,this_), int4 value) {
211 this_.union0.data0 = intBitsToFloat(value);
214float4 _c(const T this_) {
216 val = this_.union0.data0;
220void _c_set_(_ref(T ,this_), float4 value) {
221 this_.union0.data0 = value;
225 auto [
output, _, error] = process_test_string(input);
233 auto [
output, _, error] = process_test_string(input, Language::BSL);
234 EXPECT_EQ(error,
"Anonymous unions at namespace or global scope are not supported");
251A A_ctor_() {A r;r._0=float4(0);return r;}
254 return floatBitsToUint(this_._0);
256void _a_set_(_ref(A ,this_), uint4 v) {
257 this_._0 = uintBitsToFloat(v.a);
260 return floatBitsToInt(this_._0);
262void _b_set_(_ref(A ,this_), int4 v) {
263 this_._0 = intBitsToFloat(v.b);
268void _c_set_(_ref(A ,this_), float4 v) {
273 auto [
output, _, error] = process_test_string(input, Language::BSL);
298A_a0 A_a0_ctor_() {A_a0 r;r._0=float4(0);return r;}
300uint4 _a(A_a0 this_) {
301 return floatBitsToUint(this_._0);
303void _a_set_(_ref(A_a0 ,this_), uint4 v) {
304 this_._0 = uintBitsToFloat(v.a);
307 return floatBitsToInt(this_._0);
309void _b_set_(_ref(A_a0 ,this_), int4 v) {
310 this_._0 = intBitsToFloat(v.b);
318 A A_ctor_() {A r;r.a0_=A_a0_ctor_();return r;}
325 auto [
output, _, error] = process_test_string(input, Language::BSL);
331struct [[host_shared]] T {
346#define T_union0_host_shared_ T_union0
347#define T_union0_host_shared_uniform_ T_union0
354 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
358#define T_union1_host_shared_ T_union1
359#define T_union1_host_shared_uniform_ T_union1
366 T_union1 T_union1_ctor_() {T_union1 r;r.data0=float4(0);return r;}
369#define T_host_shared_ T
370#define T_host_shared_uniform_ T
384uint4 _a(const T this_);
385void _a_set_(_ref(T ,this_), uint4 value);
386uint4 _b(const T this_);
387void _b_set_(_ref(T ,this_), uint4 value);
390 T T_ctor_() {T r;r.foo=float2(0);r.bar=float2(0);r.union0=T_union0_ctor_();r.union1=T_union1_ctor_();return r;}
392uint4 _a(const T this_) {
394 val = floatBitsToUint(this_.union0.data0);
398void _a_set_(_ref(T ,this_), uint4 value) {
399 this_.union0.data0 = uintBitsToFloat(value);
402uint4 _b(const T this_) {
404 val = floatBitsToUint(this_.union1.data0);
408void _b_set_(_ref(T ,this_), uint4 value) {
409 this_.union1.data0 = uintBitsToFloat(value);
413 auto [
output, _, error] = process_test_string(input);
419struct [[host_shared]] B {
424struct [[host_shared]] A {
428struct [[host_shared]] T {
436#define B_host_shared_ B
437#define B_host_shared_uniform_ B
444 B B_ctor_() {B r;r.a=packed_float3(0);r.b=0.0f;return r;}
446#define A_host_shared_ A
447#define A_host_shared_uniform_ A
453 A A_ctor_() {A r;r.e=B_ctor_();return r;}
456#define T_union0_host_shared_ T_union0
457#define T_union0_host_shared_uniform_ T_union0
464 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
467#define T_host_shared_ T
468#define T_host_shared_uniform_ T
478void _a_set_(_ref(T ,this_), A value);
481 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
485 val.e.a = this_.union0.data0.xyz;
486 val.e.b = this_.union0.data0.w;
490void _a_set_(_ref(T ,this_), A value) {
491 this_.union0.data0.xyz = value.e.a;
492 this_.union0.data0.w = value.e.b;
496 auto [
output, _, error] = process_test_string(input);
533T T_ctor_() {T r;r.i=0;return r;}
540A_a0_a0 A_a0_a0_ctor_() {A_a0_a0 r;r.a=uint4(0);r.b=int4(0);return r;}
543 float4 _0; float4 _1;
547A_a0 A_a0_ctor_() {A_a0 r;r._0=float4(0);r._1=float4(0);return r;}
549A_a0_a0 _a0_(A_a0 this_) {
551 r.a = floatBitsToUint(this_._0);
552 r.b = floatBitsToInt(this_._1);
555void _a0__set_(_ref(A_a0 ,this_), A_a0_a0 v) {
556 this_._0 = uintBitsToFloat(v.a);
557 this_._1 = intBitsToFloat(v.b);
559uint4 _c(A_a0 this_) {
560 return floatBitsToUint(this_._0);
562void _c_set_(_ref(A_a0 ,this_), uint4 v) {
563 this_._0 = uintBitsToFloat(v.c);
571A_a1 A_a1_ctor_() {A_a1 r;r._0=float4(0);return r;}
575 r.i = floatBitsToInt(this_._0.x);
578void _d_set_(_ref(A_a1 ,this_), T v) {
579 this_._0.x = intBitsToFloat(v.i);
581uint4 _e(A_a1 this_) {
582 return floatBitsToUint(this_._0);
584void _e_set_(_ref(A_a1 ,this_), uint4 v) {
585 this_._0 = uintBitsToFloat(v.e);
595 A A_ctor_() {A r;r.a0_=A_a0_ctor_();r.a1_=A_a1_ctor_();return r;}
605 auto [
output, _, error] = process_test_string(input, Language::BSL);
611struct [[host_shared]] T {
620#define T_union0_host_shared_ T_union0
621#define T_union0_host_shared_uniform_ T_union0
631 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);r.data1=float4(0);r.data2=float4(0);r.data3=float4(0);return r;}
634#define T_host_shared_ T
635#define T_host_shared_uniform_ T
644float4x4 _a(const T this_);
645void _a_set_(_ref(T ,this_), float4x4 value);
648 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
650float4x4 _a(const T this_) {
652 val[0] = this_.union0.data0;
653 val[1] = this_.union0.data1;
654 val[2] = this_.union0.data2;
655 val[3] = this_.union0.data3;
659void _a_set_(_ref(T ,this_), float4x4 value) {
660 this_.union0.data0 = value[0];
661 this_.union0.data1 = value[1];
662 this_.union0.data2 = value[2];
663 this_.union0.data3 = value[3];
667 auto [
output, _, error] = process_test_string(input);
673struct [[host_shared]] T {
679 auto [
output, _, error] = process_test_string(input);
681 "All union members must have their type wrapped using the union_t<T> template.");
687 using namespace shader;
692enum class enum_class : int {
698static constexpr int enum_class_VALUE = 0;
700#define enum_class int
703enum_class enum_class_ctor_() { return enum_class(0); }
709 auto [
output, _, error] = process_test_string(input);
716enum E : int { A, B = 2, C, D = 1, E };
719static constexpr int A = 0;
721static constexpr int B = 2;
723static constexpr int C = B + 1;
725static constexpr int D = 1;
727static constexpr int E = D + 1;
732E E_ctor_() { return E(0); }
736 auto [
output, _, error] = process_test_string(input);
742enum class enum_class : int {
748static constexpr int enum_class_VALUE = 0;
750#define enum_class int
753enum_class enum_class_ctor_() { return enum_class(0); }
759 auto [
output, _, error] = process_test_string(input);
765enum class enum_class {
769 auto [
output, _, error] = process_test_string(input);
770 EXPECT_EQ(error,
"enum declaration must explicitly use an underlying type");
774TEST(shader_tool, EmptyStruct)
785struct S { int _pad;};
787 S S_ctor_() {S r;r._pad=0;return r;}
788struct T { int _pad;};
790 T T_ctor_() {T r;r._pad=0;return r;}
800 U U_ctor_() {U r;r._pad=0;return r;}
804 auto [
output, _, error] = process_test_string(input);
821S S_ctor_() {S r;r._pad=0;return r;}
826T T_ctor_() {T r;r._pad=0;return r;}
831U U_ctor_() {U r;r._pad=0;return r;}
842 auto [
output, _, error] = process_test_string(input, Language::BSL);
848enum : int { VALUE = 0 };
851static constexpr int VALUE = 0;
853 auto [
output, _, error] = process_test_string(input, Language::BSL);
859TEST(shader_tool, NestedStruct)
874 string expect = R
"(#line 4
879S_a0 S_a0_ctor_() {S_a0 r;r.b=0;return r;}
886S_C S_C_ctor_() {S_C r;r.b=0;return r;}
895 S S_ctor_() {S r;r.i=0;r.a0_=S_a0_ctor_();r.c=S_C_ctor_();return r;}
898 auto [
output, _, error] = process_test_string(input, Language::BSL);
904TEST(shader_tool, StructuredBindings)
927 auto [c, d] = test();
940 S S_ctor_() {S r;r.i=0;r.b=0.0f;return r;}
947void fn(S u, _ref(S ,v))
955 S _u0= S_ctor_();int a=_u0.i;float b=_u0.b;
956 S _u1= test();int c=_u1.i;float d=_u1.b;
957 S _u2= t;int e=_u2.i;float f=_u2.b;
958 S _u3= u;int g=_u3.i;float h=_u3.b;
959 S _u4= t;int i=_u4.i;float j=_u4.b;
960 S _u5= v;int k=_u5.i;float l=_u5.b;
963 auto [
output, _, error] = process_test_string(input);
985A A_ctor_() {A r;r.foo=0;r.bar=0;return r;}
994 auto [
output, _, error] = process_test_string(input, Language::BSL);
1004T fn1() { return T{1, 2}; }
1005T fn2() { return T{1, 2, }; }
1006T fn3() { return T{.a=1, .b=2}; }
1007T fn4() { return T{.a=1, .b=2, }; }
1008T fn5() { return {1, 2}; }
1009T fn6() { return {1, 2, }; }
1010T fn7() { return {.a=1, .b=2}; }
1011T fn8() { return {.a=1, .b=2, }; }
1016 T t4=T{.a=1, .b=2, };
1020 T t8={.a=1, .b=2, };
1021 T t9=T{.a=1, .b=T{0, 2}.x};
1022 T t10=T{1, T{0, 2}.x};
1026T fn1() { return _ctor(T) 1, 2 _rotc() ; }
1027T fn2() { return _ctor(T) 1, 2 _rotc() ; }
1028T fn3() { {T _tmp ; _tmp.a=1; _tmp.b=2; return _tmp;}; }
1029T fn4() { {T _tmp ; _tmp.a=1; _tmp.b=2 ; return _tmp;}; }
1030T fn5() { return _ctor(T) 1, 2 _rotc() ; }
1031T fn6() { return _ctor(T) 1, 2 _rotc() ; }
1032T fn7() { {T _tmp ; _tmp.a=1; _tmp.b=2; return _tmp;}; }
1033T fn8() { {T _tmp ; _tmp.a=1; _tmp.b=2 ; return _tmp;}; }
1035 T t1=_ctor(T) 1, 2 _rotc() ;
1036 T t2=_ctor(T) 1, 2 _rotc() ;
1037 T t3; t3.a=1; t3.b=2;
1038 T t4; t4.a=1; t4.b=2 ;
1039 T t5=_ctor(T) 1, 2 _rotc() ;
1040 T t6=_ctor(T) 1, 2 _rotc() ;
1041 T t7; t7.a=1; t7.b=2;
1042 T t8; t8.a=1; t8.b=2 ;
1043 T t9; t9.a=1; t9.b=_ctor(T) 0, 2 _rotc() .x;
1044 T t10=_ctor(T) 1, _ctor(T) 0, 2 _rotc() .x _rotc() ;
1047 auto [
output, _, error] = process_test_string(input);
1054 T t9={1, T{.a=1, .b=2}.a};
1058 EXPECT_EQ(error,
"Designated initializers are only supported in assignments");
1063 T t10={1, float4{0}};
1069 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
1074 T t11={.a=1, .b=T{.a=1, .b=2}.a};
1078 EXPECT_EQ(error,
"Nested initializer lists are not supported");
1083 T t12={.a=1, .b=float4{0}};
1089 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
1095T fn1() { return T{1, 2}; }
1096T fn2() { return T{1, 2, }; }
1097T fn3() { return T{.a=1, .b=2}; }
1098T fn4() { return T{.a=1, .b=2, }; }
1099T fn5() { return {1, 2}; }
1100T fn6() { return {1, 2, }; }
1101T fn7() { return {.a=1, .b=2}; }
1102T fn8() { return {.a=1, .b=2, }; }
1108 T t4=T{.a=1, .b=2, };
1112 T t8={.a=1, .b=2, };
1113 T t9=T{.a=1, .b=T{0, 2}.a};
1114 T t10=T{1, T{0, 2}.a};
1124T T_ctor_() {T r;r.a=0;r.b=0;return r;}
1125T fn1() { return _ctor(T)1, 2 _rotc();
1128T fn2() { return _ctor(T)1, 2 _rotc();
1131T fn3() { return _ctor(T)1, 2 _rotc();
1134T fn4() { return _ctor(T)1, 2 _rotc();
1137T fn5() { return _ctor(T)1, 2 _rotc();
1140T fn6() { return _ctor(T)1, 2 _rotc();
1143T fn7() { return _ctor(T)1, 2 _rotc();
1146T fn8() { return _ctor(T)1, 2 _rotc();
1151 T t1=_ctor(T)1, 2 _rotc();
1152 T t2=_ctor(T)1, 2 _rotc();
1153 T t3=_ctor(T)1, 2 _rotc();
1154 T t4=_ctor(T)1, 2 _rotc();
1155 T t5=_ctor(T)1, 2 _rotc();
1156 T t6=_ctor(T)1, 2 _rotc();
1157 T t7=_ctor(T)1, 2 _rotc();
1158 T t8=_ctor(T)1, 2 _rotc();
1159 T t9=_ctor(T)1, _ctor(T)0, 2 _rotc().a _rotc();
1160 T t10=_ctor(T)1, _ctor(T)0, 2 _rotc().a _rotc();
1163 auto [
output, _, error] = process_test_string(input, Language::BSL);
1171 T t10=T{.a=1,.c=1,.b=1};
1174 auto [
output, _, error] = process_test_string(input, Language::BSL);
1175 EXPECT_EQ(error,
"Field designator 'c' does not refer to any field in type 'T'");
1184 auto [
output, _, error] = process_test_string(input, Language::BSL);
1185 EXPECT_EQ(error,
"Field designator 'a' needs to be specified in declaration order");
1228A A_ctor_() {A r;r.a=0;r.c=0;r.e=0;r.f=0;return r;}
1232 bitfieldExtract(t.a, 0, 2) + 2;
1233 bitfieldExtract(t.a, 2, 15) + 2;
1234 bitfieldExtract(t.c, 0, 30) + 2;
1235 bitfieldExtract(t.c, 30, 1) + 2;
1237 bitfieldExtract(t.f, 0, 1) + 2;
1238 bitfieldInsertAssign(t.a, 2, 0, 2) ;
1239 bitfieldInsertAssign(t.a, 2, 2, 15) ;
1240 bitfieldInsertAssign(t.c, 2, 0, 30) ;
1241 bitfieldInsertAssign(t.c, 2, 30, 1) ;
1243 bitfieldInsertAssign(t.f, 2, 0, 1) ;
1246 auto [
output, _, error] = process_test_string(input, Language::BSL);
1252 string input = R
"(struct A { float a : 2; };)";
1253 auto [
output, _, error] = process_test_string(input, Language::BSL);
1254 EXPECT_EQ(error,
"Bit-field has non-integral type 'float'");
1257 string input = R
"(struct A { int a : -2; };)";
1258 auto [
output, _, error] = process_test_string(input, Language::BSL);
1259 EXPECT_EQ(error,
"Width of bit-field (-2 bits) is negative");
1262 string input = R
"(struct A { int a : 33+2; };)";
1263 auto [
output, _, error] = process_test_string(input, Language::BSL);
1264 EXPECT_EQ(error,
"Width of bit-field (35 bits) exceeds the width of its type");
1267 string input = R
"(struct A { int a : 2 - 2; };)";
1268 auto [
output, _, error] = process_test_string(input, Language::BSL);
1269 EXPECT_EQ(error,
"Width of bit-field is zero");
1272 string input = R
"(struct A { int a : 2, b : 2; };)";
1273 auto [
output, _, error] = process_test_string(input, Language::BSL);
1274 EXPECT_EQ(error,
"Bitfields must have single declarator");
1297A A_ctor_() {A r;r.a=0;return r;}
1301 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) + 1, 0, 2) ;
1302 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) * 1, 0, 2) ;
1303 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) / 1, 0, 2) ;
1304 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) - 1, 0, 2) ;
1305 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) << 1, 0, 2) ;
1306 bitfieldInsertAssign(t.a, bitfieldExtract(t.a, 0, 2) >> 1, 0, 2) ;
1309 auto [
output, _, error] = process_test_string(input, Language::BSL);
1324 auto [
output, _, error] = process_test_string(input, Language::BSL);
1325 EXPECT_EQ(error,
"Increment and decrement operators are not supported on bitfield");
1334 std::string prefix =
"struct S {\n";
1335 std::string suffix = R
"(
1338 )" + buffer + R"( S &a;
1341 auto [
result,
metadata, error] = process_test_string(prefix +
str + suffix, language);
1342 result =
result.substr(prefix.size(),
result.size() - suffix.size() - prefix.size());
1358TEST(shader_tool, StructBufferLinting)
1361 string input = R
"(char d;)";
1363 EXPECT_EQ(error,
"Type 'char' is not allowed in storage buffer");
1366 string input = R
"(float3 d;)";
1370 "Type 'float3' is not allowed in uniform and storage buffer; use 'packed_float3' instead");
1373 string input = R
"(float3 d;)";
1376 "Type 'float3' is not allowed in storage buffer; use 'packed_float3' instead");
1379 string input = R
"(bool d;)";
1381 EXPECT_EQ(error,
"Type 'bool' is not allowed in storage buffer; use 'bool32_t' instead");
1384 string input = R
"(float3x3 d;)";
1386 EXPECT_EQ(error,
"Type 'float3x3' is not allowed in storage buffer; use 'float3x4' instead");
1395 EXPECT_EQ(error,
"Implicit 4 bytes padding before 'b', add manual padding");
1402 EXPECT_EQ(error,
"Implicit 12 bytes padding before 'b', add manual padding");
1409 EXPECT_EQ(error,
"Implicit 12 bytes padding before 'b', add manual padding");
1416 EXPECT_EQ(error,
"Implicit 12 bytes padding before 'b', add manual padding");
1423 EXPECT_EQ(error,
"Implicit 12 bytes padding before 'b', add manual padding");
1453 "Implicit padding of 12 bytes per array element, use a 16 bytes type or use a "
1456 EXPECT_EQ(error_ssbo,
"Implicit 12 bytes padding before 'b', add manual padding");
1467 "Implicit padding of 8 bytes per array element, use a 16 bytes type or use a "
1470 EXPECT_EQ(error_ssbo,
"Implicit 8 bytes padding before 'b', add manual padding");
1482 "Implicit padding of 12 bytes per array element, use a 16 bytes type or use a "
1496 "Implicit padding of 8 bytes per array element, use a 16 bytes type or use a "
1504 float b[SOME_CONST];
1507 EXPECT_EQ(error,
"Array size is not a constant expression");
1515 EXPECT_EQ(error,
"Implicit 12 bytes padding at the end of 'S', add manual padding");
1525 EXPECT_EQ(error,
"Implicit 8 bytes padding at the end of 'S', add manual padding");
1535 EXPECT_EQ(error,
"Implicit 4 bytes padding before 'b', add manual padding");
1543 EXPECT_EQ(error,
"Implicit 4 bytes padding at the end of 'S', add manual padding");
1550 EXPECT_EQ(error,
"Implicit 4 bytes padding at the end of 'S', add manual padding");
1559 EXPECT_EQ(error,
"Implicit 4 bytes padding at the end of 'S', add manual padding");
1565struct T { uint a; };
1570 EXPECT_EQ(error,
"Implicit 12 bytes padding at the end of 'T', add manual padding");
1572 EXPECT_EQ(error_ssbo,
"Implicit 12 bytes padding before 'b', add manual padding");
1576struct T { float2 a; };
1581 EXPECT_EQ(error,
"Implicit 8 bytes padding at the end of 'T', add manual padding");
1583 EXPECT_EQ(error_ssbo,
"Implicit 12 bytes padding before 'b', add manual padding");
#define EXPECT_EQ(result, expect)
static Result process_test_storage(std::string str, shader::Language language=shader::Language::BLENDER_GLSL)
static Result process_test_uniform(std::string str, shader::Language language=shader::Language::BLENDER_GLSL)
static Result process_test_buffer(std::string str, std::string buffer, shader::Language language)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)