24namespace blender::bke::volume_grid::multi_function_eval {
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)
47 AlignedBuffer<8192, 8> allocation_buffer;
48 ResourceScope scope(allocation_buffer);
51 const IndexMask index_mask = IndexMask::from_predicate(
52 IndexRange(LeafNodeMask::SIZE),
54 [&](
const int64_t i) { return leaf_node_mask.isOn(i); },
57 mf::ParamsBuilder params{fn, &index_mask};
62 const openvdb::Coord any_voxel_in_leaf = leaf_bbox.min();
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());
71 return *voxel_coords_opt;
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 ¶m_cpp_type = param_type.data_type().single_type();
79 if (
const openvdb::GridBase *
const *grid_base = std::get_if<const openvdb::GridBase *>(
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();
89 if (
const auto *leaf_node = tree.probeLeaf(any_voxel_in_leaf)) {
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());
97 const openvdb::Coord &coord = voxels[i];
98 values[i] = tree.getValue(coord);
100 params.add_readonly_single_input(values);
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()) {
108 params.add_readonly_single_input(
109 GSpan(param_cpp_type, values.data(), values.size()));
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;
119 params.add_readonly_single_input(
120 GSpan(param_cpp_type, copied_values.data(), copied_values.size()));
128 const auto &single_value = tree.getValue(any_voxel_in_leaf);
129 params.add_readonly_single_input(GPointer(param_cpp_type, &single_value));
133 else if (
const fn::GField *
const *field = std::get_if<const fn::GField *>(&value_variant)) {
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};
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);
148 params.add_readonly_single_input(std::get<GPointer>(value_variant));
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 ¶m_cpp_type = param_type.data_type().single_type();
155 if (!output_grids[output_i]) {
156 params.add_ignored_single_output();
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;
165 auto &tree = grid.tree();
166 auto *leaf_node = tree.probeLeaf(any_voxel_in_leaf);
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);
178 ValueT *values = leaf_node->buffer().data();
179 params.add_uninitialized_single_output(
180 GMutableSpan(param_cpp_type, values, LeafNodeMask::SIZE));
187 fn.prepare_for_execution();
188 fn.call_auto(index_mask, params, context);
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 ¶m_cpp_type = param_type.data_type().single_type();
194 if (!param_cpp_type.is<
bool>()) {
197 set_mask_leaf_buffer_from_bools(
static_cast<openvdb::BoolGrid &
>(*output_grids[output_i]),
198 params.computed_array(param_index).typed<
bool>(),
200 ensure_voxel_coords());
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)
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};
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 ¶m_cpp_type = param_type.data_type().single_type();
232 if (
const openvdb::GridBase *
const *grid_base = std::get_if<const openvdb::GridBase *>(
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();
242 auto accessor = grid.getConstUnsafeAccessor();
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);
249 BLI_assert(param_cpp_type.size ==
sizeof(ValueType));
250 params.add_readonly_single_input(GSpan(param_cpp_type, values.data(), voxels_num));
253 else if (
const fn::GField *
const *field = std::get_if<const fn::GField *>(&value_variant)) {
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);
266 params.add_readonly_single_input(std::get<GPointer>(value_variant));
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});
281 fn.call_auto(index_mask, params, context);
284 for (
const int output_i : output_grids.index_range()) {
285 if (!output_grids[output_i]) {
288 const int param_index = input_values.
size() + output_i;
289 set_grid_values(*output_grids[output_i], params.computed_array(param_index), voxels);
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)
311 const IndexMask index_mask{tiles_num};
313 AlignedBuffer<8192, 8> allocation_buffer;
314 ResourceScope scope(allocation_buffer);
315 mf::ParamsBuilder params{fn, &index_mask};
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 ¶m_cpp_type = param_type.data_type().single_type();
323 if (
const openvdb::GridBase *
const *grid_base = std::get_if<const openvdb::GridBase *>(
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();
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];
338 const openvdb::Coord any_coord_in_tile =
tile.min();
339 values[i] = tree.getValue(any_coord_in_tile, accessor);
341 BLI_assert(param_cpp_type.size ==
sizeof(ValueType));
342 params.add_readonly_single_input(GSpan(param_cpp_type, values.data(), tiles_num));
345 else if (
const fn::GField *
const *field = std::get_if<const fn::GField *>(&value_variant)) {
348 const CPPType &type = (*field)->cpp_type();
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);
358 params.add_readonly_single_input(std::get<GPointer>(value_variant));
364 for ([[maybe_unused]]
const int output_i : output_grids.index_range()) {
365 if (!output_grids[output_i]) {
366 params.add_ignored_single_output();
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});
377 fn.call_auto(index_mask, params, context);
380 for (
const int output_i : output_grids.index_range()) {
381 if (!output_grids[output_i]) {
384 const int param_index = input_values.
size() + output_i;
385 set_tile_values(*output_grids[output_i], params.computed_array(param_index),
tiles);
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)
394 AlignedBuffer<160, 8> allocation_buffer;
395 ResourceScope scope(allocation_buffer);
397 const IndexMask
mask(1);
398 mf::ParamsBuilder params(fn, &
mask);
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 ¶m_cpp_type = param_type.data_type().single_type();
406 if (
const openvdb::GridBase *
const *grid_base = std::get_if<const openvdb::GridBase *>(
409 to_typed_grid(**grid_base, [&](
const auto &grid) {
411 using GridT = std::decay_t<
decltype(grid)>;
412 using ValueType =
typename GridT::ValueType;
413 BLI_assert(param_cpp_type.size ==
sizeof(ValueType));
415 const auto &tree = grid.tree();
416 params.add_readonly_single_input(GPointer(param_cpp_type, &tree.background()));
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()));
434 params.add_readonly_single_input(std::get<GPointer>(value_variant));
437 for ([[maybe_unused]]
const int output_i : output_grids.index_range()) {
438 if (!output_grids[output_i]) {
439 params.add_ignored_single_output();
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();
446 GMutableSpan value_buffer(type, scope.allocator().allocate(type), 1);
447 params.add_uninitialized_single_output(value_buffer);
450 fn.call_auto(
mask, params, context);
452 for ([[maybe_unused]]
const int output_i : output_grids.index_range()) {
453 if (!output_grids[output_i]) {
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()));
462EvalResult evaluate_multi_function_on_grid(
const mf::MultiFunction &fn,
463 const Span<InputVariant> input_values,
464 const Span<bool> output_usages)
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) {
473 else if (param_type.interface_type() == mf::ParamType::Output) {
483 Array<bke::VolumeTreeAccessToken> input_volume_tokens(inputs_num);
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 *>(
491 input_grids.append(*grid);
495 const openvdb::math::Transform *
transform =
nullptr;
496 for (
const openvdb::GridBase *grid : input_grids) {
497 const openvdb::math::Transform &other_transform = grid->transform();
503 return {EvalResult::Failure{
TIP_(
"Input grids have incompatible transforms")}};
507 return {EvalResult::Failure{
TIP_(
"No input grid found that can determine the topology")}};
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()); });
515 Array<openvdb::GridBase::Ptr> output_grids(outputs_num);
516 for (
const int i : IndexRange(outputs_num)) {
517 if (!output_usages[i]) {
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);
525 return {EvalResult::Failure{
TIP_(
"Grid type not supported")}};
527 output_grids[i] = create_grid_with_topology(mask_tree, *
transform, *grid_type);
530 fn.prepare_for_execution();
532 parallel_grid_topology_tasks(
534 [&](
const LeafNodeMask &leaf_node_mask,
535 const openvdb::CoordBBox &leaf_bbox,
536 const GetVoxelsFn get_voxels_fn) {
538 fn, input_values, output_grids, *
transform, leaf_node_mask, leaf_bbox, get_voxels_fn);
540 [&](
const Span<openvdb::Coord> voxels) {
541 process_voxels(fn, input_values, output_grids, *
transform, voxels);
543 [&](
const Span<openvdb::CoordBBox>
tiles) {
547 process_background(fn, input_values, *
transform, output_grids);
549 return {EvalResult::Success{std::move(output_grids)}};
CustomData interface, see also DNA_customdata_types.h.
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
constexpr int64_t size() const
constexpr IndexRange index_range() const
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)