9using namespace shader::parser;
10using namespace shader;
13TEST(shader_tool, ResourceGuard)
18 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
24#if defined(CREATE_INFO_draw_resource_id_varying)
26 interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
32 auto [
output, _, error] = process_test_string(input);
40 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
47#if defined(CREATE_INFO_draw_resource_id_varying)
50 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
60 auto [
output, _, error] = process_test_string(input);
69 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
79#if defined(CREATE_INFO_draw_resource_id_varying)
81 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
89 auto [
output, _, error] = process_test_string(input);
98 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
99 i += buffer_get(draw_resource_id, resource_id_buf)[0];
109#if defined(CREATE_INFO_draw_resource_id_varying)
112#if defined(CREATE_INFO_draw_resource_id)
114 i += interface_get(draw_resource_id_varying, drw_ResourceID_iface).resource_id;
115 i += buffer_get(draw_resource_id, resource_id_buf)[0];
125 auto [
output, _, error] = process_test_string(input);
132template<> uint my_func<uint>(uint i) {
133 return buffer_get(draw_resource_id, resource_id_buf)[i];
137 uint my_funcTuint(uint i) {
139#if defined(CREATE_INFO_draw_resource_id)
141 return buffer_get(draw_resource_id, resource_id_buf)[i];
150 auto [
output, _, error] = process_test_string(input);
156TEST(shader_tool, SrtMutations)
160float fn([[resource_table]] SRT &srt) {
166#if defined(CREATE_INFO_SRT)
169 return srt_access(SRT, member);
175 auto [
output, _, error] = process_test_string(input);
181float fn([[resource_table]] SRT srt) {
185 auto [
output, _, error] = process_test_string(input);
186 EXPECT_EQ(error,
"Shader Resource Table arguments must be references.");
190float fn([[resource_table]] SRT &srt) {
191 [[resource_table]] OtherSRT &other_srt = srt.other_srt;
192 return other_srt.member;
197#if defined(CREATE_INFO_SRT)
201 return srt_access(OtherSRT, member);
207 auto [
output, _, error] = process_test_string(input);
213TEST(shader_tool, SrtMethod)
218 [[resource_table]] srt_t<T> a;
226#define access_SRT_a() T_ctor_()
227#ifdef CREATE_INFO_RES_PASS_SRT
228CREATE_INFO_RES_PASS_SRT
230#ifdef CREATE_INFO_RES_BATCH_SRT
231CREATE_INFO_RES_BATCH_SRT
233#ifdef CREATE_INFO_RES_GEOMETRY_SRT
234CREATE_INFO_RES_GEOMETRY_SRT
236#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
237CREATE_INFO_RES_SHARED_VARS_SRT
247void _method(SRT this_, int t);
251 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
255#if defined(CREATE_INFO_SRT)
257 void _method(SRT this_, int t) {
270 auto [
output, _, error] = process_test_string(input);
276TEST(shader_tool, SrtCompilationConstant)
281 [[compilation_constant]] uint a;
284 [[resource_table]] A srt;
285 [[compilation_constant]] uint a;
289 EXPECT_EQ(error,
"Redefinition of compilation constant 'a'");
293TEST(shader_tool, SrtTemplateWrapper)
298 [[resource_table]] srt_t<T> a;
302#define access_SRT_a() T_ctor_()
303#ifdef CREATE_INFO_RES_PASS_SRT
304CREATE_INFO_RES_PASS_SRT
306#ifdef CREATE_INFO_RES_BATCH_SRT
307CREATE_INFO_RES_BATCH_SRT
309#ifdef CREATE_INFO_RES_GEOMETRY_SRT
310CREATE_INFO_RES_GEOMETRY_SRT
312#ifdef CREATE_INFO_RES_SHARED_VARS_SRT
313CREATE_INFO_RES_SHARED_VARS_SRT
326 SRT SRT_ctor_() {SRT r;r.a=T_ctor_();return r;}
338 auto [
output, _, error] = process_test_string(input);
345 [[resource_table]] T a;
348 auto [
output, _, error] = process_test_string(input);
350 "Members declared with the [[resource_table]] attribute must wrap their type "
351 "with the srt_t<T> template.");
359 auto [
output, _, error] = process_test_string(input);
361 "The srt_t<T> template is only to be used with members declared with the "
362 "[[resource_table]] attribute.");
367 [[resource_table]] srt_t<T> a[4];
370 auto [
output, _, error] = process_test_string(input);
371 EXPECT_EQ(error,
"[[resource_table]] members cannot be arrays.");
375TEST(shader_tool, EntryPointResources)
382 [[smooth]] float3 local_pos;
386 [[frag_color(0)]] float3 color;
387 [[frag_color(1), index(1)]] uint test;
392 [[attribute(0)]] T pos;
394template struct VertIn<float>;
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)
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)
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)
442 global_invocation_id;
444 local_invocation_index;
458 ns_VertOut ns_VertOut_ctor_() {ns_VertOut r;r.local_pos=float3(0);return r;}
465 ns_FragOut ns_FragOut_ctor_() {ns_FragOut r;r.color=float3(0);r.test=0u;return r;}
467struct ns_VertInTfloat {
471 ns_VertInTfloat ns_VertInTfloat_ctor_() {ns_VertInTfloat r;r.pos=0.0f;return r;}
474#if defined(CREATE_INFO_Resources)
477#if defined(ENTRY_POINT_ns_vertex_function)
479 void ns_vertex_function(
483#if defined(GPU_VERTEX_SHADER)
485 Resources srt = Resources_ctor_();
500#if defined(CREATE_INFO_Resources)
503#if defined(ENTRY_POINT_ns_fragment_function)
505 void ns_fragment_function(
509#if defined(GPU_FRAGMENT_SHADER)
511 Resources srt = Resources_ctor_();
515 gl_FragStencilRefARB;
527#if defined(CREATE_INFO_Resources)
530#if defined(ENTRY_POINT_ns_compute_function)
532 void ns_compute_function(
536#if defined(GPU_COMPUTE_SHADER)
538 Resources srt = Resources_ctor_();
539 gl_GlobalInvocationID;
540 gl_LocalInvocationID;
541 gl_LocalInvocationIndex;
551 string expect_infos = R
"(#pragma once
555GPU_SHADER_CREATE_INFO(ns_VertInTfloat)
556.vertex_in(0, Type::float_t, "pos")
557GPU_SHADER_CREATE_END()
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()
566GPU_SHADER_INTERFACE_INFO(ns_VertOut_t)
567SMOOTH(float3, ns_VertOut_local_pos)
568GPU_SHADER_INTERFACE_END()
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()
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()
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()
606 string infos =
metadata.serialize_infos();
614TEST(shader_tool, ConditionalLinting)
617 using namespace shader::parser;
621void foo([[condition(false)]] float a) {}
624 EXPECT_EQ(error,
"Condition attribute is only allowed on resources declaration");
628struct foo { [[condition(false)]] float a; };
631 EXPECT_EQ(error,
"Condition attribute is only allowed on resources declaration");
635struct foo { [[condition(false)]] [[shared]] float a; };
638 EXPECT_EQ(error,
"Condition attribute is not compatible with resource type 'SHARED'");
642struct foo { [[condition(false)]] [[push_constant]] float a; };
649TEST(shader_tool, PipelineDescription)
652 using namespace shader::parser;
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});
671 string expect_infos = R
"(#pragma once
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()
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()
703 string infos =
metadata.serialize_infos();
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)