5#include "testing/testing.h"
23float d[] = {a[0], a(0, 1)};
27float a[2] = ARRAY_T(float) ARRAY_V( 0, 1 );
28float b[2] = ARRAY_T(float) ARRAY_V(
32float d[2] = ARRAY_T(float) ARRAY_V( a[0], a(0, 1) );
34 auto [
output, _, error] = process_test_string(input);
42 auto [
output, _, error] = process_test_string(input);
43 EXPECT_EQ(error,
"Array size must be greater than zero.");
49 auto [
output, _, error] = process_test_string(input);
50 EXPECT_EQ(error,
"Array size must be greater than zero.");
54float2 c[2] = {{0, 1}, {0, 1}};
56 auto [
output, _, error] = process_test_string(input);
57 EXPECT_EQ(error,
"Nested initializer list is not supported.");
61TEST(shader_tool, CommaDeclaration)
63 using namespace shader;
77 A A_ctor_() {A r;r.a=0;r.b=0;return r;}
80 auto [
output, _, error] = process_test_string(input);
86TEST(shader_tool, Include)
99#if !defined(GPU_SHADER)
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);
119 using namespace shader;
123struct [[host_shared]] T {
135#define T_union0_host_shared_ T_union0
136#define T_union0_host_shared_uniform_ T_union0
143 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
146#define T_host_shared_ T
147#define T_host_shared_uniform_ T
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);
164 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
166uint4 _a(const T this_) {
168 val = floatBitsToUint(this_.union0.data0);
172void _a_set_(_ref(T ,this_), uint4 value) {
173 this_.union0.data0 = uintBitsToFloat(value);
176int4 _b(const T this_) {
178 val = floatBitsToInt(this_.union0.data0);
182void _b_set_(_ref(T ,this_), int4 value) {
183 this_.union0.data0 = intBitsToFloat(value);
186float4 _c(const T this_) {
188 val = this_.union0.data0;
192void _c_set_(_ref(T ,this_), float4 value) {
193 this_.union0.data0 = value;
197 auto [
output, _, error] = process_test_string(input);
203struct [[host_shared]] T {
218#define T_union0_host_shared_ T_union0
219#define T_union0_host_shared_uniform_ T_union0
226 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
229#define T_union1_host_shared_ T_union1
230#define T_union1_host_shared_uniform_ T_union1
237 T_union1 T_union1_ctor_() {T_union1 r;r.data0=float4(0);return r;}
240#define T_host_shared_ T
241#define T_host_shared_uniform_ T
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);
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;}
262uint4 _a(const T this_) {
264 val = floatBitsToUint(this_.union0.data0);
268void _a_set_(_ref(T ,this_), uint4 value) {
269 this_.union0.data0 = uintBitsToFloat(value);
272uint4 _b(const T this_) {
274 val = floatBitsToUint(this_.union1.data0);
278void _b_set_(_ref(T ,this_), uint4 value) {
279 this_.union1.data0 = uintBitsToFloat(value);
283 auto [
output, _, error] = process_test_string(input);
289struct [[host_shared]] B {
294struct [[host_shared]] A {
298struct [[host_shared]] T {
306#define B_host_shared_ B
307#define B_host_shared_uniform_ B
314 B B_ctor_() {B r;r.a=packed_float3(0);r.b=0.0f;return r;}
316#define A_host_shared_ A
317#define A_host_shared_uniform_ A
323 A A_ctor_() {A r;r.e=B_ctor_();return r;}
326#define T_union0_host_shared_ T_union0
327#define T_union0_host_shared_uniform_ T_union0
334 T_union0 T_union0_ctor_() {T_union0 r;r.data0=float4(0);return r;}
337#define T_host_shared_ T
338#define T_host_shared_uniform_ T
348void _a_set_(_ref(T ,this_), A value);
351 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
355 val.e.a = this_.union0.data0.xyz;
356 val.e.b = this_.union0.data0.w;
360void _a_set_(_ref(T ,this_), A value) {
361 this_.union0.data0.xyz = value.e.a;
362 this_.union0.data0.w = value.e.b;
366 auto [
output, _, error] = process_test_string(input);
372struct [[host_shared]] T {
381#define T_union0_host_shared_ T_union0
382#define T_union0_host_shared_uniform_ T_union0
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;}
395#define T_host_shared_ T
396#define T_host_shared_uniform_ T
405float4x4 _a(const T this_);
406void _a_set_(_ref(T ,this_), float4x4 value);
409 T T_ctor_() {T r;r.union0=T_union0_ctor_();return r;}
411float4x4 _a(const T this_) {
413 val[0] = this_.union0.data0;
414 val[1] = this_.union0.data1;
415 val[2] = this_.union0.data2;
416 val[3] = this_.union0.data3;
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];
428 auto [
output, _, error] = process_test_string(input);
434struct [[host_shared]] T {
440 auto [
output, _, error] = process_test_string(input);
442 "All union members must have their type wrapped using the union_t<T> template.");
446TEST(shader_tool, Unroll)
448 using namespace shader;
453for (int i = 2; i < 4; i++) [[unroll]] { content += i; })";
463 auto [
output, _, error] = process_test_local(input);
469for (int i = 2; i < 4; i++, y++) [[unroll]] { content += i; })";
482 auto [
output, _, error] = process_test_local(input);
488for (int i = 2; i < 4 && i < y; i++, y++) [[unroll]] { cont += i; })";
505 auto [
output, _, error] = process_test_local(input);
511for (; i < j;) [[unroll_n(2)]] { content += i; })";
525 auto [
output, _, error] = process_test_local(input);
531for (; i < j;) [[unroll_n(2)]] { for (; j < k;) [[unroll_n(2)]] {} })";
567 auto [
output, _, error] = process_test_local(input);
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.");
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.");
583for (; i < j;) [[unroll_n(2)]] { for (; j < k;) {break;continue;} })";
590 { for (; j < k;) {break;continue;} }
594 { for (; j < k;) {break;continue;} }
597 auto [
output, _, error] = process_test_local(input);
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.");
610 using namespace shader;
616void func() { T::fn(); }
617template void func<A>();
621void funcTA() { A_fn(); }
624 auto [
output, _, error] = process_test_string(input);
632template void func<float>(float a);
635template void foo<float>(float &a);
645void func(float a) {a;}
647void foo(_ref(float ,a)) {a;}
655 auto [
output, _, error] = process_test_string(input);
661template<typename T, int i>
665template void func<float, 1>(float a);
669void funcTfloatT1(float a) {
674 auto [
output, _, error] = process_test_string(input);
680template<int i, uint j, int k> E func() { return E(i + j + k); }
681template E func<0x1, 2, -1>();
684E funcT0x1T2T_1() { return E(0x1 + 2 + -1); }
687 auto [
output, _, error] = process_test_string(input);
693template<enum E e, char i> E func() { return E(e + i); }
694template E func<v, 2>();
697E funcTvT2() { return E(v + 2); }
700 auto [
output, _, error] = process_test_string(input);
706template<> void func<T, Q>(T a) {a}
709 void funcTTTQ(T a) {a}
711 auto [
output, _, error] = process_test_string(input);
717template<typename T, int i = 0> void func(T a) {a;}
719 auto [
output, _, error] = process_test_string(input);
720 EXPECT_EQ(error,
"Default arguments are not supported inside template declaration");
724template void func(float a);
726 auto [
output, _, error] = process_test_string(input);
728 "Template instantiation and specialization require explicit template arguments");
732template A<f> fn(A<f> a);
734 auto [
output, _, error] = process_test_string(input);
736 "Template instantiation and specialization require explicit template arguments");
740template<> A fn(A a) {}
742 auto [
output, _, error] = process_test_string(input);
744 "Template instantiation and specialization require explicit template arguments");
748template<> A<f> fn(A<f> a) {}
750 auto [
output, _, error] = process_test_string(input);
752 "Template instantiation and specialization require explicit template arguments");
755 string input = R
"(func<float, 1>(a);)";
756 string expect = R
"(funcTfloatT1(a);)";
757 auto [
output, _, error] = process_test_local(input);
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);
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);
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);
784TEST(shader_tool, TemplateStruct)
799template struct A<float>;
809ATfloat ATfloat_ctor_();
810ATfloat _method(const ATfloat this_, float b);
813 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
815 ATfloat _method(const ATfloat this_, float b)
817 return _ctor(ATfloat) b _rotc() ;
821 auto [
output, _, error] = process_test_string(input);
829template struct A<float>;
833struct ATfloat { float a; };
835 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
838 auto [
output, _, error] = process_test_string(input);
844template<> struct A<float>{
853 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
856 auto [
output, _, error] = process_test_string(input);
862void func(A<float> a) {}
865void func(ATfloat a) {}
867 auto [
output, _, error] = process_test_string(input);
876template<typename B> struct A {
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); }
884template struct A<int>;
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>();
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_));
916 N_ATint N_ATint_ctor_() {N_ATint r;r.i=0;return r;}
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); }
933 auto [
output, _, error] = process_test_string(input);
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);
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);
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);
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.");
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.");
976 string input = R
"(void func() { auto &a = b[0 + 1]; })";
977 auto [
output, _, error] = process_test_string(input);
979 "Array subscript inside reference declaration must be a single variable or a "
980 "constant, not an expression.");
983 string input = R
"(void func() { auto &a = b[c]; })";
984 auto [
output, _, error] = process_test_string(input);
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.");
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.");
996 string input = R
"(auto &a = b;)";
997 auto [
output, _, error] = process_test_string(input);
998 EXPECT_EQ(error,
"Unexpected token \"&\": Expecting declaration");
1002TEST(shader_tool, Cleanup)
1004 using namespace shader;
1005 using namespace std;
1030 auto [
output, _, error] = process_test_string(input);
1036TEST(shader_tool, DefaultArguments)
1039 using namespace std;
1043int func(int a, int b = 0)
1049int func(int a, int b )
1061 auto [
output, _, error] = process_test_string(input);
1067int func(int a = 0, const int b = 0)
1073int func(int a , const int b )
1091 auto [
output, _, error] = process_test_string(input);
1097int2 func(int2 a = int2(0, 0)) {
1109 return func(int2(0, 0));
1113 auto [
output, _, error] = process_test_string(input);
1119void func(int a = 0) {
1135 auto [
output, _, error] = process_test_string(input);
1141TEST(shader_tool, SrtTemplateWrapper)
1143 using namespace shader;
1144 using namespace std;
1149 [[resource_table]] srt_t<T> a;
1153#define access_SRT_a() T_ctor_()
1154#ifdef CREATE_INFO_RES_PASS_SRT
1155CREATE_INFO_RES_PASS_SRT
1157#ifdef CREATE_INFO_RES_BATCH_SRT
1158CREATE_INFO_RES_BATCH_SRT
1160#ifdef CREATE_INFO_RES_GEOMETRY_SRT
1161CREATE_INFO_RES_GEOMETRY_SRT
1163#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
1164CREATE_INFO_RES_SHARED_VARS_SRT
1177 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
1182 result.a = T_new_();
1188 auto [
output, _, error] = process_test_string(input);
1195 [[resource_table]] T a;
1198 auto [
output, _, error] = process_test_string(input);
1200 "Members declared with the [[resource_table]] attribute must wrap their type "
1201 "with the srt_t<T> template.");
1209 auto [
output, _, error] = process_test_string(input);
1211 "The srt_t<T> template is only to be used with members declared with the "
1212 "[[resource_table]] attribute.");
1217 [[resource_table]] srt_t<T> a[4];
1220 auto [
output, _, error] = process_test_string(input);
1221 EXPECT_EQ(error,
"[[resource_table]] members cannot be arrays.");
1225TEST(shader_tool, SrtMethod)
1227 using namespace shader;
1228 using namespace std;
1233 [[resource_table]] srt_t<T> a;
1235 void method(int t) {
1241#define access_SRT_a() T_ctor_()
1242#ifdef CREATE_INFO_RES_PASS_SRT
1243CREATE_INFO_RES_PASS_SRT
1245#ifdef CREATE_INFO_RES_BATCH_SRT
1246CREATE_INFO_RES_BATCH_SRT
1248#ifdef CREATE_INFO_RES_GEOMETRY_SRT
1249CREATE_INFO_RES_GEOMETRY_SRT
1251#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
1252CREATE_INFO_RES_SHARED_VARS_SRT
1262void _method(SRT this_, int t);
1266 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
1269#if defined(CREATE_INFO_SRT)
1271 void _method(SRT this_, int t) {
1278 result.a = T_new_();
1284 auto [
output, _, error] = process_test_string(input);
1290TEST(shader_tool, StaticBranch)
1292 using namespace shader;
1293 using namespace std;
1298 [[compilation_constant]] const int use_color_band;
1301 if (use_color_band) [[static_branch]] {
1307void func([[resource_table]] Resources &srt)
1309 if (srt.use_color_band) [[static_branch]] {
1313 if (srt.use_color_band == 1) [[static_branch]] {
1319 if (srt.use_color_band) [[static_branch]] {
1321 } else if (srt.use_color_band) [[static_branch]] {
1325 if (srt.use_color_band) [[static_branch]] {
1327 } else if (srt.use_color_band) [[static_branch]] {
1333 if (srt.use_color_band) [[static_branch]] {
1334 if (srt.use_color_band) [[static_branch]] {
1341#define access_Resources_use_color_band() use_color_band
1342#ifdef CREATE_INFO_RES_PASS_Resources
1343CREATE_INFO_RES_PASS_Resources
1345#ifdef CREATE_INFO_RES_BATCH_Resources
1346CREATE_INFO_RES_BATCH_Resources
1348#ifdef CREATE_INFO_RES_GEOMETRY_Resources
1349CREATE_INFO_RES_GEOMETRY_Resources
1351#ifdef CREATE_INFO_RES_SHARED_VARS_Resources
1352CREATE_INFO_RES_SHARED_VARS_Resources
1360Resources Resources_ctor_();
1361void _fn(Resources this_);
1362Resources Resources_new_();
1365 Resources Resources_ctor_() {Resources r;r._pad=0;return r;}
1368#if defined(CREATE_INFO_Resources)
1370 void _fn(Resources this_) {
1372#if SRT_CONSTANT_use_color_band
1382 Resources Resources_new_()
1391#if defined(CREATE_INFO_Resources)
1393void func(Resources srt)
1396#if SRT_CONSTANT_use_color_band
1403#if SRT_CONSTANT_use_color_band == 1
1415#if SRT_CONSTANT_use_color_band
1420#elif SRT_CONSTANT_use_color_band
1427#if SRT_CONSTANT_use_color_band
1432#elif SRT_CONSTANT_use_color_band
1444#if SRT_CONSTANT_use_color_band
1448#if SRT_CONSTANT_use_color_band
1465 auto [
output, _, error] = process_test_string(input);
1471void func([[resource_table]] Resources &srt)
1473 if (srt.use_color_band) [[static_branch]] {
1475 } else if (srt.use_color_band) {
1480 auto [
output, _, error] = process_test_string(input);
1481 EXPECT_EQ(error,
"Expecting next if statement to also be a static branch.");
1485void func([[resource_table]] Resources &srt)
1487 if (use_color_band) [[static_branch]] {
1492 auto [
output, _, error] = process_test_string(input);
1494 "Expecting compilation or specialization constant. Make sure SRT arguments "
1495 "have the [[resource_table]] attribute.");
1499void func([[resource_table]] Resources &srt)
1501 if (srt.use_color_band && srt.use_color_band) [[static_branch]] {
1506 auto [
output, _, error] = process_test_string(input);
1507 EXPECT_EQ(error,
"Expecting single condition.");
1513 using namespace shader;
1514 using namespace std;
1529 auto [
output, _, error] = process_test_string(input);
1530 EXPECT_EQ(error,
"Call to function is ambiguous. Specify namespace to remove ambiguity.");
1535int func(int a) { return 0; }
1545int A_func(int a) { return 0; }
1548 int func = A_func();
1553 auto [
output, _, error] = process_test_string(input);
1564 return B::func(int a);
1577struct A_S { int _pad;};
1579 A_S A_S_ctor_() {A_S r;r._pad=0;return r;}
1583 return B_func(int a);
1590 return A_func(int a);
1594 auto [
output, _, error] = process_test_string(input);
1619 return A_B_func(int a);
1623 auto [
output, _, error] = process_test_string(input);
1641void A_B_b() { A_a(); }
1643void A_f() { A_B_b(); }
1646 auto [
output, _, error] = process_test_string(input);
1671 auto [
output, _, error] = process_test_string(input);
1698 auto [
output, _, error] = process_test_string(input);
1720struct A_B_S { int _pad;};
1722 A_B_S A_B_S_ctor_() {A_B_S r;r._pad=0;return r;}
1730 auto [
output, _, error] = process_test_string(input);
1738 auto [
output, _, error] = process_test_string(input);
1739 EXPECT_EQ(error,
"The `using` keyword is not allowed in global scope.");
1747 auto [
output, _, error] = process_test_string(input);
1749 "Unsupported `using namespace`. "
1750 "Add individual `using` directives for each needed symbol.");
1758 auto [
output, _, error] = process_test_string(input);
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 "
1770 auto [
output, _, error] = process_test_string(input);
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 "
1781template<typename T> T read(T a)
1785template float read<float>(float);
1786float write(float a){ return a; }
1792float NS_read(float a)
1797float NS_write(float a){ return a; }
1800 auto [
output, _, error] = process_test_string(input);
1809 static S static_method(S s) {
1812 S other_method(int s) {
1813 this->some_method();
1817} // End of namespace
1828NS_S NS_S_static_method(NS_S s);
1829NS_S _other_method(_ref(NS_S ,this_), int s);
1832 NS_S NS_S_ctor_() {NS_S r;r._pad=0;return r;}
1833 NS_S NS_S_static_method(NS_S s) {
1836 NS_S _other_method(_ref(NS_S ,this_), int s) {
1837 _some_method(this_);
1842 auto [
output, _, error] = process_test_string(input);
1850template<> Type a<Type>() {}
1859 auto [
output, _, error] = process_test_string(input);
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(); }
1874B fn() { return B::C(); }
1890int _R(_ref(NS_B ,this_));
1893 NS_B NS_B_ctor_() {NS_B r;r.i=0;return r;}
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_); }
1900NS_B NS_fn() { return NS_B_C(); }
1903 auto [
output, _, error] = process_test_string(input);
1909TEST(shader_tool, Swizzle)
1911 using namespace shader;
1912 using namespace std;
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);
1923TEST(shader_tool, BinaryLiterals)
1925 using namespace shader;
1926 using namespace std;
1929 string input = R
"(0b1 0b10u 0b10001000100010001000100010001000)";
1930 string expect = R
"(1 2u 2290649224)";
1931 auto [
output, _, error] = process_test_local(input);
1939 using namespace shader;
1940 using namespace std;
1944enum class enum_class : int {
1950constant static constexpr int enum_class_VALUE = 0;
1952#define enum_class int
1955enum_class enum_class_ctor_() { return enum_class(0); }
1961 auto [
output, _, error] = process_test_string(input);
1968enum E : int { A, B = 2, C, D = 1, E };
1971constant static constexpr int A = 0;
1973constant static constexpr int B = 2;
1975constant static constexpr int C = B + 1;
1977constant static constexpr int D = 1;
1979constant static constexpr int E = D + 1;
1984E E_ctor_() { return E(0); }
1988 auto [
output, _, error] = process_test_string(input);
1994enum class enum_class : int {
2000constant static constexpr int enum_class_VALUE = 0;
2002#define enum_class int
2005enum_class enum_class_ctor_() { return enum_class(0); }
2011 auto [
output, _, error] = process_test_string(input);
2017enum class enum_class {
2021 auto [
output, _, error] = process_test_string(input);
2022 EXPECT_EQ(error,
"enum declaration must explicitly use an underlying type");
2027TEST(shader_tool, MatrixConstructors)
2029 using namespace shader;
2030 using namespace std;
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);
2042TEST(shader_tool, ResourceGuard)
2044 using namespace shader;
2045 using namespace std;
2050 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2056#if defined(CREATE_INFO_draw_resource_id_varying)
2058 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2064 auto [
output, _, error] = process_test_string(input);
2072 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2079#if defined(CREATE_INFO_draw_resource_id_varying)
2082 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2092 auto [
output, _, error] = process_test_string(input);
2101 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2111#if defined(CREATE_INFO_draw_resource_id_varying)
2113 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2121 auto [
output, _, error] = process_test_string(input);
2130 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2131 i += buffer_get(draw_resource_id, resource_id_buf)[0];
2141#if defined(CREATE_INFO_draw_resource_id_varying)
2144#if defined(CREATE_INFO_draw_resource_id)
2146 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
2147 i += buffer_get(draw_resource_id, resource_id_buf)[0];
2157 auto [
output, _, error] = process_test_string(input);
2164template<> uint my_func<uint>(uint i) {
2165 return buffer_get(draw_resource_id, resource_id_buf)[i];
2169 uint my_funcTuint(uint i) {
2171#if defined(CREATE_INFO_draw_resource_id)
2173 return buffer_get(draw_resource_id, resource_id_buf)[i];
2182 auto [
output, _, error] = process_test_string(input);
2188TEST(shader_tool, EmptyStruct)
2190 using namespace shader;
2191 using namespace std;
2202struct S { int _pad;};
2204 S S_ctor_() {S r;r._pad=0;return r;}
2205struct T { int _pad;};
2207 T T_ctor_() {T r;r._pad=0;return r;}
2217 U U_ctor_() {U r;r._pad=0;return r;}
2221 auto [
output, _, error] = process_test_string(input);
2227TEST(shader_tool, StructuredBindings)
2229 using namespace shader;
2230 using namespace std;
2253 auto [c, d] = test();
2266 S S_ctor_() {S r;r.i=0;r.b=0.0f;return r;}
2273void fn(S u, _ref(S ,v))
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;
2289 auto [
output, _, error] = process_test_string(input);
2295TEST(shader_tool, StructMethods)
2297 using namespace shader;
2298 using namespace std;
2308 static S construct()
2327 return this->member;
2333 S s = S::construct();
2358S _function(_ref(S ,this_), int i);
2359int _size(const S this_);
2362 S S_ctor_() {S r;r.member=0;r.this_member=0;r.another_member=0;return r;}
2372 S _function(_ref(S ,this_), int i)
2375 this_.this_member++;
2379 int _size(const S this_)
2381 return this_.member;
2386 S s = S_construct();
2398 auto [
output, _, error] = process_test_string(input);
2407 float fn1() { return a; }
2408 float fn2() { int fn2; return fn1(); }
2409 static float fn3() { int a; return a; }
2421float _fn1(_ref(A ,this_));
2422float _fn2(_ref(A ,this_));
2426 A A_ctor_() {A r;r.a=0;r.b=0u;return r;}
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; }
2433 auto [
output, _, error] = process_test_string(input);
2441 float fn1(int a) { return a; }
2444 auto [
output, _, error] = process_test_string(input);
2445 EXPECT_EQ(error,
"Class member shadowing.");
2451 float fn1() { int a; return a; }
2454 auto [
output, _, error] = process_test_string(input);
2455 EXPECT_EQ(error,
"Class member shadowing.");
2467 auto [
output, _, error] = process_test_string(input);
2468 EXPECT_EQ(error,
"Method name matching swizzles accessor are forbidden.");
2472TEST(shader_tool, SrtMutations)
2474 using namespace std;
2475 using namespace shader::parser;
2479float fn([[resource_table]] SRT &srt) {
2485#if defined(CREATE_INFO_SRT)
2488 return srt_access(SRT, member);
2494 auto [
output, _, error] = process_test_string(input);
2500float fn([[resource_table]] SRT srt) {
2504 auto [
output, _, error] = process_test_string(input);
2505 EXPECT_EQ(error,
"Shader Resource Table arguments must be references.");
2509float fn([[resource_table]] SRT &srt) {
2510 [[resource_table]] OtherSRT &other_srt = srt.other_srt;
2511 return other_srt.member;
2516#if defined(CREATE_INFO_SRT)
2520 return srt_access(OtherSRT, member);
2526 auto [
output, _, error] = process_test_string(input);
2532TEST(shader_tool, EntryPointResources)
2534 using namespace std;
2535 using namespace shader::parser;
2542 [[smooth]] float3 local_pos;
2546 [[frag_color(0)]] float3 color;
2547 [[frag_color(1), index(1)]] uint test;
2552 [[attribute(0)]] T pos;
2554template struct VertIn<float>;
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)
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)
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)
2602 global_invocation_id;
2603 local_invocation_id;
2604 local_invocation_index;
2617 ns_VertOut ns_VertOut_ctor_() {ns_VertOut r;r.local_pos=float3(0);return r;}
2624 ns_FragOut ns_FragOut_ctor_() {ns_FragOut r;r.color=float3(0);r.test=0u;return r;}
2626struct ns_VertInTfloat {
2630 ns_VertInTfloat ns_VertInTfloat_ctor_() {ns_VertInTfloat r;r.pos=0.0f;return r;}
2633#if defined(CREATE_INFO_Resources)
2636#if defined(ENTRY_POINT_ns_vertex_function)
2638 void ns_vertex_function(
2642#if defined(GPU_VERTEX_SHADER)
2644 Resources srt = Resources_ctor_();
2659#if defined(CREATE_INFO_Resources)
2662#if defined(ENTRY_POINT_ns_fragment_function)
2664 void ns_fragment_function(
2668#if defined(GPU_FRAGMENT_SHADER)
2670 Resources srt = Resources_ctor_();
2674 gl_FragStencilRefARB;
2686#if defined(CREATE_INFO_Resources)
2689#if defined(ENTRY_POINT_ns_compute_function)
2691 void ns_compute_function(
2695#if defined(GPU_COMPUTE_SHADER)
2697 Resources srt = Resources_ctor_();
2698 gl_GlobalInvocationID;
2699 gl_LocalInvocationID;
2700 gl_LocalInvocationIndex;
2710 string expect_infos = R
"(#pragma once
2714GPU_SHADER_CREATE_INFO(ns_VertInTfloat)
2715VERTEX_IN(0, float, pos)
2716GPU_SHADER_CREATE_END()
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()
2725GPU_SHADER_INTERFACE_INFO(ns_VertOut_t)
2726SMOOTH(float3, ns_VertOut_local_pos)
2727GPU_SHADER_INTERFACE_END()
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()
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()
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()
2765 string infos =
metadata.serialize_infos();
2773TEST(shader_tool, PipelineDescription)
2775 using namespace std;
2776 using namespace shader::parser;
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});
2795 string expect_infos = R
"(#pragma once
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()
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()
2827 string infos =
metadata.serialize_infos();
2837 using namespace std;
2838 using namespace shader::parser;
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, }; }
2854 T t4=T{.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};
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;}; }
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() ;
2885 auto [
output, _, error] = process_test_string(input);
2892 T t9={1, T{.a=1, .b=2}.a};
2896 EXPECT_EQ(error,
"Designated initializers are only supported in assignments");
2901 T t10={1, float4{0}};
2907 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
2912 T t11={.a=1, .b=T{.a=1, .b=2}.a};
2916 EXPECT_EQ(error,
"Nested initializer lists are not supported");
2921 T t12={.a=1, .b=float4{0}};
2927 "Aggregate is error prone for built-in vector and matrix types, use constructors instead");
2933 using namespace std;
2934 using namespace shader::parser;
29561;1;1;1;1;1;1;1;1;1;1+1;)";
2957 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
2961[[a(0,1,b), c, d(t)]]
2964[[A(1,1,A),A,A(A)]])";
2965 string scopes = R
"(GABbcmmmbbcm)";
2979sA{AA=1;};SA{AA;};)";
2980 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
2995 EXPECT_EQ(Lexer(input, err_handler).token_types_str(), expect);
3004 string expect_scopes = R
"(GNN)";
3011 int i = 0, u = 2, v = {1.0f};
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);
3056match(, const, bool, , foo, , ;)
3057match([a], , int, , bar, [0], ;)
3063 parser().foreach_declaration([&](
Scope attributes,
3066 Scope template_scope,
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";
3086 using namespace shader::parser;
3092 catch (
const std::exception &
e) {
3093 std::cerr <<
"Error: " <<
e.what() <<
"\n";
3100 using namespace std;
3101 using namespace shader::parser;
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
static int test_expression(std::string str)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
Token(const TokenBuffer *buf, int32_t index)