Blender V5.3
attribute.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/attribute.h"
6#include "scene/hair.h"
7#include "scene/image.h"
8#include "scene/mesh.h"
9#include "scene/pointcloud.h"
10
12#include "util/log.h"
13#include "util/string.h"
14#include "util/transform.h"
15
17
18/* Attribute */
19
21 const TypeDesc type,
23 Geometry *geom,
26{
27 /* String is not supported! */
28 assert(type == TypeFloat || type == TypeColor || type == TypePoint || type == TypeVector ||
29 type == TypeNormal || type == TypeMatrix || type == TypeFloat2 || type == TypeFloat4 ||
30 type == TypeRGBA || type == TypeQuaternion || type == TypePackedSphericalHarmonicsRest);
31
33 auto *data = GuardedAllocator<ImageHandle>().allocate(1);
34 new (data) ImageHandle();
37 }
38 else {
39 resize(geom, prim);
40 }
41}
42
44 const TypeDesc type,
46 const void *data,
47 const int size,
48 ImplicitSharingInfo sharing_info)
49 : name(name),
51 type(type),
52 size(size),
54 flags(0),
55 modified(true)
56{
59 /* Implicit sharing function pointers should be set if shared attributes are created. */
63 center.sharing_info = sharing_info;
64}
65
66static size_t attribute_alloc_bytes(const size_t element_size, const size_t size)
67{
68 /* rtcSetSharedGeometryBuffer is documented as requiring 4 bytes past the
69 * end of a float3 for 16-byte SSE loads, so we add that for all attributes. */
70 static constexpr size_t ATTRIBUTE_BUFFER_PADDING = 4;
71 return element_size * size + ATTRIBUTE_BUFFER_PADDING;
72}
73
75 const AttributeElement element,
76 const size_t data_sizeof,
77 const size_t size)
78{
79 if (element & ATTR_ELEMENT_VOXEL) {
80 if (buf.data) {
81 auto *image = static_cast<ImageHandle *>(const_cast<void *>(buf.data));
82 image->~ImageHandle();
83 GuardedAllocator<ImageHandle>().deallocate(image, 1);
84 }
85 }
86 else if (buf.sharing_info) {
88 }
89 else if (buf.data) {
90 GuardedAllocator<char>().deallocate(static_cast<char *>(const_cast<void *>(buf.data)),
91 attribute_alloc_bytes(data_sizeof, size));
92 }
93 buf.data = nullptr;
94 buf.sharing_info = nullptr;
95}
96
98 : name(other.name),
99 std(other.std),
100 type(other.type),
101 element(other.element),
102 modified(other.modified)
103{
104 set_data_from(std::move(other));
105}
106
108{
109 const size_t element_size = data_sizeof();
111 for (Buffer &buf : motion) {
113 }
114 motion.clear();
115}
116
121
123{
124 if (!(element & ATTR_ELEMENT_VOXEL)) {
126 }
127}
128
129void Attribute::resize(const size_t num_elements)
130{
132 return;
133 }
134 if (num_elements == size_t(size)) {
135 return;
136 }
137 const size_t element_size = data_sizeof();
138 const size_t copy_elems = std::min(num_elements, size_t(size));
139 const size_t alloc_bytes = attribute_alloc_bytes(element_size, num_elements);
140
141 /* Allocate and copy center step. */
142 Buffer new_center;
143 new_center.data = GuardedAllocator<char>().allocate(alloc_bytes);
144 if (center.data) {
145 memcpy(const_cast<void *>(new_center.data), center.data, copy_elems * element_size);
146 }
147
148 /* Allocate and copy motion steps. */
149 vector<Buffer> new_motion(motion.size());
150 for (size_t i = 0; i < motion.size(); i++) {
151 new_motion[i].data = GuardedAllocator<char>().allocate(alloc_bytes);
152 if (motion[i].data) {
153 memcpy(const_cast<void *>(new_motion[i].data), motion[i].data, copy_elems * element_size);
154 }
155 }
156
157 free_data();
158 center = new_center;
159 motion = std::move(new_motion);
160 size = num_elements;
161}
162
164{
165 const int motion_steps = geom->get_motion_steps();
166 if (motion_steps <= 0) {
167 return;
168 }
169
170 const int motion_size = geom->get_motion_steps() - 1;
171 if (motion_size == motion.size()) {
172 return;
173 }
174 const size_t element_size = data_sizeof();
175
176 if (motion_size < motion.size()) {
177 for (size_t i = motion_size; i < motion.size(); i++) {
179 }
180 motion.resize(motion_size);
181 }
182 else {
183 motion.reserve(motion_size);
184 while (motion.size() < motion_size) {
185 Buffer buf;
186 if (size > 0) {
187 /* Left uninitialized, callers fill in the motion data for every step. */
189 }
190 motion.push_back(buf);
191 }
192 }
193
194 modified = true;
195}
196
198{
199 if (!has_motion()) {
200 return;
201 }
202 const size_t element_size = data_sizeof();
203 for (Buffer &buf : motion) {
205 }
206 motion.clear();
207 modified = true;
208}
209
211 const void *data,
212 const int new_size,
213 ImplicitSharingInfo sharing_info)
214{
215 assert(step >= 1 && size_t(step - 1) < motion.size());
216 assert(new_size == size);
217 (void)new_size;
218
219 Buffer &buf = motion[step - 1];
221
222 buf.data = data;
224 g_implicit_sharing_user_add_fn(sharing_info);
225 buf.sharing_info = sharing_info;
226
227 modified = true;
228}
229
231{
232 assert(other.type == type && other.element == element && other.size == size);
234 motion = std::move(other.motion);
235 other.motion.clear();
236 other.modified = true;
237 modified = true;
238}
239
240static char *buffer_for_write(Attribute::Buffer &buf, const size_t element_size, const size_t size)
241{
242 if (!buf.data) {
243 return nullptr;
244 }
245 if (buf.sharing_info) {
246 /* Here we assume that the sharing info is not mutable. With the addition of another sharing
247 * info callback function pointer we could check the user count to avoid unnecessary copies.
248 * For now that isn't expected to happen in practice though. */
249 auto *new_data = GuardedAllocator<char>().allocate(attribute_alloc_bytes(element_size, size));
250 memcpy(new_data, buf.data, element_size * size);
252 buf.sharing_info = nullptr;
253 buf.data = new_data;
254 }
255 return const_cast<char *>(reinterpret_cast<const char *>(buf.data));
256}
257
258char *Attribute::data_for_write_buffer(const int step)
259{
260 if (step == 0) {
261 if (!center.data) {
262 assert(size == 0);
263 return nullptr;
264 }
266 }
267 assert(step >= 1 && step <= int(motion.size()));
269}
270
272{
273 assert(other.std == std);
274 assert(other.type == type);
275 assert(other.element == element);
276
277 flags = other.flags;
278
279 const size_t element_size = data_sizeof();
280
281 /* If topology or motion steps differ, take all data. */
282 if (size != other.size || motion.size() != other.motion.size()) {
283 free_data();
284 center = other.center;
285 motion = std::move(other.motion);
286 size = other.size;
287 other.center = Buffer();
288 other.size = 0;
289 modified = true;
290 return;
291 }
292
293 /* Compare each step independently. */
294 const auto take_step = [&](Buffer &dst, Buffer &src, const AttributeElement step_element) {
295 free_step_buffer(dst, step_element, element_size, size);
296 dst = src;
297 src = Buffer();
298 modified = true;
299 };
300
301 const auto step_equals = [&](const Buffer &a, const Buffer &b) {
302 if (a.data == b.data) {
303 /* Same buffer, e.g. shared through implicit sharing. */
304 return true;
305 }
306 if (a.sharing_info != b.sharing_info) {
307 return false;
308 }
309 if (size == 0) {
310 return true;
311 }
312 return memcmp(a.data, b.data, element_size * size) == 0;
313 };
314
315 if (!step_equals(center, other.center)) {
316 take_step(center, other.center, element);
317 }
318 for (size_t i = 0; i < motion.size(); i++) {
319 if (!step_equals(motion[i], other.motion[i])) {
320 take_step(motion[i], other.motion[i], element);
321 }
322 }
323}
324
326{
328 return sizeof(ImageHandle);
329 }
331 return sizeof(uchar4);
332 }
334 return sizeof(packed_normal);
335 }
336 if (type == TypeFloat) {
337 return sizeof(float);
338 }
339 if (type == TypeFloat2) {
340 return sizeof(float2);
341 }
342 if (type == TypeMatrix) {
343 return sizeof(Transform);
344 // The float3 type is not interchangeable with float4
345 // as it is now a packed type.
346 }
347 if (type == TypeFloat4) {
348 return sizeof(float4);
349 }
350 if (type == TypeRGBA) {
351 return sizeof(float4);
352 }
353 if (type == TypeQuaternion) {
354 return sizeof(Quaternion);
355 }
356 if (type == TypePackedSphericalHarmonicsRest) {
357 return sizeof(PackedSphericalHarmonicsRest);
358 }
359 return sizeof(packed_float3);
360}
361
365{
366 size_t size = 0;
367
368 switch (element) {
372 size = 1;
373 break;
376 if (geom->is_mesh() || geom->is_volume()) {
377 Mesh *mesh = static_cast<Mesh *>(geom);
378 if (prim == ATTR_PRIM_SUBD) {
380 }
381 else {
382 size = mesh->num_verts();
383 }
384 }
385 else if (geom->is_pointcloud()) {
386 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
387 size = pointcloud->num_points();
388 }
389 break;
390
392 if (geom->is_mesh() || geom->is_volume()) {
393 Mesh *mesh = static_cast<Mesh *>(geom);
394 if (prim == ATTR_PRIM_SUBD) {
395 size = mesh->get_num_subd_faces();
396 }
397 else {
398 size = mesh->num_triangles();
399 }
400 }
401 break;
405 if (geom->is_mesh()) {
406 Mesh *mesh = static_cast<Mesh *>(geom);
407 if (prim == ATTR_PRIM_SUBD) {
408 size = mesh->get_subd_face_corners().size();
409 }
410 else {
411 size = mesh->num_triangles() * 3;
412 }
413 }
414 break;
416 if (geom->is_hair()) {
417 Hair *hair = static_cast<Hair *>(geom);
418 size = hair->num_curves();
419 }
420 break;
423 if (geom->is_hair()) {
424 Hair *hair = static_cast<Hair *>(geom);
425 size = hair->num_keys();
426 }
427 break;
428 default:
429 break;
430 }
431
432 return size;
433}
434
436{
437 /* Size of a single step buffer, as returned by data() for one step. */
438 return Attribute::element_size(geom, element, prim) * data_sizeof();
439}
440
441bool Attribute::same_storage(const TypeDesc a, const TypeDesc b)
442{
443 if (a == b) {
444 return true;
445 }
446
447 if (a == TypeColor || a == TypePoint || a == TypeVector || a == TypeNormal) {
448 if (b == TypeColor || b == TypePoint || b == TypeVector || b == TypeNormal) {
449 return true;
450 }
451 }
452 return false;
453}
454
455void Attribute::zero_data(void *dst)
456{
457 if (type == TypePackedSphericalHarmonicsRest) {
459 return;
460 }
461
462 memset(dst, 0, data_sizeof());
463}
464
466{
467 switch (std) {
469 return "P";
470 case ATTR_STD_RADIUS:
471 return "radius";
474 return "N";
475 case ATTR_STD_UV:
476 return "uv";
478 return "generated";
480 return "generated_transform";
482 return "tangent";
484 return "tangent_sign";
486 return "undisplaced_tangent";
488 return "undisplaced_tangent_sign";
490 return "vertex_color";
492 return "undeformed";
494 return "undisplaced";
496 return "undisplaced_N";
498 return "particle";
500 return "curve_intercept";
502 return "curve_length";
504 return "curve_random";
506 return "point_random";
508 return "ptex_face_id";
509 case ATTR_STD_PTEX_UV:
510 return "ptex_uv";
512 return "density";
514 return "color";
516 return "flame";
518 return "heat";
520 return "temperature";
522 return "velocity";
524 return "velocity_x";
526 return "velocity_y";
528 return "velocity_z";
530 return "pointiness";
532 return "random_per_island";
534 return "shadow_transparency";
536 return "radiance:base";
538 return "radiance:sh";
540 return "radiance";
542 return "scale";
544 return "rotation";
546 case ATTR_STD_NONE:
547 case ATTR_STD_NUM:
548 return "";
549 }
550
551 return "";
552}
553
555{
556 if (!name.empty()) {
557 for (int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++) {
559 return (AttributeStandard)std;
560 }
561 }
562 }
563
564 return ATTR_STD_NONE;
565}
566
589
591{
592 return ustring("geom:" + string(Attribute::standard_name(std)));
593}
594
595ustring Attribute::osl_name(ustring name)
596{
598 return (std != ATTR_STD_NONE) ? osl_name(std) : name;
599}
600
602{
603 if (attr.element & ATTR_ELEMENT_IS_BYTE) {
605 }
606
607 if (attr.element & ATTR_ELEMENT_IS_NORMAL) {
609 }
610
611 if (attr.type == TypeFloat) {
613 }
614
615 if (attr.type == TypeFloat2) {
617 }
618
619 if (attr.type == TypeFloat4 || attr.type == TypeRGBA || attr.type == TypeMatrix) {
621 }
622
623 if (attr.type == TypeQuaternion) {
625 }
626
627 if (attr.type == TypePackedSphericalHarmonicsRest) {
629 }
630
632}
633
636 unordered_set<int> &tiles) const
637{
638 if (type != TypeFloat2) {
639 return;
640 }
641
642 const int num = Attribute::element_size(geom, element, prim);
643 const float2 *uv = data<float2>();
644 for (int i = 0; i < num; i++, uv++) {
645 const float u = uv->x;
646 const float v = uv->y;
647 int x = (int)u;
648 int y = (int)v;
649
650 if (x < 0 || y < 0 || x >= 10) {
651 continue;
652 }
653
654 /* Be conservative in corners - precisely touching the right or upper edge of a tile
655 * should not load its right/upper neighbor as well. */
656 if (x > 0 && (u < x + 1e-6f)) {
657 x--;
658 }
659 if (y > 0 && (v < y + 1e-6f)) {
660 y--;
661 }
662
663 tiles.insert(1001 + 10 * y + x);
664 }
665}
666
667/* Attribute Set */
668
673
675
676Attribute *AttributeSet::add(ustring name, const TypeDesc type, AttributeElement element)
677{
678 Attribute *attr = find(name);
679
680 if (attr) {
681 /* return if same already exists */
682 if (attr->type == type && attr->element == element) {
683 return attr;
684 }
685
686 /* overwrite attribute with same name but different type/element */
687 remove(name);
688 }
689
690 attributes.emplace_back(name, type, element, geometry, prim);
691 tag_modified(attributes.back());
692 return &attributes.back();
693}
694
696 const TypeDesc type,
697 AttributeElement element,
698 const void *data,
699 const int size,
700 ImplicitSharingInfo sharing_info)
701{
702 Attribute *attr = find(name);
703
704 if (attr) {
705 /* overwrite attribute with same name but different type/element */
706 remove(name);
707 }
708
709 attributes.emplace_back(name, type, element, data, size, sharing_info);
710 tag_modified(attributes.back());
711 return &attributes.back();
712}
713
715{
716 Attribute *attr = find(other.name);
717 if (attr) {
718 if (attr->type == other.type && attr->element == other.element) {
719 attr->std = other.std;
720 attr->set_data_from(std::move(other));
721 return attr;
722 }
723
724 /* Overwrite attribute with the same name but different type/element. */
725 remove(other.name);
726 }
727
728 attributes.emplace_back(std::move(other));
729 tag_modified(attributes.back());
730 return &attributes.back();
731}
732
733Attribute *AttributeSet::find(ustring name) const
734{
735 for (const Attribute &attr : attributes) {
736 if (attr.name == name) {
737 return (Attribute *)&attr;
738 }
739 }
740
741 return nullptr;
742}
743
744void AttributeSet::remove(ustring name)
745{
746 Attribute *attr = find(name);
747
748 if (attr) {
749 list<Attribute>::iterator it;
750
751 for (it = attributes.begin(); it != attributes.end(); it++) {
752 if (&*it == attr) {
753 remove(it);
754 return;
755 }
756 }
757 }
758}
759
760/* Type and domain that a standard attribute is required to have on the given
761 * geometry. Returns false if the geometry type has no such standard attribute. */
762static bool standard_type_element(const Geometry *geometry,
763 const AttributeStandard std,
764 TypeDesc &type,
765 AttributeElement &element)
766{
767 if (geometry->is_mesh()) {
768 switch (std) {
773 type = TypePoint;
774 element = ATTR_ELEMENT_VERTEX;
775 return true;
778 type = TypeNormal;
780 return true;
781 case ATTR_STD_UV:
782 case ATTR_STD_PTEX_UV:
783 type = TypeFloat2;
784 element = ATTR_ELEMENT_CORNER;
785 return true;
788 type = TypeVector;
789 element = ATTR_ELEMENT_CORNER;
790 return true;
793 type = TypeFloat;
794 element = ATTR_ELEMENT_CORNER;
795 return true;
797 type = TypeRGBA;
798 element = ATTR_ELEMENT_CORNER_BYTE;
799 return true;
801 type = TypeNormal;
803 return true;
806 type = TypeFloat;
807 element = ATTR_ELEMENT_FACE;
808 return true;
810 type = TypeMatrix;
811 element = ATTR_ELEMENT_MESH;
812 return true;
814 type = TypeFloat;
815 element = ATTR_ELEMENT_VERTEX;
816 return true;
817 default:
818 return false;
819 }
820 }
821 else if (geometry->is_pointcloud()) {
822 switch (std) {
825 type = TypePoint;
826 element = ATTR_ELEMENT_VERTEX;
827 return true;
828 case ATTR_STD_RADIUS:
830 type = TypeFloat;
831 element = ATTR_ELEMENT_VERTEX;
832 return true;
833 case ATTR_STD_UV:
834 type = TypeFloat2;
835 element = ATTR_ELEMENT_VERTEX;
836 return true;
838 type = TypeMatrix;
839 element = ATTR_ELEMENT_MESH;
840 return true;
842 type = TypeFloat4;
843 element = ATTR_ELEMENT_VERTEX;
844 return true;
846 type = TypePackedSphericalHarmonicsRest;
847 element = ATTR_ELEMENT_VERTEX;
848 return true;
850 type = TypeRGBA;
851 element = ATTR_ELEMENT_VERTEX;
852 return true;
854 type = TypeVector;
855 element = ATTR_ELEMENT_VERTEX;
856 return true;
858 type = TypeQuaternion;
859 element = ATTR_ELEMENT_VERTEX;
860 return true;
861 default:
862 return false;
863 }
864 }
865 else if (geometry->is_volume()) {
866 switch (std) {
868 type = TypePoint;
869 element = ATTR_ELEMENT_VERTEX;
870 return true;
872 type = TypeNormal;
874 return true;
876 type = TypeNormal;
878 return true;
886 type = TypeFloat;
887 element = ATTR_ELEMENT_VOXEL;
888 return true;
890 type = TypeColor;
891 element = ATTR_ELEMENT_VOXEL;
892 return true;
894 type = TypeVector;
895 element = ATTR_ELEMENT_VOXEL;
896 return true;
898 type = TypeMatrix;
899 element = ATTR_ELEMENT_MESH;
900 return true;
901 default:
902 return false;
903 }
904 }
905 else if (geometry->is_hair()) {
906 switch (std) {
908 type = TypePoint;
909 element = ATTR_ELEMENT_CURVE_KEY;
910 return true;
911 case ATTR_STD_RADIUS:
913 type = TypeFloat;
914 element = ATTR_ELEMENT_CURVE_KEY;
915 return true;
917 type = TypeNormal;
919 return true;
920 case ATTR_STD_UV:
921 type = TypeFloat2;
922 element = ATTR_ELEMENT_CURVE;
923 return true;
925 type = TypePoint;
926 element = ATTR_ELEMENT_CURVE;
927 return true;
930 type = TypeFloat;
931 element = ATTR_ELEMENT_CURVE;
932 return true;
934 type = TypeMatrix;
935 element = ATTR_ELEMENT_MESH;
936 return true;
938 type = TypeFloat;
939 element = ATTR_ELEMENT_VERTEX;
940 return true;
942 type = TypeFloat;
943 element = ATTR_ELEMENT_FACE;
944 return true;
946 /* Stored as float but uses RGBE encoding. */
947 type = TypeFloat;
948 element = ATTR_ELEMENT_CURVE_KEY;
949 return true;
950 default:
951 return false;
952 }
953 }
954 return false;
955}
956
958{
959 TypeDesc std_type;
960 AttributeElement std_element;
961 if (!standard_type_element(geometry, std, std_type, std_element)) {
962 return false;
963 }
964
965 return type == std_type && element == std_element;
966}
967
969{
970 Attribute *attr = nullptr;
971
972 if (name.empty()) {
973 name = Attribute::standard_name(std);
974 }
975
976 TypeDesc type = TypeFloat;
978 const bool supported = standard_type_element(geometry, std, type, element);
979 assert(supported);
980 (void)supported;
981
982 attr = add(name, type, element);
983
984 attr->std = std;
985
986 return attr;
987}
988
990 ustring name,
991 const void *data,
992 const int size,
993 ImplicitSharingInfo sharing_info)
994{
995 Attribute *attr = nullptr;
996
997 if (name.empty()) {
998 name = Attribute::standard_name(std);
999 }
1000
1001 TypeDesc type = TypeFloat;
1003 const bool supported = standard_type_element(geometry, std, type, element);
1004 assert(supported);
1005 (void)supported;
1006
1007 attr = add_shared(name, type, element, data, size, sharing_info);
1008
1009 attr->std = std;
1010
1011 return attr;
1012}
1013
1015{
1016 Attribute &copy_attr = *add(attr.name, attr.type, attr.element);
1017 copy_attr.std = attr.std;
1018 if (attr.has_motion()) {
1019 copy_attr.add_motion(geometry);
1020 }
1021 return copy_attr;
1022}
1023
1025{
1026 for (const Attribute &attr : attributes) {
1027 if (attr.std == std) {
1028 return (Attribute *)&attr;
1029 }
1030 }
1031
1032 return nullptr;
1033}
1034
1036{
1037 for (Attribute &attr : attributes) {
1038 if (attr.name != other.name) {
1039 continue;
1040 }
1041 if (attr.std != other.std) {
1042 continue;
1043 }
1044 if (attr.type != other.type) {
1045 continue;
1046 }
1047 if (attr.element != other.element) {
1048 continue;
1049 }
1050 return &attr;
1051 }
1052 return nullptr;
1053}
1054
1056{
1057 Attribute *attr = find(std);
1058
1059 if (attr) {
1060 list<Attribute>::iterator it;
1061
1062 for (it = attributes.begin(); it != attributes.end(); it++) {
1063 if (&*it == attr) {
1064 remove(it);
1065 return;
1066 }
1067 }
1068 }
1069}
1070
1072{
1073 if (!req.name.empty()) {
1074 /* Name has priority if requested. */
1075 Attribute *attr = find(req.name);
1076 if (attr) {
1077 return attr;
1078 }
1079 }
1080
1081 if (req.std != ATTR_STD_NONE) {
1082 /* Next try standard attribute. */
1083 Attribute *attr = find(req.std);
1084 if (attr) {
1085 return attr;
1086 }
1087
1088 if (req.name.empty()) {
1089 /* Attribute not marked standard with matching name, data type and
1090 * domain is accepted as well. */
1091 attr = find(ustring(Attribute::standard_name(req.std)));
1092 if (attr && attr->matches_standard(geometry, req.std)) {
1093 return attr;
1094 }
1095 }
1096 }
1097
1098 return nullptr;
1099}
1100
1102{
1103 if (attribute->std == ATTR_STD_NONE) {
1104 remove(attribute->name);
1105 }
1106 else {
1107 remove(attribute->std);
1108 }
1109}
1110
1111void AttributeSet::remove(list<Attribute>::iterator it)
1112{
1113 tag_modified(*it);
1114 attributes.erase(it);
1115}
1116
1118{
1119 for (Attribute &attr : attributes) {
1120 attr.resize(geometry, prim);
1121 }
1122}
1123
1124void AttributeSet::clear(bool preserve_voxel_data)
1125{
1126 if (preserve_voxel_data) {
1127 list<Attribute>::iterator it;
1128
1129 for (it = attributes.begin(); it != attributes.end();) {
1130 if ((it->element & ATTR_ELEMENT_VOXEL) || it->std == ATTR_STD_GENERATED_TRANSFORM) {
1131 it++;
1132 }
1133 else {
1134 attributes.erase(it++);
1135 }
1136 }
1137 }
1138 else {
1139 attributes.clear();
1140 }
1141}
1142
1144{
1145 /* Remove any attributes not on new_attributes. */
1146 list<Attribute>::iterator it;
1147 for (it = attributes.begin(); it != attributes.end();) {
1148 const Attribute &old_attr = *it;
1149 if (new_attributes.find_matching(old_attr) == nullptr) {
1150 remove(it++);
1151 continue;
1152 }
1153 it++;
1154 }
1155
1156 /* Add or update old_attributes based on the new_attributes. */
1157 for (Attribute &attr : new_attributes.attributes) {
1158 const Attribute *new_attr = add_from(std::move(attr));
1159
1160 /* Tag geometry as modified so BVH updates when attributes affecting it change. */
1161 if (new_attr->modified &&
1162 (new_attr->std == ATTR_STD_POSITION || new_attr->std == ATTR_STD_RADIUS))
1163 {
1165 }
1166 }
1167
1168 /* If all attributes were replaced, transform is no longer applied. */
1169 geometry->transform_applied = false;
1170}
1171
1173{
1174 for (Attribute &attr : attributes) {
1175 attr.modified = false;
1176 }
1177
1178 modified_flag = 0;
1179}
1180
1181void AttributeSet::tag_modified(const Attribute &attr)
1182{
1183 const AttrKernelDataType kernel_type = Attribute::kernel_type(attr);
1184 modified_flag |= (1u << uint(kernel_type));
1185}
1186
1187bool AttributeSet::modified(const AttrKernelDataType kernel_type) const
1188{
1189 return (modified_flag & (1u << uint(kernel_type))) != 0;
1190}
1191
1192/* AttributeRequest */
1193
1195{
1196 name = name_;
1198
1199 type = TypeFloat;
1201 desc.offset = 0;
1203}
1204
1206{
1207 name = ustring();
1208 std = std_;
1209
1210 type = TypeFloat;
1212 desc.offset = 0;
1214}
1215
1217{
1218 name = name_;
1219 std = std_;
1220
1221 type = TypeFloat;
1223 desc.offset = 0;
1225}
1226
1227/* AttributeRequestSet */
1228
1230
1232
1234{
1235 if (requests.size() != other.requests.size()) {
1236 return true;
1237 }
1238
1239 for (size_t i = 0; i < requests.size(); i++) {
1240 bool found = false;
1241
1242 for (size_t j = 0; j < requests.size() && !found; j++) {
1243 if (requests[i].name == other.requests[j].name && requests[i].std == other.requests[j].std) {
1244 found = true;
1245 }
1246 }
1247
1248 if (!found) {
1249 return true;
1250 }
1251 }
1252
1253 return false;
1254}
1255
1257{
1258 for (const AttributeRequest &req : requests) {
1259 if (req.name == name && req.std == ATTR_STD_NONE) {
1260 return;
1261 }
1262 }
1263
1264 requests.push_back(AttributeRequest(name));
1265}
1266
1268{
1269 for (AttributeRequest &req : requests) {
1270 if (req.std == std) {
1271 /* Clear the name so that std is preferred if available, and name
1272 * is only used as a last resort in #AttributeSet::find. */
1273 req.name = ustring();
1274 return;
1275 }
1276 }
1277
1278 requests.push_back(AttributeRequest(std));
1279}
1280
1282{
1283 for (const AttributeRequest &req : reqs.requests) {
1284 if (req.name.empty()) {
1285 add(req.std);
1286 }
1287 else if (req.std == ATTR_STD_NONE) {
1288 add(req.name);
1289 }
1290 else {
1292 }
1293 }
1294}
1295
1297{
1298 if (name.empty()) {
1299 return;
1300 }
1301
1303
1304 if (std == ATTR_STD_NONE) {
1305 add(name);
1306 }
1307 else if (!find(std)) {
1308 requests.push_back(AttributeRequest(name, std));
1309 }
1310
1311 /* Request the UV map that a tangent is computed from. */
1312 if (std == ATTR_STD_UV_TANGENT || std == ATTR_STD_UV_TANGENT_SIGN ||
1314 {
1316 }
1317 else {
1318 const char *suffixes[] = {
1319 ".tangent_sign", ".tangent", ".undisplaced_tangent", ".undisplaced_tangent_sign"};
1320 for (const char *suffix : suffixes) {
1321 if (string_endswith(name, suffix)) {
1322 add(name.substr(0, name.size() - strlen(suffix)));
1323 }
1324 }
1325 }
1326}
1327
1328bool AttributeRequestSet::find(const ustring name) const
1329{
1330 for (const AttributeRequest &req : requests) {
1331 if (req.name == name) {
1332 return true;
1333 }
1334 }
1335
1336 return false;
1337}
1338
1340{
1341 for (const AttributeRequest &req : requests) {
1342 if (req.std == std) {
1343 return true;
1344 }
1345 }
1346
1347 return false;
1348}
1349
1351{
1352 return requests.size();
1353}
1354
1356{
1357 requests.clear();
1358}
1359
static size_t attribute_alloc_bytes(const size_t element_size, const size_t size)
Definition attribute.cpp:66
static bool standard_type_element(const Geometry *geometry, const AttributeStandard std, TypeDesc &type, AttributeElement &element)
static char * buffer_for_write(Attribute::Buffer &buf, const size_t element_size, const size_t size)
static void free_step_buffer(Attribute::Buffer &buf, const AttributeElement element, const size_t data_sizeof, const size_t size)
Definition attribute.cpp:74
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
vector< AttributeRequest > requests
void add(ustring name)
size_t size() const
bool find(ustring name) const
void add_name_or_standard(ustring name)
bool modified(const AttributeRequestSet &other) const
AttributeRequest(ustring name_)
AttributeDescriptor desc
AttributeStandard std
Geometry * geometry
void update(AttributeSet &&new_attributes)
Attribute & copy(const Attribute &attr)
list< Attribute > attributes
Attribute * find(ustring name) const
Attribute * find_matching(const Attribute &other)
bool modified(AttrKernelDataType kernel_type) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void remove(ustring name)
void clear_modified()
AttributeSet(Geometry *geometry, AttributePrimitive prim)
void clear(bool preserve_voxel_data=false)
Attribute * add_shared(ustring name, const TypeDesc type, AttributeElement element, const void *data, int size, ImplicitSharingInfo sharing_info)
AttributePrimitive prim
Attribute * add_from(Attribute &&other)
static AttributeStandard name_standard(ustring name)
AttributeElement element
Attribute(ustring name, const TypeDesc type, AttributeElement element, Geometry *geom, AttributePrimitive prim)
Definition attribute.cpp:20
vector< Buffer > motion
void take_motion_from(Attribute &other)
const T * data(const int step=0) const
void resize(Geometry *geom, AttributePrimitive prim)
static AttrKernelDataType kernel_type(const Attribute &attr)
void set_motion_step_shared(int step, const void *data, int new_size, ImplicitSharingInfo sharing_info)
static size_t element_size(Geometry *geom, AttributeElement element, AttributePrimitive prim)
static ustring osl_name(AttributeStandard std)
bool has_motion() const
TypeDesc type
void free_data()
static AttributeStandard name_volume_standard(ustring name)
void add_motion(const Geometry *geom)
void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set< int > &tiles) const
void remove_motion()
void set_data_from(Attribute &&other)
void zero_data(void *dst)
static const char * standard_name(AttributeStandard std)
size_t buffer_size(Geometry *geom, AttributePrimitive prim) const
AttributeStandard std
size_t data_sizeof() const
static bool same_storage(const TypeDesc a, const TypeDesc b)
bool matches_standard(const Geometry *geometry, AttributeStandard std) const
bool transform_applied
bool is_volume() const
bool is_pointcloud() const
bool is_hair() const
bool is_mesh() const
T * allocate(const size_t n, const void *hint=nullptr)
GuardedAllocator()=default
void deallocate(T *p, const size_t n)
Definition hair.h:13
size_t num_curves() const
Definition hair.h:109
size_t num_keys() const
Definition hair.h:103
size_t get_num_subd_faces() const
Definition scene/mesh.h:221
size_t get_num_subd_base_verts() const
Definition scene/mesh.h:229
NODE_DECLARE size_t num_verts() const
size_t num_triangles() const
Definition scene/mesh.h:73
size_t num_points() const
#define CCL_NAMESPACE_END
#define assert(assertion)
#define image(slot, qualifiers, format)
#define attribute(slot)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
const void * ImplicitSharingInfo
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
@ NODE_ATTR_FLOAT
AttributeElement
@ ATTR_ELEMENT_NONE
@ ATTR_ELEMENT_VERTEX_NORMAL
@ ATTR_ELEMENT_CORNER_BYTE
@ ATTR_ELEMENT_CURVE_KEY
@ ATTR_ELEMENT_CORNER_NORMAL
@ ATTR_ELEMENT_VOXEL
@ ATTR_ELEMENT_CORNER
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_VERTEX
@ ATTR_ELEMENT_IS_BYTE
@ ATTR_ELEMENT_CURVE_KEY_NORMAL
@ ATTR_ELEMENT_IS_NORMAL
@ ATTR_ELEMENT_FACE
@ ATTR_ELEMENT_MESH
@ ATTR_ELEMENT_CURVE
AttributePrimitive
@ ATTR_PRIM_SUBD
AttributeStandard
@ ATTR_STD_CURVE_INTERCEPT
@ ATTR_STD_GENERATED_TRANSFORM
@ ATTR_STD_UV
@ ATTR_STD_GSPLAT_SCALE
@ ATTR_STD_VOLUME_VELOCITY_Y
@ ATTR_STD_VOLUME_TEMPERATURE
@ ATTR_STD_VERTEX_NORMAL
@ ATTR_STD_UV_TANGENT
@ ATTR_STD_NOT_FOUND
@ ATTR_STD_NONE
@ ATTR_STD_VOLUME_VELOCITY_Z
@ ATTR_STD_POINT_RANDOM
@ ATTR_STD_VERTEX_COLOR
@ ATTR_STD_VOLUME_DENSITY
@ ATTR_STD_NUM
@ ATTR_STD_POSITION_UNDISPLACED
@ ATTR_STD_VOLUME_FLAME
@ ATTR_STD_PTEX_FACE_ID
@ ATTR_STD_GSPLAT_RADIANCE_BASE
@ ATTR_STD_VOLUME_VELOCITY
@ ATTR_STD_POSITION_UNDEFORMED
@ ATTR_STD_VOLUME_COLOR
@ ATTR_STD_VOLUME_HEAT
@ ATTR_STD_NORMAL_UNDISPLACED
@ ATTR_STD_UV_TANGENT_SIGN
@ ATTR_STD_CURVE_RANDOM
@ ATTR_STD_GSPLAT_ROTATION
@ ATTR_STD_UV_TANGENT_SIGN_UNDISPLACED
@ ATTR_STD_SHADOW_TRANSPARENCY
@ ATTR_STD_POSITION
@ ATTR_STD_CORNER_NORMAL
@ ATTR_STD_UV_TANGENT_UNDISPLACED
@ ATTR_STD_GSPLAT_RADIANCE
@ ATTR_STD_GSPLAT_RADIANCE_SPHERICAL_HARMONICS_REST
@ ATTR_STD_PTEX_UV
@ ATTR_STD_POINTINESS
@ ATTR_STD_GENERATED
@ ATTR_STD_RADIUS
@ ATTR_STD_CURVE_LENGTH
@ ATTR_STD_VOLUME_VELOCITY_X
@ ATTR_STD_RANDOM_PER_ISLAND
@ ATTR_STD_PARTICLE
AttrKernelDataType
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bool string_endswith(const string_view s, const string_view end)
Definition string.cpp:122
AttributeElement element
NodeAttributeType type
ImplicitSharingInfo sharing_info
void tag_modified()
float x
float y
ccl_device_forceinline void spherical_harmonics_rest_fill_zero(ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest)
ImplicitSharingUserAddFn g_implicit_sharing_user_remove_fn
CCL_NAMESPACE_BEGIN ImplicitSharingUserAddFn g_implicit_sharing_user_add_fn