Blender V5.3
shader_preprocess_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "expression.hh"
9
10namespace blender::gpu::tests {
11
12TEST(shader_tool, Array)
13{
14 using namespace shader;
15 using namespace std;
16 {
17 string input = R"(
18float a[] = {0, 1};
19float b[2] = {
20 a[0],
21 a(0, 1),
22};
23float d[] = {a[0], a(0, 1)};
24)";
25 string expect =
26 R"(
27float a[2] = ARRAY_T(float) ARRAY_V( 0, 1 );
28float b[2] = ARRAY_T(float) ARRAY_V(
29 a[0],
30 a(0, 1)
31 );
32float d[2] = ARRAY_T(float) ARRAY_V( a[0], a(0, 1) );
33)";
34 auto [output, _, error] = process_test_string(input);
35 EXPECT_EQ(expect, output);
36 EXPECT_EQ(error, "");
37 }
38 {
39 string input = R"(
40float c[] = {};
41)";
42 auto [output, _, error] = process_test_string(input);
43 EXPECT_EQ(error, "Array size must be greater than zero.");
44 }
45 {
46 string input = R"(
47float c[0] = {};
48)";
49 auto [output, _, error] = process_test_string(input);
50 EXPECT_EQ(error, "Array size must be greater than zero.");
51 }
52 {
53 string input = R"(
54float2 c[2] = {{0, 1}, {0, 1}};
55)";
56 auto [output, _, error] = process_test_string(input);
57 EXPECT_EQ(error, "Nested initializer list is not supported.");
58 }
59}
60
61TEST(shader_tool, CommaDeclaration)
62{
63 using namespace shader;
64 using namespace std;
65 {
66 string input = R"(
67struct A {
68 int a, b;
69};
70)";
71 string expect =
72 R"(
73struct A {
74 int a;int b;
75};
76#line 2
77 A A_ctor_() {A r;r.a=0;r.b=0;return r;}
78#line 5
79)";
80 auto [output, _, error] = process_test_string(input);
81 EXPECT_EQ(expect, output);
82 EXPECT_EQ(error, "");
83 }
84}
85
86TEST(shader_tool, Include)
87{
88 using namespace shader;
89 using namespace std;
90 {
91 string input = R"(
92#include "a.hh"
93#include "b.glsl"
94#if 0
95# include "c.hh"
96#else
97# include "d.hh"
98#endif
99#if !defined(GPU_SHADER)
100# include "e.hh"
101#endif
102)";
103 string expect =
104 R"(static void test(GPUSource &source, GPUFunctionDictionary *g_functions, GPUPrintFormatMap *g_formats) {
105 source.add_dependency("a.hh");
106 source.add_dependency("b.glsl");
107 source.add_dependency("d.hh");
108 UNUSED_VARS(source, g_functions, g_formats);
109}
110)";
111 auto [output, metadata, error] = process_test_string(input);
112 EXPECT_EQ(expect, metadata.serialize("test"));
113 EXPECT_EQ(error, "");
114 }
115}
116
117TEST(shader_tool, Union)
118{
119 using namespace shader;
120 using namespace std;
121 {
122 string input = R"(
123struct [[host_shared]] T {
124 union {
125 union_t<uint4> a;
126 union_t<int4> b;
127 union_t<float4> c;
128 };
129};
130)";
131 string expect =
132 R"(
133#line 3
134
135#define T_union0_host_shared_ T_union0
136#define T_union0_host_shared_uniform_ T_union0
137#line 3
138struct T_union0 {
139 float4 data0;
140
141};
142#line 3
143 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
144#line 2
145
146#define T_host_shared_ T
147#define T_host_shared_uniform_ T
148#line 2
149struct T {
150 T_union0 union0;
151#line 38
152};
153
154#ifndef GPU_METAL
155T T_ctor_();
156uint4 _a(const T this_);
157void _a_set_(_ref(T ,this_), uint4 value);
158int4 _b(const T this_);
159void _b_set_(_ref(T ,this_), int4 value);
160float4 _c(const T this_);
161void _c_set_(_ref(T ,this_), float4 value);
162#endif
163#line 2
164 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
165#line 9
166uint4 _a(const T this_) {
167 uint4 val;
168 val = floatBitsToUint(this_.union0.data0);
169 return val;
170}
171#line 15
172void _a_set_(_ref(T ,this_), uint4 value) {
173 this_.union0.data0 = uintBitsToFloat(value);
174}
175#line 19
176int4 _b(const T this_) {
177 int4 val;
178 val = floatBitsToInt(this_.union0.data0);
179 return val;
180}
181#line 25
182void _b_set_(_ref(T ,this_), int4 value) {
183 this_.union0.data0 = intBitsToFloat(value);
184}
185#line 29
186float4 _c(const T this_) {
187 float4 val;
188 val = this_.union0.data0;
189 return val;
190}
191#line 35
192void _c_set_(_ref(T ,this_), float4 value) {
193 this_.union0.data0 = value;
194}
195#line 39
196)";
197 auto [output, _, error] = process_test_string(input);
198 EXPECT_EQ(output, expect);
199 EXPECT_EQ(error, "");
200 }
201 {
202 string input = R"(
203struct [[host_shared]] T {
204 float2 foo;
205 float2 bar;
206 union {
207 union_t<uint4> a;
208 };
209 union {
210 union_t<uint4> b;
211 };
212};
213)";
214 string expect =
215 R"(
216#line 5
217
218#define T_union0_host_shared_ T_union0
219#define T_union0_host_shared_uniform_ T_union0
220#line 5
221struct T_union0 {
222 float4 data0;
223
224};
225#line 5
226 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
227#line 8
228
229#define T_union1_host_shared_ T_union1
230#define T_union1_host_shared_uniform_ T_union1
231#line 8
232struct T_union1 {
233 float4 data0;
234
235};
236#line 8
237 T_union1 T_union1_ctor_() {T_union1 r;r.data0=float4(0);return r;}
238#line 2
239
240#define T_host_shared_ T
241#define T_host_shared_uniform_ T
242#line 2
243struct T {
244 float2 foo;
245 float2 bar;
246 T_union0 union0;
247#line 8
248 T_union1 union1;
249#line 31
250};
251
252#ifndef GPU_METAL
253T T_ctor_();
254uint4 _a(const T this_);
255void _a_set_(_ref(T ,this_), uint4 value);
256uint4 _b(const T this_);
257void _b_set_(_ref(T ,this_), uint4 value);
258#endif
259#line 2
260 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;}
261#line 12
262uint4 _a(const T this_) {
263 uint4 val;
264 val = floatBitsToUint(this_.union0.data0);
265 return val;
266}
267#line 18
268void _a_set_(_ref(T ,this_), uint4 value) {
269 this_.union0.data0 = uintBitsToFloat(value);
270}
271#line 22
272uint4 _b(const T this_) {
273 uint4 val;
274 val = floatBitsToUint(this_.union1.data0);
275 return val;
276}
277#line 28
278void _b_set_(_ref(T ,this_), uint4 value) {
279 this_.union1.data0 = uintBitsToFloat(value);
280}
281#line 32
282)";
283 auto [output, _, error] = process_test_string(input);
284 EXPECT_EQ(output, expect);
285 EXPECT_EQ(error, "");
286 }
287 {
288 string input = R"(
289struct [[host_shared]] B {
290 packed_float3 a;
291 float b;
292};
293
294struct [[host_shared]] A {
295 struct B e;
296};
297
298struct [[host_shared]] T {
299 union {
300 union_t<A> a;
301 };
302};
303)";
304 string expect = R"(
305
306#define B_host_shared_ B
307#define B_host_shared_uniform_ B
308#line 2
309struct B {
310 packed_float3 a;
311 float b;
312};
313#line 2
314 B B_ctor_() {B r;r.a=packed_float3(0);r.b=0.0f;return r;}
315#line 8
316#define A_host_shared_ A
317#define A_host_shared_uniform_ A
318#line 7
319struct A {
320 B e;
321};
322#line 7
323 A A_ctor_() {A r;r.e=B_ctor_();return r;}
324#line 12
325
326#define T_union0_host_shared_ T_union0
327#define T_union0_host_shared_uniform_ T_union0
328#line 12
329struct T_union0 {
330 float4 data0;
331
332};
333#line 12
334 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
335#line 11
336
337#define T_host_shared_ T
338#define T_host_shared_uniform_ T
339#line 11
340struct T {
341 T_union0 union0;
342#line 27
343};
344
345#ifndef GPU_METAL
346T T_ctor_();
347A _a(const T this_);
348void _a_set_(_ref(T ,this_), A value);
349#endif
350#line 11
351 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
352#line 16
353A _a(const T this_) {
354 A val;
355 val.e.a = this_.union0.data0.xyz;
356 val.e.b = this_.union0.data0.w;
357 return val;
358}
359#line 23
360void _a_set_(_ref(T ,this_), A value) {
361 this_.union0.data0.xyz = value.e.a;
362 this_.union0.data0.w = value.e.b;
363}
364#line 28
365)";
366 auto [output, _, error] = process_test_string(input);
367 EXPECT_EQ(output, expect);
368 EXPECT_EQ(error, "");
369 }
370 {
371 string input = R"(
372struct [[host_shared]] T {
373 union {
374 union_t<float4x4> a;
375 };
376};
377)";
378 string expect = R"(
379#line 3
380
381#define T_union0_host_shared_ T_union0
382#define T_union0_host_shared_uniform_ T_union0
383#line 3
384struct T_union0 {
385 float4 data0;
386 float4 data1;
387 float4 data2;
388 float4 data3;
389
390};
391#line 3
392 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;}
393#line 2
394
395#define T_host_shared_ T
396#define T_host_shared_uniform_ T
397#line 2
398struct T {
399 T_union0 union0;
400#line 22
401};
402
403#ifndef GPU_METAL
404T T_ctor_();
405float4x4 _a(const T this_);
406void _a_set_(_ref(T ,this_), float4x4 value);
407#endif
408#line 2
409 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
410#line 7
411float4x4 _a(const T this_) {
412 float4x4 val;
413 val[0] = this_.union0.data0;
414 val[1] = this_.union0.data1;
415 val[2] = this_.union0.data2;
416 val[3] = this_.union0.data3;
417 return val;
418}
419#line 16
420void _a_set_(_ref(T ,this_), float4x4 value) {
421 this_.union0.data0 = value[0];
422 this_.union0.data1 = value[1];
423 this_.union0.data2 = value[2];
424 this_.union0.data3 = value[3];
425}
426#line 23
427)";
428 auto [output, _, error] = process_test_string(input);
429 EXPECT_EQ(output, expect);
430 EXPECT_EQ(error, "");
431 }
432 {
433 string input = R"(
434struct [[host_shared]] T {
435 union {
436 uint a;
437 };
438};
439)";
440 auto [output, _, error] = process_test_string(input);
441 EXPECT_EQ(error,
442 "All union members must have their type wrapped using the union_t<T> template.");
443 }
444}
445
446TEST(shader_tool, Unroll)
447{
448 using namespace shader;
449 using namespace std;
450
451 {
452 string input = R"(
453for (int i = 2; i < 4; i++) [[unroll]] { content += i; })";
454 string expect = R"(
455
456{
457#line 2
458 { content += 2; }
459#line 2
460 { content += 3; }
461#line 2
462 })";
463 auto [output, _, error] = process_test_local(input);
464 EXPECT_EQ(output, expect);
465 EXPECT_EQ(error, "");
466 }
467 {
468 string input = R"(
469for (int i = 2; i < 4; i++, y++) [[unroll]] { content += i; })";
470 string expect = R"(
471 {int i = 2;
472#line 2
473 { content += i; }
474#line 2
475 i++, y++;
476#line 2
477 { content += i; }
478#line 2
479 i++, y++;
480#line 2
481 })";
482 auto [output, _, error] = process_test_local(input);
483 EXPECT_EQ(output, expect);
484 EXPECT_EQ(error, "");
485 }
486 {
487 string input = R"(
488for (int i = 2; i < 4 && i < y; i++, y++) [[unroll]] { cont += i; })";
489 string expect = R"(
490 {int i = 2;
491#line 2
492 if(i < 4 && i < y)
493#line 2
494 { cont += i; }
495#line 2
496 i++, y++;
497#line 2
498 if(i < 4 && i < y)
499#line 2
500 { cont += i; }
501#line 2
502 i++, y++;
503#line 2
504 })";
505 auto [output, _, error] = process_test_local(input);
506 EXPECT_EQ(output, expect);
507 EXPECT_EQ(error, "");
508 }
509 {
510 string input = R"(
511for (; i < j;) [[unroll_n(2)]] { content += i; })";
512 string expect = R"(
513
514{
515#line 2
516 if(i < j)
517#line 2
518 { content += i; }
519#line 2
520 if(i < j)
521#line 2
522 { content += i; }
523#line 2
524 })";
525 auto [output, _, error] = process_test_local(input);
526 EXPECT_EQ(output, expect);
527 EXPECT_EQ(error, "");
528 }
529 {
530 string input = R"(
531for (; i < j;) [[unroll_n(2)]] { for (; j < k;) [[unroll_n(2)]] {} })";
532 string expect = R"(
533
534{
535#line 2
536 if(i < j)
537#line 2
538 {
539{
540#line 2
541 if(j < k)
542#line 2
543 {}
544#line 2
545 if(j < k)
546#line 2
547 {}
548#line 2
549 } }
550#line 2
551 if(i < j)
552#line 2
553 {
554{
555#line 2
556 if(j < k)
557#line 2
558 {}
559#line 2
560 if(j < k)
561#line 2
562 {}
563#line 2
564 } }
565#line 2
566 })";
567 auto [output, _, error] = process_test_local(input);
568 EXPECT_EQ(output, expect);
569 EXPECT_EQ(error, "");
570 }
571 {
572 string input = R"(for (; i < j;) [[unroll_n(2)]] { break; })";
573 auto [output, _, error] = process_test_local(input);
574 EXPECT_EQ(error, "Unrolled loop cannot contain \"break\" statement.");
575 }
576 {
577 string input = R"(for (; i < j;) [[unroll_n(2)]] { continue; })";
578 auto [output, _, error] = process_test_local(input);
579 EXPECT_EQ(error, "Unrolled loop cannot contain \"continue\" statement.");
580 }
581 {
582 string input = R"(
583for (; i < j;) [[unroll_n(2)]] { for (; j < k;) {break;continue;} })";
584 string expect = R"(
585
586{
587#line 2
588 if(i < j)
589#line 2
590 { for (; j < k;) {break;continue;} }
591#line 2
592 if(i < j)
593#line 2
594 { for (; j < k;) {break;continue;} }
595#line 2
596 })";
597 auto [output, _, error] = process_test_local(input);
598 EXPECT_EQ(output, expect);
599 EXPECT_EQ(error, "");
600 }
601 {
602 string input = R"(for (int i = 3; i > 2; i++) [[unroll]] {})";
603 auto [output, _, error] = process_test_local(input);
604 EXPECT_EQ(error, "Unsupported condition in unrolled loop.");
605 }
606}
607
608TEST(shader_tool, Template)
609{
610 using namespace shader;
611 using namespace std;
612
613 {
614 string input = R"(
615template<typename T>
616void func() { T::fn(); }
617template void func<A>();
618)";
619 string expect = R"(
620#line 3
621void funcTA() { A_fn(); }
622#line 5
623)";
624 auto [output, _, error] = process_test_string(input);
625 EXPECT_EQ(output, expect);
626 EXPECT_EQ(error, "");
627 }
628 {
629 string input = R"(
630template<typename T>
631void func(T a) {a;}
632template void func<float>(float a);
633template<typename T>
634void foo(T &a) {a;}
635template void foo<float>(float &a);
636
637void f(float a)
638{
639 func(a);
640 foo(a);
641}
642)";
643 string expect = R"(
644#line 3
645void func(float a) {a;}
646#line 6
647void foo(_ref(float ,a)) {a;}
648#line 9
649void f(float a)
650{
651 func(a);
652 foo(a);
653}
654)";
655 auto [output, _, error] = process_test_string(input);
656 EXPECT_EQ(output, expect);
657 EXPECT_EQ(error, "");
658 }
659 {
660 string input = R"(
661template<typename T, int i>
662void func(T a) {
663 a;
664}
665template void func<float, 1>(float a);
666)";
667 string expect = R"(
668#line 3
669void funcTfloatT1(float a) {
670 a;
671}
672#line 7
673)";
674 auto [output, _, error] = process_test_string(input);
675 EXPECT_EQ(output, expect);
676 EXPECT_EQ(error, "");
677 }
678 {
679 string input = R"(
680template<int i, uint j, int k> E func() { return E(i + j + k); }
681template E func<0x1, 2, -1>();
682)";
683 string expect = R"(
684E funcT0x1T2T_1() { return E(0x1 + 2 + -1); }
685#line 4
686)";
687 auto [output, _, error] = process_test_string(input);
688 EXPECT_EQ(output, expect);
689 EXPECT_EQ(error, "");
690 }
691 {
692 string input = R"(
693template<enum E e, char i> E func() { return E(e + i); }
694template E func<v, 2>();
695)";
696 string expect = R"(
697E funcTvT2() { return E(v + 2); }
698#line 4
699)";
700 auto [output, _, error] = process_test_string(input);
701 EXPECT_EQ(output, expect);
702 EXPECT_EQ(error, "");
703 }
704 {
705 string input = R"(
706template<> void func<T, Q>(T a) {a}
707)";
708 string expect = R"(
709 void funcTTTQ(T a) {a}
710)";
711 auto [output, _, error] = process_test_string(input);
712 EXPECT_EQ(output, expect);
713 EXPECT_EQ(error, "");
714 }
715 {
716 string input = R"(
717template<typename T, int i = 0> void func(T a) {a;}
718)";
719 auto [output, _, error] = process_test_string(input);
720 EXPECT_EQ(error, "Default arguments are not supported inside template declaration");
721 }
722 {
723 string input = R"(
724template void func(float a);
725)";
726 auto [output, _, error] = process_test_string(input);
727 EXPECT_EQ(error,
728 "Template instantiation and specialization require explicit template arguments");
729 }
730 {
731 string input = R"(
732template A<f> fn(A<f> a);
733)";
734 auto [output, _, error] = process_test_string(input);
735 EXPECT_EQ(error,
736 "Template instantiation and specialization require explicit template arguments");
737 }
738 {
739 string input = R"(
740template<> A fn(A a) {}
741)";
742 auto [output, _, error] = process_test_string(input);
743 EXPECT_EQ(error,
744 "Template instantiation and specialization require explicit template arguments");
745 }
746 {
747 string input = R"(
748template<> A<f> fn(A<f> a) {}
749)";
750 auto [output, _, error] = process_test_string(input);
751 EXPECT_EQ(error,
752 "Template instantiation and specialization require explicit template arguments");
753 }
754 {
755 string input = R"(func<float, 1>(a);)";
756 string expect = R"(funcTfloatT1(a);)";
757 auto [output, _, error] = process_test_local(input);
758 EXPECT_EQ(output, expect);
759 EXPECT_EQ(error, "");
760 }
762 string input = R"(a.template func<float, 1>(a);)";
763 string expect = R"(_funcTfloatT1(a, a);)";
764 auto [output, _, error] = process_test_local(input);
765 EXPECT_EQ(output, expect);
766 EXPECT_EQ(error, "");
767 }
768 {
769 string input = R"(this->template func<float, 1>(a);)";
770 string expect = R"(_funcTfloatT1(this_, a);)";
771 auto [output, _, error] = process_test_local(input);
772 EXPECT_EQ(output, expect);
773 EXPECT_EQ(error, "");
774 }
776 string input = R"(A<B<1, 2>, C<1, D<T, -1>>> a;)";
777 string expect = R"(ATBT1T2TCT1TDTTT_1 a;)";
778 auto [output, _, error] = process_test_local(input);
779 EXPECT_EQ(output, expect);
780 EXPECT_EQ(error, "");
781 }
782}
783
784TEST(shader_tool, TemplateStruct)
785{
786 using namespace shader;
787 using namespace std;
788
789 {
790 string input = R"(
791template<typename T>
792struct A {
793 T a;
794 A method(T b) const
795 {
796 return A<T>{b};
797 }
798};
799template struct A<float>;
800)";
801 string expect = R"(
802#line 3
803struct ATfloat {
804 float a;
805#line 9
806};
807#line 12
808#ifndef GPU_METAL
809ATfloat ATfloat_ctor_();
810ATfloat _method(const ATfloat this_, float b);
811#endif
812#line 3
813 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
814#line 5
815 ATfloat _method(const ATfloat this_, float b)
816 {
817 return _ctor(ATfloat) b _rotc() ;
818 }
819#line 11
820)";
821 auto [output, _, error] = process_test_string(input);
822 EXPECT_EQ(output, expect);
823 EXPECT_EQ(error, "");
824 }
825 {
826 string input = R"(
827template<typename T>
828struct A { T a; };
829template struct A<float>;
830)";
831 string expect = R"(
832#line 3
833struct ATfloat { float a; };
834#line 3
835 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
836#line 5
837)";
838 auto [output, _, error] = process_test_string(input);
839 EXPECT_EQ(output, expect);
840 EXPECT_EQ(error, "");
841 }
842 {
843 string input = R"(
844template<> struct A<float>{
845 float a;
846};
847)";
848 string expect = R"(
849 struct ATfloat{
850 float a;
851};
852#line 2
853 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
854#line 5
855)";
856 auto [output, _, error] = process_test_string(input);
857 EXPECT_EQ(output, expect);
858 EXPECT_EQ(error, "");
859 }
860 {
861 string input = R"(
862void func(A<float> a) {}
863)";
864 string expect = R"(
865void func(ATfloat a) {}
866)";
867 auto [output, _, error] = process_test_string(input);
868 EXPECT_EQ(output, expect);
869 EXPECT_EQ(error, "");
870 }
871 {
872 /* Struct templated methods. */
873 string input = R"(
874namespace N {
875
876template<typename B> struct A {
877 B i;
878 template<typename T> static void fn1(T a) {}
879 template<typename T> static T fn2() { return T(0); }
880 template<typename T> void fn3(T a) { i += int(fn4<T>()); }
881 template<typename T> T fn4() { fn3(0); return T(0); }
882};
883
884template struct A<int>;
885
886template void A<int>::fn1<int>(int);
887template int A<int>::fn2<int>();
888template void A<int>::fn3<int>(int);
889template int A<int>::fn4<int>();
890
891void fn(A<int> a)
892{
893 A<int>::fn1(0);
894 A<int>::fn2<int>();
895 a.fn3(0);
896 a.fn4<int>();
897}
898
899}
900)";
901 string expect = R"(
902#line 4
903struct N_ATint {
904 int i;
905#line 10
906};
907#line 14
908#ifndef GPU_METAL
909N_ATint N_ATint_ctor_();
910void N_ATint_fn1(int a);
911int N_ATint_fn2Tint();
912void _fn3(_ref(N_ATint ,this_), int a);
913int _fn4Tint(_ref(N_ATint ,this_));
914#endif
915#line 4
916 N_ATint N_ATint_ctor_() {N_ATint r;r.i=0;return r;}
917#line 6
918 void N_ATint_fn1(int a) {}
919 int N_ATint_fn2Tint() { return int(0); }
920void _fn3(_ref(N_ATint ,this_), int a) { this_.i += int(_fn4Tint(this_)); }
921int _fn4Tint(_ref(N_ATint ,this_)) { _fn3(this_, 0); return int(0); }
922#line 19
923void N_fn(N_ATint a)
924{
925 N_ATint_fn1(0);
926 N_ATint_fn2Tint();
927 _fn3(a, 0);
928 _fn4Tint(a);
929}
930
931
932)";
933 auto [output, _, error] = process_test_string(input);
934 EXPECT_EQ(output, expect);
935 EXPECT_EQ(error, "");
936 }
937}
938
939TEST(shader_tool, Reference)
940{
941 using namespace shader;
942 using namespace std;
943
944 {
945 string input = R"(void func() { auto &a = b; a.a = 0; c = a(a); a_c_a = a; })";
946 string expect = R"(void func() { b.a = 0; c = a(b); a_c_a = b; })";
947 auto [output, _, error] = process_test_string(input);
948 EXPECT_EQ(output, expect);
949 EXPECT_EQ(error, "");
950 }
951 {
952 string input = R"(void func() { const int &a = b; a.a = 0; c = a(a); })";
953 string expect = R"(void func() { b.a = 0; c = a(b); })";
954 auto [output, _, error] = process_test_string(input);
955 EXPECT_EQ(output, expect);
956 EXPECT_EQ(error, "");
957 }
958 {
959 string input = R"(void func() { const int i = 0; auto &a = b[i]; a.a = 0; })";
960 string expect = R"(void func() { const int i = 0; b[i].a = 0; })";
961 auto [output, _, error] = process_test_string(input);
962 EXPECT_EQ(output, expect);
963 EXPECT_EQ(error, "");
964 }
965 {
966 string input = R"(void func() { auto &a = b(0); })";
967 auto [output, _, error] = process_test_string(input);
968 EXPECT_EQ(error, "Reference definitions cannot contain function calls.");
969 }
970 {
971 string input = R"(void func() { int i = 0; auto &a = b[i++]; })";
972 auto [output, _, error] = process_test_string(input);
973 EXPECT_EQ(error, "Reference definitions cannot have side effects.");
974 }
975 {
976 string input = R"(void func() { auto &a = b[0 + 1]; })";
977 auto [output, _, error] = process_test_string(input);
978 EXPECT_EQ(error,
979 "Array subscript inside reference declaration must be a single variable or a "
980 "constant, not an expression.");
981 }
982 {
983 string input = R"(void func() { auto &a = b[c]; })";
984 auto [output, _, error] = process_test_string(input);
985 EXPECT_EQ(error,
986 "Cannot locate array subscript variable declaration. "
987 "If it is a global variable, assign it to a temporary const variable for "
988 "indexing inside the reference.");
989 }
990 {
991 string input = R"(void func() { int c = 0; auto &a = b[c]; })";
992 auto [output, _, error] = process_test_string(input);
993 EXPECT_EQ(error, "Array subscript variable must be declared as const qualified.");
994 }
995 {
996 string input = R"(auto &a = b;)";
997 auto [output, _, error] = process_test_string(input);
998 EXPECT_EQ(error, "Unexpected token \"&\": Expecting declaration");
999 }
1000}
1001
1002TEST(shader_tool, Cleanup)
1003{
1004 using namespace shader;
1005 using namespace std;
1006
1007 {
1008 string input = R"(
1009#line 2
1010int b = 0;
1011
1012#if 0
1013
1014int a = 1;
1015#elif 1
1016#line 321
1017#line 321
1018int a = 0;
1019#endif
1020)";
1021 string expect = R"(
1022int b = 0;
1023
1024#if 0
1025#elif 1
1026#line 321
1027int a = 0;
1028#endif
1029)";
1030 auto [output, _, error] = process_test_string(input);
1031 EXPECT_EQ(output, expect);
1032 EXPECT_EQ(error, "");
1033 }
1034}
1035
1036TEST(shader_tool, DefaultArguments)
1037{
1038 using namespace shader;
1039 using namespace std;
1040
1041 {
1042 string input = R"(
1043int func(int a, int b = 0)
1044{
1045 return a + b;
1046}
1047)";
1048 string expect = R"(
1049int func(int a, int b )
1050{
1051 return a + b;
1052}
1053#line 2
1054int func(int a)
1055{
1056#line 2
1057 return func(a, 0);
1058}
1059#line 6
1060)";
1061 auto [output, _, error] = process_test_string(input);
1062 EXPECT_EQ(output, expect);
1063 EXPECT_EQ(error, "");
1064 }
1065 {
1066 string input = R"(
1067int func(int a = 0, const int b = 0)
1068{
1069 return a + b;
1070}
1071)";
1072 string expect = R"(
1073int func(int a , const int b )
1074{
1075 return a + b;
1076}
1077#line 2
1078int func(int a)
1079{
1080#line 2
1081 return func(a, 0);
1082}
1083#line 2
1084int func()
1085{
1086#line 2
1087 return func(0);
1088}
1089#line 6
1090)";
1091 auto [output, _, error] = process_test_string(input);
1092 EXPECT_EQ(output, expect);
1093 EXPECT_EQ(error, "");
1094 }
1095 {
1096 string input = R"(
1097int2 func(int2 a = int2(0, 0)) {
1098 return a;
1099}
1100)";
1101 string expect = R"(
1102int2 func(int2 a ) {
1103 return a;
1104}
1105#line 2
1106int2 func()
1107{
1108#line 2
1109 return func(int2(0, 0));
1110}
1111#line 5
1112)";
1113 auto [output, _, error] = process_test_string(input);
1114 EXPECT_EQ(output, expect);
1115 EXPECT_EQ(error, "");
1116 }
1117 {
1118 string input = R"(
1119void func(int a = 0) {
1120 a;
1121}
1122)";
1123 string expect = R"(
1124void func(int a ) {
1125 a;
1126}
1127#line 2
1128void func()
1129{
1130#line 2
1131 func(0);
1132}
1133#line 5
1134)";
1135 auto [output, _, error] = process_test_string(input);
1136 EXPECT_EQ(output, expect);
1137 EXPECT_EQ(error, "");
1138 }
1139}
1140
1141TEST(shader_tool, SrtTemplateWrapper)
1142{
1143 using namespace shader;
1144 using namespace std;
1145
1146 {
1147 string input = R"(
1148struct SRT {
1149 [[resource_table]] srt_t<T> a;
1150};
1151)";
1152 string expect = R"(
1153#define access_SRT_a() T_ctor_()
1154#ifdef CREATE_INFO_RES_PASS_SRT
1155CREATE_INFO_RES_PASS_SRT
1156#endif
1157#ifdef CREATE_INFO_RES_BATCH_SRT
1158CREATE_INFO_RES_BATCH_SRT
1159#endif
1160#ifdef CREATE_INFO_RES_GEOMETRY_SRT
1161CREATE_INFO_RES_GEOMETRY_SRT
1162#endif
1163#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
1164CREATE_INFO_RES_SHARED_VARS_SRT
1165#endif
1166#line 2
1167struct SRT {
1168 T a;
1169#line 12
1170};
1171
1172#ifndef GPU_METAL
1173SRT SRT_ctor_();
1174SRT SRT_new_();
1175#endif
1176#line 2
1177 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
1178#line 5
1179 SRT SRT_new_()
1180{
1181 SRT result;
1182 result.a = T_new_();
1183 return result;
1184#line 3
1185}
1186#line 5
1187)";
1188 auto [output, _, error] = process_test_string(input);
1189 EXPECT_EQ(output, expect);
1190 EXPECT_EQ(error, "");
1191 }
1192 {
1193 string input = R"(
1194struct SRT {
1195 [[resource_table]] T a;
1196};
1197)";
1198 auto [output, _, error] = process_test_string(input);
1199 EXPECT_EQ(error,
1200 "Members declared with the [[resource_table]] attribute must wrap their type "
1201 "with the srt_t<T> template.");
1202 }
1203 {
1204 string input = R"(
1205struct SRT {
1206 srt_t<T> a;
1207};
1208)";
1209 auto [output, _, error] = process_test_string(input);
1210 EXPECT_EQ(error,
1211 "The srt_t<T> template is only to be used with members declared with the "
1212 "[[resource_table]] attribute.");
1213 }
1214 {
1215 string input = R"(
1216struct SRT {
1217 [[resource_table]] srt_t<T> a[4];
1218};
1219)";
1220 auto [output, _, error] = process_test_string(input);
1221 EXPECT_EQ(error, "[[resource_table]] members cannot be arrays.");
1222 }
1223}
1224
1225TEST(shader_tool, SrtMethod)
1226{
1227 using namespace shader;
1228 using namespace std;
1229
1230 {
1231 string input = R"(
1232struct SRT {
1233 [[resource_table]] srt_t<T> a;
1234
1235 void method(int t) {
1236 this->a;
1237 }
1238};
1239)";
1240 string expect = R"(
1241#define access_SRT_a() T_ctor_()
1242#ifdef CREATE_INFO_RES_PASS_SRT
1243CREATE_INFO_RES_PASS_SRT
1244#endif
1245#ifdef CREATE_INFO_RES_BATCH_SRT
1246CREATE_INFO_RES_BATCH_SRT
1247#endif
1248#ifdef CREATE_INFO_RES_GEOMETRY_SRT
1249CREATE_INFO_RES_GEOMETRY_SRT
1250#endif
1251#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
1252CREATE_INFO_RES_SHARED_VARS_SRT
1253#endif
1254#line 2
1255struct SRT {
1256 T a;
1257#line 16
1258};
1259
1260#ifndef GPU_METAL
1261SRT SRT_ctor_();
1262void _method(SRT this_, int t);
1263SRT SRT_new_();
1264#endif
1265#line 2
1266 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
1267#line 5
1268
1269#if defined(CREATE_INFO_SRT)
1270#line 5
1271 void _method(SRT this_, int t) {
1272 srt_access(SRT, a);
1273 }
1274#endif
1275 SRT SRT_new_()
1276{
1277 SRT result;
1278 result.a = T_new_();
1279 return result;
1280#line 7
1281}
1282#line 9
1283)";
1284 auto [output, _, error] = process_test_string(input);
1285 EXPECT_EQ(output, expect);
1286 EXPECT_EQ(error, "");
1287 }
1288}
1289
1290TEST(shader_tool, StaticBranch)
1291{
1292 using namespace shader;
1293 using namespace std;
1294
1295 {
1296 string input = R"(
1297struct Resources {
1298 [[compilation_constant]] const int use_color_band;
1299
1300 void fn() {
1301 if (use_color_band) [[static_branch]] {
1302 test;
1303 }
1304 }
1305};
1306
1307void func([[resource_table]] Resources &srt)
1308{
1309 if (srt.use_color_band) [[static_branch]] {
1310 test;
1311 }
1312
1313 if (srt.use_color_band == 1) [[static_branch]] {
1314 test;
1315 } else {
1316 test;
1317 }
1318
1319 if (srt.use_color_band) [[static_branch]] {
1320 test;
1321 } else if (srt.use_color_band) [[static_branch]] {
1322 test;
1323 }
1324
1325 if (srt.use_color_band) [[static_branch]] {
1326 test;
1327 } else if (srt.use_color_band) [[static_branch]] {
1328 test;
1329 } else {
1330 test;
1331 }
1332
1333 if (srt.use_color_band) [[static_branch]] {
1334 if (srt.use_color_band) [[static_branch]] {
1335 test;
1336 }
1337 }
1338}
1339)";
1340 string expect = R"(
1341#define access_Resources_use_color_band() use_color_band
1342#ifdef CREATE_INFO_RES_PASS_Resources
1343CREATE_INFO_RES_PASS_Resources
1344#endif
1345#ifdef CREATE_INFO_RES_BATCH_Resources
1346CREATE_INFO_RES_BATCH_Resources
1347#endif
1348#ifdef CREATE_INFO_RES_GEOMETRY_Resources
1349CREATE_INFO_RES_GEOMETRY_Resources
1350#endif
1351#ifdef CREATE_INFO_RES_SHARED_VARS_Resources
1352CREATE_INFO_RES_SHARED_VARS_Resources
1353#endif
1354#line 2
1355struct Resources {
1356#line 18
1357int _pad;};
1358#line 21
1359#ifndef GPU_METAL
1360Resources Resources_ctor_();
1361void _fn(Resources this_);
1362Resources Resources_new_();
1363#endif
1364#line 2
1365 Resources Resources_ctor_() {Resources r;r._pad=0;return r;}
1366#line 5
1367
1368#if defined(CREATE_INFO_Resources)
1369#line 5
1370 void _fn(Resources this_) {
1371
1372#if SRT_CONSTANT_use_color_band
1373#line 6
1374 {
1375 test;
1376 }
1377
1378#endif
1379#line 9
1380 }
1381#endif
1382 Resources Resources_new_()
1383{
1384 Resources result;
1385 result._pad = 0;
1386 return result;
1387#line 9
1388}
1389#line 12
1390
1391#if defined(CREATE_INFO_Resources)
1392#line 12
1393void func(Resources srt)
1394{
1395
1396#if SRT_CONSTANT_use_color_band
1397#line 14
1398 {
1399 test;
1400 }
1401#endif
1402
1403#if SRT_CONSTANT_use_color_band == 1
1404#line 18
1405 {
1406 test;
1407 }
1408#else
1409#line 20
1410 {
1411 test;
1412 }
1413#endif
1414
1415#if SRT_CONSTANT_use_color_band
1416#line 24
1417 {
1418 test;
1419 }
1420#elif SRT_CONSTANT_use_color_band
1421#line 26
1422 {
1423 test;
1424 }
1425#endif
1426
1427#if SRT_CONSTANT_use_color_band
1428#line 30
1429 {
1430 test;
1431 }
1432#elif SRT_CONSTANT_use_color_band
1433#line 32
1434 {
1435 test;
1436 }
1437#else
1438#line 34
1439 {
1440 test;
1441 }
1442#endif
1443
1444#if SRT_CONSTANT_use_color_band
1445#line 38
1446 {
1447
1448#if SRT_CONSTANT_use_color_band
1449#line 39
1450 {
1451 test;
1452 }
1453
1454#endif
1455#line 42
1456 }
1457
1458#endif
1459#line 43
1460}
1461
1462#endif
1463#line 44
1464)";
1465 auto [output, _, error] = process_test_string(input);
1466 EXPECT_EQ(output, expect);
1467 EXPECT_EQ(error, "");
1468 }
1469 {
1470 string input = R"(
1471void func([[resource_table]] Resources &srt)
1472{
1473 if (srt.use_color_band) [[static_branch]] {
1474 test;
1475 } else if (srt.use_color_band) {
1476 test;
1477 }
1478}
1479)";
1480 auto [output, _, error] = process_test_string(input);
1481 EXPECT_EQ(error, "Expecting next if statement to also be a static branch.");
1482 }
1483 {
1484 string input = R"(
1485void func([[resource_table]] Resources &srt)
1486{
1487 if (use_color_band) [[static_branch]] {
1488 test;
1489 }
1490}
1491)";
1492 auto [output, _, error] = process_test_string(input);
1493 EXPECT_EQ(error,
1494 "Expecting compilation or specialization constant. Make sure SRT arguments "
1495 "have the [[resource_table]] attribute.");
1496 }
1497 {
1498 string input = R"(
1499void func([[resource_table]] Resources &srt)
1500{
1501 if (srt.use_color_band && srt.use_color_band) [[static_branch]] {
1502 test;
1503 }
1504}
1505)";
1506 auto [output, _, error] = process_test_string(input);
1507 EXPECT_EQ(error, "Expecting single condition.");
1508 }
1509}
1510
1511TEST(shader_tool, Namespace)
1512{
1513 using namespace shader;
1514 using namespace std;
1515
1516 {
1517 string input = R"(
1518struct C {};
1519void fn(C b) {}
1520namespace B {
1521struct D {};
1522void fn(D b) {}
1523void fn2()
1524{
1525 fn(C{});
1526}
1527}
1528)";
1529 auto [output, _, error] = process_test_string(input);
1530 EXPECT_EQ(error, "Call to function is ambiguous. Specify namespace to remove ambiguity.");
1531 }
1532 {
1533 string input = R"(
1534namespace A {
1535int func(int a) { return 0; }
1536int func2(int a)
1537{
1538 int func = func();
1539 return func;
1540}
1541}
1542)";
1543 string expect = R"(
1544
1545int A_func(int a) { return 0; }
1546int A_func2(int a)
1547{
1548 int func = A_func();
1549 return func;
1550}
1551
1552)";
1553 auto [output, _, error] = process_test_string(input);
1554 EXPECT_EQ(output, expect);
1555 EXPECT_EQ(error, "");
1556 }
1557 {
1558 string input = R"(
1559namespace A {
1560struct S {};
1561int func(int a)
1562{
1563 S s;
1564 return B::func(int a);
1565}
1566int func2(int a)
1567{
1568 T s;
1569 s.S;
1570 s.func;
1571 return func(int a);
1572}
1573}
1574)";
1575 string expect = R"(
1576
1577struct A_S { int _pad;};
1578#line 3
1579 A_S A_S_ctor_() {A_S r;r._pad=0;return r;}
1580int A_func(int a)
1581{
1582 A_S s;
1583 return B_func(int a);
1584}
1585int A_func2(int a)
1586{
1587 T s;
1588 s.S;
1589 s.func;
1590 return A_func(int a);
1591}
1592
1593)";
1594 auto [output, _, error] = process_test_string(input);
1595 EXPECT_EQ(output, expect);
1596 EXPECT_EQ(error, "");
1597 }
1598 {
1599 string input = R"(
1600namespace A::B {
1601int func(int a)
1602{
1603 return a;
1604}
1605int func2(int a)
1606{
1607 return func(int a);
1608}
1609}
1610)";
1611 string expect = R"(
1612
1613int A_B_func(int a)
1614{
1615 return a;
1616}
1617int A_B_func2(int a)
1618{
1619 return A_B_func(int a);
1620}
1621
1622)";
1623 auto [output, _, error] = process_test_string(input);
1624 EXPECT_EQ(output, expect);
1625 EXPECT_EQ(error, "");
1626 }
1627 {
1628 string input = R"(
1629namespace A {
1630void a() {}
1631namespace B {
1632void b() { a(); }
1633}
1634void f() { B::b(); }
1635}
1636)";
1637 string expect = R"(
1638
1639void A_a() {}
1640
1641void A_B_b() { A_a(); }
1642
1643void A_f() { A_B_b(); }
1644
1645)";
1646 auto [output, _, error] = process_test_string(input);
1647 EXPECT_EQ(output, expect);
1648 EXPECT_EQ(error, "");
1649 }
1650 {
1651 string input = R"(
1652namespace A {
1653int test(int a) {}
1654int func(int a)
1655{
1656 using B::test;
1657 return test(a);
1658}
1659}
1660)";
1661 string expect = R"(
1662
1663int A_test(int a) {}
1664int A_func(int a)
1665{
1666
1667 return B_test(a);
1668}
1669
1670)";
1671 auto [output, _, error] = process_test_string(input);
1672 EXPECT_EQ(output, expect);
1673 EXPECT_EQ(error, "");
1674 }
1675 {
1676 string input = R"(
1677int func(int a)
1678{
1679 using B = A::S;
1680 B b;
1681 using C = A::F;
1682 C f = A::B();
1683 f = B();
1684 B d;
1685}
1686)";
1687 string expect = R"(
1688int func(int a)
1689{
1690
1691 A_S b;
1692
1693 A_F f = A_B();
1694 f = B();
1695 A_S d;
1696}
1697)";
1698 auto [output, _, error] = process_test_string(input);
1699 EXPECT_EQ(output, expect);
1700 EXPECT_EQ(error, "");
1701 }
1702 {
1703 string input = R"(
1704namespace A::B {
1705void func() {}
1706struct S {};
1707}
1708namespace A::B {
1709using A::B::func;
1710using S = A::B::S;
1711void test() {
1712 S s;
1713 func();
1714}
1715}
1716)";
1717 string expect = R"(
1718
1719void A_B_func() {}
1720struct A_B_S { int _pad;};
1721#line 4
1722 A_B_S A_B_S_ctor_() {A_B_S r;r._pad=0;return r;}
1723#line 9
1724void A_B_test() {
1725 A_B_S s;
1726 A_B_func();
1727}
1728
1729)";
1730 auto [output, _, error] = process_test_string(input);
1731 EXPECT_EQ(output, expect);
1732 EXPECT_EQ(error, "");
1733 }
1734 {
1735 string input = R"(
1736using B = A::T;
1737)";
1738 auto [output, _, error] = process_test_string(input);
1739 EXPECT_EQ(error, "The `using` keyword is not allowed in global scope.");
1740 }
1741 {
1742 string input = R"(
1743namespace A {
1744using namespace B;
1745}
1746)";
1747 auto [output, _, error] = process_test_string(input);
1748 EXPECT_EQ(error,
1749 "Unsupported `using namespace`. "
1750 "Add individual `using` directives for each needed symbol.");
1751 }
1752 {
1753 string input = R"(
1754namespace A {
1755using B::func;
1756}
1757)";
1758 auto [output, _, error] = process_test_string(input);
1759 EXPECT_EQ(error,
1760 "The `using` keyword is only allowed in namespace scope to make visible symbols "
1761 "from the same namespace declared in another scope, potentially from another "
1762 "file.");
1763 }
1764 {
1765 string input = R"(
1766namespace A {
1767using C = B::func;
1768}
1769)";
1770 auto [output, _, error] = process_test_string(input);
1771 EXPECT_EQ(error,
1772 "The `using` keyword is only allowed in namespace scope to make visible symbols "
1773 "from the same namespace declared in another scope, potentially from another "
1774 "file.");
1775 }
1776 {
1777 /* Template on the same line as function signature inside a namespace.
1778 * Template instantiation with other functions. */
1779 string input = R"(
1780namespace NS {
1781template<typename T> T read(T a)
1782{
1783 return a;
1784}
1785template float read<float>(float);
1786float write(float a){ return a; }
1787}
1788)";
1789
1790 string expect = R"(
1791#line 3
1792float NS_read(float a)
1793{
1794 return a;
1795}
1796#line 8
1797float NS_write(float a){ return a; }
1798
1799)";
1800 auto [output, _, error] = process_test_string(input);
1801 EXPECT_EQ(output, expect);
1802 EXPECT_EQ(error, "");
1803 }
1804 {
1805 /* Struct with member function inside namespace. */
1806 string input = R"(
1807namespace NS {
1808struct S {
1809 static S static_method(S s) {
1810 return S(0);
1811 }
1812 S other_method(int s) {
1813 this->some_method();
1814 return S(0);
1815 }
1816};
1817} // End of namespace
1818)";
1819
1820 string expect = R"(
1821
1822struct NS_S {
1823#line 11
1824int _pad;};
1825#line 14
1826#ifndef GPU_METAL
1827NS_S NS_S_ctor_();
1828NS_S NS_S_static_method(NS_S s);
1829NS_S _other_method(_ref(NS_S ,this_), int s);
1830#endif
1831#line 3
1832 NS_S NS_S_ctor_() {NS_S r;r._pad=0;return r;}
1833 NS_S NS_S_static_method(NS_S s) {
1834 return NS_S(0);
1835 }
1836 NS_S _other_method(_ref(NS_S ,this_), int s) {
1837 _some_method(this_);
1838 return NS_S(0);
1839 }
1840#line 13
1841)";
1842 auto [output, _, error] = process_test_string(input);
1843 EXPECT_EQ(output, expect);
1844 EXPECT_EQ(error, "");
1845 }
1846 {
1847 /* Template specialization inside namespace. */
1848 string input = R"(
1849namespace NS {
1850template<> Type a<Type>() {}
1851}
1852)";
1853
1854 string expect = R"(
1855
1856 Type NS_aTType() {}
1857
1858)";
1859 auto [output, _, error] = process_test_string(input);
1860 EXPECT_EQ(output, expect);
1861 EXPECT_EQ(error, "");
1862 }
1863 {
1864 /* Half namespace specified identifiers and methods. */
1865 string input = R"(
1866namespace NS {
1867struct B {
1868 int i;
1869 static int D() { return B::C().R(); }
1870 static int E() { return C().R(); }
1871 static B C() { return B(0); }
1872 int R() { return R(); }
1873};
1874B fn() { return B::C(); }
1875}
1876)";
1877
1878 string expect = R"(
1879
1880struct NS_B {
1881 int i;
1882#line 9
1883};
1884
1885#ifndef GPU_METAL
1886NS_B NS_B_ctor_();
1887int NS_B_D();
1888int NS_B_E();
1889NS_B NS_B_C();
1890int _R(_ref(NS_B ,this_));
1891#endif
1892#line 3
1893 NS_B NS_B_ctor_() {NS_B r;r.i=0;return r;}
1894#line 5
1895 int NS_B_D() { return _R(NS_B_C()); }
1896 int NS_B_E() { return _R(NS_B_C()); }
1897 NS_B NS_B_C() { return NS_B(0); }
1898 int _R(_ref(NS_B ,this_)) { return _R(this_); }
1899#line 10
1900NS_B NS_fn() { return NS_B_C(); }
1901
1902)";
1903 auto [output, _, error] = process_test_string(input);
1904 EXPECT_EQ(output, expect);
1905 EXPECT_EQ(error, "");
1906 }
1907}
1908
1909TEST(shader_tool, Swizzle)
1910{
1911 using namespace shader;
1912 using namespace std;
1913
1914 {
1915 string input = R"(a.xyzw().aaa().xxx().grba().yzww; aaaa();)";
1916 string expect = R"(a.xyzw .aaa .xxx .grba .yzww; aaaa();)";
1917 auto [output, _, error] = process_test_local(input);
1918 EXPECT_EQ(output, expect);
1919 EXPECT_EQ(error, "");
1920 }
1921}
1922
1923TEST(shader_tool, BinaryLiterals)
1924{
1925 using namespace shader;
1926 using namespace std;
1927
1928 {
1929 string input = R"(0b1 0b10u 0b10001000100010001000100010001000)";
1930 string expect = R"(1 2u 2290649224)";
1931 auto [output, _, error] = process_test_local(input);
1932 EXPECT_EQ(output, expect);
1933 EXPECT_EQ(error, "");
1934 }
1935}
1936
1937TEST(shader_tool, Enum)
1938{
1939 using namespace shader;
1940 using namespace std;
1941
1942 {
1943 string input = R"(
1944enum class enum_class : int {
1945 VALUE = 0,
1946};
1947)";
1948 string expect = R"(
1949#line 3
1950constant static constexpr int enum_class_VALUE = 0;
1951
1952#define enum_class int
1953#line 2
1954
1955enum_class enum_class_ctor_() { return enum_class(0); }
1956#line 2
1957
1958
1959
1960)";
1961 auto [output, _, error] = process_test_string(input);
1962 EXPECT_EQ(output, expect);
1963 EXPECT_EQ(error, "");
1964 }
1965
1966 {
1967 string input = R"(
1968enum E : int { A, B = 2, C, D = 1, E };
1969)";
1970 string expect = R"(
1971constant static constexpr int A = 0;
1972#line 2
1973constant static constexpr int B = 2;
1974#line 2
1975constant static constexpr int C = B + 1;
1976#line 2
1977constant static constexpr int D = 1;
1978#line 2
1979constant static constexpr int E = D + 1;
1980
1981#define E int
1982#line 2
1983
1984E E_ctor_() { return E(0); }
1985#line 2
1986
1987)";
1988 auto [output, _, error] = process_test_string(input);
1989 EXPECT_EQ(output, expect);
1990 EXPECT_EQ(error, "");
1991 }
1992 {
1993 string input = R"(
1994enum class enum_class : int {
1995 VALUE = 0,
1996};
1997)";
1998 string expect = R"(
1999#line 3
2000constant static constexpr int enum_class_VALUE = 0;
2001
2002#define enum_class int
2003#line 2
2004
2005enum_class enum_class_ctor_() { return enum_class(0); }
2006#line 2
2007
2008
2009
2010)";
2011 auto [output, _, error] = process_test_string(input);
2012 EXPECT_EQ(output, expect);
2013 EXPECT_EQ(error, "");
2014 }
2015 {
2016 string input = R"(
2017enum class enum_class {
2018 VALUE = 0,
2019};
2020)";
2021 auto [output, _, error] = process_test_string(input);
2022 EXPECT_EQ(error, "enum declaration must explicitly use an underlying type");
2023 }
2024}
2025
2026#ifdef __APPLE__ /* This processing is only done for metal compatibility. */
2027TEST(shader_tool, MatrixConstructors)
2028{
2029 using namespace shader;
2030 using namespace std;
2031
2032 {
2033 string input = R"(mat3(a); mat3 a; my_mat4x4(a); mat2x2(a); mat3x2(a);)";
2034 string expect = R"(__mat3x3(a); mat3 a; my_mat4x4(a); __mat2x2(a); mat3x2(a);)";
2035 auto [output, _, error] = process_test_string(input, Language::GLSL);
2036 EXPECT_EQ(output, expect);
2037 EXPECT_EQ(error, "");
2038 }
2039}
2040#endif
2041
2042TEST(shader_tool, ResourceGuard)
2043{
2044 using namespace shader;
2045 using namespace std;
2046
2047 {
2048 string input = R"(
2049void my_func() {
2050 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2051}
2052)";
2053 string expect = R"(
2054void my_func() {
2055
2056#if defined(CREATE_INFO_draw_resource_id_varying)
2057#line 3
2058 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2059
2060#endif
2061#line 4
2062}
2063)";
2064 auto [output, _, error] = process_test_string(input);
2065 EXPECT_EQ(output, expect);
2066 EXPECT_EQ(error, "");
2067 }
2068 {
2069 string input = R"(
2070uint my_func() {
2071 uint i = 0;
2072 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2073 return i;
2074}
2075)";
2076 string expect = R"(
2077uint my_func() {
2078
2079#if defined(CREATE_INFO_draw_resource_id_varying)
2080#line 3
2081 uint i = 0;
2082 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2083 return i;
2084
2085#else
2086#line 3
2087 return uint(0);
2088#endif
2089#line 6
2090}
2091)";
2092 auto [output, _, error] = process_test_string(input);
2093 EXPECT_EQ(output, expect);
2094 EXPECT_EQ(error, "");
2095 }
2096 {
2097 string input = R"(
2098uint my_func() {
2099 uint i = 0;
2100 {
2101 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2102 }
2103 return i;
2104}
2105)";
2106 string expect = R"(
2107uint my_func() {
2108 uint i = 0;
2109 {
2110
2111#if defined(CREATE_INFO_draw_resource_id_varying)
2112#line 5
2113 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2114
2115#endif
2116#line 6
2117 }
2118 return i;
2119}
2120)";
2121 auto [output, _, error] = process_test_string(input);
2122 EXPECT_EQ(output, expect);
2123 EXPECT_EQ(error, "");
2124 }
2125 {
2126 string input = R"(
2127uint my_func() {
2128 uint i = 0;
2129 {
2130 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2131 i += buffer_get(draw_resource_id, resource_id_buf)[0];
2132 }
2133 return i;
2134}
2135)";
2136 string expect = R"(
2137uint my_func() {
2138 uint i = 0;
2139 {
2140
2141#if defined(CREATE_INFO_draw_resource_id_varying)
2142#line 5
2143
2144#if defined(CREATE_INFO_draw_resource_id)
2145#line 5
2146 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2147 i += buffer_get(draw_resource_id, resource_id_buf)[0];
2148
2149#endif
2150
2151#endif
2152#line 7
2153 }
2154 return i;
2155}
2156)";
2157 auto [output, _, error] = process_test_string(input);
2158 EXPECT_EQ(output, expect);
2159 EXPECT_EQ(error, "");
2160 }
2161 {
2162 /* Guard in template. */
2163 string input = R"(
2164template<> uint my_func<uint>(uint i) {
2165 return buffer_get(draw_resource_id, resource_id_buf)[i];
2166}
2167)";
2168 string expect = R"(
2169 uint my_funcTuint(uint i) {
2170
2171#if defined(CREATE_INFO_draw_resource_id)
2172#line 3
2173 return buffer_get(draw_resource_id, resource_id_buf)[i];
2174
2175#else
2176#line 3
2177 return uint(0);
2178#endif
2179#line 4
2180}
2181)";
2182 auto [output, _, error] = process_test_string(input);
2183 EXPECT_EQ(output, expect);
2184 EXPECT_EQ(error, "");
2185 }
2186}
2187
2188TEST(shader_tool, EmptyStruct)
2189{
2190 using namespace shader;
2191 using namespace std;
2192
2193 {
2194 string input = R"(
2195class S {};
2196struct T {};
2197struct U {
2198 static void fn() {}
2199};
2200)";
2201 string expect = R"(
2202struct S { int _pad;};
2203#line 2
2204 S S_ctor_() {S r;r._pad=0;return r;}
2205struct T { int _pad;};
2206#line 3
2207 T T_ctor_() {T r;r._pad=0;return r;}
2208struct U {
2209
2210int _pad;};
2211
2212#ifndef GPU_METAL
2213U U_ctor_();
2214void U_fn();
2215#endif
2216#line 4
2217 U U_ctor_() {U r;r._pad=0;return r;}
2218 void U_fn() {}
2219#line 7
2220)";
2221 auto [output, _, error] = process_test_string(input);
2222 EXPECT_EQ(output, expect);
2223 EXPECT_EQ(error, "");
2224 }
2225}
2226
2227TEST(shader_tool, StructuredBindings)
2228{
2229 using namespace shader;
2230 using namespace std;
2231
2232 {
2233 string input = R"(
2234struct S {
2235 int i;
2236 float b;
2237};
2238
2239S test()
2240{
2241 return S{};
2242}
2243
2244void fn(S u, S &v)
2245{
2246 S t;
2247 S &r = t;
2248 {
2249 int u;
2250 int t;
2251 }
2252 auto [a, b] = S{};
2253 auto [c, d] = test();
2254 auto [e, f] = t;
2255 auto [g, h] = u;
2256 auto [i, j] = r;
2257 auto [k, l] = v;
2258}
2259)";
2260 string expect = R"(
2261struct S {
2262 int i;
2263 float b;
2264};
2265#line 2
2266 S S_ctor_() {S r;r.i=0;r.b=0.0f;return r;}
2267#line 7
2268S test()
2269{
2270 return S_ctor_();
2271}
2272
2273void fn(S u, _ref(S ,v))
2274{
2275 S t;
2276
2277 {
2278 int u;
2279 int t;
2280 }
2281 S _u0= S_ctor_();int a=_u0.i;float b=_u0.b;
2282 S _u1= test();int c=_u1.i;float d=_u1.b;
2283 S _u2= t;int e=_u2.i;float f=_u2.b;
2284 S _u3= u;int g=_u3.i;float h=_u3.b;
2285 S _u4= t;int i=_u4.i;float j=_u4.b;
2286 S _u5= v;int k=_u5.i;float l=_u5.b;
2287}
2288)";
2289 auto [output, _, error] = process_test_string(input);
2290 EXPECT_EQ(output, expect);
2291 EXPECT_EQ(error, "");
2292 }
2293}
2294
2295TEST(shader_tool, StructMethods)
2296{
2297 using namespace shader;
2298 using namespace std;
2299
2300 {
2301 string input = R"(
2302class S {
2303 private:
2304 int member;
2305 int this_member;
2306
2307 public:
2308 static S construct()
2309 {
2310 S a;
2311 a.member = 0;
2312 a.this_member = 0;
2313 return a;
2314 }
2315
2316 int another_member;
2317
2318 S function(int i)
2319 {
2320 this->member = i;
2321 this_member++;
2322 return *this;
2323 }
2324
2325 int size() const
2326 {
2327 return this->member;
2328 }
2329};
2330
2331void main()
2332{
2333 S s = S::construct();
2334 f.f();
2335 f(0).f();
2336 f().f();
2337 l.o.t();
2338 l.o(0).t();
2339 l.o().t();
2340 l[0].o();
2341 l.o[0].t();
2342 l.o().t[0];
2343}
2344)";
2345 string expect = R"(
2346struct S {
2347
2348 int member;
2349 int this_member;
2350#line 16
2351 int another_member;
2352#line 29
2353};
2354#line 32
2355#ifndef GPU_METAL
2356S S_ctor_();
2357S S_construct();
2358S _function(_ref(S ,this_), int i);
2359int _size(const S this_);
2360#endif
2361#line 2
2362 S S_ctor_() {S r;r.member=0;r.this_member=0;r.another_member=0;return r;}
2363#line 8
2364 S S_construct()
2365 {
2366 S a;
2367 a.member = 0;
2368 a.this_member = 0;
2369 return a;
2370 }
2371#line 18
2372 S _function(_ref(S ,this_), int i)
2373 {
2374 this_.member = i;
2375 this_.this_member++;
2376 return this_;
2377 }
2378#line 25
2379 int _size(const S this_)
2380 {
2381 return this_.member;
2382 }
2383#line 31
2384void main()
2385{
2386 S s = S_construct();
2387 _f(f);
2388 _f(f(0));
2389 _f(f());
2390 _t(l.o);
2391 _t(_o(l, 0));
2392 _t(_o(l));
2393 _o(l[0]);
2394 _t(l.o[0]);
2395 _o(l).t[0];
2396}
2397)";
2398 auto [output, _, error] = process_test_string(input);
2399 EXPECT_EQ(output, expect);
2400 EXPECT_EQ(error, "");
2401 }
2402 {
2403 string input = R"(
2404struct A {
2405 int a;
2406 uint b;
2407 float fn1() { return a; }
2408 float fn2() { int fn2; return fn1(); }
2409 static float fn3() { int a; return a; }
2410};
2411)";
2412 string expect = R"(
2413struct A {
2414 int a;
2415 uint b;
2416#line 8
2417};
2418
2419#ifndef GPU_METAL
2420A A_ctor_();
2421float _fn1(_ref(A ,this_));
2422float _fn2(_ref(A ,this_));
2423float A_fn3();
2424#endif
2425#line 2
2426 A A_ctor_() {A r;r.a=0;r.b=0u;return r;}
2427#line 5
2428 float _fn1(_ref(A ,this_)) { return this_.a; }
2429 float _fn2(_ref(A ,this_)) { int fn2; return _fn1(this_); }
2430 float A_fn3() { int a; return a; }
2431#line 9
2432)";
2433 auto [output, _, error] = process_test_string(input);
2434 EXPECT_EQ(output, expect);
2435 EXPECT_EQ(error, "");
2436 }
2437 {
2438 string input = R"(
2439struct A {
2440 int a;
2441 float fn1(int a) { return a; }
2442};
2443)";
2444 auto [output, _, error] = process_test_string(input);
2445 EXPECT_EQ(error, "Class member shadowing.");
2446 }
2447 {
2448 string input = R"(
2449struct A {
2450 int a;
2451 float fn1() { int a; return a; }
2452};
2453)";
2454 auto [output, _, error] = process_test_string(input);
2455 EXPECT_EQ(error, "Class member shadowing.");
2456 }
2457 {
2458 string input = R"(
2459class S {
2460 int xzwy() const
2461 {
2462 }
2463};
2464)";
2465 string expect = R"(
2466)";
2467 auto [output, _, error] = process_test_string(input);
2468 EXPECT_EQ(error, "Method name matching swizzles accessor are forbidden.");
2469 }
2470}
2471
2472TEST(shader_tool, SrtMutations)
2473{
2474 using namespace std;
2475 using namespace shader::parser;
2476
2477 {
2478 string input = R"(
2479float fn([[resource_table]] SRT &srt) {
2480 return srt.member;
2481}
2482)";
2483 string expect = R"(
2484
2485#if defined(CREATE_INFO_SRT)
2486#line 2
2487float fn(SRT srt) {
2488 return srt_access(SRT, member);
2489}
2490
2491#endif
2492#line 5
2493)";
2494 auto [output, _, error] = process_test_string(input);
2495 EXPECT_EQ(output, expect);
2496 EXPECT_EQ(error, "");
2497 }
2498 {
2499 string input = R"(
2500float fn([[resource_table]] SRT srt) {
2501 return srt.member;
2502}
2503)";
2504 auto [output, _, error] = process_test_string(input);
2505 EXPECT_EQ(error, "Shader Resource Table arguments must be references.");
2506 }
2507 {
2508 string input = R"(
2509float fn([[resource_table]] SRT &srt) {
2510 [[resource_table]] OtherSRT &other_srt = srt.other_srt;
2511 return other_srt.member;
2512}
2513)";
2514 string expect = R"(
2515
2516#if defined(CREATE_INFO_SRT)
2517#line 2
2518float fn(SRT srt) {
2519
2520 return srt_access(OtherSRT, member);
2521}
2522
2523#endif
2524#line 6
2525)";
2526 auto [output, _, error] = process_test_string(input);
2527 EXPECT_EQ(output, expect);
2528 EXPECT_EQ(error, "");
2529 }
2530}
2531
2532TEST(shader_tool, EntryPointResources)
2533{
2534 using namespace std;
2535 using namespace shader::parser;
2536
2537 {
2538 string input = R"(
2539namespace ns {
2540
2541struct VertOut {
2542 [[smooth]] float3 local_pos;
2543};
2544
2545struct FragOut {
2546 [[frag_color(0)]] float3 color;
2547 [[frag_color(1), index(1)]] uint test;
2548};
2549
2550template<typename T>
2551struct VertIn {
2552 [[attribute(0)]] T pos;
2553};
2554template struct VertIn<float>;
2555
2556
2557[[vertex]] void vertex_function([[resource_table]] Resources &srt,
2558 [[in]] const VertIn<float> &v_in,
2559 [[out, condition(cond)]] VertOut &v_out,
2560 [[base_instance]] const int &base_instance,
2561 [[point_size]] float &point_size,
2562 [[clip_distance]] float (&clip_distance)[6],
2563 [[layer]] int &layer,
2564 [[viewport_index]] int &viewport_index,
2565 [[position]] float4 &out_position)
2566{
2567 base_instance;
2568 point_size;
2569 clip_distance;
2570 layer;
2571 viewport_index;
2572 out_position;
2573}
2574
2575[[fragment]] void fragment_function([[resource_table]] Resources &srt,
2576 [[in, condition(cond)]] const VertOut &v_out,
2577 [[out]] FragOut &frag_out,
2578 [[frag_depth(greater)]] float depth,
2579 [[frag_stencil_ref]] int stencil,
2580 [[layer]] const int &layer,
2581 [[viewport_index]] const int &viewport_index,
2582 [[point_coord]] const float2 pt_co,
2583 [[front_facing]] const bool facing,
2584 [[frag_coord]] const float4 in_position)
2585{
2586 layer;
2587 viewport_index;
2588 depth;
2589 stencil;
2590 in_position;
2591 pt_co;
2592 facing;
2593}
2594
2595[[compute]] void compute_function([[resource_table]] Resources &srt,
2596 [[global_invocation_id]] const uint3 &global_invocation_id,
2597 [[local_invocation_id]] const uint3 &local_invocation_id,
2598 [[local_invocation_index]] const uint &local_invocation_index,
2599 [[work_group_id]] const uint3 &workgroup_id,
2600 [[num_work_groups]] const uint3 &num_work_groups)
2601{
2602 global_invocation_id;
2603 local_invocation_id;
2604 local_invocation_index;
2605 workgroup_id;
2606 num_work_groups;
2607}
2608
2609}
2610)";
2611 string expect = R"(
2612#line 4
2613struct ns_VertOut {
2614 float3 local_pos;
2615};
2616#line 4
2617 ns_VertOut ns_VertOut_ctor_() {ns_VertOut r;r.local_pos=float3(0);return r;}
2618#line 8
2619struct ns_FragOut {
2620 float3 color;
2621 uint test;
2622};
2623#line 8
2624 ns_FragOut ns_FragOut_ctor_() {ns_FragOut r;r.color=float3(0);r.test=0u;return r;}
2625#line 14
2626struct ns_VertInTfloat {
2627 float pos;
2628};
2629#line 14
2630 ns_VertInTfloat ns_VertInTfloat_ctor_() {ns_VertInTfloat r;r.pos=0.0f;return r;}
2631#line 20
2632
2633#if defined(CREATE_INFO_Resources)
2634#line 20
2635
2636#if defined(ENTRY_POINT_ns_vertex_function)
2637#line 20
2638 void ns_vertex_function(
2639#line 28
2640 )
2641{
2642#if defined(GPU_VERTEX_SHADER)
2643#line 29
2644 Resources srt = Resources_ctor_();
2645 gpu_BaseInstance;
2646 gl_PointSize;
2647 gl_ClipDistance;
2648 gl_Layer;
2649 gpu_ViewportIndex;
2650 gl_Position;
2651
2652#endif
2653#line 36
2654}
2655#endif
2656#endif
2657#line 38
2658
2659#if defined(CREATE_INFO_Resources)
2660#line 38
2661
2662#if defined(ENTRY_POINT_ns_fragment_function)
2663#line 38
2664 void ns_fragment_function(
2665#line 47
2666 )
2667{
2668#if defined(GPU_FRAGMENT_SHADER)
2669#line 48
2670 Resources srt = Resources_ctor_();
2671 gl_Layer;
2672 gpu_ViewportIndex;
2673 gl_FragDepth;
2674 gl_FragStencilRefARB;
2675 gl_FragCoord;
2676 gl_PointCoord;
2677 gl_FrontFacing;
2678
2679#endif
2680#line 56
2681}
2682#endif
2683#endif
2684#line 58
2685
2686#if defined(CREATE_INFO_Resources)
2687#line 58
2688
2689#if defined(ENTRY_POINT_ns_compute_function)
2690#line 58
2691 void ns_compute_function(
2692#line 63
2693 )
2694{
2695#if defined(GPU_COMPUTE_SHADER)
2696#line 64
2697 Resources srt = Resources_ctor_();
2698 gl_GlobalInvocationID;
2699 gl_LocalInvocationID;
2700 gl_LocalInvocationIndex;
2701 gl_WorkGroupID;
2702 gl_NumWorkGroups;
2703
2704#endif
2705#line 70
2706}
2707#endif
2708#endif
2709)";
2710 string expect_infos = R"(#pragma once
2711
2712
2713
2714GPU_SHADER_CREATE_INFO(ns_VertInTfloat)
2715VERTEX_IN(0, float, pos)
2716GPU_SHADER_CREATE_END()
2717
2718
2719GPU_SHADER_CREATE_INFO(ns_FragOut)
2720FRAGMENT_OUT(0, float3, ns_FragOut_color)
2721FRAGMENT_OUT_DUAL(1, uint, ns_FragOut_test, SRC_1)
2722GPU_SHADER_CREATE_END()
2723
2724
2725GPU_SHADER_INTERFACE_INFO(ns_VertOut_t)
2726SMOOTH(float3, ns_VertOut_local_pos)
2727GPU_SHADER_INTERFACE_END()
2728
2729
2730
2731GPU_SHADER_CREATE_INFO(ns_vertex_function_infos_)
2732ADDITIONAL_INFO(Resources)
2733ADDITIONAL_INFO(ns_VertInTfloat)
2734VERTEX_OUT(ns_VertOut_t)
2735.builtins(BuiltinBits::INSTANCE_ID)
2736.builtins(BuiltinBits::POINT_SIZE)
2737.builtins(BuiltinBits::LAYER)
2738.builtins(BuiltinBits::VIEWPORT_INDEX)
2739.builtins(BuiltinBits::CLIP_DISTANCES)
2740GPU_SHADER_CREATE_END()
2741
2742GPU_SHADER_CREATE_INFO(ns_fragment_function_infos_)
2743DEPTH_WRITE(DepthWrite::GREATER)
2744.builtins(BuiltinBits::STENCIL_REF)
2745.builtins(BuiltinBits::POINT_COORD)
2746.builtins(BuiltinBits::FRONT_FACING)
2747.builtins(BuiltinBits::FRAG_COORD)
2748ADDITIONAL_INFO(Resources)
2749ADDITIONAL_INFO(ns_FragOut)
2750.builtins(BuiltinBits::LAYER)
2751.builtins(BuiltinBits::VIEWPORT_INDEX)
2752GPU_SHADER_CREATE_END()
2753
2754GPU_SHADER_CREATE_INFO(ns_compute_function_infos_)
2755ADDITIONAL_INFO(Resources)
2756.builtins(BuiltinBits::GLOBAL_INVOCATION_ID)
2757.builtins(BuiltinBits::LOCAL_INVOCATION_ID)
2758.builtins(BuiltinBits::LOCAL_INVOCATION_INDEX)
2759.builtins(BuiltinBits::WORK_GROUP_ID)
2760.builtins(BuiltinBits::NUM_WORK_GROUP)
2761GPU_SHADER_CREATE_END()
2762
2763)";
2764 auto [output, metadata, error] = process_test_string(input);
2765 string infos = metadata.serialize_infos();
2766
2767 EXPECT_EQ(output, expect);
2768 EXPECT_EQ(infos, expect_infos);
2769 EXPECT_EQ(error, "");
2770 }
2771}
2772
2773TEST(shader_tool, PipelineDescription)
2774{
2775 using namespace std;
2776 using namespace shader::parser;
2777
2778 {
2779 string input = R"(
2780namespace ns {
2781
2782PipelineGraphic graphic_pipe(vertex_func, fragment_func, Type{.a = true, .b = 9, .c = 3u});
2783PipelineCompute compute_pipe(compute_func, Type{.a = true, .b = 8, .c = 7u});
2784
2785}
2786)";
2787 string expect = R"(
2788
2789
2790
2791
2792
2793
2794)";
2795 string expect_infos = R"(#pragma once
2796
2797
2798
2799
2800
2801
2802
2803GPU_SHADER_CREATE_INFO(ns_graphic_pipe)
2804GRAPHIC_SOURCE("test.bsl")
2805VERTEX_FUNCTION("vertex_func")
2806FRAGMENT_FUNCTION("fragment_func")
2807ADDITIONAL_INFO(vertex_func_infos_)
2808ADDITIONAL_INFO(fragment_func_infos_)
2809COMPILATION_CONSTANT(bool, a, true)
2810COMPILATION_CONSTANT(int, b, 9)
2811COMPILATION_CONSTANT(uint, c, 3u)
2812DO_STATIC_COMPILATION()
2813GPU_SHADER_CREATE_END()
2814
2815GPU_SHADER_CREATE_INFO(ns_compute_pipe)
2816COMPUTE_SOURCE("test.bsl")
2817COMPUTE_FUNCTION("compute_func")
2818ADDITIONAL_INFO(compute_func_infos_)
2819COMPILATION_CONSTANT(bool, a, true)
2820COMPILATION_CONSTANT(int, b, 8)
2821COMPILATION_CONSTANT(uint, c, 7u)
2822DO_STATIC_COMPILATION()
2823GPU_SHADER_CREATE_END()
2824
2825)";
2826 auto [output, metadata, error] = process_test_string(input);
2827 string infos = metadata.serialize_infos();
2828
2829 EXPECT_EQ(output, expect);
2830 EXPECT_EQ(infos, expect_infos);
2831 EXPECT_EQ(error, "");
2832 }
2833}
2834
2835TEST(shader_tool, InitializerList)
2836{
2837 using namespace std;
2838 using namespace shader::parser;
2839
2840 {
2841 string input = R"(
2842T fn1() { return T{1, 2}; }
2843T fn2() { return T{1, 2, }; }
2844T fn3() { return T{.a=1, .b=2}; }
2845T fn4() { return T{.a=1, .b=2, }; }
2846T fn5() { return {1, 2}; }
2847T fn6() { return {1, 2, }; }
2848T fn7() { return {.a=1, .b=2}; }
2849T fn8() { return {.a=1, .b=2, }; }
2850void fn() {
2851 T t1=T{1, 2};
2852 T t2=T{1, 2, };
2853 T t3=T{.a=1, .b=2};
2854 T t4=T{.a=1, .b=2, };
2855 T t5={1, 2};
2856 T t6={1, 2, };
2857 T t7={.a=1, .b=2};
2858 T t8={.a=1, .b=2, };
2859 T t9=T{.a=1, .b=T{0, 2}.x};
2860 T t10=T{1, T{0, 2}.x};
2861}
2862)";
2863 string expect = R"(
2864T fn1() { return _ctor(T) 1, 2 _rotc() ; }
2865T fn2() { return _ctor(T) 1, 2 _rotc() ; }
2866T fn3() { {T _tmp ; _tmp.a=1; _tmp.b=2; return _tmp;}; }
2867T fn4() { {T _tmp ; _tmp.a=1; _tmp.b=2 ; return _tmp;}; }
2868T fn5() { return _ctor(T) 1, 2 _rotc() ; }
2869T fn6() { return _ctor(T) 1, 2 _rotc() ; }
2870T fn7() { {T _tmp ; _tmp.a=1; _tmp.b=2; return _tmp;}; }
2871T fn8() { {T _tmp ; _tmp.a=1; _tmp.b=2 ; return _tmp;}; }
2872void fn() {
2873 T t1=_ctor(T) 1, 2 _rotc() ;
2874 T t2=_ctor(T) 1, 2 _rotc() ;
2875 T t3; t3.a=1; t3.b=2;
2876 T t4; t4.a=1; t4.b=2 ;
2877 T t5=_ctor(T) 1, 2 _rotc() ;
2878 T t6=_ctor(T) 1, 2 _rotc() ;
2879 T t7; t7.a=1; t7.b=2;
2880 T t8; t8.a=1; t8.b=2 ;
2881 T t9; t9.a=1; t9.b=_ctor(T) 0, 2 _rotc() .x;
2882 T t10=_ctor(T) 1, _ctor(T) 0, 2 _rotc() .x _rotc() ;
2883}
2884)";
2885 auto [output, _, error] = process_test_string(input);
2886 EXPECT_EQ(output, expect);
2887 EXPECT_EQ(error, "");
2888 }
2889 {
2890 string input = R"(
2891void fn() {
2892 T t9={1, T{.a=1, .b=2}.a};
2893}
2894)";
2895 auto [output, metadata, error] = process_test_string(input);
2896 EXPECT_EQ(error, "Designated initializers are only supported in assignments");
2897 }
2898 {
2899 string input = R"(
2900void fn() {
2901 T t10={1, float4{0}};
2902}
2903)";
2904 auto [output, metadata, error] = process_test_string(input);
2905 EXPECT_EQ(
2906 error,
2907 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
2908 }
2909 {
2910 string input = R"(
2911void fn() {
2912 T t11={.a=1, .b=T{.a=1, .b=2}.a};
2913}
2914)";
2915 auto [output, metadata, error] = process_test_string(input);
2916 EXPECT_EQ(error, "Nested initializer lists are not supported");
2917 }
2918 {
2919 string input = R"(
2920void fn() {
2921 T t12={.a=1, .b=float4{0}};
2922}
2923)";
2924 auto [output, metadata, error] = process_test_string(input);
2925 EXPECT_EQ(
2926 error,
2927 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
2928 }
2929}
2930
2931TEST(shader_tool, Parser)
2932{
2933 using namespace std;
2934 using namespace shader::parser;
2935
2938
2939 ErrorHandler err_handler;
2940
2941 {
2942 string input = R"(
29431;
29441.0;
29452e10;
29462e10f;
29472.e10f;
29482.0e-1f;
29492.0e-1;
29502.0e-1f;
29510xFF;
29520xFFu;
29530+8;
2954)";
2955 string expect = R"(
29561;1;1;1;1;1;1;1;1;1;1+1;)";
2957 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
2958 }
2959 {
2960 string input = R"(
2961[[a(0,1,b), c, d(t)]]
2962)";
2963 string expect = R"(
2964[[A(1,1,A),A,A(A)]])";
2965 string scopes = R"(GABbcmmmbbcm)";
2966 EXPECT_EQ(Parser(input, err_handler).token_types_str(), expect);
2967 EXPECT_EQ(Parser(input, err_handler).scope_types_str, scopes);
2968 }
2969 {
2970 string input = R"(
2971struct T {
2972 int t = 1;
2973};
2974class B {
2975 T t;
2976};
2977)";
2978 string expect = R"(
2979sA{AA=1;};SA{AA;};)";
2980 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
2981 }
2982 {
2983 string input = R"(
2984a /* Comment */
2985//
2986a
2987/* //
2988*/
2989a
2990// a
2991a
2992)";
2993 string expect = R"(
2994AZZAZAZA)";
2995 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
2996 }
2997 {
2998 string input = R"(
2999namespace T {}
3000namespace T::U::V {}
3001)";
3002 string expect = R"(
3003nA{}nA::A::A{})";
3004 string expect_scopes = R"(GNN)";
3005 EXPECT_EQ(Parser(input, err_handler).token_types_str(), expect);
3006 EXPECT_EQ(Parser(input, err_handler).scope_types_str, expect_scopes);
3007 }
3008 {
3009 string input = R"(
3010void f(int t = 0) {
3011 int i = 0, u = 2, v = {1.0f};
3012 {
3013 v = i = u, v++;
3014 if (v == i) {
3015 return;
3016 }
3017 }
3018}
3019)";
3020 string expect = R"(
3021AA(AA=1){AA=1,A=1,A={1};{A=A=A,AP;i(AEA){r;}}})";
3022 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
3023 }
3024 {
3025 Parser parser("float i;", err_handler);
3026 parser.insert_after(Token(parser, 0), "A ");
3027 parser.insert_after(Token(parser, 0), "B ");
3028 EXPECT_EQ(parser.result_get(), "float A B i;");
3029 }
3030 {
3031 string input = R"(
3032A
3033#line 100
3034B
3035)";
3036 Parser parser(input, err_handler);
3037 string expect = R"(
3038A#A1A)";
3039 EXPECT_EQ(parser.token_types_str(), expect);
3040
3041 Token A = Token(parser, 1);
3042 Token B = Token(parser, 5);
3043
3044 EXPECT_EQ(A.str(), "A");
3045 EXPECT_EQ(B.str(), "B");
3046 EXPECT_EQ(A.line_number(), 2);
3047 EXPECT_EQ(B.line_number(), 100);
3048 }
3049 {
3050 string input = R"(
3051const bool foo;
3052[[a]] int bar[0];
3053)";
3054
3055 string expect = R"(
3056match(, const, bool, , foo, , ;)
3057match([a], , int, , bar, [0], ;)
3058)";
3059
3060 Parser parser(input, err_handler);
3061
3062 string result = "\n";
3063 parser().foreach_declaration([&](Scope attributes,
3064 Token const_tok,
3065 Token type,
3066 Scope template_scope,
3067 Token name,
3068 Scope array,
3069 Token decl_end) {
3070 result += "match(";
3071 result += string(attributes.str()) + ", ";
3072 result += string(const_tok.str()) + ", ";
3073 result += string(type.str()) + ", ";
3074 result += string(template_scope.str()) + ", ";
3075 result += string(name.str()) + ", ";
3076 result += string(array.str()) + ", ";
3077 result += string(decl_end.str()) + ")\n";
3078 });
3079
3080 EXPECT_EQ(expect, result);
3081 }
3082}
3083
3084static int test_expression(std::string str)
3085{
3086 using namespace shader::parser;
3088 parser.lexical_analysis(str);
3089 try {
3090 return parser.eval();
3091 }
3092 catch (const std::exception &e) {
3093 std::cerr << "Error: " << e.what() << "\n";
3094 return 9999999;
3095 }
3096}
3097
3098TEST(shader_tool, ExpressionParser)
3099{
3100 using namespace std;
3101 using namespace shader::parser;
3102
3103 /* --- Basic arithmetic --- */
3104 EXPECT_EQ(test_expression("1+2+3"), 6);
3105 EXPECT_EQ(test_expression("1*2+3"), 5);
3106 EXPECT_EQ(test_expression("1+2*3"), 7);
3107 EXPECT_EQ(test_expression("10-3-2"), 5);
3108 EXPECT_EQ(test_expression("10-(3-2)"), 9);
3109 EXPECT_EQ(test_expression("20/5/2"), 2);
3110
3111 /* --- Parenthesis --- */
3112 EXPECT_EQ(test_expression("(1+2)*3"), 9);
3113 EXPECT_EQ(test_expression("((2+3)*4)"), 20);
3114
3115 /* --- Unary operators --- */
3116 EXPECT_EQ(test_expression("-1+2"), 1);
3117 EXPECT_EQ(test_expression("~0"), ~0);
3118 EXPECT_EQ(test_expression("!0"), 1);
3119 EXPECT_EQ(test_expression("!5"), 0);
3120
3121 /* --- Bitwise operators --- */
3122 EXPECT_EQ(test_expression("1|2"), 3);
3123 EXPECT_EQ(test_expression("3&1"), 1);
3124 EXPECT_EQ(test_expression("1^3"), 2);
3125 /* Not supported yet. */
3126 // EXPECT_EQ(test_expression("1 << 3"), 8);
3127 // EXPECT_EQ(test_expression("8 >> 2"), 2);
3128
3129 /* --- Bitwise vs arithmetic precedence --- */
3130 /* Not supported yet. */
3131 // EXPECT_EQ(test_expression("1 + 2 << 2"), 12); /* (1+2)<<2 */
3132 // EXPECT_EQ(test_expression("1 << 2 + 1"), 8); /* 1<<(2+1) */
3133
3134 /* --- Comparison operators --- */
3135 EXPECT_EQ(test_expression("1 < 2"), 1);
3136 EXPECT_EQ(test_expression("2 <= 2"), 1);
3137 EXPECT_EQ(test_expression("3 > 5"), 0);
3138 EXPECT_EQ(test_expression("3 != 4"), 1);
3139 EXPECT_EQ(test_expression("3 == 3"), 1);
3140
3141 /* --- Logical operators --- */
3142 EXPECT_EQ(test_expression("1 && 1"), 1);
3143 EXPECT_EQ(test_expression("1 && 0"), 0);
3144 EXPECT_EQ(test_expression("0 || 1"), 1);
3145 EXPECT_EQ(test_expression("0 || 0"), 0);
3146 EXPECT_EQ(test_expression("0 || 0 || 1"), 1);
3147
3148 /* --- Logical precedence --- */
3149 EXPECT_EQ(test_expression("0 || 1 && 0"), 0); /* && before || */
3150 EXPECT_EQ(test_expression("(0 || 1) && 0"), 0);
3151
3152 /* --- Ternary operator --- */
3153 EXPECT_EQ(test_expression("1 ? 2 : 3"), 2);
3154 EXPECT_EQ(test_expression("0 ? 2 : 3"), 3);
3155 EXPECT_EQ(test_expression("1 ? 0 ? 2 : 3 : 4"), 3);
3156 EXPECT_EQ(test_expression("0 ? 1 : 2 ? 3 : 4"), 3);
3157
3158 /* --- Mixed complex expressions --- */
3159 EXPECT_EQ(test_expression("(1+2*3) == 7 && (4|1) == 5"), 1);
3160 EXPECT_EQ(test_expression("!((3<1) == 0)"), 0);
3161 EXPECT_EQ(test_expression("!0 && !0"), 1);
3162 EXPECT_EQ(test_expression("!1 && !0"), 0);
3163 EXPECT_EQ(test_expression("!!1 && !0"), 1);
3164
3165 /* --- Deep Ternary Nesting --- */
3166 EXPECT_EQ(test_expression("1 ? 10 + 5 : 20"), 15);
3167 EXPECT_EQ(test_expression("0 ? 1 : 0 ? 2 : 3"), 3);
3168 EXPECT_EQ(test_expression("1 ? (0 ? 1 : 2) : 3"), 2);
3169 EXPECT_EQ(test_expression("10 + (1 ? 5 : 0) * 2"), 20);
3170
3171 /* --- Unary Chains --- */
3172 EXPECT_EQ(test_expression("! ~ -1"), 1);
3173 EXPECT_EQ(test_expression("-5 * -2"), 10);
3174
3175 /* --- Precedence Boundary Tests --- */
3176 EXPECT_EQ(test_expression("1 == 1 | 2"), 3);
3177 EXPECT_EQ(test_expression("1 + 2 < 4"), 1);
3178 EXPECT_EQ(test_expression("1 | 2 && 0"), 0);
3179
3180 /* --- Complex Boolean Logic --- */
3181 EXPECT_EQ(test_expression("!((1 + 2 == 3) && (4 * 5 <= 20) || (0 ? 1 : 0))"), 0);
3182
3183 /* --- The Kitchen Sink --- */
3184 EXPECT_EQ(test_expression("(10 - 2 * 3 == 4) ? 50 : 100 + !0"), 50);
3185}
3186
3187} // namespace blender::gpu::tests
#define A
#define output
#define str(s)
#define EXPECT_EQ(result, expect)
#define B
TEST(shader_tool, ExpressionParser)
static int test_expression(std::string str)
TEST(array_store, Nop)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
@ Enum
Definition types.hh:106
@ Union
Definition types.hh:109
@ Template
Definition types.hh:119
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337