Blender V5.3
realize_instances.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
11
13#include "DNA_listBase.h"
14#include "DNA_object_types.h"
15
16#include "BLI_array_utils.hh"
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix.hh"
19#include "BLI_noise.hh"
20
21#include "PRF_profile.hh"
22
23#include "BKE_attribute.hh"
24#include "BKE_curves.hh"
25#include "BKE_customdata.hh"
27#include "BKE_grease_pencil.hh"
29#include "BKE_instances.hh"
30#include "BKE_material.hh"
31#include "BKE_mesh.hh"
32#include "BKE_pointcloud.hh"
34
36
37#include "BLT_translation.hh"
38
39namespace blender::geometry {
40
41using bke::AttrDomain;
42using bke::AttributeDomainAndType;
43using bke::GSpanAttributeWriter;
44using bke::InstanceReference;
45using bke::Instances;
46using bke::SpanAttributeWriter;
47
55
56 int size() const
57 {
58 return this->kinds.size();
59 }
60
62 {
63 return this->kinds.index_range();
64 }
65};
66
78
88
100
103 int vert = 0;
104 int edge = 0;
105 int face = 0;
106 int corner = 0;
107};
108
127
137
182
185 int point = 0;
186 int curve = 0;
187 int custom_knot = 0;
188 int fill_id = 0;
189};
190
201
209
217
222
233
258
272
283
292
307
325
356
372 uint32_t id = 0;
373
375 : pointclouds(gather_info.pointclouds.attributes.size()),
376 meshes(gather_info.meshes.attributes.size()),
377 curves(gather_info.curves.attributes.size()),
378 grease_pencils(gather_info.grease_pencils.attributes.size()),
379 instances(gather_info.instances_attriubutes.size())
380 {
381 // empty
382 }
383};
384
385static bool valid_int_num(const int64_t num)
386{
387 return num >= 0 && num <= INT32_MAX;
388}
389
391{
392 int64_t points_num = 0;
393 if (!tasks.pointcloud_tasks.is_empty()) {
394 const RealizePointCloudTask &task = tasks.pointcloud_tasks.last();
395 points_num += task.start_index + task.pointcloud_info->pointcloud->totpoint;
396 }
397 if (!tasks.mesh_tasks.is_empty()) {
398 const RealizeMeshTask &task = tasks.mesh_tasks.last();
399 points_num += task.start_indices.vert + task.mesh_info->mesh->verts_num;
400 }
401 if (!tasks.curve_tasks.is_empty()) {
402 const RealizeCurveTask &task = tasks.curve_tasks.last();
403 points_num += task.start_indices.point + task.curve_info->curves->geometry.point_num;
404 }
405 return points_num;
406}
407
409{
411}
412
413static void threaded_copy(const GVArray &src, GMutableSpan dst)
414{
415 BLI_assert(src.size() == dst.size());
416 BLI_assert(src.type() == dst.type());
417 threading::parallel_for(src.index_range(), 1024, [&](const IndexRange range) {
418 src.materialize_compressed_to_uninitialized(range, dst.slice(range).data());
419 });
420}
421
422static void threaded_fill(const GPointer value, GMutableSpan dst)
423{
424 BLI_assert(*value.type() == dst.type());
425 threading::parallel_for(IndexRange(dst.size()), 1024, [&](const IndexRange range) {
426 value.type()->fill_construct_n(value.get(), dst.slice(range).data(), range.size());
427 });
428}
429
431 const Span<std::optional<GVArray>> src_attributes,
432 const AttributeFallbacksArray &attribute_fallbacks,
433 const OrderedAttributes &ordered_attributes,
434 const FunctionRef<IndexRange(bke::AttrDomain)> &range_fn,
435 MutableSpan<GSpanAttributeWriter> dst_attribute_writers)
436{
438 dst_attribute_writers.index_range(), 10, [&](const IndexRange attribute_range) {
439 for (const int attribute_index : attribute_range) {
440 const bke::AttrDomain domain = ordered_attributes.kinds[attribute_index].domain;
441 const IndexRange element_slice = range_fn(domain);
442
443 GSpanAttributeWriter &writer = dst_attribute_writers[attribute_index];
444 if (!writer) {
445 continue;
446 }
447 GMutableSpan dst_span = writer.span.slice(element_slice);
448 if (src_attributes[attribute_index].has_value()) {
449 threaded_copy(*src_attributes[attribute_index], dst_span);
450 }
451 else {
452 const CPPType &cpp_type = dst_span.type();
453 const void *fallback = attribute_fallbacks.array[attribute_index] == nullptr ?
454 cpp_type.default_value() :
455 attribute_fallbacks.array[attribute_index];
456 threaded_fill({cpp_type, fallback}, dst_span);
457 }
458 }
459 });
460}
461
463 const VArray<int> &stored_ids,
464 const int task_id,
465 MutableSpan<int> dst_ids)
466{
467 if (options.keep_original_ids) {
468 if (stored_ids.is_empty()) {
469 dst_ids.fill(0);
470 }
471 else {
472 stored_ids.materialize(dst_ids);
473 }
474 }
475 else {
476 if (stored_ids.is_empty()) {
477 threading::parallel_for(dst_ids.index_range(), 1024, [&](const IndexRange range) {
478 for (const int i : range) {
479 dst_ids[i] = noise::hash(task_id, i);
480 }
481 });
482 }
483 else {
484 threading::parallel_for(dst_ids.index_range(), 1024, [&](const IndexRange range) {
485 for (const int i : range) {
486 dst_ids[i] = noise::hash(task_id, stored_ids[i]);
487 }
488 });
489 }
490 }
491}
492
494{
495 if (fill_ids.is_empty()) {
496 return VectorSet<int>();
497 }
498 if (fill_ids.is_single()) {
499 const int fill_id = fill_ids.get_internal_single();
500 if (fill_id != 0) {
501 return VectorSet<int>({fill_id});
502 }
503 }
504 if (fill_ids.is_span()) {
505 VectorSet<int> index_by_fill_ids;
506 const Span<int> fill_ids_span = fill_ids.get_internal_span();
507 for (const int fill_id : fill_ids_span) {
508 if (fill_id != 0) {
509 index_by_fill_ids.add(fill_id);
510 }
511 }
512 return index_by_fill_ids;
513 }
514 VectorSet<int> index_by_fill_ids;
515 for (const int i : fill_ids.index_range()) {
516 const int fill_id = fill_ids[i];
517 if (fill_id != 0) {
518 index_by_fill_ids.add(fill_id);
519 }
520 }
521 return index_by_fill_ids;
522}
523
528
530 const int tasks_num,
531 const OrderedAttributes &ordered_attributes,
532 const FunctionRef<TaskAttrInfo(int)> get_task_attributes,
533 const int attr_index,
534 bke::MutableAttributeAccessor dst_attributes)
535{
536 const bke::AttrType data_type = ordered_attributes.kinds[attr_index].data_type;
537 const auto get_single_value = [&](const int task_index) -> GPointer {
538 const TaskAttrInfo task_info = get_task_attributes(task_index);
539 if (task_info.attributes[attr_index].has_value()) {
540 const CommonVArrayInfo info = task_info.attributes[attr_index]->common_info();
542 return GPointer();
543 }
544 return GPointer(task_info.attributes[attr_index]->type(), info.data);
545 }
546 const CPPType &type = bke::attribute_type_to_cpp_type(data_type);
547 if (const void *value = task_info.fallbacks.array[attr_index]) {
548 return GPointer(type, value);
549 }
550 return GPointer(type, type.default_value());
551 };
552 const GPointer first_value = get_single_value(0);
553 if (!first_value) {
554 return false;
555 }
556 const bool all_equal = threading::parallel_reduce(
557 IndexRange(tasks_num).drop_front(1),
558 32,
559 true,
560 [&](const IndexRange range, bool value) {
561 if (!value) {
562 return false;
563 }
564 for (const int i : range) {
565 const GPointer value = get_single_value(i);
566 if (!value) {
567 return false;
568 }
569 if (!value.type()->is_equal(value.get(), first_value.get())) {
570 return false;
571 }
572 }
573 return true;
574 },
575 std::logical_and<bool>());
576 if (!all_equal) {
577 return false;
578 }
579 const StringRef name = ordered_attributes.names[attr_index];
580 const bke::AttrDomain domain = ordered_attributes.kinds[attr_index].domain;
581 return dst_attributes.add(name, domain, data_type, bke::AttributeInitValue(first_value));
582}
583
584/* -------------------------------------------------------------------- */
587
588/* Forward declaration. */
589static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
590 const int current_depth,
591 const int target_depth,
592 const bke::GeometrySet &geometry_set,
593 const float4x4 &base_transform,
594 const InstanceContext &base_instance_context);
595
598 std::variant<GSpan, GPointer> data;
599};
600
607 GatherTasksInfo &gather_info,
608 const Instances &instances,
609 const OrderedAttributes &ordered_attributes)
610{
611 Vector<AttrFallbackData> attributes_to_override;
612 const bke::AttributeAccessor attributes = instances.attributes();
613 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
614 const int attribute_index = ordered_attributes.names.index_of_try(iter.name);
615 if (attribute_index == -1) {
616 /* The attribute is not propagated to the final geometry. */
617 return;
618 }
619 const bke::GAttributeReader attribute = iter.get();
620 if (!attribute) {
621 return;
622 }
623 const bke::AttrType expected_type = ordered_attributes.kinds[attribute_index].data_type;
624 if (attribute.varray.is_single()) {
625 const CPPType &attr_type = attribute.varray.type();
626 const CPPType &expected_cpp_type = bke::attribute_type_to_cpp_type(expected_type);
627 void *ptr = gather_info.r_temporary_arrays.allocate_owned(attr_type);
628 attr_type.default_construct(ptr);
629 attribute.varray.get_internal_single(ptr);
630 if (iter.data_type != expected_type) {
632 if (!conversions.is_convertible(attr_type, expected_cpp_type)) {
633 /* Ignore the attribute because it can not be converted to the desired type. */
634 return;
635 }
636 /* Convert the attribute on the instances component to the expected attribute type. */
637 void *ptr_converted = gather_info.r_temporary_arrays.allocate_owned(expected_cpp_type);
638 conversions.convert_to_uninitialized(attr_type, expected_cpp_type, ptr, ptr_converted);
639 ptr = ptr_converted;
640 }
641 attributes_to_override.append({attribute_index, GPointer(expected_cpp_type, ptr)});
642 }
643 else {
644 GSpan span = attribute.varray.get_internal_span();
645 if (iter.data_type != expected_type) {
646 const CPPType &from_type = span.type();
647 const CPPType &to_type = bke::attribute_type_to_cpp_type(expected_type);
649 if (!conversions.is_convertible(from_type, to_type)) {
650 /* Ignore the attribute because it can not be converted to the desired type. */
651 return;
652 }
653 /* Convert the attribute on the instances component to the expected attribute type. */
654 GMutableSpan temporary_array = gather_info.r_temporary_arrays.construct<GArray<>>(
655 to_type, instances.instances_num());
656 conversions.convert_to_initialized_n(span, temporary_array);
657 span = temporary_array;
658 }
659 attributes_to_override.append({attribute_index, span});
660 }
661 });
662 return attributes_to_override;
663}
664
670 const InstanceReference &reference,
671 const float4x4 &base_transform,
672 const uint32_t id,
673 FunctionRef<void(const bke::GeometrySet &geometry_set, const float4x4 &transform, uint32_t id)>
674 fn)
675{
676 bke::GeometrySet geometry_set;
677 reference.to_geometry_set(geometry_set);
678 fn(geometry_set, base_transform, id);
679}
680
682 const int current_depth,
683 const int target_depth,
684 const Instances &instances,
685 const float4x4 &base_transform,
686 const InstanceContext &base_instance_context)
687{
688 const Span<InstanceReference> references = instances.references();
689 const Span<int> handles = instances.reference_handles();
690 const Span<float4x4> transforms = instances.transforms();
691
692 VArray<int> stored_instance_ids;
693 if (gather_info.create_id_attribute_on_any_component) {
694 bke::GAttributeReader ids = instances.attributes().lookup("id");
695 if (ids && ids.domain == bke::AttrDomain::Instance && ids.varray.type().is<int>()) {
696 stored_instance_ids = ids.varray.typed<int>();
697 }
698 }
699
700 /* Prepare attribute fallbacks. */
701 InstanceContext instance_context = base_instance_context;
702 Vector<AttrFallbackData> pointcloud_attributes_to_override = prepare_attribute_fallbacks(
703 gather_info, instances, gather_info.pointclouds.attributes);
704 Vector<AttrFallbackData> mesh_attributes_to_override = prepare_attribute_fallbacks(
705 gather_info, instances, gather_info.meshes.attributes);
706 Vector<AttrFallbackData> curve_attributes_to_override = prepare_attribute_fallbacks(
707 gather_info, instances, gather_info.curves.attributes);
708 Vector<AttrFallbackData> grease_pencil_attributes_to_override = prepare_attribute_fallbacks(
709 gather_info, instances, gather_info.grease_pencils.attributes);
710 Vector<AttrFallbackData> instance_attributes_to_override = prepare_attribute_fallbacks(
711 gather_info, instances, gather_info.instances_attriubutes);
712
713 const bool is_top_level = current_depth == 0;
714 /* If at top level, get instance indices from selection field, else use all instances. */
715 const IndexMask indices = is_top_level ? gather_info.selection :
716 IndexMask(IndexRange(instances.instances_num()));
717 indices.foreach_index([&](const int i) {
718 /* If at top level, retrieve depth from gather_info, else continue with target_depth. */
719 const int child_target_depth = is_top_level ? gather_info.depths[i] : target_depth;
720 const int handle = handles[i];
721 const float4x4 &transform = transforms[i];
722 const InstanceReference &reference = references[handle];
723 const float4x4 new_base_transform = base_transform * transform;
724
725 /* Update attribute fallbacks for the current instance. */
726 const auto set_fallbacks = [&](Span<AttrFallbackData> fallbacks,
728 for (const AttrFallbackData &attr : fallbacks) {
729 if (const auto *array = std::get_if<GSpan>(&attr.data)) {
730 ptrs[attr.attr_index] = (*array)[i];
731 }
732 else if (const auto *single = std::get_if<GPointer>(&attr.data)) {
733 ptrs[attr.attr_index] = single->get();
734 }
735 }
736 };
737 set_fallbacks(pointcloud_attributes_to_override, instance_context.pointclouds.array);
738 set_fallbacks(mesh_attributes_to_override, instance_context.meshes.array);
739 set_fallbacks(curve_attributes_to_override, instance_context.curves.array);
740 set_fallbacks(grease_pencil_attributes_to_override, instance_context.grease_pencils.array);
741 set_fallbacks(instance_attributes_to_override, instance_context.instances.array);
742
743 uint32_t local_instance_id = 0;
744 if (gather_info.create_id_attribute_on_any_component) {
745 if (stored_instance_ids.is_empty()) {
746 local_instance_id = uint32_t(i);
747 }
748 else {
749 local_instance_id = uint32_t(stored_instance_ids[i]);
750 }
751 }
752 const uint32_t instance_id = noise::hash(base_instance_context.id, local_instance_id);
753
754 /* Add realize tasks for all referenced geometry sets recursively. */
756 new_base_transform,
758 [&](const bke::GeometrySet &instance_geometry_set,
759 const float4x4 &transform,
760 const uint32_t id) {
761 instance_context.id = id;
763 current_depth + 1,
764 child_target_depth,
765 instance_geometry_set,
766 transform,
767 instance_context);
768 });
769 });
770}
771
776 const int current_depth,
777 const int target_depth,
778 const bke::GeometrySet &geometry_set,
779 const float4x4 &base_transform,
780 const InstanceContext &base_instance_context)
781{
782 if (geometry_set.has_bundle()) {
783 gather_info.r_tasks.bundles.add(geometry_set.bundle());
784 }
785 for (const bke::GeometryComponent *component : geometry_set.get_components()) {
786 const bke::GeometryComponent::Type type = component->type();
787 switch (type) {
789 const Mesh *mesh = (*static_cast<const bke::MeshComponent *>(component)).get();
790 if (mesh != nullptr && mesh->verts_num > 0) {
791 const int mesh_index = gather_info.meshes.order.index_of(mesh);
792 const MeshRealizeInfo &mesh_info = gather_info.meshes.realize_info[mesh_index];
793 gather_info.r_tasks.mesh_tasks.append({{int(gather_info.r_offsets.mesh_offsets.vert),
794 int(gather_info.r_offsets.mesh_offsets.edge),
795 int(gather_info.r_offsets.mesh_offsets.face),
796 int(gather_info.r_offsets.mesh_offsets.corner)},
797 &mesh_info,
798 base_transform,
799 base_instance_context.meshes,
800 base_instance_context.id});
801 gather_info.r_offsets.mesh_offsets.vert += mesh->verts_num;
802 gather_info.r_offsets.mesh_offsets.edge += mesh->edges_num;
803 gather_info.r_offsets.mesh_offsets.corner += mesh->corners_num;
804 gather_info.r_offsets.mesh_offsets.face += mesh->faces_num;
805 }
806 break;
807 }
809 const auto &pointcloud_component = *static_cast<const bke::PointCloudComponent *>(
810 component);
811 const PointCloud *pointcloud = pointcloud_component.get();
812 if (pointcloud != nullptr) {
813 const int pointcloud_index = gather_info.pointclouds.order.index_of(pointcloud);
814 const PointCloudRealizeInfo &pointcloud_info =
815 gather_info.pointclouds.realize_info[pointcloud_index];
816 gather_info.r_tasks.pointcloud_tasks.append(
817 {int(gather_info.r_offsets.pointcloud_offset),
818 &pointcloud_info,
819 base_transform,
820 base_instance_context.pointclouds,
821 base_instance_context.id});
822 gather_info.r_offsets.pointcloud_offset += pointcloud->totpoint;
823 }
824 break;
825 }
827 const auto &curve_component = *static_cast<const bke::CurveComponent *>(component);
828 const Curves *curves = curve_component.get();
829 if (curves != nullptr && curves->geometry.curve_num > 0) {
830 const int curve_index = gather_info.curves.order.index_of(curves);
831 const RealizeCurveInfo &curve_info = gather_info.curves.realize_info[curve_index];
832 gather_info.r_tasks.curve_tasks.append(
833 {{int(gather_info.r_offsets.curves_offsets.point),
834 int(gather_info.r_offsets.curves_offsets.curve),
835 int(gather_info.r_offsets.curves_offsets.custom_knot),
836 int(gather_info.r_offsets.curves_offsets.fill_id)},
837 &curve_info,
838 base_transform,
839 base_instance_context.curves,
840 base_instance_context.id});
841 gather_info.r_offsets.curves_offsets.point += curves->geometry.point_num;
842 gather_info.r_offsets.curves_offsets.curve += curves->geometry.curve_num;
843 gather_info.r_offsets.curves_offsets.custom_knot += curves->geometry.custom_knot_num;
844 gather_info.r_offsets.curves_offsets.fill_id += curve_info.index_by_fill_id.size();
845 }
846 break;
847 }
849 const auto &grease_pencil_component = *static_cast<const bke::GreasePencilComponent *>(
850 component);
851 const GreasePencil *grease_pencil = grease_pencil_component.get();
852 if (grease_pencil != nullptr && !grease_pencil->layers().is_empty()) {
853 const int grease_pencil_index = gather_info.grease_pencils.order.index_of(grease_pencil);
854 const GreasePencilRealizeInfo &grease_pencil_info =
855 gather_info.grease_pencils.realize_info[grease_pencil_index];
856 gather_info.r_tasks.grease_pencil_tasks.append(
857 {int(gather_info.r_offsets.grease_pencil_layer_offset),
858 &grease_pencil_info,
859 base_transform,
860 base_instance_context.grease_pencils});
861 gather_info.r_offsets.grease_pencil_layer_offset += grease_pencil->layers().size();
862 }
863 break;
864 }
866 if (current_depth == target_depth) {
867 gather_info.instances.attribute_fallback.append(base_instance_context.instances);
868 gather_info.instances.instances_components_to_merge.append(component->copy());
869 gather_info.instances.instances_components_transforms.append(base_transform);
870 }
871 else {
872 const Instances *instances =
873 (*static_cast<const bke::InstancesComponent *>(component)).get();
874 if (instances != nullptr && instances->instances_num() > 0) {
876 current_depth,
877 target_depth,
878 *instances,
879 base_transform,
880 base_instance_context);
881 }
882 }
883 break;
884 }
886 if (!gather_info.r_tasks.first_volume) {
887 const bke::VolumeComponent *volume_component = static_cast<const bke::VolumeComponent *>(
888 component);
889 volume_component->add_user();
891 volume_component);
892 }
893 break;
894 }
896 const auto *edit_component = static_cast<const bke::GeometryComponentEditData *>(
897 component);
898 if (edit_component->gizmo_edit_hints_ || edit_component->curves_edit_hints_) {
899 gather_info.r_tasks.edit_data_tasks.append({edit_component, base_transform});
900 }
901 break;
902 }
903 }
904 }
905}
906
909 const bke::GeometryComponent::Type component_type,
911 const int current_depth,
912 const std::optional<int> max_depth,
913 Set<bke::GeometryComponentPtr> &r_components)
914{
915 if (const bke::GeometryComponent *component = geometry.get_component(component_type)) {
916 if (r_components.add_as(component)) {
917 component->add_user();
918 }
919 }
920 if (current_depth == max_depth) {
921 return;
922 }
923 const auto *instances_component = geometry.get_component<bke::InstancesComponent>();
924 if (!instances_component) {
925 return;
926 }
927 const bke::Instances *instances = instances_component->get();
928 if (!instances) {
929 return;
930 }
931 if (options.realize_instance_attributes) {
932 if (r_components.add_as(instances_component)) {
933 instances_component->add_user();
934 }
935 }
936 for (const bke::InstanceReference &reference : instances->references()) {
937 bke::GeometrySet reference_geometry;
938 reference.to_geometry_set(reference_geometry);
940 reference_geometry, component_type, options, current_depth + 1, max_depth, r_components);
941 }
942}
943
946 const bke::GeometryComponent::Type component_type,
948 const VariedDepthOptions &varied_depth_option,
949 Set<bke::GeometryComponentPtr> &r_components)
950{
951 if (const bke::GeometryComponent *component = geometry.get_component(component_type)) {
952 if (r_components.add_as(component)) {
953 component->add_user();
954 }
955 }
956 const auto *instances_component = geometry.get_component<bke::InstancesComponent>();
957 if (!instances_component) {
958 return;
959 }
960 const bke::Instances *instances = instances_component->get();
961 if (!instances) {
962 return;
963 }
964
965 const Span<bke::InstanceReference> references = instances->references();
966 const Span<int> handles = instances->reference_handles();
967 const int references_num = references.size();
968 Array<std::optional<int>> max_reference_depth(references_num, 0);
969
970 varied_depth_option.selection.foreach_index([&](const int instance_i) {
971 const int reference_i = handles[instance_i];
972 const int instance_depth = varied_depth_option.depths[instance_i];
973 std::optional<int> &max_depth = max_reference_depth[reference_i];
974 if (!max_depth.has_value()) {
975 /* Is already at max depth. */
976 return;
977 }
978 if (instance_depth == VariedDepthOptions::MAX_DEPTH) {
979 max_depth.reset();
980 return;
981 }
982 max_depth = std::max<int>(*max_depth, instance_depth);
983 });
984
985 bool is_anything_realized = false;
986 for (const int reference_i : IndexRange(references_num)) {
987 const std::optional<int> max_depth = max_reference_depth[reference_i];
988 if (max_depth == 0) {
989 continue;
990 }
991 const bke::InstanceReference &reference = references[reference_i];
992 bke::GeometrySet reference_geometry;
993 reference.to_geometry_set(reference_geometry);
995 reference_geometry, component_type, options, 1, max_depth, r_components);
996 is_anything_realized = true;
997 }
998
999 if (is_anything_realized) {
1000 if (options.realize_instance_attributes) {
1001 if (r_components.add_as(instances_component)) {
1002 instances_component->add_user();
1003 }
1004 }
1005 }
1006}
1007
1010 const bke::GeometryComponent::Type component_type,
1012 const VariedDepthOptions &varied_depth_option)
1013{
1014 const bke::AttributeFilter &attribute_filter = options.attribute_filter;
1015
1016 const bke::InstancesComponent *top_level_instances_component =
1017 geometry.get_component<bke::InstancesComponent>();
1018 const int top_level_instances_num = top_level_instances_component ?
1019 top_level_instances_component->attribute_domain_size(
1020 AttrDomain::Instance) :
1021 0;
1022
1023 /* Needs to take ownership because some components are only temporary otherwise. */
1025 if (varied_depth_option.depths.get_if_single() == VariedDepthOptions::MAX_DEPTH &&
1026 varied_depth_option.selection.size() == top_level_instances_num)
1027 {
1028 /* In this case we don't have to iterate over all instances, just over the references. */
1030 geometry, component_type, options, 0, std::nullopt, components);
1031 }
1032 else {
1034 geometry, component_type, options, varied_depth_option, components);
1035 }
1036
1037 /* Actually gather the attributes to propagate from the found components. */
1038 bke::GeometrySet::GatheredAttributes attributes_to_propagate;
1039 for (const bke::GeometryComponentPtr &component : components) {
1040 const bke::AttributeAccessor attributes = *component->attributes();
1041 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
1042 if (iter.is_builtin) {
1043 if (!bke::attribute_is_builtin_on_component_type(component_type, iter.name)) {
1044 /* Don't propagate built-in attributes that are not built-in on the
1045 * destination component. */
1046 return;
1047 }
1048 }
1049 if (component->type() == bke::GeometryComponent::Type::Instance) {
1050 if (ELEM(iter.name, "instance_transform", ".reference_index")) {
1051 /* These attributes reference potentially temporary instance components in the set above.
1052 * If we added these names, the string references in the result map would outlive the
1053 * attributes they reference. */
1054 return;
1055 }
1056 }
1057 if (iter.data_type == bke::AttrType::String) {
1058 /* Propagating string attributes is not supported yet. */
1059 return;
1060 }
1061 if (attribute_filter.allow_skip(iter.name)) {
1062 return;
1063 }
1064 AttrDomain dst_domain = iter.domain;
1065 if (component_type != bke::GeometryComponent::Type::Instance &&
1066 dst_domain == AttrDomain::Instance)
1067 {
1068 if (component_type == bke::GeometryComponent::Type::GreasePencil) {
1069 /* For Grease Pencil, we want to propagate the instance attributes to the layers. */
1070 dst_domain = AttrDomain::Layer;
1071 }
1072 else if (component_type == bke::GeometryComponent::Type::Curve &&
1073 !options.realize_to_point_domain)
1074 {
1075 /* For curves, storing the attribute on curves is more efficient. */
1076 dst_domain = AttrDomain::Curve;
1077 }
1078 else {
1079 /* Other instance attributes are realized on the point domain currently. */
1080 dst_domain = AttrDomain::Point;
1081 }
1082 }
1083 attributes_to_propagate.add(iter.name, AttributeDomainAndType{dst_domain, iter.data_type});
1084 });
1085 }
1086
1087 return attributes_to_propagate;
1088}
1089
1091
1092/* -------------------------------------------------------------------- */
1095
1097 const bke::GeometrySet &in_geometry_set,
1099 const VariedDepthOptions &varied_depth_option)
1100{
1103 in_geometry_set, bke::GeometryComponent::Type::Instance, options, varied_depth_option);
1104 OrderedAttributes ordered_attributes;
1105 for (const int i : attributes_to_propagate.names.index_range()) {
1106 if (attributes_to_propagate.names[i] == "id") {
1107 continue;
1108 }
1109 ordered_attributes.names.add_new(attributes_to_propagate.names[i]);
1110 ordered_attributes.kinds.append(attributes_to_propagate.kinds[i]);
1111 }
1112 return ordered_attributes;
1113}
1114
1116 const Span<bke::GeometryComponentPtr> src_components,
1117 const Span<float4x4> src_base_transforms,
1118 const OrderedAttributes &all_instances_attributes,
1119 const Span<geometry::AttributeFallbacksArray> attribute_fallback,
1120 bke::GeometrySet &r_realized_geometry)
1121{
1122 BLI_assert(src_components.size() == src_base_transforms.size() &&
1123 src_components.size() == attribute_fallback.size());
1124 if (src_components.is_empty()) {
1125 return;
1126 }
1127
1129 Array<int> offsets_data(src_components.size() + 1);
1130 for (const int component_index : src_components.index_range()) {
1131 const bke::InstancesComponent &src_component = static_cast<const bke::InstancesComponent &>(
1132 *src_components[component_index]);
1133 offsets_data[component_index] = src_component.get()->instances_num();
1134 }
1135 const OffsetIndices offsets = offset_indices::accumulate_counts_to_offsets(offsets_data);
1136
1137 auto dst_instances = std::make_unique<bke::Instances>(offsets.total_size());
1138 bke::MutableAttributeAccessor dst_attributes = dst_instances->attributes_for_write();
1139
1140 MutableSpan<float4x4> all_transforms = dst_instances->transforms_for_write();
1141 MutableSpan<int> all_handles = dst_instances->reference_handles_for_write();
1142
1143 for (const int component_index : src_components.index_range()) {
1144 const bke::InstancesComponent &src_component = static_cast<const bke::InstancesComponent &>(
1145 *src_components[component_index]);
1146 const bke::Instances &src_instances = *src_component.get();
1147 const float4x4 &src_base_transform = src_base_transforms[component_index];
1148 const Span<bke::InstanceReference> src_references = src_instances.references();
1149 Array<int> handle_map(src_references.size());
1150
1151 for (const int src_handle : src_references.index_range()) {
1152 handle_map[src_handle] = dst_instances->add_reference(src_references[src_handle]);
1153 }
1154 const IndexRange dst_range = offsets[component_index];
1155
1156 const Span<int> src_handles = src_instances.reference_handles();
1157 array_utils::gather(handle_map.as_span(), src_handles, all_handles.slice(dst_range));
1158 array_utils::copy(src_instances.transforms(), all_transforms.slice(dst_range));
1159
1160 for (float4x4 &transform : all_transforms.slice(dst_range)) {
1161 transform = src_base_transform * transform;
1162 }
1163 }
1164
1165 Array<Array<std::optional<GVArray>>> attributes_by_component(src_components.size());
1166 for (const int component_index : src_components.index_range()) {
1167 const auto &src_component = static_cast<const bke::InstancesComponent &>(
1168 *src_components[component_index]);
1169 const bke::AttributeAccessor attributes = *src_component.attributes();
1170 attributes_by_component[component_index].reinitialize(all_instances_attributes.size());
1171 for (const int attr_index : all_instances_attributes.index_range()) {
1172 if (const GVArray attr = *attributes.lookup(
1173 all_instances_attributes.names[attr_index],
1174 all_instances_attributes.kinds[attr_index].domain,
1175 all_instances_attributes.kinds[attr_index].data_type))
1176 {
1177 attributes_by_component[component_index][attr_index] = attr;
1178 }
1179 }
1180 }
1181
1182 for (const int attribute_index : all_instances_attributes.index_range()) {
1183 const StringRef name = all_instances_attributes.names[attribute_index];
1184 const bke::AttrType type = all_instances_attributes.kinds[attribute_index].data_type;
1185 if (ELEM(name, "instance_transform", ".reference_index")) {
1186 continue;
1187 }
1188
1190 src_components.size(),
1191 all_instances_attributes,
1192 [&](const int task_index) -> TaskAttrInfo {
1193 return {.attributes = attributes_by_component[task_index],
1194 .fallbacks = attribute_fallback[task_index]};
1195 },
1196 attribute_index,
1197 dst_attributes))
1198 {
1199 continue;
1200 }
1201
1202 bke::GSpanAttributeWriter write_attribute = dst_attributes.lookup_or_add_for_write_only_span(
1203 name, bke::AttrDomain::Instance, type);
1204 GMutableSpan dst_span = write_attribute.span;
1205 const CPPType &cpp_type = dst_span.type();
1206 for (const int component_index : src_components.index_range()) {
1207 const Span<std::optional<GVArray>> src_attributes = attributes_by_component[component_index];
1208 const Span<const void *> fallbacks = attribute_fallback[component_index].array;
1209 const IndexRange dst_range = offsets[component_index];
1210
1211 if (src_attributes[attribute_index].has_value()) {
1212 threaded_copy(*src_attributes[attribute_index], dst_span.slice(dst_range));
1213 }
1214 else {
1215 const void *fallback = fallbacks[attribute_index] ? fallbacks[attribute_index] :
1216 cpp_type.default_value();
1217 threaded_fill({cpp_type, fallback}, dst_span.slice(dst_range));
1218 }
1219
1220 write_attribute.finish();
1221 }
1222 }
1223
1224 r_realized_geometry.replace_instances(dst_instances.release());
1225}
1226
1228
1229/* -------------------------------------------------------------------- */
1232
1234 const bke::GeometrySet &in_geometry_set,
1236 const VariedDepthOptions &varied_depth_option,
1237 bool &r_create_radii,
1238 bool &r_create_id)
1239{
1241 in_geometry_set, bke::GeometryComponent::Type::PointCloud, options, varied_depth_option);
1242 OrderedAttributes ordered_attributes;
1243 for (const int i : attributes_to_propagate.names.index_range()) {
1244 if (attributes_to_propagate.names[i] == "position") {
1245 continue;
1246 }
1247 if (attributes_to_propagate.names[i] == "id") {
1248 r_create_id = true;
1249 continue;
1250 }
1251 if (attributes_to_propagate.names[i] == "radius") {
1252 r_create_radii = true;
1253 continue;
1254 }
1255 ordered_attributes.names.add_new(attributes_to_propagate.names[i]);
1256 ordered_attributes.kinds.append(attributes_to_propagate.kinds[i]);
1257 }
1258 return ordered_attributes;
1259}
1260
1261static void gather_pointclouds_to_realize(const bke::GeometrySet &geometry_set,
1262 VectorSet<const PointCloud *> &r_pointclouds)
1263{
1264 if (const PointCloud *pointcloud = geometry_set.get_pointcloud()) {
1265 r_pointclouds.add(pointcloud);
1266 }
1267 if (const Instances *instances = geometry_set.get_instances()) {
1268 instances->foreach_referenced_geometry([&](const bke::GeometrySet &instance_geometry_set) {
1269 gather_pointclouds_to_realize(instance_geometry_set, r_pointclouds);
1270 });
1271 }
1272}
1273
1276 const VariedDepthOptions &varied_depth_option)
1277{
1279 AllPointCloudsInfo info;
1281 options,
1282 varied_depth_option,
1284 info.create_id_attribute);
1285
1286 gather_pointclouds_to_realize(geometry_set, info.order);
1287 info.realize_info.reinitialize(info.order.size());
1288 for (const int pointcloud_index : info.realize_info.index_range()) {
1289 PointCloudRealizeInfo &pointcloud_info = info.realize_info[pointcloud_index];
1290 const PointCloud *pointcloud = info.order[pointcloud_index];
1291 pointcloud_info.pointcloud = pointcloud;
1292
1293 /* Access attributes. */
1294 bke::AttributeAccessor attributes = pointcloud->attributes();
1295 pointcloud_info.attributes.reinitialize(info.attributes.size());
1296 for (const int attribute_index : info.attributes.index_range()) {
1297 const StringRef name = info.attributes.names[attribute_index];
1298 const bke::AttrType data_type = info.attributes.kinds[attribute_index].data_type;
1299 const bke::AttrDomain domain = info.attributes.kinds[attribute_index].domain;
1300 if (attributes.contains(name)) {
1301 GVArray attribute = *attributes.lookup_or_default(name, domain, data_type);
1302 pointcloud_info.attributes[attribute_index].emplace(std::move(attribute));
1303 }
1304 }
1305 if (info.create_id_attribute) {
1306 bke::GAttributeReader ids_attribute = attributes.lookup("id");
1307 if (ids_attribute && ids_attribute.domain == bke::AttrDomain::Point &&
1308 ids_attribute.varray.type().is<int>())
1309 {
1310 pointcloud_info.stored_ids = ids_attribute.varray.typed<int>();
1311 }
1312 }
1313 if (info.create_radius_attribute) {
1314 pointcloud_info.radii = *attributes.lookup_or_default(
1315 "radius", bke::AttrDomain::Point, 0.01f);
1316 }
1317 const VArray<float3> position_attribute = *attributes.lookup_or_default<float3>(
1318 "position", bke::AttrDomain::Point, float3(0));
1319 pointcloud_info.positions = position_attribute.get_internal_span();
1320 }
1321 return info;
1322}
1323
1326 const RealizePointCloudTask &task,
1327 const OrderedAttributes &ordered_attributes,
1328 MutableSpan<GSpanAttributeWriter> dst_attribute_writers,
1329 MutableSpan<float> all_dst_radii,
1330 MutableSpan<int> all_dst_ids,
1331 MutableSpan<float3> all_dst_positions)
1332{
1333 const PointCloudRealizeInfo &pointcloud_info = *task.pointcloud_info;
1334 const PointCloud &pointcloud = *pointcloud_info.pointcloud;
1335 const IndexRange point_slice{task.start_index, pointcloud.totpoint};
1336
1338 pointcloud_info.positions, task.transform, all_dst_positions.slice(point_slice));
1339
1340 /* Create point ids. */
1341 if (!all_dst_ids.is_empty()) {
1343 options, pointcloud_info.stored_ids, task.id, all_dst_ids.slice(point_slice));
1344 }
1345 if (!all_dst_radii.is_empty()) {
1346 pointcloud_info.radii.materialize(all_dst_radii.slice(point_slice));
1347 }
1348
1350 pointcloud_info.attributes,
1352 ordered_attributes,
1353 [&](const bke::AttrDomain domain) {
1354 BLI_assert(domain == bke::AttrDomain::Point);
1355 UNUSED_VARS_NDEBUG(domain);
1356 return point_slice;
1357 },
1358 dst_attribute_writers);
1359}
1360
1362 const OrderedAttributes &ordered_attributes,
1363 const AttributeFallbacksArray &attribute_fallbacks,
1365{
1366 for (const int attribute_index : ordered_attributes.index_range()) {
1367 const void *value = attribute_fallbacks.array[attribute_index];
1368 if (!value) {
1369 continue;
1370 }
1371 const bke::AttrDomain domain = ordered_attributes.kinds[attribute_index].domain;
1372 const bke::AttrType data_type = ordered_attributes.kinds[attribute_index].data_type;
1373 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(data_type);
1374 attributes.add(ordered_attributes.names[attribute_index],
1375 domain,
1376 data_type,
1377 bke::AttributeInitValue(GPointer(cpp_type, value)));
1378 }
1379}
1380
1382 const GatherOffsets &offsets,
1383 const AllPointCloudsInfo &all_pointclouds_info,
1384 const Span<RealizePointCloudTask> tasks,
1385 const OrderedAttributes &ordered_attributes,
1386 RealizeInstancesResult &r_result)
1387{
1388 if (tasks.is_empty()) {
1389 return;
1390 }
1391
1393 if (tasks.size() == 1) {
1394 const RealizePointCloudTask &task = tasks.first();
1396 if (!skip_transform(task.transform)) {
1397 math::transform_points(task.transform, new_points->positions_for_write());
1398 new_points->tag_positions_changed();
1399 }
1401 ordered_attributes, task.attribute_fallbacks, new_points->attributes_for_write());
1402 r_result.geometry.replace_pointcloud(new_points);
1403 return;
1404 }
1405
1406 const int64_t tot_points = offsets.pointcloud_offset;
1407 if (!valid_int_num(tot_points)) {
1408 r_result.errors.append(RPT_("Realized point cloud has too many points."));
1409 return;
1410 }
1411
1412 /* Allocate new point cloud. */
1413 const RealizePointCloudTask &first_task = tasks.first();
1414 const PointCloud &first_pointcloud = *first_task.pointcloud_info->pointcloud;
1415 PointCloud *dst_pointcloud = BKE_pointcloud_new_nomain(first_pointcloud.type, tot_points);
1416 r_result.geometry.replace_pointcloud(dst_pointcloud);
1417 bke::MutableAttributeAccessor dst_attributes = dst_pointcloud->attributes_for_write();
1418
1419 dst_pointcloud->mat = MEM_dupalloc(first_pointcloud.mat);
1420 dst_pointcloud->totcol = first_pointcloud.totcol;
1421
1423 "position", bke::AttrDomain::Point);
1424
1425 /* Prepare id attribute. */
1426 SpanAttributeWriter<int> point_ids;
1427 if (all_pointclouds_info.create_id_attribute) {
1428 point_ids = dst_attributes.lookup_or_add_for_write_only_span<int>("id",
1430 }
1431 SpanAttributeWriter<float> point_radii;
1432 if (all_pointclouds_info.create_radius_attribute) {
1433 point_radii = dst_attributes.lookup_or_add_for_write_only_span<float>("radius",
1435 }
1436
1437 /* Prepare generic output attributes. */
1438 Vector<GSpanAttributeWriter> dst_attribute_writers;
1439 for (const int attribute_index : ordered_attributes.index_range()) {
1440 const StringRef name = ordered_attributes.names[attribute_index];
1441 const bke::AttrType data_type = ordered_attributes.kinds[attribute_index].data_type;
1443 tasks.size(),
1444 ordered_attributes,
1445 [&](const int task_index) -> TaskAttrInfo {
1446 return {.attributes = tasks[task_index].pointcloud_info->attributes,
1447 .fallbacks = tasks[task_index].attribute_fallbacks};
1448 },
1449 attribute_index,
1450 dst_attributes))
1451 {
1452 dst_attribute_writers.append({});
1453 continue;
1454 }
1455 dst_attribute_writers.append(
1456 dst_attributes.lookup_or_add_for_write_only_span(name, bke::AttrDomain::Point, data_type));
1457 }
1458
1459 /* Actually execute all tasks. */
1460 threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
1461 for (const int task_index : task_range) {
1462 const RealizePointCloudTask &task = tasks[task_index];
1463 execute_realize_pointcloud_task(options,
1464 task,
1465 ordered_attributes,
1466 dst_attribute_writers,
1467 point_radii.span,
1468 point_ids.span,
1469 positions.span);
1470 }
1471 });
1472
1473 /* Tag modified attributes. */
1474 for (GSpanAttributeWriter &dst_attribute : dst_attribute_writers) {
1475 dst_attribute.finish();
1476 }
1477 positions.finish();
1478 point_radii.finish();
1479 point_ids.finish();
1480}
1481
1483
1484/* -------------------------------------------------------------------- */
1487
1489 const bke::GeometrySet &in_geometry_set,
1491 const VariedDepthOptions &varied_depth_option,
1492 bool &r_create_id,
1493 bool &r_create_material_index)
1494{
1496 in_geometry_set, bke::GeometryComponent::Type::Mesh, options, varied_depth_option);
1497 OrderedAttributes ordered_attributes;
1498 for (const int i : attributes_to_propagate.names.index_range()) {
1499 if (ELEM(attributes_to_propagate.names[i],
1500 "position",
1501 ".edge_verts",
1502 ".corner_vert",
1503 ".corner_edge",
1504 "custom_normal"))
1505 {
1506 continue;
1507 }
1508 if (attributes_to_propagate.names[i] == "id") {
1509 r_create_id = true;
1510 continue;
1511 }
1512 if (attributes_to_propagate.names[i] == "material_index") {
1513 r_create_material_index = true;
1514 continue;
1515 }
1516 ordered_attributes.names.add_new(attributes_to_propagate.names[i]);
1517 ordered_attributes.kinds.append(attributes_to_propagate.kinds[i]);
1518 }
1519 return ordered_attributes;
1520}
1521
1522static void gather_meshes_to_realize(const bke::GeometrySet &geometry_set,
1523 VectorSet<const Mesh *> &r_meshes)
1524{
1525 if (const Mesh *mesh = geometry_set.get_mesh()) {
1526 if (mesh->verts_num > 0) {
1527 r_meshes.add(mesh);
1528 }
1529 }
1530 if (const Instances *instances = geometry_set.get_instances()) {
1531 instances->foreach_referenced_geometry([&](const bke::GeometrySet &instance_geometry_set) {
1532 gather_meshes_to_realize(instance_geometry_set, r_meshes);
1533 });
1534 }
1535}
1536
1539 const VariedDepthOptions &varied_depth_option)
1540{
1542 AllMeshesInfo info;
1544 geometry_set,
1545 options,
1546 varied_depth_option,
1549
1550 gather_meshes_to_realize(geometry_set, info.order);
1551 for (const Mesh *mesh : info.order) {
1552 if (mesh->totcol == 0) {
1553 /* Add an empty material slot for the default material. */
1554 info.materials.add(nullptr);
1555 }
1556 else {
1557 for (const int slot_index : IndexRange(mesh->totcol)) {
1558 Material *material = mesh->mat[slot_index];
1559 info.materials.add(material);
1560 }
1561 }
1562 }
1563
1564 for (const Mesh *mesh : info.order) {
1566 }
1567
1568 info.create_material_index_attribute |= info.materials.size() > 1;
1569 info.realize_info.reinitialize(info.order.size());
1570 for (const int mesh_index : info.realize_info.index_range()) {
1571 MeshRealizeInfo &mesh_info = info.realize_info[mesh_index];
1572 const Mesh *mesh = info.order[mesh_index];
1573 mesh_info.mesh = mesh;
1574 mesh_info.positions = mesh->vert_positions();
1575 mesh_info.edges = mesh->edges();
1576 mesh_info.faces = mesh->faces();
1577 mesh_info.corner_verts = mesh->corner_verts();
1578 mesh_info.corner_edges = mesh->corner_edges();
1579
1580 /* Create material index mapping. */
1581 mesh_info.material_index_map.reinitialize(std::max<int>(mesh->totcol, 1));
1582 if (mesh->totcol == 0) {
1583 mesh_info.material_index_map.first() = info.materials.index_of(nullptr);
1584 }
1585 else {
1586 for (const int old_slot_index : IndexRange(mesh->totcol)) {
1587 Material *material = mesh->mat[old_slot_index];
1588 const int new_slot_index = info.materials.index_of(material);
1589 mesh_info.material_index_map[old_slot_index] = new_slot_index;
1590 }
1591 }
1592
1593 /* Access attributes. */
1594 bke::AttributeAccessor attributes = mesh->attributes();
1595 mesh_info.attributes.reinitialize(info.attributes.size());
1596 for (const int attribute_index : info.attributes.index_range()) {
1597 const StringRef name = info.attributes.names[attribute_index];
1598 const bke::AttrType data_type = info.attributes.kinds[attribute_index].data_type;
1599 const bke::AttrDomain domain = info.attributes.kinds[attribute_index].domain;
1600 if (attributes.contains(name)) {
1601 GVArray attribute = *attributes.lookup_or_default(name, domain, data_type);
1602 mesh_info.attributes[attribute_index].emplace(std::move(attribute));
1603 }
1604 }
1605 if (info.create_id_attribute) {
1606 bke::GAttributeReader ids_attribute = attributes.lookup("id");
1607 if (ids_attribute && ids_attribute.domain == bke::AttrDomain::Point &&
1608 ids_attribute.varray.type().is<int>())
1609 {
1610 mesh_info.stored_vert_ids = ids_attribute.varray.typed<int>();
1611 }
1612 }
1613 mesh_info.material_indices = *attributes.lookup_or_default<int>(
1614 "material_index", bke::AttrDomain::Face, 0);
1615
1616 if (CustomData_has_layer(&mesh->vert_data, CD_ORIGINDEX)) {
1618 }
1619 if (CustomData_has_layer(&mesh->edge_data, CD_ORIGINDEX)) {
1621 }
1622 if (CustomData_has_layer(&mesh->face_data, CD_ORIGINDEX)) {
1624 }
1625
1626 switch (info.custom_normal_info.result_type) {
1628 break;
1629 }
1631 if (const bke::GAttributeReader custom_normal = attributes.lookup("custom_normal")) {
1632 const bke::AttributeMetaData meta_data{
1633 custom_normal.domain, bke::cpp_type_to_attribute_type(custom_normal.varray.type())};
1634 if (bke::mesh::is_corner_fan_normals(meta_data)) {
1635 mesh_info.custom_normal = custom_normal.varray.typed<short2>();
1636 }
1637 }
1638 break;
1639 }
1641 switch (*info.custom_normal_info.result_domain) {
1643 mesh_info.custom_normal = VArray<float3>::from_span(mesh->vert_normals());
1644 break;
1646 mesh_info.custom_normal = VArray<float3>::from_span(mesh->face_normals());
1647 break;
1649 mesh_info.custom_normal = VArray<float3>::from_span(mesh->corner_normals());
1650 break;
1651 default:
1653 }
1654 break;
1655 }
1656 }
1657 }
1658
1659 info.no_loose_edges_hint = std::all_of(
1660 info.order.begin(), info.order.end(), [](const Mesh *mesh) {
1661 return mesh->runtime->loose_edges_cache.is_cached() && mesh->loose_edges().is_empty();
1662 });
1663 info.no_loose_verts_hint = std::all_of(
1664 info.order.begin(), info.order.end(), [](const Mesh *mesh) {
1665 return mesh->runtime->loose_verts_cache.is_cached() && mesh->loose_verts().is_empty();
1666 });
1667 info.no_overlapping_hint = std::all_of(
1668 info.order.begin(), info.order.end(), [](const Mesh *mesh) {
1669 return mesh->no_overlapping_topology();
1670 });
1671
1672 return info;
1673}
1674
1676 const RealizeMeshTask &task,
1677 const OrderedAttributes &ordered_attributes,
1678 MutableSpan<GSpanAttributeWriter> dst_attribute_writers,
1679 MutableSpan<float3> all_dst_positions,
1680 MutableSpan<int2> all_dst_edges,
1681 MutableSpan<int> all_dst_face_offsets,
1682 MutableSpan<int> all_dst_corner_verts,
1683 MutableSpan<int> all_dst_corner_edges,
1684 MutableSpan<int> all_dst_vert_ids,
1685 GSpanAttributeWriter &all_dst_custom_normals,
1686 MutableSpan<int> all_dst_origindex_vert,
1687 MutableSpan<int> all_dst_origindex_edge,
1688 MutableSpan<int> all_dst_origindex_face)
1689{
1690 const MeshRealizeInfo &mesh_info = *task.mesh_info;
1691 const Mesh &mesh = *mesh_info.mesh;
1692
1693 const Span<float3> src_positions = mesh_info.positions;
1694 const Span<int2> src_edges = mesh_info.edges;
1695 const OffsetIndices src_faces = mesh_info.faces;
1696 const Span<int> src_corner_verts = mesh_info.corner_verts;
1697 const Span<int> src_corner_edges = mesh_info.corner_edges;
1698
1699 const IndexRange dst_vert_range(task.start_indices.vert, src_positions.size());
1700 const IndexRange dst_edge_range(task.start_indices.edge, src_edges.size());
1701 const IndexRange dst_face_range(task.start_indices.face, src_faces.size());
1702 const IndexRange dst_corner_range(task.start_indices.corner, src_corner_verts.size());
1703
1704 MutableSpan<float3> dst_positions = all_dst_positions.slice(dst_vert_range);
1705 MutableSpan<int2> dst_edges = all_dst_edges.slice(dst_edge_range);
1706 MutableSpan<int> dst_face_offsets = all_dst_face_offsets.slice(dst_face_range);
1707 MutableSpan<int> dst_corner_verts = all_dst_corner_verts.slice(dst_corner_range);
1708 MutableSpan<int> dst_corner_edges = all_dst_corner_edges.slice(dst_corner_range);
1709
1710 math::transform_points(src_positions, task.transform, dst_positions);
1711
1712 threading::parallel_for(src_edges.index_range(), 1024, [&](const IndexRange edge_range) {
1713 for (const int i : edge_range) {
1714 dst_edges[i] = src_edges[i] + task.start_indices.vert;
1715 }
1716 });
1718 src_corner_verts.index_range(), 1024, [&](const IndexRange corner_range) {
1719 for (const int i : corner_range) {
1720 dst_corner_verts[i] = src_corner_verts[i] + task.start_indices.vert;
1721 }
1722 });
1723 threading::parallel_for(
1724 src_corner_edges.index_range(), 1024, [&](const IndexRange corner_range) {
1725 for (const int i : corner_range) {
1726 dst_corner_edges[i] = src_corner_edges[i] + task.start_indices.edge;
1727 }
1728 });
1729 threading::parallel_for(src_faces.index_range(), 1024, [&](const IndexRange face_range) {
1730 for (const int i : face_range) {
1731 dst_face_offsets[i] = src_faces[i].start() + task.start_indices.corner;
1732 }
1733 });
1734
1735 if (!all_dst_vert_ids.is_empty()) {
1737 mesh_info.stored_vert_ids,
1738 task.id,
1739 all_dst_vert_ids.slice(task.start_indices.vert, mesh.verts_num));
1740 }
1741
1742 const auto domain_to_range = [&](const bke::AttrDomain domain) {
1743 switch (domain) {
1744 case bke::AttrDomain::Point:
1745 return dst_vert_range;
1746 case bke::AttrDomain::Edge:
1747 return dst_edge_range;
1748 case bke::AttrDomain::Face:
1749 return dst_face_range;
1750 case bke::AttrDomain::Corner:
1751 return dst_corner_range;
1752 default:
1754 return IndexRange();
1755 }
1756 };
1757
1758 if (all_dst_custom_normals) {
1759 if (all_dst_custom_normals.span.type().is<short2>()) {
1760 if (mesh_info.custom_normal.is_empty()) {
1761 all_dst_custom_normals.span.typed<short2>().slice(dst_corner_range).fill(short2(0));
1762 }
1763 else {
1764 all_dst_custom_normals.span.typed<short2>()
1765 .slice(dst_corner_range)
1766 .copy_from(mesh_info.custom_normal.typed<short2>());
1767 }
1768 }
1769 else {
1770 const IndexRange dst_range = domain_to_range(all_dst_custom_normals.domain);
1771 math::transform_normals(mesh_info.custom_normal.typed<float3>(),
1772 float3x3(task.transform),
1773 all_dst_custom_normals.span.typed<float3>().slice(dst_range));
1774 }
1775 }
1776
1777 copy_generic_attributes_to_result(mesh_info.attributes,
1778 task.attribute_fallbacks,
1779 ordered_attributes,
1780 domain_to_range,
1781 dst_attribute_writers);
1782
1783 /* Copy original index data per instance. */
1784 if (!all_dst_origindex_vert.is_empty()) {
1785 const IndexRange dst_range(task.start_indices.vert, mesh.verts_num);
1786 if (const int *src_data = static_cast<const int *>(
1787 CustomData_get_layer(&mesh.vert_data, CD_ORIGINDEX)))
1788 {
1789 all_dst_origindex_vert.slice(dst_range).copy_from(Span<int>(src_data, mesh.verts_num));
1790 }
1791 else {
1792 all_dst_origindex_vert.slice(dst_range).fill(ORIGINDEX_NONE);
1793 }
1794 }
1795
1796 if (!all_dst_origindex_edge.is_empty()) {
1797 const IndexRange dst_range(task.start_indices.edge, mesh.edges_num);
1798 if (const int *src_data = static_cast<const int *>(
1799 CustomData_get_layer(&mesh.edge_data, CD_ORIGINDEX)))
1800 {
1801 all_dst_origindex_edge.slice(dst_range).copy_from(Span<int>(src_data, mesh.edges_num));
1802 }
1803 else {
1804 all_dst_origindex_edge.slice(dst_range).fill(ORIGINDEX_NONE);
1805 }
1806 }
1807
1808 if (!all_dst_origindex_face.is_empty()) {
1809 const IndexRange dst_range(task.start_indices.face, mesh.faces_num);
1810 if (const int *src_data = static_cast<const int *>(
1811 CustomData_get_layer(&mesh.face_data, CD_ORIGINDEX)))
1812 {
1813 all_dst_origindex_face.slice(dst_range).copy_from(Span<int>(src_data, mesh.faces_num));
1814 }
1815 else {
1816 all_dst_origindex_face.slice(dst_range).fill(ORIGINDEX_NONE);
1817 }
1818 }
1819}
1820
1821static void copy_vertex_group_name(ListBaseT<bDeformGroup> *dst_deform_group,
1822 const OrderedAttributes &ordered_attributes,
1823 const bDeformGroup &src_deform_group)
1824{
1825 const StringRef src_name = src_deform_group.name;
1826 const int attribute_index = ordered_attributes.names.index_of_try(src_name);
1827 if (attribute_index == -1) {
1828 /* The attribute is not propagated to the result (possibly because the mesh isn't included
1829 * in the realized output because of the #VariedDepthOptions input). */
1830 return;
1831 }
1832 const bke::AttributeDomainAndType kind = ordered_attributes.kinds[attribute_index];
1834 /* Skip if the source attribute can't possibly contain vertex weights. */
1835 return;
1836 }
1837 bDeformGroup *dst = MEM_new<bDeformGroup>(__func__);
1838 src_name.copy_utf8_truncated(dst->name);
1839 BLI_addtail(dst_deform_group, dst);
1840}
1841
1842static void copy_vertex_group_names(Mesh &dst_mesh,
1843 const OrderedAttributes &ordered_attributes,
1844 const Span<const Mesh *> src_meshes)
1845{
1846 Set<StringRef> existing_names;
1847 for (const bDeformGroup &defgroup : dst_mesh.vertex_group_names) {
1848 existing_names.add(defgroup.name);
1849 }
1850 for (const Mesh *mesh : src_meshes) {
1851 for (const bDeformGroup &src : mesh->vertex_group_names) {
1852 if (!existing_names.add(src.name)) {
1853 continue;
1854 }
1855 copy_vertex_group_name(&dst_mesh.vertex_group_names, ordered_attributes, src);
1856 }
1857 }
1858}
1859
1860static int get_mapped_material_index(const MeshRealizeInfo &info, const int index)
1861{
1862 if (info.mesh->totcol == 0) {
1863 return info.material_index_map.first();
1864 }
1865 const bool valid = IndexRange(info.mesh->totcol).contains(index);
1866 return valid ? info.material_index_map[index] : info.material_index_map.first();
1867}
1868
1873static std::optional<int> calc_material_index_max_hint(const Span<MeshRealizeInfo> infos)
1874{
1875 int final_max = std::numeric_limits<int>::min();
1876 for (const MeshRealizeInfo &info : infos) {
1877 if (info.mesh->runtime->max_material_index.is_cached()) {
1878 if (const std::optional<int> max = info.mesh->runtime->max_material_index.data()) {
1879 final_max = std::max(final_max, get_mapped_material_index(info, *max));
1880 }
1881 continue;
1882 }
1883 if (const std::optional<int> single = info.material_indices.get_if_single()) {
1884 final_max = std::max(final_max, get_mapped_material_index(info, *single));
1885 continue;
1886 }
1887 return std::nullopt;
1888 }
1889 return final_max;
1890}
1891
1892static std::optional<int> calc_single_value_material_index(const AllMeshesInfo &all_meshes_info)
1893{
1894 const Span<MeshRealizeInfo> infos = all_meshes_info.realize_info;
1895 const std::optional<int> first_opt = infos[0].material_indices.get_if_single();
1896 if (!first_opt) {
1897 return std::nullopt;
1898 }
1899 const int first = get_mapped_material_index(infos[0], *first_opt);
1900 const bool all_equal = threading::parallel_reduce(
1901 infos.index_range().drop_front(1),
1902 32,
1903 true,
1904 [&](const IndexRange range, bool value) {
1905 if (!value) {
1906 return false;
1907 }
1908 for (const int i : range) {
1909 const MeshRealizeInfo &info = infos[i];
1910 const std::optional<int> value = info.material_indices.get_if_single();
1911 if (!value) {
1912 return false;
1913 }
1914 if (get_mapped_material_index(info, *value) != first) {
1915 return false;
1916 }
1917 }
1918 return true;
1919 },
1920 std::logical_and<bool>());
1921 if (!all_equal) {
1922 return std::nullopt;
1923 }
1924 return first;
1925}
1926
1927static void join_mesh_material_indices(const AllMeshesInfo &all_meshes_info,
1928 const Span<RealizeMeshTask> tasks,
1929 Mesh &dst_mesh)
1930{
1931 if (all_meshes_info.materials.size() <= 1) {
1932 dst_mesh.runtime->max_material_index.ensure([&](std::optional<int> &data) { data = 0; });
1933 return;
1934 }
1935 if (const std::optional<int> max = calc_material_index_max_hint(all_meshes_info.realize_info)) {
1936 dst_mesh.runtime->max_material_index.ensure([&](std::optional<int> &data) { data = max; });
1937 if (max == 0) {
1938 return;
1939 }
1940 }
1941
1942 bke::MutableAttributeAccessor dst_attributes = dst_mesh.attributes_for_write();
1943 if (const std::optional<int> single = calc_single_value_material_index(all_meshes_info)) {
1944 dst_attributes.add<int>(
1945 "material_index", bke::AttrDomain::Face, bke::AttributeInitValue(*single));
1946 return;
1947 }
1948
1949 bke::SpanAttributeWriter dst_attr = dst_attributes.lookup_or_add_for_write_only_span<int>(
1950 "material_index", bke::AttrDomain::Face);
1951
1952 threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange range) {
1953 for (const int task_i : range) {
1954 const RealizeMeshTask &task = tasks[task_i];
1955 const MeshRealizeInfo &mesh_info = *task.mesh_info;
1956 const Mesh &mesh = *mesh_info.mesh;
1957 const IndexRange dst_face_range(task.start_indices.face, mesh.faces_num);
1958 MutableSpan<int> dst_material_indices = dst_attr.span.slice(dst_face_range);
1959 if (mesh.totcol == 0) {
1960 /* The material index map contains the index of the null material in the result. */
1961 dst_material_indices.fill(get_mapped_material_index(mesh_info, 0));
1962 }
1963 else {
1964 if (const std::optional<int> src_index = mesh_info.material_indices.get_if_single()) {
1965 dst_material_indices.fill(get_mapped_material_index(mesh_info, *src_index));
1966 }
1967 else {
1968 const VArraySpan<int> src_span(mesh_info.material_indices);
1969 const Span<int> map = mesh_info.material_index_map;
1970 const int src_mat_num = mesh.totcol;
1971 threading::parallel_for(src_span.index_range(), 1024, [&](const IndexRange face_range) {
1972 for (const int i : face_range) {
1973 const int src_index = src_span[i];
1974 const bool valid = IndexRange(src_mat_num).contains(src_index);
1975 dst_material_indices[i] = valid ? map[src_index] : 0;
1976 }
1977 });
1978 }
1979 }
1980 }
1981 });
1982
1983 dst_attr.finish();
1984}
1985
1987 const GatherOffsets &offsets,
1988 const AllMeshesInfo &all_meshes_info,
1989 const Span<RealizeMeshTask> tasks,
1990 const OrderedAttributes &ordered_attributes,
1991 const VectorSet<Material *> &ordered_materials,
1992 RealizeInstancesResult &r_result)
1993{
1994 if (tasks.is_empty()) {
1995 return;
1996 }
1997
1999 if (tasks.size() == 1) {
2000 const RealizeMeshTask &task = tasks.first();
2001 Mesh *new_mesh = BKE_mesh_copy_for_eval(*task.mesh_info->mesh);
2002 if (!skip_transform(task.transform)) {
2003 bke::mesh_transform(*new_mesh, task.transform, false);
2004 }
2005 if (options.preserve_normals && math::is_negative(task.transform)) {
2006 bke::mesh_flip_faces(*new_mesh, IndexMask(new_mesh->faces_num));
2007 }
2009 ordered_attributes, task.attribute_fallbacks, new_mesh->attributes_for_write());
2010 r_result.geometry.replace_mesh(new_mesh);
2011 return;
2012 }
2013
2014 const int64_t verts_num = offsets.mesh_offsets.vert;
2015 const int64_t edges_num = offsets.mesh_offsets.edge;
2016 const int64_t faces_num = offsets.mesh_offsets.face;
2017 const int64_t corners_num = offsets.mesh_offsets.corner;
2018
2019 if (!valid_int_num(verts_num) || !valid_int_num(edges_num) || !valid_int_num(corners_num) ||
2020 !valid_int_num(faces_num))
2021 {
2022 r_result.errors.append(RPT_("Realized mesh has too many elements."));
2023 return;
2024 }
2025
2026 Mesh *dst_mesh = BKE_mesh_new_nomain(verts_num, edges_num, faces_num, corners_num);
2027 r_result.geometry.replace_mesh(dst_mesh);
2028 bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
2029
2031 MutableSpan<int> dst_origindex_vert;
2032 if (all_meshes_info.create_origindex_vert_attribute) {
2033 int *data = static_cast<int *>(
2035 dst_origindex_vert = MutableSpan<int>(data, verts_num);
2036 }
2037 MutableSpan<int> dst_origindex_edge;
2038 if (all_meshes_info.create_origindex_edge_attribute) {
2039 int *data = static_cast<int *>(
2041 dst_origindex_edge = MutableSpan<int>(data, edges_num);
2042 }
2043 MutableSpan<int> dst_origindex_face;
2044 if (all_meshes_info.create_origindex_face_attribute) {
2045 int *data = static_cast<int *>(
2047 dst_origindex_face = MutableSpan<int>(data, faces_num);
2048 }
2049
2050 MutableSpan<float3> dst_positions = dst_mesh->vert_positions_for_write();
2051 MutableSpan<int2> dst_edges = dst_mesh->edges_for_write();
2052 MutableSpan<int> dst_face_offsets = dst_mesh->face_offsets_for_write();
2053 MutableSpan<int> dst_corner_verts = dst_mesh->corner_verts_for_write();
2054 MutableSpan<int> dst_corner_edges = dst_mesh->corner_edges_for_write();
2055
2056 /* Copy settings from the first input geometry set with a mesh. */
2057 const RealizeMeshTask &first_task = tasks.first();
2058 const Mesh &first_mesh = *first_task.mesh_info->mesh;
2059 BKE_mesh_copy_parameters_for_eval(dst_mesh, &first_mesh);
2060
2061 BLI_assert(dst_mesh->vertex_group_names.count() == first_mesh.vertex_group_names.count());
2063 *dst_mesh, ordered_attributes, all_meshes_info.order.as_span().drop_front(1));
2065
2066 /* Add materials. */
2067 for (const int i : IndexRange(ordered_materials.size())) {
2068 Material *material = ordered_materials[i];
2069 BKE_id_material_eval_assign(&dst_mesh->id, i + 1, material);
2070 }
2071
2072 /* Prepare id attribute. */
2073 SpanAttributeWriter<int> vert_ids;
2074 if (all_meshes_info.create_id_attribute) {
2075 vert_ids = dst_attributes.lookup_or_add_for_write_only_span<int>("id", bke::AttrDomain::Point);
2076 }
2077
2078 GSpanAttributeWriter custom_normals;
2079 switch (all_meshes_info.custom_normal_info.result_type) {
2081 break;
2082 }
2084 custom_normals = dst_attributes.lookup_or_add_for_write_only_span(
2086 break;
2087 }
2089 const bke::AttrDomain domain = *all_meshes_info.custom_normal_info.result_domain;
2090 custom_normals = dst_attributes.lookup_or_add_for_write_only_span(
2091 "custom_normal", domain, bke::AttrType::Float3);
2092 break;
2093 }
2094 }
2095
2096 /* Prepare generic output attributes. */
2097 Vector<GSpanAttributeWriter> dst_attribute_writers;
2098 for (const int attribute_index : ordered_attributes.index_range()) {
2099 const StringRef name = ordered_attributes.names[attribute_index];
2100 const bke::AttrDomain domain = ordered_attributes.kinds[attribute_index].domain;
2101 const bke::AttrType data_type = ordered_attributes.kinds[attribute_index].data_type;
2103 tasks.size(),
2104 ordered_attributes,
2105 [&](const int task_index) -> TaskAttrInfo {
2106 return {.attributes = tasks[task_index].mesh_info->attributes,
2107 .fallbacks = tasks[task_index].attribute_fallbacks};
2108 },
2109 attribute_index,
2110 dst_attributes))
2111 {
2112 dst_attribute_writers.append({});
2113 continue;
2114 }
2115 dst_attribute_writers.append(
2116 dst_attributes.lookup_or_add_for_write_only_span(name, domain, data_type));
2117 }
2118
2119 /* Actually execute all tasks. */
2120 threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
2121 for (const int task_index : task_range) {
2122 const RealizeMeshTask &task = tasks[task_index];
2123 execute_realize_mesh_task(options,
2124 task,
2125 ordered_attributes,
2126 dst_attribute_writers,
2127 dst_positions,
2128 dst_edges,
2129 dst_face_offsets,
2130 dst_corner_verts,
2131 dst_corner_edges,
2132 vert_ids.span,
2133 custom_normals,
2134 dst_origindex_vert,
2135 dst_origindex_edge,
2136 dst_origindex_face);
2137 }
2138 });
2139
2140 join_mesh_material_indices(all_meshes_info, tasks, *dst_mesh);
2141
2142 /* Tag modified attributes. */
2143 for (GSpanAttributeWriter &dst_attribute : dst_attribute_writers) {
2144 dst_attribute.finish();
2145 }
2146 vert_ids.finish();
2147 custom_normals.finish();
2148
2149 if (options.preserve_normals) {
2150 IndexMaskMemory memory;
2151 const IndexMask mirrored_tasks = IndexMask::from_predicate(
2152 tasks.index_range(),
2153 memory,
2154 [&](const int task_i) { return math::is_negative(tasks[task_i].transform); },
2155 exec_mode::grain_size(1024));
2156 if (!mirrored_tasks.is_empty()) {
2157 Array<IndexMask::Initializer> mirrored_face_ranges(mirrored_tasks.size());
2158 mirrored_tasks.foreach_index(
2159 [&](const int task_i, const int pos) {
2160 const RealizeMeshTask &task = tasks[task_i];
2161 mirrored_face_ranges[pos] = IndexRange(task.start_indices.face,
2162 task.mesh_info->mesh->faces_num);
2163 },
2164 exec_mode::grain_size(1024));
2165 const IndexMask mirrored_faces = IndexMask::from_initializers(mirrored_face_ranges, memory);
2166 bke::mesh_flip_faces(*dst_mesh, mirrored_faces);
2167 }
2168 }
2169
2170 bke::mesh_ensure_default_uv_map(*dst_mesh);
2171
2172 if (all_meshes_info.no_loose_edges_hint) {
2173 dst_mesh->tag_loose_edges_none();
2174 }
2175 if (all_meshes_info.no_loose_verts_hint) {
2176 dst_mesh->tag_loose_verts_none();
2177 }
2178 if (all_meshes_info.no_overlapping_hint) {
2179 dst_mesh->tag_overlapping_none();
2180 }
2181}
2182
2184
2185/* -------------------------------------------------------------------- */
2188
2190 const bke::GeometrySet &in_geometry_set,
2192 const VariedDepthOptions &varied_depth_option,
2193 bool &r_create_id,
2194 bool &r_create_fill_id)
2195{
2197 in_geometry_set, bke::GeometryComponent::Type::Curve, options, varied_depth_option);
2198 OrderedAttributes ordered_attributes;
2199 for (const int i : attributes_to_propagate.names.index_range()) {
2200 if (ELEM(attributes_to_propagate.names[i],
2201 "position",
2202 "radius",
2203 "handle_left",
2204 "handle_right",
2205 "custom_normal"))
2206 {
2207 continue;
2208 }
2209 if (attributes_to_propagate.names[i] == "id") {
2210 r_create_id = true;
2211 continue;
2212 }
2213 if (attributes_to_propagate.names[i] == "fill_id") {
2214 r_create_fill_id = true;
2215 continue;
2216 }
2217 ordered_attributes.names.add_new(attributes_to_propagate.names[i]);
2218 ordered_attributes.kinds.append(attributes_to_propagate.kinds[i]);
2219 }
2220 return ordered_attributes;
2221}
2222
2223static void gather_curves_to_realize(const bke::GeometrySet &geometry_set,
2224 VectorSet<const Curves *> &r_curves)
2225{
2226 if (const Curves *curves = geometry_set.get_curves()) {
2227 if (curves->geometry.curve_num != 0) {
2228 r_curves.add(curves);
2229 }
2230 }
2231 if (const Instances *instances = geometry_set.get_instances()) {
2232 instances->foreach_referenced_geometry([&](const bke::GeometrySet &instance_geometry_set) {
2233 gather_curves_to_realize(instance_geometry_set, r_curves);
2234 });
2235 }
2236}
2237
2240 const VariedDepthOptions &varied_depth_option)
2241{
2243 AllCurvesInfo info;
2245 options,
2246 varied_depth_option,
2249
2250 gather_curves_to_realize(geometry_set, info.order);
2251 info.realize_info.reinitialize(info.order.size());
2252 for (const int curve_index : info.realize_info.index_range()) {
2253 RealizeCurveInfo &curve_info = info.realize_info[curve_index];
2254 const Curves *curves_id = info.order[curve_index];
2255 const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
2256 curve_info.curves = curves_id;
2257
2258 /* Access attributes. */
2259 bke::AttributeAccessor attributes = curves.attributes();
2260 curve_info.attributes.reinitialize(info.attributes.size());
2261 for (const int attribute_index : info.attributes.index_range()) {
2262 const bke::AttrDomain domain = info.attributes.kinds[attribute_index].domain;
2263 const StringRef name = info.attributes.names[attribute_index];
2264 const bke::AttrType data_type = info.attributes.kinds[attribute_index].data_type;
2265 if (attributes.contains(name)) {
2266 GVArray attribute = *attributes.lookup_or_default(name, domain, data_type);
2267 curve_info.attributes[attribute_index].emplace(std::move(attribute));
2268 }
2269 }
2270 if (info.create_id_attribute) {
2271 bke::GAttributeReader id_attribute = attributes.lookup("id");
2272 if (id_attribute && id_attribute.domain == bke::AttrDomain::Point &&
2273 id_attribute.varray.type().is<int>())
2274 {
2275 curve_info.stored_ids = id_attribute.varray.typed<int>();
2276 }
2277 }
2278 if (info.create_fill_id_attribute) {
2279 bke::GAttributeReader fill_id_attribute = attributes.lookup("fill_id");
2280 if (fill_id_attribute && fill_id_attribute.domain == bke::AttrDomain::Curve &&
2281 fill_id_attribute.varray.type().is<int>())
2282 {
2283 curve_info.stored_fill_ids = fill_id_attribute.varray.typed<int>();
2284 curve_info.index_by_fill_id = gather_all_fill_ids(curve_info.stored_fill_ids);
2285 }
2286 }
2287
2288 if (attributes.contains("radius")) {
2289 curve_info.radius = *attributes.lookup<float>("radius", bke::AttrDomain::Point);
2290 info.create_radius_attribute = true;
2291 }
2292 if (attributes.contains("handle_right")) {
2293 curve_info.handle_left = attributes.lookup<float3>("handle_left", bke::AttrDomain::Point)
2294 .varray.get_internal_span();
2295 curve_info.handle_right = attributes.lookup<float3>("handle_right", bke::AttrDomain::Point)
2296 .varray.get_internal_span();
2298 }
2299 if (attributes.contains("custom_normal")) {
2300 curve_info.custom_normal = attributes.lookup<float3>("custom_normal", bke::AttrDomain::Point)
2301 .varray.get_internal_span();
2303 }
2304 }
2305 return info;
2306}
2307
2309 InstanceContext &attribute_fallbacks)
2310{
2311 if (all_curves_info.order.is_empty()) {
2312 return;
2313 }
2314 const Curves *first = all_curves_info.order[0];
2315 const bke::CurvesGeometry &first_curves = first->geometry.wrap();
2316 for (const int attribute_i : attribute_fallbacks.curves.array.index_range()) {
2317 const StringRef name = all_curves_info.attributes.names[attribute_i];
2318 if (first_curves.attributes().is_builtin(name)) {
2319 attribute_fallbacks.curves.array[attribute_i] =
2320 first_curves.attributes().get_builtin_default(name).get();
2321 }
2322 }
2323}
2324
2326 const AllCurvesInfo &all_curves_info,
2327 const RealizeCurveTask &task,
2328 const OrderedAttributes &ordered_attributes,
2329 bke::CurvesGeometry &dst_curves,
2330 MutableSpan<GSpanAttributeWriter> dst_attribute_writers,
2331 MutableSpan<int> all_dst_ids,
2332 MutableSpan<int> all_dst_fill_ids,
2333 MutableSpan<float3> all_handle_left,
2334 MutableSpan<float3> all_handle_right,
2335 MutableSpan<float> all_radii,
2336 MutableSpan<float3> all_custom_normals)
2337{
2338 const RealizeCurveInfo &curves_info = *task.curve_info;
2339 const Curves &curves_id = *curves_info.curves;
2340 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
2341
2342 const IndexRange dst_point_range{task.start_indices.point, curves.points_num()};
2343 const IndexRange dst_curve_range{task.start_indices.curve, curves.curves_num()};
2344 const IndexRange dst_custom_knot_range{task.start_indices.custom_knot,
2345 curves.nurbs_custom_knots_by_curve().total_size()};
2346
2348 curves.positions(), task.transform, dst_curves.positions_for_write().slice(dst_point_range));
2349
2350 /* Copy and transform handle positions if necessary. */
2351 if (all_curves_info.create_handle_postion_attributes) {
2352 if (curves_info.handle_left.is_empty()) {
2353 all_handle_left.slice(dst_point_range).fill(float3(0));
2354 }
2355 else {
2357 curves_info.handle_left, task.transform, all_handle_left.slice(dst_point_range));
2358 }
2359 if (curves_info.handle_right.is_empty()) {
2360 all_handle_right.slice(dst_point_range).fill(float3(0));
2361 }
2362 else {
2364 curves_info.handle_right, task.transform, all_handle_right.slice(dst_point_range));
2365 }
2366 }
2367
2368 if (all_curves_info.create_radius_attribute) {
2369 if (curves_info.radius.is_empty()) {
2370 all_radii.slice(dst_point_range).fill(1.0f);
2371 }
2372 else {
2373 curves_info.radius.materialize(all_radii.slice(dst_point_range));
2374 }
2375 }
2376
2377 if (all_curves_info.create_custom_normal_attribute) {
2378 if (curves_info.custom_normal.is_empty()) {
2379 all_custom_normals.slice(dst_point_range).fill(float3(0, 0, 1));
2380 }
2381 else {
2383 float3x3(task.transform),
2384 all_custom_normals.slice(dst_point_range));
2385 }
2386 }
2387
2388 /* Copy curve offsets. */
2389 const Span<int> src_offsets = curves.offsets();
2390 const MutableSpan<int> dst_offsets = dst_curves.offsets_for_write().slice(dst_curve_range);
2391 threading::parallel_for(curves.curves_range(), 2048, [&](const IndexRange range) {
2392 for (const int i : range) {
2393 dst_offsets[i] = task.start_indices.point + src_offsets[i];
2394 }
2395 });
2396
2397 dst_curves.nurbs_custom_knots_for_write()
2398 .slice(dst_custom_knot_range)
2399 .copy_from(curves.nurbs_custom_knots());
2400
2401 if (!all_dst_ids.is_empty()) {
2402 create_result_ids(
2403 options, curves_info.stored_ids, task.id, all_dst_ids.slice(dst_point_range));
2404 }
2405
2406 if (!all_dst_fill_ids.is_empty()) {
2407 MutableSpan<int> dst_fill_ids = all_dst_fill_ids.slice(dst_curve_range);
2408 const VArray<int> &src_fill_ids = curves_info.stored_fill_ids;
2409 const VectorSet<int> &index_by_fill_id = curves_info.index_by_fill_id;
2410
2411 if (src_fill_ids.is_empty() || index_by_fill_id.is_empty()) {
2412 dst_fill_ids.fill(0);
2413 }
2414 else {
2415 threading::parallel_for(curves.curves_range(), 2048, [&](const IndexRange range) {
2416 for (const int i : range) {
2417 const int src_fill_id = src_fill_ids[i];
2418 if (src_fill_id == 0) {
2419 dst_fill_ids[i] = 0;
2420 }
2421 else {
2422 /* Use the index of the source fill ID to create new IDs. */
2423 const int fill_id_index = index_by_fill_id.index_of(src_fill_id);
2424 /* Valid fill IDs must not be 0, so start at 1. */
2425 dst_fill_ids[i] = task.start_indices.fill_id + fill_id_index + 1;
2426 }
2427 }
2428 });
2429 }
2430 }
2431
2433 curves_info.attributes,
2434 task.attribute_fallbacks,
2435 ordered_attributes,
2436 [&](const bke::AttrDomain domain) {
2437 switch (domain) {
2438 case bke::AttrDomain::Point:
2439 return IndexRange(task.start_indices.point, curves.points_num());
2440 case bke::AttrDomain::Curve:
2441 return IndexRange(task.start_indices.curve, curves.curves_num());
2442 default:
2443 BLI_assert_unreachable();
2444 return IndexRange();
2445 }
2446 },
2447 dst_attribute_writers);
2448}
2449
2451 const OrderedAttributes &ordered_attributes,
2452 const Span<const Curves *> src_curves)
2453{
2454 Set<StringRef> existing_names;
2455 for (const bDeformGroup &defgroup : dst_curve.vertex_group_names) {
2456 existing_names.add(defgroup.name);
2457 }
2458 for (const Curves *src_curve : src_curves) {
2459 for (const bDeformGroup &src : src_curve->geometry.vertex_group_names) {
2460 if (!existing_names.add(src.name)) {
2461 continue;
2462 }
2463 copy_vertex_group_name(&dst_curve.vertex_group_names, ordered_attributes, src);
2464 existing_names.add(src.name);
2465 }
2466 }
2467}
2468
2470 const GatherOffsets &offsets,
2471 const AllCurvesInfo &all_curves_info,
2472 const Span<RealizeCurveTask> tasks,
2473 const OrderedAttributes &ordered_attributes,
2474 RealizeInstancesResult &r_result)
2475{
2476 if (tasks.is_empty()) {
2477 return;
2478 }
2479
2481 if (tasks.size() == 1) {
2482 const RealizeCurveTask &task = tasks.first();
2483 Curves *new_curves = BKE_curves_copy_for_eval(task.curve_info->curves);
2484 if (!skip_transform(task.transform)) {
2485 new_curves->geometry.wrap().transform(task.transform);
2486 }
2489 new_curves->geometry.wrap().attributes_for_write());
2490 r_result.geometry.replace_curves(new_curves);
2491 return;
2492 }
2493
2494 const int64_t points_num = offsets.curves_offsets.point;
2495 const int64_t curves_num = offsets.curves_offsets.curve;
2496 const int64_t custom_knot_num = offsets.curves_offsets.custom_knot;
2497
2498 if (!valid_int_num(points_num) || !valid_int_num(curves_num) || !valid_int_num(custom_knot_num))
2499 {
2500 r_result.errors.append(RPT_("Realized curves data has too many elements."));
2501 return;
2502 }
2503
2504 /* Allocate new curves data-block. */
2505 Curves *dst_curves_id = bke::curves_new_nomain(points_num, curves_num);
2506 bke::CurvesGeometry &dst_curves = dst_curves_id->geometry.wrap();
2507 if (custom_knot_num) {
2508 dst_curves.nurbs_custom_knots_resize(custom_knot_num);
2509 }
2510 dst_curves.offsets_for_write().last() = points_num;
2511 r_result.geometry.replace_curves(dst_curves_id);
2512 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
2513
2514 /* Copy settings from the first input geometry set with curves. */
2515 const RealizeCurveTask &first_task = tasks.first();
2516 const Curves &first_curves_id = *first_task.curve_info->curves;
2517 bke::curves_copy_parameters(first_curves_id, *dst_curves_id);
2518
2519 copy_vertex_group_names(dst_curves, ordered_attributes, all_curves_info.order);
2520
2521 /* Prepare id attribute. */
2522 SpanAttributeWriter<int> point_ids;
2523 if (all_curves_info.create_id_attribute) {
2524 point_ids = dst_attributes.lookup_or_add_for_write_only_span<int>("id",
2526 }
2527
2528 /* Prepare fill id attribute. */
2529 SpanAttributeWriter<int> fill_ids;
2530 if (all_curves_info.create_fill_id_attribute) {
2531 fill_ids = dst_attributes.lookup_or_add_for_write_only_span<int>("fill_id",
2533 }
2534
2535 /* Prepare generic output attributes. */
2536 Vector<GSpanAttributeWriter> dst_attribute_writers;
2537 for (const int attribute_index : ordered_attributes.index_range()) {
2538 const StringRef name = ordered_attributes.names[attribute_index];
2539 const bke::AttrDomain domain = ordered_attributes.kinds[attribute_index].domain;
2540 const bke::AttrType data_type = ordered_attributes.kinds[attribute_index].data_type;
2542 tasks.size(),
2543 ordered_attributes,
2544 [&](const int task_index) -> TaskAttrInfo {
2545 return {.attributes = tasks[task_index].curve_info->attributes,
2546 .fallbacks = tasks[task_index].attribute_fallbacks};
2547 },
2548 attribute_index,
2549 dst_attributes))
2550 {
2551 dst_attribute_writers.append({});
2552 continue;
2553 }
2554 dst_attribute_writers.append(
2555 dst_attributes.lookup_or_add_for_write_only_span(name, domain, data_type));
2556 }
2557
2558 /* Prepare handle position attributes if necessary. */
2559 SpanAttributeWriter<float3> handle_left;
2560 SpanAttributeWriter<float3> handle_right;
2561 if (all_curves_info.create_handle_postion_attributes) {
2562 handle_left = dst_attributes.lookup_or_add_for_write_only_span<float3>("handle_left",
2564 handle_right = dst_attributes.lookup_or_add_for_write_only_span<float3>(
2565 "handle_right", bke::AttrDomain::Point);
2566 }
2567
2569 if (all_curves_info.create_radius_attribute) {
2570 radius = dst_attributes.lookup_or_add_for_write_only_span<float>("radius",
2571 bke::AttrDomain::Point);
2572 }
2573 SpanAttributeWriter<float3> custom_normal;
2574 if (all_curves_info.create_custom_normal_attribute) {
2575 custom_normal = dst_attributes.lookup_or_add_for_write_only_span<float3>(
2576 "custom_normal", bke::AttrDomain::Point);
2577 }
2578
2579 /* Actually execute all tasks. */
2580 threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
2581 for (const int task_index : task_range) {
2582 const RealizeCurveTask &task = tasks[task_index];
2583 execute_realize_curve_task(options,
2584 all_curves_info,
2585 task,
2586 ordered_attributes,
2587 dst_curves,
2588 dst_attribute_writers,
2589 point_ids.span,
2590 fill_ids.span,
2591 handle_left.span,
2592 handle_right.span,
2593 radius.span,
2594 custom_normal.span);
2595 }
2596 });
2597
2598 /* Type counts have to be updated eagerly. */
2599 dst_curves.runtime->type_counts.fill(0);
2600 for (const RealizeCurveTask &task : tasks) {
2601 for (const int i : IndexRange(CURVE_TYPES_NUM)) {
2602 dst_curves.runtime->type_counts[i] +=
2603 task.curve_info->curves->geometry.runtime->type_counts[i];
2604 }
2605 }
2606
2607 /* Tag modified attributes. */
2608 for (GSpanAttributeWriter &dst_attribute : dst_attribute_writers) {
2609 dst_attribute.finish();
2610 }
2611 point_ids.finish();
2612 fill_ids.finish();
2613 radius.finish();
2614 handle_left.finish();
2615 handle_right.finish();
2616 custom_normal.finish();
2617}
2618
2620
2621/* -------------------------------------------------------------------- */
2624
2626 const bke::GeometrySet &in_geometry_set,
2628 const VariedDepthOptions &varied_depth_options)
2629{
2631 in_geometry_set, bke::GeometryComponent::Type::GreasePencil, options, varied_depth_options);
2632 OrderedAttributes ordered_attributes;
2633 for (const int i : attributes_to_propagate.names.index_range()) {
2634 ordered_attributes.names.add_new(attributes_to_propagate.names[i]);
2635 ordered_attributes.kinds.append(attributes_to_propagate.kinds[i]);
2636 }
2637 return ordered_attributes;
2638}
2639
2641 VectorSet<const GreasePencil *> &r_grease_pencils)
2642{
2643 if (const GreasePencil *grease_pencil = geometry_set.get_grease_pencil()) {
2644 if (!grease_pencil->layers().is_empty()) {
2645 r_grease_pencils.add(grease_pencil);
2646 }
2647 }
2648 if (const Instances *instances = geometry_set.get_instances()) {
2649 instances->foreach_referenced_geometry([&](const bke::GeometrySet &instance_geometry_set) {
2650 gather_grease_pencils_to_realize(instance_geometry_set, r_grease_pencils);
2651 });
2652 }
2653}
2654
2656 const bke::GeometrySet &geometry_set,
2658 const VariedDepthOptions &varied_depth_options)
2659{
2663 geometry_set, options, varied_depth_options);
2664
2665 gather_grease_pencils_to_realize(geometry_set, info.order);
2666 info.realize_info.reinitialize(info.order.size());
2667 for (const int grease_pencil_index : info.realize_info.index_range()) {
2668 GreasePencilRealizeInfo &grease_pencil_info = info.realize_info[grease_pencil_index];
2669 const GreasePencil *grease_pencil = info.order[grease_pencil_index];
2670 grease_pencil_info.grease_pencil = grease_pencil;
2671
2672 bke::AttributeAccessor attributes = grease_pencil->attributes();
2673 grease_pencil_info.attributes.reinitialize(info.attributes.size());
2674 for (const int attribute_index : info.attributes.index_range()) {
2675 const StringRef name = info.attributes.names[attribute_index];
2676 const bke::AttrType data_type = info.attributes.kinds[attribute_index].data_type;
2677 const bke::AttrDomain domain = info.attributes.kinds[attribute_index].domain;
2678 if (attributes.contains(name)) {
2679 GVArray attribute = *attributes.lookup_or_default(name, domain, data_type);
2680 grease_pencil_info.attributes[attribute_index].emplace(std::move(attribute));
2681 }
2682 }
2683
2684 grease_pencil_info.material_index_map.reinitialize(grease_pencil->material_array_num);
2685 for (const int i : IndexRange(grease_pencil->material_array_num)) {
2686 Material *material = grease_pencil->material_array[i];
2687 grease_pencil_info.material_index_map[i] = info.materials.index_of_or_add(material);
2688 }
2689 }
2690 return info;
2691}
2692
2694 const RealizeGreasePencilTask &task,
2695 const OrderedAttributes &ordered_attributes,
2696 GreasePencil &dst_grease_pencil,
2697 MutableSpan<GSpanAttributeWriter> dst_attribute_writers)
2698{
2699 const GreasePencilRealizeInfo &grease_pencil_info = *task.grease_pencil_info;
2700 const GreasePencil &src_grease_pencil = *grease_pencil_info.grease_pencil;
2701 const Span<const bke::greasepencil::Layer *> src_layers = src_grease_pencil.layers();
2702 const IndexRange dst_layers_slice{task.start_index, src_layers.size()};
2703 const Span<bke::greasepencil::Layer *> dst_layers = dst_grease_pencil.layers_for_write().slice(
2704 dst_layers_slice);
2705
2706 for (const int layer_i : src_layers.index_range()) {
2707 const bke::greasepencil::Layer &src_layer = *src_layers[layer_i];
2708 bke::greasepencil::Layer &dst_layer = *dst_layers[layer_i];
2709 BKE_grease_pencil_copy_layer_parameters(src_layer, dst_layer);
2710
2711 dst_layer.set_name(src_layer.name());
2712 dst_layer.set_local_transform(task.transform * src_layer.local_transform());
2713
2714 const bke::greasepencil::Drawing *src_drawing = src_grease_pencil.get_eval_drawing(src_layer);
2715 if (!src_drawing) {
2716 continue;
2717 }
2718 bke::greasepencil::Drawing &dst_drawing = *dst_grease_pencil.get_eval_drawing(dst_layer);
2719
2720 const bke::CurvesGeometry &src_curves = src_drawing->strokes();
2721 bke::CurvesGeometry &dst_curves = dst_drawing.strokes_for_write();
2722 dst_curves = src_curves;
2723
2724 /* Remap materials. */
2725 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
2726 bke::SpanAttributeWriter<int> material_indices =
2727 dst_attributes.lookup_or_add_for_write_span<int>("material_index", bke::AttrDomain::Curve);
2728 for (int &material_index : material_indices.span) {
2729 if (material_index >= 0 && material_index < src_grease_pencil.material_array_num) {
2730 material_index = grease_pencil_info.material_index_map[material_index];
2731 }
2732 }
2733 material_indices.finish();
2734 }
2735
2737 grease_pencil_info.attributes,
2739 ordered_attributes,
2740 [&](const bke::AttrDomain domain) {
2741 BLI_assert(domain == bke::AttrDomain::Layer);
2742 UNUSED_VARS_NDEBUG(domain);
2743 return dst_layers_slice;
2744 },
2745 dst_attribute_writers);
2746}
2747
2749 const float4x4 &transform)
2750{
2751 for (bke::greasepencil::Layer *layer : layers) {
2752 layer->set_local_transform(transform * layer->local_transform());
2753 }
2754}
2755
2757 const AllGreasePencilsInfo &all_grease_pencils_info,
2758 const GatherOffsets &offsets,
2760 const OrderedAttributes &ordered_attributes,
2761 RealizeInstancesResult &r_result)
2762{
2763 if (tasks.is_empty()) {
2764 return;
2765 }
2766
2768 if (tasks.size() == 1) {
2769 const RealizeGreasePencilTask &task = tasks.first();
2771 if (!skip_transform(task.transform)) {
2772 transform_grease_pencil_layers(new_gp->layers_for_write(), task.transform);
2773 }
2775 ordered_attributes, task.attribute_fallbacks, new_gp->attributes_for_write());
2776 r_result.geometry.replace_grease_pencil(new_gp);
2777 return;
2778 }
2779
2780 const int64_t new_layers_num = offsets.grease_pencil_layer_offset;
2781 if (!valid_int_num(new_layers_num)) {
2782 r_result.errors.append(RPT_("Realized grease pencil has too many layers."));
2783 return;
2784 }
2785
2786 /* Allocate new grease pencil. */
2787 GreasePencil *dst_grease_pencil = BKE_grease_pencil_new_nomain();
2788 BKE_grease_pencil_copy_parameters(*tasks.first().grease_pencil_info->grease_pencil,
2789 *dst_grease_pencil);
2790 r_result.geometry.replace_grease_pencil(dst_grease_pencil);
2791
2792 /* Allocate all layers. */
2793 dst_grease_pencil->add_layers_with_empty_drawings_for_eval(new_layers_num);
2794
2795 /* Transfer material pointers. The material indices are updated for each task separately. */
2796 if (!all_grease_pencils_info.materials.is_empty()) {
2797 MEM_SAFE_DELETE(dst_grease_pencil->material_array);
2798 dst_grease_pencil->material_array_num = all_grease_pencils_info.materials.size();
2799 dst_grease_pencil->material_array = MEM_new_array_zeroed<Material *>(
2800 dst_grease_pencil->material_array_num, __func__);
2801 uninitialized_copy_n(all_grease_pencils_info.materials.data(),
2802 dst_grease_pencil->material_array_num,
2803 dst_grease_pencil->material_array);
2804 }
2805
2806 /* Prepare generic output attributes. */
2807 bke::MutableAttributeAccessor dst_attributes = dst_grease_pencil->attributes_for_write();
2808 Vector<GSpanAttributeWriter> dst_attribute_writers;
2809 for (const int attribute_index : ordered_attributes.index_range()) {
2810 const StringRef name = ordered_attributes.names[attribute_index];
2811 const bke::AttrType data_type = ordered_attributes.kinds[attribute_index].data_type;
2813 tasks.size(),
2814 ordered_attributes,
2815 [&](const int task_index) -> TaskAttrInfo {
2816 return {.attributes = tasks[task_index].grease_pencil_info->attributes,
2817 .fallbacks = tasks[task_index].attribute_fallbacks};
2818 },
2819 attribute_index,
2820 dst_attributes))
2821 {
2822 dst_attribute_writers.append({});
2823 continue;
2824 }
2825 dst_attribute_writers.append(
2826 dst_attributes.lookup_or_add_for_write_only_span(name, bke::AttrDomain::Layer, data_type));
2827 }
2828
2829 /* Actually execute all tasks. */
2830 threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
2831 for (const int task_index : task_range) {
2832 const RealizeGreasePencilTask &task = tasks[task_index];
2833 execute_realize_grease_pencil_task(
2834 task, ordered_attributes, *dst_grease_pencil, dst_attribute_writers);
2835 }
2836 });
2837
2838 /* Tag modified attributes. */
2839 for (GSpanAttributeWriter &dst_attribute : dst_attribute_writers) {
2840 dst_attribute.finish();
2841 }
2842}
2843
2845
2846/* -------------------------------------------------------------------- */
2849
2851 bke::GeometrySet &r_realized_geometry)
2852{
2853 if (tasks.is_empty()) {
2854 return;
2855 }
2856
2858 auto &component = r_realized_geometry.get_component_for_write<bke::GeometryComponentEditData>();
2859 for (const RealizeEditDataTask &task : tasks) {
2860 if (!component.curves_edit_hints_) {
2861 if (task.edit_data->curves_edit_hints_) {
2862 component.curves_edit_hints_ = std::make_unique<bke::CurvesEditHints>(
2863 *task.edit_data->curves_edit_hints_);
2864 }
2865 }
2866 if (const bke::GizmoEditHints *src_gizmo_edit_hints = task.edit_data->gizmo_edit_hints_.get())
2867 {
2868 if (!component.gizmo_edit_hints_) {
2869 component.gizmo_edit_hints_ = std::make_unique<bke::GizmoEditHints>();
2870 }
2871 for (auto item : src_gizmo_edit_hints->gizmo_transforms.items()) {
2872 component.gizmo_edit_hints_->gizmo_transforms.add(item.key, task.transform * item.value);
2873 }
2874 }
2875 }
2876}
2877
2879
2880/* -------------------------------------------------------------------- */
2883
2885{
2886 Instances *instances = geometry_set.get_instances_for_write();
2887 if (!instances) {
2888 return;
2889 }
2890 instances->attributes_for_write().remove("id");
2891 instances->ensure_geometry_instances();
2892 for (bke::InstanceReference &reference : instances->references_for_write()) {
2893 if (reference.type() == bke::InstanceReference::Type::GeometrySet) {
2895 }
2896 }
2897}
2898
2903static void propagate_instances_to_keep(const bke::GeometrySet &geometry_set,
2904 const IndexMask &selection,
2905 bke::GeometrySet &new_geometry_set,
2906 const bke::AttributeFilter &attribute_filter)
2907{
2908 const Instances &instances = *geometry_set.get_instances();
2909 IndexMaskMemory inverse_selection_indices;
2910 const IndexMask inverse_selection = selection.complement(IndexRange(instances.instances_num()),
2911 inverse_selection_indices);
2912 /* Check not all instances are being realized. */
2913 if (inverse_selection.is_empty()) {
2914 return;
2915 }
2916
2917 std::unique_ptr<Instances> new_instances = std::make_unique<Instances>(instances);
2918 new_instances->remove(inverse_selection, attribute_filter);
2919
2920 bke::InstancesComponent &new_instances_components =
2922 new_instances_components.replace(new_instances.release(), bke::GeometryOwnershipType::Owned);
2923}
2924
2927{
2928 if (!geometry_set.has_instances()) {
2929 return {geometry_set};
2930 }
2931
2932 VariedDepthOptions all_instances;
2934 geometry_set.get_instances()->instances_num());
2935 all_instances.selection = IndexMask(geometry_set.get_instances()->instances_num());
2936 return realize_instances(geometry_set, options, all_instances);
2937}
2938
2941 const VariedDepthOptions &varied_depth_option)
2942{
2944 /* The algorithm works in three steps:
2945 * 1. Preprocess each unique geometry that is instanced (e.g. each `Mesh`).
2946 * 2. Gather "tasks" that need to be executed to realize the instances. Each task corresponds
2947 * to instances of the previously preprocessed geometry.
2948 * 3. Execute all tasks in parallel.
2949 */
2950
2951 if (!geometry_set.has_instances()) {
2952 return {geometry_set};
2953 }
2954
2955 bke::GeometrySet not_to_realize_set;
2957 geometry_set, varied_depth_option.selection, not_to_realize_set, options.attribute_filter);
2958
2959 if (options.keep_original_ids) {
2961 }
2962
2963 AllPointCloudsInfo all_pointclouds_info = preprocess_pointclouds(
2964 geometry_set, options, varied_depth_option);
2965 AllMeshesInfo all_meshes_info = preprocess_meshes(geometry_set, options, varied_depth_option);
2966 AllCurvesInfo all_curves_info = preprocess_curves(geometry_set, options, varied_depth_option);
2967 AllGreasePencilsInfo all_grease_pencils_info = preprocess_grease_pencils(
2968 geometry_set, options, varied_depth_option);
2970 geometry_set, options, varied_depth_option);
2971
2972 const bool create_id_attribute = all_pointclouds_info.create_id_attribute ||
2973 all_meshes_info.create_id_attribute ||
2974 all_curves_info.create_id_attribute;
2975 ResourceScope temporary_arrays;
2976 GatherTasksInfo gather_info = {all_pointclouds_info,
2977 all_meshes_info,
2978 all_curves_info,
2979 all_grease_pencils_info,
2980 all_instance_attributes,
2981 create_id_attribute,
2982 varied_depth_option.selection,
2983 varied_depth_option.depths,
2984 temporary_arrays};
2985
2986 if (not_to_realize_set.has_instances()) {
2987 gather_info.instances.instances_components_to_merge.append(
2988 not_to_realize_set.get_component_for_write<bke::InstancesComponent>().copy());
2990 gather_info.instances.attribute_fallback.append(gather_info.instances_attriubutes.size());
2991 }
2992
2994 InstanceContext attribute_fallbacks(gather_info);
2995
2996 initialize_curves_builtin_attribute_defaults(all_curves_info, attribute_fallbacks);
2997
2998 {
2999 PRF_scope_with_name("gather_realize_tasks_recursive", ProfileCategory::Default);
3001 0,
3003 geometry_set,
3004 transform,
3005 attribute_fallbacks);
3006 }
3007
3011 all_instance_attributes,
3012 gather_info.instances.attribute_fallback,
3013 result.geometry);
3014
3015 const int64_t total_points_num = get_final_points_num(gather_info.r_tasks);
3016 /* This doesn't have to be exact at all, it's just a rough estimate to make decisions about
3017 * multi-threading (overhead). */
3018 const int64_t approximate_used_bytes_num = total_points_num * 32;
3019 threading::memory_bandwidth_bound_task(approximate_used_bytes_num, [&]() {
3021 gather_info.r_offsets,
3022 all_pointclouds_info,
3023 gather_info.r_tasks.pointcloud_tasks,
3024 all_pointclouds_info.attributes,
3025 result);
3027 gather_info.r_offsets,
3028 all_meshes_info,
3029 gather_info.r_tasks.mesh_tasks,
3030 all_meshes_info.attributes,
3031 all_meshes_info.materials,
3032 result);
3034 gather_info.r_offsets,
3035 all_curves_info,
3036 gather_info.r_tasks.curve_tasks,
3037 all_curves_info.attributes,
3038 result);
3039 execute_realize_grease_pencil_tasks(all_grease_pencils_info,
3040 gather_info.r_offsets,
3041 gather_info.r_tasks.grease_pencil_tasks,
3042 all_grease_pencils_info.attributes,
3043 result);
3045 });
3046 if (gather_info.r_tasks.first_volume) {
3047 result.geometry.add(*gather_info.r_tasks.first_volume);
3048 }
3049 for (const nodes::Bundle *bundle : gather_info.r_tasks.bundles) {
3050 result.geometry.bundle_for_write().merge(*bundle);
3051 }
3052
3053 return result;
3054}
3055
3057
3058} // namespace blender::geometry
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
General operations for point clouds.
#define INT32_MAX
#define RPT_(msgid)
#define CURVE_TYPES_NUM
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
#define PRF_scope_with_name(ui_name, category)
#define PRF_scope(category)
#define BLI_assert_unreachable()
#define ELEM(...)
btScalar max
Span< T > as_span() const
Definition BLI_array.hh:254
IndexRange index_range() const
Definition BLI_array.hh:371
const T & first() const
Definition BLI_array.hh:292
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_equal(const void *a, const void *b) const
bool is() const
const void * default_value() const
void default_construct(void *ptr) const
GMutableSpan slice(const int64_t start, int64_t size) const
const CPPType & type() const
const CPPType * type() const
const void * get() const
Span< T > typed() const
const CPPType & type() const
constexpr IndexRange()=default
constexpr bool contains(int64_t value) const
constexpr IndexRange drop_front(int64_t n) const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
T & construct(Args &&...args)
void * allocate_owned(const CPPType &type)
bool add_as(ForwardKey &&key)
Definition BLI_set.hh:256
bool add(const Key &key)
Definition BLI_set.hh:248
void ensure(FunctionRef< void(T &data)> compute_cache)
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
std::optional< T > get_if_single() const
IndexRange index_range() const
void materialize(MutableSpan< T > r_span) const
Span< T > get_internal_span() const
static VArray from_single(T value, const int64_t size)
static VArray from_span(Span< T > values)
bool add(const Key &key)
int64_t size() const
int64_t size() const
void append(const T &value)
IndexRange index_range() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
GPointer get_builtin_default(const StringRef name) const
bool is_builtin(const StringRef name) const
GAttributeReader get() const
MutableSpan< float3 > positions_for_write()
AttributeAccessor attributes() const
MutableSpan< int > offsets_for_write()
void nurbs_custom_knots_resize(int knots_num)
MutableAttributeAccessor attributes_for_write()
void convert_to_uninitialized(const CPPType &from_type, const CPPType &to_type, const void *from_value, void *to_value) const
void convert_to_initialized_n(GSpan from_span, GMutableSpan to_span) const
bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
int attribute_domain_size(AttrDomain domain) const
void to_geometry_set(GeometrySet &r_geometry_set) const
Definition instances.cc:115
GeometryComponentPtr copy() const override
void replace(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Span< int > reference_handles() const
Definition instances.cc:222
Span< float4x4 > transforms() const
Definition instances.cc:235
Span< InstanceReference > references() const
Definition instances.cc:290
bke::MutableAttributeAccessor attributes_for_write()
Definition instances.cc:73
bke::AttributeAccessor attributes() const
Definition instances.cc:68
MutableSpan< InstanceReference > references_for_write()
Definition instances.cc:295
int instances_num() const
Definition instances.cc:408
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void set_local_transform(const float4x4 &transform)
std::optional< bke::AttrDomain > result_domain
Definition BKE_mesh.hh:263
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
CCL_NAMESPACE_BEGIN struct Options options
#define instance_id
#define attribute(slot)
#define layer
VecBase< short, 2 > short2
uint to_type(bool)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
bool is_corner_fan_normals(const AttributeMetaData &meta_data)
void curves_copy_parameters(const Curves &src, Curves &dst)
ImplicitSharingPtr< GeometryComponent > GeometryComponentPtr
const DataTypeConversions & get_implicit_type_conversions()
bool attribute_is_builtin_on_component_type(const GeometryComponent::Type type, StringRef name)
void mesh_flip_faces(Mesh &mesh, const IndexMask &selection)
void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
Curves * curves_new_nomain(int points_num, int curves_num)
static void propagate_instances_to_keep(const bke::GeometrySet &geometry_set, const IndexMask &selection, bke::GeometrySet &new_geometry_set, const bke::AttributeFilter &attribute_filter)
static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info, const int current_depth, const int target_depth, const bke::GeometrySet &geometry_set, const float4x4 &base_transform, const InstanceContext &base_instance_context)
static Vector< AttrFallbackData > prepare_attribute_fallbacks(GatherTasksInfo &gather_info, const Instances &instances, const OrderedAttributes &ordered_attributes)
static OrderedAttributes gather_generic_pointcloud_attributes_to_propagate(const bke::GeometrySet &in_geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option, bool &r_create_radii, bool &r_create_id)
static void execute_realize_curve_tasks(const RealizeInstancesOptions &options, const GatherOffsets &offsets, const AllCurvesInfo &all_curves_info, const Span< RealizeCurveTask > tasks, const OrderedAttributes &ordered_attributes, RealizeInstancesResult &r_result)
static std::optional< int > calc_single_value_material_index(const AllMeshesInfo &all_meshes_info)
static void execute_realize_curve_task(const RealizeInstancesOptions &options, const AllCurvesInfo &all_curves_info, const RealizeCurveTask &task, const OrderedAttributes &ordered_attributes, bke::CurvesGeometry &dst_curves, MutableSpan< GSpanAttributeWriter > dst_attribute_writers, MutableSpan< int > all_dst_ids, MutableSpan< int > all_dst_fill_ids, MutableSpan< float3 > all_handle_left, MutableSpan< float3 > all_handle_right, MutableSpan< float > all_radii, MutableSpan< float3 > all_custom_normals)
static void execute_realize_grease_pencil_tasks(const AllGreasePencilsInfo &all_grease_pencils_info, const GatherOffsets &offsets, const Span< RealizeGreasePencilTask > tasks, const OrderedAttributes &ordered_attributes, RealizeInstancesResult &r_result)
static void execute_realize_mesh_tasks(const RealizeInstancesOptions &options, const GatherOffsets &offsets, const AllMeshesInfo &all_meshes_info, const Span< RealizeMeshTask > tasks, const OrderedAttributes &ordered_attributes, const VectorSet< Material * > &ordered_materials, RealizeInstancesResult &r_result)
static AllMeshesInfo preprocess_meshes(const bke::GeometrySet &geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option)
static void gather_grease_pencils_to_realize(const bke::GeometrySet &geometry_set, VectorSet< const GreasePencil * > &r_grease_pencils)
static OrderedAttributes gather_generic_curve_attributes_to_propagate(const bke::GeometrySet &in_geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option, bool &r_create_id, bool &r_create_fill_id)
static void execute_realize_pointcloud_task(const RealizeInstancesOptions &options, const RealizePointCloudTask &task, const OrderedAttributes &ordered_attributes, MutableSpan< GSpanAttributeWriter > dst_attribute_writers, MutableSpan< float > all_dst_radii, MutableSpan< int > all_dst_ids, MutableSpan< float3 > all_dst_positions)
static int64_t get_final_points_num(const GatherTasks &tasks)
static void gather_pointclouds_to_realize(const bke::GeometrySet &geometry_set, VectorSet< const PointCloud * > &r_pointclouds)
static std::optional< int > calc_material_index_max_hint(const Span< MeshRealizeInfo > infos)
static void execute_realize_grease_pencil_task(const RealizeGreasePencilTask &task, const OrderedAttributes &ordered_attributes, GreasePencil &dst_grease_pencil, MutableSpan< GSpanAttributeWriter > dst_attribute_writers)
static void copy_generic_attributes_to_result(const Span< std::optional< GVArray > > src_attributes, const AttributeFallbacksArray &attribute_fallbacks, const OrderedAttributes &ordered_attributes, const FunctionRef< IndexRange(bke::AttrDomain)> &range_fn, MutableSpan< GSpanAttributeWriter > dst_attribute_writers)
static VectorSet< int > gather_all_fill_ids(const VArray< int > &fill_ids)
static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info, const int current_depth, const int target_depth, const Instances &instances, const float4x4 &base_transform, const InstanceContext &base_instance_context)
static void remove_id_attribute_from_instances(bke::GeometrySet &geometry_set)
static void initialize_curves_builtin_attribute_defaults(const AllCurvesInfo &all_curves_info, InstanceContext &attribute_fallbacks)
static void gather_curves_to_realize(const bke::GeometrySet &geometry_set, VectorSet< const Curves * > &r_curves)
static void transform_grease_pencil_layers(Span< bke::greasepencil::Layer * > layers, const float4x4 &transform)
static bool valid_int_num(const int64_t num)
static bool try_join_single_value_attribute(const int tasks_num, const OrderedAttributes &ordered_attributes, const FunctionRef< TaskAttrInfo(int)> get_task_attributes, const int attr_index, bke::MutableAttributeAccessor dst_attributes)
static void copy_vertex_group_name(ListBaseT< bDeformGroup > *dst_deform_group, const OrderedAttributes &ordered_attributes, const bDeformGroup &src_deform_group)
static void gather_attribute_propagation_components_with_custom_depths(const bke::GeometrySet &geometry, const bke::GeometryComponent::Type component_type, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option, Set< bke::GeometryComponentPtr > &r_components)
static AllPointCloudsInfo preprocess_pointclouds(const bke::GeometrySet &geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option)
static int get_mapped_material_index(const MeshRealizeInfo &info, const int index)
static bke::GeometrySet::GatheredAttributes gather_attributes_to_propagate(const bke::GeometrySet &geometry, const bke::GeometryComponent::Type component_type, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option)
static void create_result_ids(const RealizeInstancesOptions &options, const VArray< int > &stored_ids, const int task_id, MutableSpan< int > dst_ids)
static void copy_vertex_group_names(Mesh &dst_mesh, const OrderedAttributes &ordered_attributes, const Span< const Mesh * > src_meshes)
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
static void execute_realize_pointcloud_tasks(const RealizeInstancesOptions &options, const GatherOffsets &offsets, const AllPointCloudsInfo &all_pointclouds_info, const Span< RealizePointCloudTask > tasks, const OrderedAttributes &ordered_attributes, RealizeInstancesResult &r_result)
static AllCurvesInfo preprocess_curves(const bke::GeometrySet &geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option)
static void gather_meshes_to_realize(const bke::GeometrySet &geometry_set, VectorSet< const Mesh * > &r_meshes)
static void add_instance_attributes_to_single_geometry(const OrderedAttributes &ordered_attributes, const AttributeFallbacksArray &attribute_fallbacks, bke::MutableAttributeAccessor attributes)
static void execute_instances_tasks(const Span< bke::GeometryComponentPtr > src_components, const Span< float4x4 > src_base_transforms, const OrderedAttributes &all_instances_attributes, const Span< geometry::AttributeFallbacksArray > attribute_fallback, bke::GeometrySet &r_realized_geometry)
static AllGreasePencilsInfo preprocess_grease_pencils(const bke::GeometrySet &geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_options)
static OrderedAttributes gather_generic_instance_attributes_to_propagate(const bke::GeometrySet &in_geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option)
static void execute_realize_mesh_task(const RealizeInstancesOptions &options, const RealizeMeshTask &task, const OrderedAttributes &ordered_attributes, MutableSpan< GSpanAttributeWriter > dst_attribute_writers, MutableSpan< float3 > all_dst_positions, MutableSpan< int2 > all_dst_edges, MutableSpan< int > all_dst_face_offsets, MutableSpan< int > all_dst_corner_verts, MutableSpan< int > all_dst_corner_edges, MutableSpan< int > all_dst_vert_ids, GSpanAttributeWriter &all_dst_custom_normals, MutableSpan< int > all_dst_origindex_vert, MutableSpan< int > all_dst_origindex_edge, MutableSpan< int > all_dst_origindex_face)
static void foreach_geometry_in_reference(const InstanceReference &reference, const float4x4 &base_transform, const uint32_t id, FunctionRef< void(const bke::GeometrySet &geometry_set, const float4x4 &transform, uint32_t id)> fn)
static void threaded_fill(const GPointer value, GMutableSpan dst)
static void execute_realize_edit_data_tasks(const Span< RealizeEditDataTask > tasks, bke::GeometrySet &r_realized_geometry)
static bool skip_transform(const float4x4 &transform)
static OrderedAttributes gather_generic_grease_pencil_attributes_to_propagate(const bke::GeometrySet &in_geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_options)
static void threaded_copy(const GVArray &src, GMutableSpan dst)
static void gather_attribute_propagation_components(const bke::GeometrySet &geometry, const bke::GeometryComponent::Type component_type, const RealizeInstancesOptions &options, const int current_depth, const std::optional< int > max_depth, Set< bke::GeometryComponentPtr > &r_components)
static void join_mesh_material_indices(const AllMeshesInfo &all_meshes_info, const Span< RealizeMeshTask > tasks, Mesh &dst_mesh)
static OrderedAttributes gather_generic_mesh_attributes_to_propagate(const bke::GeometrySet &in_geometry_set, const RealizeInstancesOptions &options, const VariedDepthOptions &varied_depth_option, bool &r_create_id, bool &r_create_material_index)
bool is_negative(const MatBase< T, 3, 3 > &mat)
void transform_normals(const float3x3 &transform, MutableSpan< float3 > normals)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
uint32_t hash(uint32_t kx)
Definition noise.cc:89
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
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
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
PointCloud * BKE_pointcloud_copy_for_eval(const PointCloud *pointcloud_src)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_id_material_eval_assign(ID *id, int slot, Material *material)
__int64 int64_t
Definition BKE_text.h:90
GreasePencil * BKE_grease_pencil_copy_for_eval(const GreasePencil *grease_pencil_src)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
struct Curves * BKE_curves_copy_for_eval(const struct Curves *curves_src)
MatBase< float, 4, 4 > float4x4
void BKE_grease_pencil_copy_layer_parameters(const bke::greasepencil::Layer &src, bke::greasepencil::Layer &dst)
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
void BKE_grease_pencil_copy_parameters(const GreasePencil &src, GreasePencil &dst)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const size_t num
MatBase< float, 3, 3 > float3x3
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
VecBase< int16_t, 2 > short2
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
VecBase< float, 3 > float3
GreasePencil * BKE_grease_pencil_new_nomain()
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
ListBaseT< struct bDeformGroup > vertex_group_names
ListBaseT< bDeformGroup > vertex_group_names
bke::MeshRuntime * runtime
int vertex_group_active_index
CustomData face_data
CustomData edge_data
CustomData vert_data
struct Material ** mat
bool allow_skip(const StringRef name) const
void add(const StringRef name, const AttributeDomainAndType &kind)
Vector< AttributeDomainAndType, 16 > kinds
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
Instances * get_instances_for_write()
const GreasePencil * get_grease_pencil() const
Vector< const GeometryComponent * > get_components() const
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const nodes::Bundle * bundle() const
const Curves * get_curves() const
const Instances * get_instances() const
void replace_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void replace_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
SharedCache< std::optional< int > > max_material_index
VectorSet< const Curves * > order
Array< RealizeCurveInfo > realize_info
VectorSet< const GreasePencil * > order
Array< GreasePencilRealizeInfo > realize_info
Vector< AttributeFallbacksArray > attribute_fallback
Vector< bke::GeometryComponentPtr > instances_components_to_merge
VectorSet< const Mesh * > order
bke::mesh::NormalJoinInfo custom_normal_info
Array< MeshRealizeInfo > realize_info
Array< PointCloudRealizeInfo > realize_info
VectorSet< const PointCloud * > order
std::variant< GSpan, GPointer > data
struct blender::geometry::GatherOffsets::@126330241025251350205004126146067057057341077305 curves_offsets
struct blender::geometry::GatherOffsets::@312066275037000240171062267334372124230202046235 mesh_offsets
const AllPointCloudsInfo & pointclouds
const OrderedAttributes & instances_attriubutes
const AllGreasePencilsInfo & grease_pencils
Vector< RealizeMeshTask > mesh_tasks
Vector< RealizeEditDataTask > edit_data_tasks
Vector< RealizePointCloudTask > pointcloud_tasks
Vector< RealizeCurveTask > curve_tasks
ImplicitSharingPtr< const bke::VolumeComponent > first_volume
Vector< RealizeGreasePencilTask > grease_pencil_tasks
VectorSet< const nodes::Bundle * > bundles
Array< std::optional< GVArray > > attributes
InstanceContext(const GatherTasksInfo &gather_info)
Array< std::optional< GVArray > > attributes
Vector< AttributeDomainAndType > kinds
Array< std::optional< GVArray > > attributes
Array< std::optional< GVArray > > attributes
AttributeFallbacksArray attribute_fallbacks
CurvesElementStartIndices start_indices
const bke::GeometryComponentEditData * edit_data
const GreasePencilRealizeInfo * grease_pencil_info
AttributeFallbacksArray attribute_fallbacks
const PointCloudRealizeInfo * pointcloud_info
const AttributeFallbacksArray & fallbacks
Span< std::optional< GVArray > > attributes