Blender V5.3
struct_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::parser;
10using namespace shader;
11using namespace std;
12
13TEST(shader_tool, Union)
14{
15 {
16 string input = R"(
17struct A {
18 float2 a[2];
19};
20
21struct B {
22 float2 b;
23};
24
25struct C {
26 B b;
27 float2 c;
28 float2 d;
29};
30
31union U {
32 A a;
33 B b;
34 C c;
35};
36
37C f(A a)
38{
39 U u;
40 u.a = a;
41 return u.c;
42}
43)";
44 string expect =
45 R"(
46struct A {
47 float2 a[2];
48};
49#line 2
50A A_ctor_() {A r;for(int a =0;a < 2;++a) {r.a[a]=float2(0);}return r;}
51#line 6
52struct B {
53 float2 b;
54};
55#line 6
56B B_ctor_() {B r;r.b=float2(0);return r;}
57#line 10
58struct C {
59 B b;
60 float2 c;
61 float2 d;
62};
63#line 10
64C C_ctor_() {C r;r.b=B_ctor_();r.c=float2(0);r.d=float2(0);return r;}
65#line 16
66struct U {
67 float4 _0; float2 _1;
68
69
70};
71#line 16
72U U_ctor_() {U r;r._0=float4(0);r._1=float2(0);return r;}
73
74A _a(U this_) {
75 A r;
76 r.a[0] = this_._0.xy;
77 r.a[1] = this_._0.zw;
78 return r;
79}
80void _a_set_(_ref(U ,this_), A v) {
81 this_._0.xy = v.a[0];
82 this_._0.zw = v.a[1];
83}
84B _b(U this_) {
85 B r;
86 r.b = this_._0.xy;
87 return r;
88}
89void _b_set_(_ref(U ,this_), B v) {
90 this_._0.xy = v.b;
91}
92C _c(U this_) {
93 C r;
94 r.b.b = this_._0.xy;
95 r.c = this_._0.zw;
96 r.d = this_._1.xy;
97 return r;
98}
99void _c_set_(_ref(U ,this_), C v) {
100 this_._0.xy = v.b.b;
101 this_._0.zw = v.c;
102 this_._1.xy = v.d;
103}
104#line 22
105C f(A a)
106{
107 U u;
108 _a_set_(u, a);
109 return _c(u);
110}
111)";
112 auto [output, _, error] = process_test_string(input, Language::BSL);
113 EXPECT_EQ(output, expect);
114 EXPECT_EQ(error, "");
115 }
116 {
117 string input = R"(
118union U {
119 struct A {
120 float2 a[2][2];
121 };
122 A a;
123};
124)";
125 auto [output, _, error] = process_test_string(input, Language::BSL);
126 EXPECT_EQ(error, "Multi-dimensional arrays are not supported");
127 }
128 {
129 string input = R"(
130union U {
131 struct A {
132 float2 a[SOME_CONST];
133 };
134 A a;
135};
136)";
137 auto [output, _, error] = process_test_string(input, Language::BSL);
138 EXPECT_EQ(error, "Array size is not a constant expression");
139 }
140 {
141 string input = R"(
142union U {
143 float2 a[2];
144};
145)";
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");
148 }
149 {
150 string input = R"(
151struct [[host_shared]] T {
152 union {
153 union_t<uint4> a;
154 union_t<int4> b;
155 union_t<float4> c;
156 };
157};
158)";
159 string expect =
160 R"(
161
163#define T_union0_host_shared_ T_union0
164#define T_union0_host_shared_uniform_ T_union0
165#line 3
166struct T_union0 {
167 float4 data0;
168
169};
170#line 3
171 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
172#line 2
173
174#define T_host_shared_ T
175#define T_host_shared_uniform_ T
176#line 2
177struct T {
178 T_union0 union0;
179#line 38
180};
181
182#ifndef GPU_METAL
183T T_ctor_();
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);
190#endif
191#line 2
192 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
193#line 9
194uint4 _a(const T this_) {
195 uint4 val;
196 val = floatBitsToUint(this_.union0.data0);
197 return val;
198}
199
200void _a_set_(_ref(T ,this_), uint4 value) {
201 this_.union0.data0 = uintBitsToFloat(value);
202}
203
204int4 _b(const T this_) {
205 int4 val;
206 val = floatBitsToInt(this_.union0.data0);
207 return val;
208}
209
210void _b_set_(_ref(T ,this_), int4 value) {
211 this_.union0.data0 = intBitsToFloat(value);
212}
213
214float4 _c(const T this_) {
215 float4 val;
216 val = this_.union0.data0;
217 return val;
218}
219
220void _c_set_(_ref(T ,this_), float4 value) {
221 this_.union0.data0 = value;
222}
223
224)";
225 auto [output, _, error] = process_test_string(input);
226 EXPECT_EQ(output, expect);
227 EXPECT_EQ(error, "");
228 }
229 {
230 string input = R"(
231union {};
232)";
233 auto [output, _, error] = process_test_string(input, Language::BSL);
234 EXPECT_EQ(error, "Anonymous unions at namespace or global scope are not supported");
235 }
236 {
237 string input = R"(
238union A {
239 uint4 a;
240 int4 b;
241 float4 c;
242};
243)";
244 string expect = R"(
245struct A {
246 float4 _0;
247
248
249};
250#line 2
251A A_ctor_() {A r;r._0=float4(0);return r;}
252
253uint4 _a(A this_) {
254 return floatBitsToUint(this_._0);
255}
256void _a_set_(_ref(A ,this_), uint4 v) {
257 this_._0 = uintBitsToFloat(v.a);
258}
259int4 _b(A this_) {
260 return floatBitsToInt(this_._0);
261}
262void _b_set_(_ref(A ,this_), int4 v) {
263 this_._0 = intBitsToFloat(v.b);
264}
265float4 _c(A this_) {
266 return this_._0;
267}
268void _c_set_(_ref(A ,this_), float4 v) {
269 this_._0 = v.c;
270}
271#line 8
272)";
273 auto [output, _, error] = process_test_string(input, Language::BSL);
274 EXPECT_EQ(output, expect);
275 EXPECT_EQ(error, "");
276 }
277 {
278 string input = R"(
279struct A {
280 union {
281 uint4 a;
282 int4 b;
283 };
284};
285
286void f(A a) {
287 a.a;
288 a.b;
289}
290)";
291 string expect = R"(
292
293 struct A_a0 {
294 float4 _0;
295
296 };
297#line 3
298A_a0 A_a0_ctor_() {A_a0 r;r._0=float4(0);return r;}
299
300uint4 _a(A_a0 this_) {
301 return floatBitsToUint(this_._0);
302}
303void _a_set_(_ref(A_a0 ,this_), uint4 v) {
304 this_._0 = uintBitsToFloat(v.a);
306int4 _b(A_a0 this_) {
307 return floatBitsToInt(this_._0);
308}
309void _b_set_(_ref(A_a0 ,this_), int4 v) {
310 this_._0 = intBitsToFloat(v.b);
312#line 2
313struct A {
314 A_a0 a0_;
315#line 7
316};
317#line 3
318 A A_ctor_() {A r;r.a0_=A_a0_ctor_();return r;}
319#line 9
320void f(A a) {
321 _a(a.a0_);
322 _b(a.a0_);
323}
324)";
325 auto [output, _, error] = process_test_string(input, Language::BSL);
326 EXPECT_EQ(output, expect);
327 EXPECT_EQ(error, "");
328 }
329 {
330 string input = R"(
331struct [[host_shared]] T {
332 float2 foo;
333 float2 bar;
334 union {
335 union_t<uint4> a;
336 };
337 union {
338 union_t<uint4> b;
339 };
340};
341)";
342 string expect =
343 R"(
344#line 5
345
346#define T_union0_host_shared_ T_union0
347#define T_union0_host_shared_uniform_ T_union0
348#line 5
349struct T_union0 {
350 float4 data0;
351
352};
353#line 5
354 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
355
356
357
358#define T_union1_host_shared_ T_union1
359#define T_union1_host_shared_uniform_ T_union1
360#line 8
361struct T_union1 {
362 float4 data0;
363
364};
365#line 8
366 T_union1 T_union1_ctor_() {T_union1 r;r.data0=float4(0);return r;}
367#line 2
368
369#define T_host_shared_ T
370#define T_host_shared_uniform_ T
371#line 2
372struct T {
373 float2 foo;
374 float2 bar;
375 T_union0 union0;
376
377
378 T_union1 union1;
379#line 31
380};
381
382#ifndef GPU_METAL
383T T_ctor_();
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);
388#endif
389#line 2
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;}
391#line 12
392uint4 _a(const T this_) {
393 uint4 val;
394 val = floatBitsToUint(this_.union0.data0);
395 return val;
396}
397
398void _a_set_(_ref(T ,this_), uint4 value) {
399 this_.union0.data0 = uintBitsToFloat(value);
400}
401
402uint4 _b(const T this_) {
403 uint4 val;
404 val = floatBitsToUint(this_.union1.data0);
405 return val;
406}
407
408void _b_set_(_ref(T ,this_), uint4 value) {
409 this_.union1.data0 = uintBitsToFloat(value);
410}
411
412)";
413 auto [output, _, error] = process_test_string(input);
414 EXPECT_EQ(output, expect);
415 EXPECT_EQ(error, "");
416 }
417 {
418 string input = R"(
419struct [[host_shared]] B {
420 packed_float3 a;
421 float b;
422};
423
424struct [[host_shared]] A {
425 struct B e;
426};
427
428struct [[host_shared]] T {
429 union {
430 union_t<A> a;
431 };
432};
433)";
434 string expect = R"(
435
436#define B_host_shared_ B
437#define B_host_shared_uniform_ B
438#line 2
439struct B {
440 packed_float3 a;
441 float b;
442};
443#line 2
444 B B_ctor_() {B r;r.a=packed_float3(0);r.b=0.0f;return r;}
445#line 8
446#define A_host_shared_ A
447#define A_host_shared_uniform_ A
448#line 7
449struct A {
450 B e;
451};
452#line 7
453 A A_ctor_() {A r;r.e=B_ctor_();return r;}
454#line 12
455
456#define T_union0_host_shared_ T_union0
457#define T_union0_host_shared_uniform_ T_union0
458#line 12
459struct T_union0 {
460 float4 data0;
461
462};
463#line 12
464 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
465#line 11
466
467#define T_host_shared_ T
468#define T_host_shared_uniform_ T
469#line 11
470struct T {
471 T_union0 union0;
472#line 27
473};
474
475#ifndef GPU_METAL
476T T_ctor_();
477A _a(const T this_);
478void _a_set_(_ref(T ,this_), A value);
479#endif
480#line 11
481 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
482#line 16
483A _a(const T this_) {
484 A val;
485 val.e.a = this_.union0.data0.xyz;
486 val.e.b = this_.union0.data0.w;
487 return val;
488}
489
490void _a_set_(_ref(T ,this_), A value) {
491 this_.union0.data0.xyz = value.e.a;
492 this_.union0.data0.w = value.e.b;
493}
494
495)";
496 auto [output, _, error] = process_test_string(input);
497 EXPECT_EQ(output, expect);
498 EXPECT_EQ(error, "");
499 }
500
501 {
502 string input = R"(
503struct T {
504 int i;
505};
506struct A {
507 union {
508 struct {
509 uint4 a;
510 int4 b;
511 };
512 uint4 c;
513 };
514 union {
515 T d;
516 uint4 e;
517 };
518};
519
520void f(A a) {
521a.a;
522a.b;
523a.c;
524a.d.i;
525a.e;
526}
527)";
528 string expect = R"(
529struct T {
530 int i;
531};
532#line 2
533T T_ctor_() {T r;r.i=0;return r;}
534#line 7
535 struct A_a0_a0 {
536 uint4 a;
537 int4 b;
538 };
539#line 7
540A_a0_a0 A_a0_a0_ctor_() {A_a0_a0 r;r.a=uint4(0);r.b=int4(0);return r;}
541#line 6
542 struct A_a0 {
543 float4 _0; float4 _1;
544#line 12
545 };
546#line 6
547A_a0 A_a0_ctor_() {A_a0 r;r._0=float4(0);r._1=float4(0);return r;}
548
549A_a0_a0 _a0_(A_a0 this_) {
550 A_a0_a0 r;
551 r.a = floatBitsToUint(this_._0);
552 r.b = floatBitsToInt(this_._1);
553 return r;
554}
555void _a0__set_(_ref(A_a0 ,this_), A_a0_a0 v) {
556 this_._0 = uintBitsToFloat(v.a);
557 this_._1 = intBitsToFloat(v.b);
558}
559uint4 _c(A_a0 this_) {
560 return floatBitsToUint(this_._0);
561}
562void _c_set_(_ref(A_a0 ,this_), uint4 v) {
563 this_._0 = uintBitsToFloat(v.c);
564}
565#line 13
566 struct A_a1 {
567 float4 _0;
568
569 };
570#line 13
571A_a1 A_a1_ctor_() {A_a1 r;r._0=float4(0);return r;}
572
573T _d(A_a1 this_) {
574 T r;
575 r.i = floatBitsToInt(this_._0.x);
576 return r;
577}
578void _d_set_(_ref(A_a1 ,this_), T v) {
579 this_._0.x = intBitsToFloat(v.i);
580}
581uint4 _e(A_a1 this_) {
582 return floatBitsToUint(this_._0);
583}
584void _e_set_(_ref(A_a1 ,this_), uint4 v) {
585 this_._0 = uintBitsToFloat(v.e);
586}
587#line 5
588struct A {
589 A_a0 a0_;
590#line 13
591 A_a1 a1_;
592#line 17
593};
594#line 13
595 A A_ctor_() {A r;r.a0_=A_a0_ctor_();r.a1_=A_a1_ctor_();return r;}
596#line 19
597void f(A a) {
598_a0_(a.a0_).a;
599_a0_(a.a0_).b;
600_c(a.a0_);
601_d(a.a1_).i;
602_e(a.a1_);
603}
604)";
605 auto [output, _, error] = process_test_string(input, Language::BSL);
606 EXPECT_EQ(output, expect);
607 EXPECT_EQ(error, "");
608 }
609 {
610 string input = R"(
611struct [[host_shared]] T {
612 union {
613 union_t<float4x4> a;
614 };
615};
616)";
617 string expect = R"(
618
619
620#define T_union0_host_shared_ T_union0
621#define T_union0_host_shared_uniform_ T_union0
622#line 3
623struct T_union0 {
624 float4 data0;
625 float4 data1;
626 float4 data2;
627 float4 data3;
628
629};
630#line 3
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;}
632#line 2
633
634#define T_host_shared_ T
635#define T_host_shared_uniform_ T
636#line 2
637struct T {
638 T_union0 union0;
639#line 22
640};
641
642#ifndef GPU_METAL
643T T_ctor_();
644float4x4 _a(const T this_);
645void _a_set_(_ref(T ,this_), float4x4 value);
646#endif
647#line 2
648 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
649#line 7
650float4x4 _a(const T this_) {
651 float4x4 val;
652 val[0] = this_.union0.data0;
653 val[1] = this_.union0.data1;
654 val[2] = this_.union0.data2;
655 val[3] = this_.union0.data3;
656 return val;
657}
658
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];
664}
665
666)";
667 auto [output, _, error] = process_test_string(input);
668 EXPECT_EQ(output, expect);
669 EXPECT_EQ(error, "");
670 }
671 {
672 string input = R"(
673struct [[host_shared]] T {
674 union {
675 uint a;
676 };
677};
678)";
679 auto [output, _, error] = process_test_string(input);
680 EXPECT_EQ(error,
681 "All union members must have their type wrapped using the union_t<T> template.");
682 }
683}
684
685TEST(shader_tool, Enum)
686{
687 using namespace shader;
688 using namespace std;
689
690 {
691 string input = R"(
692enum class enum_class : int {
693 VALUE = 0,
694};
695)";
696 string expect = R"(
697
698static constexpr int enum_class_VALUE = 0;
699
700#define enum_class int
701#line 2
702
703enum_class enum_class_ctor_() { return enum_class(0); }
704#line 2
705
706
707
708)";
709 auto [output, _, error] = process_test_string(input);
710 EXPECT_EQ(output, expect);
711 EXPECT_EQ(error, "");
712 }
713
714 {
715 string input = R"(
716enum E : int { A, B = 2, C, D = 1, E };
717)";
718 string expect = R"(
719static constexpr int A = 0;
720#line 2
721static constexpr int B = 2;
722#line 2
723static constexpr int C = B + 1;
724#line 2
725static constexpr int D = 1;
726#line 2
727static constexpr int E = D + 1;
728
729#define E int
730#line 2
731
732E E_ctor_() { return E(0); }
733#line 2
734
735)";
736 auto [output, _, error] = process_test_string(input);
737 EXPECT_EQ(output, expect);
738 EXPECT_EQ(error, "");
739 }
740 {
741 string input = R"(
742enum class enum_class : int {
743 VALUE = 0,
744};
745)";
746 string expect = R"(
747
748static constexpr int enum_class_VALUE = 0;
749
750#define enum_class int
751#line 2
752
753enum_class enum_class_ctor_() { return enum_class(0); }
754#line 2
755
756
757
758)";
759 auto [output, _, error] = process_test_string(input);
760 EXPECT_EQ(output, expect);
761 EXPECT_EQ(error, "");
762 }
763 {
764 string input = R"(
765enum class enum_class {
766 VALUE = 0,
767};
768)";
769 auto [output, _, error] = process_test_string(input);
770 EXPECT_EQ(error, "enum declaration must explicitly use an underlying type");
771 }
772}
773
774TEST(shader_tool, EmptyStruct)
775{
776 {
777 string input = R"(
778class S {};
779struct T {};
780struct U {
781 static void fn() {}
782};
783)";
784 string expect = R"(
785struct S { int _pad;};
786#line 2
787 S S_ctor_() {S r;r._pad=0;return r;}
788struct T { int _pad;};
789#line 3
790 T T_ctor_() {T r;r._pad=0;return r;}
791struct U {
792
793int _pad;};
794
795#ifndef GPU_METAL
796U U_ctor_();
797void U_fn();
798#endif
799#line 4
800 U U_ctor_() {U r;r._pad=0;return r;}
801 void U_fn() {}
802
803)";
804 auto [output, _, error] = process_test_string(input);
805 EXPECT_EQ(output, expect);
806 EXPECT_EQ(error, "");
807 }
808 {
809 string input = R"(
810class S {};
811struct T {};
812struct U {
813 static void fn() {}
814};
815)";
816 string expect = R"(
817struct S {int _pad;
818#line 2
819 };
820#line 2
821S S_ctor_() {S r;r._pad=0;return r;}
822struct T {int _pad;
823#line 3
824 };
825#line 3
826T T_ctor_() {T r;r._pad=0;return r;}
827struct U {
828 int _pad;
829};
830#line 4
831U U_ctor_() {U r;r._pad=0;return r;}
832
833#ifndef GPU_METAL
834 void U_fn();
835#endif
836#line 5
837 void U_fn() {
838#line 5
839 }
840
841)";
842 auto [output, _, error] = process_test_string(input, Language::BSL);
843 EXPECT_EQ(output, expect);
844 EXPECT_EQ(error, "");
845 }
846 {
847 string input = R"(
848enum : int { VALUE = 0 };
849)";
850 string expect = R"(
851static constexpr int VALUE = 0;
852)";
853 auto [output, _, error] = process_test_string(input, Language::BSL);
854 EXPECT_EQ(output, expect);
855 EXPECT_EQ(error, "");
856 }
857}
858
859TEST(shader_tool, NestedStruct)
860{
861 {
862 string input = R"(
863struct S {
864 int i;
865 struct {
866 int b;
867 };
868 struct C {
869 int b;
870 };
871 C c;
872};
873)";
874 string expect = R"(#line 4
875 struct S_a0 {
876 int b;
877 };
878#line 4
879S_a0 S_a0_ctor_() {S_a0 r;r.b=0;return r;}
880
881
882 struct S_C {
883 int b;
884 };
885#line 7
886S_C S_C_ctor_() {S_C r;r.b=0;return r;}
887#line 2
888struct S {
889 int i;
890 S_a0 a0_;
891#line 10
892 S_C c;
893};
894#line 4
895 S S_ctor_() {S r;r.i=0;r.a0_=S_a0_ctor_();r.c=S_C_ctor_();return r;}
896#line 13
897)";
898 auto [output, _, error] = process_test_string(input, Language::BSL);
899 EXPECT_EQ(output, expect);
900 EXPECT_EQ(error, "");
901 }
902}
903
904TEST(shader_tool, StructuredBindings)
905{
906 {
907 string input = R"(
908struct S {
909 int i;
910 float b;
911};
912
913S test()
914{
915 return S{};
916}
917
918void fn(S u, S &v)
919{
920 S t;
921 S &r = t;
922 {
923 int u;
924 int t;
925 }
926 auto [a, b] = S{};
927 auto [c, d] = test();
928 auto [e, f] = t;
929 auto [g, h] = u;
930 auto [i, j] = r;
931 auto [k, l] = v;
932}
933)";
934 string expect = R"(
935struct S {
936 int i;
937 float b;
938};
939#line 2
940 S S_ctor_() {S r;r.i=0;r.b=0.0f;return r;}
941#line 7
942S test()
943{
944 return S_ctor_();
945}
946
947void fn(S u, _ref(S ,v))
948{
949 S t;
950
951 {
952 int u;
953 int t;
954 }
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;
961}
962)";
963 auto [output, _, error] = process_test_string(input);
964 EXPECT_EQ(output, expect);
965 EXPECT_EQ(error, "");
966 }
967 {
968 string input = R"(
969struct A {
970 int foo, bar;
971};
972
973void f()
974{
975 A c;
976 auto [a, b] = c;
977 a + b;
978}
979)";
980 string expect = R"(
981struct A {
982 int foo, bar;
983};
984#line 2
985A A_ctor_() {A r;r.foo=0;r.bar=0;return r;}
986#line 6
987void f()
988{
989 A c;
990 A _4 = c;
991 _4.foo + _4.bar;
992}
993)";
994 auto [output, _, error] = process_test_string(input, Language::BSL);
995 EXPECT_EQ(output, expect);
996 EXPECT_EQ(error, "");
997 }
998}
999
1000TEST(shader_tool, InitializerList)
1001{
1002 {
1003 string input = R"(
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, }; }
1012void fn() {
1013 T t1=T{1, 2};
1014 T t2=T{1, 2, };
1015 T t3=T{.a=1, .b=2};
1016 T t4=T{.a=1, .b=2, };
1017 T t5={1, 2};
1018 T t6={1, 2, };
1019 T t7={.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};
1023}
1024)";
1025 string expect = R"(
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;}; }
1034void fn() {
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() ;
1045}
1046)";
1047 auto [output, _, error] = process_test_string(input);
1048 EXPECT_EQ(output, expect);
1049 EXPECT_EQ(error, "");
1050 }
1051 {
1052 string input = R"(
1053void fn() {
1054 T t9={1, T{.a=1, .b=2}.a};
1055}
1056)";
1057 auto [output, metadata, error] = process_test_string(input);
1058 EXPECT_EQ(error, "Designated initializers are only supported in assignments");
1059 }
1060 {
1061 string input = R"(
1062void fn() {
1063 T t10={1, float4{0}};
1064}
1065)";
1066 auto [output, metadata, error] = process_test_string(input);
1067 EXPECT_EQ(
1068 error,
1069 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
1070 }
1071 {
1072 string input = R"(
1073void fn() {
1074 T t11={.a=1, .b=T{.a=1, .b=2}.a};
1075}
1076)";
1077 auto [output, metadata, error] = process_test_string(input);
1078 EXPECT_EQ(error, "Nested initializer lists are not supported");
1079 }
1080 {
1081 string input = R"(
1082void fn() {
1083 T t12={.a=1, .b=float4{0}};
1084}
1085)";
1086 auto [output, metadata, error] = process_test_string(input);
1087 EXPECT_EQ(
1088 error,
1089 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
1090 }
1091
1092 {
1093 string input = R"(
1094struct T{int a,b;};
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, }; }
1103void fn() {
1104 T t0={};
1105 T t1=T{1, 2};
1106 T t2=T{1, 2, };
1107 T t3=T{.a=1, .b=2};
1108 T t4=T{.a=1, .b=2, };
1109 T t5={1, 2};
1110 T t6={1, 2, };
1111 T t7={.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};
1115}
1116)";
1117 string expect = R"(
1118struct T{
1119#line 2
1120 int a,b;
1121#line 2
1122 };
1123#line 2
1124T T_ctor_() {T r;r.a=0;r.b=0;return r;}
1125T fn1() { return _ctor(T)1, 2 _rotc();
1126#line 3
1127 }
1128T fn2() { return _ctor(T)1, 2 _rotc();
1129#line 4
1130 }
1131T fn3() { return _ctor(T)1, 2 _rotc();
1132#line 5
1133 }
1134T fn4() { return _ctor(T)1, 2 _rotc();
1135#line 6
1136 }
1137T fn5() { return _ctor(T)1, 2 _rotc();
1138#line 7
1139 }
1140T fn6() { return _ctor(T)1, 2 _rotc();
1141#line 8
1142 }
1143T fn7() { return _ctor(T)1, 2 _rotc();
1144#line 9
1145 }
1146T fn8() { return _ctor(T)1, 2 _rotc();
1147#line 10
1148 }
1149void fn() {
1150 T t0=T_ctor_();
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();
1161}
1162)";
1163 auto [output, _, error] = process_test_string(input, Language::BSL);
1164 EXPECT_EQ(output, expect);
1165 EXPECT_EQ(error, "");
1166 }
1167 {
1168 string input = R"(
1169struct T{int a,b;};
1170void fn() {
1171 T t10=T{.a=1,.c=1,.b=1};
1172}
1173)";
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'");
1176 }
1177 {
1178 string input = R"(
1179struct T{int a,b;};
1180void fn() {
1181 T t10=T{.b=1,.a=1};
1182}
1183)";
1184 auto [output, _, error] = process_test_string(input, Language::BSL);
1185 EXPECT_EQ(error, "Field designator 'a' needs to be specified in declaration order");
1186 }
1187}
1188
1189TEST(shader_tool, BitField)
1190{
1191 {
1192 string input = R"(
1193struct A {
1194 int a : 2;
1195 int b : 15;
1196 int c : 30;
1197 int d : 1;
1198 int e;
1199 int f : 1;
1200};
1201
1202void f(A t)
1203{
1204 t.a + 2;
1205 t.b + 2;
1206 t.c + 2;
1207 t.d + 2;
1208 t.e + 2;
1209 t.f + 2;
1210 t.a = 2;
1211 t.b = 2;
1212 t.c = 2;
1213 t.d = 2;
1214 t.e = 2;
1215 t.f = 2;
1216}
1217)";
1218 string expect = R"(
1219struct A {
1220 int a ;
1221
1222 int c ;
1223
1224 int e;
1225 int f ;
1226};
1227#line 2
1228A A_ctor_() {A r;r.a=0;r.c=0;r.e=0;r.f=0;return r;}
1229#line 11
1230void f(A t)
1231{
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;
1236 t.e + 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) ;
1242 t.e = 2;
1243 bitfieldInsertAssign(t.f, 2, 0, 1) ;
1244}
1245)";
1246 auto [output, _, error] = process_test_string(input, Language::BSL);
1247 EXPECT_EQ(output, expect);
1248 EXPECT_EQ(error, "");
1249 }
1250
1251 {
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'");
1255 }
1256 {
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");
1260 }
1261 {
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");
1265 }
1266 {
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");
1270 }
1271 {
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");
1275 }
1276 {
1277 string input = R"(
1278struct A {
1279 int a : 2;
1280};
1281
1282void f(A t)
1283{
1284 t.a += 1;
1285 t.a *= 1;
1286 t.a /= 1;
1287 t.a -= 1;
1288 t.a <<= 1;
1289 t.a >>= 1;
1290}
1291)";
1292 string expect = R"(
1293struct A {
1294 int a ;
1295};
1296#line 2
1297A A_ctor_() {A r;r.a=0;return r;}
1298#line 6
1299void f(A t)
1300{
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) ;
1307}
1308)";
1309 auto [output, _, error] = process_test_string(input, Language::BSL);
1310 EXPECT_EQ(output, expect);
1311 EXPECT_EQ(error, "");
1312 }
1313 {
1314 string input = R"(
1315struct A {
1316 int a : 2;
1317};
1318
1319void f(A t)
1320{
1321 t.a++;
1322}
1323)";
1324 auto [output, _, error] = process_test_string(input, Language::BSL);
1325 EXPECT_EQ(error, "Increment and decrement operators are not supported on bitfield");
1326 }
1327}
1328
1329/* Process a test string inside a wrapper function. */
1330static inline Result process_test_buffer(std::string str,
1331 std::string buffer,
1332 shader::Language language)
1333{
1334 std::string prefix = "struct S {\n";
1335 std::string suffix = R"(
1336};
1337struct Res {
1338 )" + buffer + R"( S &a;
1339};
1340)";
1341 auto [result, metadata, error] = process_test_string(prefix + str + suffix, language);
1342 result = result.substr(prefix.size(), result.size() - suffix.size() - prefix.size());
1343 return {result, metadata, error};
1344}
1345
1346static inline Result process_test_uniform(
1347 std::string str, shader::Language language = shader::Language::BLENDER_GLSL)
1348{
1349 return process_test_buffer(str, "[[uniform(0)]]", language);
1350}
1351
1352static inline Result process_test_storage(
1353 std::string str, shader::Language language = shader::Language::BLENDER_GLSL)
1354{
1355 return process_test_buffer(str, "[[storage(0, read)]]", language);
1356}
1357
1358TEST(shader_tool, StructBufferLinting)
1359{
1360 {
1361 string input = R"(char d;)";
1362 auto [output, _, error] = process_test_storage(input, Language::BSL);
1363 EXPECT_EQ(error, "Type 'char' is not allowed in storage buffer");
1364 }
1365 {
1366 string input = R"(float3 d;)";
1367 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1368 EXPECT_EQ(
1369 error,
1370 "Type 'float3' is not allowed in uniform and storage buffer; use 'packed_float3' instead");
1371 }
1372 {
1373 string input = R"(float3 d;)";
1374 auto [output, _, error] = process_test_storage(input, Language::BSL);
1375 EXPECT_EQ(error,
1376 "Type 'float3' is not allowed in storage buffer; use 'packed_float3' instead");
1377 }
1378 {
1379 string input = R"(bool d;)";
1380 auto [output, _, error] = process_test_storage(input, Language::BSL);
1381 EXPECT_EQ(error, "Type 'bool' is not allowed in storage buffer; use 'bool32_t' instead");
1382 }
1383 {
1384 string input = R"(float3x3 d;)";
1385 auto [output, _, error] = process_test_storage(input, Language::BSL);
1386 EXPECT_EQ(error, "Type 'float3x3' is not allowed in storage buffer; use 'float3x4' instead");
1387 }
1388
1389 /* Basic padding. */
1390 {
1391 string input = R"(
1392float a;
1393float2 b;)";
1394 auto [output, _, error] = process_test_storage(input, Language::BSL);
1395 EXPECT_EQ(error, "Implicit 4 bytes padding before 'b', add manual padding");
1396 }
1397 {
1398 string input = R"(
1399float a;
1400packed_float3 b;)";
1401 auto [output, _, error] = process_test_storage(input, Language::BSL);
1402 EXPECT_EQ(error, "Implicit 12 bytes padding before 'b', add manual padding");
1403 }
1404 {
1405 string input = R"(
1406float a;
1407float4 b;)";
1408 auto [output, _, error] = process_test_storage(input, Language::BSL);
1409 EXPECT_EQ(error, "Implicit 12 bytes padding before 'b', add manual padding");
1410 }
1411 {
1412 string input = R"(
1413float a;
1414float4x4 b;)";
1415 auto [output, _, error] = process_test_storage(input, Language::BSL);
1416 EXPECT_EQ(error, "Implicit 12 bytes padding before 'b', add manual padding");
1417 }
1418 {
1419 string input = R"(
1420float a;
1421float3x4 b;)";
1422 auto [output, _, error] = process_test_storage(input, Language::BSL);
1423 EXPECT_EQ(error, "Implicit 12 bytes padding before 'b', add manual padding");
1424 }
1425 {
1426 string input = R"(
1427float A;
1428float V;
1429float C;
1430float D;
1431float4 a;
1432packed_float3 b;
1433float c;
1434float S;
1435float R;
1436float2 d;
1437float e;
1438float f;
1439float2 g;)";
1440 auto [output, _, error] = process_test_storage(input, Language::BSL);
1441 EXPECT_EQ(error, "");
1442 }
1443
1444 {
1445 /* std140 arrays force 16-byte alignment. */
1446 string input = R"(
1447 float a;
1448 float b[2];
1449 float c;
1450 )";
1451 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1452 EXPECT_EQ(error,
1453 "Implicit padding of 12 bytes per array element, use a 16 bytes type or use a "
1454 "storage buffer");
1455 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1456 EXPECT_EQ(error_ssbo, "Implicit 12 bytes padding before 'b', add manual padding");
1457 }
1458 {
1459 /* std140 arrays force 16-byte alignment. */
1460 string input = R"(
1461 float2 a;
1462 float2 b[2];
1463 float2 c;
1464 )";
1465 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1466 EXPECT_EQ(error,
1467 "Implicit padding of 8 bytes per array element, use a 16 bytes type or use a "
1468 "storage buffer");
1469 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1470 EXPECT_EQ(error_ssbo, "Implicit 8 bytes padding before 'b', add manual padding");
1471 }
1472 {
1473 /* std140 arrays force 16-byte alignment. */
1474 string input = R"(
1475 float b[2];
1476 float a;
1477 float c;
1478 float d;
1479 )";
1480 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1481 EXPECT_EQ(error,
1482 "Implicit padding of 12 bytes per array element, use a 16 bytes type or use a "
1483 "storage buffer");
1484 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1485 EXPECT_EQ(error_ssbo, "");
1486 }
1487 {
1488 /* std140 arrays force 16-byte alignment. */
1489 string input = R"(
1490 float2 b[2];
1491 float2 a;
1492 float2 c;
1493 )";
1494 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1495 EXPECT_EQ(error,
1496 "Implicit padding of 8 bytes per array element, use a 16 bytes type or use a "
1497 "storage buffer");
1498 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1499 EXPECT_EQ(error_ssbo, "");
1500 }
1501 {
1502 /* Unknown size. */
1503 string input = R"(
1504 float b[SOME_CONST];
1505 )";
1506 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1507 EXPECT_EQ(error, "Array size is not a constant expression");
1508 }
1509 {
1510 /* Tiny struct. */
1511 string input = R"(
1512 float a;
1513 )";
1514 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1515 EXPECT_EQ(error, "Implicit 12 bytes padding at the end of 'S', add manual padding");
1516 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1517 EXPECT_EQ(error_ssbo, "");
1518 }
1519 {
1520 /* Tiny struct. */
1521 string input = R"(
1522 float2 a;
1523 )";
1524 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1525 EXPECT_EQ(error, "Implicit 8 bytes padding at the end of 'S', add manual padding");
1526 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1527 EXPECT_EQ(error_ssbo, "");
1528 }
1529 {
1530 string input = R"(
1531 packed_float3 a;
1532 float2 b;
1533 )";
1534 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1535 EXPECT_EQ(error, "Implicit 4 bytes padding before 'b', add manual padding");
1536 }
1537 {
1538 string input = R"(
1539 float2 a;
1540 float b;
1541 )";
1542 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1543 EXPECT_EQ(error, "Implicit 4 bytes padding at the end of 'S', add manual padding");
1544 }
1545 {
1546 string input = R"(
1547 packed_float3 a;
1548 )";
1549 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1550 EXPECT_EQ(error, "Implicit 4 bytes padding at the end of 'S', add manual padding");
1551 }
1552 {
1553 string input = R"(
1554 float a;
1555 float b;
1556 float c;
1557 )";
1558 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1559 EXPECT_EQ(error, "Implicit 4 bytes padding at the end of 'S', add manual padding");
1560 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1561 EXPECT_EQ(error_ssbo, "");
1562 }
1563 {
1564 string input = R"(
1565struct T { uint a; };
1566T a;
1567T b[2];
1568)";
1569 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1570 EXPECT_EQ(error, "Implicit 12 bytes padding at the end of 'T', add manual padding");
1571 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1572 EXPECT_EQ(error_ssbo, "Implicit 12 bytes padding before 'b', add manual padding");
1573 }
1574 {
1575 string input = R"(
1576struct T { float2 a; };
1577float a;
1578T b;
1579)";
1580 auto [output, _, error] = process_test_uniform(input, Language::BSL);
1581 EXPECT_EQ(error, "Implicit 8 bytes padding at the end of 'T', add manual padding");
1582 auto [output_ssbo, m, error_ssbo] = process_test_storage(input, Language::BSL);
1583 EXPECT_EQ(error_ssbo, "Implicit 12 bytes padding before 'b', add manual padding");
1584 }
1585}
1586
1587} // namespace blender::gpu::tests
#define output
#define str(s)
#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)
@ Enum
Definition types.hh:106
@ Union
Definition types.hh:109