1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* Apache License, Version 2.0 */
#include "testing/testing.h"
#include "FN_cpp_type.hh"
#include "FN_field.hh"
#include "FN_multi_function_builder.hh"
namespace blender::fn::tests {
TEST(field, ConstantFunction)
{
/* TODO: Figure out how to not use another "OperationFieldSource(" inside of std::make_shared. */
GField constant_field{std::make_shared<OperationFieldSource>(OperationFieldSource(
std::make_unique<CustomMF_Constant<int>>(10), {})),
0};
Array<int> result(4);
GMutableSpan result_generic(result.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans(
{&constant_field}, IndexMask(IndexRange(4)), field_context, {result_generic});
EXPECT_EQ(result[0], 10);
EXPECT_EQ(result[1], 10);
EXPECT_EQ(result[2], 10);
EXPECT_EQ(result[3], 10);
}
class IndexContextFieldSource final : public ContextFieldSource {
public:
IndexContextFieldSource() : ContextFieldSource(CPPType::get<int>(), "Index")
{
}
const GVArray *try_get_varray_for_context(const FieldContext &UNUSED(context),
IndexMask mask,
ResourceScope &scope) const final
{
auto index_func = [](int i) { return i; };
return &scope.construct<
GVArray_For_EmbeddedVArray<int, VArray_For_Func<int, decltype(index_func)>>>(
__func__, mask.min_array_size(), mask.min_array_size(), index_func);
}
};
TEST(field, VArrayInput)
{
GField index_field{std::make_shared<IndexContextFieldSource>()};
Array<int> result_1(4);
GMutableSpan result_generic_1(result_1.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans(
{&index_field}, IndexMask(IndexRange(4)), field_context, {result_generic_1});
EXPECT_EQ(result_1[0], 0);
EXPECT_EQ(result_1[1], 1);
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[3], 3);
/* Evaluate a second time, just to test that the first didn't break anything. */
Array<int> result_2(10);
GMutableSpan result_generic_2(result_2.as_mutable_span());
evaluate_fields_to_spans({&index_field}, {2, 4, 6, 8}, field_context, {result_generic_2});
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, VArrayInputMultipleOutputs)
{
std::shared_ptr<ContextFieldSource> index_input = std::make_shared<IndexContextFieldSource>();
GField field_1{index_input};
GField field_2{index_input};
Array<int> result_1(10);
Array<int> result_2(10);
GMutableSpan result_generic_1(result_1.as_mutable_span());
GMutableSpan result_generic_2(result_2.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans(
{&field_1, &field_2}, {2, 4, 6, 8}, field_context, {result_generic_1, result_generic_2});
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[4], 4);
EXPECT_EQ(result_1[6], 6);
EXPECT_EQ(result_1[8], 8);
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, InputAndFunction)
{
GField index_field{std::make_shared<IndexContextFieldSource>()};
std::unique_ptr<MultiFunction> add_fn = std::make_unique<CustomMF_SI_SI_SO<int, int, int>>(
"add", [](int a, int b) { return a + b; });
GField output_field{std::make_shared<OperationFieldSource>(
OperationFieldSource(std::move(add_fn), {index_field, index_field})),
0};
Array<int> result(10);
GMutableSpan result_generic(result.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans({&output_field}, {2, 4, 6, 8}, field_context, {result_generic});
EXPECT_EQ(result[2], 4);
EXPECT_EQ(result[4], 8);
EXPECT_EQ(result[6], 12);
EXPECT_EQ(result[8], 16);
}
TEST(field, TwoFunctions)
{
GField index_field{std::make_shared<IndexContextFieldSource>()};
std::unique_ptr<MultiFunction> add_fn = std::make_unique<CustomMF_SI_SI_SO<int, int, int>>(
"add", [](int a, int b) { return a + b; });
GField add_field{std::make_shared<OperationFieldSource>(
OperationFieldSource(std::move(add_fn), {index_field, index_field})),
0};
std::unique_ptr<MultiFunction> add_10_fn = std::make_unique<CustomMF_SI_SO<int, int>>(
"add_10", [](int a) { return a + 10; });
GField result_field{std::make_shared<OperationFieldSource>(
OperationFieldSource(std::move(add_10_fn), {add_field})),
0};
Array<int> result(10);
GMutableSpan result_generic(result.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans({&result_field}, {2, 4, 6, 8}, field_context, {result_generic});
EXPECT_EQ(result[2], 14);
EXPECT_EQ(result[4], 18);
EXPECT_EQ(result[6], 22);
EXPECT_EQ(result[8], 26);
}
class TwoOutputFunction : public MultiFunction {
private:
MFSignature signature_;
public:
TwoOutputFunction(StringRef name)
{
MFSignatureBuilder signature{name};
signature.single_input<int>("In1");
signature.single_input<int>("In2");
signature.single_output<int>("Add");
signature.single_output<int>("Add10");
signature_ = signature.build();
this->set_signature(&signature_);
}
void call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const override
{
const VArray<int> &in1 = params.readonly_single_input<int>(0, "In1");
const VArray<int> &in2 = params.readonly_single_input<int>(1, "In2");
MutableSpan<int> add = params.uninitialized_single_output<int>(2, "Add");
MutableSpan<int> add_10 = params.uninitialized_single_output<int>(3, "Add10");
mask.foreach_index([&](const int64_t i) {
add[i] = in1[i] + in2[i];
add_10[i] = add[i] + 10;
});
}
};
TEST(field, FunctionTwoOutputs)
{
/* Also use two separate input fields, why not. */
GField index_field_1{std::make_shared<IndexContextFieldSource>()};
GField index_field_2{std::make_shared<IndexContextFieldSource>()};
std::shared_ptr<OperationFieldSource> fn = std::make_shared<OperationFieldSource>(
OperationFieldSource(std::make_unique<TwoOutputFunction>("SI_SI_SO_SO"),
{index_field_1, index_field_2}));
GField result_field_1{fn, 0};
GField result_field_2{fn, 1};
Array<int> result_1(10);
Array<int> result_2(10);
GMutableSpan result_generic_1(result_1.as_mutable_span());
GMutableSpan result_generic_2(result_2.as_mutable_span());
FieldContext field_context;
evaluate_fields_to_spans({&result_field_1, &result_field_2},
{2, 4, 6, 8},
field_context,
{result_generic_1, result_generic_2});
EXPECT_EQ(result_1[2], 4);
EXPECT_EQ(result_1[4], 8);
EXPECT_EQ(result_1[6], 12);
EXPECT_EQ(result_1[8], 16);
EXPECT_EQ(result_2[2], 14);
EXPECT_EQ(result_2[4], 18);
EXPECT_EQ(result_2[6], 22);
EXPECT_EQ(result_2[8], 26);
}
TEST(field, TwoFunctionsTwoOutputs)
{
GField index_field{std::make_shared<IndexContextFieldSource>()};
std::shared_ptr<OperationFieldSource> fn = std::make_shared<OperationFieldSource>(
OperationFieldSource(std::make_unique<TwoOutputFunction>("SI_SI_SO_SO"),
{index_field, index_field}));
Vector<int64_t> mask_indices = {2, 4, 6, 8};
IndexMask mask = mask_indices.as_span();
Field<int> result_field_1{fn, 0};
Field<int> intermediate_field{fn, 1};
std::unique_ptr<MultiFunction> add_10_fn = std::make_unique<CustomMF_SI_SO<int, int>>(
"add_10", [](int a) { return a + 10; });
Field<int> result_field_2{std::make_shared<OperationFieldSource>(
OperationFieldSource(std::move(add_10_fn), {intermediate_field})),
0};
FieldContext field_context;
FieldEvaluator field_evaluator{field_context, &mask};
const VArray<int> *result_1 = nullptr;
const VArray<int> *result_2 = nullptr;
field_evaluator.add(result_field_1, &result_1);
field_evaluator.add(result_field_2, &result_2);
field_evaluator.evaluate();
EXPECT_EQ(result_1->get(2), 4);
EXPECT_EQ(result_1->get(4), 8);
EXPECT_EQ(result_1->get(6), 12);
EXPECT_EQ(result_1->get(8), 16);
EXPECT_EQ(result_2->get(2), 24);
EXPECT_EQ(result_2->get(4), 28);
EXPECT_EQ(result_2->get(6), 32);
EXPECT_EQ(result_2->get(8), 36);
}
} // namespace blender::fn::tests
|