Blender V5.3
scene/volume.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/volume.h"
6#include "scene/attribute.h"
7#include "scene/background.h"
8#include "scene/geometry.h"
9#include "scene/image_vdb.h"
10#include "scene/integrator.h"
11#include "scene/light.h"
12#include "scene/object.h"
13#include "scene/scene.h"
14
15#ifdef WITH_OPENVDB
16# include <openvdb/tools/GridTransformer.h>
17# include <openvdb/tools/LevelSetUtil.h>
18# include <openvdb/tools/Morphology.h>
19#endif
20
21#include "util/hash.h"
22#include "util/log.h"
23#include "util/nanovdb.h"
24#include "util/path.h"
25#include "util/progress.h"
26#include "util/types.h"
27
28#include "bvh/octree.h"
29
30#include <OpenImageIO/filesystem.h>
31
33
35{
36 NodeType *type = NodeType::add("volume", create, NodeType::NONE, Mesh::get_node_type());
37
38 SOCKET_FLOAT(step_size, "Step Size", 0.0f);
39 SOCKET_BOOLEAN(object_space, "Object Space", false);
40 SOCKET_FLOAT(velocity_scale, "Velocity Scale", 1.0f);
41
42 return type;
43}
44
45Volume::Volume() : Mesh(get_node_type(), Geometry::VOLUME)
46{
47 step_size = 0.0f;
48 object_space = false;
49}
50
51void Volume::clear(bool preserve_shaders)
52{
53 Mesh::clear(preserve_shaders, true);
54}
55
56struct QuadData {
57 int v0, v1, v2, v3;
58
60};
61
62struct VertHash {
63 size_t operator()(const int3 &i) const
64 {
65 return hash_uint3(i.x, i.y, i.z);
66 }
67};
68
69using VertHashMap = unordered_map<int3, int, VertHash>;
70
71enum {
78};
79
80#if defined(WITH_OPENVDB) && defined(WITH_NANOVDB)
81const int quads_indices[6][4] = {
82 /* QUAD_X_MIN */
83 {4, 0, 3, 7},
84 /* QUAD_X_MAX */
85 {1, 5, 6, 2},
86 /* QUAD_Y_MIN */
87 {4, 5, 1, 0},
88 /* QUAD_Y_MAX */
89 {3, 2, 6, 7},
90 /* QUAD_Z_MIN */
91 {0, 1, 2, 3},
92 /* QUAD_Z_MAX */
93 {5, 4, 7, 6},
94};
95
96const float3 quads_normals[6] = {
97 /* QUAD_X_MIN */
98 make_float3(-1.0f, 0.0f, 0.0f),
99 /* QUAD_X_MAX */
100 make_float3(1.0f, 0.0f, 0.0f),
101 /* QUAD_Y_MIN */
102 make_float3(0.0f, -1.0f, 0.0f),
103 /* QUAD_Y_MAX */
104 make_float3(0.0f, 1.0f, 0.0f),
105 /* QUAD_Z_MIN */
106 make_float3(0.0f, 0.0f, -1.0f),
107 /* QUAD_Z_MAX */
108 make_float3(0.0f, 0.0f, 1.0f),
109};
110
111static int add_vertex(const int3 v, vector<int3> &vertices, VertHashMap &used_verts)
112{
113 const VertHashMap::iterator it = used_verts.find(v);
114
115 if (it != used_verts.end()) {
116 return it->second;
117 }
118
119 const int vertex_offset = vertices.size();
120 used_verts[v] = vertex_offset;
121 vertices.push_back(v);
122 return vertex_offset;
123}
124
125static void create_quad(const int3 corners[8],
126 vector<int3> &vertices,
127 vector<QuadData> &quads,
128 VertHashMap &used_verts,
129 const int face_index)
130{
131 QuadData quad;
132 quad.v0 = add_vertex(corners[quads_indices[face_index][0]], vertices, used_verts);
133 quad.v1 = add_vertex(corners[quads_indices[face_index][1]], vertices, used_verts);
134 quad.v2 = add_vertex(corners[quads_indices[face_index][2]], vertices, used_verts);
135 quad.v3 = add_vertex(corners[quads_indices[face_index][3]], vertices, used_verts);
136 quad.normal = quads_normals[face_index];
137
138 quads.push_back(quad);
139}
140
141/* Create a mesh from a volume.
142 *
143 * The way the algorithm works is as follows:
144 *
145 * - The topologies of input OpenVDB grids are merged into a temporary grid.
146 * - Voxels of the temporary grid are dilated to account for the padding necessary for volume
147 * sampling.
148 * - A bounding box of the temporary grid is created.
149 */
150class VolumeMeshBuilder {
151 public:
152 /* use a MaskGrid to store the topology to save memory */
153 openvdb::MaskGrid::Ptr topology_grid;
154 bool first_grid;
155
156 VolumeMeshBuilder();
157
158 void add_grid(const nanovdb::GridHandle<> &grid);
159
160 void add_padding(const int pad_size);
161
162 void create_mesh(vector<float3> &vertices, vector<int> &indices, const bool ray_marching);
163
164 void generate_vertices_and_quads(vector<int3> &vertices_is,
165 vector<QuadData> &quads,
166 const bool ray_marching);
167
168 void convert_object_space(const vector<int3> &vertices, vector<float3> &out_vertices);
169
170 void convert_quads_to_tris(const vector<QuadData> &quads, vector<int> &tris);
171
172 bool empty_grid() const;
173};
174
175VolumeMeshBuilder::VolumeMeshBuilder()
176{
177 first_grid = true;
178}
179
180void VolumeMeshBuilder::add_grid(const nanovdb::GridHandle<> &nanogrid)
181{
182 /* set the transform of our grid from the first one */
183 openvdb::MaskGrid::Ptr grid = nanovdb_to_openvdb_mask(nanogrid);
184
185 if (first_grid) {
186 topology_grid = grid;
187 first_grid = false;
188 return;
189 }
190
191 /* If the transforms do not match, we need to resample one of the grids so that
192 * its index space registers with that of the other, here we resample our mask
193 * grid so memory usage is kept low */
194 if (topology_grid->transform() != grid->transform()) {
195 const openvdb::MaskGrid::Ptr temp_grid = topology_grid->copyWithNewTree();
196 temp_grid->setTransform(grid->transform().copy());
197 openvdb::tools::resampleToMatch<openvdb::tools::BoxSampler>(*topology_grid, *temp_grid);
198 topology_grid = temp_grid;
199 topology_grid->setTransform(grid->transform().copy());
200 }
201
202 topology_grid->topologyUnion(*grid);
203}
204
205void VolumeMeshBuilder::add_padding(const int pad_size)
206{
207 openvdb::tools::dilateActiveValues(topology_grid->tree(),
208 pad_size,
209 openvdb::tools::NN_FACE_EDGE_VERTEX,
210 openvdb::tools::PRESERVE_TILES);
211}
212
213void VolumeMeshBuilder::create_mesh(vector<float3> &vertices,
214 vector<int> &indices,
215 const bool ray_marching)
216{
217 /* We create vertices in index space (is), and only convert them to object
218 * space when done. */
219 vector<int3> vertices_is;
220 vector<QuadData> quads;
221
222 generate_vertices_and_quads(vertices_is, quads, ray_marching);
223
224 convert_object_space(vertices_is, vertices);
225
226 convert_quads_to_tris(quads, indices);
227}
228
229static bool is_non_empty_leaf(const openvdb::MaskGrid::TreeType &tree, const openvdb::Coord coord)
230{
231 const auto *leaf_node = tree.probeLeaf(coord);
232 return (leaf_node && !leaf_node->isEmpty());
233}
234
235void VolumeMeshBuilder::generate_vertices_and_quads(vector<ccl::int3> &vertices_is,
236 vector<QuadData> &quads,
237 const bool ray_marching)
238{
239 if (ray_marching) {
240 /* Make sure we only have leaf nodes in the tree, as tiles are not handled by this algorithm */
241 topology_grid->tree().voxelizeActiveTiles();
242
243 const openvdb::MaskGrid::TreeType &tree = topology_grid->tree();
244
245 /* Compute the active voxel bounding box to clip leaf bounding boxes. Leaf nodes are
246 * aligned to 8x8x8 blocks and can extend far beyond the actual active voxels, especially
247 * for low-resolution grids with large voxel sizes. */
248 openvdb::CoordBBox active_bbox;
249 tree.evalActiveVoxelBoundingBox(active_bbox);
250 /* +1 to convert from inclusive to exclusive bounds. */
251 active_bbox.max() = active_bbox.max().offsetBy(1);
252
253 VertHashMap used_verts;
254 for (auto iter = tree.cbeginLeaf(); iter; ++iter) {
255 if (iter->isEmpty()) {
256 continue;
257 }
258 openvdb::CoordBBox leaf_bbox = iter->getNodeBoundingBox();
259
260 /* Compute the leaf center from the (unclipped) node bounding box. */
261 static const int LEAF_DIM = openvdb::MaskGrid::TreeType::LeafNodeType::DIM;
262 const auto center = leaf_bbox.min() + openvdb::Coord(LEAF_DIM / 2);
263
264 /* +1 to convert from inclusive to exclusive bounds. */
265 leaf_bbox.max() = leaf_bbox.max().offsetBy(1);
266 /* Clip leaf bounding box to the active voxel region. */
267 leaf_bbox.min() = openvdb::Coord::maxComponent(leaf_bbox.min(), active_bbox.min());
268 leaf_bbox.max() = openvdb::Coord::minComponent(leaf_bbox.max(), active_bbox.max());
269
270 const int3 min = make_int3(leaf_bbox.min().x(), leaf_bbox.min().y(), leaf_bbox.min().z());
271 const int3 max = make_int3(leaf_bbox.max().x(), leaf_bbox.max().y(), leaf_bbox.max().z());
272 const int3 corners[8] = {
273 make_int3(min[0], min[1], min[2]),
274 make_int3(max[0], min[1], min[2]),
275 make_int3(max[0], max[1], min[2]),
276 make_int3(min[0], max[1], min[2]),
277 make_int3(min[0], min[1], max[2]),
278 make_int3(max[0], min[1], max[2]),
279 make_int3(max[0], max[1], max[2]),
280 make_int3(min[0], max[1], max[2]),
281 };
282 /* Only create a quad if on the border between an active and an inactive leaf.
283 *
284 * We verify that a leaf exists by probing a coordinate that is at its center,
285 * to do so we compute the center of the current leaf and offset this coordinate
286 * by the size of a leaf in each direction.
287 */
288 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x() - LEAF_DIM, center.y(), center.z())))
289 {
290 create_quad(corners, vertices_is, quads, used_verts, QUAD_X_MIN);
291 }
292 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x() + LEAF_DIM, center.y(), center.z())))
293 {
294 create_quad(corners, vertices_is, quads, used_verts, QUAD_X_MAX);
295 }
296 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x(), center.y() - LEAF_DIM, center.z())))
297 {
298 create_quad(corners, vertices_is, quads, used_verts, QUAD_Y_MIN);
299 }
300 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x(), center.y() + LEAF_DIM, center.z())))
301 {
302 create_quad(corners, vertices_is, quads, used_verts, QUAD_Y_MAX);
303 }
304 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x(), center.y(), center.z() - LEAF_DIM)))
305 {
306 create_quad(corners, vertices_is, quads, used_verts, QUAD_Z_MIN);
307 }
308 if (!is_non_empty_leaf(tree, openvdb::Coord(center.x(), center.y(), center.z() + LEAF_DIM)))
309 {
310 create_quad(corners, vertices_is, quads, used_verts, QUAD_Z_MAX);
311 }
312 }
313 return;
314 }
315
316 openvdb::CoordBBox bbox = topology_grid->evalActiveVoxelBoundingBox();
317 const int3 min = make_int3(bbox.min().x(), bbox.min().y(), bbox.min().z());
318 const int3 max = make_int3(bbox.max().x(), bbox.max().y(), bbox.max().z());
319
320 const int3 corners[8] = {
321 make_int3(min[0], min[1], min[2]),
322 make_int3(max[0], min[1], min[2]),
323 make_int3(max[0], max[1], min[2]),
324 make_int3(min[0], max[1], min[2]),
325 make_int3(min[0], min[1], max[2]),
326 make_int3(max[0], min[1], max[2]),
327 make_int3(max[0], max[1], max[2]),
328 make_int3(min[0], max[1], max[2]),
329 };
330
331 /* Create 6 quads of the bounding box. */
332 VertHashMap used_verts;
333
334 create_quad(corners, vertices_is, quads, used_verts, QUAD_X_MIN);
335 create_quad(corners, vertices_is, quads, used_verts, QUAD_X_MAX);
336 create_quad(corners, vertices_is, quads, used_verts, QUAD_Y_MIN);
337 create_quad(corners, vertices_is, quads, used_verts, QUAD_Y_MAX);
338 create_quad(corners, vertices_is, quads, used_verts, QUAD_Z_MIN);
339 create_quad(corners, vertices_is, quads, used_verts, QUAD_Z_MAX);
340}
341
342void VolumeMeshBuilder::convert_object_space(const vector<int3> &vertices,
343 vector<float3> &out_vertices)
344{
345 out_vertices.reserve(vertices.size());
346
347 for (size_t i = 0; i < vertices.size(); ++i) {
348 openvdb::math::Vec3d p = topology_grid->indexToWorld(
349 openvdb::math::Vec3d(vertices[i].x, vertices[i].y, vertices[i].z));
350 const float3 vertex = make_float3((float)p.x(), (float)p.y(), (float)p.z());
351 out_vertices.push_back(vertex);
352 }
353}
354
355void VolumeMeshBuilder::convert_quads_to_tris(const vector<QuadData> &quads, vector<int> &tris)
356{
357 /* If the grid transform has negative scale, the vertices transform by indexToWorld
358 * will have reversed winding order. Here we compensate for that by flipping the
359 * triangle order. */
360 const bool negative_scale =
361 topology_grid->transform().baseMap()->getAffineMap()->getMat4().det() < 0.0;
362
363 int index_offset = 0;
364 tris.resize(quads.size() * 6);
365
366 for (size_t i = 0; i < quads.size(); ++i) {
367 tris[index_offset++] = quads[i].v0;
368 tris[index_offset++] = negative_scale ? quads[i].v1 : quads[i].v2;
369 tris[index_offset++] = negative_scale ? quads[i].v2 : quads[i].v1;
370
371 tris[index_offset++] = quads[i].v0;
372 tris[index_offset++] = negative_scale ? quads[i].v2 : quads[i].v3;
373 tris[index_offset++] = negative_scale ? quads[i].v3 : quads[i].v2;
374 }
375}
376
377bool VolumeMeshBuilder::empty_grid() const
378{
379 return !topology_grid ||
380 (!topology_grid->tree().hasActiveTiles() && topology_grid->tree().leafCount() == 0);
381}
382
383/* -------------------------------------------------------------------- */
389
390struct Vec3Stats {
391 double avg[3] = {0.0, 0.0, 0.0};
392 double var[3] = {0.0, 0.0, 0.0};
393 uint size = 0;
394
395 /* Numerically stable way of computing online mean and variance, from Donald Knuth in “The Art
396 * Of Computer Programming” (1998). */
397 void add(const double value[3])
398 {
399 size++;
400 for (int i = 0; i < 3; i++) {
401 const double delta = value[i] - avg[i];
402 avg[i] += delta / double(size);
403 var[i] += delta * (value[i] - avg[i]);
404 }
405 }
406
407 void add(const Vec3Stats &other)
408 {
409 if (other.size > 0) {
410 const double denom = 1.0 / (double(size + other.size));
411 for (int i = 0; i < 3; i++) {
412 const double delta = other.avg[i] - avg[i];
413 avg[i] += denom * delta * double(other.size);
414 var[i] += other.var[i] + denom * delta * delta * double(size) * double(other.size);
415 }
416 size += other.size;
417 }
418 }
419
420 void finalize()
421 {
422 if (size < 2) {
423 var[0] = var[1] = var[2] = 0.0;
424 }
425 else {
426 for (int i = 0; i < 3; i++) {
427 var[i] /= double(size);
428 }
429 }
430 }
431};
432
433template<typename ChildT> static Vec3Stats compute_stats(const nanovdb::LeafNode<ChildT> &leaf)
434{
435 Vec3Stats stats;
436 for (auto value_it = leaf.cbeginValueOn(); value_it; ++value_it) {
437 const double value[3] = {(*value_it)[0], (*value_it)[1], (*value_it)[2]};
438 stats.add(value);
439 }
440 return stats;
441}
442
443template<typename ChildT> static Vec3Stats compute_stats(const nanovdb::InternalNode<ChildT> &node)
444{
445 const uint32_t num_leaf = node.mChildMask.countOn();
446
447 std::unique_ptr<const ChildT *[]> childNodes(new const ChildT *[num_leaf]);
448 const ChildT **ptr = childNodes.get();
449 for (auto it = node.mChildMask.beginOn(); it; ++it) {
450 *ptr++ = node.getChild(*it);
451 }
452
453 auto reduction_func = [&](const blocked_range<uint32_t> &r, Vec3Stats init) -> Vec3Stats {
454 for (uint32_t i = r.begin(); i < r.end(); ++i) {
455 init.add(compute_stats(*childNodes[i]));
456 }
457 return init;
458 };
459
460 auto join_func = [](Vec3Stats a, Vec3Stats b) -> Vec3Stats {
461 a.add(b);
462 return a;
463 };
464
465 const tbb::blocked_range<uint32_t> range(0, num_leaf);
466
467 return parallel_reduce(range, Vec3Stats(), reduction_func, join_func);
468}
469
471
472static int estimate_required_velocity_padding(const nanovdb::GridHandle<> &grid,
473 const float velocity_scale)
474{
475 const auto *typed_grid = grid.template grid<nanovdb::Vec3f>(0);
476
477 if (typed_grid == nullptr) {
478 return 0;
479 }
480
481 const nanovdb::Vec3d voxel_size = typed_grid->voxelSize();
482
483 /* We should only have uniform grids, so x = y = z, but we never know. */
484 const double max_voxel_size = openvdb::math::Max(voxel_size[0], voxel_size[1], voxel_size[2]);
485 if (max_voxel_size == 0.0 || velocity_scale == 0.0f) {
486 return 0;
487 }
488
489 Vec3Stats stats;
490 for (auto internal = typed_grid->tree().root().cbeginChild(); internal; ++internal) {
491 stats.add(compute_stats(*internal));
492 }
493 stats.finalize();
494
495 /* A standard score of 2.32635 makes sure only 1% of the values are above `avg + score * std`. */
496 const double score = 2.32635;
497 double estimated_padding = 0.0f;
498 for (int i = 0; i < 3; i++) {
499 const double max_velocity = max(std::fabs(stats.avg[i] + score * sqrt(stats.var[i])),
500 std::fabs(stats.avg[i] - score * sqrt(stats.var[i])));
501 const double max_dist_in_voxel = max_velocity * double(velocity_scale) / voxel_size[i];
502
503 /* Clamp padding to half of the volume size, and find the max padding in all 3 dimensions. */
504 estimated_padding = max(
505 min(max_dist_in_voxel, 0.5 * double(typed_grid->tree().bbox().dim()[i])),
506 estimated_padding);
507 }
508
509 return static_cast<int>(std::ceil(estimated_padding));
510}
511#endif
512
513#ifdef WITH_OPENVDB
514static openvdb::FloatGrid::ConstPtr get_vdb_for_attribute(Volume *volume, AttributeStandard std)
515{
516 Attribute *attr = volume->attributes.find(std);
517 if (!attr) {
518 return nullptr;
519 }
520
521 const ImageHandle &handle = attr->data_voxel();
522 VDBImageLoader *vdb_loader = handle.vdb_loader();
523 if (!vdb_loader) {
524 return nullptr;
525 }
526
527 const openvdb::GridBase::ConstPtr grid = vdb_loader->get_grid();
528 if (!grid) {
529 return nullptr;
530 }
531
532 if (!grid->isType<openvdb::FloatGrid>()) {
533 return nullptr;
534 }
535
536 return openvdb::gridConstPtrCast<openvdb::FloatGrid>(grid);
537}
538
539class MergeScalarGrids {
540 using ScalarTree = openvdb::FloatTree;
541
542 openvdb::tree::ValueAccessor<const ScalarTree> m_acc_x, m_acc_y, m_acc_z;
543
544 public:
545 MergeScalarGrids(const ScalarTree *x_tree, const ScalarTree *y_tree, const ScalarTree *z_tree)
546 : m_acc_x(*x_tree), m_acc_y(*y_tree), m_acc_z(*z_tree)
547 {
548 }
549
550 MergeScalarGrids(const MergeScalarGrids &other)
551
552 = default;
553
554 void operator()(const openvdb::Vec3STree::ValueOnIter &it) const
555 {
556 using namespace openvdb;
557
558 const math::Coord xyz = it.getCoord();
559 const float x = m_acc_x.getValue(xyz);
560 const float y = m_acc_y.getValue(xyz);
561 const float z = m_acc_z.getValue(xyz);
562
563 it.setValue(math::Vec3s(x, y, z));
564 }
565};
566
567static void merge_scalar_grids_for_velocity(const Scene *scene, Volume *volume)
568{
570 /* A vector grid for velocity is already available. */
571 return;
572 }
573
574 const openvdb::FloatGrid::ConstPtr vel_x_grid = get_vdb_for_attribute(
576 const openvdb::FloatGrid::ConstPtr vel_y_grid = get_vdb_for_attribute(
578 const openvdb::FloatGrid::ConstPtr vel_z_grid = get_vdb_for_attribute(
580
581 if (!(vel_x_grid && vel_y_grid && vel_z_grid)) {
582 return;
583 }
584
585 const openvdb::Vec3fGrid::Ptr vecgrid = openvdb::Vec3SGrid::create(openvdb::Vec3s(0.0f));
586
587 /* Activate voxels in the vector grid based on the scalar grids to ensure thread safety during
588 * the merge. */
589 vecgrid->tree().topologyUnion(vel_x_grid->tree());
590 vecgrid->tree().topologyUnion(vel_y_grid->tree());
591 vecgrid->tree().topologyUnion(vel_z_grid->tree());
592
593 MergeScalarGrids op(&vel_x_grid->tree(), &vel_y_grid->tree(), &vel_z_grid->tree());
594 openvdb::tools::foreach(vecgrid->beginValueOn(), op, true, false);
595
596 /* Assume all grids have the same transformation. */
597 const openvdb::math::Transform::Ptr transform = openvdb::ConstPtrCast<openvdb::math::Transform>(
598 vel_x_grid->transformPtr());
599 vecgrid->setTransform(transform);
600
601 /* Make an attribute for it. */
603 unique_ptr<ImageLoader> loader = make_unique<VDBImageLoader>(vecgrid, "merged_velocity");
604 const ImageParams params;
605 attr->data_voxel_for_write() = scene->image_manager->add_image(std::move(loader), params);
606}
607#endif /* defined(WITH_OPENVDB) && defined(WITH_NANOVDB) */
608
609/* ************************************************************************** */
610
611void GeometryManager::create_volume_mesh(const Scene *scene, Volume *volume, Progress &progress)
612{
613 const string msg = string_printf("Computing Volume Mesh %s", volume->name.c_str());
614 progress.set_status("Updating Mesh", msg);
615
616 /* Find shader and compute padding based on volume shader interpolation settings. */
617 Shader *volume_shader = nullptr;
618 int pad_size = 0;
619
620 for (Node *node : volume->get_used_shaders()) {
621 Shader *shader = static_cast<Shader *>(node);
622
623 if (!shader->has_volume) {
624 continue;
625 }
626
627 volume_shader = shader;
628
629 if (shader->get_volume_interpolation_method() == VOLUME_INTERPOLATION_LINEAR) {
630 pad_size = max(1, pad_size);
631 }
632 else if (shader->get_volume_interpolation_method() == VOLUME_INTERPOLATION_CUBIC) {
633 pad_size = max(2, pad_size);
634 }
635
636 break;
637 }
638
639 /* Clear existing volume mesh, done here in case we early out due to
640 * empty grid or missing volume shader.
641 * Also keep the shaders to avoid infinite loops when synchronizing, as this will tag the shaders
642 * as having changed. */
643 volume->clear(true);
644 volume->need_update_rebuild = true;
645
646 if (!volume_shader) {
647 return;
648 }
649
650#if defined(WITH_OPENVDB) && defined(WITH_NANOVDB)
651 /* Create volume mesh builder. */
652 VolumeMeshBuilder builder;
653
654 for (Attribute &attr : volume->attributes.attributes) {
655 if (attr.element != ATTR_ELEMENT_VOXEL) {
656 continue;
657 }
658
659 ImageHandle &handle = attr.data_voxel_for_write();
660
661 if (handle.empty()) {
662 continue;
663 }
664
665 /* Create NanoVDB grid handle from image memory. */
667 if (image == nullptr || image->host_pointer == nullptr ||
668 image->info.data_type == IMAGE_DATA_TYPE_NANOVDB_EMPTY ||
669 !is_nanovdb_type(image->info.data_type))
670 {
671 continue;
672 }
673
674 nanovdb::GridHandle grid(
675 nanovdb::HostBuffer::createFull(image->memory_size(), image->host_pointer));
676
677 /* Add padding based on the maximum velocity vector. */
678 if (attr.std == ATTR_STD_VOLUME_VELOCITY && scene->need_motion() != Scene::MOTION_NONE) {
679 pad_size = max(pad_size,
680 estimate_required_velocity_padding(grid, volume->get_velocity_scale()));
681 }
682
683 builder.add_grid(grid);
684 }
685
686 /* If nothing to build, early out. */
687 if (builder.empty_grid()) {
688 LOG_DEBUG << "Memory usage volume mesh: 0 Mb. (empty grid)";
689 return;
690 }
691
692 builder.add_padding(pad_size);
693
694 /* Create mesh. */
695 vector<float3> vertices;
696 vector<int> indices;
697 const bool ray_marching = scene->integrator->get_volume_ray_marching();
698 builder.create_mesh(vertices, indices, ray_marching);
699
700 volume->resize_mesh(vertices.size(), indices.size() / 3);
701 volume->used_shaders.clear();
702 volume->used_shaders.push_back_slow(volume_shader);
703
704 std::ranges::copy(vertices, volume->get_position_for_write());
705 std::ranges::copy(indices, volume->triangles.data());
706 std::ranges::fill(volume->get_shader(), 0);
707 std::ranges::fill(volume->get_smooth(), false);
708
709 /* Print stats. */
710 LOG_DEBUG << "Memory usage volume mesh: "
711 << (vertices.size() * sizeof(float3) + indices.size() * sizeof(int)) /
712 (1024.0 * 1024.0)
713 << "Mb.";
714#else
715 (void)scene;
716#endif /* defined(WITH_OPENVDB) && defined(WITH_NANOVDB) */
717}
718
719void Volume::merge_grids(const Scene *scene)
720{
721#ifdef WITH_OPENVDB
722 merge_scalar_grids_for_velocity(scene, this);
723#else
724 (void)scene;
725#endif
726}
727
729{
730 need_rebuild_ = true;
732}
733
735{
736 need_rebuild_ = true;
737}
738
739/* Remove changed objects from the list of octrees and tag for rebuild. */
740void VolumeManager::tag_update(const set<Object *> &objects, uint32_t flag)
741{
742 if (object_octrees_.empty()) {
743 /* Volume object is not in the octree, can happen when using ray marching. */
744 return;
745 }
746
747 bool volume_object_updated = false;
748 for (const Object *object : objects) {
749 if (!object->get_geometry()->has_volume) {
750 continue;
751 }
752
753 volume_object_updated = true;
754
755 for (const Node *node : object->get_geometry()->get_used_shaders()) {
756 const Shader *shader = static_cast<const Shader *>(node);
758 /* TODO(weizhen): no need to update if the spatial variation is not in world space. */
759 tag_update();
760 object_octrees_.erase({object, shader});
761 }
762 }
763 }
764
765 if (volume_object_updated) {
767 tag_update();
768 }
769 if (!need_rebuild_ && (flag & ObjectManager::TRANSFORM_MODIFIED)) {
770 /* Octree is not tagged for rebuild but the transformation changed, so a redraw is needed. */
771 update_visualization_ = true;
772 }
773 }
774}
775
776/* Remove object with changed shader from the list of octrees and tag for rebuild. */
778{
779 tag_update();
780 for (auto it = object_octrees_.begin(); it != object_octrees_.end();) {
781 if (it->first.second == shader) {
782 it = object_octrees_.erase(it);
783 }
784 else {
785 it++;
786 }
787 }
788}
789
790/* Remove objects with changed geometry from the list of octrees and tag for rebuild. */
791void VolumeManager::tag_update(const set<Geometry *> &geometry)
792{
793 bool volume_geometry_updated = false;
794 for (Geometry *geometry : geometry) {
795 if (geometry->has_volume) {
796 volume_geometry_updated = true;
797 }
798 }
799
800 if (!volume_geometry_updated) {
801 return;
802 }
803
804 tag_update();
805 /* Tag Octree for update. */
806 for (auto it = object_octrees_.begin(); it != object_octrees_.end();) {
807 const Object *object = it->first.first;
808 if (geometry.contains(object->get_geometry())) {
809 it = object_octrees_.erase(it);
810 }
811 else {
812 it++;
813 }
814 }
815
816#ifdef WITH_OPENVDB
817 /* Tag VDB map for update. */
818 for (auto it = vdb_map_.begin(); it != vdb_map_.end();) {
819 if (geometry.contains(const_cast<Geometry *>(it->first.first))) {
820 it = vdb_map_.erase(it);
821 }
822 else {
823 it++;
824 }
825 }
826#endif
827}
828
830{
831 update_root_indices_ = true;
832}
833
835{
836 need_rebuild_ = true;
837 algorithm_modified_ = true;
838}
839
840bool VolumeManager::is_homogeneous_volume(const Object *object, const Shader *shader)
841{
842 if (!shader->has_volume || shader->has_volume_spatial_varying) {
843 return false;
844 }
845
847 for (Attribute &attr : object->get_geometry()->attributes.attributes) {
848 /* If both the shader and the object needs volume attributes, the volume is heterogeneous. */
849 if (attr.element == ATTR_ELEMENT_VOXEL) {
850 return false;
851 }
852 }
853 }
854
855 return true;
856}
857
858#ifdef WITH_OPENVDB
859/* Given a mesh, check if every edge has exactly two incident triangles, and if the two triangles
860 * have the same orientation. */
861static bool mesh_is_closed(const std::vector<openvdb::Vec3I> &triangles)
862{
863 const size_t num_triangles = triangles.size();
864 if (num_triangles % 2) {
865 return false;
866 }
867
868 /* Store the two vertices that forms an edge. */
869 std::multiset<std::pair<int, int>> edges;
870 int num_edges = 0;
871
872 for (const auto &tri : triangles) {
873 for (int i = 0; i < 3; i++) {
874 const std::pair<int, int> e = {tri[i], tri[(i + 1) % 3]};
875 if (edges.contains(e)) {
876 /* Same edge exists. */
877 return false;
878 }
879
880 /* Check if an edge in the opposite order exists. */
881 const auto count = edges.count({e.second, e.first});
882 if (count > 1) {
883 /* Edge has more than 2 incident faces. */
884 return false;
885 }
886 if (count == 1) {
887 /* If an edge in the opposite order exists, increment the count. */
888 edges.insert({e.second, e.first});
889 }
890 else {
891 /* Insert a new edge. */
892 num_edges++;
893 edges.insert(e);
894 }
895 }
896 }
897
898 /* Until this point, the count of each element in the set is at most 2; to check if they are
899 * exactly 2, we just need to compare the total numbers. */
900 return num_triangles * 3 == num_edges * 2;
901}
902
903openvdb::BoolGrid::ConstPtr VolumeManager::mesh_to_sdf_grid(const Mesh *mesh,
904 const Shader *shader,
905 const float half_width)
906{
907 const int num_verts = mesh->num_verts();
908 std::vector<openvdb::Vec3f> points(num_verts);
909 parallel_for(0, num_verts, [&](int i) {
910 const float3 &vert = mesh->get_position()[i];
911 points[i] = openvdb::Vec3f(vert.x, vert.y, vert.z);
912 });
913
914 const int max_num_triangles = mesh->num_triangles();
915 std::vector<openvdb::Vec3I> triangles;
916 triangles.reserve(max_num_triangles);
917 for (int i = 0; i < max_num_triangles; i++) {
918 /* Only push triangles with matching shader. */
919 const int shader_index = mesh->get_shader()[i];
920 if (static_cast<const Shader *>(mesh->get_used_shaders()[shader_index]) == shader) {
921 triangles.emplace_back(mesh->get_triangles()[i * 3],
922 mesh->get_triangles()[i * 3 + 1],
923 mesh->get_triangles()[i * 3 + 2]);
924 }
925 }
926
927 if (!mesh_is_closed(triangles)) {
928 /* `meshToLevelSet()` requires a closed mesh, otherwise we can not determine the interior of
929 * the mesh. Evaluate the whole bounding box in this case. */
930 return openvdb::BoolGrid::create();
931 }
932
933 /* TODO(weizhen): Should consider object instead of mesh size. */
934 const float3 mesh_size = mesh->bounds.size();
935 const auto vdb_voxel_size = openvdb::Vec3d(mesh_size.x, mesh_size.y, mesh_size.z) /
936 double(1 << VOLUME_OCTREE_MAX_DEPTH);
937
938 auto xform = openvdb::math::Transform::createLinearTransform(1.0);
939 xform->postScale(vdb_voxel_size);
940
941 auto sdf_grid = openvdb::tools::meshToLevelSet<openvdb::FloatGrid>(
942 *xform, points, triangles, half_width);
943
944 return openvdb::tools::sdfInteriorMask(*sdf_grid, 0.5 * vdb_voxel_size.length());
945}
946
947openvdb::BoolGrid::ConstPtr VolumeManager::get_vdb(const Geometry *geom,
948 const Shader *shader) const
949{
950 if (geom->is_mesh()) {
951 if (auto it = vdb_map_.find({geom, shader}); it != vdb_map_.end()) {
952 return it->second;
953 }
954 }
955 /* Create empty grid. */
956 return openvdb::BoolGrid::create();
957}
958#endif
959
960void VolumeManager::initialize_octree(const Scene *scene, Progress &progress)
961{
962 /* Instanced objects without spatial variation can share one octree. */
963 std::map<std::pair<const Geometry *, const Shader *>, std::shared_ptr<Octree>> geometry_octrees;
964 for (const auto &it : object_octrees_) {
965 const Shader *shader = it.first.second;
966 if (!shader->has_volume_spatial_varying) {
967 if (const Object *object = it.first.first) {
968 geometry_octrees[{object->get_geometry(), shader}] = it.second;
969 }
970 }
971 }
972
973 /* Loop through the volume objects to initialize their root nodes. */
974 for (const Object *object : scene->objects) {
975 const Geometry *geom = object->get_geometry();
976 if (!geom->has_volume) {
977 continue;
978 }
979
980 /* Create Octree. */
981 for (const Node *node : geom->get_used_shaders()) {
982 const Shader *shader = static_cast<const Shader *>(node);
983 if (!shader->has_volume) {
984 continue;
985 }
986
987 if (!object_octrees_.contains({object, shader})) {
988 if (geom->is_light()) {
989 const Light *light = static_cast<const Light *>(geom);
990 if (light->is_background_light()) {
991 /* World volume is unbounded, use some practical large number instead. */
992 const float3 size = make_float3(10000.0f);
993 object_octrees_[{object, shader}] = std::make_shared<Octree>(BoundBox(-size, size));
994 }
995 }
996 else {
997 const Mesh *mesh = static_cast<const Mesh *>(geom);
998 if (is_zero(mesh->bounds.size())) {
999 continue;
1000 }
1001 if (!shader->has_volume_spatial_varying) {
1002 /* TODO(weizhen): check object attribute. */
1003 if (auto it = geometry_octrees.find({geom, shader}); it != geometry_octrees.end()) {
1004 /* Share octree with other instances. */
1005 object_octrees_[{object, shader}] = it->second;
1006 }
1007 else {
1008 auto octree = std::make_shared<Octree>(mesh->bounds);
1009 geometry_octrees[{geom, shader}] = octree;
1010 object_octrees_[{object, shader}] = octree;
1011 }
1012 }
1013 else {
1014 /* TODO(weizhen): we can still share the octree if the spatial variation is in object
1015 * space, but that might be tricky to determine. */
1016 object_octrees_[{object, shader}] = std::make_shared<Octree>(mesh->bounds);
1017 }
1018 }
1019 }
1020
1021#ifdef WITH_OPENVDB
1022 if (geom->is_mesh() && !VolumeManager::is_homogeneous_volume(object, shader) &&
1023 !vdb_map_.contains({geom, shader}))
1024 {
1025 const Mesh *mesh = static_cast<const Mesh *>(geom);
1026 const float3 dim = mesh->bounds.size();
1027 if (dim.x > 0.0f && dim.y > 0.0f && dim.z > 0.0f) {
1028 const char *name = object->get_asset_name().c_str();
1029 progress.set_substatus(string_printf("Creating SDF grid for %s", name));
1030 vdb_map_[{geom, shader}] = mesh_to_sdf_grid(mesh, shader, 1.0f);
1031 }
1032 }
1033#else
1034 (void)progress;
1035#endif
1036 }
1037 }
1038}
1039
1040void VolumeManager::update_num_octree_nodes()
1041{
1042 num_octree_nodes_ = 0;
1043 num_octree_roots_ = 0;
1044
1045 std::set<const Octree *> unique_octrees;
1046 for (const auto &it : object_octrees_) {
1047 const Octree *octree = it.second.get();
1048 if (unique_octrees.contains(octree)) {
1049 continue;
1050 }
1051
1052 unique_octrees.insert(octree);
1053
1054 num_octree_roots_++;
1055 num_octree_nodes_ += octree->get_num_nodes();
1056 }
1057}
1058
1059int VolumeManager::num_octree_nodes() const
1060{
1061 return num_octree_nodes_;
1062}
1063
1064int VolumeManager::num_octree_roots() const
1065{
1066 return num_octree_roots_;
1067}
1068
1069void VolumeManager::build_octree(Device *device, Progress &progress)
1070{
1071 const double start_time = time_dt();
1072
1073 for (auto &it : object_octrees_) {
1074 if (it.second->is_built()) {
1075 continue;
1076 }
1077
1078 const Object *object = it.first.first;
1079 const Shader *shader = it.first.second;
1080#ifdef WITH_OPENVDB
1081 openvdb::BoolGrid::ConstPtr interior_mask = get_vdb(object->get_geometry(), shader);
1082 it.second->build(device, progress, interior_mask, object, shader);
1083#else
1084 it.second->build(device, progress, object, shader);
1085#endif
1086 }
1087
1088 update_num_octree_nodes();
1089
1090 const double build_time = time_dt() - start_time;
1091
1092 LOG_DEBUG << object_octrees_.size() << " volume octree(s) with a total of " << num_octree_nodes()
1093 << " nodes are built in " << build_time << " seconds.";
1094}
1095
1096void VolumeManager::update_root_indices(DeviceScene *dscene, const Scene *scene) const
1097{
1098 if (object_octrees_.empty()) {
1099 return;
1100 }
1101
1102 /* Keep track of the root index of the unique octrees. */
1103 std::map<const Octree *, int> octree_root_indices;
1104
1105 int *roots = dscene->volume_tree_root_ids.alloc(scene->objects.size());
1106
1107 int root_index = 0;
1108 for (const auto &it : object_octrees_) {
1109 const Object *object = it.first.first;
1110 const int object_id = object->get_device_index();
1111 const Octree *octree = it.second.get();
1112 auto entry = octree_root_indices.find(octree);
1113 if (entry == octree_root_indices.end()) {
1114 roots[object_id] = root_index;
1115 octree_root_indices[octree] = root_index;
1116
1117 root_index++;
1118 }
1119 else {
1120 /* Instances share the same octree. */
1121 roots[object_id] = entry->second;
1122 }
1123 }
1124
1126}
1127
1128void VolumeManager::flatten_octree(DeviceScene *dscene, const Scene *scene) const
1129{
1130 if (object_octrees_.empty()) {
1131 return;
1132 }
1133
1134 update_root_indices(dscene, scene);
1135
1136 for (const auto &it : object_octrees_) {
1137 /* Octrees need to be re-flattened. */
1138 it.second->set_flattened(false);
1139 }
1140
1141 KernelOctreeRoot *kroots = dscene->volume_tree_roots.alloc(num_octree_roots());
1142 KernelOctreeNode *knodes = dscene->volume_tree_nodes.alloc(num_octree_nodes());
1143
1144 int node_index = 0;
1145 int root_index = 0;
1146 for (const auto &it : object_octrees_) {
1147 std::shared_ptr<Octree> octree = it.second;
1148 if (octree->is_flattened()) {
1149 continue;
1150 }
1151
1152 /* If an object has multiple shaders, the root index is overwritten, so we also write the
1153 * shader id, and perform a linear search in the kernel to find the correct octree. */
1154 kroots[root_index].shader = it.first.second->id;
1155 kroots[root_index].id = node_index;
1156
1157 /* Transform from object space into octree space. */
1158 auto root = octree->get_root();
1159 const float3 scale = 1.0f / root->bbox.size();
1160 kroots[root_index].scale = scale;
1161 kroots[root_index].translation = -root->bbox.min * scale + 1.0f;
1162
1163 root_index++;
1164
1165 /* Flatten octree. */
1166 const uint current_index = node_index++;
1167 knodes[current_index].parent = -1;
1168 octree->flatten(knodes, current_index, root, node_index);
1169 octree->set_flattened();
1170 }
1171
1174
1175 LOG_DEBUG << "Memory usage of volume octrees: "
1176 << (dscene->volume_tree_nodes.size() * sizeof(KernelOctreeNode) +
1177 dscene->volume_tree_roots.size() * sizeof(KernelOctreeRoot) +
1178 dscene->volume_tree_root_ids.size() * sizeof(int)) /
1179 (1024.0 * 1024.0)
1180 << "Mb.";
1181}
1182
1183/* Dump octree as python script, enabled by `CYCLES_VOLUME_OCTREE_DUMP` environment variable. */
1184std::string VolumeManager::visualize_octree(const char *filename) const
1185{
1186 const std::string filename_full = path_join(OIIO::Filesystem::current_path(), filename);
1187
1188 std::ofstream file(filename_full);
1189 if (file.is_open()) {
1190 std::ostringstream buffer;
1191 file << "# Visualize volume octree.\n\n"
1192 "import bpy\nimport mathutils\n\n"
1193 "if bpy.context.active_object:\n"
1194 " bpy.context.active_object.select_set(False)\n\n"
1195 "octree = bpy.data.collections.new(name='Octree')\n"
1196 "bpy.context.scene.collection.children.link(octree)\n\n";
1197
1198 for (const auto &it : object_octrees_) {
1199 /* Draw Octree. */
1200 const auto octree = it.second;
1201 const std::string object_name = it.first.first->get_asset_name().string();
1202 octree->visualize(file, object_name);
1203
1204 /* Apply transform. */
1205 const Object *object = it.first.first;
1206 const Geometry *geom = object->get_geometry();
1207 if (!geom->is_light() && !geom->transform_applied) {
1208 const Transform t = object->get_tfm();
1209 file << "obj.matrix_world = mathutils.Matrix((" << t.x << ", " << t.y << ", " << t.z
1210 << ", (" << 0 << "," << 0 << "," << 0 << "," << 1 << ")))\n\n";
1211 }
1212 }
1213
1214 file.close();
1215 }
1216
1217 return filename_full;
1218}
1219
1220void VolumeManager::update_step_size(const Scene *scene, DeviceScene *dscene, Progress &progress)
1221{
1222 assert(scene->integrator->get_volume_ray_marching());
1223
1225 !scene->integrator->volume_step_rate_is_modified() && !algorithm_modified_)
1226 {
1227 return;
1228 }
1229
1230 if (dscene->volume_step_size.need_realloc()) {
1231 dscene->volume_step_size.alloc(scene->objects.size());
1232 }
1233
1234 float *volume_step_size = dscene->volume_step_size.data();
1235
1236 for (const Object *object : scene->objects) {
1237 const Geometry *geom = object->get_geometry();
1238 if (!geom->has_volume) {
1239 continue;
1240 }
1241
1242 volume_step_size[object->index] = scene->integrator->get_volume_step_rate() *
1243 object->compute_volume_step_size(progress);
1244 }
1245
1248 need_update_step_size = false;
1249}
1250
1252 DeviceScene *dscene,
1253 const Scene *scene,
1254 Progress &progress)
1255{
1256 if (scene->integrator->get_volume_ray_marching()) {
1257 /* No need to update octree for ray marching. */
1258 if (algorithm_modified_) {
1259 dscene->volume_tree_nodes.free();
1260 dscene->volume_tree_roots.free();
1261 dscene->volume_tree_root_ids.free();
1262 }
1263 update_step_size(scene, dscene, progress);
1264 algorithm_modified_ = false;
1265 return;
1266 }
1267
1268 if (need_rebuild_) {
1269 /* Data needed for volume shader evaluation. */
1270 device->const_copy_to("data", &dscene->data, sizeof(dscene->data));
1271
1272 initialize_octree(scene, progress);
1273 build_octree(device, progress);
1274 flatten_octree(dscene, scene);
1275
1276 update_visualization_ = true;
1277 need_rebuild_ = false;
1278 update_root_indices_ = false;
1279 }
1280 else if (update_root_indices_) {
1281 update_root_indices(dscene, scene);
1282 update_root_indices_ = false;
1283 }
1284
1285 if (update_visualization_) {
1286 static const bool dump_octree = getenv("CYCLES_VOLUME_OCTREE_DUMP") != nullptr;
1287 if (dump_octree) {
1288 const std::string octree_path = visualize_octree("octree.py");
1289 LOG_INFO << "Octree visualization has been written to " << octree_path;
1290 }
1291 update_visualization_ = false;
1292 }
1293
1294 if (algorithm_modified_) {
1295 dscene->volume_step_size.free();
1296 algorithm_modified_ = false;
1297 }
1298}
1299
1301{
1302 dscene->volume_tree_nodes.free();
1303 dscene->volume_tree_roots.free();
1304 dscene->volume_tree_root_ids.free();
1305 dscene->volume_step_size.free();
1306}
1307
1309{
1310#ifdef WITH_OPENVDB
1311 for (auto &it : vdb_map_) {
1312 it.second.reset();
1313 }
1314#endif
1315}
1316
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
void init()
btScalar max
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
char build_time[]
Definition buildinfo.c:33
list< Attribute > attributes
Attribute * find(ustring name) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
ImageHandle & data_voxel_for_write()
AttributeElement element
const ImageHandle & data_voxel() const
AttributeStandard std
__forceinline BoundBox()=default
__forceinline float3 size() const
Definition boundbox.h:122
device_vector< int > volume_tree_root_ids
Definition devicescene.h:94
device_vector< KernelOctreeRoot > volume_tree_roots
Definition devicescene.h:93
device_vector< KernelOctreeNode > volume_tree_nodes
Definition devicescene.h:92
device_vector< float > volume_step_size
Definition devicescene.h:95
KernelData data
virtual void const_copy_to(const char *name, void *host, const size_t size)=0
void create_volume_mesh(const Scene *scene, Volume *volume, Progress &progress)
BoundBox bounds
bool transform_applied
bool is_light() const
const packed_float3 * get_position() const
bool need_update_rebuild
AttributeSet attributes
bool is_mesh() const
Geometry(const NodeType *node_type, const Type type)
packed_float3 * get_position_for_write()
device_image * vdb_image_memory() const
bool empty() const
VDBImageLoader * vdb_loader() const
bool is_background_light() const
NODE_DECLARE size_t num_verts() const
Mesh(const NodeType *node_type_, Type geom_type_)
void clear(bool preserve_shaders=false) override
size_t num_triangles() const
Definition scene/mesh.h:73
void resize_mesh(const int numverts, const int numtris)
vector< ParamValue > attributes
int get_num_nodes() const
void set_substatus(const string &substatus_)
Definition progress.h:259
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
Definition scene.h:133
MotionType need_motion() const
Definition scene.cpp:429
@ MOTION_NONE
Definition scene.h:207
unique_ptr_vector< Object > objects
Definition scene.h:162
Integrator * integrator
Definition scene.h:149
unique_ptr< ImageManager > image_manager
Definition scene.h:166
bool has_volume_attribute_dependency
bool has_volume
bool has_volume_spatial_varying
void tag_update_algorithm()
void device_update(Device *, DeviceScene *, const Scene *, Progress &)
void device_free(DeviceScene *)
bool need_update_step_size
static bool is_homogeneous_volume(const Object *, const Shader *)
void tag_update_indices()
NODE_DECLARE Volume()
void merge_grids(const Scene *scene)
void clear(bool preserve_shaders=false) override
size_t size() const
T * alloc(const size_t width, const size_t height=0)
bool is_modified() const
size_t size() const
#define VOLUME_OCTREE_MAX_DEPTH
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
uint hash_uint3(uint kx, uint ky, uint kz)
#define assert(assertion)
#define image(slot, qualifiers, format)
#define vertex
#define sqrt
VecBase< float, 3 > float3
float3x3 scale(float3x3 mat, float2 scale)
bool is_zero(float3x3 a)
@ ATTR_ELEMENT_VOXEL
AttributeStandard
@ ATTR_STD_VOLUME_VELOCITY_Y
@ ATTR_STD_VOLUME_VELOCITY_Z
@ ATTR_STD_VOLUME_VELOCITY
@ ATTR_STD_VOLUME_VELOCITY_X
#define LOG_DEBUG
Definition log.h:107
#define LOG_INFO
Definition log.h:106
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
void add(Depsgraph &depsgraph, std::function< void()> fn)
int size(const AssetLibraryReference *library_reference)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
unsigned int uint
Definition BKE_text.h:55
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int uint32_t
Definition BKE_text.h:81
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void vert(const int vert_id, float4 &position)
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition node_type.h:219
#define NODE_DEFINE(structname)
Definition node_type.h:157
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:211
string path_join(const string &dir, const string &file)
Definition path.cpp:436
@ VOLUME_INTERPOLATION_LINEAR
@ VOLUME_INTERPOLATION_CUBIC
unordered_map< int3, int, VertHash > VertHashMap
@ QUAD_X_MAX
@ QUAD_Y_MIN
@ QUAD_Z_MIN
@ QUAD_Y_MAX
@ QUAD_X_MIN
@ QUAD_Z_MAX
#define min(a, b)
Definition sort.cc:38
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
packed_float3 translation
packed_float3 scale
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
ustring name
Definition graph/node.h:178
float3 normal
float4 y
Definition transform.h:53
float4 x
Definition transform.h:53
float4 z
Definition transform.h:53
size_t operator()(const int3 &i) const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31
@ IMAGE_DATA_TYPE_NANOVDB_EMPTY
Definition types_image.h:49
ccl_device_inline bool is_nanovdb_type(int type)
Definition types_image.h:54