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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
/** \file
* \ingroup fn
*
* Field serve as an intermediate representation for calculation of a group of functions. Having
* an intermediate representation is helpful mainly to separate the execution system from the
* system that describes the necessary computations. Fields can be executed in different contexts,
* and optimization might mean executing the fields differently based on some factors like the
* number of elements.
*
* For now, fields are very tied to the multi-function system, but in the future the #FieldFunction
* class could be extended to use different descriptions of its outputs and computation besides
* the embedded multi-function.
*/
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"
#include "FN_generic_virtual_array.hh"
#include "FN_multi_function_builder.hh"
#include "FN_multi_function_procedure.hh"
#include "FN_multi_function_procedure_builder.hh"
#include "FN_multi_function_procedure_executor.hh"
namespace blender::fn {
class FieldInput;
class FieldFunction;
/**
* Descibes the output of a function. Generally corresponds to the combination of an output socket
* and link combination in a node graph.
*/
class GField {
/**
* The function that calculates this field's values. Many fields can share the same function,
* since a function can have many outputs, just like a node graph, where a single output can be
* used as multiple inputs. This avoids calling the same function many times, only using one of
* its results.
*/
std::shared_ptr<FieldFunction> function_;
/**
* Which output of the function this field corresponds to.
*/
int output_index_ = 0;
std::shared_ptr<FieldInput> input_;
public:
GField() = default;
GField(std::shared_ptr<FieldFunction> function, const int output_index)
: function_(std::move(function)), output_index_(output_index)
{
}
GField(std::shared_ptr<FieldInput> input) : input_(std::move(input))
{
}
const fn::CPPType &cpp_type() const;
bool is_input() const
{
return input_.get() != nullptr;
}
const FieldInput &input() const
{
BLI_assert(!function_);
BLI_assert(input_);
return *input_;
}
bool is_function() const
{
return function_.get() != nullptr;
}
const FieldFunction &function() const
{
BLI_assert(function_ != nullptr);
BLI_assert(input_ == nullptr);
return *function_;
}
int function_output_index() const
{
BLI_assert(function_ != nullptr);
BLI_assert(input_ == nullptr);
return output_index_;
}
};
template<typename T> class Field {
private:
GField field_;
public:
Field() = default;
Field(GField field) : field_(std::move(field))
{
BLI_assert(field_.cpp_type().is<T>());
}
const GField *operator->() const
{
return &field_;
}
const GField &operator*() const
{
return field_;
}
};
/**
* An operation acting on data described by fields. Generally corresponds
* to a node or a subset of a node in a node graph.
*/
class FieldFunction {
/**
* The function used to calculate the field.
*/
std::unique_ptr<const MultiFunction> owned_function_;
const MultiFunction *function_;
/**
* References to descriptions of the results from the functions this function depends on.
*/
blender::Vector<GField> inputs_;
public:
FieldFunction(std::unique_ptr<const MultiFunction> function, Vector<GField> inputs = {})
: owned_function_(std::move(function)), inputs_(std::move(inputs))
{
function_ = owned_function_.get();
}
Span<GField> inputs() const
{
return inputs_;
}
const MultiFunction &multi_function() const
{
return *function_;
}
const CPPType &cpp_type_of_output_index(int index) const
{
MFParamType param_type = function_->param_type(index);
MFDataType data_type = param_type.data_type();
BLI_assert(param_type.interface_type() == MFParamType::Output);
BLI_assert(data_type.is_single());
return data_type.single_type();
}
};
class FieldInput {
protected:
const CPPType *type_;
std::string debug_name_;
public:
FieldInput(const CPPType &type, std::string debug_name = "")
: type_(&type), debug_name_(std::move(debug_name))
{
}
virtual GVArrayPtr get_varray_generic_context(IndexMask mask) const = 0;
blender::StringRef debug_name() const
{
return debug_name_;
}
const CPPType &cpp_type() const
{
return *type_;
}
};
/**
* Evaluate more than one field at a time, as an optimization
* in case they share inputs or various intermediate values.
*/
void evaluate_fields(blender::Span<GField> fields,
blender::IndexMask mask,
blender::Span<GMutableSpan> outputs);
void evaluate_constant_field(const GField &field, void *r_value);
template<typename T> T evaluate_constant_field(const Field<T> &field)
{
T value;
value.~T();
evaluate_constant_field(*field, &value);
return value;
}
template<typename T> Field<T> make_constant_field(T value)
{
auto constant_fn = std::make_unique<fn::CustomMF_Constant<T>>(std::forward<T>(value));
auto field_fn = std::make_shared<FieldFunction>(std::move(constant_fn));
return Field<T>{GField{std::move(field_fn), 0}};
}
/* --------------------------------------------------------------------
* GField inline methods.
*/
inline const CPPType &GField::cpp_type() const
{
if (this->is_function()) {
return function_->cpp_type_of_output_index(output_index_);
}
return input_->cpp_type();
}
} // namespace blender::fn
|