Blender V5.3
volume_grid.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BKE_customdata.hh"
11
12#include "BKE_volume_grid.hh"
15#include "BKE_volume_openvdb.hh"
16
17#include "BLI_index_mask.hh"
18#include "BLI_memory_counter.hh"
19#include "BLI_resource_scope.hh"
20#include "BLI_task.hh"
21
23
24#ifdef WITH_OPENVDB
25# include <openvdb/Grid.h>
26# include <openvdb/tools/Prune.h>
27# include <openvdb/tree/LeafManager.h>
28#endif
29
31
32#ifdef WITH_OPENVDB
33
34VolumeGridData::VolumeGridData()
35{
36 tree_access_token_ = std::make_shared<AccessToken>(*this);
37}
38
39struct CreateGridOp {
40 template<typename GridT> openvdb::GridBase::Ptr operator()() const
41 {
42 return GridT::create();
43 }
44};
45
46static openvdb::GridBase::Ptr create_grid_for_type(const VolumeGridType grid_type)
47{
48 return BKE_volume_grid_type_operation(grid_type, CreateGridOp{});
49}
50
51VolumeGridData::VolumeGridData(const VolumeGridType grid_type)
52 : VolumeGridData(create_grid_for_type(grid_type))
53{
54}
55
56VolumeGridData::VolumeGridData(std::shared_ptr<openvdb::GridBase> grid)
57 : grid_(std::move(grid)), tree_loaded_(true), transform_loaded_(true), meta_data_loaded_(true)
58{
59 BLI_assert(grid_);
60 BLI_assert(grid_.use_count() == 1);
61 BLI_assert(grid_->isTreeUnique());
62
63 tree_sharing_info_ = OpenvdbTreeSharingInfo::make(grid_->baseTreePtr());
64 tree_access_token_ = std::make_shared<AccessToken>(*this);
65}
66
67VolumeGridData::VolumeGridData(std::function<LazyLoadedGrid()> lazy_load_grid,
68 std::shared_ptr<openvdb::GridBase> meta_data_and_transform_grid)
69 : grid_(std::move(meta_data_and_transform_grid)), lazy_load_grid_(std::move(lazy_load_grid))
70{
71 if (grid_) {
72 transform_loaded_ = true;
73 meta_data_loaded_ = true;
74 }
75 tree_access_token_ = std::make_shared<AccessToken>(*this);
76}
77
78VolumeGridData::~VolumeGridData()
79{
80 /* When destructed the grid data should not have any active access tokens. */
81 BLI_assert(tree_access_token_.use_count() == 1);
82}
83
84void VolumeGridData::delete_self()
85{
86 MEM_delete(this);
87}
88
89const openvdb::GridBase &VolumeGridData::grid(VolumeTreeAccessToken &r_token) const
90{
91 return *this->grid_ptr(r_token);
92}
93
94openvdb::GridBase &VolumeGridData::grid_for_write(VolumeTreeAccessToken &r_token)
95{
96 return *this->grid_ptr_for_write(r_token);
97}
98
99std::shared_ptr<const openvdb::GridBase> VolumeGridData::grid_ptr(
100 VolumeTreeAccessToken &r_token) const
101{
102 std::lock_guard lock{mutex_};
103 this->ensure_grid_loaded();
104 r_token.token_ = tree_access_token_;
105 return grid_;
106}
107
108std::shared_ptr<openvdb::GridBase> VolumeGridData::grid_ptr_for_write(
109 VolumeTreeAccessToken &r_token)
110{
111 BLI_assert(this->is_mutable());
112 std::lock_guard lock{mutex_};
113 this->ensure_grid_loaded();
114 r_token.token_ = tree_access_token_;
115 if (tree_sharing_info_->is_mutable()) {
116 tree_sharing_info_->tag_ensured_mutable();
117 }
118 else {
119 auto tree_copy = grid_->baseTree().copy();
120 grid_->setTree(tree_copy);
121 tree_sharing_info_ = OpenvdbTreeSharingInfo::make(std::move(tree_copy));
122 }
123 /* Can't reload the grid anymore if it has been changed. */
124 lazy_load_grid_ = {};
125 return grid_;
126}
127
128const openvdb::math::Transform &VolumeGridData::transform() const
129{
130 std::lock_guard lock{mutex_};
131 if (!transform_loaded_) {
132 this->ensure_grid_loaded();
133 }
134 return grid_->transform();
135}
136
137openvdb::math::Transform &VolumeGridData::transform_for_write()
138{
139 BLI_assert(this->is_mutable());
140 std::lock_guard lock{mutex_};
141 if (!transform_loaded_) {
142 this->ensure_grid_loaded();
143 }
144 return grid_->transform();
145}
146
147std::string VolumeGridData::name() const
148{
149 std::lock_guard lock{mutex_};
150 if (!meta_data_loaded_) {
151 this->ensure_grid_loaded();
152 }
153 return grid_->getName();
154}
155
156void VolumeGridData::set_name(const StringRef name)
157{
158 BLI_assert(this->is_mutable());
159 std::lock_guard lock{mutex_};
160 if (!meta_data_loaded_) {
161 this->ensure_grid_loaded();
162 }
163 grid_->setName(name);
164}
165
166VolumeGridType VolumeGridData::grid_type() const
167{
168 std::lock_guard lock{mutex_};
169 if (!meta_data_loaded_) {
170 this->ensure_grid_loaded();
171 }
172 return get_type(*grid_);
173}
174
175std::optional<VolumeGridType> VolumeGridData::grid_type_without_load() const
176{
177 std::lock_guard lock{mutex_};
178 if (!meta_data_loaded_) {
179 return std::nullopt;
180 }
181 return get_type(*grid_);
182}
183
184openvdb::GridClass VolumeGridData::grid_class() const
185{
186 std::lock_guard lock{mutex_};
187 if (!meta_data_loaded_) {
188 this->ensure_grid_loaded();
189 }
190 return grid_->getGridClass();
191}
192
193bool VolumeGridData::is_reloadable() const
194{
195 return bool(lazy_load_grid_);
196}
197
198void VolumeGridData::tag_tree_modified() const
199{
200 active_voxels_mutex_.tag_dirty();
201 active_leaf_voxels_mutex_.tag_dirty();
202 active_tiles_mutex_.tag_dirty();
203 size_in_bytes_mutex_.tag_dirty();
204 active_bounds_mutex_.tag_dirty();
205}
206
207bool VolumeGridData::is_loaded() const
208{
209 std::lock_guard lock{mutex_};
210 return tree_loaded_ && transform_loaded_ && meta_data_loaded_;
211}
212
213const CPPType *VolumeGridData::cpp_type() const
214{
215 const VolumeGridType grid_type = this->grid_type();
216 const CPPType *cpp_type = nullptr;
217 BKE_volume_grid_type_to_blender_value_type(grid_type,
218 [&]<typename T>() { cpp_type = &CPPType::get<T>(); });
219 return cpp_type;
220}
221
222void VolumeGridData::count_memory(MemoryCounter &memory) const
223{
224 std::lock_guard lock{mutex_};
225 if (!tree_loaded_) {
226 return;
227 }
228 memory.add_shared(tree_sharing_info_.get(), [&](MemoryCounter &shared_memory) {
229 shared_memory.add(this->size_in_bytes());
230 });
231}
232
233int64_t VolumeGridData::active_voxels() const
234{
235 active_voxels_mutex_.ensure([&]() {
236 VolumeTreeAccessToken token;
237 const openvdb::GridBase &grid = this->grid(token);
238 const openvdb::TreeBase &tree = grid.baseTree();
239 active_voxels_ = tree.activeVoxelCount();
240 });
241 return active_voxels_;
242}
243
244int64_t VolumeGridData::active_leaf_voxels() const
245{
246 active_leaf_voxels_mutex_.ensure([&]() {
247 VolumeTreeAccessToken token;
248 const openvdb::GridBase &grid = this->grid(token);
249 const openvdb::TreeBase &tree = grid.baseTree();
250 active_leaf_voxels_ = tree.activeLeafVoxelCount();
251 });
252 return active_leaf_voxels_;
253}
254
255int64_t VolumeGridData::active_tiles() const
256{
257 active_tiles_mutex_.ensure([&]() {
258 VolumeTreeAccessToken token;
259 const openvdb::GridBase &grid = this->grid(token);
260 const openvdb::TreeBase &tree = grid.baseTree();
261 active_tiles_ = tree.activeTileCount();
262 });
263 return active_tiles_;
264}
265
266int64_t VolumeGridData::size_in_bytes() const
267{
268 size_in_bytes_mutex_.ensure([&]() {
269 VolumeTreeAccessToken token;
270 const openvdb::GridBase &grid = this->grid(token);
271 const openvdb::TreeBase &tree = grid.baseTree();
272 size_in_bytes_ = tree.memUsage();
273 });
274 return size_in_bytes_;
275}
276
277const openvdb::CoordBBox &VolumeGridData::active_bounds() const
278{
279 active_bounds_mutex_.ensure([&]() {
280 VolumeTreeAccessToken token;
281 const openvdb::GridBase &grid = this->grid(token);
282 const openvdb::TreeBase &tree = grid.baseTree();
283 tree.evalActiveVoxelBoundingBox(active_bounds_);
284 });
285 return active_bounds_;
286}
287
288std::string VolumeGridData::error_message() const
289{
290 std::lock_guard lock{mutex_};
291 return error_message_;
292}
293
294void VolumeGridData::unload_tree_if_possible() const
295{
296 std::lock_guard lock{mutex_};
297 if (!grid_) {
298 return;
299 }
300 if (!tree_loaded_) {
301 return;
302 }
303 if (!this->is_reloadable()) {
304 return;
305 }
306 if (tree_access_token_.use_count() != 1) {
307 /* Some code is using the tree currently, so it can't be freed. */
308 return;
309 }
310 grid_->newTree();
311 tree_loaded_ = false;
312 tree_sharing_info_.reset();
313}
314
315GVolumeGrid VolumeGridData::copy() const
316{
317 std::lock_guard lock{mutex_};
318 this->ensure_grid_loaded();
319 /* Can't use #MEM_new because the default constructor is private. */
320 VolumeGridData *new_copy = new (MEM_new_uninitialized(sizeof(VolumeGridData), __func__))
321 VolumeGridData();
322 /* Makes a deep copy of the meta-data but shares the tree. */
323 new_copy->grid_ = grid_->copyGrid();
324 new_copy->tree_sharing_info_ = tree_sharing_info_;
325 new_copy->tree_loaded_ = tree_loaded_;
326 new_copy->transform_loaded_ = transform_loaded_;
327 new_copy->meta_data_loaded_ = meta_data_loaded_;
328 return GVolumeGrid(new_copy);
329}
330
331void VolumeGridData::ensure_grid_loaded() const
332{
333 /* Assert that the mutex is locked. */
334 BLI_assert(!mutex_.try_lock());
335
336 if (tree_loaded_ && transform_loaded_ && meta_data_loaded_) {
337 return;
338 }
339 BLI_assert(lazy_load_grid_);
340 LazyLoadedGrid loaded_grid;
341 /* Isolate because the a mutex is locked. */
342 threading::isolate_task([&]() {
343 error_message_.clear();
344 try {
345 loaded_grid = lazy_load_grid_();
346 }
347 catch (const openvdb::IoError &e) {
348 error_message_ = e.what();
349 }
350 catch (...) {
351 error_message_ = "Unknown error reading VDB file";
352 }
353 });
354 if (!loaded_grid.grid) {
355 BLI_assert(!loaded_grid.tree_sharing_info);
356 if (grid_) {
357 const openvdb::Name &grid_type = grid_->type();
358 if (openvdb::GridBase::isRegistered(grid_type)) {
359 /* Create a dummy grid of the expected type. */
360 loaded_grid.grid = openvdb::GridBase::createGrid(grid_type);
361 }
362 }
363 }
364 if (!loaded_grid.grid) {
365 /* Create a dummy grid. We can't really know the expected data type here. */
366 loaded_grid.grid = openvdb::FloatGrid::create();
367 }
368 BLI_assert(loaded_grid.grid);
369 BLI_assert(loaded_grid.grid.use_count() == 1);
370
371 if (!loaded_grid.tree_sharing_info) {
372 BLI_assert(loaded_grid.grid->isTreeUnique());
373 loaded_grid.tree_sharing_info = OpenvdbTreeSharingInfo::make(loaded_grid.grid->baseTreePtr());
374 }
375
376 if (grid_) {
377 /* Keep the existing grid pointer and just insert the newly loaded data. */
378 BLI_assert(!tree_loaded_);
379 BLI_assert(meta_data_loaded_);
380 grid_->setTree(loaded_grid.grid->baseTreePtr());
381 if (!transform_loaded_) {
382 grid_->setTransform(loaded_grid.grid->transformPtr());
383 }
384 }
385 else {
386 grid_ = std::move(loaded_grid.grid);
387 }
388
389 BLI_assert(!tree_sharing_info_);
390 BLI_assert(loaded_grid.tree_sharing_info);
391 tree_sharing_info_ = std::move(loaded_grid.tree_sharing_info);
392
393 tree_loaded_ = true;
394 transform_loaded_ = true;
395 meta_data_loaded_ = true;
396}
397
398GVolumeGrid::GVolumeGrid(std::shared_ptr<openvdb::GridBase> grid)
399{
400 data_ = ImplicitSharingPtr(MEM_new<VolumeGridData>(__func__, std::move(grid)));
401}
402
403GVolumeGrid::GVolumeGrid(const VolumeGridType grid_type)
404 : GVolumeGrid(create_grid_for_type(grid_type))
405{
406}
407
408VolumeGridData &GVolumeGrid::get_for_write()
409{
410 BLI_assert(*this);
411 if (data_->is_mutable()) {
412 data_->tag_ensured_mutable();
413 }
414 else {
415 *this = data_->copy();
416 }
417 return const_cast<VolumeGridData &>(*data_);
418}
419
420VolumeGridType get_type(const openvdb::TreeBase &tree)
421{
422 if (tree.isType<openvdb::FloatTree>()) {
423 return VOLUME_GRID_FLOAT;
424 }
425 if (tree.isType<openvdb::Vec3fTree>()) {
427 }
428 if (tree.isType<openvdb::BoolTree>()) {
429 return VOLUME_GRID_BOOLEAN;
430 }
431 if (tree.isType<openvdb::DoubleTree>()) {
432 return VOLUME_GRID_DOUBLE;
433 }
434 if (tree.isType<openvdb::Int32Tree>()) {
435 return VOLUME_GRID_INT;
436 }
437 if (tree.isType<openvdb::Int64Tree>()) {
438 return VOLUME_GRID_INT64;
439 }
440 if (tree.isType<openvdb::Vec3ITree>()) {
442 }
443 if (tree.isType<openvdb::Vec3dTree>()) {
445 }
446 if (tree.isType<openvdb::MaskTree>()) {
447 return VOLUME_GRID_MASK;
448 }
449 if (tree.isType<openvdb::points::PointDataTree>()) {
450 return VOLUME_GRID_POINTS;
451 }
452 return VOLUME_GRID_UNKNOWN;
453}
454
455VolumeGridType get_type(const openvdb::GridBase &grid)
456{
457 return get_type(grid.baseTree());
458}
459
460ImplicitSharingPtr<> OpenvdbTreeSharingInfo::make(std::shared_ptr<openvdb::tree::TreeBase> tree)
461{
462 return ImplicitSharingPtr<>{MEM_new<OpenvdbTreeSharingInfo>(__func__, std::move(tree))};
463}
464
465OpenvdbTreeSharingInfo::OpenvdbTreeSharingInfo(std::shared_ptr<openvdb::tree::TreeBase> tree)
466 : tree_(std::move(tree))
467{
468}
469
470void OpenvdbTreeSharingInfo::delete_self_with_data()
471{
472 MEM_delete(this);
473}
474
475void OpenvdbTreeSharingInfo::delete_data_only()
476{
477 tree_.reset();
478}
479
480VolumeTreeAccessToken::~VolumeTreeAccessToken()
481{
482 const VolumeGridData *grid = token_ ? &token_->grid : nullptr;
483 token_.reset();
484 if (grid) {
485 /* Unload immediately when the value is not used anymore. However, the tree may still be cached
486 * at a deeper level and thus usually does not have to be loaded from disk again. */
487 grid->unload_tree_if_possible();
488 }
489}
490
491#endif /* WITH_OPENVDB */
492
493std::string get_name(const VolumeGridData &grid)
494{
495#ifdef WITH_OPENVDB
496 return grid.name();
497#else
498 UNUSED_VARS(grid);
499 return "density";
500#endif
501}
502
503VolumeGridType get_type(const VolumeGridData &grid)
504{
505#ifdef WITH_OPENVDB
506 return grid.grid_type();
507#else
508 UNUSED_VARS(grid);
509 return VOLUME_GRID_UNKNOWN;
510#endif
511}
512
514{
515 switch (type) {
519 case VOLUME_GRID_INT:
521 case VOLUME_GRID_MASK:
522 return 1;
526 return 3;
529 return 0;
530 }
531 return 0;
532}
533
534float4x4 get_transform_matrix(const VolumeGridData &grid)
535{
536#ifdef WITH_OPENVDB
537 return BKE_volume_transform_to_blender(grid.transform());
538#else
539 UNUSED_VARS(grid);
540 return float4x4::identity();
541#endif
542}
543
544void set_transform_matrix(VolumeGridData &grid, const float4x4 &matrix)
545{
546#ifdef WITH_OPENVDB
547 grid.transform_for_write() = BKE_volume_transform_to_openvdb(matrix);
548#else
549 UNUSED_VARS(grid, matrix);
550#endif
551}
552
553void clear_tree(VolumeGridData &grid)
554{
555#ifdef WITH_OPENVDB
556 VolumeTreeAccessToken tree_token;
557 grid.grid_for_write(tree_token).clear();
558 grid.tag_tree_modified();
559#else
560 UNUSED_VARS(grid);
561#endif
562}
563
564bool is_loaded(const VolumeGridData &grid)
565{
566#ifdef WITH_OPENVDB
567 return grid.is_loaded();
568#else
569 UNUSED_VARS(grid);
570 return false;
571#endif
572}
573
574void count_memory(const VolumeGridData &grid, MemoryCounter &memory)
575{
576#ifdef WITH_OPENVDB
577 grid.count_memory(memory);
578#else
579 UNUSED_VARS(grid, memory);
580#endif
581}
582
583void load(const VolumeGridData &grid)
584{
585#ifdef WITH_OPENVDB
586 VolumeTreeAccessToken tree_token;
587 /* Just "touch" the grid, so that it is loaded. */
588 grid.grid(tree_token);
589#else
590 UNUSED_VARS(grid);
591#endif
592}
593
594std::string error_message_from_load(const VolumeGridData &grid)
595{
596#ifdef WITH_OPENVDB
597 return grid.error_message();
598#else
599 UNUSED_VARS(grid);
600 return "";
601#endif
602}
603
604#ifdef WITH_OPENVDB
605
610template<typename LeafNodeT>
611static void parallel_grid_topology_tasks_leaf_node(const LeafNodeT &node,
612 const ProcessLeafFn process_leaf_fn,
614{
615 using NodeMaskT = typename LeafNodeT::NodeMaskType;
616
617 const int on_count = node.onVoxelCount();
618 /* This number is somewhat arbitrary. 64 is a 1/8th of the number of voxels in a standard leaf
619 * which is 8x8x8. It's a trade-off between benefiting from the better performance of
620 * leaf-processing vs. processing more voxels in a batch. */
621 const int on_count_threshold = 64;
622 if (on_count <= on_count_threshold) {
623 /* The leaf contains only a few active voxels. It's beneficial to process them in a batch with
624 * active voxels from other leafs. So only gather them here for later processing. */
625 for (auto value_iter = node.cbeginValueOn(); value_iter.test(); ++value_iter) {
626 const openvdb::Coord coord = value_iter.getCoord();
627 r_coords.append(coord);
628 }
629 return;
630 }
631 /* Process entire leaf at once. This is especially beneficial when very many of the voxels in
632 * the leaf are active. In that case, one can work on the openvdb arrays stored in the leafs
633 * directly. */
634 const NodeMaskT &value_mask = node.getValueMask();
635 const openvdb::CoordBBox bbox = node.getNodeBoundingBox();
636 process_leaf_fn(value_mask, bbox, [&](MutableSpan<openvdb::Coord> r_voxels) {
637 for (auto value_iter = node.cbeginValueOn(); value_iter.test(); ++value_iter) {
638 r_voxels[value_iter.pos()] = value_iter.getCoord();
639 }
640 });
641}
642
646template<typename InternalNodeT>
647static void parallel_grid_topology_tasks_internal_node(const InternalNodeT &node,
648 const ProcessLeafFn process_leaf_fn,
649 const ProcessVoxelsFn process_voxels_fn,
650 const ProcessTilesFn process_tiles_fn)
651{
652 using ChildNodeT = typename InternalNodeT::ChildNodeType;
653 using LeafNodeT = typename InternalNodeT::LeafNodeType;
654 using NodeMaskT = typename InternalNodeT::NodeMaskType;
655 using UnionT = typename InternalNodeT::UnionType;
656
657 /* Gather the active sub-nodes first, to be able to parallelize over them more easily. */
658 const NodeMaskT &child_mask = node.getChildMask();
659 const UnionT *table = node.getTable();
660 Vector<int, 512> child_indices;
661 for (auto child_mask_iter = child_mask.beginOn(); child_mask_iter.test(); ++child_mask_iter) {
662 child_indices.append(child_mask_iter.pos());
663 }
664
665 threading::parallel_for(child_indices.index_range(), 8, [&](const IndexRange range) {
666 /* Voxels collected from potentially multiple leaf nodes to be processed in one batch. This
667 * inline buffer size is sufficient to avoid an allocation in all cases (a single standard leaf
668 * has 512 voxels). */
669 Vector<openvdb::Coord, 1024> gathered_voxels;
670 for (const int child_index : child_indices.as_span().slice(range)) {
671 const ChildNodeT &child = *table[child_index].getChild();
672 if constexpr (std::is_same_v<ChildNodeT, LeafNodeT>) {
673 parallel_grid_topology_tasks_leaf_node(child, process_leaf_fn, gathered_voxels);
674 /* If enough voxels have been gathered, process them in one batch. */
675 if (gathered_voxels.size() >= 512) {
676 process_voxels_fn(gathered_voxels);
677 gathered_voxels.clear();
678 }
679 }
680 else {
681 /* Recurse into lower-level internal nodes. */
682 parallel_grid_topology_tasks_internal_node(
683 child, process_leaf_fn, process_voxels_fn, process_tiles_fn);
684 }
685 }
686 /* Process any remaining voxels. */
687 if (!gathered_voxels.is_empty()) {
688 process_voxels_fn(gathered_voxels);
689 gathered_voxels.clear();
690 }
691 });
692
693 /* Process the active tiles within the internal node. Note that these are not processed above
694 * already because there only sub-nodes are handled, but tiles are "inlined" into internal nodes.
695 * All tiles are first gathered and then processed in one batch. */
696 const NodeMaskT &value_mask = node.getValueMask();
697 Vector<openvdb::CoordBBox> tile_bboxes;
698 for (auto value_mask_iter = value_mask.beginOn(); value_mask_iter.test(); ++value_mask_iter) {
699 const openvdb::Index32 index = value_mask_iter.pos();
700 const openvdb::Coord tile_origin = node.offsetToGlobalCoord(index);
701 const openvdb::CoordBBox tile_bbox = openvdb::CoordBBox::createCube(tile_origin,
702 ChildNodeT::DIM);
703 tile_bboxes.append(tile_bbox);
704 }
705 if (!tile_bboxes.is_empty()) {
706 process_tiles_fn(tile_bboxes);
707 }
708}
709
710/* Call the process functions on all active tiles and voxels in the given tree. */
711void parallel_grid_topology_tasks(const openvdb::MaskTree &mask_tree,
712 const ProcessLeafFn process_leaf_fn,
713 const ProcessVoxelsFn process_voxels_fn,
714 const ProcessTilesFn process_tiles_fn)
715{
716 /* Iterate over the root internal nodes. */
717 for (auto root_child_iter = mask_tree.cbeginRootChildren(); root_child_iter.test();
718 ++root_child_iter)
719 {
720 const auto &internal_node = *root_child_iter;
721 parallel_grid_topology_tasks_internal_node(
722 internal_node, process_leaf_fn, process_voxels_fn, process_tiles_fn);
723 }
724}
725
726openvdb::GridBase::Ptr create_grid_with_topology(const openvdb::MaskTree &topology,
727 const openvdb::math::Transform &transform,
728 const VolumeGridType grid_type)
729{
730 openvdb::GridBase::Ptr grid;
731 BKE_volume_grid_type_to_static_type(
732 grid_type, [&]<std::derived_from<openvdb::GridBase> GridT>() {
733 using TreeT = typename GridT::TreeType;
734 using ValueType = typename TreeT::ValueType;
735 const ValueType background{};
736 auto tree = std::make_shared<TreeT>(topology, background, openvdb::TopologyCopy());
737 grid = openvdb::createGrid(std::move(tree));
738 grid->setTransform(transform.copy());
739 });
740 return grid;
741}
742
743void set_grid_values(openvdb::GridBase &grid_base,
744 const GSpan values,
745 const Span<openvdb::Coord> voxels)
746{
747 BLI_assert(values.size() == voxels.size());
748 to_typed_grid(grid_base, [&](auto &grid) {
749 using GridT = std::decay_t<decltype(grid)>;
750 using ValueType = typename GridT::ValueType;
751 const ValueType *data = static_cast<const ValueType *>(values.data());
752
753 auto accessor = grid.getUnsafeAccessor();
754 for (const int64_t i : voxels.index_range()) {
755 accessor.setValue(voxels[i], data[i]);
756 }
757 });
758}
759
760void set_tile_values(openvdb::GridBase &grid_base,
761 const GSpan values,
763{
764 BLI_assert(values.size() == tiles.size());
765 to_typed_grid(grid_base, [&](auto &grid) {
766 using GridT = typename std::decay_t<decltype(grid)>;
767 using TreeT = typename GridT::TreeType;
768 using ValueType = typename GridT::ValueType;
769 auto &tree = grid.tree();
770
771 const ValueType *computed_values = static_cast<const ValueType *>(values.data());
772
773 const auto set_tile_value = [&](auto &node, const openvdb::Coord &coord_in_tile, auto value) {
774 const openvdb::Index n = node.coordToOffset(coord_in_tile);
775 BLI_assert(node.isChildMaskOff(n));
776 /* TODO: Figure out how to do this without const_cast, although the same is done in
777 * `openvdb_ax/openvdb_ax/compiler/VolumeExecutable.cc` which has a similar purpose.
778 * It seems like OpenVDB generally allows that, but it does not have a proper public
779 * API for this yet. */
780 using UnionType = typename std::decay_t<decltype(node)>::UnionType;
781 auto *table = const_cast<UnionType *>(node.getTable());
782 table[n].setValue(value);
783 };
784
785 for (const int i : tiles.index_range()) {
786 const openvdb::CoordBBox tile = tiles[i];
787 const openvdb::Coord coord_in_tile = tile.min();
788 const auto &computed_value = computed_values[i];
789 using InternalNode1 = typename TreeT::RootNodeType::ChildNodeType;
790 using InternalNode2 = typename InternalNode1::ChildNodeType;
791 /* Find the internal node that contains the tile and update the value in there. */
792 if (auto *node = tree.template probeNode<InternalNode2>(coord_in_tile)) {
793 set_tile_value(*node, coord_in_tile, computed_value);
794 }
795 else if (auto *node = tree.template probeNode<InternalNode1>(coord_in_tile)) {
796 set_tile_value(*node, coord_in_tile, computed_value);
797 }
798 else {
800 }
801 }
802 });
803}
804
805void set_leaf_values_off(openvdb::GridBase &grid_base,
806 const openvdb::Coord &probe_coord,
807 const Span<bool> selection)
808{
809 to_typed_grid(grid_base, [&](auto &grid) {
810 using GridType = std::decay_t<decltype(grid)>;
811 using TreeType = typename GridType::TreeType;
812 using LeafNodeType = typename TreeType::LeafNodeType;
813 using NodeMaskType = typename LeafNodeType::NodeMaskType;
814
815 BLI_assert(selection.size() <= LeafNodeType::SIZE);
816
817 TreeType &tree = grid.tree();
818 LeafNodeType *leaf_node = tree.probeLeaf(probe_coord);
819 BLI_assert(leaf_node);
820 NodeMaskType &mask = leaf_node->getValueMask();
821
822 for (const int i : selection.index_range()) {
823 if (selection[i]) {
824 mask.setOff(i);
825 }
826 }
827 });
828}
829
830void set_grid_values_off(openvdb::GridBase &grid_base,
831 const Span<bool> selection,
832 const Span<openvdb::Coord> voxels)
833{
834 to_typed_grid(grid_base, [&](auto &grid) {
835 auto accessor = grid.getUnsafeAccessor();
836 for (const int i : selection.index_range()) {
837 if (selection[i]) {
838 accessor.setValueOff(voxels[i]);
839 }
840 }
841 });
842}
843
844void set_tile_values_off(openvdb::GridBase &grid_base,
845 const Span<bool> selection,
847{
848 to_typed_grid(grid_base, [&](auto &grid) {
849 using GridT = typename std::decay_t<decltype(grid)>;
850 using TreeT = typename GridT::TreeType;
851 auto &tree = grid.tree();
852
853 const auto set_tile_value_off = [&](auto &node, const openvdb::Coord &coord_in_tile) {
854 const openvdb::Index n = node.coordToOffset(coord_in_tile);
855 node.setValueOffUnsafe(n);
856 };
857
858 for (const int i : selection.index_range()) {
859 if (!selection[i]) {
860 continue;
861 }
862
863 const openvdb::CoordBBox tile = tiles[i];
864 const openvdb::Coord coord_in_tile = tile.min();
865 using InternalNode1 = typename TreeT::RootNodeType::ChildNodeType;
866 using InternalNode2 = typename InternalNode1::ChildNodeType;
867 /* Find the internal node that contains the tile and update the value in there. */
868 if (auto *node = tree.template probeNode<InternalNode2>(coord_in_tile)) {
869 set_tile_value_off(*node, coord_in_tile);
870 }
871 else if (auto *node = tree.template probeNode<InternalNode1>(coord_in_tile)) {
872 set_tile_value_off(*node, coord_in_tile);
873 }
874 else {
876 }
877 }
878 });
879}
880
881void set_mask_leaf_buffer_from_bools(openvdb::BoolGrid &grid,
882 const Span<bool> values,
883 const IndexMask &index_mask,
884 const Span<openvdb::Coord> voxels)
885{
886 auto accessor = grid.getUnsafeAccessor();
887 /* Could probably use int16_t for the iteration index. Double check this. */
888 index_mask.foreach_index_optimized<int>([&](const int i) {
889 const openvdb::Coord &coord = voxels[i];
890 accessor.setValue(coord, values[i]);
891 });
892}
893
894void set_grid_background(openvdb::GridBase &grid_base, const GPointer value)
895{
896 to_typed_grid(grid_base, [&](auto &grid) {
897 using GridT = std::decay_t<decltype(grid)>;
898 using ValueType = typename GridT::ValueType;
899 auto &tree = grid.tree();
900
901 BLI_assert(value.type()->size == sizeof(ValueType));
902 tree.root().setBackground(*static_cast<const ValueType *>(value.get()), true);
903 });
904}
905
906void set_inactive_values(openvdb::GridBase &grid_base, const GPointer value)
907{
908 to_typed_grid(grid_base, [&](auto &grid) {
909 using GridT = std::decay_t<decltype(grid)>;
910 using ValueType = typename GridT::ValueType;
911 auto &tree = grid.tree();
912
913 BLI_assert(value.type()->size == sizeof(ValueType));
914 const ValueType &new_value = *static_cast<const ValueType *>(value.get());
915
916 using TreeT = typename GridT::TreeType;
917 /* Thread over whole leaf nodes: bool leaves store values as bits in shared words, so writing
918 * neighboring voxels from different threads would race. */
919 openvdb::tree::LeafManager<TreeT> leaf_manager(tree);
920 leaf_manager.foreach([&](typename TreeT::LeafNodeType &leaf, size_t /*index*/) {
921 for (auto iter = leaf.beginValueOff(); iter; ++iter) {
922 iter.setValue(new_value);
923 }
924 });
925 /* Inactive tiles in internal and root nodes. */
926 auto tile_iter = tree.beginValueOff();
927 tile_iter.setMaxDepth(tile_iter.getLeafDepth() - 1);
928 for (; tile_iter; ++tile_iter) {
929 tile_iter.setValue(new_value);
930 }
931 });
932}
933
934void prune_inactive(openvdb::GridBase &grid_base)
935{
936 to_typed_grid(grid_base, [&](auto &grid) { openvdb::tools::pruneInactive(grid.tree()); });
937}
938
939template<typename T>
940static void sample_tree_indices(const bke::OpenvdbTreeType<T> &tree,
941 const Span<int> x,
942 const Span<int> y,
943 const Span<int> z,
944 const IndexMask &mask,
945 MutableSpan<T> dst)
946{
947 using TreeType = bke::OpenvdbTreeType<T>;
948 using TreeValueT = typename TreeType::ValueType;
949 using AccessorT = typename TreeType::ConstUnsafeAccessor;
950 using TraitsT = typename bke::VolumeGridTraits<T>;
951 /* Can use unsafe accessor because we know that the tree topology is not modified while we access
952 * it here. This reduces a significant amount of overhead. */
953 AccessorT accessor = const_cast<TreeType &>(tree).getConstUnsafeAccessor();
954
955 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
956 TreeValueT value = accessor.getValue(openvdb::Coord(x[i], y[i], z[i]));
957 dst[i] = TraitsT::to_blender(value);
958 });
959}
960
961void sample_tree_indices(const VolumeGridType grid_type,
962 const openvdb::TreeBase &tree_base,
963 Span<int> xs,
964 Span<int> ys,
965 Span<int> zs,
966 const IndexMask &mask,
967 GMutableSpan r_values)
968{
969 BLI_assert(grid_type == get_type(tree_base));
970 BKE_volume_grid_type_to_blender_value_type(grid_type, [&]<typename T>() {
971 if constexpr (is_same_any_v<T, bool, float, int, float3>) {
972 sample_tree_indices<T>(static_cast<const bke::OpenvdbTreeType<T> &>(tree_base),
973 xs,
974 ys,
975 zs,
976 mask,
977 r_values.typed<T>());
978 }
979 });
980}
981
982std::optional<VolumeGridType> cpp_type_to_grid_type(const CPPType &cpp_type)
983{
984 const std::optional<eCustomDataType> cd_type = bke::cpp_type_to_custom_data_type(cpp_type);
985 if (!cd_type) {
986 return std::nullopt;
987 }
988 return bke::custom_data_type_to_volume_grid_type(*cd_type);
989}
990
991BLI_NOINLINE static void process_leaf_node(const Span<fn::GField> fields,
992 const openvdb::math::Transform &transform,
993 const LeafNodeMask &leaf_node_mask,
994 const openvdb::CoordBBox &leaf_bbox,
995 const GetVoxelsFn get_voxels_fn,
996 const Span<openvdb::GridBase::Ptr> output_grids)
997{
998 AlignedBuffer<8192, 8> allocation_buffer;
999 ResourceScope scope(allocation_buffer);
1000
1001 const IndexMask index_mask = IndexMask::from_predicate(
1002 IndexRange(LeafNodeMask::SIZE),
1003 scope.allocator(),
1004 [&](const int64_t i) { return leaf_node_mask.isOn(i); },
1005 exec_mode::serial);
1006
1007 const openvdb::Coord any_voxel_in_leaf = leaf_bbox.min();
1008 MutableSpan<openvdb::Coord> voxels = scope.allocator().allocate_array<openvdb::Coord>(
1009 index_mask.min_array_size());
1010 get_voxels_fn(voxels);
1011
1012 bke::VoxelFieldContext field_context{transform, voxels};
1013 fn::FieldEvaluator evaluator{field_context, &index_mask};
1014
1015 Array<MutableSpan<bool>> boolean_outputs(fields.size());
1016 for (const int i : fields.index_range()) {
1017 const CPPType &type = fields[i].cpp_type();
1018 to_typed_grid(*output_grids[i], [&](auto &grid) {
1019 using GridT = typename std::decay_t<decltype(grid)>;
1020 using ValueT = typename GridT::ValueType;
1021
1022 auto &tree = grid.tree();
1023 auto *leaf_node = tree.probeLeaf(any_voxel_in_leaf);
1024 /* Should have been added before. */
1025 BLI_assert(leaf_node);
1026
1027 /* Boolean grids are special because they encode the values as bitmask. */
1028 if constexpr (std::is_same_v<ValueT, bool>) {
1029 boolean_outputs[i] = scope.allocator().allocate_array<bool>(index_mask.min_array_size());
1030 evaluator.add_with_destination(fields[i], boolean_outputs[i]);
1031 }
1032 else {
1033 /* Write directly into the buffer of the output leaf node. */
1034 ValueT *buffer = leaf_node->buffer().data();
1035 evaluator.add_with_destination(fields[i], GMutableSpan(type, buffer, LeafNodeMask::SIZE));
1036 }
1037 });
1038 }
1039
1040 evaluator.evaluate();
1041
1042 for (const int i : fields.index_range()) {
1043 if (!boolean_outputs[i].is_empty()) {
1044 set_mask_leaf_buffer_from_bools(static_cast<openvdb::BoolGrid &>(*output_grids[i]),
1045 boolean_outputs[i],
1046 index_mask,
1047 voxels);
1048 }
1049 }
1050}
1051
1052BLI_NOINLINE static void process_voxels(const Span<fn::GField> fields,
1053 const openvdb::math::Transform &transform,
1054 const Span<openvdb::Coord> voxels,
1055 const Span<openvdb::GridBase::Ptr> output_grids)
1056{
1057 const int64_t voxels_num = voxels.size();
1058 AlignedBuffer<8192, 8> allocation_buffer;
1059 ResourceScope scope(allocation_buffer);
1060
1061 bke::VoxelFieldContext field_context{transform, voxels};
1062 fn::FieldEvaluator evaluator{field_context, voxels_num};
1063
1064 Array<GMutableSpan> output_values(output_grids.size());
1065 for (const int i : fields.index_range()) {
1066 const CPPType &type = fields[i].cpp_type();
1067 output_values[i] = {type, scope.allocator().allocate_array(type, voxels_num), voxels_num};
1068 evaluator.add_with_destination(fields[i], output_values[i]);
1069 }
1070 evaluator.evaluate();
1071
1072 for (const int i : fields.index_range()) {
1073 set_grid_values(*output_grids[i], output_values[i], voxels);
1074 }
1075}
1076
1077BLI_NOINLINE static void process_tiles(const Span<fn::GField> fields,
1078 const openvdb::math::Transform &transform,
1080 const Span<openvdb::GridBase::Ptr> output_grids)
1081{
1082 const int64_t tiles_num = tiles.size();
1083 AlignedBuffer<8192, 8> allocation_buffer;
1084 ResourceScope scope(allocation_buffer);
1085
1086 bke::TilesFieldContext field_context{transform, tiles};
1087 fn::FieldEvaluator evaluator{field_context, tiles_num};
1088
1089 Array<GMutableSpan> output_values(output_grids.size());
1090 for (const int i : fields.index_range()) {
1091 const CPPType &type = fields[i].cpp_type();
1092 output_values[i] = {type, scope.allocator().allocate_array(type, tiles_num), tiles_num};
1093 evaluator.add_with_destination(fields[i], output_values[i]);
1094 }
1095 evaluator.evaluate();
1096
1097 for (const int i : fields.index_range()) {
1098 set_tile_values(*output_grids[i], output_values[i], tiles);
1099 }
1100}
1101
1102BLI_NOINLINE static void process_background(const Span<fn::GField> fields,
1103 const openvdb::math::Transform &transform,
1104 const Span<openvdb::GridBase::Ptr> output_grids)
1105{
1106 AlignedBuffer<256, 8> allocation_buffer;
1107 ResourceScope scope(allocation_buffer);
1108
1109 static const openvdb::CoordBBox background_space = openvdb::CoordBBox::inf();
1110 bke::TilesFieldContext field_context(transform, Span<openvdb::CoordBBox>(&background_space, 1));
1111 fn::FieldEvaluator evaluator(field_context, 1);
1112
1113 Array<GMutablePointer> output_values(output_grids.size());
1114 for (const int i : fields.index_range()) {
1115 const CPPType &type = fields[i].cpp_type();
1116 output_values[i] = {type, scope.allocator().allocate(type)};
1117 evaluator.add_with_destination(fields[i], GMutableSpan{type, output_values[i].get(), 1});
1118 }
1119 evaluator.evaluate();
1120
1121 for (const int i : fields.index_range()) {
1122 set_grid_background(*output_grids[i], output_values[i]);
1123 }
1124}
1125
1126void evaluate_fields_to_grid(const openvdb::MaskTree &mask_tree,
1127 const openvdb::math::Transform &transform,
1128 const Span<fn::GField> fields,
1129 MutableSpan<bke::GVolumeGrid> r_output_grids)
1130{
1131 BLI_assert(r_output_grids.size() == fields.size());
1132
1133 Vector<openvdb::GridBase::Ptr> output_grids(fields.size());
1134 for (const int i : fields.index_range()) {
1135 const std::optional<VolumeGridType> grid_type = cpp_type_to_grid_type(fields[i].cpp_type());
1136 BLI_assert(grid_type);
1137 output_grids[i] = create_grid_with_topology(mask_tree, transform, *grid_type);
1138 }
1139
1140 parallel_grid_topology_tasks(
1141 mask_tree,
1142 [&](const LeafNodeMask &leaf_node_mask,
1143 const openvdb::CoordBBox &leaf_bbox,
1144 const GetVoxelsFn get_voxels_fn) {
1145 process_leaf_node(
1146 fields, transform, leaf_node_mask, leaf_bbox, get_voxels_fn, output_grids);
1147 },
1148 [&](const Span<openvdb::Coord> voxels) {
1149 process_voxels(fields, transform, voxels, output_grids);
1150 },
1151 [&](const Span<openvdb::CoordBBox> tiles) {
1152 process_tiles(fields, transform, tiles, output_grids);
1153 });
1154
1155 process_background(fields, transform, output_grids);
1156
1157 for (const int i : fields.index_range()) {
1158 r_output_grids[i] = bke::GVolumeGrid(std::move(output_grids[i]));
1159 }
1160}
1161
1162#endif /* WITH_OPENVDB */
1163
1164} // namespace blender::bke::volume_grid
CustomData interface, see also DNA_customdata_types.h.
volatile int lock
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define BLI_assert(a)
#define BLI_NOINLINE
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
long long int int64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
#define index(i)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
if(state< num_states)
const ccl_global KernelWorkTile * tile
void * MEM_new_uninitialized(size_t len, const char *str)
Definition mallocn.cc:124
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
float4x4 get_transform_matrix(const VolumeGridData &grid)
std::string get_name(const VolumeGridData &grid)
void count_memory(const VolumeGridData &grid, MemoryCounter &memory)
int get_channels_num(VolumeGridType type)
std::string error_message_from_load(const VolumeGridData &grid)
void load(const VolumeGridData &grid)
void clear_tree(VolumeGridData &grid)
VolumeGridType get_type(const VolumeGridData &grid)
void set_transform_matrix(VolumeGridData &grid, const float4x4 &matrix)
bool is_loaded(const VolumeGridData &grid)
MatBase< float, 4, 4 > float4x4
BMesh const char void * data
@ VOLUME_GRID_VECTOR_FLOAT
@ VOLUME_GRID_VECTOR_DOUBLE
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e