Blender V5.3
FN_field_evaluation.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
12#include "BLI_vector.hh"
13
14#include "FN_field.hh"
15
16namespace blender::fn {
17
22 struct OutputPointerInfo {
23 void *dst = nullptr;
24 /* When a destination virtual array is provided for an input, this is
25 * unnecessary, otherwise this is used to construct the required virtual array. */
26 void (*set)(void *dst, const GVArray &varray, ResourceScope &scope) = nullptr;
27 };
28
29 ResourceScope scope_;
30 const FieldContext &context_;
31 const IndexMask &mask_;
32 Vector<GField> fields_to_evaluate_;
33 Vector<GVMutableArray> dst_varrays_;
34 Vector<GVArray> evaluated_varrays_;
35 Vector<OutputPointerInfo> output_pointer_infos_;
36 bool is_evaluated_ = false;
37
38 std::optional<Field<bool>> selection_field_;
39 IndexMask selection_mask_;
40
41 public:
43 FieldEvaluator(const FieldContext &context, const IndexMask *mask)
44 : context_(context), mask_(*mask)
45 {
46 }
47
49 FieldEvaluator(const FieldContext &context, const int64_t size)
50 : context_(context), mask_(scope_.construct<IndexMask>(size))
51 {
52 }
53
55 {
56 /* While this assert isn't strictly necessary, and could be replaced with a warning,
57 * it will catch cases where someone forgets to call #evaluate(). */
58 BLI_assert(is_evaluated_);
59 }
60
68 void set_selection(Field<bool> selection)
69 {
70 selection_field_ = std::move(selection);
71 }
72
78
80 template<typename T> int add_with_destination(Field<T> field, VMutableArray<T> dst)
81 {
82 return this->add_with_destination(GField(std::move(field)), GVMutableArray(std::move(dst)));
83 }
84
92
99 template<typename T> int add_with_destination(Field<T> field, MutableSpan<T> dst)
100 {
101 return this->add_with_destination(std::move(field), VMutableArray<T>::from_span(dst));
102 }
103
104 int add(GField field, GVArray *varray_ptr);
105
112 template<typename T> int add(Field<T> field, VArray<T> *varray_ptr)
113 {
114 const int field_index = fields_to_evaluate_.append_and_get_index(std::move(field));
115 dst_varrays_.append({});
116 output_pointer_infos_.append(OutputPointerInfo{
117 varray_ptr, [](void *dst, const GVArray &varray, ResourceScope & /*scope*/) {
118 *static_cast<VArray<T> *>(dst) = varray.typed<T>();
119 }});
120 return field_index;
121 }
122
123 template<typename T> int add(Field<T> field, VArraySpan<T> *varray_span_ptr)
124 {
125 const int field_index = fields_to_evaluate_.append_and_get_index(std::move(field));
126 dst_varrays_.append({});
127 output_pointer_infos_.append(OutputPointerInfo{
128 varray_span_ptr, [](void *dst, const GVArray &varray, ResourceScope & /*scope*/) {
129 *static_cast<VArraySpan<T> *>(dst) = varray.typed<T>();
130 }});
131 return field_index;
132 }
133
137 int add(GField field);
138
142 void evaluate();
143
144 const GVArray &get_evaluated(const int field_index) const
145 {
146 BLI_assert(is_evaluated_);
147 return evaluated_varrays_[field_index];
148 }
149
150 template<typename T> VArray<T> get_evaluated(const int field_index) const
151 {
152 return this->get_evaluated(field_index).typed<T>();
153 }
154
156
162 IndexMask get_evaluated_as_mask(int field_index);
163
165 {
166 return mask_;
167 }
168};
169
189 Span<GFieldRef> fields_to_evaluate,
190 const IndexMask &mask,
191 const FieldContext &context,
192 Span<GVMutableArray> dst_varrays = {});
193
194/* -------------------------------------------------------------------- */
197
198void evaluate_constant_field(const GField &field, void *r_value);
199
200template<typename T> T evaluate_constant_field(const Field<T> &field)
201{
202 T value;
203 value.~T();
204 evaluate_constant_field(field, &value);
205 return value;
206}
207
218
220
221} // namespace blender::fn
static VMutableArray from_span(MutableSpan< T > values)
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
int add(Field< T > field, VArraySpan< T > *varray_span_ptr)
FieldEvaluator(const FieldContext &context, const int64_t size)
FieldEvaluator(const FieldContext &context, const IndexMask *mask)
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
int add(Field< T > field, VArray< T > *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
int add_with_destination(Field< T > field, MutableSpan< T > dst)
VArray< T > get_evaluated(const int field_index) const
int add_with_destination(GField field, GVMutableArray dst)
const GVArray & get_evaluated(const int field_index) const
const IndexMask & evaluation_mask() const
int add_with_destination(Field< T > field, VMutableArray< T > dst)
#define T
Vector< GVArray > evaluate_fields(ResourceScope &scope, Span< GFieldRef > fields_to_evaluate, const IndexMask &mask, const FieldContext &context, Span< GVMutableArray > dst_varrays={})
GField make_field_constant_if_possible(GField field)
void evaluate_constant_field(const GField &field, void *r_value)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90