Blender V5.3
volume_grid_multi_function_eval.cc
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#ifdef WITH_OPENVDB
10
11# include "DNA_customdata_types.h"
12
14# include "BKE_customdata.hh"
15# include "BKE_volume_enums.hh"
19
20# include "BLT_translation.hh"
21
22# include "FN_field_evaluation.hh"
23
24namespace blender::bke::volume_grid::multi_function_eval {
25
39BLI_NOINLINE static void process_leaf_node(const mf::MultiFunction &fn,
40 const Span<InputVariant> input_values,
41 MutableSpan<openvdb::GridBase::Ptr> output_grids,
42 const openvdb::math::Transform &transform,
43 const LeafNodeMask &leaf_node_mask,
44 const openvdb::CoordBBox &leaf_bbox,
45 const GetVoxelsFn get_voxels_fn)
46{
47 AlignedBuffer<8192, 8> allocation_buffer;
48 ResourceScope scope(allocation_buffer);
49
50 /* Create an index mask for all the active voxels in the leaf. */
51 const IndexMask index_mask = IndexMask::from_predicate(
52 IndexRange(LeafNodeMask::SIZE),
53 scope.allocator(),
54 [&](const int64_t i) { return leaf_node_mask.isOn(i); },
55 exec_mode::serial);
56
57 mf::ParamsBuilder params{fn, &index_mask};
58 mf::ContextBuilder context;
59
60 /* We need to find the corresponding leaf nodes in all the input and output grids. That's done by
61 * finding the leaf that contains this voxel. */
62 const openvdb::Coord any_voxel_in_leaf = leaf_bbox.min();
63
64 std::optional<MutableSpan<openvdb::Coord>> voxel_coords_opt;
65 auto ensure_voxel_coords = [&]() {
66 if (!voxel_coords_opt.has_value()) {
67 voxel_coords_opt = scope.allocator().allocate_array<openvdb::Coord>(
68 index_mask.min_array_size());
69 get_voxels_fn(voxel_coords_opt.value());
70 }
71 return *voxel_coords_opt;
72 };
73
74 for (const int input_i : input_values.index_range()) {
75 const InputVariant &value_variant = input_values[input_i];
76 const mf::ParamType param_type = fn.param_type(params.next_param_index());
77 const CPPType &param_cpp_type = param_type.data_type().single_type();
78
79 if (const openvdb::GridBase *const *grid_base = std::get_if<const openvdb::GridBase *>(
80 &value_variant))
81 {
82 /* The input is a grid, so we can attempt to reference the grid values directly. */
83 to_typed_grid(**grid_base, [&](const auto &grid) {
84 using GridT = typename std::decay_t<decltype(grid)>;
85 using ValueT = typename GridT::ValueType;
86 BLI_assert(param_cpp_type.size == sizeof(ValueT));
87 const auto &tree = grid.tree();
88
89 if (const auto *leaf_node = tree.probeLeaf(any_voxel_in_leaf)) {
90 /* Boolean grids are special because they encode the values as bitmask. So create a
91 * temporary buffer for the inputs. */
92 if constexpr (std::is_same_v<ValueT, bool>) {
93 const Span<openvdb::Coord> voxels = ensure_voxel_coords();
94 MutableSpan<bool> values = scope.allocator().allocate_array<bool>(
95 index_mask.min_array_size());
96 index_mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
97 const openvdb::Coord &coord = voxels[i];
98 values[i] = tree.getValue(coord);
99 });
100 params.add_readonly_single_input(values);
101 }
102 else {
103 const Span<ValueT> values(leaf_node->buffer().data(), LeafNodeMask::SIZE);
104 const LeafNodeMask &input_leaf_mask = leaf_node->valueMask();
105 const LeafNodeMask missing_mask = leaf_node_mask & !input_leaf_mask;
106 if (missing_mask.isOff()) {
107 /* All values available, so reference the data directly. */
108 params.add_readonly_single_input(
109 GSpan(param_cpp_type, values.data(), values.size()));
110 }
111 else {
112 /* Fill in the missing values with the background value. */
113 MutableSpan copied_values = scope.allocator().construct_array_copy(values);
114 const auto &background = tree.background();
115 for (auto missing_it = missing_mask.beginOn(); missing_it.test(); ++missing_it) {
116 const int index = missing_it.pos();
117 copied_values[index] = background;
118 }
119 params.add_readonly_single_input(
120 GSpan(param_cpp_type, copied_values.data(), copied_values.size()));
121 }
122 }
123 }
124 else {
125 /* The input does not have this leaf node, so just get the value that's used for the
126 * entire leaf. The leaf may be in a tile or is inactive in which case the background
127 * value is used. */
128 const auto &single_value = tree.getValue(any_voxel_in_leaf);
129 params.add_readonly_single_input(GPointer(param_cpp_type, &single_value));
130 }
131 });
132 }
133 else if (const fn::GField *const *field = std::get_if<const fn::GField *>(&value_variant)) {
134 /* Compute the field on all active voxels in the leaf and pass the result to the
135 * multi-function. */
136 const CPPType &type = (*field)->cpp_type();
137 const Span<openvdb::Coord> voxels = ensure_voxel_coords();
138 bke::VoxelFieldContext field_context{transform, voxels};
139 fn::FieldEvaluator evaluator{field_context, &index_mask};
140 GMutableSpan values{
141 type, scope.allocator().allocate_array(type, voxels.size()), voxels.size()};
142 evaluator.add_with_destination(**field, values);
143 evaluator.evaluate();
144 params.add_readonly_single_input(values);
145 }
146 else {
147 /* Pass the single value directly to the multi-function. */
148 params.add_readonly_single_input(std::get<GPointer>(value_variant));
149 }
150 }
151
152 for (const int output_i : output_grids.index_range()) {
153 const mf::ParamType param_type = fn.param_type(params.next_param_index());
154 const CPPType &param_cpp_type = param_type.data_type().single_type();
155 if (!output_grids[output_i]) {
156 params.add_ignored_single_output();
157 continue;
158 }
159
160 openvdb::GridBase &grid_base = *output_grids[output_i];
161 to_typed_grid(grid_base, [&](auto &grid) {
162 using GridT = typename std::decay_t<decltype(grid)>;
163 using ValueT = typename GridT::ValueType;
164
165 auto &tree = grid.tree();
166 auto *leaf_node = tree.probeLeaf(any_voxel_in_leaf);
167 /* Should have been added before. */
168 BLI_assert(leaf_node);
169
170 /* Boolean grids are special because they encode the values as bitmask. */
171 if constexpr (std::is_same_v<ValueT, bool>) {
172 MutableSpan<bool> values = scope.allocator().allocate_array<bool>(
173 index_mask.min_array_size());
174 params.add_uninitialized_single_output(values);
175 }
176 else {
177 /* Write directly into the buffer of the output leaf node. */
178 ValueT *values = leaf_node->buffer().data();
179 params.add_uninitialized_single_output(
180 GMutableSpan(param_cpp_type, values, LeafNodeMask::SIZE));
181 }
182 });
183 }
184
185 /* Actually call the multi-function which will write the results into the output grids (except
186 * for boolean grids). */
187 fn.prepare_for_execution();
188 fn.call_auto(index_mask, params, context);
189
190 for (const int output_i : output_grids.index_range()) {
191 const int param_index = input_values.size() + output_i;
192 const mf::ParamType param_type = fn.param_type(param_index);
193 const CPPType &param_cpp_type = param_type.data_type().single_type();
194 if (!param_cpp_type.is<bool>()) {
195 continue;
196 }
197 set_mask_leaf_buffer_from_bools(static_cast<openvdb::BoolGrid &>(*output_grids[output_i]),
198 params.computed_array(param_index).typed<bool>(),
199 index_mask,
200 ensure_voxel_coords());
201 }
202}
203
214BLI_NOINLINE static void process_voxels(const mf::MultiFunction &fn,
215 const Span<InputVariant> input_values,
216 MutableSpan<openvdb::GridBase::Ptr> output_grids,
217 const openvdb::math::Transform &transform,
218 const Span<openvdb::Coord> voxels)
219{
220 const int64_t voxels_num = voxels.size();
221 const IndexMask index_mask{voxels_num};
222 AlignedBuffer<8192, 8> allocation_buffer;
223 ResourceScope scope(allocation_buffer);
224 mf::ParamsBuilder params{fn, &index_mask};
225 mf::ContextBuilder context;
226
227 for (const int input_i : input_values.index_range()) {
228 const InputVariant &value_variant = input_values[input_i];
229 const mf::ParamType param_type = fn.param_type(params.next_param_index());
230 const CPPType &param_cpp_type = param_type.data_type().single_type();
231
232 if (const openvdb::GridBase *const *grid_base = std::get_if<const openvdb::GridBase *>(
233 &value_variant))
234 {
235 /* Retrieve all voxel values from the input grid. */
236 to_typed_grid(**grid_base, [&](const auto &grid) {
237 using ValueType = typename std::decay_t<decltype(grid)>::ValueType;
238 const auto &tree = grid.tree();
239 /* Could try to cache the accessor across batches, but it's not straight forward since its
240 * type depends on the grid type and thread-safety has to be maintained. It's likely not
241 * worth it because the cost is already negligible since we are processing a full batch. */
242 auto accessor = grid.getConstUnsafeAccessor();
243
244 MutableSpan<ValueType> values = scope.allocator().allocate_array<ValueType>(voxels_num);
245 for (const int64_t i : IndexRange(voxels_num)) {
246 const openvdb::Coord &coord = voxels[i];
247 values[i] = tree.getValue(coord, accessor);
248 }
249 BLI_assert(param_cpp_type.size == sizeof(ValueType));
250 params.add_readonly_single_input(GSpan(param_cpp_type, values.data(), voxels_num));
251 });
252 }
253 else if (const fn::GField *const *field = std::get_if<const fn::GField *>(&value_variant)) {
254 /* Evaluate the field on all voxels.
255 * TODO: Collect fields from all inputs to evaluate together. */
256 const CPPType &type = (*field)->cpp_type();
257 bke::VoxelFieldContext field_context{transform, voxels};
258 fn::FieldEvaluator evaluator{field_context, voxels_num};
259 GMutableSpan values{type, scope.allocator().allocate_array(type, voxels_num), voxels_num};
260 evaluator.add_with_destination(**field, values);
261 evaluator.evaluate();
262 params.add_readonly_single_input(values);
263 }
264 else {
265 /* Pass the single value directly to the multi-function. */
266 params.add_readonly_single_input(std::get<GPointer>(value_variant));
267 }
268 }
269
270 /* Prepare temporary output buffers for the field evaluation. Those will later be copied into the
271 * output grids. */
272 for ([[maybe_unused]] const int output_i : output_grids.index_range()) {
273 const int param_index = input_values.size() + output_i;
274 const mf::ParamType param_type = fn.param_type(param_index);
275 const CPPType &type = param_type.data_type().single_type();
276 void *buffer = scope.allocator().allocate_array(type, voxels_num);
277 params.add_uninitialized_single_output(GMutableSpan{type, buffer, voxels_num});
278 }
279
280 /* Actually call the multi-function which will fill the temporary output buffers. */
281 fn.call_auto(index_mask, params, context);
282
283 /* Copy the values from the temporary buffers into the output grids. */
284 for (const int output_i : output_grids.index_range()) {
285 if (!output_grids[output_i]) {
286 continue;
287 }
288 const int param_index = input_values.size() + output_i;
289 set_grid_values(*output_grids[output_i], params.computed_array(param_index), voxels);
290 }
291}
292
304BLI_NOINLINE static void process_tiles(const mf::MultiFunction &fn,
305 const Span<InputVariant> input_values,
306 MutableSpan<openvdb::GridBase::Ptr> output_grids,
307 const openvdb::math::Transform &transform,
308 const Span<openvdb::CoordBBox> tiles)
309{
310 const int64_t tiles_num = tiles.size();
311 const IndexMask index_mask{tiles_num};
312
313 AlignedBuffer<8192, 8> allocation_buffer;
314 ResourceScope scope(allocation_buffer);
315 mf::ParamsBuilder params{fn, &index_mask};
316 mf::ContextBuilder context;
317
318 for (const int input_i : input_values.index_range()) {
319 const InputVariant &value_variant = input_values[input_i];
320 const mf::ParamType param_type = fn.param_type(params.next_param_index());
321 const CPPType &param_cpp_type = param_type.data_type().single_type();
322
323 if (const openvdb::GridBase *const *grid_base = std::get_if<const openvdb::GridBase *>(
324 &value_variant))
325 {
326 /* Sample the tile values from the input grid. */
327 to_typed_grid(**grid_base, [&](const auto &grid) {
328 using GridT = std::decay_t<decltype(grid)>;
329 using ValueType = typename GridT::ValueType;
330 const auto &tree = grid.tree();
331 auto accessor = grid.getConstUnsafeAccessor();
332
333 MutableSpan<ValueType> values = scope.allocator().allocate_array<ValueType>(tiles_num);
334 for (const int64_t i : IndexRange(tiles_num)) {
335 const openvdb::CoordBBox &tile = tiles[i];
336 /* The tile is assumed to have a single constant value. Therefore, we can get the value
337 * from any voxel in that tile as representative. */
338 const openvdb::Coord any_coord_in_tile = tile.min();
339 values[i] = tree.getValue(any_coord_in_tile, accessor);
340 }
341 BLI_assert(param_cpp_type.size == sizeof(ValueType));
342 params.add_readonly_single_input(GSpan(param_cpp_type, values.data(), tiles_num));
343 });
344 }
345 else if (const fn::GField *const *field = std::get_if<const fn::GField *>(&value_variant)) {
346 /* Evaluate the field on all tiles.
347 * TODO: Gather fields from all inputs to evaluate together. */
348 const CPPType &type = (*field)->cpp_type();
349 bke::TilesFieldContext field_context{transform, tiles};
350 fn::FieldEvaluator evaluator{field_context, tiles_num};
351 GMutableSpan values{type, scope.allocator().allocate_array(type, tiles_num), tiles_num};
352 evaluator.add_with_destination(**field, values);
353 evaluator.evaluate();
354 params.add_readonly_single_input(values);
355 }
356 else {
357 /* Pass the single value directly to the multi-function. */
358 params.add_readonly_single_input(std::get<GPointer>(value_variant));
359 }
360 }
361
362 /* Prepare temporary output buffers for the field evaluation. Those will later be copied into the
363 * output grids. */
364 for ([[maybe_unused]] const int output_i : output_grids.index_range()) {
365 if (!output_grids[output_i]) {
366 params.add_ignored_single_output();
367 continue;
368 }
369 const int param_index = input_values.size() + output_i;
370 const mf::ParamType param_type = fn.param_type(param_index);
371 const CPPType &type = param_type.data_type().single_type();
372 void *buffer = scope.allocator().allocate_array(type, tiles_num);
373 params.add_uninitialized_single_output(GMutableSpan{type, buffer, tiles_num});
374 }
375
376 /* Actually call the multi-function which will fill the temporary output buffers. */
377 fn.call_auto(index_mask, params, context);
378
379 /* Copy the values from the temporary buffers into the output grids. */
380 for (const int output_i : output_grids.index_range()) {
381 if (!output_grids[output_i]) {
382 continue;
383 }
384 const int param_index = input_values.size() + output_i;
385 set_tile_values(*output_grids[output_i], params.computed_array(param_index), tiles);
386 }
387}
388
389BLI_NOINLINE static void process_background(const mf::MultiFunction &fn,
390 const Span<InputVariant> input_values,
391 const openvdb::math::Transform &transform,
392 MutableSpan<openvdb::GridBase::Ptr> output_grids)
393{
394 AlignedBuffer<160, 8> allocation_buffer;
395 ResourceScope scope(allocation_buffer);
396
397 const IndexMask mask(1);
398 mf::ParamsBuilder params(fn, &mask);
399 mf::ContextBuilder context;
400
401 for (const int input_i : input_values.index_range()) {
402 const InputVariant &value_variant = input_values[input_i];
403 const mf::ParamType param_type = fn.param_type(params.next_param_index());
404 const CPPType &param_cpp_type = param_type.data_type().single_type();
405
406 if (const openvdb::GridBase *const *grid_base = std::get_if<const openvdb::GridBase *>(
407 &value_variant))
408 {
409 to_typed_grid(**grid_base, [&](const auto &grid) {
410# ifndef NDEBUG
411 using GridT = std::decay_t<decltype(grid)>;
412 using ValueType = typename GridT::ValueType;
413 BLI_assert(param_cpp_type.size == sizeof(ValueType));
414# endif
415 const auto &tree = grid.tree();
416 params.add_readonly_single_input(GPointer(param_cpp_type, &tree.background()));
417 });
418 continue;
419 }
420
421 if (const fn::GField *const *field = std::get_if<const fn::GField *>(&value_variant)) {
422 const CPPType &type = (*field)->cpp_type();
423 static const openvdb::CoordBBox background_space = openvdb::CoordBBox::inf();
424 bke::TilesFieldContext field_context(transform,
425 Span<openvdb::CoordBBox>(&background_space, 1));
426 fn::FieldEvaluator evaluator(field_context, 1);
427 GMutableSpan value(type, scope.allocator().allocate(type), 1);
428 evaluator.add_with_destination(**field, value);
429 evaluator.evaluate();
430 params.add_readonly_single_input(GPointer(type, value.data()));
431 continue;
432 }
433
434 params.add_readonly_single_input(std::get<GPointer>(value_variant));
435 }
436
437 for ([[maybe_unused]] const int output_i : output_grids.index_range()) {
438 if (!output_grids[output_i]) {
439 params.add_ignored_single_output();
440 continue;
441 }
442 const int param_index = input_values.size() + output_i;
443 const mf::ParamType param_type = fn.param_type(param_index);
444 const CPPType &type = param_type.data_type().single_type();
445
446 GMutableSpan value_buffer(type, scope.allocator().allocate(type), 1);
447 params.add_uninitialized_single_output(value_buffer);
448 }
449
450 fn.call_auto(mask, params, context);
451
452 for ([[maybe_unused]] const int output_i : output_grids.index_range()) {
453 if (!output_grids[output_i]) {
454 continue;
455 }
456 const int param_index = input_values.size() + output_i;
457 const GSpan value = params.computed_array(param_index);
458 set_grid_background(*output_grids[output_i], GPointer(value.type(), value.data()));
459 }
460}
461
462EvalResult evaluate_multi_function_on_grid(const mf::MultiFunction &fn,
463 const Span<InputVariant> input_values,
464 const Span<bool> output_usages)
465{
466 int inputs_num = 0;
467 int outputs_num = 0;
468 for (const int param_i : fn.param_indices()) {
469 const mf::ParamType param_type = fn.param_type(param_i);
470 if (param_type.interface_type() == mf::ParamType::Input) {
471 inputs_num++;
472 }
473 else if (param_type.interface_type() == mf::ParamType::Output) {
474 outputs_num++;
475 }
476 else {
478 }
479 }
480
481 BLI_assert(input_values.size() == inputs_num);
482 BLI_assert(output_usages.size() == outputs_num);
483 Array<bke::VolumeTreeAccessToken> input_volume_tokens(inputs_num);
484
485 Vector<const openvdb::GridBase *> input_grids;
486 for (const int input_i : IndexRange(inputs_num)) {
487 const InputVariant &value_variant = input_values[input_i];
488 if (const openvdb::GridBase *const *grid = std::get_if<const openvdb::GridBase *>(
489 &value_variant))
490 {
491 input_grids.append(*grid);
492 }
493 }
494
495 const openvdb::math::Transform *transform = nullptr;
496 for (const openvdb::GridBase *grid : input_grids) {
497 const openvdb::math::Transform &other_transform = grid->transform();
498 if (!transform) {
499 transform = &other_transform;
500 continue;
501 }
502 if (*transform != other_transform) {
503 return {EvalResult::Failure{TIP_("Input grids have incompatible transforms")}};
504 }
505 }
506 if (transform == nullptr) {
507 return {EvalResult::Failure{TIP_("No input grid found that can determine the topology")}};
508 }
509
510 openvdb::MaskTree mask_tree;
511 for (const openvdb::GridBase *grid : input_grids) {
512 to_typed_grid(*grid, [&](const auto &grid) { mask_tree.topologyUnion(grid.tree()); });
513 }
514
515 Array<openvdb::GridBase::Ptr> output_grids(outputs_num);
516 for (const int i : IndexRange(outputs_num)) {
517 if (!output_usages[i]) {
518 continue;
519 }
520 const int param_index = input_values.size() + i;
521 const mf::ParamType param_type = fn.param_type(param_index);
522 const CPPType &cpp_type = param_type.data_type().single_type();
523 const std::optional<VolumeGridType> grid_type = cpp_type_to_grid_type(cpp_type);
524 if (!grid_type) {
525 return {EvalResult::Failure{TIP_("Grid type not supported")}};
526 }
527 output_grids[i] = create_grid_with_topology(mask_tree, *transform, *grid_type);
528 }
529
530 fn.prepare_for_execution();
531
532 parallel_grid_topology_tasks(
533 mask_tree,
534 [&](const LeafNodeMask &leaf_node_mask,
535 const openvdb::CoordBBox &leaf_bbox,
536 const GetVoxelsFn get_voxels_fn) {
537 process_leaf_node(
538 fn, input_values, output_grids, *transform, leaf_node_mask, leaf_bbox, get_voxels_fn);
539 },
540 [&](const Span<openvdb::Coord> voxels) {
541 process_voxels(fn, input_values, output_grids, *transform, voxels);
542 },
543 [&](const Span<openvdb::CoordBBox> tiles) {
544 process_tiles(fn, input_values, output_grids, *transform, tiles);
545 });
546
547 process_background(fn, input_values, *transform, output_grids);
548
549 return {EvalResult::Success{std::move(output_grids)}};
550}
551
552} // namespace blender::bke::volume_grid::multi_function_eval
553
554#endif
CustomData interface, see also DNA_customdata_types.h.
#define TIP_(msgid)
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define BLI_NOINLINE
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
long long int int64_t
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
#define index(i)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
const ccl_global KernelWorkTile * tile
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
int context(const bContext *C, const char *member, bContextDataResult *result)