Blender V5.3
templates_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7namespace blender::gpu::tests {
8
9using namespace shader::parser;
10using namespace shader;
11using namespace std;
12
13TEST(shader_tool, Template)
14{
15 {
16 string input = R"(
17template<typename T>
18void func() { T::fn(); }
19template void func<A>();
20)";
21 string expect = R"(
22
23void funcTA() { A_fn(); }
24
25)";
26 auto [output, _, error] = process_test_string(input);
27 EXPECT_EQ(output, expect);
28 EXPECT_EQ(error, "");
29 }
30 {
31 string input = R"(
32template<typename T> void f(T a) {}
33struct A {};
34template void f<A>(A);
35)";
36 auto [output, _, error] = process_test_string(input);
37 EXPECT_EQ(error, "");
38 }
39 {
40 string input = R"(
41template<typename T>
42void func() { T a; }
43template void func<float>();
44)";
45 string expect = R"(
46
47void funcTfloat() {
48#line 3
49 float a;
50#line 3
51 }
52
53)";
54 auto [output, _, error] = process_test_string(input, Language::BSL);
55 EXPECT_EQ(output, expect);
56 EXPECT_EQ(error, "");
57 }
58 {
59 string input = R"(
60template<int U>
61int func() { return U; }
62template void func<1 + 3>();
63)";
64 string expect = R"(
65
66int funcT4() {
67#line 3
68 return 4; }
69
70)";
71 auto [output, _, error] = process_test_string(input, Language::BSL);
72 EXPECT_EQ(output, expect);
73 EXPECT_EQ(error, "");
74 }
75 {
76 /* CRTP. */
77 string input = R"(
78template<typename T> struct A {
79 void f(T t)
80 {
81 t.f();
82 }
83};
84
85struct U {
86 void f() {}
87};
88
89template struct A<U>;
90)";
91 string expect = R"(#line 9
92struct U {
93 int _pad;
94};
95#line 9
96U U_ctor_() {U r;r._pad=0;return r;}
97
98#ifndef GPU_METAL
99void _f(_ref(U ,this_));
100#endif
101#line 10
102 void _f(_ref(U ,this_)) {
103#line 10
104 }
105#line 2
106 struct ATU {
107 int _pad;
108#line 7
109};
110#line 2
111ATU ATU_ctor_() {ATU r;r._pad=0;return r;}
112
113#ifndef GPU_METAL
114void _f(_ref(ATU ,this_), U _0);
115#endif
116#line 3
117 void _f(_ref(ATU ,this_), U t)
118 {
119 _f(t);
120 }
121#line 15
122)";
123 auto [output, _, error] = process_test_string(input, Language::BSL);
124 EXPECT_EQ(output, expect);
125 EXPECT_EQ(error, "");
126 }
127 {
128 string input = R"(
129template<typename T>
130void func(T a) {a;}
131template void func<float>(float a);
132template<typename T>
133void foo(T &a) {a;}
134template void foo<float>(float &a);
135
136void f(float a)
137{
138 func(a);
139 foo(a);
140}
141)";
142 string expect = R"(
143
144void func(float a) {a;}
145
146
147void foo(_ref(float ,a)) {a;}
148
149
150void f(float a)
151{
152 func(a);
153 foo(a);
154}
155)";
156 auto [output, _, error] = process_test_string(input);
157 EXPECT_EQ(output, expect);
158 EXPECT_EQ(error, "");
159 }
160 /* Disabled until BSL 5.3 is default. */
161 if (false) {
162 string input = R"(
163template<typename T> void foo(T a) { a; }
164template void foo<float>(float a);
165template void foo<int>(int a);
166template<> void foo<uint>(uint a) {}
167
168void f(float a, int b, uint c)
169{
170 foo(a);
171 foo(b);
172 foo(c);
173 foo<float>(a);
174 foo<int>(b);
175 foo<uint>(c);
176}
177)";
178 string expect = R"(
179 void fooTfloat(float a) {
180#line 2
181 a; }
182#line 2
183 void fooTint(int a) {
184#line 2
185 a; }
186
187
188 void fooTuint(uint a) { }
189
190void f(float a, int b, uint c)
191{
192 fooTfloat(a);
193 fooTint(b);
194 fooTuint(c);
195 fooTfloat(a);
196 fooTint(b);
197 fooTuint(c);
198}
199)";
200 auto [output, _, error] = process_test_string(input, Language::BSL);
201 EXPECT_EQ(output, expect);
202 EXPECT_EQ(error, "");
203 }
204 {
205 string input = R"(
206template<typename T, int i>
207void func(T a) {
208 a;
209}
210template void func<float, 1>(float a);
211)";
212 string expect = R"(
213
214void funcTfloatT1(float a) {
215 a;
216}
217
218)";
219 auto [output, _, error] = process_test_string(input);
220 EXPECT_EQ(output, expect);
221 EXPECT_EQ(error, "");
222 }
223 {
224 string input = R"(
225enum E : char { A, B };
226template<int i, uint j, int k> int foo() { return int(i + j + k); }
227template int foo<0xF + 2, 2, -1 - 2>();
228template<enum E e, char i> E f() { return e; }
229template E f<B, 2>();
230
231int bar()
232{
233 foo<0xF + 2, 2, -1 - 2>();
234 f<B, 1 + 1>();
235}
236)";
237 string expect = R"(
238
239#define E char
240#line 2
241static constexpr char A = 0;
242#line 2
243static constexpr char B = 1;
244 int fooT17T2T_3() {
245#line 3
246 return int(17 + 2 + -3);
247#line 3
248 }
249
250 E fT1T2() {
251#line 5
252 return 1; }
253
254
255int bar()
256{
257 fooT17T2T_3();
258 fT1T2();
259}
260)";
261 auto [output, _, error] = process_test_string(input, Language::BSL);
262 EXPECT_EQ(output, expect);
263 EXPECT_EQ(error, "");
264 }
265 {
266 string input = R"(
267template<int i, uint j, int k> E func() { return E(i + j + k); }
268template E func<0x1, 2, -1>();
269)";
270 string expect = R"(
271E funcT0x1T2T_1() { return E(0x1 + 2 + -1); }
272
273)";
274 auto [output, _, error] = process_test_string(input);
275 EXPECT_EQ(output, expect);
276 EXPECT_EQ(error, "");
277 }
278 {
279 string input = R"(
280template<enum E e, char i> E func() { return E(e + i); }
281template E func<v, 2>();
282)";
283 string expect = R"(
284E funcTvT2() { return E(v + 2); }
285
286)";
287 auto [output, _, error] = process_test_string(input);
288 EXPECT_EQ(output, expect);
289 EXPECT_EQ(error, "");
290 }
291 {
292 string input = R"(
293template<> void func<T, Q>(T a) {a}
294)";
295 string expect = R"(
296 void funcTTTQ(T a) {a}
297)";
298 auto [output, _, error] = process_test_string(input);
299 EXPECT_EQ(output, expect);
300 EXPECT_EQ(error, "");
301 }
302 {
303 string input = R"(template<typename T, int i = 0> void func(T a) {a;})";
304 auto [output, _, error] = process_test_string(input);
305 EXPECT_EQ(error, "Default arguments are not supported inside template declaration");
306 }
307 {
308 string input = R"(template void func(float a);)";
309 auto [output, _, error] = process_test_string(input);
310 EXPECT_EQ(error,
311 "Template instantiation and specialization require explicit template arguments");
312 }
313 {
314 string input = R"(template A<f> fn(A<f> a);)";
315 auto [output, _, error] = process_test_string(input);
316 EXPECT_EQ(error,
317 "Template instantiation and specialization require explicit template arguments");
318 }
319 {
320 string input = R"(template<> A fn(A a) {})";
321 auto [output, _, error] = process_test_string(input);
322 EXPECT_EQ(error,
323 "Template instantiation and specialization require explicit template arguments");
324 }
325 {
326 string input = R"(template<> A<f> fn(A<f> a) {})";
327 auto [output, _, error] = process_test_string(input);
328 EXPECT_EQ(error,
329 "Template instantiation and specialization require explicit template arguments");
330 }
331 {
332 string input = R"(template<typename T, int i = 0> void func(T a) {a;})";
333 auto [output, _, error] = process_test_string(input, Language::BSL);
334 EXPECT_EQ(error, "Default arguments are not supported inside template declaration");
335 }
336 {
337 string input = R"(template void func(float a);)";
338 auto [output, _, error] = process_test_string(input, Language::BSL);
339 EXPECT_EQ(error, "Expected template arguments");
340 }
341 {
342 string input = R"(template A<f> fn(A<f> a);)";
343 auto [output, _, error] = process_test_string(input, Language::BSL);
344 EXPECT_EQ(error, "Expected template arguments");
345 }
346 {
347 string input = R"(template<> A fn(A a) {})";
348 auto [output, _, error] = process_test_string(input, Language::BSL);
349 EXPECT_EQ(error, "Expected template arguments");
350 }
351 {
352 string input = R"(template<> A<f> fn(A<f> a) {})";
353 auto [output, _, error] = process_test_string(input, Language::BSL);
354 EXPECT_EQ(error, "Expected template arguments");
355 }
356 {
357 string input = R"(func<float, 1>(a);)";
358 string expect = R"(funcTfloatT1(a);)";
359 auto [output, _, error] = process_test_local(input);
360 EXPECT_EQ(output, expect);
361 EXPECT_EQ(error, "");
362 }
363 {
364 string input = R"(a.template func<float, 1>(a);)";
365 string expect = R"(_funcTfloatT1(a, a);)";
366 auto [output, _, error] = process_test_local(input);
367 EXPECT_EQ(output, expect);
368 EXPECT_EQ(error, "");
369 }
370 {
371 string input = R"(this->template func<float, 1>(a);)";
372 string expect = R"(_funcTfloatT1(this_, a);)";
373 auto [output, _, error] = process_test_local(input);
374 EXPECT_EQ(output, expect);
375 EXPECT_EQ(error, "");
376 }
377 {
378 string input = R"(A<B<1, 2>, C<1, D<T, -1>>> a;)";
379 string expect = R"(ATBT1T2TCT1TDTTT_1 a;)";
380 auto [output, _, error] = process_test_local(input);
381 EXPECT_EQ(output, expect);
382 EXPECT_EQ(error, "");
383 }
384 {
385 string input = R"(
386struct T {};
387template<int T, int U> struct B {};
388template<int T, typename U> struct C {};
389template<typename T, int U> struct D {};
390template<typename T, typename U> struct A {};
391
392template struct B<1, 2>;
393template struct D<T, -1>;
394template struct C<1, D<T, -1>>;
395template struct A<B<1, 2>, C<1, D<T, -1>>>;
396
397A<B<1, 2>, C<1, D<T, -1>>> a;
398)";
399 string expect = R"(
400struct T {int _pad;
401#line 2
402 };
403#line 2
404T T_ctor_() {T r;r._pad=0;return r;}
405 struct BT1T2 {int _pad;
406#line 3
407 };
408#line 3
409BT1T2 BT1T2_ctor_() {BT1T2 r;r._pad=0;return r;}
410
411 struct DTTT_1 {int _pad;
412#line 5
413 };
414#line 5
415DTTT_1 DTTT_1_ctor_() {DTTT_1 r;r._pad=0;return r;}
416#line 4
417 struct CT1TDTTT_1 {int _pad;
418#line 4
419 };
420#line 4
421CT1TDTTT_1 CT1TDTTT_1_ctor_() {CT1TDTTT_1 r;r._pad=0;return r;}
422
423 struct ATBT1T2TCT1TDTTT_1 {int _pad;
424#line 6
425 };
426#line 6
427ATBT1T2TCT1TDTTT_1 ATBT1T2TCT1TDTTT_1_ctor_() {ATBT1T2TCT1TDTTT_1 r;r._pad=0;return r;}
428#line 13
429ATBT1T2TCT1TDTTT_1 a;
430)";
431 auto [output, _, error] = process_test_string(input, Language::BSL);
432 EXPECT_EQ(output, expect);
433 EXPECT_EQ(error, "");
434 }
435}
436
437TEST(shader_tool, TemplateStruct)
438{
439 {
440 string input = R"(
441template<typename T>
442struct A {
443 T a;
444 A method(T b) const
445 {
446 return A<T>{b};
447 }
448};
449template struct A<float>;
450)";
451 string expect = R"(
452
453struct ATfloat {
454 float a;
455#line 9
456};
457
458
459#ifndef GPU_METAL
460ATfloat ATfloat_ctor_();
461ATfloat _method(const ATfloat this_, float b);
462#endif
463#line 3
464 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
465
466 ATfloat _method(const ATfloat this_, float b)
467 {
468 return _ctor(ATfloat) b _rotc() ;
469 }
470
471
472)";
473 auto [output, _, error] = process_test_string(input);
474 EXPECT_EQ(output, expect);
475 EXPECT_EQ(error, "");
476 }
477 {
478 string input = R"(
479template<typename T>
480struct A { T a; };
481template struct A<float>;
482)";
483 string expect = R"(
484
485struct ATfloat { float a; };
486#line 3
487 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
488
489)";
490 auto [output, _, error] = process_test_string(input);
491 EXPECT_EQ(output, expect);
492 EXPECT_EQ(error, "");
493 }
494 {
495 string input = R"(
496template<> struct A<float>{
497 float a;
498};
499)";
500 string expect = R"(
501 struct ATfloat{
502 float a;
503};
504#line 2
505 ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
506
507
508)";
509 auto [output, _, error] = process_test_string(input);
510 EXPECT_EQ(output, expect);
511 EXPECT_EQ(error, "");
512 }
513 {
514 string input = R"(
515void func(A<float> a) {}
516)";
517 string expect = R"(
518void func(ATfloat a) {}
519)";
520 auto [output, _, error] = process_test_string(input);
521 EXPECT_EQ(output, expect);
522 EXPECT_EQ(error, "");
523 }
524 {
525 /* Struct templated methods. */
526 string input = R"(
527namespace N {
528
529template<typename B> struct A {
530 B i;
531 template<typename T> static void fn1(T a) {}
532 template<typename T> static T fn2() { return T(0); }
533 template<typename T> void fn3(T a) { i += int(fn4<T>()); }
534 template<typename T> T fn4() { fn3(0); return T(0); }
535};
536
537template struct A<int>;
538
539template void A<int>::fn1<int>(int);
540template int A<int>::fn2<int>();
541template void A<int>::fn3<int>(int);
542template int A<int>::fn4<int>();
543
544void fn(A<int> a)
545{
546 A<int>::fn1(0);
547 A<int>::fn2<int>();
548 a.fn3(0);
549 a.fn4<int>();
550}
551
552}
553)";
554 string expect = R"(
555
556
557struct N_ATint {
558 int i;
559#line 10
560};
561#line 14
562#ifndef GPU_METAL
563N_ATint N_ATint_ctor_();
564void N_ATint_fn1(int a);
565int N_ATint_fn2Tint();
566void _fn3(_ref(N_ATint ,this_), int a);
567int _fn4Tint(_ref(N_ATint ,this_));
568#endif
569#line 4
570 N_ATint N_ATint_ctor_() {N_ATint r;r.i=0;return r;}
571
572 void N_ATint_fn1(int a) {}
573 int N_ATint_fn2Tint() { return int(0); }
574void _fn3(_ref(N_ATint ,this_), int a) { this_.i += int(_fn4Tint(this_)); }
575int _fn4Tint(_ref(N_ATint ,this_)) { _fn3(this_, 0); return int(0); }
576#line 19
577void N_fn(N_ATint a)
578{
579 N_ATint_fn1(0);
580 N_ATint_fn2Tint();
581 _fn3(a, 0);
582 _fn4Tint(a);
583}
584
585
586)";
587 auto [output, _, error] = process_test_string(input);
588 EXPECT_EQ(output, expect);
589 EXPECT_EQ(error, "");
590 }
591 {
592 string input = R"(
593template<typename T> struct A {
594 T a;
595 A method(T b) const
596 {
597 A<T> a;
598 A c;
599 a.a = b;
600 c.a = b;
601 return a;
602 }
603};
604template struct A<float>;
605)";
606 string expect = R"(
607 struct ATfloat {
608 float a;
609#line 12
610};
611#line 2
612ATfloat ATfloat_ctor_() {ATfloat r;r.a=0.0f;return r;}
613
614#ifndef GPU_METAL
615ATfloat _method(ATfloat this_, float _0);
616#endif
617#line 4
618 ATfloat _method(ATfloat this_, float b)
619 {
620 ATfloat a;
621 ATfloat c;
622 a.a = b;
623 c.a = b;
624 return a;
625 }
626#line 15
627)";
628 auto [output, _, error] = process_test_string(input, Language::BSL);
629 EXPECT_EQ(output, expect);
630 EXPECT_EQ(error, "");
631 }
632
633 {
634 string input = R"(
635template<int i> struct T {};
636
637template<> struct T<0> {
638 int b[1];
639};
640
641int f(T<0> a)
642{
643 return a.b[1];
644}
645)";
646 string expect = R"(#line 4
647 struct TT0 {
648 int b[1];
649};
650#line 4
651TT0 TT0_ctor_() {TT0 r;for(int a =0;a < 1;++a) {r.b[a]=0;}return r;}
652#line 8
653int f(TT0 a)
654{
655 return a.b[1];
656}
657)";
658 auto [output, _, error] = process_test_string(input, Language::BSL);
659 EXPECT_EQ(output, expect);
660 EXPECT_EQ(error, "");
661 }
662}
663
664} // namespace blender::gpu::tests
#define output
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
@ Template
Definition types.hh:119