Blender V5.3
resource_table_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, ResourceGuard)
14{
15 {
16 string input = R"(
17void my_func() {
18 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
19}
20)";
21 string expect = R"(
22void my_func() {
23
24#if defined(CREATE_INFO_draw_resource_id_varying)
25#line 3
26 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
27
28#endif
29#line 4
30}
31)";
32 auto [output, _, error] = process_test_string(input);
33 EXPECT_EQ(output, expect);
34 EXPECT_EQ(error, "");
35 }
36 {
37 string input = R"(
38uint my_func() {
39 uint i = 0;
40 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
41 return i;
42}
43)";
44 string expect = R"(
45uint my_func() {
46
47#if defined(CREATE_INFO_draw_resource_id_varying)
48#line 3
49 uint i = 0;
50 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
51 return i;
52
53#else
54#line 3
55 return uint(0);
56#endif
57
58}
59)";
60 auto [output, _, error] = process_test_string(input);
61 EXPECT_EQ(output, expect);
62 EXPECT_EQ(error, "");
63 }
64 {
65 string input = R"(
66uint my_func() {
67 uint i = 0;
68 {
69 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
70 }
71 return i;
72}
73)";
74 string expect = R"(
75uint my_func() {
76 uint i = 0;
77 {
78
79#if defined(CREATE_INFO_draw_resource_id_varying)
80#line 5
81 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
82
83#endif
84#line 6
85 }
86 return i;
88)";
89 auto [output, _, error] = process_test_string(input);
90 EXPECT_EQ(output, expect);
91 EXPECT_EQ(error, "");
92 }
93 {
94 string input = R"(
95uint my_func() {
96 uint i = 0;
97 {
98 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
99 i += buffer_get(draw_resource_id, resource_id_buf)[0];
100 }
101 return i;
102}
103)";
104 string expect = R"(
105uint my_func() {
106 uint i = 0;
107 {
108
109#if defined(CREATE_INFO_draw_resource_id_varying)
110#line 5
111
112#if defined(CREATE_INFO_draw_resource_id)
113#line 5
114 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
115 i += buffer_get(draw_resource_id, resource_id_buf)[0];
116
117#endif
118
119#endif
120#line 7
121 }
122 return i;
123}
124)";
125 auto [output, _, error] = process_test_string(input);
127 EXPECT_EQ(error, "");
128 }
129 {
130 /* Guard in template. */
131 string input = R"(
132template<> uint my_func<uint>(uint i) {
133 return buffer_get(draw_resource_id, resource_id_buf)[i];
134}
135)";
136 string expect = R"(
137 uint my_funcTuint(uint i) {
138
139#if defined(CREATE_INFO_draw_resource_id)
140#line 3
141 return buffer_get(draw_resource_id, resource_id_buf)[i];
142
143#else
144#line 3
145 return uint(0);
146#endif
147#line 4
148}
149)";
150 auto [output, _, error] = process_test_string(input);
151 EXPECT_EQ(output, expect);
152 EXPECT_EQ(error, "");
153 }
154}
155
156TEST(shader_tool, SrtMutations)
157{
158 {
159 string input = R"(
160float fn([[resource_table]] SRT &srt) {
161 return srt.member;
162}
163)";
164 string expect = R"(
165
166#if defined(CREATE_INFO_SRT)
167#line 2
168float fn(SRT srt) {
169 return srt_access(SRT, member);
170}
171
172#endif
173#line 5
174)";
175 auto [output, _, error] = process_test_string(input);
176 EXPECT_EQ(output, expect);
177 EXPECT_EQ(error, "");
178 }
179 {
180 string input = R"(
181float fn([[resource_table]] SRT srt) {
182 return srt.member;
183}
184)";
185 auto [output, _, error] = process_test_string(input);
186 EXPECT_EQ(error, "Shader Resource Table arguments must be references.");
187 }
188 {
189 string input = R"(
190float fn([[resource_table]] SRT &srt) {
191 [[resource_table]] OtherSRT &other_srt = srt.other_srt;
192 return other_srt.member;
193}
194)";
195 string expect = R"(
196
197#if defined(CREATE_INFO_SRT)
198#line 2
199float fn(SRT srt) {
200
201 return srt_access(OtherSRT, member);
202}
203
204#endif
205#line 6
206)";
207 auto [output, _, error] = process_test_string(input);
208 EXPECT_EQ(output, expect);
209 EXPECT_EQ(error, "");
210 }
211}
212
213TEST(shader_tool, SrtMethod)
214{
215 {
216 string input = R"(
217struct SRT {
218 [[resource_table]] srt_t<T> a;
219
220 void method(int t) {
221 this->a;
222 }
223};
224)";
225 string expect = R"(
226#define access_SRT_a() T_ctor_()
227#ifdef CREATE_INFO_RES_PASS_SRT
228CREATE_INFO_RES_PASS_SRT
229#endif
230#ifdef CREATE_INFO_RES_BATCH_SRT
231CREATE_INFO_RES_BATCH_SRT
232#endif
233#ifdef CREATE_INFO_RES_GEOMETRY_SRT
234CREATE_INFO_RES_GEOMETRY_SRT
235#endif
236#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
237CREATE_INFO_RES_SHARED_VARS_SRT
238#endif
239#line 2
240struct SRT {
241 T a;
242#line 16
243};
244
245#ifndef GPU_METAL
246SRT SRT_ctor_();
247void _method(SRT this_, int t);
248SRT SRT_new_();
249#endif
250#line 2
251 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
252
253
254
255#if defined(CREATE_INFO_SRT)
256#line 5
257 void _method(SRT this_, int t) {
258 srt_access(SRT, a);
259 }
260#endif
261 SRT SRT_new_()
262{
263 SRT result;
264 result.a = T_new_();
265 return result;
266#line 7
267}
268
269)";
270 auto [output, _, error] = process_test_string(input);
271 EXPECT_EQ(output, expect);
272 EXPECT_EQ(error, "");
273 }
274}
275
276TEST(shader_tool, SrtCompilationConstant)
277{
278 {
279 string input = R"(
280struct A {
281 [[compilation_constant]] uint a;
282};
283struct B {
284 [[resource_table]] A srt;
285 [[compilation_constant]] uint a;
286};
287)";
288 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
289 EXPECT_EQ(error, "Redefinition of compilation constant 'a'");
290 }
291}
292
293TEST(shader_tool, SrtTemplateWrapper)
294{
295 {
296 string input = R"(
297struct SRT {
298 [[resource_table]] srt_t<T> a;
299};
300)";
301 string expect = R"(
302#define access_SRT_a() T_ctor_()
303#ifdef CREATE_INFO_RES_PASS_SRT
304CREATE_INFO_RES_PASS_SRT
305#endif
306#ifdef CREATE_INFO_RES_BATCH_SRT
307CREATE_INFO_RES_BATCH_SRT
308#endif
309#ifdef CREATE_INFO_RES_GEOMETRY_SRT
310CREATE_INFO_RES_GEOMETRY_SRT
311#endif
312#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
313CREATE_INFO_RES_SHARED_VARS_SRT
314#endif
315#line 2
316struct SRT {
317 T a;
318#line 12
319};
320
321#ifndef GPU_METAL
322SRT SRT_ctor_();
323SRT SRT_new_();
324#endif
325#line 2
326 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
327
328
329 SRT SRT_new_()
330{
331 SRT result;
332 result.a = T_new_();
333 return result;
334#line 3
335}
336
337)";
338 auto [output, _, error] = process_test_string(input);
339 EXPECT_EQ(output, expect);
340 EXPECT_EQ(error, "");
341 }
342 {
343 string input = R"(
344struct SRT {
345 [[resource_table]] T a;
346};
347)";
348 auto [output, _, error] = process_test_string(input);
349 EXPECT_EQ(error,
350 "Members declared with the [[resource_table]] attribute must wrap their type "
351 "with the srt_t<T> template.");
352 }
353 {
354 string input = R"(
355struct SRT {
356 srt_t<T> a;
357};
358)";
359 auto [output, _, error] = process_test_string(input);
360 EXPECT_EQ(error,
361 "The srt_t<T> template is only to be used with members declared with the "
362 "[[resource_table]] attribute.");
363 }
364 {
365 string input = R"(
366struct SRT {
367 [[resource_table]] srt_t<T> a[4];
368};
369)";
370 auto [output, _, error] = process_test_string(input);
371 EXPECT_EQ(error, "[[resource_table]] members cannot be arrays.");
372 }
373}
374
375TEST(shader_tool, EntryPointResources)
376{
377 {
378 string input = R"(
379namespace ns {
380
381struct VertOut {
382 [[smooth]] float3 local_pos;
383};
384
385struct FragOut {
386 [[frag_color(0)]] float3 color;
387 [[frag_color(1), index(1)]] uint test;
388};
389
390template<typename T>
391struct VertIn {
392 [[attribute(0)]] T pos;
393};
394template struct VertIn<float>;
395
396
397[[vertex]] void vertex_function([[resource_table]] Resources &srt,
398 [[in]] const VertIn<float> &v_in,
399 [[out, condition(cond)]] VertOut &v_out,
400 [[base_instance]] const int &base_instance,
401 [[point_size]] float &point_size,
402 [[clip_distance]] float (&clip_distance)[6],
403 [[layer]] int &layer,
404 [[viewport_index]] int &viewport_index,
405 [[position]] float4 &out_position)
406{
407 base_instance;
408 point_size;
409 clip_distance;
410 layer;
411 viewport_index;
412 out_position;
413}
414
415[[fragment]] void fragment_function([[resource_table]] Resources &srt,
416 [[in, condition(cond)]] const VertOut &v_out,
417 [[out]] FragOut &frag_out,
418 [[frag_depth(greater)]] float depth,
419 [[frag_stencil_ref]] int stencil,
420 [[layer]] const int &layer,
421 [[viewport_index]] const int &viewport_index,
422 [[point_coord]] const float2 pt_co,
423 [[front_facing]] const bool facing,
424 [[frag_coord]] const float4 in_position)
425{
426 layer;
427 viewport_index;
428 depth;
429 stencil;
430 in_position;
431 pt_co;
432 facing;
433}
434
435[[compute]] void compute_function([[resource_table]] Resources &srt,
436 [[global_invocation_id]] const uint3 &global_invocation_id,
437 [[local_invocation_id]] const uint3 &local_invocation_id,
438 [[local_invocation_index]] const uint &local_invocation_index,
439 [[work_group_id]] const uint3 &workgroup_id,
440 [[num_work_groups]] const uint3 &num_work_groups)
441{
442 global_invocation_id;
443 local_invocation_id;
444 local_invocation_index;
445 workgroup_id;
446 num_work_groups;
447}
448
449}
450)";
451 string expect = R"(
452
453
454struct ns_VertOut {
455 float3 local_pos;
456};
457#line 4
458 ns_VertOut ns_VertOut_ctor_() {ns_VertOut r;r.local_pos=float3(0);return r;}
459#line 8
460struct ns_FragOut {
461 float3 color;
462 uint test;
463};
464#line 8
465 ns_FragOut ns_FragOut_ctor_() {ns_FragOut r;r.color=float3(0);r.test=0u;return r;}
466#line 14
467struct ns_VertInTfloat {
468 float pos;
469};
470#line 14
471 ns_VertInTfloat ns_VertInTfloat_ctor_() {ns_VertInTfloat r;r.pos=0.0f;return r;}
472#line 20
473
474#if defined(CREATE_INFO_Resources)
475#line 20
476
477#if defined(ENTRY_POINT_ns_vertex_function)
478#line 20
479 void ns_vertex_function(
480#line 28
481 )
482{
483#if defined(GPU_VERTEX_SHADER)
484#line 29
485 Resources srt = Resources_ctor_();
486 gpu_BaseInstance;
487 gl_PointSize;
488 gl_ClipDistance;
489 gl_Layer;
490 gpu_ViewportIndex;
491 gl_Position;
492
493#endif
494#line 36
495}
496#endif
497#endif
498#line 38
499
500#if defined(CREATE_INFO_Resources)
501#line 38
502
503#if defined(ENTRY_POINT_ns_fragment_function)
504#line 38
505 void ns_fragment_function(
506#line 47
507 )
508{
509#if defined(GPU_FRAGMENT_SHADER)
510#line 48
511 Resources srt = Resources_ctor_();
512 gl_Layer;
513 gpu_ViewportIndex;
514 gl_FragDepth;
515 gl_FragStencilRefARB;
516 gl_FragCoord;
517 gl_PointCoord;
518 gl_FrontFacing;
519
520#endif
521#line 56
522}
523#endif
524#endif
525#line 58
526
527#if defined(CREATE_INFO_Resources)
528#line 58
529
530#if defined(ENTRY_POINT_ns_compute_function)
531#line 58
532 void ns_compute_function(
533#line 63
534 )
535{
536#if defined(GPU_COMPUTE_SHADER)
537#line 64
538 Resources srt = Resources_ctor_();
539 gl_GlobalInvocationID;
540 gl_LocalInvocationID;
541 gl_LocalInvocationIndex;
542 gl_WorkGroupID;
543 gl_NumWorkGroups;
544
545#endif
546#line 70
547}
548#endif
549#endif
550)";
551 string expect_infos = R"(#pragma once
552
553
554
555GPU_SHADER_CREATE_INFO(ns_VertInTfloat)
556.vertex_in(0, Type::float_t, "pos")
557GPU_SHADER_CREATE_END()
558
559
560GPU_SHADER_CREATE_INFO(ns_FragOut)
561FRAGMENT_OUT(0, float3, ns_FragOut_color)
562FRAGMENT_OUT_DUAL(1, uint, ns_FragOut_test, SRC_1)
563GPU_SHADER_CREATE_END()
564
565
566GPU_SHADER_INTERFACE_INFO(ns_VertOut_t)
567SMOOTH(float3, ns_VertOut_local_pos)
568GPU_SHADER_INTERFACE_END()
569
570
571
572GPU_SHADER_CREATE_INFO(ns_vertex_function_infos_)
573.additional_info_with_condition("Resources")
574ADDITIONAL_INFO(ns_VertInTfloat)
575.vertex_out(ns_VertOut_t, [](blender::Span<CompilationConstant> constants) { int cond = ShaderCreateInfo::find_constant(constants, "cond"); return cond;})
576BUILTINS(BuiltinBits::INSTANCE_ID)
577BUILTINS(BuiltinBits::POINT_SIZE)
578.builtins(BuiltinBits::LAYER)
579.builtins(BuiltinBits::VIEWPORT_INDEX)
580.builtins(BuiltinBits::CLIP_DISTANCES)
581GPU_SHADER_CREATE_END()
582
583GPU_SHADER_CREATE_INFO(ns_fragment_function_infos_)
584DEPTH_WRITE(DepthWrite::GREATER)
585BUILTINS(BuiltinBits::STENCIL_REF)
586BUILTINS(BuiltinBits::POINT_COORD)
587BUILTINS(BuiltinBits::FRONT_FACING)
588BUILTINS(BuiltinBits::FRAG_COORD)
589.additional_info_with_condition("Resources")
590.additional_info_with_condition("ns_FragOut")
591.builtins(BuiltinBits::LAYER)
592.builtins(BuiltinBits::VIEWPORT_INDEX)
593GPU_SHADER_CREATE_END()
594
595GPU_SHADER_CREATE_INFO(ns_compute_function_infos_)
596.additional_info_with_condition("Resources")
597BUILTINS(BuiltinBits::GLOBAL_INVOCATION_ID)
598BUILTINS(BuiltinBits::LOCAL_INVOCATION_ID)
599BUILTINS(BuiltinBits::LOCAL_INVOCATION_INDEX)
600BUILTINS(BuiltinBits::WORK_GROUP_ID)
601BUILTINS(BuiltinBits::NUM_WORK_GROUP)
602GPU_SHADER_CREATE_END()
603
604)";
605 auto [output, metadata, error] = process_test_string(input);
606 string infos = metadata.serialize_infos();
607
608 EXPECT_EQ(output, expect);
609 EXPECT_EQ(infos, expect_infos);
610 EXPECT_EQ(error, "");
611 }
612}
613
614TEST(shader_tool, ConditionalLinting)
615{
616 using namespace std;
617 using namespace shader::parser;
618
619 {
620 string input = R"(
621void foo([[condition(false)]] float a) {}
622)";
623 auto [output, metadata, error] = process_test_string(input, shader::Language::BSL);
624 EXPECT_EQ(error, "Condition attribute is only allowed on resources declaration");
625 }
626 {
627 string input = R"(
628struct foo { [[condition(false)]] float a; };
629)";
630 auto [output, metadata, error] = process_test_string(input, shader::Language::BSL);
631 EXPECT_EQ(error, "Condition attribute is only allowed on resources declaration");
632 }
633 {
634 string input = R"(
635struct foo { [[condition(false)]] [[shared]] float a; };
636)";
637 auto [output, metadata, error] = process_test_string(input, shader::Language::BSL);
638 EXPECT_EQ(error, "Condition attribute is not compatible with resource type 'SHARED'");
639 }
640 {
641 string input = R"(
642struct foo { [[condition(false)]] [[push_constant]] float a; };
643)";
644 auto [output, metadata, error] = process_test_string(input, shader::Language::BSL);
645 EXPECT_EQ(error, "");
646 }
647}
648
649TEST(shader_tool, PipelineDescription)
650{
651 using namespace std;
652 using namespace shader::parser;
653
654 {
655 string input = R"(
656namespace ns {
657
658PipelineGraphic graphic_pipe(vertex_func, fragment_func, Type{.a = true, .b = 9, .c = 3u});
659PipelineCompute compute_pipe(compute_func, Type{.a = true, .b = 8, .c = 7u});
660
661}
662)";
663 string expect = R"(
664
665
666
667
668
669
670)";
671 string expect_infos = R"(#pragma once
672
673
674
675
676
677
678
679GPU_SHADER_CREATE_INFO(ns_graphic_pipe)
680GRAPHIC_SOURCE("test.bsl")
681VERTEX_FUNCTION("vertex_func")
682FRAGMENT_FUNCTION("fragment_func")
683ADDITIONAL_INFO(vertex_func_infos_)
684ADDITIONAL_INFO(fragment_func_infos_)
685COMPILATION_CONSTANT(bool, a, true)
686COMPILATION_CONSTANT(int, b, 9)
687COMPILATION_CONSTANT(uint, c, 3u)
688DO_STATIC_COMPILATION()
689GPU_SHADER_CREATE_END()
690
691GPU_SHADER_CREATE_INFO(ns_compute_pipe)
692COMPUTE_SOURCE("test.bsl")
693COMPUTE_FUNCTION("compute_func")
694ADDITIONAL_INFO(compute_func_infos_)
695COMPILATION_CONSTANT(bool, a, true)
696COMPILATION_CONSTANT(int, b, 8)
697COMPILATION_CONSTANT(uint, c, 7u)
698DO_STATIC_COMPILATION()
699GPU_SHADER_CREATE_END()
700
701)";
702 auto [output, metadata, error] = process_test_string(input);
703 string infos = metadata.serialize_infos();
704
705 EXPECT_EQ(output, expect);
706 EXPECT_EQ(infos, expect_infos);
707 EXPECT_EQ(error, "");
708 }
709}
710
711} // namespace blender::gpu::tests
#define output
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
TEST(array_store, Nop)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)