Blender V4.5
scene/geometry.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "bvh/bvh.h"
6
7#include "device/device.h"
8
9#include "scene/attribute.h"
10#include "scene/camera.h"
11#include "scene/geometry.h"
12#include "scene/hair.h"
13#include "scene/light.h"
14#include "scene/mesh.h"
15#include "scene/object.h"
16#include "scene/osl.h"
17#include "scene/pointcloud.h"
18#include "scene/scene.h"
19#include "scene/shader.h"
20#include "scene/shader_nodes.h"
21#include "scene/stats.h"
22#include "scene/volume.h"
23
24#include "subd/split.h"
25
26#ifdef WITH_OSL
27# include "kernel/osl/globals.h"
28#endif
29
30#include "util/log.h"
31#include "util/progress.h"
32#include "util/task.h"
33
35
36/* Geometry */
37
39{
40 NodeType *type = NodeType::add("geometry_base", nullptr);
41
42 SOCKET_UINT(motion_steps, "Motion Steps", 0);
43 SOCKET_BOOLEAN(use_motion_blur, "Use Motion Blur", false);
44 SOCKET_NODE_ARRAY(used_shaders, "Shaders", Shader::get_node_type());
45
46 return type;
47}
48
66
71
72void Geometry::clear(bool preserve_shaders)
73{
74 if (!preserve_shaders) {
75 used_shaders.clear();
76 }
77
78 transform_applied = false;
82}
83
84float Geometry::motion_time(const int step) const
85{
86 return (motion_steps > 1) ? 2.0f * step / (motion_steps - 1) - 1.0f : 0.0f;
87}
88
89int Geometry::motion_step(const float time) const
90{
91 if (motion_steps > 1) {
92 int attr_step = 0;
93
94 for (int step = 0; step < motion_steps; step++) {
95 const float step_time = motion_time(step);
96 if (step_time == time) {
97 return attr_step;
98 }
99
100 /* Center step is stored in a separate attribute. */
101 if (step != motion_steps / 2) {
102 attr_step++;
103 }
104 }
105 }
106
107 return -1;
108}
109
111{
112 return is_instanced() || layout == BVH_LAYOUT_OPTIX || layout == BVH_LAYOUT_MULTI_OPTIX ||
113 layout == BVH_LAYOUT_METAL || layout == BVH_LAYOUT_MULTI_OPTIX_EMBREE ||
115 layout == BVH_LAYOUT_HIPRT || layout == BVH_LAYOUT_MULTI_HIPRT ||
118}
119
121{
122 /* Currently we treat subsurface objects as instanced.
123 *
124 * While it might be not very optimal for ray traversal, it avoids having
125 * duplicated BVH in the memory, saving quite some space.
126 */
128}
129
131{
132 for (Node *node : used_shaders) {
133 Shader *shader = static_cast<Shader *>(node);
134 if (shader->has_displacement && shader->get_displacement_method() != DISPLACE_BUMP) {
135 return true;
136 }
137 }
138
139 return false;
140}
141
143{
144 return (use_motion_blur && attributes.find(ATTR_STD_MOTION_VERTEX_POSITION));
145}
146
147void Geometry::tag_update(Scene *scene, bool rebuild)
148{
149 if (rebuild) {
150 need_update_rebuild = true;
151 scene->light_manager->tag_update(scene, LightManager::MESH_NEED_REBUILD);
152 }
153 else {
154 for (Node *node : used_shaders) {
155 Shader *shader = static_cast<Shader *>(node);
157 scene->light_manager->tag_update(scene, LightManager::EMISSIVE_MESH_MODIFIED);
158 break;
159 }
160 }
161 }
162
163 scene->geometry_manager->tag_update(scene, GeometryManager::GEOMETRY_MODIFIED);
164}
165
166/* Geometry Manager */
167
169{
170 update_flags = UPDATE_ALL;
171 need_flags_update = true;
172}
173
175
177{
178#ifdef WITH_OSL
179 OSLGlobals *og = device->get_cpu_osl_memory();
180 if (og == nullptr) {
181 /* Can happen when rendering with multiple GPUs, but no CPU (in which case the name maps filled
182 * below are not used anyway) */
183 return;
184 }
185
186 og->object_name_map.clear();
187 og->object_names.clear();
188
189 for (size_t i = 0; i < scene->objects.size(); i++) {
190 /* set object name to object index map */
191 Object *object = scene->objects[i];
192 og->object_name_map[object->name] = i;
193 og->object_names.push_back(object->name);
194 }
195#else
196 (void)device;
197 (void)scene;
198#endif
199}
200
201static void update_device_flags_attribute(uint32_t &device_update_flags,
202 const AttributeSet &attributes)
203{
204 for (const Attribute &attr : attributes.attributes) {
205 if (!attr.modified) {
206 continue;
207 }
208
209 const AttrKernelDataType kernel_type = Attribute::kernel_type(attr);
210
211 switch (kernel_type) {
213 device_update_flags |= ATTR_FLOAT_MODIFIED;
214 break;
215 }
217 device_update_flags |= ATTR_FLOAT2_MODIFIED;
218 break;
219 }
221 device_update_flags |= ATTR_FLOAT3_MODIFIED;
222 break;
223 }
225 device_update_flags |= ATTR_FLOAT4_MODIFIED;
226 break;
227 }
229 device_update_flags |= ATTR_UCHAR4_MODIFIED;
230 break;
231 }
233 break;
234 }
235 }
236 }
237}
238
239static void update_attribute_realloc_flags(uint32_t &device_update_flags,
240 const AttributeSet &attributes)
241{
242 if (attributes.modified(AttrKernelDataType::FLOAT)) {
243 device_update_flags |= ATTR_FLOAT_NEEDS_REALLOC;
244 }
245 if (attributes.modified(AttrKernelDataType::FLOAT2)) {
246 device_update_flags |= ATTR_FLOAT2_NEEDS_REALLOC;
247 }
248 if (attributes.modified(AttrKernelDataType::FLOAT3)) {
249 device_update_flags |= ATTR_FLOAT3_NEEDS_REALLOC;
250 }
251 if (attributes.modified(AttrKernelDataType::FLOAT4)) {
252 device_update_flags |= ATTR_FLOAT4_NEEDS_REALLOC;
253 }
254 if (attributes.modified(AttrKernelDataType::UCHAR4)) {
255 device_update_flags |= ATTR_UCHAR4_NEEDS_REALLOC;
256 }
257}
258
260{
261 size_t vert_size = 0;
262 size_t tri_size = 0;
263
264 size_t curve_size = 0;
265 size_t curve_key_size = 0;
266 size_t curve_segment_size = 0;
267
268 size_t point_size = 0;
269
270 size_t face_size = 0;
271 size_t corner_size = 0;
272
273 for (Geometry *geom : scene->geometry) {
274 bool prim_offset_changed = false;
275
276 if (geom->is_mesh() || geom->is_volume()) {
277 Mesh *mesh = static_cast<Mesh *>(geom);
278
279 prim_offset_changed = (mesh->prim_offset != tri_size);
280
281 mesh->vert_offset = vert_size;
282 mesh->prim_offset = tri_size;
283
284 mesh->face_offset = face_size;
285 mesh->corner_offset = corner_size;
286
287 vert_size += mesh->verts.size();
288 tri_size += mesh->num_triangles();
289
290 face_size += mesh->get_num_subd_faces();
291 corner_size += mesh->subd_face_corners.size();
292 }
293 else if (geom->is_hair()) {
294 Hair *hair = static_cast<Hair *>(geom);
295
296 prim_offset_changed = (hair->curve_segment_offset != curve_segment_size);
297 hair->curve_key_offset = curve_key_size;
298 hair->curve_segment_offset = curve_segment_size;
299 hair->prim_offset = curve_size;
300
301 curve_size += hair->num_curves();
302 curve_key_size += hair->get_curve_keys().size();
303 curve_segment_size += hair->num_segments();
304 }
305 else if (geom->is_pointcloud()) {
306 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
307
308 prim_offset_changed = (pointcloud->prim_offset != point_size);
309
310 pointcloud->prim_offset = point_size;
311 point_size += pointcloud->num_points();
312 }
313
314 if (prim_offset_changed) {
315 /* Need to rebuild BVH in OptiX, since refit only allows modified mesh data.
316 * Metal has optimization for static BVH, that also require a rebuild. */
317 const bool need_update_rebuild = (bvh_layout == BVH_LAYOUT_OPTIX ||
318 bvh_layout == BVH_LAYOUT_MULTI_OPTIX ||
319 bvh_layout == BVH_LAYOUT_MULTI_OPTIX_EMBREE) ||
320 ((bvh_layout == BVH_LAYOUT_METAL ||
321 bvh_layout == BVH_LAYOUT_MULTI_METAL ||
322 bvh_layout == BVH_LAYOUT_MULTI_METAL_EMBREE) &&
324 geom->need_update_rebuild |= need_update_rebuild;
325 geom->need_update_bvh_for_offset = true;
326 }
327 }
328}
329
331{
332 if (!need_update() && !need_flags_update) {
333 return;
334 }
335
336 uint32_t device_update_flags = 0;
337
338 const scoped_callback_timer timer([scene](double time) {
339 if (scene->update_stats) {
340 scene->update_stats->geometry.times.add_entry({"device_update_preprocess", time});
341 }
342 });
343
344 progress.set_status("Updating Meshes Flags");
345
346 /* Update flags. */
347 bool volume_images_updated = false;
348
349 for (Geometry *geom : scene->geometry) {
350 geom->has_volume = false;
351
352 update_attribute_realloc_flags(device_update_flags, geom->attributes);
353
354 if (geom->is_mesh()) {
355 Mesh *mesh = static_cast<Mesh *>(geom);
356 update_attribute_realloc_flags(device_update_flags, mesh->subd_attributes);
357 }
358
359 for (Node *node : geom->get_used_shaders()) {
360 Shader *shader = static_cast<Shader *>(node);
361 if (shader->has_volume) {
362 geom->has_volume = true;
363 }
364
365 if (shader->has_surface_bssrdf) {
366 geom->has_surface_bssrdf = true;
367 }
368
369 if (shader->need_update_uvs) {
370 device_update_flags |= ATTR_FLOAT2_NEEDS_REALLOC;
371
372 /* Attributes might need to be tessellated if added. */
373 if (geom->is_mesh()) {
374 Mesh *mesh = static_cast<Mesh *>(geom);
375 if (mesh->need_tesselation()) {
376 mesh->tag_modified();
377 }
378 }
379 }
380
381 if (shader->need_update_attribute) {
382 device_update_flags |= ATTRS_NEED_REALLOC;
383
384 /* Attributes might need to be tessellated if added. */
385 if (geom->is_mesh()) {
386 Mesh *mesh = static_cast<Mesh *>(geom);
387 if (mesh->need_tesselation()) {
388 mesh->tag_modified();
389 }
390 }
391 }
392
393 if (shader->need_update_displacement) {
394 /* tag displacement related sockets as modified */
395 if (geom->is_mesh()) {
396 Mesh *mesh = static_cast<Mesh *>(geom);
397 mesh->tag_verts_modified();
398 mesh->tag_subd_dicing_rate_modified();
399 mesh->tag_subd_max_level_modified();
400 mesh->tag_subd_objecttoworld_modified();
401
402 device_update_flags |= ATTRS_NEED_REALLOC;
403 }
404 }
405 }
406
407 /* only check for modified attributes if we do not need to reallocate them already */
408 if ((device_update_flags & ATTRS_NEED_REALLOC) == 0) {
409 update_device_flags_attribute(device_update_flags, geom->attributes);
410 /* don't check for subd_attributes, as if they were modified, we would need to reallocate
411 * anyway */
412 }
413
414 /* Re-create volume mesh if we will rebuild or refit the BVH. Note we
415 * should only do it in that case, otherwise the BVH and mesh can go
416 * out of sync. */
417 if (geom->is_modified() && geom->is_volume()) {
418 /* Create volume meshes if there is voxel data. */
419 if (!volume_images_updated) {
420 progress.set_status("Updating Meshes Volume Bounds");
422 volume_images_updated = true;
423 }
424
425 Volume *volume = static_cast<Volume *>(geom);
426 create_volume_mesh(scene, volume, progress);
427
428 /* always reallocate when we have a volume, as we need to rebuild the BVH */
429 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
430 }
431
432 if (geom->is_hair()) {
433 /* Set curve shape, still a global scene setting for now. */
434 Hair *hair = static_cast<Hair *>(geom);
435 hair->curve_shape = scene->params.hair_shape;
436
437 if (hair->need_update_rebuild) {
438 device_update_flags |= DEVICE_CURVE_DATA_NEEDS_REALLOC;
439 }
440 else if (hair->is_modified()) {
441 device_update_flags |= DEVICE_CURVE_DATA_MODIFIED;
442 }
443 }
444
445 if (geom->is_mesh()) {
446 Mesh *mesh = static_cast<Mesh *>(geom);
447
448 if (mesh->need_update_rebuild) {
449 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
450 }
451 else if (mesh->is_modified()) {
452 device_update_flags |= DEVICE_MESH_DATA_MODIFIED;
453 }
454 }
455
456 if (geom->is_pointcloud()) {
457 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
458
459 if (pointcloud->need_update_rebuild) {
460 device_update_flags |= DEVICE_POINT_DATA_NEEDS_REALLOC;
461 }
462 else if (pointcloud->is_modified()) {
463 device_update_flags |= DEVICE_POINT_DATA_MODIFIED;
464 }
465 }
466 }
467
468 if (update_flags & (MESH_ADDED | MESH_REMOVED)) {
469 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
470 }
471
472 if (update_flags & (HAIR_ADDED | HAIR_REMOVED)) {
473 device_update_flags |= DEVICE_CURVE_DATA_NEEDS_REALLOC;
474 }
475
476 if (update_flags & (POINT_ADDED | POINT_REMOVED)) {
477 device_update_flags |= DEVICE_POINT_DATA_NEEDS_REALLOC;
478 }
479
480 /* tag the device arrays for reallocation or modification */
481 DeviceScene *dscene = &scene->dscene;
482
485 {
486 scene->bvh.reset();
487
488 dscene->bvh_nodes.tag_realloc();
489 dscene->bvh_leaf_nodes.tag_realloc();
490 dscene->object_node.tag_realloc();
491 dscene->prim_type.tag_realloc();
493 dscene->prim_index.tag_realloc();
494 dscene->prim_object.tag_realloc();
495 dscene->prim_time.tag_realloc();
496
497 if (device_update_flags & DEVICE_MESH_DATA_NEEDS_REALLOC) {
498 dscene->tri_verts.tag_realloc();
499 dscene->tri_vnormal.tag_realloc();
500 dscene->tri_vindex.tag_realloc();
501 dscene->tri_shader.tag_realloc();
502 }
503
504 if (device_update_flags & DEVICE_CURVE_DATA_NEEDS_REALLOC) {
505 dscene->curves.tag_realloc();
506 dscene->curve_keys.tag_realloc();
507 dscene->curve_segments.tag_realloc();
508 }
509
510 if (device_update_flags & DEVICE_POINT_DATA_NEEDS_REALLOC) {
511 dscene->points.tag_realloc();
512 dscene->points_shader.tag_realloc();
513 }
514 }
515
516 if ((update_flags & VISIBILITY_MODIFIED) != 0) {
518 }
519
520 if (device_update_flags & ATTR_FLOAT_NEEDS_REALLOC) {
521 dscene->attributes_map.tag_realloc();
523 }
524 else if (device_update_flags & ATTR_FLOAT_MODIFIED) {
526 }
527
528 if (device_update_flags & ATTR_FLOAT2_NEEDS_REALLOC) {
529 dscene->attributes_map.tag_realloc();
531 }
532 else if (device_update_flags & ATTR_FLOAT2_MODIFIED) {
534 }
535
536 if (device_update_flags & ATTR_FLOAT3_NEEDS_REALLOC) {
537 dscene->attributes_map.tag_realloc();
539 }
540 else if (device_update_flags & ATTR_FLOAT3_MODIFIED) {
542 }
543
544 if (device_update_flags & ATTR_FLOAT4_NEEDS_REALLOC) {
545 dscene->attributes_map.tag_realloc();
546 dscene->attributes_float4.tag_realloc();
547 }
548 else if (device_update_flags & ATTR_FLOAT4_MODIFIED) {
549 dscene->attributes_float4.tag_modified();
550 }
551
552 if (device_update_flags & ATTR_UCHAR4_NEEDS_REALLOC) {
553 dscene->attributes_map.tag_realloc();
555 }
556 else if (device_update_flags & ATTR_UCHAR4_MODIFIED) {
558 }
559
560 if (device_update_flags & DEVICE_MESH_DATA_MODIFIED) {
561 /* if anything else than vertices or shaders are modified, we would need to reallocate, so
562 * these are the only arrays that can be updated */
563 dscene->tri_verts.tag_modified();
564 dscene->tri_vnormal.tag_modified();
565 dscene->tri_shader.tag_modified();
566 }
567
568 if (device_update_flags & DEVICE_CURVE_DATA_MODIFIED) {
569 dscene->curve_keys.tag_modified();
570 dscene->curves.tag_modified();
572 }
573
574 if (device_update_flags & DEVICE_POINT_DATA_MODIFIED) {
575 dscene->points.tag_modified();
576 dscene->points_shader.tag_modified();
577 }
578
579 need_flags_update = false;
580}
581
583 Scene *scene,
585{
586 progress.set_status("Updating Displacement Images");
587 TaskPool pool;
588 ImageManager *image_manager = scene->image_manager.get();
589 set<int> bump_images;
590#ifdef WITH_OSL
591 bool has_osl_node = false;
592#endif
593 for (Geometry *geom : scene->geometry) {
594 if (geom->is_modified()) {
595 /* Geometry-level check for hair shadow transparency.
596 * This matches the logic in the `Hair::update_shadow_transparency()`, avoiding access to
597 * possible non-loaded images. */
598 bool need_shadow_transparency = false;
599 if (geom->is_hair()) {
600 Hair *hair = static_cast<Hair *>(geom);
601 need_shadow_transparency = hair->need_shadow_transparency();
602 }
603
604 for (Node *node : geom->get_used_shaders()) {
605 Shader *shader = static_cast<Shader *>(node);
606 const bool is_true_displacement = (shader->has_displacement &&
607 shader->get_displacement_method() != DISPLACE_BUMP);
608 if (!is_true_displacement && !need_shadow_transparency) {
609 continue;
610 }
611 for (ShaderNode *node : shader->graph->nodes) {
612#ifdef WITH_OSL
614 has_osl_node = true;
615 }
616#endif
618 continue;
619 }
620
621 ImageSlotTextureNode *image_node = static_cast<ImageSlotTextureNode *>(node);
622 for (int i = 0; i < image_node->handle.num_svm_slots(); i++) {
623 const int slot = image_node->handle.svm_slot(i);
624 if (slot != -1) {
625 bump_images.insert(slot);
626 }
627 }
628 }
629 }
630 }
631 }
632
633#ifdef WITH_OSL
634 /* If any OSL node is used for displacement, it may reference a texture. But it's
635 * unknown which ones, so have to load them all. */
636 if (has_osl_node) {
637 OSLShaderManager::osl_image_slots(device, image_manager, bump_images);
638 }
639#endif
640
641 for (const int slot : bump_images) {
642 pool.push([image_manager, device, scene, slot, &progress] {
643 image_manager->device_update_slot(device, scene, slot, progress);
644 });
645 }
646 pool.wait_work();
647}
648
650{
651 progress.set_status("Updating Volume Images");
652 TaskPool pool;
653 ImageManager *image_manager = scene->image_manager.get();
654 set<int> volume_images;
655
656 for (Geometry *geom : scene->geometry) {
657 if (!geom->is_modified()) {
658 continue;
659 }
660
661 for (Attribute &attr : geom->attributes.attributes) {
662 if (attr.element != ATTR_ELEMENT_VOXEL) {
663 continue;
664 }
665
666 const ImageHandle &handle = attr.data_voxel();
667 /* We can build directly from OpenVDB data structures, no need to
668 * load such images early. */
669 if (!handle.vdb_loader()) {
670 const int slot = handle.svm_slot();
671 if (slot != -1) {
672 volume_images.insert(slot);
673 }
674 }
675 }
676 }
677
678 for (const int slot : volume_images) {
679 pool.push([image_manager, device, scene, slot, &progress] {
680 image_manager->device_update_slot(device, scene, slot, progress);
681 });
682 }
683 pool.wait_work();
684}
685
687 DeviceScene *dscene,
688 Scene *scene,
690{
691 if (!need_update()) {
692 return;
693 }
694
695 VLOG_INFO << "Total " << scene->geometry.size() << " meshes.";
696
697 bool true_displacement_used = false;
698 bool curve_shadow_transparency_used = false;
699 size_t num_tessellation = 0;
700
701 {
702 const scoped_callback_timer timer([scene](double time) {
703 if (scene->update_stats) {
704 scene->update_stats->geometry.times.add_entry({"device_update (normals)", time});
705 }
706 });
707
708 for (Geometry *geom : scene->geometry) {
709 if (geom->is_modified()) {
710 if (geom->is_mesh() || geom->is_volume()) {
711 Mesh *mesh = static_cast<Mesh *>(geom);
712
713 /* Test if we need tessellation and setup normals if required. */
714 if (mesh->need_tesselation()) {
715 num_tessellation++;
716 /* OPENSUBDIV Catmull-Clark does not make use of input normals and will overwrite them.
717 */
718#ifdef WITH_OPENSUBDIV
719 if (mesh->get_subdivision_type() != Mesh::SUBDIVISION_CATMULL_CLARK)
720#endif
721 {
722 mesh->add_vertex_normals();
723 }
724 }
725 else {
726 mesh->add_vertex_normals();
727 }
728
729 /* Test if we need displacement. */
730 if (mesh->has_true_displacement()) {
731 true_displacement_used = true;
732 }
733 }
734 else if (geom->is_hair()) {
735 Hair *hair = static_cast<Hair *>(geom);
736 if (hair->need_shadow_transparency()) {
737 curve_shadow_transparency_used = true;
738 }
739 }
740
741 if (progress.get_cancel()) {
742 return;
743 }
744 }
745 }
746 }
747
748 if (progress.get_cancel()) {
749 return;
750 }
751
752 /* Tessellate meshes that are using subdivision */
753 const scoped_callback_timer timer([scene, num_tessellation](double time) {
754 if (scene->update_stats) {
755 scene->update_stats->geometry.times.add_entry(
756 {(num_tessellation) ? "device_update (tessellation and tangents)" :
757 "device_update (tangents)",
758 time});
759 }
760 });
761
762 Camera *dicing_camera = scene->dicing_camera;
763 if (num_tessellation) {
764 dicing_camera->set_screen_size(dicing_camera->get_full_width(),
765 dicing_camera->get_full_height());
766 dicing_camera->update(scene);
767 }
768
769 size_t i = 0;
770 for (Geometry *geom : scene->geometry) {
771 if (!(geom->is_modified() && geom->is_mesh())) {
772 continue;
773 }
774
775 Mesh *mesh = static_cast<Mesh *>(geom);
776
777 if (num_tessellation && mesh->need_tesselation()) {
778 string msg = "Tessellating ";
779 if (mesh->name.empty()) {
780 msg += string_printf("%u/%u", (uint)(i + 1), (uint)num_tessellation);
781 }
782 else {
783 msg += string_printf(
784 "%s %u/%u", mesh->name.c_str(), (uint)(i + 1), (uint)num_tessellation);
785 }
786
787 progress.set_status("Updating Mesh", msg);
788
789 SubdParams subd_params(mesh);
790 subd_params.dicing_rate = mesh->get_subd_dicing_rate();
791 subd_params.max_level = mesh->get_subd_max_level();
792 subd_params.objecttoworld = mesh->get_subd_objecttoworld();
793 subd_params.camera = dicing_camera;
794
795 mesh->tessellate(subd_params);
796
797 i++;
798 }
799
800 /* Apply generated attribute if needed or remove if not needed */
801 mesh->update_generated(scene);
802 /* Apply tangents for generated and UVs (if any need them) or remove if not needed */
803 mesh->update_tangents(scene);
804
805 if (progress.get_cancel()) {
806 return;
807 }
808 }
809
810 if (progress.get_cancel()) {
811 return;
812 }
813
814 /* Update images needed for true displacement. */
815 if (true_displacement_used || curve_shadow_transparency_used) {
816 const scoped_callback_timer timer([scene](double time) {
817 if (scene->update_stats) {
818 scene->update_stats->geometry.times.add_entry(
819 {"device_update (displacement: load images)", time});
820 }
821 });
823 scene->object_manager->device_update_flags(device, dscene, scene, progress, false);
824 }
825
826 /* Device update. */
827 device_free(device, dscene, false);
828
829 const BVHLayout bvh_layout = BVHParams::best_bvh_layout(
830 scene->params.bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
831 geom_calc_offset(scene, bvh_layout);
832 if (true_displacement_used || curve_shadow_transparency_used) {
833 const scoped_callback_timer timer([scene](double time) {
834 if (scene->update_stats) {
835 scene->update_stats->geometry.times.add_entry(
836 {"device_update (displacement: copy meshes to device)", time});
837 }
838 });
839 device_update_mesh(device, dscene, scene, progress);
840 }
841
842 if (progress.get_cancel()) {
843 return;
844 }
845
846 /* Apply transforms, to prepare for static BVH building. */
847 if (scene->params.bvh_type == BVH_TYPE_STATIC) {
848 const scoped_callback_timer timer([scene](double time) {
849 if (scene->update_stats) {
850 scene->update_stats->object.times.add_entry(
851 {"device_update (apply static transforms)", time});
852 }
853 });
854
855 progress.set_status("Updating Objects", "Applying Static Transformations");
856 scene->object_manager->apply_static_transforms(dscene, scene, progress);
857 }
858
859 if (progress.get_cancel()) {
860 return;
861 }
862
863 {
864 const scoped_callback_timer timer([scene](double time) {
865 if (scene->update_stats) {
866 scene->update_stats->geometry.times.add_entry({"device_update (attributes)", time});
867 }
868 });
869 device_update_attributes(device, dscene, scene, progress);
870 if (progress.get_cancel()) {
871 return;
872 }
873 }
874
875 /* Update displacement and hair shadow transparency. */
876 bool displacement_done = false;
877 bool curve_shadow_transparency_done = false;
878
879 {
880 /* Copy constant data needed by shader evaluation. */
881 device->const_copy_to("data", &dscene->data, sizeof(dscene->data));
882
883 const scoped_callback_timer timer([scene](double time) {
884 if (scene->update_stats) {
885 scene->update_stats->geometry.times.add_entry({"device_update (displacement)", time});
886 }
887 });
888
889 for (Geometry *geom : scene->geometry) {
890 if (geom->is_modified()) {
891 if (geom->is_mesh()) {
892 Mesh *mesh = static_cast<Mesh *>(geom);
893 if (displace(device, scene, mesh, progress)) {
894 displacement_done = true;
895 }
896 }
897 else if (geom->is_hair()) {
898 Hair *hair = static_cast<Hair *>(geom);
899 if (hair->update_shadow_transparency(device, scene, progress)) {
900 curve_shadow_transparency_done = true;
901 }
902 }
903 }
904
905 if (progress.get_cancel()) {
906 return;
907 }
908 }
909 }
910
911 if (progress.get_cancel()) {
912 return;
913 }
914
915 /* Device re-update after applying transforms and displacement. */
916 if (displacement_done || curve_shadow_transparency_done) {
917 const scoped_callback_timer timer([scene](double time) {
918 if (scene->update_stats) {
919 scene->update_stats->geometry.times.add_entry(
920 {"device_update (displacement: attributes)", time});
921 }
922 });
923 device_free(device, dscene, false);
924
925 device_update_attributes(device, dscene, scene, progress);
926 if (progress.get_cancel()) {
927 return;
928 }
929 }
930
931 /* Update the BVH even when there is no geometry so the kernel's BVH data is still valid,
932 * especially when removing all of the objects during interactive renders.
933 * Also update the BVH if the transformations change, we cannot rely on tagging the Geometry
934 * as modified in this case, as we may accumulate displacement if the vertices do not also
935 * change. */
936 bool need_update_scene_bvh = (scene->bvh == nullptr ||
937 (update_flags & (TRANSFORM_MODIFIED | VISIBILITY_MODIFIED)) != 0);
938 {
939 const scoped_callback_timer timer([scene](double time) {
940 if (scene->update_stats) {
941 scene->update_stats->geometry.times.add_entry({"device_update (build object BVHs)", time});
942 }
943 });
944
945 /* Work around Embree/oneAPI bug #129596 with BVH updates. */
946 /* Also note the use of #bvh_task_pool_, see its definition for details. */
947 const bool use_multithreaded_build = first_bvh_build ||
949 first_bvh_build = false;
950
951 size_t i = 0;
952 size_t num_bvh = 0;
953 for (Geometry *geom : scene->geometry) {
954 if (geom->is_modified() || geom->need_update_bvh_for_offset) {
955 need_update_scene_bvh = true;
956
957 if (geom->need_build_bvh(bvh_layout)) {
958 i++;
959 num_bvh++;
960 }
961
962 if (use_multithreaded_build) {
963 bvh_task_pool_.push([geom, device, dscene, scene, &progress, i, &num_bvh] {
964 geom->compute_bvh(device, dscene, &scene->params, &progress, i, num_bvh);
965 });
966 }
967 else {
968 geom->compute_bvh(device, dscene, &scene->params, &progress, i, num_bvh);
969 }
970 }
971 }
972
973 TaskPool::Summary summary;
974 bvh_task_pool_.wait_work(&summary);
975 VLOG_WORK << "Objects BVH build pool statistics:\n" << summary.full_report();
976 }
977
978 for (Shader *shader : scene->shaders) {
979 shader->need_update_uvs = false;
980 shader->need_update_attribute = false;
981 shader->need_update_displacement = false;
982 }
983
984 const Scene::MotionType need_motion = scene->need_motion();
985 const bool motion_blur = need_motion == Scene::MOTION_BLUR;
986
987 /* Update objects. */
988 {
989 const scoped_callback_timer timer([scene](double time) {
990 if (scene->update_stats) {
991 scene->update_stats->geometry.times.add_entry({"device_update (compute bounds)", time});
992 }
993 });
994 for (Object *object : scene->objects) {
995 object->compute_bounds(motion_blur);
996 }
997 }
998
999 if (progress.get_cancel()) {
1000 return;
1001 }
1002
1003 if (need_update_scene_bvh) {
1004 const scoped_callback_timer timer([scene](double time) {
1005 if (scene->update_stats) {
1006 scene->update_stats->geometry.times.add_entry({"device_update (build scene BVH)", time});
1007 }
1008 });
1009 device_update_bvh(device, dscene, scene, progress);
1010 if (progress.get_cancel()) {
1011 return;
1012 }
1013 }
1014
1015 /* Always set BVH layout again after displacement where it was set to none,
1016 * to avoid ray-tracing at that stage. */
1017 dscene->data.bvh.bvh_layout = BVHParams::best_bvh_layout(
1018 scene->params.bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
1019
1020 {
1021 const scoped_callback_timer timer([scene](double time) {
1022 if (scene->update_stats) {
1023 scene->update_stats->geometry.times.add_entry(
1024 {"device_update (copy meshes to device)", time});
1025 }
1026 });
1027 device_update_mesh(device, dscene, scene, progress);
1028 if (progress.get_cancel()) {
1029 return;
1030 }
1031 }
1032
1033 /* unset flags */
1034
1035 for (Geometry *geom : scene->geometry) {
1036 geom->clear_modified();
1037 geom->attributes.clear_modified();
1038
1039 if (geom->is_mesh()) {
1040 Mesh *mesh = static_cast<Mesh *>(geom);
1042 }
1043 }
1044
1045 update_flags = UPDATE_NONE;
1046
1047 dscene->bvh_nodes.clear_modified();
1049 dscene->object_node.clear_modified();
1050 dscene->prim_type.clear_modified();
1052 dscene->prim_index.clear_modified();
1053 dscene->prim_object.clear_modified();
1054 dscene->prim_time.clear_modified();
1055 dscene->tri_verts.clear_modified();
1056 dscene->tri_shader.clear_modified();
1057 dscene->tri_vindex.clear_modified();
1058 dscene->tri_vnormal.clear_modified();
1059 dscene->curves.clear_modified();
1060 dscene->curve_keys.clear_modified();
1062 dscene->points.clear_modified();
1063 dscene->points_shader.clear_modified();
1068 dscene->attributes_float4.clear_modified();
1070}
1071
1072void GeometryManager::device_free(Device *device, DeviceScene *dscene, bool force_free)
1073{
1074 dscene->bvh_nodes.free_if_need_realloc(force_free);
1075 dscene->bvh_leaf_nodes.free_if_need_realloc(force_free);
1076 dscene->object_node.free_if_need_realloc(force_free);
1077 dscene->prim_type.free_if_need_realloc(force_free);
1078 dscene->prim_visibility.free_if_need_realloc(force_free);
1079 dscene->prim_index.free_if_need_realloc(force_free);
1080 dscene->prim_object.free_if_need_realloc(force_free);
1081 dscene->prim_time.free_if_need_realloc(force_free);
1082 dscene->tri_verts.free_if_need_realloc(force_free);
1083 dscene->tri_shader.free_if_need_realloc(force_free);
1084 dscene->tri_vnormal.free_if_need_realloc(force_free);
1085 dscene->tri_vindex.free_if_need_realloc(force_free);
1086 dscene->curves.free_if_need_realloc(force_free);
1087 dscene->curve_keys.free_if_need_realloc(force_free);
1088 dscene->curve_segments.free_if_need_realloc(force_free);
1089 dscene->points.free_if_need_realloc(force_free);
1090 dscene->points_shader.free_if_need_realloc(force_free);
1091 dscene->attributes_map.free_if_need_realloc(force_free);
1092 dscene->attributes_float.free_if_need_realloc(force_free);
1093 dscene->attributes_float2.free_if_need_realloc(force_free);
1094 dscene->attributes_float3.free_if_need_realloc(force_free);
1095 dscene->attributes_float4.free_if_need_realloc(force_free);
1096 dscene->attributes_uchar4.free_if_need_realloc(force_free);
1097
1098 /* Signal for shaders like displacement not to do ray tracing. */
1099 dscene->data.bvh.bvh_layout = BVH_LAYOUT_NONE;
1100
1101#ifdef WITH_OSL
1102 OSLGlobals *og = device->get_cpu_osl_memory();
1103
1104 if (og) {
1105 og->object_name_map.clear();
1106 og->object_names.clear();
1107 }
1108#else
1109 (void)device;
1110#endif
1111}
1112
1113void GeometryManager::tag_update(Scene *scene, const uint32_t flag)
1114{
1115 update_flags |= flag;
1116
1117 /* do not tag the object manager for an update if it is the one who tagged us */
1118 if ((flag & OBJECT_MANAGER) == 0) {
1119 scene->object_manager->tag_update(scene, ObjectManager::GEOMETRY_MANAGER);
1120 }
1121}
1122
1124{
1125 return update_flags != UPDATE_NONE;
1126}
1127
1129{
1130 for (const Geometry *geometry : scene->geometry) {
1131 stats->mesh.geometry.add_entry(
1132 NamedSizeEntry(string(geometry->name.c_str()), geometry->get_total_size_in_bytes()));
1133 }
1134}
1135
unsigned int uint
float progress
Definition WM_types.hh:1019
list< Attribute > attributes
bool modified(AttrKernelDataType kernel_type) const
static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts)
Definition bvh.cpp:65
bool contains_device_type(const DeviceType type) const
device_vector< uint > points_shader
Definition devicescene.h:38
device_vector< float2 > prim_time
Definition devicescene.h:24
device_vector< float4 > points
Definition devicescene.h:37
device_vector< uint > prim_visibility
Definition devicescene.h:21
device_vector< float4 > attributes_float4
Definition devicescene.h:56
device_vector< KernelCurveSegment > curve_segments
Definition devicescene.h:34
device_vector< float2 > attributes_float2
Definition devicescene.h:54
device_vector< AttributeMap > attributes_map
Definition devicescene.h:52
device_vector< KernelCurve > curves
Definition devicescene.h:32
device_vector< packed_uint3 > tri_vindex
Definition devicescene.h:30
device_vector< int > object_node
Definition devicescene.h:19
device_vector< packed_float3 > attributes_float3
Definition devicescene.h:55
device_vector< int > prim_object
Definition devicescene.h:23
device_vector< float4 > curve_keys
Definition devicescene.h:33
device_vector< packed_float3 > tri_vnormal
Definition devicescene.h:29
device_vector< int4 > bvh_leaf_nodes
Definition devicescene.h:18
device_vector< uint > tri_shader
Definition devicescene.h:28
device_vector< packed_float3 > tri_verts
Definition devicescene.h:27
device_vector< float > attributes_float
Definition devicescene.h:53
KernelData data
Definition devicescene.h:89
device_vector< int > prim_type
Definition devicescene.h:20
device_vector< int4 > bvh_nodes
Definition devicescene.h:17
device_vector< int > prim_index
Definition devicescene.h:22
device_vector< uchar4 > attributes_uchar4
Definition devicescene.h:57
virtual void const_copy_to(const char *name, void *host, const size_t size)=0
virtual BVHLayoutMask get_bvh_layout_mask(const uint kernel_features) const =0
DeviceInfo info
virtual OSLGlobals * get_cpu_osl_memory()
void device_update_displacement_images(Device *device, Scene *scene, Progress &progress)
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void geom_calc_offset(Scene *scene, BVHLayout bvh_layout)
void tag_update(Scene *scene, const uint32_t flag)
void device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void update_osl_globals(Device *device, Scene *scene)
void create_volume_mesh(const Scene *scene, Volume *volume, Progress &progress)
void device_free(Device *device, DeviceScene *dscene, bool force_free)
void device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void collect_statistics(const Scene *scene, RenderStats *stats)
void device_update_mesh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void device_update_preprocess(Device *device, Scene *scene, Progress &progress)
void device_update_volume_images(Device *device, Scene *scene, Progress &progress)
bool need_update() const
bool displace(Device *device, Scene *scene, Mesh *mesh, Progress &progress)
Transform transform_normal
Type geometry_type
int motion_step(const float time) const
BoundBox bounds
bool need_update_bvh_for_offset
bool transform_applied
bool has_true_displacement() const
bool need_build_bvh(BVHLayout layout) const
bool is_volume() const
bool is_pointcloud() const
bool is_hair() const
bool has_surface_bssrdf
bool is_instanced() const
void compute_bvh(Device *device, DeviceScene *dscene, SceneParams *params, Progress *progress, const size_t n, size_t total)
~Geometry() override
size_t attr_map_offset
size_t prim_offset
virtual bool has_motion_blur() const
void tag_update(Scene *scene, bool rebuild)
bool need_update_rebuild
AttributeSet attributes
bool is_mesh() const
Geometry(const NodeType *node_type, const Type type)
bool transform_negative_scaled
float motion_time(const int step) const
virtual void clear(bool preserve_shaders=false)
Definition hair.h:13
bool need_shadow_transparency()
Definition hair.cpp:582
size_t curve_key_offset
Definition hair.h:89
size_t curve_segment_offset
Definition hair.h:90
size_t num_curves() const
Definition hair.h:126
bool update_shadow_transparency(Device *device, Scene *scene, Progress &progress)
Definition hair.cpp:594
size_t num_segments() const
Definition hair.h:131
CurveShapeType curve_shape
Definition hair.h:91
VDBImageLoader * vdb_loader() const
int num_svm_slots() const
int svm_slot(const int slot_index=0) const
void device_update_slot(Device *device, Scene *scene, const size_t slot, Progress &progress)
@ EMISSIVE_MESH_MODIFIED
Definition scene/light.h:89
NamedSizeStats geometry
void add_entry(const NamedSizeEntry &entry)
Definition stats.cpp:56
BVHLayout bvh_layout
Definition scene.h:65
CurveShapeType hair_shape
Definition scene.h:73
BVHType bvh_type
Definition scene.h:67
ShaderNodeSpecialType special_type
bool has_surface_bssrdf
bool need_update_attribute
bool has_volume
bool need_update_displacement
EmissionSampling emission_sampling
bool need_update_uvs
bool has_displacement
NODE_DECLARE unique_ptr< ShaderGraph > graph
void free_if_need_realloc(bool force_free)
size_t size() const
#define CCL_NAMESPACE_END
@ DEVICE_ONEAPI
#define this
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
@ ATTR_STD_MOTION_VERTEX_POSITION
@ EMISSION_SAMPLING_NONE
@ BVH_LAYOUT_OPTIX
@ BVH_LAYOUT_MULTI_HIPRT_EMBREE
@ BVH_LAYOUT_NONE
@ BVH_LAYOUT_MULTI_EMBREEGPU
@ BVH_LAYOUT_METAL
@ BVH_LAYOUT_MULTI_HIPRT
@ BVH_LAYOUT_HIPRT
@ BVH_LAYOUT_MULTI_OPTIX
@ BVH_LAYOUT_EMBREEGPU
@ BVH_LAYOUT_MULTI_METAL
@ BVH_LAYOUT_MULTI_METAL_EMBREE
@ BVH_LAYOUT_MULTI_EMBREEGPU_EMBREE
@ BVH_LAYOUT_MULTI_OPTIX_EMBREE
@ ATTR_ELEMENT_VOXEL
@ ATTR_PRIM_GEOMETRY
#define VLOG_INFO
Definition log.h:71
#define VLOG_WORK
Definition log.h:74
#define SOCKET_NODE_ARRAY(name, ui_name, node_type,...)
Definition node_type.h:277
#define SOCKET_UINT(name, ui_name, default_value,...)
Definition node_type.h:196
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:192
#define NODE_ABSTRACT_DEFINE(structname)
Definition node_type.h:164
KernelBVHLayout BVHLayout
Definition params.h:22
@ BVH_TYPE_STATIC
Definition params.h:40
AttrKernelDataType
@ FLOAT4
@ NUM
@ FLOAT2
@ UCHAR4
@ FLOAT3
@ FLOAT
static void update_device_flags_attribute(uint32_t &device_update_flags, const AttributeSet &attributes)
static void update_attribute_realloc_flags(uint32_t &device_update_flags, const AttributeSet &attributes)
@ ATTR_FLOAT4_MODIFIED
@ ATTR_UCHAR4_MODIFIED
@ DEVICE_MESH_DATA_NEEDS_REALLOC
@ ATTR_FLOAT3_MODIFIED
@ DEVICE_POINT_DATA_MODIFIED
@ DEVICE_POINT_DATA_NEEDS_REALLOC
@ ATTR_FLOAT4_NEEDS_REALLOC
@ ATTR_UCHAR4_NEEDS_REALLOC
@ ATTR_FLOAT_MODIFIED
@ ATTR_FLOAT2_NEEDS_REALLOC
@ DEVICE_MESH_DATA_MODIFIED
@ DEVICE_CURVE_DATA_MODIFIED
@ ATTR_FLOAT2_MODIFIED
@ ATTR_FLOAT_NEEDS_REALLOC
@ DEVICE_CURVE_DATA_NEEDS_REALLOC
@ ATTR_FLOAT3_NEEDS_REALLOC
@ ATTRS_NEED_REALLOC
@ DISPLACE_BUMP
@ SHADER_SPECIAL_TYPE_IMAGE_SLOT
@ SHADER_SPECIAL_TYPE_OSL
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
AttributeElement element
static AttrKernelDataType kernel_type(const Attribute &attr)
ImageHandle & data_voxel()
void tessellate(SubdParams &params)
size_t get_num_subd_faces() const
Definition scene/mesh.h:229
size_t face_offset
Definition scene/mesh.h:173
size_t vert_offset
Definition scene/mesh.h:171
void update_tangents(Scene *scene)
size_t corner_offset
Definition scene/mesh.h:174
void add_vertex_normals()
AttributeSet subd_attributes
Definition scene/mesh.h:168
bool need_tesselation()
@ SUBDIVISION_CATMULL_CLARK
Definition scene/mesh.h:120
size_t num_triangles() const
Definition scene/mesh.h:77
void update_generated(Scene *scene)
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
const NodeType * type
Definition graph/node.h:178
void dereference_all_used_nodes()
ustring name
Definition graph/node.h:177
size_t get_total_size_in_bytes() const
void clear_modified()
void tag_modified()
bool is_modified() const
Node(const NodeType *type, ustring name=ustring())
size_t num_points() const
MeshStats mesh
unique_ptr< ObjectManager > object_manager
Definition scene.h:150
MotionType need_motion() const
Definition scene.cpp:399
unique_ptr< LightManager > light_manager
Definition scene.h:146
Camera * dicing_camera
Definition scene.h:127
SceneParams params
Definition scene.h:167
unique_ptr_vector< Geometry > geometry
Definition scene.h:140
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:174
unique_ptr_vector< Shader > shaders
Definition scene.h:137
MotionType
Definition scene.h:184
@ MOTION_BLUR
Definition scene.h:184
unique_ptr< GeometryManager > geometry_manager
Definition scene.h:149
unique_ptr_vector< Object > objects
Definition scene.h:141
unique_ptr< ImageManager > image_manager
Definition scene.h:145
unique_ptr< BVH > bvh
Definition scene.h:123
DeviceScene dscene
Definition scene.h:164
int max_level
Definition dice.h:32
Camera * camera
Definition dice.h:33
Transform objecttoworld
Definition dice.h:34
float dicing_rate
Definition dice.h:31
string full_report() const
Definition task.cpp:233
void push(TaskRunFunction &&task)
Definition task.cpp:21
void wait_work(Summary *stats=nullptr)
Definition task.cpp:27
i
Definition text_draw.cc:230
ccl_device_inline Transform transform_identity()
Definition transform.h:289
wmTimer * timer
uint8_t flag
Definition wm_window.cc:139