Blender V5.0
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
49Geometry::Geometry(const NodeType *node_type, const Type type)
51{
52 need_update_rebuild = false;
54
55 transform_applied = false;
59
60 has_volume = false;
61 has_surface_bssrdf = false;
62
64 prim_offset = 0;
65}
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 const bool prev_has_volume = geom->has_volume;
351 geom->has_volume = false;
352
353 update_attribute_realloc_flags(device_update_flags, geom->attributes);
354
355 if (geom->is_mesh()) {
356 Mesh *mesh = static_cast<Mesh *>(geom);
357 update_attribute_realloc_flags(device_update_flags, mesh->subd_attributes);
358 }
359
360 for (Node *node : geom->get_used_shaders()) {
361 Shader *shader = static_cast<Shader *>(node);
362 if (shader->has_volume) {
363 geom->has_volume = true;
364 }
365
366 if (shader->has_surface_bssrdf) {
367 geom->has_surface_bssrdf = true;
368 }
369
370 if (shader->need_update_uvs) {
371 device_update_flags |= ATTR_FLOAT2_NEEDS_REALLOC;
372
373 /* Attributes might need to be tessellated if added. */
374 if (geom->is_mesh()) {
375 Mesh *mesh = static_cast<Mesh *>(geom);
376 if (mesh->need_tesselation()) {
377 mesh->tag_modified();
378 }
379 }
380 }
381
382 if (shader->need_update_attribute) {
383 device_update_flags |= ATTRS_NEED_REALLOC;
384
385 /* Attributes might need to be tessellated if added. */
386 if (geom->is_mesh()) {
387 Mesh *mesh = static_cast<Mesh *>(geom);
388 if (mesh->need_tesselation()) {
389 mesh->tag_modified();
390 }
391 }
392 }
393
394 if (shader->need_update_displacement) {
395 /* tag displacement related sockets as modified */
396 if (geom->is_mesh()) {
397 Mesh *mesh = static_cast<Mesh *>(geom);
398 mesh->tag_verts_modified();
399 mesh->tag_subd_dicing_rate_modified();
400 mesh->tag_subd_max_level_modified();
401 mesh->tag_subd_objecttoworld_modified();
402
403 device_update_flags |= ATTRS_NEED_REALLOC;
404 }
405 }
406
407 if (geom->is_hair() && shader->shadow_transparency_needs_realloc) {
408 device_update_flags |= ATTR_FLOAT_NEEDS_REALLOC;
409 }
410 }
411
412 /* only check for modified attributes if we do not need to reallocate them already */
413 if ((device_update_flags & ATTRS_NEED_REALLOC) == 0) {
414 update_device_flags_attribute(device_update_flags, geom->attributes);
415 /* don't check for subd_attributes, as if they were modified, we would need to reallocate
416 * anyway */
417 }
418
419 /* Re-create volume mesh if we will rebuild or refit the BVH. Note we
420 * should only do it in that case, otherwise the BVH and mesh can go
421 * out of sync. */
422 if (geom->is_modified() && geom->is_volume()) {
423 /* Create volume meshes if there is voxel data. */
424 if (!volume_images_updated) {
425 progress.set_status("Updating Meshes Volume Bounds");
426 device_update_volume_images(device, scene, progress);
427 volume_images_updated = true;
428 }
429
430 Volume *volume = static_cast<Volume *>(geom);
431 create_volume_mesh(scene, volume, progress);
432
433 /* always reallocate when we have a volume, as we need to rebuild the BVH */
434 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
435 }
436
437 if (geom->has_volume) {
438 if (geom->is_modified()) {
439 scene->volume_manager->tag_update(geom);
440 }
441 if (!prev_has_volume) {
442 scene->volume_manager->tag_update();
443 }
444 }
445 else if (prev_has_volume) {
446 scene->volume_manager->tag_update(geom);
447 }
448
449 if (geom->is_hair()) {
450 Hair *hair = static_cast<Hair *>(geom);
451
452 if (hair->need_update_rebuild) {
453 device_update_flags |= DEVICE_CURVE_DATA_NEEDS_REALLOC;
454 }
455 else if (hair->is_modified()) {
456 device_update_flags |= DEVICE_CURVE_DATA_MODIFIED;
457 }
458 }
459
460 if (geom->is_mesh()) {
461 Mesh *mesh = static_cast<Mesh *>(geom);
462
463 if (mesh->need_update_rebuild) {
464 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
465 }
466 else if (mesh->is_modified()) {
467 device_update_flags |= DEVICE_MESH_DATA_MODIFIED;
468 }
469 }
470
471 if (geom->is_pointcloud()) {
472 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
473
474 if (pointcloud->need_update_rebuild) {
475 device_update_flags |= DEVICE_POINT_DATA_NEEDS_REALLOC;
476 }
477 else if (pointcloud->is_modified()) {
478 device_update_flags |= DEVICE_POINT_DATA_MODIFIED;
479 }
480 }
481 }
482
483 if (update_flags & (MESH_ADDED | MESH_REMOVED)) {
484 device_update_flags |= DEVICE_MESH_DATA_NEEDS_REALLOC;
485 }
486
487 if (update_flags & (HAIR_ADDED | HAIR_REMOVED)) {
488 device_update_flags |= DEVICE_CURVE_DATA_NEEDS_REALLOC;
489 }
490
491 if (update_flags & (POINT_ADDED | POINT_REMOVED)) {
492 device_update_flags |= DEVICE_POINT_DATA_NEEDS_REALLOC;
493 }
494
495 /* tag the device arrays for reallocation or modification */
496 DeviceScene *dscene = &scene->dscene;
497
500 {
501 scene->bvh.reset();
502
503 dscene->bvh_nodes.tag_realloc();
504 dscene->bvh_leaf_nodes.tag_realloc();
505 dscene->object_node.tag_realloc();
506 dscene->prim_type.tag_realloc();
508 dscene->prim_index.tag_realloc();
509 dscene->prim_object.tag_realloc();
510 dscene->prim_time.tag_realloc();
511
512 if (device_update_flags & DEVICE_MESH_DATA_NEEDS_REALLOC) {
513 dscene->tri_verts.tag_realloc();
514 dscene->tri_vnormal.tag_realloc();
515 dscene->tri_vindex.tag_realloc();
516 dscene->tri_shader.tag_realloc();
517 }
518
519 if (device_update_flags & DEVICE_CURVE_DATA_NEEDS_REALLOC) {
520 dscene->curves.tag_realloc();
521 dscene->curve_keys.tag_realloc();
522 dscene->curve_segments.tag_realloc();
523 }
524
525 if (device_update_flags & DEVICE_POINT_DATA_NEEDS_REALLOC) {
526 dscene->points.tag_realloc();
527 dscene->points_shader.tag_realloc();
528 }
529 }
530
531 if ((update_flags & VISIBILITY_MODIFIED) != 0) {
533 }
534
535 if (device_update_flags & ATTR_FLOAT_NEEDS_REALLOC) {
536 dscene->attributes_map.tag_realloc();
538 }
539 else if (device_update_flags & ATTR_FLOAT_MODIFIED) {
541 }
542
543 if (device_update_flags & ATTR_FLOAT2_NEEDS_REALLOC) {
544 dscene->attributes_map.tag_realloc();
546 }
547 else if (device_update_flags & ATTR_FLOAT2_MODIFIED) {
549 }
550
551 if (device_update_flags & ATTR_FLOAT3_NEEDS_REALLOC) {
552 dscene->attributes_map.tag_realloc();
554 }
555 else if (device_update_flags & ATTR_FLOAT3_MODIFIED) {
557 }
558
559 if (device_update_flags & ATTR_FLOAT4_NEEDS_REALLOC) {
560 dscene->attributes_map.tag_realloc();
562 }
563 else if (device_update_flags & ATTR_FLOAT4_MODIFIED) {
565 }
566
567 if (device_update_flags & ATTR_UCHAR4_NEEDS_REALLOC) {
568 dscene->attributes_map.tag_realloc();
570 }
571 else if (device_update_flags & ATTR_UCHAR4_MODIFIED) {
573 }
574
575 if (device_update_flags & DEVICE_MESH_DATA_MODIFIED) {
576 /* if anything else than vertices or shaders are modified, we would need to reallocate, so
577 * these are the only arrays that can be updated */
578 dscene->tri_verts.tag_modified();
579 dscene->tri_vnormal.tag_modified();
580 dscene->tri_shader.tag_modified();
581 }
582
583 if (device_update_flags & DEVICE_CURVE_DATA_MODIFIED) {
584 dscene->curve_keys.tag_modified();
585 dscene->curves.tag_modified();
587 }
588
589 if (device_update_flags & DEVICE_POINT_DATA_MODIFIED) {
590 dscene->points.tag_modified();
591 dscene->points_shader.tag_modified();
592 }
593
594 need_flags_update = false;
595}
596
598 Scene *scene,
599 Progress &progress)
600{
601 progress.set_status("Updating Displacement Images");
602 TaskPool pool;
603 ImageManager *image_manager = scene->image_manager.get();
604 set<int> bump_images;
605#ifdef WITH_OSL
606 bool has_osl_node = false;
607#endif
608 for (Geometry *geom : scene->geometry) {
609 if (geom->is_modified()) {
610 /* Geometry-level check for hair shadow transparency.
611 * This matches the logic in the `Hair::update_shadow_transparency()`, avoiding access to
612 * possible non-loaded images. */
613 bool need_shadow_transparency = false;
614 if (geom->is_hair()) {
615 Hair *hair = static_cast<Hair *>(geom);
616 need_shadow_transparency = hair->need_shadow_transparency();
617 }
618
619 for (Node *node : geom->get_used_shaders()) {
620 Shader *shader = static_cast<Shader *>(node);
621 const bool is_true_displacement = (shader->has_displacement &&
622 shader->get_displacement_method() != DISPLACE_BUMP);
623 if (!is_true_displacement && !need_shadow_transparency) {
624 continue;
625 }
626 for (ShaderNode *node : shader->graph->nodes) {
627#ifdef WITH_OSL
629 has_osl_node = true;
630 }
631#endif
633 continue;
634 }
635
636 ImageSlotTextureNode *image_node = static_cast<ImageSlotTextureNode *>(node);
637 for (int i = 0; i < image_node->handle.num_svm_slots(); i++) {
638 const int slot = image_node->handle.svm_slot(i);
639 if (slot != -1) {
640 bump_images.insert(slot);
641 }
642 }
643 }
644 }
645 }
646 }
647
648#ifdef WITH_OSL
649 /* If any OSL node is used for displacement, it may reference a texture. But it's
650 * unknown which ones, so have to load them all. */
651 if (has_osl_node) {
652 OSLShaderManager::osl_image_slots(device, image_manager, bump_images);
653 }
654#endif
655
656 for (const int slot : bump_images) {
657 pool.push([image_manager, device, scene, slot, &progress] {
658 image_manager->device_update_slot(device, scene, slot, progress);
659 });
660 }
661 pool.wait_work();
662}
663
665{
666 progress.set_status("Updating Volume Images");
667 TaskPool pool;
668 ImageManager *image_manager = scene->image_manager.get();
669 set<int> volume_images;
670
671 for (Geometry *geom : scene->geometry) {
672 if (!geom->is_modified()) {
673 continue;
674 }
675
676 for (Attribute &attr : geom->attributes.attributes) {
677 if (attr.element != ATTR_ELEMENT_VOXEL) {
678 continue;
679 }
680
681 const ImageHandle &handle = attr.data_voxel();
682 const int slot = handle.svm_slot();
683 if (slot != -1) {
684 volume_images.insert(slot);
685 }
686 }
687 }
688
689 for (const int slot : volume_images) {
690 pool.push([image_manager, device, scene, slot, &progress] {
691 image_manager->device_update_slot(device, scene, slot, progress);
692 });
693 }
694 pool.wait_work();
695}
696
698 DeviceScene *dscene,
699 Scene *scene,
700 Progress &progress)
701{
702 if (!need_update()) {
703 return;
704 }
705
706 LOG_INFO << "Total " << scene->geometry.size() << " meshes.";
707
708 bool true_displacement_used = false;
709 bool curve_shadow_transparency_used = false;
710 size_t num_tessellation = 0;
711
712 {
713 const scoped_callback_timer timer([scene](double time) {
714 if (scene->update_stats) {
715 scene->update_stats->geometry.times.add_entry({"device_update (normals)", time});
716 }
717 });
718
719 for (Geometry *geom : scene->geometry) {
720 if (geom->is_modified()) {
721 if (geom->is_mesh() || geom->is_volume()) {
722 Mesh *mesh = static_cast<Mesh *>(geom);
723
724 /* Test if we need tessellation and setup normals if required. */
725 if (mesh->need_tesselation()) {
726 num_tessellation++;
727 /* OPENSUBDIV Catmull-Clark does not make use of input normals and will overwrite them.
728 */
729#ifdef WITH_OPENSUBDIV
730 if (mesh->get_subdivision_type() != Mesh::SUBDIVISION_CATMULL_CLARK)
731#endif
732 {
733 mesh->add_vertex_normals();
734 }
735 }
736 else {
737 mesh->add_vertex_normals();
738 }
739
740 /* Test if we need displacement. */
741 if (mesh->has_true_displacement()) {
742 true_displacement_used = true;
743 }
744 }
745 else if (geom->is_hair()) {
746 Hair *hair = static_cast<Hair *>(geom);
747 if (hair->need_shadow_transparency()) {
748 curve_shadow_transparency_used = true;
749 }
750 }
751
752 if (progress.get_cancel()) {
753 return;
754 }
755 }
756 }
757 }
758
759 if (progress.get_cancel()) {
760 return;
761 }
762
763 /* Tessellate meshes that are using subdivision */
764 const scoped_callback_timer timer([scene, num_tessellation](double time) {
765 if (scene->update_stats) {
766 scene->update_stats->geometry.times.add_entry(
767 {(num_tessellation) ? "device_update (tessellation and tangents)" :
768 "device_update (tangents)",
769 time});
770 }
771 });
772
773 Camera *dicing_camera = scene->dicing_camera;
774 if (num_tessellation) {
775 dicing_camera->set_screen_size(dicing_camera->get_full_width(),
776 dicing_camera->get_full_height());
777 dicing_camera->update(scene);
778 }
779
780 size_t i = 0;
781 thread_mutex status_mutex;
782 parallel_for_each(scene->geometry.begin(), scene->geometry.end(), [&](Geometry *geom) {
783 if (progress.get_cancel()) {
784 return;
785 }
786
787 if (!(geom->is_modified() && geom->is_mesh())) {
788 return;
789 }
790
791 Mesh *mesh = static_cast<Mesh *>(geom);
792
793 if (num_tessellation && mesh->need_tesselation()) {
794 {
795 const thread_scoped_lock status_lock(status_mutex);
796 string msg = "Tessellating ";
797 if (mesh->name.empty()) {
798 msg += string_printf("%u/%u", (uint)(i + 1), (uint)num_tessellation);
799 }
800 else {
801 msg += string_printf(
802 "%s %u/%u", mesh->name.c_str(), (uint)(i + 1), (uint)num_tessellation);
803 }
804
805 progress.set_status("Updating Mesh", msg);
806 i++;
807 }
808
809 SubdParams subd_params(mesh);
810 subd_params.dicing_rate = mesh->get_subd_dicing_rate();
811 subd_params.max_level = mesh->get_subd_max_level();
812 if (mesh->get_subd_adaptive_space() == Mesh::SUBDIVISION_ADAPTIVE_SPACE_PIXEL) {
813 subd_params.objecttoworld = mesh->get_subd_objecttoworld();
814 subd_params.camera = dicing_camera;
815 }
816
817 mesh->tessellate(subd_params);
818 }
819
820 /* Apply generated attribute if needed or remove if not needed */
821 mesh->update_generated(scene);
822 /* Apply tangents for generated and UVs (if any need them) or remove if not needed */
823 mesh->update_tangents(scene, true);
824 if (!mesh->has_true_displacement()) {
825 mesh->update_tangents(scene, false);
826 }
827 });
828
829 if (progress.get_cancel()) {
830 return;
831 }
832
833 /* Update images needed for true displacement. */
834 if (true_displacement_used || curve_shadow_transparency_used) {
835 const scoped_callback_timer timer([scene](double time) {
836 if (scene->update_stats) {
837 scene->update_stats->geometry.times.add_entry(
838 {"device_update (displacement: load images)", time});
839 }
840 });
841 device_update_displacement_images(device, scene, progress);
842 scene->object_manager->device_update_flags(device, dscene, scene, progress, false);
843 }
844
845 /* Device update. */
846 device_free(device, dscene, false);
847
848 const BVHLayout bvh_layout = BVHParams::best_bvh_layout(
849 scene->params.bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
850 geom_calc_offset(scene, bvh_layout);
851 if (true_displacement_used || curve_shadow_transparency_used) {
852 const scoped_callback_timer timer([scene](double time) {
853 if (scene->update_stats) {
854 scene->update_stats->geometry.times.add_entry(
855 {"device_update (displacement: copy meshes to device)", time});
856 }
857 });
858 device_update_mesh(device, dscene, scene, progress);
859 }
860
861 if (progress.get_cancel()) {
862 return;
863 }
864
865 /* Apply transforms, to prepare for static BVH building. */
866 if (scene->params.bvh_type == BVH_TYPE_STATIC) {
867 const scoped_callback_timer timer([scene](double time) {
868 if (scene->update_stats) {
869 scene->update_stats->object.times.add_entry(
870 {"device_update (apply static transforms)", time});
871 }
872 });
873
874 progress.set_status("Updating Objects", "Applying Static Transformations");
875 scene->object_manager->apply_static_transforms(dscene, scene, progress);
876 }
877
878 if (progress.get_cancel()) {
879 return;
880 }
881
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 (attributes)", time});
886 }
887 });
888 device_update_attributes(device, dscene, scene, progress);
889 if (progress.get_cancel()) {
890 return;
891 }
892 }
893
894 /* Update displacement and hair shadow transparency. */
895 bool displacement_done = false;
896 bool curve_shadow_transparency_done = false;
897
898 {
899 /* Copy constant data needed by shader evaluation. */
900 device->const_copy_to("data", &dscene->data, sizeof(dscene->data));
901
902 const scoped_callback_timer timer([scene](double time) {
903 if (scene->update_stats) {
904 scene->update_stats->geometry.times.add_entry({"device_update (displacement)", time});
905 }
906 });
907
908 for (Geometry *geom : scene->geometry) {
909 if (geom->is_modified()) {
910 if (geom->is_mesh()) {
911 Mesh *mesh = static_cast<Mesh *>(geom);
912 if (displace(device, scene, mesh, progress)) {
913 displacement_done = true;
914 }
915 }
916 else if (geom->is_hair()) {
917 Hair *hair = static_cast<Hair *>(geom);
918 if (hair->update_shadow_transparency(device, scene, progress)) {
919 curve_shadow_transparency_done = true;
920 }
921 }
922 }
923
924 if (progress.get_cancel()) {
925 return;
926 }
927 }
928 }
929
930 if (progress.get_cancel()) {
931 return;
932 }
933
934 /* Device re-update after applying transforms and displacement. */
935 if (displacement_done || curve_shadow_transparency_done) {
936 const scoped_callback_timer timer([scene](double time) {
937 if (scene->update_stats) {
938 scene->update_stats->geometry.times.add_entry(
939 {"device_update (displacement: attributes)", time});
940 }
941 });
942 device_free(device, dscene, false);
943
944 device_update_attributes(device, dscene, scene, progress);
945 if (progress.get_cancel()) {
946 return;
947 }
948 }
949
950 /* Update the BVH even when there is no geometry so the kernel's BVH data is still valid,
951 * especially when removing all of the objects during interactive renders.
952 * Also update the BVH if the transformations change, we cannot rely on tagging the Geometry
953 * as modified in this case, as we may accumulate displacement if the vertices do not also
954 * change. */
955 bool need_update_scene_bvh = (scene->bvh == nullptr ||
956 (update_flags & (TRANSFORM_MODIFIED | VISIBILITY_MODIFIED)) != 0);
957 {
958 const scoped_callback_timer timer([scene](double time) {
959 if (scene->update_stats) {
960 scene->update_stats->geometry.times.add_entry({"device_update (build object BVHs)", time});
961 }
962 });
963
964 size_t i = 0;
965 size_t num_bvh = 0;
966 for (Geometry *geom : scene->geometry) {
967 if (geom->is_modified() || geom->need_update_bvh_for_offset) {
968 need_update_scene_bvh = true;
969
970 if (geom->need_build_bvh(bvh_layout)) {
971 i++;
972 num_bvh++;
973 }
974
975 /* Note the use of #bvh_task_pool_, see its definition for details. */
976 bvh_task_pool_.push([geom, device, dscene, scene, &progress, i, &num_bvh] {
977 geom->compute_bvh(device, dscene, &scene->params, &progress, i, num_bvh);
978 });
979 }
980 }
981
982 TaskPool::Summary summary;
983 bvh_task_pool_.wait_work(&summary);
984 LOG_DEBUG << "Objects BVH build pool statistics:\n" << summary.full_report();
985 }
986
987 for (Shader *shader : scene->shaders) {
988 shader->need_update_uvs = false;
989 shader->need_update_attribute = false;
990 shader->need_update_displacement = false;
991 shader->shadow_transparency_needs_realloc = false;
992 }
993
994 const Scene::MotionType need_motion = scene->need_motion();
995 const bool motion_blur = need_motion == Scene::MOTION_BLUR;
996
997 /* Update objects. */
998 {
999 const scoped_callback_timer timer([scene](double time) {
1000 if (scene->update_stats) {
1001 scene->update_stats->geometry.times.add_entry({"device_update (compute bounds)", time});
1002 }
1003 });
1004 for (Object *object : scene->objects) {
1005 object->compute_bounds(motion_blur);
1006 }
1007 }
1008
1009 if (progress.get_cancel()) {
1010 return;
1011 }
1012
1013 if (need_update_scene_bvh) {
1014 const scoped_callback_timer timer([scene](double time) {
1015 if (scene->update_stats) {
1016 scene->update_stats->geometry.times.add_entry({"device_update (build scene BVH)", time});
1017 }
1018 });
1019 device_update_bvh(device, dscene, scene, progress);
1020 if (progress.get_cancel()) {
1021 return;
1022 }
1023 }
1024
1025 /* Always set BVH layout again after displacement where it was set to none,
1026 * to avoid ray-tracing at that stage. */
1027 dscene->data.bvh.bvh_layout = BVHParams::best_bvh_layout(
1028 scene->params.bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
1029
1030 {
1031 const scoped_callback_timer timer([scene](double time) {
1032 if (scene->update_stats) {
1033 scene->update_stats->geometry.times.add_entry(
1034 {"device_update (copy meshes to device)", time});
1035 }
1036 });
1037 device_update_mesh(device, dscene, scene, progress);
1038 if (progress.get_cancel()) {
1039 return;
1040 }
1041 }
1042
1043 /* unset flags */
1044
1045 for (Geometry *geom : scene->geometry) {
1046 geom->clear_modified();
1047 geom->attributes.clear_modified();
1048
1049 if (geom->is_mesh()) {
1050 Mesh *mesh = static_cast<Mesh *>(geom);
1052 }
1053 }
1054
1055 update_flags = UPDATE_NONE;
1056
1057 dscene->bvh_nodes.clear_modified();
1058 dscene->bvh_leaf_nodes.clear_modified();
1059 dscene->object_node.clear_modified();
1060 dscene->prim_type.clear_modified();
1061 dscene->prim_visibility.clear_modified();
1062 dscene->prim_index.clear_modified();
1063 dscene->prim_object.clear_modified();
1064 dscene->prim_time.clear_modified();
1065 dscene->tri_verts.clear_modified();
1066 dscene->tri_shader.clear_modified();
1067 dscene->tri_vindex.clear_modified();
1068 dscene->tri_vnormal.clear_modified();
1069 dscene->curves.clear_modified();
1070 dscene->curve_keys.clear_modified();
1071 dscene->curve_segments.clear_modified();
1072 dscene->points.clear_modified();
1073 dscene->points_shader.clear_modified();
1074 dscene->attributes_map.clear_modified();
1075 dscene->attributes_float.clear_modified();
1076 dscene->attributes_float2.clear_modified();
1077 dscene->attributes_float3.clear_modified();
1078 dscene->attributes_float4.clear_modified();
1079 dscene->attributes_uchar4.clear_modified();
1080}
1081
1082void GeometryManager::device_free(Device *device, DeviceScene *dscene, bool force_free)
1083{
1084 dscene->bvh_nodes.free_if_need_realloc(force_free);
1085 dscene->bvh_leaf_nodes.free_if_need_realloc(force_free);
1086 dscene->object_node.free_if_need_realloc(force_free);
1087 dscene->prim_type.free_if_need_realloc(force_free);
1088 dscene->prim_visibility.free_if_need_realloc(force_free);
1089 dscene->prim_index.free_if_need_realloc(force_free);
1090 dscene->prim_object.free_if_need_realloc(force_free);
1091 dscene->prim_time.free_if_need_realloc(force_free);
1092 dscene->tri_verts.free_if_need_realloc(force_free);
1093 dscene->tri_shader.free_if_need_realloc(force_free);
1094 dscene->tri_vnormal.free_if_need_realloc(force_free);
1095 dscene->tri_vindex.free_if_need_realloc(force_free);
1096 dscene->curves.free_if_need_realloc(force_free);
1097 dscene->curve_keys.free_if_need_realloc(force_free);
1098 dscene->curve_segments.free_if_need_realloc(force_free);
1099 dscene->points.free_if_need_realloc(force_free);
1100 dscene->points_shader.free_if_need_realloc(force_free);
1101 dscene->attributes_map.free_if_need_realloc(force_free);
1102 dscene->attributes_float.free_if_need_realloc(force_free);
1103 dscene->attributes_float2.free_if_need_realloc(force_free);
1104 dscene->attributes_float3.free_if_need_realloc(force_free);
1105 dscene->attributes_float4.free_if_need_realloc(force_free);
1106 dscene->attributes_uchar4.free_if_need_realloc(force_free);
1107
1108 /* Signal for shaders like displacement not to do ray tracing. */
1109 dscene->data.bvh.bvh_layout = BVH_LAYOUT_NONE;
1110
1111#ifdef WITH_OSL
1112 OSLGlobals *og = device->get_cpu_osl_memory();
1113
1114 if (og) {
1115 og->object_name_map.clear();
1116 og->object_names.clear();
1117 }
1118#else
1119 (void)device;
1120#endif
1121}
1122
1123void GeometryManager::tag_update(Scene *scene, const uint32_t flag)
1124{
1125 update_flags |= flag;
1126
1127 /* do not tag the object manager for an update if it is the one who tagged us */
1128 if ((flag & OBJECT_MANAGER) == 0) {
1129 scene->object_manager->tag_update(scene, ObjectManager::GEOMETRY_MANAGER);
1130 }
1131}
1132
1134{
1135 return update_flags != UPDATE_NONE;
1136}
1137
1139{
1140 for (const Geometry *geometry : scene->geometry) {
1141 stats->mesh.geometry.add_entry(
1142 NamedSizeEntry(string(geometry->name.c_str()), geometry->get_total_size_in_bytes()));
1143 }
1144}
1145
list< Attribute > attributes
bool modified(AttrKernelDataType kernel_type) const
static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts)
Definition bvh.cpp:65
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:55
device_vector< KernelCurveSegment > curve_segments
Definition devicescene.h:34
device_vector< float2 > attributes_float2
Definition devicescene.h:53
device_vector< AttributeMap > attributes_map
Definition devicescene.h:51
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:54
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:52
KernelData data
Definition devicescene.h:94
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:56
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 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 collect_statistics(const Scene *scene, RenderStats *stats)
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
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:585
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:601
size_t num_segments() const
Definition hair.h:131
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
bool get_cancel() const
Definition progress.h:77
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
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 shadow_transparency_needs_realloc
bool need_update_uvs
bool has_displacement
NODE_DECLARE unique_ptr< ShaderGraph > graph
void free_if_need_realloc(bool force_free)
ConstIterator begin() const
ConstIterator end() const
size_t size() const
#define CCL_NAMESPACE_END
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 LOG_DEBUG
Definition log.h:107
#define LOG_INFO
Definition log.h:106
#define SOCKET_NODE_ARRAY(name, ui_name, node_type,...)
Definition node_type.h:288
#define SOCKET_UINT(name, ui_name, default_value,...)
Definition node_type.h:207
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:203
#define NODE_ABSTRACT_DEFINE(structname)
Definition node_type.h:175
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
AttributeElement element
static AttrKernelDataType kernel_type(const Attribute &attr)
ImageHandle & data_voxel()
size_t get_num_subd_faces() const
Definition scene/mesh.h:235
@ SUBDIVISION_ADAPTIVE_SPACE_PIXEL
Definition scene/mesh.h:139
size_t face_offset
Definition scene/mesh.h:179
size_t vert_offset
Definition scene/mesh.h:177
size_t corner_offset
Definition scene/mesh.h:180
void add_vertex_normals()
AttributeSet subd_attributes
Definition scene/mesh.h:174
bool need_tesselation()
@ SUBDIVISION_CATMULL_CLARK
Definition scene/mesh.h:120
size_t num_triangles() const
Definition scene/mesh.h:77
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
unique_ptr< LightManager > light_manager
Definition scene.h:146
Camera * dicing_camera
Definition scene.h:127
SceneParams params
Definition scene.h:168
unique_ptr_vector< Geometry > geometry
Definition scene.h:140
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:175
MotionType
Definition scene.h:185
@ MOTION_BLUR
Definition scene.h:185
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:165
unique_ptr< VolumeManager > volume_manager
Definition scene.h:154
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
std::mutex thread_mutex
Definition thread.h:27
ccl_device_inline Transform transform_identity()
Definition transform.h:322
wmTimer * timer
uint8_t flag
Definition wm_window.cc:145