Blender V4.3
FN_lazy_function_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
10
11#include "BLI_task.h"
12#include "BLI_timeit.hh"
13
15
17 public:
19 {
20 debug_name_ = "Add";
21 inputs_.append({"A", CPPType::get<int>()});
22 inputs_.append({"B", CPPType::get<int>()});
23 outputs_.append({"Result", CPPType::get<int>()});
24 }
25
26 void execute_impl(Params &params, const Context & /*context*/) const override
27 {
28 const int a = params.get_input<int>(0);
29 const int b = params.get_input<int>(1);
30 params.set_output(0, a + b);
31 }
32};
33
35 private:
36 int *dst1_;
37 int *dst2_;
38
39 public:
40 StoreValueFunction(int *dst1, int *dst2) : dst1_(dst1), dst2_(dst2)
41 {
42 debug_name_ = "Store Value";
43 inputs_.append({"A", CPPType::get<int>()});
45 }
46
47 void execute_impl(Params &params, const Context & /*context*/) const override
48 {
49 *dst1_ = params.get_input<int>(0);
50 if (int *value = params.try_get_input_data_ptr_or_request<int>(1)) {
51 *dst2_ = *value;
52 }
53 }
54};
55
57 private:
58 Vector<const FunctionNode *> side_effect_nodes_;
59
60 public:
62 : side_effect_nodes_(side_effect_nodes)
63 {
64 }
65
67 const Context & /*context*/) const override
68 {
69 return side_effect_nodes_;
70 }
71};
72
73TEST(lazy_function, SimpleAdd)
74{
75 const AddLazyFunction add_fn;
76 int result = 0;
78 add_fn, nullptr, nullptr, std::make_tuple(30, 5), std::make_tuple(&result));
79 EXPECT_EQ(result, 35);
80}
81
82TEST(lazy_function, SideEffects)
83{
85 int dst1 = 0;
86 int dst2 = 0;
87
88 const AddLazyFunction add_fn;
89 const StoreValueFunction store_fn{&dst1, &dst2};
90
92 FunctionNode &add_node_1 = graph.add_function(add_fn);
93 FunctionNode &add_node_2 = graph.add_function(add_fn);
94 FunctionNode &store_node = graph.add_function(store_fn);
95 GraphInputSocket &graph_input = graph.add_input(CPPType::get<int>());
96
97 graph.add_link(graph_input, add_node_1.input(0));
98 graph.add_link(graph_input, add_node_2.input(0));
99 graph.add_link(add_node_1.output(0), store_node.input(0));
100 graph.add_link(add_node_2.output(0), store_node.input(1));
101
102 const int value_10 = 10;
103 const int value_100 = 100;
104 add_node_1.input(1).set_default_value(&value_10);
105 add_node_2.input(1).set_default_value(&value_100);
106
107 graph.update_node_indices();
108
109 SimpleSideEffectProvider side_effect_provider{{&store_node}};
110
111 GraphExecutor executor_fn{graph, {&graph_input}, {}, nullptr, &side_effect_provider, nullptr};
113 executor_fn, nullptr, nullptr, std::make_tuple(5), std::make_tuple());
114
115 EXPECT_EQ(dst1, 15);
116 EXPECT_EQ(dst2, 105);
117}
118
120 public:
122 {
123 debug_name_ = "Partial Evaluation";
125
126 inputs_.append_as("A", CPPType::get<int>(), ValueUsage::Used);
127 inputs_.append_as("B", CPPType::get<int>(), ValueUsage::Used);
128
129 outputs_.append_as("A*2", CPPType::get<int>());
130 outputs_.append_as("B*5", CPPType::get<int>());
131 }
132
133 void execute_impl(Params &params, const Context & /*context*/) const override
134 {
135 if (!params.output_was_set(0)) {
136 if (int *a = params.try_get_input_data_ptr<int>(0)) {
137 params.set_output(0, *a * 2);
138 }
139 }
140 if (!params.output_was_set(1)) {
141 if (int *b = params.try_get_input_data_ptr<int>(1)) {
142 params.set_output(1, *b * 5);
143 }
144 }
145 }
146
147 void possible_output_dependencies(const int output_index,
148 FunctionRef<void(Span<int>)> fn) const override
149 {
150 /* Each output only depends on the input with the same index. */
151 const int input_index = output_index;
152 fn({input_index});
153 }
154};
155
156TEST(lazy_function, GraphWithCycle)
157{
159
160 Graph graph;
161 FunctionNode &fn_node = graph.add_function(fn);
162
163 GraphInputSocket &input_socket = graph.add_input(CPPType::get<int>());
164 GraphOutputSocket &output_socket = graph.add_output(CPPType::get<int>());
165
166 graph.add_link(input_socket, fn_node.input(0));
167 /* NOTE: This creates a cycle in the graph. However, it should still be possible to evaluate it,
168 * because there is no actual data dependency in the cycle. */
169 graph.add_link(fn_node.output(0), fn_node.input(1));
170 graph.add_link(fn_node.output(1), output_socket);
171
172 graph.update_node_indices();
173
174 GraphExecutor executor_fn{graph, {&input_socket}, {&output_socket}, nullptr, nullptr, nullptr};
175 int result = 0;
177 executor_fn, nullptr, nullptr, std::make_tuple(10), std::make_tuple(&result));
178
179 EXPECT_EQ(result, 10 * 2 * 5);
180}
181
182} // namespace blender::fn::lazy_function::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_task_scheduler_init(void)
static const CPPType & get()
const InputSocket & input(int index) const
const OutputSocket & output(int index) const
void execute_impl(Params &params, const Context &) const override
void execute_impl(Params &params, const Context &) const override
void possible_output_dependencies(const int output_index, FunctionRef< void(Span< int >)> fn) const override
SimpleSideEffectProvider(Span< const FunctionNode * > side_effect_nodes)
Vector< const FunctionNode * > get_nodes_with_side_effects(const Context &) const override
void execute_impl(Params &params, const Context &) const override
local_group_size(16, 16) .push_constant(Type b
Depsgraph * graph
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void execute_lazy_function_eagerly(const LazyFunction &fn, UserData *user_data, LocalUserData *local_user_data, std::tuple< Inputs... > inputs, std::tuple< Outputs *... > outputs)