Blender V5.3
customdata.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <algorithm>
13#include <cstddef>
14
15#include "MEM_guardedalloc.h"
16
17/* Since we have versioning code here (CustomData_verify_versions()). */
18#define DNA_DEPRECATED_ALLOW
19
20#include "DNA_ID.h"
22#include "DNA_meshdata_types.h"
23#include "DNA_modifier_enums.h"
24#include "DNA_userdef_types.h"
25
26#include "BLI_bit_vector.hh"
27#include "BLI_bitmap.hh"
28#include "BLI_index_range.hh"
31#include "BLI_math_vector.hh"
32#include "BLI_memory_counter.hh"
33#include "BLI_mempool.hh"
34#include "BLI_path_utils.hh"
35#include "BLI_resource_scope.hh"
36#include "BLI_set.hh"
37#include "BLI_span.hh"
38#include "BLI_string.hh"
39#include "BLI_string_ref.hh"
40#include "BLI_string_utf8.hh"
41#include "BLI_string_utils.hh"
42#include "BLI_utildefines.hh"
43
44#include "PRF_profile.hh"
45
46#ifndef NDEBUG
47# include "BLI_dynstr.hh"
48#endif
49
50#include "BLT_translation.hh"
51
54#include "BKE_attribute_math.hh"
56#include "BKE_ccg.hh"
57#include "BKE_customdata.hh"
58#include "BKE_customdata_file.h"
59#include "BKE_deform.hh"
60#include "BKE_library.hh"
61#include "BKE_main.hh"
62#include "BKE_mesh_remap.hh"
63#include "BKE_multires.hh"
64
65#include "BLO_read_write.hh"
66
67#include "bmesh.hh"
68
69#include "CLG_log.h"
70
71/* only for customdata_data_transfer_interp_normal_normals */
73
74namespace blender {
75
76/* number of layers to add when growing a CustomData object */
77#define CUSTOMDATA_GROW 5
78
79/* ensure typemap size is ok */
81 "size mismatch");
82
83static CLG_LogRef LOG = {"geom.customdata"};
84
85/* -------------------------------------------------------------------- */
88
90 const CustomData_MeshMasks *mask_src)
91{
92 mask_dst->vmask |= mask_src->vmask;
93 mask_dst->emask |= mask_src->emask;
94 mask_dst->fmask |= mask_src->fmask;
95 mask_dst->pmask |= mask_src->pmask;
96 mask_dst->lmask |= mask_src->lmask;
97}
98
100 const CustomData_MeshMasks *mask_required)
101{
102 return (((mask_required->vmask & mask_ref->vmask) == mask_required->vmask) &&
103 ((mask_required->emask & mask_ref->emask) == mask_required->emask) &&
104 ((mask_required->fmask & mask_ref->fmask) == mask_required->fmask) &&
105 ((mask_required->pmask & mask_ref->pmask) == mask_required->pmask) &&
106 ((mask_required->lmask & mask_ref->lmask) == mask_required->lmask));
107}
108
110
111/* -------------------------------------------------------------------- */
114
116 int size = 0; /* the memory size of one element of this layer's data */
117 int alignment = 0;
118
120 const char *structname = nullptr;
122 int structnum = 0;
123
130 const char *defaultname = nullptr;
131
137 cd_copy copy = nullptr;
138
144 cd_free free = nullptr;
145
157 cd_interp interp = nullptr;
158
160 void (*swap)(void *data, const int *corner_indices) = nullptr;
161
171 void (*construct)(void *data, int count) = nullptr;
172
175
177 bool (*equal)(const void *data1, const void *data2) = nullptr;
178 void (*multiply)(void *data, float fac) = nullptr;
179 void (*initminmax)(void *min, void *max) = nullptr;
180 void (*add)(void *data1, const void *data2) = nullptr;
181 void (*dominmax)(const void *data1, void *min, void *max) = nullptr;
182 void (*copyvalue)(const void *source, void *dest, int mixmode, const float mixfactor) = nullptr;
183
185 bool (*read)(CDataFile *cdf, void *data, int count) = nullptr;
186
188 bool (*write)(CDataFile *cdf, const void *data, int count) = nullptr;
189
191 size_t (*filesize)(CDataFile *cdf, const void *data, int count) = nullptr;
192
197 int (*layers_max)() = nullptr;
198};
199
201
202/* -------------------------------------------------------------------- */
205
206static void layerCopy_mdeformvert(const void *source, void *dest, const int count)
207{
208 int i, size = sizeof(MDeformVert);
209
210 memcpy(dest, source, count * size);
211
212 for (i = 0; i < count; i++) {
213 MDeformVert *dvert = static_cast<MDeformVert *>(POINTER_OFFSET(dest, i * size));
214
215 if (dvert->totweight) {
217 __func__);
218
219 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
220 dvert->dw = dw;
221 }
222 else {
223 dvert->dw = nullptr;
224 }
225 }
226}
227
228static void layerFree_mdeformvert(void *data, const int count)
229{
230 for (MDeformVert &dvert : MutableSpan(static_cast<MDeformVert *>(data), count)) {
231 if (dvert.dw) {
232 MEM_delete(dvert.dw);
233 dvert.dw = nullptr;
234 dvert.totweight = 0;
235 }
236 }
237}
238
239static void layerInterp_mdeformvert(const void **sources,
240 const float *weights,
241 const int count,
242 void *dest)
243{
244 /* A single linked list of #MDeformWeight's.
245 * use this to avoid double allocations (which #LinkNode would do). */
246 struct MDeformWeight_Link {
247 MDeformWeight_Link *next;
248 MDeformWeight dw;
249 };
250
251 MDeformVert *dvert = static_cast<MDeformVert *>(dest);
252 MDeformWeight_Link *dest_dwlink = nullptr;
253 MDeformWeight_Link *node;
254
255 /* build a list of unique def_nrs for dest */
256 int totweight = 0;
257 for (int i = 0; i < count; i++) {
258 const MDeformVert *source = static_cast<const MDeformVert *>(sources[i]);
259 float interp_weight = weights[i];
260
261 for (int j = 0; j < source->totweight; j++) {
262 MDeformWeight *dw = &source->dw[j];
263 float weight = dw->weight * interp_weight;
264
265 if (weight == 0.0f) {
266 continue;
267 }
268
269 for (node = dest_dwlink; node; node = node->next) {
270 MDeformWeight *tmp_dw = &node->dw;
271
272 if (tmp_dw->def_nr == dw->def_nr) {
273 tmp_dw->weight += weight;
274 break;
275 }
276 }
277
278 /* if this def_nr is not in the list, add it */
279 if (!node) {
280 MDeformWeight_Link *tmp_dwlink = static_cast<MDeformWeight_Link *>(
281 alloca(sizeof(*tmp_dwlink)));
282 tmp_dwlink->dw.def_nr = dw->def_nr;
283 tmp_dwlink->dw.weight = weight;
284
285 /* Inline linked-list. */
286 tmp_dwlink->next = dest_dwlink;
287 dest_dwlink = tmp_dwlink;
288
289 totweight++;
290 }
291 }
292 }
293
294 /* Delay writing to the destination in case dest is in sources. */
295
296 /* now we know how many unique deform weights there are, so realloc */
297 if (dvert->dw && (dvert->totweight == totweight)) {
298 /* pass (fast-path if we don't need to realloc). */
299 }
300 else {
301 if (dvert->dw) {
302 MEM_delete(dvert->dw);
303 }
304
305 if (totweight) {
306 dvert->dw = MEM_new_array_uninitialized<MDeformWeight>(size_t(totweight), __func__);
307 }
308 }
309
310 if (totweight) {
311 dvert->totweight = totweight;
312 int i = 0;
313 for (node = dest_dwlink; node; node = node->next, i++) {
314 node->dw.weight = std::min(node->dw.weight, 1.0f);
315 dvert->dw[i] = node->dw;
316 }
317 }
318 else {
319 *dvert = MDeformVert{};
320 }
321}
322
323static void layerConstruct_mdeformvert(void *data, const int count)
324{
325 std::fill_n(static_cast<MDeformVert *>(data), count, MDeformVert{});
326}
327
329
330/* -------------------------------------------------------------------- */
333
334static void layerInterp_normal(const void **sources,
335 const float *weights,
336 const int count,
337 void *dest)
338{
339 /* NOTE: This is linear interpolation, which is not optimal for vectors.
340 * Unfortunately, spherical interpolation of more than two values is hairy,
341 * so for now it will do... */
342 float no[3] = {0.0f};
343
344 for (const int i : IndexRange(count)) {
345 madd_v3_v3fl(no, static_cast<const float *>(sources[i]), weights[i]);
346 }
347
348 /* Weighted sum of normalized vectors will **not** be normalized, even if weights are. */
349 normalize_v3_v3(static_cast<float *>(dest), no);
350}
351
352static void layerCopyValue_normal(const void *source,
353 void *dest,
354 const int mixmode,
355 const float mixfactor)
356{
357 const float *no_src = static_cast<const float *>(source);
358 float *no_dst = static_cast<float *>(dest);
359 float no_tmp[3];
360
361 if (ELEM(mixmode,
365 {
366 /* Above/below threshold modes are not supported here, fallback to nomix (just in case). */
367 copy_v3_v3(no_dst, no_src);
368 }
369 else { /* Modes that support 'real' mix factor. */
370 /* Since we normalize in the end, MIX and ADD are the same op here. */
371 if (ELEM(mixmode, CDT_MIX_MIX, CDT_MIX_ADD)) {
372 add_v3_v3v3(no_tmp, no_dst, no_src);
373 normalize_v3(no_tmp);
374 }
375 else if (mixmode == CDT_MIX_SUB) {
376 sub_v3_v3v3(no_tmp, no_dst, no_src);
377 normalize_v3(no_tmp);
378 }
379 else if (mixmode == CDT_MIX_MUL) {
380 mul_v3_v3v3(no_tmp, no_dst, no_src);
381 normalize_v3(no_tmp);
382 }
383 else {
384 copy_v3_v3(no_tmp, no_src);
385 }
386 interp_v3_v3v3_slerp_safe(no_dst, no_dst, no_tmp, mixfactor);
387 }
388}
389
391
392/* -------------------------------------------------------------------- */
395
396static void layerCopy_tface(const void *source, void *dest, const int count)
397{
398 const MTFace *source_tf = static_cast<const MTFace *>(source);
399 MTFace *dest_tf = static_cast<MTFace *>(dest);
400 for (int i = 0; i < count; i++) {
401 dest_tf[i] = source_tf[i];
402 }
403}
404
405static void layerInterp_tface(const void **sources,
406 const float *weights,
407 const int count,
408 void *dest)
409{
410 MTFace *tf = static_cast<MTFace *>(dest);
411 float uv[4][2] = {{0.0f}};
412
413 for (int i = 0; i < count; i++) {
414 const float interp_weight = weights[i];
415 const MTFace *src = static_cast<const MTFace *>(sources[i]);
416
417 for (int j = 0; j < 4; j++) {
418 madd_v2_v2fl(uv[j], src->uv[j], interp_weight);
419 }
420 }
421
422 /* Delay writing to the destination in case dest is in sources. */
423 *tf = *static_cast<MTFace *>(const_cast<void *>((*sources)));
424 memcpy(tf->uv, uv, sizeof(tf->uv));
425}
426
427static void layerSwap_tface(void *data, const int *corner_indices)
428{
429 MTFace *tf = static_cast<MTFace *>(data);
430 float uv[4][2];
431
432 for (int j = 0; j < 4; j++) {
433 const int source_index = corner_indices[j];
434 copy_v2_v2(uv[j], tf->uv[source_index]);
435 }
436
437 memcpy(tf->uv, uv, sizeof(tf->uv));
438}
439
440static void layerDefault_tface(void *data, const int count)
441{
442 static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
443 MTFace *tf = static_cast<MTFace *>(data);
444
445 for (int i = 0; i < count; i++) {
446 tf[i] = default_tf;
447 }
448}
449
451{
452 return MAX_MTFACE;
453}
454
456
457/* -------------------------------------------------------------------- */
460
461static void layerCopy_propFloat(const void *source, void *dest, const int count)
462{
463 memcpy(dest, source, sizeof(MFloatProperty) * count);
464}
465
466static void layerInterp_propFloat(const void **sources,
467 const float *weights,
468 const int count,
469 void *dest)
470{
471 float result = 0.0f;
472 for (int i = 0; i < count; i++) {
473 const float interp_weight = weights[i];
474 const float src = *static_cast<const float *>(sources[i]);
475 result += src * interp_weight;
476 }
477 *static_cast<float *>(dest) = result;
478}
479
480static bool layerValidate_propFloat(void *data, const uint totitems, const bool do_fixes)
481{
482 MFloatProperty *fp = static_cast<MFloatProperty *>(data);
483 bool has_errors = false;
484
485 for (int i = 0; i < totitems; i++, fp++) {
486 if (!isfinite(fp->f)) {
487 if (do_fixes) {
488 fp->f = 0.0f;
489 }
490 has_errors = true;
491 }
492 }
493
494 return has_errors;
495}
496
498
499/* -------------------------------------------------------------------- */
502
503static void layerInterp_propInt(const void **sources,
504 const float *weights,
505 const int count,
506 void *dest)
507{
508 float result = 0.0f;
509 for (const int i : IndexRange(count)) {
510 const float weight = weights[i];
511 const float src = *static_cast<const int *>(sources[i]);
512 result += src * weight;
513 }
514 const int rounded_result = int(round(result));
515 *static_cast<int *>(dest) = rounded_result;
516}
517
519
520/* -------------------------------------------------------------------- */
523
524static void layerCopy_propString(const void *source, void *dest, const int count)
525{
526 memcpy(dest, source, sizeof(MStringProperty) * count);
527}
528
530
531/* -------------------------------------------------------------------- */
534
535static void layerCopy_origspace_face(const void *source, void *dest, const int count)
536{
537 const OrigSpaceFace *source_tf = static_cast<const OrigSpaceFace *>(source);
538 OrigSpaceFace *dest_tf = static_cast<OrigSpaceFace *>(dest);
539
540 for (int i = 0; i < count; i++) {
541 dest_tf[i] = source_tf[i];
542 }
543}
544
545static void layerInterp_origspace_face(const void **sources,
546 const float *weights,
547 const int count,
548 void *dest)
549{
550 OrigSpaceFace *osf = static_cast<OrigSpaceFace *>(dest);
551 float uv[4][2] = {{0.0f}};
552
553 for (int i = 0; i < count; i++) {
554 const float interp_weight = weights[i];
555 const OrigSpaceFace *src = static_cast<const OrigSpaceFace *>(sources[i]);
556
557 for (int j = 0; j < 4; j++) {
558 madd_v2_v2fl(uv[j], src->uv[j], interp_weight);
559 }
560 }
561
562 /* Delay writing to the destination in case dest is in sources. */
563 memcpy(osf->uv, uv, sizeof(osf->uv));
564}
565
566static void layerSwap_origspace_face(void *data, const int *corner_indices)
567{
568 OrigSpaceFace *osf = static_cast<OrigSpaceFace *>(data);
569 float uv[4][2];
570
571 for (int j = 0; j < 4; j++) {
572 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
573 }
574 memcpy(osf->uv, uv, sizeof(osf->uv));
575}
576
577static void layerDefault_origspace_face(void *data, const int count)
578{
579 static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
580 OrigSpaceFace *osf = static_cast<OrigSpaceFace *>(data);
581
582 for (int i = 0; i < count; i++) {
583 osf[i] = default_osf;
584 }
585}
586
588
589/* -------------------------------------------------------------------- */
592
593static void layerSwap_mdisps(void *data, const int *ci)
594{
595 MDisps *s = static_cast<MDisps *>(data);
596
597 if (s->disps) {
598 int nverts = (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
599 int corners = multires_mdisp_corners(s);
600 int cornersize = s->totdisp / corners;
601
602 if (corners != nverts) {
603 /* happens when face changed vertex count in edit mode
604 * if it happened, just forgot displacement */
605
606 MEM_delete(s->disps);
607 s->totdisp = (s->totdisp / corners) * nverts;
608 s->disps = MEM_new_array_zeroed<float[3]>(s->totdisp, "mdisp swap");
609 return;
610 }
611
612 float (*d)[3] = MEM_new_array_zeroed<float[3]>(s->totdisp, "mdisps swap");
613
614 for (int S = 0; S < corners; S++) {
615 memcpy(d + cornersize * S, s->disps + cornersize * ci[S], sizeof(float[3]) * cornersize);
616 }
617
618 MEM_delete(s->disps);
619 s->disps = d;
620 }
621}
622
623static void layerCopy_mdisps(const void *source, void *dest, const int count)
624{
625 const MDisps *s = static_cast<const MDisps *>(source);
626 MDisps *d = static_cast<MDisps *>(dest);
627
628 for (int i = 0; i < count; i++) {
629 if (s[i].disps) {
630 d[i].disps = MEM_dupalloc(s[i].disps);
631 d[i].hidden = MEM_dupalloc(s[i].hidden);
632 }
633 else {
634 d[i].disps = nullptr;
635 d[i].hidden = nullptr;
636 }
637
638 /* still copy even if not in memory, displacement can be external */
639 d[i].totdisp = s[i].totdisp;
640 }
641}
642
643static void layerFree_mdisps(void *data, const int count)
644{
645 for (MDisps &d : MutableSpan(static_cast<MDisps *>(data), count)) {
646 MEM_SAFE_DELETE(d.disps);
647 MEM_SAFE_DELETE(d.hidden);
648 d.totdisp = 0;
649 }
650}
651
652static void layerConstruct_mdisps(void *data, const int count)
653{
654 std::fill_n(static_cast<MDisps *>(data), count, MDisps{});
655}
656
657static bool layerRead_mdisps(CDataFile *cdf, void *data, const int count)
658{
659 MDisps *d = static_cast<MDisps *>(data);
660
661 for (int i = 0; i < count; i++) {
662 if (!d[i].disps) {
663 d[i].disps = MEM_new_array_zeroed<float[3]>(d[i].totdisp, "mdisps read");
664 }
665
666 if (!cdf_read_data(cdf, sizeof(float[3]) * d[i].totdisp, d[i].disps)) {
667 CLOG_ERROR(&LOG, "failed to read multires displacement %d/%d %d", i, count, d[i].totdisp);
668 return false;
669 }
670 }
671
672 return true;
673}
674
675static bool layerWrite_mdisps(CDataFile *cdf, const void *data, const int count)
676{
677 const MDisps *d = static_cast<const MDisps *>(data);
678
679 for (int i = 0; i < count; i++) {
680 if (!cdf_write_data(cdf, sizeof(float[3]) * d[i].totdisp, d[i].disps)) {
681 CLOG_ERROR(&LOG, "failed to write multires displacement %d/%d %d", i, count, d[i].totdisp);
682 return false;
683 }
684 }
685
686 return true;
687}
688
689static size_t layerFilesize_mdisps(CDataFile * /*cdf*/, const void *data, const int count)
690{
691 const MDisps *d = static_cast<const MDisps *>(data);
692 size_t size = 0;
693
694 for (int i = 0; i < count; i++) {
695 size += sizeof(float[3]) * d[i].totdisp;
696 }
697
698 return size;
699}
700
702
703/* -------------------------------------------------------------------- */
706
707/* copy just zeros in this case */
708static void layerCopy_bmesh_elem_py_ptr(const void * /*source*/, void *dest, const int count)
709{
710 const int size = sizeof(void *);
711
712 for (int i = 0; i < count; i++) {
713 void **ptr = static_cast<void **> POINTER_OFFSET(dest, i * size);
714 *ptr = nullptr;
715 }
716}
717
718#ifndef WITH_PYTHON
720{
721 /* dummy */
722}
723#endif
724
725static void layerFree_bmesh_elem_py_ptr(void *data, const int count)
726{
727 for (int i = 0; i < count; i++) {
728 void **ptr = static_cast<void **> POINTER_OFFSET(data, i * sizeof(void *));
729 if (*ptr) {
730 bpy_bm_generic_invalidate(static_cast<BPy_BMGeneric *>(*ptr));
731 }
732 }
733}
734
736
737/* -------------------------------------------------------------------- */
740
741static void layerCopy_grid_paint_mask(const void *source, void *dest, const int count)
742{
743 const GridPaintMask *s = static_cast<const GridPaintMask *>(source);
744 GridPaintMask *d = static_cast<GridPaintMask *>(dest);
745
746 for (int i = 0; i < count; i++) {
747 if (s[i].data) {
748 d[i].data = MEM_dupalloc(s[i].data);
749 d[i].level = s[i].level;
750 }
751 else {
752 d[i].data = nullptr;
753 d[i].level = 0;
754 }
755 }
756}
757
758static void layerFree_grid_paint_mask(void *data, const int count)
759{
760 for (GridPaintMask &gpm : MutableSpan(static_cast<GridPaintMask *>(data), count)) {
761 MEM_SAFE_DELETE(gpm.data);
762 gpm.level = 0;
763 }
764}
765
766static void layerConstruct_grid_paint_mask(void *data, const int count)
767{
768 std::fill_n(static_cast<GridPaintMask *>(data), count, GridPaintMask{});
769}
770
772
773/* -------------------------------------------------------------------- */
776
777static void layerCopyValue_mloopcol(const void *source,
778 void *dest,
779 const int mixmode,
780 const float mixfactor)
781{
782 const MLoopCol *m1 = static_cast<const MLoopCol *>(source);
783 MLoopCol *m2 = static_cast<MLoopCol *>(dest);
784 uchar tmp_col[4];
785
786 if (ELEM(mixmode,
790 {
791 /* Modes that do a full copy or nothing. */
793 /* TODO: Check for a real valid way to get 'factor' value of our dest color? */
794 const float f = (float(m2->r) + float(m2->g) + float(m2->b)) / 3.0f;
795 if (mixmode == CDT_MIX_REPLACE_ABOVE_THRESHOLD && f < mixfactor) {
796 return; /* Do Nothing! */
797 }
798 if (mixmode == CDT_MIX_REPLACE_BELOW_THRESHOLD && f > mixfactor) {
799 return; /* Do Nothing! */
800 }
801 }
802 m2->r = m1->r;
803 m2->g = m1->g;
804 m2->b = m1->b;
805 m2->a = m1->a;
806 }
807 else { /* Modes that support 'real' mix factor. */
808 uchar src[4] = {m1->r, m1->g, m1->b, m1->a};
809 uchar dst[4] = {m2->r, m2->g, m2->b, m2->a};
810
811 if (mixmode == CDT_MIX_MIX) {
812 blend_color_mix_byte(tmp_col, dst, src);
813 }
814 else if (mixmode == CDT_MIX_ADD) {
815 blend_color_add_byte(tmp_col, dst, src);
816 }
817 else if (mixmode == CDT_MIX_SUB) {
818 blend_color_sub_byte(tmp_col, dst, src);
819 }
820 else if (mixmode == CDT_MIX_MUL) {
821 blend_color_mul_byte(tmp_col, dst, src);
822 }
823 else {
824 memcpy(tmp_col, src, sizeof(tmp_col));
825 }
826
827 blend_color_interpolate_byte(dst, dst, tmp_col, mixfactor);
828
829 m2->r = char(dst[0]);
830 m2->g = char(dst[1]);
831 m2->b = char(dst[2]);
832 m2->a = char(dst[3]);
833 }
834}
835
836static bool layerEqual_mloopcol(const void *data1, const void *data2)
837{
838 const MLoopCol *m1 = static_cast<const MLoopCol *>(data1);
839 const MLoopCol *m2 = static_cast<const MLoopCol *>(data2);
840 float r, g, b, a;
841
842 r = m1->r - m2->r;
843 g = m1->g - m2->g;
844 b = m1->b - m2->b;
845 a = m1->a - m2->a;
846
847 return r * r + g * g + b * b + a * a < 0.001f;
848}
849
850static void layerMultiply_mloopcol(void *data, const float fac)
851{
852 MLoopCol *m = static_cast<MLoopCol *>(data);
853
854 m->r = float(m->r) * fac;
855 m->g = float(m->g) * fac;
856 m->b = float(m->b) * fac;
857 m->a = float(m->a) * fac;
858}
859
860static void layerAdd_mloopcol(void *data1, const void *data2)
861{
862 MLoopCol *m = static_cast<MLoopCol *>(data1);
863 const MLoopCol *m2 = static_cast<const MLoopCol *>(data2);
864
865 m->r += m2->r;
866 m->g += m2->g;
867 m->b += m2->b;
868 m->a += m2->a;
869}
870
871static void layerDoMinMax_mloopcol(const void *data, void *vmin, void *vmax)
872{
873 const MLoopCol *m = static_cast<const MLoopCol *>(data);
874 MLoopCol *min = static_cast<MLoopCol *>(vmin);
875 MLoopCol *max = static_cast<MLoopCol *>(vmax);
876
877 min->r = std::min(m->r, min->r);
878 min->g = std::min(m->g, min->g);
879 min->b = std::min(m->b, min->b);
880 min->a = std::min(m->a, min->a);
881 max->r = std::max(m->r, max->r);
882 max->g = std::max(m->g, max->g);
883 max->b = std::max(m->b, max->b);
884 max->a = std::max(m->a, max->a);
885}
886
887static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
888{
889 MLoopCol *min = static_cast<MLoopCol *>(vmin);
890 MLoopCol *max = static_cast<MLoopCol *>(vmax);
891
892 min->r = 255;
893 min->g = 255;
894 min->b = 255;
895 min->a = 255;
896
897 max->r = 0;
898 max->g = 0;
899 max->b = 0;
900 max->a = 0;
901}
902
903static void layerDefault_mloopcol(void *data, const int count)
904{
905 MLoopCol default_mloopcol = {255, 255, 255, 255};
906 MLoopCol *mlcol = static_cast<MLoopCol *>(data);
907 for (int i = 0; i < count; i++) {
908 mlcol[i] = default_mloopcol;
909 }
910}
911
912static void layerInterp_mloopcol(const void **sources, const float *weights, int count, void *dest)
913{
914 MLoopCol *mc = static_cast<MLoopCol *>(dest);
915 struct {
916 float a;
917 float r;
918 float g;
919 float b;
920 } col = {0};
921
922 for (int i = 0; i < count; i++) {
923 const float interp_weight = weights[i];
924 const MLoopCol *src = static_cast<const MLoopCol *>(sources[i]);
925 col.r += src->r * interp_weight;
926 col.g += src->g * interp_weight;
927 col.b += src->b * interp_weight;
928 col.a += src->a * interp_weight;
929 }
930
931 /* Subdivide smooth or fractal can cause problems without clamping
932 * although weights should also not cause this situation */
933
934 /* Also delay writing to the destination in case dest is in sources. */
935 mc->r = round_fl_to_uchar_clamp(col.r);
936 mc->g = round_fl_to_uchar_clamp(col.g);
937 mc->b = round_fl_to_uchar_clamp(col.b);
938 mc->a = round_fl_to_uchar_clamp(col.a);
939}
940
942
943/* -------------------------------------------------------------------- */
946
947/* origspace is almost exact copy of #MLoopUV, keep in sync. */
948static void layerCopyValue_mloop_origspace(const void *source,
949 void *dest,
950 const int /*mixmode*/,
951 const float /*mixfactor*/)
952{
953 const OrigSpaceLoop *luv1 = static_cast<const OrigSpaceLoop *>(source);
954 OrigSpaceLoop *luv2 = static_cast<OrigSpaceLoop *>(dest);
955
956 copy_v2_v2(luv2->uv, luv1->uv);
957}
958
959static bool layerEqual_mloop_origspace(const void *data1, const void *data2)
960{
961 const OrigSpaceLoop *luv1 = static_cast<const OrigSpaceLoop *>(data1);
962 const OrigSpaceLoop *luv2 = static_cast<const OrigSpaceLoop *>(data2);
963
964 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
965}
966
967static void layerMultiply_mloop_origspace(void *data, const float fac)
968{
969 OrigSpaceLoop *luv = static_cast<OrigSpaceLoop *>(data);
970
971 mul_v2_fl(luv->uv, fac);
972}
973
974static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
975{
976 OrigSpaceLoop *min = static_cast<OrigSpaceLoop *>(vmin);
977 OrigSpaceLoop *max = static_cast<OrigSpaceLoop *>(vmax);
978
979 INIT_MINMAX2(min->uv, max->uv);
980}
981
982static void layerDoMinMax_mloop_origspace(const void *data, void *vmin, void *vmax)
983{
984 const OrigSpaceLoop *luv = static_cast<const OrigSpaceLoop *>(data);
985 OrigSpaceLoop *min = static_cast<OrigSpaceLoop *>(vmin);
986 OrigSpaceLoop *max = static_cast<OrigSpaceLoop *>(vmax);
987
988 minmax_v2v2_v2(min->uv, max->uv, luv->uv);
989}
990
991static void layerAdd_mloop_origspace(void *data1, const void *data2)
992{
993 OrigSpaceLoop *l1 = static_cast<OrigSpaceLoop *>(data1);
994 const OrigSpaceLoop *l2 = static_cast<const OrigSpaceLoop *>(data2);
995
996 add_v2_v2(l1->uv, l2->uv);
997}
998
999static void layerInterp_mloop_origspace(const void **sources,
1000 const float *weights,
1001 int count,
1002 void *dest)
1003{
1004 float uv[2];
1005 zero_v2(uv);
1006
1007 for (int i = 0; i < count; i++) {
1008 const float interp_weight = weights[i];
1009 const OrigSpaceLoop *src = static_cast<const OrigSpaceLoop *>(sources[i]);
1010 madd_v2_v2fl(uv, src->uv, interp_weight);
1011 }
1012
1013 /* Delay writing to the destination in case dest is in sources. */
1014 copy_v2_v2((static_cast<OrigSpaceLoop *>(dest))->uv, uv);
1015}
1016/* --- end copy */
1017
1018static void layerInterp_mcol(const void **sources,
1019 const float *weights,
1020 const int count,
1021 void *dest)
1022{
1023 MCol *mc = static_cast<MCol *>(dest);
1024 struct {
1025 float a;
1026 float r;
1027 float g;
1028 float b;
1029 } col[4] = {{0.0f}};
1030
1031 for (int i = 0; i < count; i++) {
1032 const float interp_weight = weights[i];
1033
1034 for (int j = 0; j < 4; j++) {
1035 const MCol *src = static_cast<const MCol *>(sources[i]);
1036 col[j].a += src[j].a * interp_weight;
1037 col[j].r += src[j].r * interp_weight;
1038 col[j].g += src[j].g * interp_weight;
1039 col[j].b += src[j].b * interp_weight;
1040 }
1041 }
1042
1043 /* Delay writing to the destination in case dest is in sources. */
1044 for (int j = 0; j < 4; j++) {
1045
1046 /* Subdivide smooth or fractal can cause problems without clamping
1047 * although weights should also not cause this situation */
1048 mc[j].a = round_fl_to_uchar_clamp(col[j].a);
1049 mc[j].r = round_fl_to_uchar_clamp(col[j].r);
1050 mc[j].g = round_fl_to_uchar_clamp(col[j].g);
1051 mc[j].b = round_fl_to_uchar_clamp(col[j].b);
1052 }
1053}
1054
1055static void layerSwap_mcol(void *data, const int *corner_indices)
1056{
1057 MCol *mcol = static_cast<MCol *>(data);
1058 MCol col[4];
1059
1060 for (int j = 0; j < 4; j++) {
1061 col[j] = mcol[corner_indices[j]];
1062 }
1063
1064 memcpy(mcol, col, sizeof(col));
1065}
1066
1067static void layerDefault_mcol(void *data, const int count)
1068{
1069 static MCol default_mcol = {255, 255, 255, 255};
1070 MCol *mcol = static_cast<MCol *>(data);
1071
1072 for (int i = 0; i < 4 * count; i++) {
1073 mcol[i] = default_mcol;
1074 }
1075}
1076
1077static void layerDefault_origindex(void *data, const int count)
1078{
1079 std::fill_n(static_cast<int *>(data), count, ORIGINDEX_NONE);
1080}
1081
1082static void layerInterp_shapekey(const void **sources, const float *weights, int count, void *dest)
1083{
1084 float **in = reinterpret_cast<float **>(const_cast<void **>(sources));
1085
1086 if (count <= 0) {
1087 return;
1088 }
1089
1090 float co[3];
1091 zero_v3(co);
1092
1093 for (int i = 0; i < count; i++) {
1094 const float interp_weight = weights[i];
1095 madd_v3_v3fl(co, in[i], interp_weight);
1096 }
1097
1098 /* Delay writing to the destination in case dest is in sources. */
1099 copy_v3_v3(static_cast<float *>(dest), co);
1100}
1101
1103
1104/* -------------------------------------------------------------------- */
1107
1108static void layerSwap_flnor(void *data, const int *corner_indices)
1109{
1110 short (*flnors)[4][3] = static_cast<short (*)[4][3]>(data);
1111 short nors[4][3];
1112 int i = 4;
1113
1114 while (i--) {
1115 copy_v3_v3_short(nors[i], (*flnors)[corner_indices[i]]);
1116 }
1117
1118 memcpy(flnors, nors, sizeof(nors));
1119}
1120
1122
1123/* -------------------------------------------------------------------- */
1126
1127static void layerCopyValue_propcol(const void *source,
1128 void *dest,
1129 const int mixmode,
1130 const float mixfactor)
1131{
1132 const MPropCol *m1 = static_cast<const MPropCol *>(source);
1133 MPropCol *m2 = static_cast<MPropCol *>(dest);
1134 float tmp_col[4];
1135
1136 if (ELEM(mixmode,
1140 {
1141 /* Modes that do a full copy or nothing. */
1143 /* TODO: Check for a real valid way to get 'factor' value of our dest color? */
1144 const float f = (m2->color[0] + m2->color[1] + m2->color[2]) / 3.0f;
1145 if (mixmode == CDT_MIX_REPLACE_ABOVE_THRESHOLD && f < mixfactor) {
1146 return; /* Do Nothing! */
1147 }
1148 if (mixmode == CDT_MIX_REPLACE_BELOW_THRESHOLD && f > mixfactor) {
1149 return; /* Do Nothing! */
1150 }
1151 }
1152 copy_v4_v4(m2->color, m1->color);
1153 }
1154 else { /* Modes that support 'real' mix factor. */
1155 if (mixmode == CDT_MIX_MIX) {
1156 blend_color_mix_float(tmp_col, m2->color, m1->color);
1157 }
1158 else if (mixmode == CDT_MIX_ADD) {
1159 blend_color_add_float(tmp_col, m2->color, m1->color);
1160 }
1161 else if (mixmode == CDT_MIX_SUB) {
1162 blend_color_sub_float(tmp_col, m2->color, m1->color);
1163 }
1164 else if (mixmode == CDT_MIX_MUL) {
1165 blend_color_mul_float(tmp_col, m2->color, m1->color);
1166 }
1167 else {
1168 memcpy(tmp_col, m1->color, sizeof(tmp_col));
1169 }
1170 blend_color_interpolate_float(m2->color, m2->color, tmp_col, mixfactor);
1171 }
1172}
1173
1174static bool layerEqual_propcol(const void *data1, const void *data2)
1175{
1176 const MPropCol *m1 = static_cast<const MPropCol *>(data1);
1177 const MPropCol *m2 = static_cast<const MPropCol *>(data2);
1178 float tot = 0;
1179
1180 for (int i = 0; i < 4; i++) {
1181 float c = (m1->color[i] - m2->color[i]);
1182 tot += c * c;
1183 }
1184
1185 return tot < 0.001f;
1186}
1187
1188static void layerMultiply_propcol(void *data, const float fac)
1189{
1190 MPropCol *m = static_cast<MPropCol *>(data);
1191 mul_v4_fl(m->color, fac);
1192}
1193
1194static void layerAdd_propcol(void *data1, const void *data2)
1195{
1196 MPropCol *m = static_cast<MPropCol *>(data1);
1197 const MPropCol *m2 = static_cast<const MPropCol *>(data2);
1198 add_v4_v4(m->color, m2->color);
1199}
1200
1201static void layerDoMinMax_propcol(const void *data, void *vmin, void *vmax)
1202{
1203 const MPropCol *m = static_cast<const MPropCol *>(data);
1204 MPropCol *min = static_cast<MPropCol *>(vmin);
1205 MPropCol *max = static_cast<MPropCol *>(vmax);
1206 minmax_v4v4_v4(min->color, max->color, m->color);
1207}
1208
1209static void layerInitMinMax_propcol(void *vmin, void *vmax)
1210{
1211 MPropCol *min = static_cast<MPropCol *>(vmin);
1212 MPropCol *max = static_cast<MPropCol *>(vmax);
1213
1214 copy_v4_fl(min->color, FLT_MAX);
1215 copy_v4_fl(max->color, FLT_MIN);
1216}
1217
1218static void layerDefault_propcol(void *data, const int count)
1219{
1220 /* Default to white, full alpha. */
1221 MPropCol default_propcol = {{1.0f, 1.0f, 1.0f, 1.0f}};
1222 MPropCol *pcol = static_cast<MPropCol *>(data);
1223 for (int i = 0; i < count; i++) {
1224 copy_v4_v4(pcol[i].color, default_propcol.color);
1225 }
1226}
1227
1228static void layerInterp_propcol(const void **sources, const float *weights, int count, void *dest)
1229{
1230 MPropCol *mc = static_cast<MPropCol *>(dest);
1231 float col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1232 for (int i = 0; i < count; i++) {
1233 const float interp_weight = weights[i];
1234 const MPropCol *src = static_cast<const MPropCol *>(sources[i]);
1235 madd_v4_v4fl(col, src->color, interp_weight);
1236 }
1237 copy_v4_v4(mc->color, col);
1238}
1239
1241
1242/* -------------------------------------------------------------------- */
1245
1246static void layerInterp_propfloat4(const void **sources,
1247 const float *weights,
1248 int count,
1249 void *dest)
1250{
1251 vec4f result = {0.0f, 0.0f, 0.0f, 0.0f};
1252 for (int i = 0; i < count; i++) {
1253 const float interp_weight = weights[i];
1254 const vec4f *src = static_cast<const vec4f *>(sources[i]);
1255 madd_v4_v4fl(&result.x, &src->x, interp_weight);
1256 }
1257 copy_v4_v4(static_cast<float *>(dest), &result.x);
1258}
1259
1260static void layerMultiply_propfloat4(void *data, const float fac)
1261{
1262 vec4f *vec = static_cast<vec4f *>(data);
1263 vec->x *= fac;
1264 vec->y *= fac;
1265 vec->z *= fac;
1266 vec->w *= fac;
1267}
1268
1269static void layerAdd_propfloat4(void *data1, const void *data2)
1270{
1271 vec4f *vec1 = static_cast<vec4f *>(data1);
1272 const vec4f *vec2 = static_cast<const vec4f *>(data2);
1273 vec1->x += vec2->x;
1274 vec1->y += vec2->y;
1275 vec1->z += vec2->z;
1276 vec1->w += vec2->w;
1277}
1278
1280
1281/* -------------------------------------------------------------------- */
1284
1285static void layerInterp_propfloat3(const void **sources,
1286 const float *weights,
1287 int count,
1288 void *dest)
1289{
1290 vec3f result = {0.0f, 0.0f, 0.0f};
1291 for (int i = 0; i < count; i++) {
1292 const float interp_weight = weights[i];
1293 const vec3f *src = static_cast<const vec3f *>(sources[i]);
1294 madd_v3_v3fl(&result.x, &src->x, interp_weight);
1295 }
1296 copy_v3_v3(static_cast<float *>(dest), &result.x);
1297}
1298
1299static void layerMultiply_propfloat3(void *data, const float fac)
1300{
1301 vec3f *vec = static_cast<vec3f *>(data);
1302 vec->x *= fac;
1303 vec->y *= fac;
1304 vec->z *= fac;
1305}
1306
1307static void layerAdd_propfloat3(void *data1, const void *data2)
1308{
1309 vec3f *vec1 = static_cast<vec3f *>(data1);
1310 const vec3f *vec2 = static_cast<const vec3f *>(data2);
1311 vec1->x += vec2->x;
1312 vec1->y += vec2->y;
1313 vec1->z += vec2->z;
1314}
1315
1316static bool layerValidate_propfloat3(void *data, const uint totitems, const bool do_fixes)
1317{
1318 float *values = static_cast<float *>(data);
1319 bool has_errors = false;
1320 for (int i = 0; i < totitems * 3; i++) {
1321 if (!isfinite(values[i])) {
1322 if (do_fixes) {
1323 values[i] = 0.0f;
1324 }
1325 has_errors = true;
1326 }
1327 }
1328 return has_errors;
1329}
1330
1332
1333/* -------------------------------------------------------------------- */
1336
1337static void layerInterp_propfloat2(const void **sources,
1338 const float *weights,
1339 int count,
1340 void *dest)
1341{
1342 vec2f result = {0.0f, 0.0f};
1343 for (int i = 0; i < count; i++) {
1344 const float interp_weight = weights[i];
1345 const vec2f *src = static_cast<const vec2f *>(sources[i]);
1346 madd_v2_v2fl(&result.x, &src->x, interp_weight);
1347 }
1348 copy_v2_v2(static_cast<float *>(dest), &result.x);
1349}
1350
1351static void layerMultiply_propfloat2(void *data, const float fac)
1352{
1353 vec2f *vec = static_cast<vec2f *>(data);
1354 vec->x *= fac;
1355 vec->y *= fac;
1356}
1357
1358static void layerAdd_propfloat2(void *data1, const void *data2)
1359{
1360 vec2f *vec1 = static_cast<vec2f *>(data1);
1361 const vec2f *vec2 = static_cast<const vec2f *>(data2);
1362 vec1->x += vec2->x;
1363 vec1->y += vec2->y;
1364}
1365
1366static bool layerValidate_propfloat2(void *data, const uint totitems, const bool do_fixes)
1367{
1368 float *values = static_cast<float *>(data);
1369 bool has_errors = false;
1370 for (int i = 0; i < totitems * 2; i++) {
1371 if (!isfinite(values[i])) {
1372 if (do_fixes) {
1373 values[i] = 0.0f;
1374 }
1375 has_errors = true;
1376 }
1377 }
1378 return has_errors;
1379}
1380
1381static bool layerEqual_propfloat2(const void *data1, const void *data2)
1382{
1383 const float2 &a = *static_cast<const float2 *>(data1);
1384 const float2 &b = *static_cast<const float2 *>(data2);
1385 return math::distance_squared(a, b) < 0.00001f;
1386}
1387
1388static void layerInitMinMax_propfloat2(void *vmin, void *vmax)
1389{
1390 float2 &min = *static_cast<float2 *>(vmin);
1391 float2 &max = *static_cast<float2 *>(vmax);
1393}
1394
1395static void layerDoMinMax_propfloat2(const void *data, void *vmin, void *vmax)
1396{
1397 const float2 &value = *static_cast<const float2 *>(data);
1398 float2 &a = *static_cast<float2 *>(vmin);
1399 float2 &b = *static_cast<float2 *>(vmax);
1400 math::min_max(value, a, b);
1401}
1402
1403static void layerCopyValue_propfloat2(const void *source,
1404 void *dest,
1405 const int mixmode,
1406 const float mixfactor)
1407{
1408 const float2 &a = *static_cast<const float2 *>(source);
1409 float2 &b = *static_cast<float2 *>(dest);
1410
1411 /* We only support a limited subset of advanced mixing here-
1412 * namely the mixfactor interpolation. */
1413 if (mixmode == CDT_MIX_NOMIX) {
1414 b = a;
1415 }
1416 else {
1417 b = math::interpolate(b, a, mixfactor);
1418 }
1419}
1420
1422
1423/* -------------------------------------------------------------------- */
1426
1427static void layerInterp_propbool(const void **sources, const float *weights, int count, void *dest)
1428{
1429 bool result = false;
1430 for (int i = 0; i < count; i++) {
1431 const float interp_weight = weights[i];
1432 const bool src = *static_cast<const bool *>(sources[i]);
1433 result |= src && (interp_weight > 0.0f);
1434 }
1435 *static_cast<bool *>(dest) = result;
1436}
1437
1439
1440/* -------------------------------------------------------------------- */
1443
1444static void layerDefault_propquaternion(void *data, const int count)
1445{
1446 MutableSpan(static_cast<math::Quaternion *>(data), count).fill(math::Quaternion::identity());
1447}
1448
1449static void layerInterp_propquaternion(const void **sources,
1450 const float *weights,
1451 int count,
1452 void *dest)
1453{
1454 using math::Quaternion;
1457
1458 for (int i = 0; i < count; i++) {
1459 const float interp_weight = weights[i];
1460 const Quaternion *src = static_cast<const Quaternion *>(sources[i]);
1461 mixer.mix_in(0, *src, interp_weight);
1462 }
1463 mixer.finalize();
1464 *static_cast<Quaternion *>(dest) = result;
1465}
1466
1468
1469/* -------------------------------------------------------------------- */
1472
1473static void layerDefault_propfloat4x4(void *data, const int count)
1474{
1475 MutableSpan(static_cast<float4x4 *>(data), count).fill(float4x4::identity());
1476}
1477
1479
1480/* -------------------------------------------------------------------- */
1483
1485 /* 0: CD_MVERT */ /* DEPRECATED */
1486 {
1487 .size = sizeof(MVert),
1488 .alignment = alignof(MVert),
1489 .structname = "MVert",
1490 .structnum = 1,
1491 },
1492 /* 1: CD_MSTICKY */ /* DEPRECATED */
1493 {
1494 .size = sizeof(float[2]),
1495 .alignment = alignof(float2),
1496 .structname = "",
1497 .structnum = 1,
1498 },
1499 /* 2: CD_MDEFORMVERT */
1500 {
1501 .size = sizeof(MDeformVert),
1502 .alignment = alignof(MDeformVert),
1503 .structname = "MDeformVert",
1504 .structnum = 1,
1505 .copy = layerCopy_mdeformvert,
1506 .free = layerFree_mdeformvert,
1507 .interp = layerInterp_mdeformvert,
1508 .construct = layerConstruct_mdeformvert,
1509 },
1510 /* 3: CD_MEDGE */ /* DEPRECATED */
1511 {
1512 .size = sizeof(MEdge),
1513 .alignment = alignof(MEdge),
1514 .structname = "MEdge",
1515 .structnum = 1,
1516 },
1517 /* 4: CD_MFACE */
1518 {
1519 .size = sizeof(MFace),
1520 .alignment = alignof(MFace),
1521 .structname = "MFace",
1522 .structnum = 1,
1523 },
1524 /* 5: CD_MTFACE */
1525 {
1526 .size = sizeof(MTFace),
1527 .alignment = alignof(MTFace),
1528 .structname = "MTFace",
1529 .structnum = 1,
1530 .defaultname = N_("UVMap"),
1531 .copy = layerCopy_tface,
1532 .interp = layerInterp_tface,
1533 .swap = layerSwap_tface,
1534 .construct = layerDefault_tface,
1535 .layers_max = layerMaxNum_tface,
1536 },
1537 /* 6: CD_MCOL */
1538 /* 4 MCol structs per face */
1539 {
1540 .size = sizeof(MCol[4]),
1541 .alignment = alignof(MCol[4]),
1542 .structname = "MCol",
1543 .structnum = 4,
1544 .defaultname = N_("Col"),
1545 .interp = layerInterp_mcol,
1546 .swap = layerSwap_mcol,
1547 .set_default_value = layerDefault_mcol,
1548 },
1549 /* 7: CD_ORIGINDEX */
1550 {
1551 .size = sizeof(int),
1552 .alignment = alignof(int),
1553 .structname = "",
1554 .structnum = 0,
1555 .set_default_value = layerDefault_origindex,
1556 },
1557 /* 8: CD_NORMAL */
1558 /* 3 floats per normal vector */
1559 {
1560 .size = sizeof(float[3]),
1561 .alignment = alignof(float3),
1562 .structname = "vec3f",
1563 .structnum = 1,
1564 .interp = layerInterp_normal,
1565 .copyvalue = layerCopyValue_normal,
1566 },
1567 /* 9: CD_FACEMAP */ /* DEPRECATED */
1568 {
1569 .size = sizeof(int),
1570 .alignment = alignof(int),
1571 .structname = "",
1572 },
1573 /* 10: CD_PROP_FLOAT */
1574 {
1575 .size = sizeof(MFloatProperty),
1576 .alignment = alignof(float),
1577 .structname = "MFloatProperty",
1578 .structnum = 1,
1579 .defaultname = N_("Float"),
1580 .copy = layerCopy_propFloat,
1581 .interp = layerInterp_propFloat,
1582 .validate = layerValidate_propFloat,
1583 },
1584 /* 11: CD_PROP_INT32 */
1585 {
1586 .size = sizeof(MIntProperty),
1587 .alignment = alignof(int),
1588 .structname = "MIntProperty",
1589 .structnum = 1,
1590 .defaultname = N_("Int"),
1591 .interp = layerInterp_propInt,
1592 },
1593 /* 12: CD_PROP_STRING */
1594 {
1595 .size = sizeof(MStringProperty),
1596 .alignment = alignof(MStringProperty),
1597 .structname = "MStringProperty",
1598 .structnum = 1,
1599 .defaultname = N_("String"),
1600 .copy = layerCopy_propString,
1601 },
1602 /* 13: CD_ORIGSPACE */
1603 {
1604 .size = sizeof(OrigSpaceFace),
1605 .alignment = alignof(OrigSpaceFace),
1606 .structname = "OrigSpaceFace",
1607 .structnum = 1,
1608 .defaultname = N_("UVMap"),
1612 .set_default_value = layerDefault_origspace_face,
1613 },
1614 /* 14: CD_ORCO */
1615 {
1616 .size = sizeof(float[3]),
1617 .alignment = alignof(float3),
1618 .structname = "",
1619 .structnum = 0,
1620 },
1621 /* 15: CD_MTEXPOLY */ /* DEPRECATED */
1622 /* NOTE: when we expose the UV Map / TexFace split to the user,
1623 * change this back to face Texture. */
1624 {
1625 .size = sizeof(int),
1626 .alignment = alignof(int),
1627 .structname = "",
1628 .structnum = 0,
1629 },
1630 /* 16: CD_MLOOPUV */ /* DEPRECATED */
1631 {
1632 .size = sizeof(MLoopUV),
1633 .alignment = alignof(MLoopUV),
1634 .structname = "MLoopUV",
1635 .structnum = 1,
1636 .defaultname = N_("UVMap"),
1637 },
1638 /* 17: CD_PROP_BYTE_COLOR */
1639 {
1640 .size = sizeof(MLoopCol),
1641 .alignment = alignof(MLoopCol),
1642 .structname = "MLoopCol",
1643 .structnum = 1,
1644 .defaultname = N_("Col"),
1645 .interp = layerInterp_mloopcol,
1646 .set_default_value = layerDefault_mloopcol,
1647 .equal = layerEqual_mloopcol,
1648 .multiply = layerMultiply_mloopcol,
1649 .initminmax = layerInitMinMax_mloopcol,
1650 .add = layerAdd_mloopcol,
1651 .dominmax = layerDoMinMax_mloopcol,
1652 .copyvalue = layerCopyValue_mloopcol,
1653 },
1654 /* 18: CD_TANGENT */ /* DEPRECATED */
1655 {
1656 .size = sizeof(float[4]),
1657 .alignment = alignof(float[4]),
1658 .structname = "",
1659 .structnum = 0,
1660 .defaultname = N_("Tangent"),
1661 },
1662 /* 19: CD_MDISPS */
1663 {
1664 .size = sizeof(MDisps),
1665 .alignment = alignof(MDisps),
1666 .structname = "MDisps",
1667 .structnum = 1,
1668 .copy = layerCopy_mdisps,
1669 .free = layerFree_mdisps,
1670 .swap = layerSwap_mdisps,
1671 .construct = layerConstruct_mdisps,
1672 .read = layerRead_mdisps,
1673 .write = layerWrite_mdisps,
1674 .filesize = layerFilesize_mdisps,
1675 },
1676 /* 20: CD_PREVIEW_MCOL */
1677 {
1678 .size = sizeof(float4x4),
1679 .alignment = alignof(float4x4),
1680 .structname = "mat4x4f",
1681 .structnum = 1,
1682 .defaultname = N_("4 by 4 Float Matrix"),
1683 .set_default_value = layerDefault_propfloat4x4,
1684 },
1685 /* 21: CD_ID_MCOL */ /* DEPRECATED */
1686 {
1687 .size = sizeof(MCol[4]),
1688 .alignment = alignof(MCol[4]),
1689 .structname = "",
1690 .structnum = 0,
1691 },
1692 /* 22: CD_PROP_INT16_2D */
1693 {
1694 .size = sizeof(short2),
1695 .alignment = alignof(short2),
1696 .structname = "vec2s",
1697 .structnum = 1,
1698 .defaultname = N_("2D 16-Bit Integer"),
1699 },
1700 /* 23: CD_CLOTH_ORCO */
1701 {
1702 .size = sizeof(float[3]),
1703 .alignment = alignof(float[3]),
1704 .structname = "",
1705 .structnum = 0,
1706 },
1707 /* 24: CD_PROP_FLOAT4 */
1708 {
1709 .size = sizeof(float4),
1710 .alignment = alignof(float4),
1711 .structname = "vec4f",
1712 .structnum = 1,
1713 .defaultname = N_("Float4"),
1714 .interp = layerInterp_propfloat4,
1715 .multiply = layerMultiply_propfloat4,
1716 .add = layerAdd_propfloat4,
1717 },
1718 /* 25: CD_MPOLY */ /* DEPRECATED */
1719 {
1720 .size = sizeof(MPoly),
1721 .alignment = alignof(MPoly),
1722 .structname = "MPoly",
1723 .structnum = 1,
1724 .defaultname = N_("NGon Face"),
1725 },
1726 /* 26: CD_MLOOP */ /* DEPRECATED */
1727 {
1728 .size = sizeof(MLoop),
1729 .alignment = alignof(MLoop),
1730 .structname = "MLoop",
1731 .structnum = 1,
1732 .defaultname = N_("NGon Face-Vertex"),
1733 },
1734 /* 27: CD_SHAPE_KEYINDEX */
1735 {
1736 .size = sizeof(int),
1737 .alignment = alignof(int),
1738 .structname = "",
1739 .structnum = 0,
1740 },
1741 /* 28: CD_SHAPEKEY */
1742 {
1743 .size = sizeof(float[3]),
1744 .alignment = alignof(float[3]),
1745 .structname = "",
1746 .structnum = 0,
1747 .defaultname = N_("ShapeKey"),
1748 .interp = layerInterp_shapekey,
1749 },
1750 /* 29: CD_BWEIGHT */ /* DEPRECATED */
1751 {
1752 .size = sizeof(MFloatProperty),
1753 .alignment = alignof(MFloatProperty),
1754 .structname = "MFloatProperty",
1755 .structnum = 1,
1756 },
1757 /* 30: CD_CREASE */ /* DEPRECATED */
1758 {
1759 .size = sizeof(float),
1760 .alignment = alignof(float),
1761 .structname = "",
1762 },
1763 /* 31: CD_ORIGSPACE_MLOOP */
1764 {
1765 .size = sizeof(OrigSpaceLoop),
1766 .alignment = alignof(OrigSpaceLoop),
1767 .structname = "OrigSpaceLoop",
1768 .structnum = 1,
1769 .defaultname = N_("OS Loop"),
1773 .initminmax = layerInitMinMax_mloop_origspace,
1776 .copyvalue = layerCopyValue_mloop_origspace,
1777 },
1778 /* 32: CD_PREVIEW_MLOOPCOL */ /* DEPRECATED */ /* UNUSED */
1779 {},
1780 /* 33: CD_BM_ELEM_PYPTR */
1781 {
1782 .size = sizeof(void *),
1783 .alignment = alignof(void *),
1784 .structname = "",
1785 .structnum = 1,
1788 },
1789 /* 34: CD_PAINT_MASK */ /* DEPRECATED */
1790 {
1791 .size = sizeof(float),
1792 .alignment = alignof(float),
1793 .structname = "",
1794 .structnum = 0,
1795 },
1796 /* 35: CD_GRID_PAINT_MASK */
1797 {
1798 .size = sizeof(GridPaintMask),
1799 .alignment = alignof(GridPaintMask),
1800 .structname = "GridPaintMask",
1801 .structnum = 1,
1804 .construct = layerConstruct_grid_paint_mask,
1805 },
1806 /* 36: CD_MVERT_SKIN */ /* DEPRECATED */
1807 {
1808 .size = sizeof(MVertSkin),
1809 .alignment = alignof(MVertSkin),
1810 .structname = "MVertSkin",
1811 .structnum = 1,
1812 },
1813 /* 37: CD_FREESTYLE_EDGE */ /* DEPRECATED */
1814 {
1815 .size = sizeof(FreestyleEdge),
1816 .alignment = alignof(FreestyleEdge),
1817 .structname = "FreestyleEdge",
1818 .structnum = 1,
1819 },
1820 /* 38: CD_FREESTYLE_FACE */ /* DEPRECATED */
1821 {
1822 .size = sizeof(FreestyleFace),
1823 .alignment = alignof(FreestyleFace),
1824 .structname = "FreestyleFace",
1825 .structnum = 1,
1826 },
1827 /* 39: CD_MLOOPTANGENT */
1828 {
1829 .size = sizeof(float[4]),
1830 .alignment = alignof(float[4]),
1831 .structname = "",
1832 .structnum = 0,
1833 },
1834 /* 40: CD_TESSLOOPNORMAL */
1835 {
1836 .size = sizeof(short[4][3]),
1837 .alignment = alignof(short[4][3]),
1838 .structname = "",
1839 .structnum = 0,
1840 .swap = layerSwap_flnor,
1841 },
1842 /* 41: CD_CUSTOMLOOPNORMAL */ /* DEPRECATED */
1843 {
1844 .size = sizeof(short[2]),
1845 .alignment = alignof(short[2]),
1846 .structname = "vec2s",
1847 .structnum = 1,
1848 },
1849 /* 42: CD_SCULPT_FACE_SETS */ /* DEPRECATED */
1850 {
1851 .size = sizeof(int),
1852 .alignment = alignof(int),
1853 .structname = "",
1854 },
1855 /* 43: CD_LOCATION */
1856 {
1857 .size = sizeof(float[3]),
1858 .alignment = alignof(float[3]),
1859 .structname = "vec3f",
1860 .structnum = 1,
1861 },
1862 /* 44: CD_RADIUS */
1863 {
1864 .size = sizeof(float),
1865 .alignment = alignof(float),
1866 .structname = "MFloatProperty",
1867 .structnum = 1,
1868 },
1869 /* 45: CD_PROP_INT8 */
1870 {
1871 .size = sizeof(int8_t),
1872 .alignment = alignof(int8_t),
1873 .structname = "MInt8Property",
1874 .structnum = 1,
1875 .defaultname = N_("Int8"),
1876 },
1877 /* 46: CD_PROP_INT32_2D */
1878 {
1879 .size = sizeof(int2),
1880 .alignment = alignof(int2),
1881 .structname = "vec2i",
1882 .structnum = 1,
1883 .defaultname = N_("Int 2D"),
1884 },
1885 /* 47: CD_PROP_COLOR */
1886 {
1887 .size = sizeof(MPropCol),
1888 .alignment = alignof(MPropCol),
1889 .structname = "MPropCol",
1890 .structnum = 1,
1891 .defaultname = N_("Color"),
1892 .interp = layerInterp_propcol,
1893 .set_default_value = layerDefault_propcol,
1894 .equal = layerEqual_propcol,
1895 .multiply = layerMultiply_propcol,
1896 .initminmax = layerInitMinMax_propcol,
1897 .add = layerAdd_propcol,
1898 .dominmax = layerDoMinMax_propcol,
1899 .copyvalue = layerCopyValue_propcol,
1900 },
1901 /* 48: CD_PROP_FLOAT3 */
1902 {
1903 .size = sizeof(float[3]),
1904 .alignment = alignof(float3),
1905 .structname = "vec3f",
1906 .structnum = 1,
1907 .defaultname = N_("Float3"),
1908 .interp = layerInterp_propfloat3,
1909 .validate = layerValidate_propfloat3,
1910 .multiply = layerMultiply_propfloat3,
1911 .add = layerAdd_propfloat3,
1912 },
1913 /* 49: CD_PROP_FLOAT2 */
1914 {
1915 .size = sizeof(float[2]),
1916 .alignment = alignof(float2),
1917 .structname = "vec2f",
1918 .structnum = 1,
1919 .defaultname = N_("Float2"),
1920 .interp = layerInterp_propfloat2,
1921 .validate = layerValidate_propfloat2,
1922 .equal = layerEqual_propfloat2,
1923 .multiply = layerMultiply_propfloat2,
1924 .initminmax = layerInitMinMax_propfloat2,
1925 .add = layerAdd_propfloat2,
1926 .dominmax = layerDoMinMax_propfloat2,
1927 .copyvalue = layerCopyValue_propfloat2,
1928 },
1929 /* 50: CD_PROP_BOOL */
1930 {
1931 .size = sizeof(bool),
1932 .alignment = alignof(bool),
1933 .structname = "bool",
1934 .structnum = 1,
1935 .defaultname = N_("Boolean"),
1936 .interp = layerInterp_propbool,
1937 },
1938 /* 51: CD_HAIRLENGTH */ /* DEPRECATED */ /* UNUSED */
1939 {
1940 .size = sizeof(float),
1941 .alignment = alignof(float),
1942 .structname = "float",
1943 .structnum = 1,
1944 },
1945 /* 52: CD_PROP_QUATERNION */
1946 {
1947 .size = sizeof(float[4]),
1948 .alignment = alignof(float4),
1949 .structname = "vec4f",
1950 .structnum = 1,
1951 .defaultname = N_("Quaternion"),
1953 .set_default_value = layerDefault_propquaternion,
1954 },
1955};
1956
1957static_assert(sizeof(mat4x4f) == sizeof(float4x4));
1958
1959static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1960 /* 0-4 */ "CDMVert",
1961 "CDMSticky",
1962 "CDMDeformVert",
1963 "CDMEdge",
1964 "CDMFace",
1965 /* 5-9 */ "CDMTFace",
1966 "CDMCol",
1967 "CDOrigIndex",
1968 "CDNormal",
1969 "CDFaceMap",
1970 /* 10-14 */ "CDMFloatProperty",
1971 "CDMIntProperty",
1972 "CDMStringProperty",
1973 "CDOrigSpace",
1974 "CDOrco",
1975 /* 15-19 */ "CDMTexPoly",
1976 "CDMLoopUV",
1977 "CDMloopCol",
1978 "CDTangent",
1979 "CDMDisps",
1980 /* 20-24 */ "CDPreviewMCol",
1981 "CDIDMCol",
1982 "CDTextureMCol",
1983 "CDClothOrco",
1984 "CDMRecast",
1985
1986 /* BMESH ONLY */
1987 /* 25-29 */ "CDMPoly",
1988 "CDMLoop",
1989 "CDShapeKeyIndex",
1990 "CDShapeKey",
1991 "CDBevelWeight",
1992 /* 30-34 */ "CDSubSurfCrease",
1993 "CDOrigSpaceLoop",
1994 "CDPreviewLoopCol",
1995 "CDBMElemPyPtr",
1996 "CDPaintMask",
1997 /* 35-36 */ "CDGridPaintMask",
1998 "CDMVertSkin",
1999 /* 37-38 */ "CDFreestyleEdge",
2000 "CDFreestyleFace",
2001 /* 39-42 */ "CDMLoopTangent",
2002 "CDTessLoopNormal",
2003 "CDCustomLoopNormal",
2004 "CDSculptFaceGroups",
2005 /* 43-46 */ "CDHairPoint",
2006 "CDPropInt8",
2007 "CDHairMapping",
2008 "CDPoint",
2009 "CDPropCol",
2010 "CDPropFloat3",
2011 "CDPropFloat2",
2012 "CDPropBoolean",
2013 "CDHairLength",
2014 "CDPropQuaternion",
2015};
2016
2018 /*vmask*/ CD_MASK_PROP_FLOAT3,
2019 /*emask*/ CD_MASK_PROP_INT32_2D,
2020 /*fmask*/ 0,
2021 /*pmask*/ 0,
2022 /*lmask*/ CD_MASK_PROP_INT32,
2023};
2027 /*fmask*/ 0,
2028 /*pmask*/ CD_MASK_ORIGINDEX,
2029 /*lmask*/ CD_MASK_PROP_INT32,
2030};
2033 /*emask*/
2035 /*fmask*/ 0,
2036 /*pmask*/
2038 /*lmask*/
2040};
2044 /*emask*/
2047 /*pmask*/
2049 /*lmask*/
2050 (CD_MASK_ORIGSPACE_MLOOP | CD_MASK_PROP_ALL), /* XXX: MISSING #CD_MASK_MLOOPTANGENT ? */
2051};
2054 /*emask*/ CD_MASK_PROP_ALL,
2055 /*fmask*/ 0,
2056 /*pmask*/
2058 /*lmask*/
2060};
2075
2077{
2078 if (type < 0 || type >= CD_NUMTYPES) {
2079 return nullptr;
2080 }
2081
2082 return &LAYERTYPEINFO[type];
2083}
2084
2085static const char *layerType_getName(const eCustomDataType type)
2086{
2087 if (type < 0 || type >= CD_NUMTYPES) {
2088 return nullptr;
2089 }
2090
2091 return LAYERTYPENAMES[type];
2092}
2093
2095{
2096 printf("verts mask=0x%" PRIx64 ":\n", mask->vmask);
2097 for (int i = 0; i < CD_NUMTYPES; i++) {
2098 if (mask->vmask & CD_TYPE_AS_MASK(eCustomDataType(i))) {
2100 }
2101 }
2102
2103 printf("edges mask=0x%" PRIx64 ":\n", mask->emask);
2104 for (int i = 0; i < CD_NUMTYPES; i++) {
2105 if (mask->emask & CD_TYPE_AS_MASK(eCustomDataType(i))) {
2107 }
2108 }
2109
2110 printf("faces mask=0x%" PRIx64 ":\n", mask->fmask);
2111 for (int i = 0; i < CD_NUMTYPES; i++) {
2112 if (mask->fmask & CD_TYPE_AS_MASK(eCustomDataType(i))) {
2114 }
2115 }
2116
2117 printf("loops mask=0x%" PRIx64 ":\n", mask->lmask);
2118 for (int i = 0; i < CD_NUMTYPES; i++) {
2119 if (mask->lmask & CD_TYPE_AS_MASK(eCustomDataType(i))) {
2121 }
2122 }
2123
2124 printf("polys mask=0x%" PRIx64 ":\n", mask->pmask);
2125 for (int i = 0; i < CD_NUMTYPES; i++) {
2126 if (mask->pmask & CD_TYPE_AS_MASK(eCustomDataType(i))) {
2128 }
2129 }
2130}
2131
2133
2134/* -------------------------------------------------------------------- */
2137
2138static void customData_update_offsets(CustomData *data);
2139
2140static CustomDataLayer *customData_add_layer__internal(
2141 CustomData *data,
2142 eCustomDataType type,
2143 std::optional<eCDAllocType> alloctype,
2144 void *layer_data_to_assign,
2145 const ImplicitSharingInfo *sharing_info_to_assign,
2146 int totelem,
2147 const StringRef name);
2148
2150{
2151 int lasttype = -1;
2152
2153 for (int i = 0; i < CD_NUMTYPES; i++) {
2154 data->typemap[i] = -1;
2155 }
2156
2157 for (int i = 0; i < data->totlayer; i++) {
2158 const eCustomDataType type = eCustomDataType(data->layers[i].type);
2159 if (type != lasttype) {
2160 data->typemap[type] = i;
2161 lasttype = type;
2162 }
2163 }
2164}
2165
2166/* currently only used in BLI_assert */
2167#ifndef NDEBUG
2169{
2170 CustomData data_copy = *data;
2171 CustomData_update_typemap(&data_copy);
2172 return (memcmp(data->typemap, data_copy.typemap, sizeof(data->typemap)) == 0);
2173}
2174#endif
2175
2176static void *copy_layer_data(const eCustomDataType type, const void *data, const int totelem)
2177{
2178 const LayerTypeInfo &type_info = *layerType_getInfo(type);
2179 const int64_t size_in_bytes = int64_t(totelem) * type_info.size;
2180 void *new_data = MEM_new_uninitialized_aligned(size_in_bytes, type_info.alignment, __func__);
2181 if (type_info.copy) {
2182 type_info.copy(data, new_data, totelem);
2183 }
2184 else {
2185 memcpy(new_data, data, size_in_bytes);
2186 }
2187 return new_data;
2188}
2189
2190static void free_layer_data(const eCustomDataType type, const void *data, const int totelem)
2191{
2192 const LayerTypeInfo &type_info = *layerType_getInfo(type);
2193 if (type_info.free) {
2194 type_info.free(const_cast<void *>(data), totelem);
2195 }
2196 MEM_delete_void(const_cast<void *>(data));
2197}
2198
2199static bool customdata_merge_internal(const CustomData *source,
2200 CustomData *dest,
2201 const eCustomDataMask mask,
2202 const std::optional<eCDAllocType> alloctype,
2203 const int totelem)
2204{
2205 bool changed = false;
2206
2207 int last_type = -1;
2208 int last_active = 0;
2209 int last_render = 0;
2210 int current_type_layer_count = 0;
2211 int max_current_type_layer_count = -1;
2212
2213 for (int i = 0; i < source->totlayer; i++) {
2214 const CustomDataLayer &src_layer = source->layers[i];
2215 const eCustomDataType type = eCustomDataType(src_layer.type);
2216 const eCustomDataLayer_Flag src_layer_flag = src_layer.flag;
2217
2218 if (type != last_type) {
2219 /* Don't exceed layer count on destination. */
2220 const int layernum_dst = CustomData_number_of_layers(dest, type);
2221 current_type_layer_count = layernum_dst;
2222 max_current_type_layer_count = CustomData_layertype_layers_max(type);
2223 last_active = src_layer.active;
2224 last_render = src_layer.active_rnd;
2225 last_type = type;
2226 }
2227 else {
2228 current_type_layer_count++;
2229 }
2230
2231 if (src_layer_flag & CD_FLAG_NOCOPY) {
2232 /* Don't merge this layer because it's not supposed to leave the source data. */
2233 continue;
2234 }
2235 if (!(mask & CD_TYPE_AS_MASK(type))) {
2236 /* Don't merge this layer because it does not match the type mask. */
2237 continue;
2238 }
2239 if ((max_current_type_layer_count != -1) &&
2240 (current_type_layer_count >= max_current_type_layer_count))
2241 {
2242 /* Don't merge this layer because the maximum amount of layers of this type is reached. */
2243 continue;
2244 }
2245 if (CustomData_get_named_layer_index(dest, type, src_layer.name) != -1) {
2246 /* Don't merge this layer because it exists in the destination already. */
2247 continue;
2248 }
2249
2250 void *layer_data_to_assign = nullptr;
2251 const ImplicitSharingInfo *sharing_info_to_assign = nullptr;
2252 if (!alloctype.has_value()) {
2253 if (src_layer.data != nullptr) {
2254 if (src_layer.sharing_info == nullptr) {
2255 /* Can't share the layer, duplicate it instead. */
2256 layer_data_to_assign = copy_layer_data(type, src_layer.data, totelem);
2257 }
2258 else {
2259 /* Share the layer. */
2260 layer_data_to_assign = src_layer.data;
2261 sharing_info_to_assign = src_layer.sharing_info;
2262 }
2263 }
2264 }
2265
2267 type,
2268 alloctype,
2269 layer_data_to_assign,
2270 sharing_info_to_assign,
2271 totelem,
2272 src_layer.name);
2273
2274 new_layer->uid = src_layer.uid;
2275 new_layer->flag |= src_layer_flag & (CD_FLAG_EXTERNAL | CD_FLAG_IN_MEMORY);
2276 new_layer->active = last_active;
2277 new_layer->active_rnd = last_render;
2278 changed = true;
2279 }
2280
2282 return changed;
2283}
2284
2285bool CustomData_merge(const CustomData *source,
2286 CustomData *dest,
2288 int totelem)
2289{
2290 return customdata_merge_internal(source, dest, mask, std::nullopt, totelem);
2291}
2292
2294 CustomData *dest,
2295 const eCustomDataMask mask,
2296 const eCDAllocType alloctype,
2297 const int totelem)
2298{
2299 return customdata_merge_internal(source, dest, mask, alloctype, totelem);
2300}
2301
2307 private:
2308 const void *data_;
2309 int totelem_;
2310 const eCustomDataType type_;
2311
2312 public:
2313 CustomDataLayerImplicitSharing(const void *data, const int totelem, const eCustomDataType type)
2314 : ImplicitSharingInfo(), data_(data), totelem_(totelem), type_(type)
2315 {
2316 }
2317
2318 private:
2319 void delete_self_with_data() override
2320 {
2321 if (data_ != nullptr) {
2322 free_layer_data(type_, data_, totelem_);
2323 }
2324 MEM_delete(this);
2325 }
2326
2327 void delete_data_only() override
2328 {
2329 free_layer_data(type_, data_, totelem_);
2330 data_ = nullptr;
2331 totelem_ = 0;
2332 }
2333};
2334
2336 const void *data,
2337 const int totelem)
2338{
2339 return MEM_new<CustomDataLayerImplicitSharing>(__func__, data, totelem, type);
2340}
2341
2346{
2347 if (layer.data == nullptr) {
2348 return;
2349 }
2350 BLI_assert(layer.sharing_info != nullptr);
2351 if (layer.sharing_info->is_mutable()) {
2352 layer.sharing_info->tag_ensured_mutable();
2353 }
2354 else {
2355 const eCustomDataType type = eCustomDataType(layer.type);
2356 const void *old_data = layer.data;
2357 /* Copy the layer before removing the user because otherwise the data might be freed while
2358 * we're still copying from it here. */
2359 layer.data = copy_layer_data(type, old_data, totelem);
2360 layer.sharing_info->remove_user_and_delete_if_last();
2361 layer.sharing_info = CustomData_make_layer_sharing_info(type, layer.data, totelem);
2362 }
2363}
2364
2365[[maybe_unused]] static bool layer_is_mutable(CustomDataLayer &layer)
2366{
2367 if (!layer.data) {
2368 return true;
2369 }
2370 return layer.sharing_info->is_mutable();
2371}
2372
2377
2379{
2380 for (const int i : IndexRange(data->totlayer)) {
2381 ensure_layer_data_is_mutable(data->layers[i], totelem);
2382 }
2383}
2384
2386 const int old_size,
2387 const int new_size,
2388 const eCDAllocType alloctype)
2389{
2391 BLI_assert(new_size >= 0);
2392 for (int i = 0; i < data->totlayer; i++) {
2393 CustomDataLayer *layer = &data->layers[i];
2394 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
2395 const int64_t old_size_in_bytes = int64_t(old_size) * typeInfo->size;
2396 const int64_t new_size_in_bytes = int64_t(new_size) * typeInfo->size;
2397
2398 void *new_layer_data = (new_size > 0) ? MEM_new_uninitialized_aligned(
2399 new_size_in_bytes, typeInfo->alignment, __func__) :
2400 nullptr;
2401 if (old_size_in_bytes > 0) {
2402 if (new_layer_data != nullptr) {
2403 /* Copy data to new array. */
2404 if (typeInfo->copy) {
2405 typeInfo->copy(layer->data, new_layer_data, std::min(old_size, new_size));
2406 }
2407 else {
2408 BLI_assert(layer->data != nullptr);
2409 memcpy(new_layer_data, layer->data, std::min(old_size_in_bytes, new_size_in_bytes));
2410 }
2411 }
2412 BLI_assert(layer->sharing_info != nullptr);
2413 layer->sharing_info->remove_user_and_delete_if_last();
2414 layer->sharing_info = nullptr;
2415 }
2416 /* Take ownership of new array. */
2417 layer->data = new_layer_data;
2418 if (layer->data) {
2420 eCustomDataType(layer->type), layer->data, new_size);
2421 }
2422
2423 if (new_size > old_size) {
2424 const int new_elements_num = new_size - old_size;
2425 void *new_elements_begin = POINTER_OFFSET(layer->data, old_size_in_bytes);
2426 switch (alloctype) {
2427 case CD_CONSTRUCT: {
2428 /* Initialize new values for non-trivial types. */
2429 if (typeInfo->construct) {
2430 typeInfo->construct(new_elements_begin, new_elements_num);
2431 }
2432 break;
2433 }
2434 case CD_SET_DEFAULT: {
2435 if (typeInfo->set_default_value) {
2436 typeInfo->set_default_value(new_elements_begin, new_elements_num);
2437 }
2438 else {
2439 memset(new_elements_begin, 0, typeInfo->size * new_elements_num);
2440 }
2441 break;
2442 }
2443 }
2444 }
2445 }
2446}
2447
2449 CustomData *dest,
2451 int totelem)
2452{
2453 CustomData_reset(dest);
2454
2455 if (source->external) {
2456 dest->external = MEM_dupalloc(source->external);
2457 }
2458
2459 CustomData_merge(source, dest, mask, totelem);
2460}
2461
2463 CustomData *dest,
2465 eCDAllocType alloctype,
2466 int totelem)
2467{
2468 CustomData_reset(dest);
2469
2470 if (source->external) {
2471 dest->external = MEM_dupalloc(source->external);
2472 }
2473
2474 CustomData_merge_layout(source, dest, mask, alloctype, totelem);
2475}
2476
2478{
2479 if (!layer->sharing_info) {
2480 BLI_assert(!layer->data);
2481 return;
2482 }
2483 layer->sharing_info->remove_user_and_delete_if_last();
2484 layer->sharing_info = nullptr;
2485}
2486
2488{
2489 if (data->external) {
2490 MEM_delete(data->external);
2491 data->external = nullptr;
2492 }
2493}
2494
2496{
2497 *data = CustomData{};
2498 std::fill_n(data->typemap, CD_NUMTYPES, -1);
2499}
2500
2502{
2503 for (int i = 0; i < data->totlayer; i++) {
2505 }
2506
2507 if (data->layers) {
2508 MEM_delete(data->layers);
2509 }
2510
2513}
2514
2515static int customData_pad_up_to_alignment(const int value, const int alignment)
2516{
2517 return (value + (alignment - 1)) & ~(alignment - 1);
2518}
2519
2521{
2522 const LayerTypeInfo *typeInfo;
2523 int offset = 0;
2524 /* Padding up is necessary for #pointer_can_point_to_instance, see #152145. */
2525 int max_alignment = 1;
2526
2527 for (int i = 0; i < data->totlayer; i++) {
2528 typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
2529 offset = customData_pad_up_to_alignment(offset, typeInfo->alignment);
2530
2531 data->layers[i].offset = offset;
2532 offset += typeInfo->size;
2533 max_alignment = std::max(max_alignment, typeInfo->alignment);
2534 }
2535
2536 data->totsize = customData_pad_up_to_alignment(offset, max_alignment);
2538}
2539
2540/* to use when we're in the middle of modifying layers */
2542 const eCustomDataType type)
2543{
2544 for (int i = 0; i < data->totlayer; i++) {
2545 if (data->layers[i].type == type) {
2546 return i;
2547 }
2548 }
2549
2550 return -1;
2551}
2552
2553/* -------------------------------------------------------------------- */
2554/* index values to access the layers (offset from the layer start) */
2555
2557{
2559 return data->typemap[type];
2560}
2561
2563{
2564 BLI_assert(n >= 0);
2565 int i = CustomData_get_layer_index(data, type);
2566
2567 if (i != -1) {
2568 /* If the value of n goes past the block of layers of the correct type, return -1. */
2569 i = (i + n < data->totlayer && data->layers[i + n].type == type) ? (i + n) : (-1);
2570 }
2571
2572 return i;
2573}
2574
2576 const eCustomDataType type,
2577 const StringRef name)
2578{
2579 for (int i = 0; i < data->totlayer; i++) {
2580 if (data->layers[i].type == type) {
2581 if (data->layers[i].name == name) {
2582 return i;
2583 }
2584 }
2585 }
2586
2587 return -1;
2588}
2589
2591{
2592 for (int i = 0; i < data->totlayer; i++) {
2593 if (data->layers[i].name == name) {
2594 return i;
2595 }
2596 }
2597
2598 return -1;
2599}
2600
2602{
2603 const int layer_index = data->typemap[type];
2605 return (layer_index != -1) ? layer_index + data->layers[layer_index].active : -1;
2606}
2607
2609{
2610 const int layer_index = data->typemap[type];
2612 return (layer_index != -1) ? layer_index + data->layers[layer_index].active_rnd : -1;
2613}
2614
2615/* -------------------------------------------------------------------- */
2616/* index values per layer type */
2617
2619 const eCustomDataType type,
2620 const StringRef name)
2621{
2622 const int named_index = CustomData_get_named_layer_index(data, type, name);
2623 const int layer_index = data->typemap[type];
2625 return (named_index != -1) ? named_index - layer_index : -1;
2626}
2627
2629{
2630 const int layer_index = data->typemap[type];
2632 return (layer_index != -1) ? data->layers[layer_index].active : -1;
2633}
2634
2636{
2637 const int layer_index = data->typemap[type];
2639 return (layer_index != -1) ? data->layers[layer_index].active_rnd : -1;
2640}
2641
2643{
2644 /* Get the layer index of the active layer of this type. */
2645 const int layer_index = CustomData_get_active_layer_index(data, type);
2646 return layer_index < 0 ? nullptr : data->layers[layer_index].name;
2647}
2648
2650{
2651 const int layer_index = CustomData_get_render_layer_index(data, type);
2652 return layer_index < 0 ? nullptr : data->layers[layer_index].name;
2653}
2654
2656{
2657#ifndef NDEBUG
2658 const int layer_num = CustomData_number_of_layers(data, type);
2659#endif
2660 for (int i = 0; i < data->totlayer; i++) {
2661 if (data->layers[i].type == type) {
2662 BLI_assert(uint(n) < uint(layer_num));
2663 data->layers[i].active = n;
2664 }
2665 }
2666}
2667
2669{
2670#ifndef NDEBUG
2671 const int layer_num = CustomData_number_of_layers(data, type);
2672#endif
2673 for (int i = 0; i < data->totlayer; i++) {
2674 if (data->layers[i].type == type) {
2675 BLI_assert(uint(n) < uint(layer_num));
2676 data->layers[i].active_rnd = n;
2677 }
2678 }
2679}
2680
2682{
2683#ifndef NDEBUG
2684 const int layer_num = CustomData_number_of_layers(data, type);
2685#endif
2686 const int layer_index = n - data->typemap[type];
2688
2689 for (int i = 0; i < data->totlayer; i++) {
2690 if (data->layers[i].type == type) {
2691 BLI_assert(uint(layer_index) < uint(layer_num));
2692 data->layers[i].active = layer_index;
2693 }
2694 }
2695}
2696
2698{
2699#ifndef NDEBUG
2700 const int layer_num = CustomData_number_of_layers(data, type);
2701#endif
2702 const int layer_index = n - data->typemap[type];
2704
2705 for (int i = 0; i < data->totlayer; i++) {
2706 if (data->layers[i].type == type) {
2707 BLI_assert(uint(layer_index) < uint(layer_num));
2708 data->layers[i].active_rnd = layer_index;
2709 }
2710 }
2711}
2712
2714 const eCustomDataType type,
2715 const eCustomDataLayer_Flag flag)
2716{
2717 for (int i = 0; i < data->totlayer; i++) {
2718 if (data->layers[i].type == type) {
2719 data->layers[i].flag |= flag;
2720 }
2721 }
2722}
2723
2724static void customData_resize(CustomData *data, const int grow_amount)
2725{
2726 data->layers = static_cast<CustomDataLayer *>(MEM_realloc_uninitialized(
2727 data->layers, (data->maxlayer + grow_amount) * sizeof(CustomDataLayer)));
2728 data->maxlayer += grow_amount;
2729}
2730
2733 const eCustomDataType type,
2734 const std::optional<eCDAllocType> alloctype,
2735 void *layer_data_to_assign,
2736 const ImplicitSharingInfo *sharing_info_to_assign,
2737 const int totelem,
2738 StringRef name)
2739{
2740 const LayerTypeInfo &type_info = *layerType_getInfo(type);
2741 eCustomDataLayer_Flag flag = {};
2742
2743 /* Some layer types only support a single layer. */
2744 if (!type_info.defaultname && CustomData_has_layer(data, type)) {
2745 /* This function doesn't support dealing with existing layer data for these layer types when
2746 * the layer already exists. */
2747 BLI_assert(layer_data_to_assign == nullptr);
2748 return &data->layers[CustomData_get_layer_index(data, type)];
2749 }
2750
2751 int index = data->totlayer;
2752 if (index >= data->maxlayer) {
2754 }
2755
2756 data->totlayer++;
2757
2758 /* Keep layers ordered by type. */
2759 for (; index > 0 && data->layers[index - 1].type > type; index--) {
2760 data->layers[index] = data->layers[index - 1];
2761 }
2762
2763 CustomDataLayer &new_layer = data->layers[index];
2764
2765 /* Clear remaining data on the layer. The original data on the layer has been moved to another
2766 * index. Without this, it can happen that information from the previous layer at that index
2767 * leaks into the new layer. */
2768 new_layer = CustomDataLayer{};
2769
2770 const int64_t size_in_bytes = int64_t(totelem) * type_info.size;
2771 const char *alloc_name = layerType_getName(type);
2772
2773 if (alloctype.has_value()) {
2774 switch (*alloctype) {
2775 case CD_SET_DEFAULT: {
2776 if (totelem > 0) {
2778 size_in_bytes, type_info.alignment, alloc_name);
2779 if (type_info.set_default_value) {
2780 type_info.set_default_value(new_layer.data, totelem);
2781 }
2782 else {
2783 /* Alternatively, #MEM_new_array_zeroed is faster, but has no aligned version. */
2784 memset(new_layer.data, 0, size_in_bytes);
2785 }
2786 }
2787 break;
2788 }
2789 case CD_CONSTRUCT: {
2790 if (totelem > 0) {
2792 size_in_bytes, type_info.alignment, alloc_name);
2793 if (type_info.construct) {
2794 type_info.construct(new_layer.data, totelem);
2795 }
2796 }
2797 break;
2798 }
2799 }
2800 }
2801 else {
2802 if (totelem == 0 && sharing_info_to_assign == nullptr) {
2803 MEM_SAFE_DELETE_VOID(layer_data_to_assign);
2804 }
2805 else {
2806 new_layer.data = layer_data_to_assign;
2807 new_layer.sharing_info = sharing_info_to_assign;
2808 if (new_layer.sharing_info) {
2809 new_layer.sharing_info->add_user();
2810 }
2811 }
2812 }
2813
2814 if (new_layer.data != nullptr && new_layer.sharing_info == nullptr) {
2815 /* Make layer data shareable. */
2816 new_layer.sharing_info = CustomData_make_layer_sharing_info(type, new_layer.data, totelem);
2817 }
2818
2819 new_layer.type = type;
2820 new_layer.flag = flag;
2821
2822 /* Set default name if none exists. Note we only call DATA_() once
2823 * we know there is a default name, to avoid overhead of locale lookups
2824 * in the depsgraph. */
2825 if (name.is_empty() && type_info.defaultname) {
2826 name = DATA_(type_info.defaultname);
2827 }
2828
2829 if (!name.is_empty()) {
2830 name.copy_utf8_truncated(new_layer.name);
2832 if (STR_ELEM(new_layer.name, "skin_modifier_root", "skin_modifier_loose")) {
2833 new_layer.flag |= CD_FLAG_RUNTIME_NOINTERP;
2834 }
2835 }
2836 else {
2837 new_layer.name[0] = '\0';
2838 }
2839
2840 if (index > 0 && data->layers[index - 1].type == type) {
2841 new_layer.active = data->layers[index - 1].active;
2842 new_layer.active_rnd = data->layers[index - 1].active_rnd;
2843 }
2844 else {
2845 new_layer.active = 0;
2846 new_layer.active_rnd = 0;
2847 }
2848
2850
2851 return &data->layers[index];
2852}
2853
2855 const eCustomDataType type,
2856 eCDAllocType alloctype,
2857 const int totelem)
2858{
2859 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2860
2862 data, type, alloctype, nullptr, nullptr, totelem, typeInfo->defaultname);
2864
2865 if (layer) {
2866 return layer->data;
2867 }
2868
2869 return nullptr;
2870}
2871
2873 const eCustomDataType type,
2874 void *layer_data,
2875 const int totelem,
2876 const ImplicitSharingInfo *sharing_info)
2877{
2878 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2879
2881 data, type, std::nullopt, layer_data, sharing_info, totelem, typeInfo->defaultname);
2883
2884 if (layer) {
2885 return layer->data;
2886 }
2887
2888 return nullptr;
2889}
2890
2892 const eCustomDataType type,
2893 const eCDAllocType alloctype,
2894 const int totelem,
2895 const StringRef name)
2896{
2898 data, type, alloctype, nullptr, nullptr, totelem, name);
2900
2901 if (layer) {
2902 return layer->data;
2903 }
2904 return nullptr;
2905}
2906
2908 eCustomDataType type,
2909 void *layer_data,
2910 int totelem,
2911 const StringRef name,
2912 const ImplicitSharingInfo *sharing_info)
2913{
2915 data, type, std::nullopt, layer_data, sharing_info, totelem, name);
2917
2918 if (layer) {
2919 return layer->data;
2920 }
2921 return nullptr;
2922}
2923
2925{
2926 const int index_first = CustomData_get_layer_index(data, type);
2927 const int n = index - index_first;
2928
2929 BLI_assert(index >= index_first);
2930 if ((index_first == -1) || (n < 0)) {
2931 return false;
2932 }
2933 BLI_assert(data->layers[index].type == type);
2934
2936
2937 for (int i = index + 1; i < data->totlayer; i++) {
2938 data->layers[i - 1] = data->layers[i];
2939 }
2940
2941 data->totlayer--;
2942
2943 /* if layer was last of type in array, set new active layer */
2945
2946 if (i != -1) {
2947 /* don't decrement zero index */
2948 const int index_nonzero = n ? n : 1;
2950
2951 for (layer = &data->layers[i]; i < data->totlayer && layer->type == type; i++, layer++) {
2952 if (layer->active >= index_nonzero) {
2953 layer->active--;
2954 }
2955 if (layer->active_rnd >= index_nonzero) {
2956 layer->active_rnd--;
2957 }
2958 }
2959 }
2960
2961 if (data->totlayer <= data->maxlayer - CUSTOMDATA_GROW) {
2963 }
2964
2966
2967 return true;
2968}
2969
2971{
2972 for (const int i : IndexRange(data->totlayer)) {
2973 const CustomDataLayer &layer = data->layers[i];
2974 if (StringRef(layer.name) == name) {
2976 return true;
2977 }
2978 }
2979 return false;
2980}
2981
2983{
2984 const int index = CustomData_get_active_layer_index(data, type);
2985 if (index == -1) {
2986 return false;
2987 }
2988 return CustomData_free_layer(data, type, index);
2989}
2990
2992{
2993 const int index = CustomData_get_layer_index(data, type);
2994 bool any_removed = false;
2995 while (CustomData_free_layer(data, type, index)) {
2996 any_removed = true;
2997 }
2998 return any_removed;
2999}
3000
3002 const eCustomDataType type,
3003 const StringRef name)
3004{
3005 return CustomData_get_named_layer_index(data, type, name) != -1;
3006}
3007
3009{
3010 return (CustomData_get_layer_index(data, type) != -1);
3011}
3012
3014{
3015 int number = 0;
3016
3017 for (int i = 0; i < data->totlayer; i++) {
3018 if (data->layers[i].type == type) {
3019 number++;
3020 }
3021 }
3022
3023 return number;
3024}
3025
3027{
3028 int number = 0;
3029
3030 for (int i = 0; i < data->totlayer; i++) {
3031 if (data->layers[i].type == type && bke::attribute_name_is_anonymous(data->layers[i].name)) {
3032 number++;
3033 }
3034 }
3035
3036 return number;
3037}
3038
3040{
3041 int number = 0;
3042
3043 for (int i = 0; i < data->totlayer; i++) {
3044 if (mask & CD_TYPE_AS_MASK(eCustomDataType(data->layers[i].type))) {
3045 number++;
3046 }
3047 }
3048
3049 return number;
3050}
3051
3053{
3054 for (int i = 0; i < data->totlayer; i++) {
3055 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(data->layers[i].type)))) {
3056 data->layers[i].flag |= CD_FLAG_NOCOPY;
3057 }
3058 }
3059}
3060
3062 const void *src_data,
3063 void *dst_data,
3064 const int count)
3065{
3066 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3067
3068 if (typeInfo->copy) {
3069 typeInfo->copy(src_data, dst_data, count);
3070 }
3071 else {
3072 memcpy(dst_data, src_data, size_t(count) * typeInfo->size);
3073 }
3074}
3075
3077 CustomData *dest,
3078 const int src_layer_index,
3079 const int dst_layer_index,
3080 const int src_index,
3081 const int dst_index,
3082 const int count)
3083{
3084 const LayerTypeInfo *typeInfo;
3085
3086 BLI_assert(layer_is_mutable(dest->layers[dst_layer_index]));
3087
3088 const void *src_data = source->layers[src_layer_index].data;
3089 void *dst_data = dest->layers[dst_layer_index].data;
3090
3091 typeInfo = layerType_getInfo(eCustomDataType(source->layers[src_layer_index].type));
3092
3093 const size_t src_offset = size_t(src_index) * typeInfo->size;
3094 const size_t dst_offset = size_t(dst_index) * typeInfo->size;
3095
3096 if (!count || !src_data || !dst_data) {
3097 if (count && !(src_data == nullptr && dst_data == nullptr)) {
3098 CLOG_WARN(&LOG,
3099 "null data for %s type (%p --> %p), skipping",
3100 layerType_getName(eCustomDataType(source->layers[src_layer_index].type)),
3101 (void *)src_data,
3102 (void *)dst_data);
3103 }
3104 return;
3105 }
3106
3107 if (typeInfo->copy) {
3108 typeInfo->copy(
3109 POINTER_OFFSET(src_data, src_offset), POINTER_OFFSET(dst_data, dst_offset), count);
3110 }
3111 else {
3112 memcpy(POINTER_OFFSET(dst_data, dst_offset),
3113 POINTER_OFFSET(src_data, src_offset),
3114 size_t(count) * typeInfo->size);
3115 }
3116}
3117
3119 CustomData *dest,
3120 const int source_index,
3121 const int dest_index,
3122 const int count)
3123{
3124 /* copies a layer at a time */
3125 int dest_i = 0;
3126 for (int src_i = 0; src_i < source->totlayer; src_i++) {
3127
3128 /* find the first dest layer with type >= the source type
3129 * (this should work because layers are ordered by type)
3130 */
3131 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
3132 dest_i++;
3133 }
3134
3135 /* if there are no more dest layers, we're done */
3136 if (dest_i >= dest->totlayer) {
3137 return;
3138 }
3139
3140 /* if we found a matching layer, copy the data */
3141 if (dest->layers[dest_i].type == source->layers[src_i].type) {
3142 CustomData_copy_data_layer(source, dest, src_i, dest_i, source_index, dest_index, count);
3143
3144 /* if there are multiple source & dest layers of the same type,
3145 * we don't want to copy all source layers to the same dest, so
3146 * increment dest_i
3147 */
3148 dest_i++;
3149 }
3150 }
3151}
3152
3154 CustomData *destination,
3155 const eCustomDataType type,
3156 int source_index,
3157 int destination_index,
3158 int count)
3159{
3160 const int source_layer_index = CustomData_get_layer_index(source, type);
3161 if (source_layer_index == -1) {
3162 return;
3163 }
3164 const int destinaiton_layer_index = CustomData_get_layer_index(destination, type);
3165 if (destinaiton_layer_index == -1) {
3166 return;
3167 }
3169 destination,
3170 source_layer_index,
3171 destinaiton_layer_index,
3172 source_index,
3173 destination_index,
3174 count);
3175}
3176
3177void CustomData_free_elem(CustomData *data, const int index, const int count)
3178{
3179 for (int i = 0; i < data->totlayer; i++) {
3180 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
3181
3182 if (typeInfo->free) {
3183 size_t offset = size_t(index) * typeInfo->size;
3184 BLI_assert(layer_is_mutable(data->layers[i]));
3185
3186 typeInfo->free(POINTER_OFFSET(data->layers[i].data, offset), count);
3187 }
3188 }
3189}
3190
3191#define SOURCE_BUF_SIZE 100
3192
3194 CustomData *dest,
3195 const int *src_indices,
3196 const float *weights,
3197 int count,
3198 int dest_index)
3199{
3200 if (count <= 0) {
3201 return;
3202 }
3203
3204 const void *source_buf[SOURCE_BUF_SIZE];
3205 const void **sources = source_buf;
3206
3207 /* Slow fallback in case we're interpolating a ridiculous number of elements. */
3208 if (count > SOURCE_BUF_SIZE) {
3209 sources = MEM_new_array_uninitialized<const void *>(size_t(count), __func__);
3210 }
3211
3212 /* If no weights are given, generate default ones to produce an average result. */
3213 float default_weights_buf[SOURCE_BUF_SIZE];
3214 float *default_weights = nullptr;
3215 if (weights == nullptr) {
3216 default_weights = (count > SOURCE_BUF_SIZE) ?
3217 MEM_new_array_uninitialized<float>(size_t(count), __func__) :
3218 default_weights_buf;
3219 std::fill_n(default_weights, count, 1.0f / count);
3220 weights = default_weights;
3221 }
3222
3223 /* interpolates a layer at a time */
3224 int dest_i = 0;
3225 for (int src_i = 0; src_i < source->totlayer; src_i++) {
3226 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(source->layers[src_i].type));
3227 if (!typeInfo->interp) {
3228 continue;
3229 }
3230
3231 /* find the first dest layer with type >= the source type
3232 * (this should work because layers are ordered by type)
3233 */
3234 while (dest_i < dest->totlayer && dest->layers[dest_i].type < source->layers[src_i].type) {
3235 dest_i++;
3236 }
3237
3238 /* if there are no more dest layers, we're done */
3239 if (dest_i >= dest->totlayer) {
3240 break;
3241 }
3242
3243 /* if we found a matching layer, copy the data */
3244 if (dest->layers[dest_i].type == source->layers[src_i].type) {
3245 void *src_data = source->layers[src_i].data;
3246
3247 for (int j = 0; j < count; j++) {
3248 sources[j] = POINTER_OFFSET(src_data, size_t(src_indices[j]) * typeInfo->size);
3249 }
3250
3251 typeInfo->interp(
3252 sources,
3253 weights,
3254 count,
3255 POINTER_OFFSET(dest->layers[dest_i].data, size_t(dest_index) * typeInfo->size));
3256
3257 /* if there are multiple source & dest layers of the same type,
3258 * we don't want to copy all source layers to the same dest, so
3259 * increment dest_i
3260 */
3261 dest_i++;
3262 }
3263 }
3264
3265 if (count > SOURCE_BUF_SIZE) {
3266 MEM_delete(sources);
3267 }
3268 if (!ELEM(default_weights, nullptr, default_weights_buf)) {
3269 MEM_delete(default_weights);
3270 }
3271}
3272
3273void CustomData_swap_corners(CustomData *data, const int index, const int *corner_indices)
3274{
3275 for (int i = 0; i < data->totlayer; i++) {
3276 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
3277
3278 if (typeInfo->swap) {
3279 const size_t offset = size_t(index) * typeInfo->size;
3280
3281 typeInfo->swap(POINTER_OFFSET(data->layers[i].data, offset), corner_indices);
3282 }
3283 }
3284}
3285
3287 const int index,
3288 const eCustomDataType type,
3289 int totelem)
3290{
3291 BLI_assert(index >= 0);
3292 void *layer_data = CustomData_get_layer_for_write(data, type, totelem);
3293 if (!layer_data) {
3294 return nullptr;
3295 }
3296 return POINTER_OFFSET(layer_data, size_t(index) * layerType_getInfo(type)->size);
3297}
3298
3300 CustomData *data, const eCustomDataType type, const int index, const int n, int totelem)
3301{
3302 BLI_assert(index >= 0);
3303 void *layer_data = CustomData_get_layer_n_for_write(data, type, n, totelem);
3304 if (!layer_data) {
3305 return nullptr;
3306 }
3307
3308 return POINTER_OFFSET(layer_data, size_t(index) * layerType_getInfo(type)->size);
3309}
3310
3312{
3313 int layer_index = CustomData_get_active_layer_index(data, type);
3314 if (layer_index == -1) {
3315 return nullptr;
3316 }
3317
3318 return data->layers[layer_index].data;
3319}
3320
3322 const eCustomDataType type,
3323 const int totelem)
3324{
3325 const int layer_index = CustomData_get_active_layer_index(data, type);
3326 if (layer_index == -1) {
3327 return nullptr;
3328 }
3329 CustomDataLayer &layer = data->layers[layer_index];
3331 return layer.data;
3332}
3333
3334const void *CustomData_get_layer_n(const CustomData *data, const eCustomDataType type, const int n)
3335{
3336 int layer_index = CustomData_get_layer_index_n(data, type, n);
3337 if (layer_index == -1) {
3338 return nullptr;
3339 }
3340 return data->layers[layer_index].data;
3341}
3342
3344 const eCustomDataType type,
3345 const int n,
3346 const int totelem)
3347{
3348 const int layer_index = CustomData_get_layer_index_n(data, type, n);
3349 if (layer_index == -1) {
3350 return nullptr;
3351 }
3352 CustomDataLayer &layer = data->layers[layer_index];
3354 return layer.data;
3355}
3356
3358 const eCustomDataType type,
3359 const StringRef name)
3360{
3361 int layer_index = CustomData_get_named_layer_index(data, type, name);
3362 if (layer_index == -1) {
3363 return nullptr;
3364 }
3365 return data->layers[layer_index].data;
3366}
3367
3369 const eCustomDataType type,
3370 const StringRef name,
3371 const int totelem)
3372{
3373 const int layer_index = CustomData_get_named_layer_index(data, type, name);
3374 if (layer_index == -1) {
3375 return nullptr;
3376 }
3377 CustomDataLayer &layer = data->layers[layer_index];
3379 return layer.data;
3380}
3381
3383{
3384 int layer_index = CustomData_get_active_layer_index(data, type);
3385 if (layer_index == -1) {
3386 return -1;
3387 }
3388 return data->layers[layer_index].offset;
3389}
3390
3391int CustomData_get_n_offset(const CustomData *data, const eCustomDataType type, const int n)
3392{
3393 int layer_index = CustomData_get_layer_index_n(data, type, n);
3394 if (layer_index == -1) {
3395 return -1;
3396 }
3397
3398 return data->layers[layer_index].offset;
3399}
3400
3402 const eCustomDataType type,
3403 const StringRef name)
3404{
3405 int layer_index = CustomData_get_named_layer_index(data, type, name);
3406 if (layer_index == -1) {
3407 return -1;
3408 }
3409
3410 return data->layers[layer_index].offset;
3411}
3412
3414 const eCustomDataType type,
3415 const int n,
3416 const StringRef name)
3417{
3418 const int layer_index = CustomData_get_layer_index_n(data, type, n);
3419 if (layer_index == -1) {
3420 return false;
3421 }
3422
3423 name.copy_utf8_truncated(data->layers[layer_index].name);
3424
3425 return true;
3426}
3427
3429 const eCustomDataType type,
3430 const int n)
3431{
3432 const int layer_index = CustomData_get_layer_index_n(data, type, n);
3433
3434 return (layer_index == -1) ? nullptr : data->layers[layer_index].name;
3435}
3436
3437/* BMesh functions */
3438
3439void CustomData_bmesh_init_pool(CustomData *data, const int totelem, const char htype)
3440{
3441 int chunksize;
3442
3443 /* Dispose old pools before calling here to avoid leaks */
3444 BLI_assert(data->pool == nullptr);
3445
3446 switch (htype) {
3447 case BM_VERT:
3449 break;
3450 case BM_EDGE:
3452 break;
3453 case BM_LOOP:
3455 break;
3456 case BM_FACE:
3458 break;
3459 default:
3461 chunksize = 512;
3462 break;
3463 }
3464
3465 /* If there are no layers, no pool is needed just yet */
3466 if (data->totlayer) {
3467 data->pool = BLI_mempool_create(data->totsize, totelem, chunksize, BLI_MEMPOOL_NOP);
3468 }
3469}
3470
3472 CustomData *dest,
3474 eCDAllocType alloctype,
3475 BMesh *bm,
3476 const char htype)
3477{
3478
3479 if (CustomData_number_of_layers_typemask(source, mask) == 0) {
3480 return false;
3481 }
3482
3483 /* copy old layer description so that old data can be copied into
3484 * the new allocation */
3485 CustomData destold = *dest;
3486 if (destold.layers) {
3487 destold.layers = MEM_dupalloc(destold.layers);
3488 }
3489
3490 if (CustomData_merge_layout(source, dest, mask, alloctype, 0) == false) {
3491 if (destold.layers) {
3492 MEM_delete(destold.layers);
3493 }
3494 return false;
3495 }
3496
3497 const BMCustomDataCopyMap map = CustomData_bmesh_copy_map_calc(destold, *dest);
3498
3499 int iter_type;
3500 int totelem;
3501 switch (htype) {
3502 case BM_VERT:
3503 iter_type = BM_VERTS_OF_MESH;
3504 totelem = bm->totvert;
3505 break;
3506 case BM_EDGE:
3507 iter_type = BM_EDGES_OF_MESH;
3508 totelem = bm->totedge;
3509 break;
3510 case BM_LOOP:
3511 iter_type = BM_LOOPS_OF_FACE;
3512 totelem = bm->totloop;
3513 break;
3514 case BM_FACE:
3515 iter_type = BM_FACES_OF_MESH;
3516 totelem = bm->totface;
3517 break;
3518 default: /* should never happen */
3519 BLI_assert_msg(0, "invalid type given");
3520 iter_type = BM_VERTS_OF_MESH;
3521 totelem = bm->totvert;
3522 break;
3523 }
3524
3525 dest->pool = nullptr;
3526 CustomData_bmesh_init_pool(dest, totelem, htype);
3527
3528 if (iter_type != BM_LOOPS_OF_FACE) {
3529 BMHeader *h;
3530 BMIter iter;
3531 /* Ensure all current elements follow new customdata layout. */
3532 BM_ITER_MESH (h, &iter, bm, iter_type) {
3533 void *tmp = nullptr;
3534 CustomData_bmesh_copy_block(*dest, map, h->data, &tmp);
3535 CustomData_bmesh_free_block(&destold, &h->data);
3536 h->data = tmp;
3537 }
3538 }
3539 else {
3540 BMFace *f;
3541 BMLoop *l;
3542 BMIter iter;
3543 BMIter liter;
3544
3545 /* Ensure all current elements follow new customdata layout. */
3546 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
3547 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
3548 void *tmp = nullptr;
3549 CustomData_bmesh_copy_block(*dest, map, l->head.data, &tmp);
3551 l->head.data = tmp;
3552 }
3553 }
3554 }
3555
3556 if (destold.pool) {
3557 BLI_mempool_destroy(destold.pool);
3558 }
3559 if (destold.layers) {
3560 MEM_delete(destold.layers);
3561 }
3562 return true;
3563}
3564
3566{
3567 if (*block == nullptr) {
3568 return;
3569 }
3570
3571 for (int i = 0; i < data->totlayer; i++) {
3572 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
3573
3574 if (typeInfo->free) {
3575 int offset = data->layers[i].offset;
3576 typeInfo->free(POINTER_OFFSET(*block, offset), 1);
3577 }
3578 }
3579
3580 if (data->totsize) {
3581 BLI_mempool_free(data->pool, *block);
3582 }
3583
3584 *block = nullptr;
3585}
3586
3588{
3589 if (block == nullptr) {
3590 return;
3591 }
3592 for (int i = 0; i < data->totlayer; i++) {
3593 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
3594 if (typeInfo->free) {
3595 const size_t offset = data->layers[i].offset;
3596 typeInfo->free(POINTER_OFFSET(block, offset), 1);
3597 }
3598 }
3599 if (data->totsize) {
3600 memset(block, 0, data->totsize);
3601 }
3602}
3603
3605{
3606 if (*block) {
3608 }
3609
3610 if (data->totsize > 0) {
3611 *block = BLI_mempool_alloc(data->pool);
3612 }
3613 else {
3614 *block = nullptr;
3615 }
3616}
3617
3619{
3620 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3621 if (typeInfo->set_default_value) {
3622 typeInfo->set_default_value(elem, 1);
3623 }
3624 else {
3625 memset(elem, 0, typeInfo->size);
3626 }
3627}
3628
3629static void CustomData_bmesh_set_default_n(CustomData *data, void **block, const int n)
3630{
3631 const int offset = data->layers[n].offset;
3633 POINTER_OFFSET(*block, offset));
3634}
3635
3637{
3638 if (*block == nullptr) {
3640 }
3641
3642 for (int i = 0; i < data->totlayer; i++) {
3644 }
3645}
3646
3648 const CustomData &dst,
3649 const eCustomDataMask mask_exclude)
3650{
3652 for (const CustomDataLayer &layer_dst : Span(dst.layers, dst.totlayer)) {
3653 const int dst_offset = layer_dst.offset;
3654 const eCustomDataType dst_type = eCustomDataType(layer_dst.type);
3655 const LayerTypeInfo &type_info = *layerType_getInfo(dst_type);
3656
3657 const int src_offset = CustomData_get_offset_named(&src, dst_type, layer_dst.name);
3658 if (src_offset == -1 || CD_TYPE_AS_MASK(dst_type) & mask_exclude) {
3659 if (type_info.set_default_value) {
3660 map.defaults.append({type_info.set_default_value, dst_offset});
3661 }
3662 else {
3663 map.trivial_defaults.append({type_info.size, dst_offset});
3664 }
3665 }
3666 else {
3667 if (type_info.copy) {
3668 map.copies.append({type_info.copy, src_offset, dst_offset});
3669 }
3670 else {
3671 /* NOTE: A way to improve performance of copies (by reducing the number of `memcpy`
3672 * calls) would be combining contiguous chunks in the source and result format. */
3673 map.trivial_copies.append({type_info.size, src_offset, dst_offset});
3674 }
3675 }
3676
3677 if (type_info.free) {
3678 map.free.append({type_info.free, dst_offset});
3679 }
3680 }
3681 return map;
3682}
3683
3685 const BMCustomDataCopyMap &copy_map,
3686 const void *src_block,
3687 void **dst_block)
3688{
3689 if (*dst_block) {
3690 for (const BMCustomDataCopyMap::Free &info : copy_map.free) {
3691 info.fn(POINTER_OFFSET(*dst_block, info.dst_offset), 1);
3692 }
3693 }
3694 else {
3695 if (dst_data.totsize == 0) {
3696 return;
3697 }
3698 *dst_block = BLI_mempool_alloc(dst_data.pool);
3699 }
3700
3701 for (const BMCustomDataCopyMap::TrivialCopy &info : copy_map.trivial_copies) {
3702 memcpy(POINTER_OFFSET(*dst_block, info.dst_offset),
3703 POINTER_OFFSET(src_block, info.src_offset),
3704 info.size);
3705 }
3706 for (const BMCustomDataCopyMap::Copy &info : copy_map.copies) {
3707 info.fn(POINTER_OFFSET(src_block, info.src_offset),
3708 POINTER_OFFSET(*dst_block, info.dst_offset),
3709 1);
3710 }
3711 for (const BMCustomDataCopyMap::TrivialDefault &info : copy_map.trivial_defaults) {
3712 memset(POINTER_OFFSET(*dst_block, info.dst_offset), 0, info.size);
3713 }
3714 for (const BMCustomDataCopyMap::Default &info : copy_map.defaults) {
3715 info.fn(POINTER_OFFSET(*dst_block, info.dst_offset), 1);
3716 }
3717}
3718
3719void CustomData_bmesh_copy_block(CustomData &data, void *src_block, void **dst_block)
3720{
3721 if (*dst_block) {
3722 for (const CustomDataLayer &layer : Span(data.layers, data.totlayer)) {
3724 if (info.free) {
3725 info.free(POINTER_OFFSET(*dst_block, layer.offset), 1);
3726 }
3727 }
3728 }
3729 else {
3730 if (data.totsize == 0) {
3731 return;
3732 }
3733 *dst_block = BLI_mempool_alloc(data.pool);
3734 }
3735
3736 for (const CustomDataLayer &layer : Span(data.layers, data.totlayer)) {
3737 const int offset = layer.offset;
3739 if (info.copy) {
3740 info.copy(POINTER_OFFSET(src_block, offset), POINTER_OFFSET(*dst_block, offset), 1);
3741 }
3742 else {
3743 memcpy(POINTER_OFFSET(*dst_block, offset), POINTER_OFFSET(src_block, offset), info.size);
3744 }
3745 }
3746}
3747
3748void *CustomData_bmesh_get(const CustomData *data, void *block, const eCustomDataType type)
3749{
3750 int layer_index = CustomData_get_active_layer_index(data, type);
3751 if (layer_index == -1) {
3752 return nullptr;
3753 }
3754
3755 return POINTER_OFFSET(block, data->layers[layer_index].offset);
3756}
3757
3759 void *block,
3760 const eCustomDataType type,
3761 const int n)
3762{
3763 int layer_index = CustomData_get_layer_index(data, type);
3764 if (layer_index == -1) {
3765 return nullptr;
3766 }
3767
3768 return POINTER_OFFSET(block, data->layers[layer_index + n].offset);
3769}
3770
3771void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, const int n)
3772{
3773 if (n < 0 || n >= data->totlayer) {
3774 return nullptr;
3775 }
3776
3777 return POINTER_OFFSET(block, data->layers[n].offset);
3778}
3779
3780bool CustomData_layer_has_math(const CustomData *data, const int layer_n)
3781{
3782 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[layer_n].type));
3783
3784 if (typeInfo->equal && typeInfo->add && typeInfo->multiply && typeInfo->initminmax &&
3785 typeInfo->dominmax)
3786 {
3787 return true;
3788 }
3789
3790 return false;
3791}
3792
3793bool CustomData_layer_has_interp(const CustomData *data, const int layer_n)
3794{
3795 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[layer_n].type));
3796
3797 if (typeInfo->interp) {
3798 return true;
3799 }
3800
3801 return false;
3802}
3803
3805{
3806 /* interpolates a layer at a time */
3807 for (int i = 0; i < data->totlayer; i++) {
3809 return true;
3810 }
3811 }
3812
3813 return false;
3814}
3815
3817{
3818 for (int i = 0; i < data->totlayer; i++) {
3819 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
3820 if (typeInfo->free) {
3821 return true;
3822 }
3823 }
3824 return false;
3825}
3826
3828{
3829 /* interpolates a layer at a time */
3830 for (int i = 0; i < data->totlayer; i++) {
3832 return true;
3833 }
3834 }
3835
3836 return false;
3837}
3838
3839void CustomData_data_copy_value(const eCustomDataType type, const void *source, void *dest)
3840{
3841 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3842
3843 if (!dest) {
3844 return;
3845 }
3846
3847 if (typeInfo->copy) {
3848 typeInfo->copy(source, dest, 1);
3849 }
3850 else {
3851 memcpy(dest, source, typeInfo->size);
3852 }
3853}
3854
3856 const void *source,
3857 void *dest,
3858 const int mixmode,
3859 const float mixfactor)
3860{
3861 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3862
3863 if (!dest) {
3864 return;
3865 }
3866
3867 if (typeInfo->copyvalue) {
3868 typeInfo->copyvalue(source, dest, mixmode, mixfactor);
3869 }
3870 else {
3871 /* Mere copy if no advanced interpolation is supported. */
3872 memcpy(dest, source, typeInfo->size);
3873 }
3874}
3875
3876bool CustomData_data_equals(const eCustomDataType type, const void *data1, const void *data2)
3877{
3878 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3879
3880 if (typeInfo->equal) {
3881 return typeInfo->equal(data1, data2);
3882 }
3883
3884 return !memcmp(data1, data2, typeInfo->size);
3885}
3886
3888{
3889 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3890
3891 if (typeInfo->initminmax) {
3892 typeInfo->initminmax(min, max);
3893 }
3894}
3895
3896void CustomData_data_dominmax(const eCustomDataType type, const void *data, void *min, void *max)
3897{
3898 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3899
3900 if (typeInfo->dominmax) {
3901 typeInfo->dominmax(data, min, max);
3902 }
3903}
3904
3905void CustomData_data_multiply(const eCustomDataType type, void *data, const float fac)
3906{
3907 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3908
3909 if (typeInfo->multiply) {
3910 typeInfo->multiply(data, fac);
3911 }
3912}
3913
3914void CustomData_data_add(const eCustomDataType type, void *data1, const void *data2)
3915{
3916 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3917
3918 if (typeInfo->add) {
3919 typeInfo->add(data1, data2);
3920 }
3921}
3922
3924 CustomData *data, void *block, const eCustomDataType type, const int n, const void *source)
3925{
3926 void *dest = CustomData_bmesh_get_n(data, block, type, n);
3927 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
3928
3929 if (!dest) {
3930 return;
3931 }
3932
3933 if (typeInfo->copy) {
3934 typeInfo->copy(source, dest, 1);
3935 }
3936 else {
3937 memcpy(dest, source, typeInfo->size);
3938 }
3939}
3940
3942 const void **src_blocks_ofs,
3943 const float *weights,
3944 int count,
3945 void *dst_block_ofs,
3946 int n)
3947{
3948 BLI_assert(weights != nullptr);
3949 BLI_assert(count > 0);
3950
3951 CustomDataLayer *layer = &data->layers[n];
3952 if (layer->flag & CD_FLAG_RUNTIME_NOINTERP) {
3954 return;
3955 }
3956
3957 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
3958
3959 typeInfo->interp(src_blocks_ofs, weights, count, dst_block_ofs);
3960}
3961
3963 CustomData *data, const void **src_blocks, const float *weights, int count, void *dst_block)
3964{
3965 if (count <= 0) {
3966 return;
3967 }
3968
3969 void *source_buf[SOURCE_BUF_SIZE];
3970 const void **sources = const_cast<const void **>(source_buf);
3971
3972 /* Slow fallback in case we're interpolating a ridiculous number of elements. */
3973 if (count > SOURCE_BUF_SIZE) {
3974 sources = MEM_new_array_uninitialized<const void *>(size_t(count), __func__);
3975 }
3976
3977 /* If no weights are given, generate default ones to produce an average result. */
3978 float default_weights_buf[SOURCE_BUF_SIZE];
3979 float *default_weights = nullptr;
3980 if (weights == nullptr) {
3981 default_weights = (count > SOURCE_BUF_SIZE) ?
3982 MEM_new_array_uninitialized<float>(size_t(count), __func__) :
3983 default_weights_buf;
3984 std::fill_n(default_weights, count, 1.0f / count);
3985 weights = default_weights;
3986 }
3987
3988 /* interpolates a layer at a time */
3989 for (int i = 0; i < data->totlayer; i++) {
3990 CustomDataLayer *layer = &data->layers[i];
3991 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
3992 if (typeInfo->interp) {
3993 for (int j = 0; j < count; j++) {
3994 sources[j] = POINTER_OFFSET(src_blocks[j], layer->offset);
3995 }
3997 data, sources, weights, count, POINTER_OFFSET(dst_block, layer->offset), i);
3998 }
3999 }
4000
4001 if (count > SOURCE_BUF_SIZE) {
4002 MEM_delete(sources);
4003 }
4004 if (!ELEM(default_weights, nullptr, default_weights_buf)) {
4005 MEM_delete(default_weights);
4006 }
4007}
4008
4010{
4011 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
4012
4013 return typeInfo->size;
4014}
4015
4017{
4018 return layerType_getName(type);
4019}
4020
4022{
4023 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
4024 return typeInfo->defaultname == nullptr;
4025}
4026
4028{
4029 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
4030
4031 return (typeInfo->free != nullptr);
4032}
4033
4035{
4036 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
4037
4038 /* Same test as for singleton above. */
4039 if (typeInfo->defaultname == nullptr) {
4040 return 1;
4041 }
4042 if (typeInfo->layers_max == nullptr) {
4043 return -1;
4044 }
4045
4046 return typeInfo->layers_max();
4047}
4048
4050 const StringRef name,
4051 const eCustomDataType type,
4052 const int index)
4053{
4054 /* see if there is a duplicate */
4055 for (int i = 0; i < data->totlayer; i++) {
4056 if (i != index) {
4057 CustomDataLayer *layer = &data->layers[i];
4058
4059 if (CD_TYPE_AS_MASK(type) & CD_MASK_PROP_ALL) {
4061 layer->name == name)
4062 {
4063 return true;
4064 }
4065 }
4066 else {
4067 if (i != index && layer->type == type && layer->name == name) {
4068 return true;
4069 }
4070 }
4071 }
4072 }
4073
4074 return false;
4075}
4076
4078{
4079 if (name.startswith(".")) {
4081 }
4082 for (const StringRef prefix : {UV_PINNED_NAME "."}) {
4083 if (name.startswith(prefix)) {
4085 }
4086 }
4088}
4089
4091{
4092 CustomDataLayer *nlayer = &data->layers[index];
4093 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(nlayer->type));
4094
4095 if (!typeInfo->defaultname) {
4096 return;
4097 }
4098
4099 const int name_maxncpy = CustomData_name_maxncpy_calc(nlayer->name);
4100
4101 /* Set default name if none specified. Note we only call DATA_() when
4102 * needed to avoid overhead of locale lookups in the depsgraph. */
4103 if (nlayer->name[0] == '\0') {
4104 STRNCPY_UTF8(nlayer->name, DATA_(typeInfo->defaultname));
4105 }
4106
4107 const char *defname = ""; /* Dummy argument, never used as `name` is never zero length. */
4109 [&](const StringRef name) {
4110 return cd_layer_find_dupe(data, name, eCustomDataType(nlayer->type), index);
4111 },
4112 defname,
4113 '.',
4114 nlayer->name,
4115 name_maxncpy);
4116}
4117
4119{
4120 const LayerTypeInfo *typeInfo;
4121 CustomDataLayer *layer = &data->layers[index];
4122 bool keeplayer = true;
4123
4124 if (layer->type >= CD_NUMTYPES) {
4125 keeplayer = false; /* unknown layer type from future version */
4126 }
4127 else {
4128 typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4129
4130 if (!typeInfo->defaultname && (index > 0) && data->layers[index - 1].type == layer->type) {
4131 keeplayer = false; /* multiple layers of which we only support one */
4132 }
4133 /* This is a preemptive fix for cases that should not happen
4134 * (layers that should not be written in .blend files),
4135 * but can happen due to bugs (see e.g. #62318).
4136 * Also for forward compatibility, in future,
4137 * we may put into `.blend` file some currently un-written data types,
4138 * this should cover that case as well.
4139 * Better to be safe here, and fix issue on the fly rather than crash... */
4140 /* 0 structnum is used in writing code to tag layer types that should not be written. */
4141 else if (typeInfo->structnum == 0 &&
4142 /* XXX Not sure why those three are exception, maybe that should be fixed? */
4143 !ELEM(layer->type,
4144 CD_PAINT_MASK,
4145 CD_FACEMAP,
4146 CD_MTEXPOLY,
4147 CD_SCULPT_FACE_SETS,
4148 CD_CREASE))
4149 {
4150 keeplayer = false;
4151 CLOG_WARN(&LOG, ".blend file read: removing a data layer that should not have been written");
4152 }
4153 }
4154
4155 if (!keeplayer) {
4156 for (int i = index + 1; i < data->totlayer; i++) {
4157 data->layers[i - 1] = data->layers[i];
4158 }
4159 data->totlayer--;
4160 }
4161
4162 return keeplayer;
4163}
4164
4166{
4168 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4169 BLI_assert(typeInfo);
4170
4171 if (layer->data || count == 0) {
4172 return false;
4173 }
4174
4175 switch (layer->type) {
4176 /* When more instances of corrupt files are found, add them here. */
4177 case CD_PROP_BOOL: /* See #84935. */
4178 case CD_MLOOPUV: /* See #90620. */
4179 case CD_PROP_FLOAT2: /* See #90620. */
4181 count, typeInfo->size, layerType_getName(eCustomDataType(layer->type)));
4182 BLI_assert(layer->data);
4183 if (typeInfo->set_default_value) {
4184 typeInfo->set_default_value(layer->data, count);
4185 }
4186 return true;
4187 break;
4188
4189 case CD_MTEXPOLY:
4190 /* TODO: Investigate multiple test failures on cycles, e.g. cycles_shadow_catcher_cpu. */
4191 break;
4192
4193 default:
4194 /* Log an error so we can collect instances of bad files. */
4195 CLOG_WARN(&LOG, "CustomDataLayer->data is null for type %d.", layer->type);
4196 break;
4197 }
4198 return false;
4199}
4200
4202
4203/* -------------------------------------------------------------------- */
4206
4207static void customdata_external_filename(char filepath[FILE_MAX],
4208 StringRefNull basepath,
4209 ID *id,
4211{
4212 BLI_strncpy(filepath, external->filepath, FILE_MAX);
4213 if (basepath.is_empty()) {
4215 }
4216 else {
4217 BLI_path_abs(filepath, basepath.c_str());
4218 }
4219}
4220
4222{
4223 for (int i = 0; i < data->totlayer; i++) {
4224 CustomDataLayer *layer = &data->layers[i];
4225 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4226
4227 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(layer->type)))) {
4228 /* pass */
4229 }
4230 else if ((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
4231 if (typeInfo->free) {
4232 typeInfo->free(layer->data, totelem);
4233 }
4234 layer->flag &= ~CD_FLAG_IN_MEMORY;
4235 }
4236 }
4237}
4238
4240 CustomData *data, StringRefNull basepath, ID *id, eCustomDataMask mask, const int totelem)
4241{
4242 CustomDataExternal *external = data->external;
4244 char filepath[FILE_MAX];
4245 int update = 0;
4246
4247 if (!external) {
4248 return;
4249 }
4250
4251 for (int i = 0; i < data->totlayer; i++) {
4252 layer = &data->layers[i];
4253 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4254
4255 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(layer->type)))) {
4256 /* pass */
4257 }
4258 else if (layer->flag & CD_FLAG_IN_MEMORY) {
4259 /* pass */
4260 }
4261 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
4262 update = 1;
4263 }
4264 }
4265
4266 if (!update) {
4267 return;
4268 }
4269
4270 customdata_external_filename(filepath, basepath, id, external);
4271
4273 if (!cdf_read_open(cdf, filepath)) {
4274 cdf_free(cdf);
4275 CLOG_ERROR(&LOG,
4276 "Failed to read %s layer from %s.",
4278 filepath);
4279 return;
4280 }
4281
4282 for (int i = 0; i < data->totlayer; i++) {
4283 layer = &data->layers[i];
4284 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4285
4286 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(layer->type)))) {
4287 /* pass */
4288 }
4289 else if (layer->flag & CD_FLAG_IN_MEMORY) {
4290 /* pass */
4291 }
4292 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
4293 const CDataFileLayer *blay = cdf_layer_find(cdf, layer->type, layer->name);
4294
4295 if (blay) {
4296 if (cdf_read_layer(cdf, blay)) {
4297 if (typeInfo->read(cdf, layer->data, totelem)) {
4298 /* pass */
4299 }
4300 else {
4301 break;
4302 }
4303 layer->flag |= CD_FLAG_IN_MEMORY;
4304 }
4305 else {
4306 break;
4307 }
4308 }
4309 }
4310 }
4311
4312 cdf_read_close(cdf);
4313 cdf_free(cdf);
4314}
4315
4317 StringRefNull basepath,
4318 ID *id,
4320 const int totelem,
4321 const int free)
4322{
4323 CustomDataExternal *external = data->external;
4324 int update = 0;
4325 char filepath[FILE_MAX];
4326
4327 if (!external) {
4328 return;
4329 }
4330
4331 /* test if there is anything to write */
4332 for (int i = 0; i < data->totlayer; i++) {
4333 CustomDataLayer *layer = &data->layers[i];
4334 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4335
4336 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(layer->type)))) {
4337 /* pass */
4338 }
4339 else if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
4340 update = 1;
4341 }
4342 }
4343
4344 if (!update) {
4345 return;
4346 }
4347
4348 /* make sure data is read before we try to write */
4349 CustomData_external_read(data, basepath, id, mask, totelem);
4350 customdata_external_filename(filepath, basepath, id, external);
4351
4353
4354 for (int i = 0; i < data->totlayer; i++) {
4355 CustomDataLayer *layer = &data->layers[i];
4356 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4357
4358 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
4359 if (layer->flag & CD_FLAG_IN_MEMORY) {
4361 cdf, layer->type, layer->name, typeInfo->filesize(cdf, layer->data, totelem));
4362 }
4363 else {
4364 cdf_free(cdf);
4365 return; /* read failed for a layer! */
4366 }
4367 }
4368 }
4369
4370 if (!cdf_write_open(cdf, filepath)) {
4371 CLOG_ERROR(&LOG, "Failed to open %s for writing.", filepath);
4372 cdf_free(cdf);
4373 return;
4374 }
4375
4376 int i;
4377 for (i = 0; i < data->totlayer; i++) {
4378 CustomDataLayer *layer = &data->layers[i];
4379 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4380
4381 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
4382 CDataFileLayer *blay = cdf_layer_find(cdf, layer->type, layer->name);
4383
4384 if (cdf_write_layer(cdf, blay)) {
4385 if (typeInfo->write(cdf, layer->data, totelem)) {
4386 /* pass */
4387 }
4388 else {
4389 break;
4390 }
4391 }
4392 else {
4393 break;
4394 }
4395 }
4396 }
4397
4398 if (i != data->totlayer) {
4399 CLOG_ERROR(&LOG, "Failed to write data to %s.", filepath);
4400 cdf_write_close(cdf);
4401 cdf_free(cdf);
4402 return;
4403 }
4404
4405 for (i = 0; i < data->totlayer; i++) {
4406 CustomDataLayer *layer = &data->layers[i];
4407 const LayerTypeInfo *typeInfo = layerType_getInfo(eCustomDataType(layer->type));
4408
4409 if ((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
4410 if (free) {
4411 if (typeInfo->free) {
4412 typeInfo->free(layer->data, totelem);
4413 }
4414 layer->flag &= ~CD_FLAG_IN_MEMORY;
4415 }
4416 }
4417 }
4418
4419 cdf_write_close(cdf);
4420 cdf_free(cdf);
4421}
4422
4424 ID * /*id*/,
4425 const eCustomDataType type,
4426 const int /*totelem*/,
4427 const char *filepath)
4428{
4429 CustomDataExternal *external = data->external;
4430
4431 int layer_index = CustomData_get_active_layer_index(data, type);
4432 if (layer_index == -1) {
4433 return;
4434 }
4435
4436 CustomDataLayer *layer = &data->layers[layer_index];
4437
4438 if (layer->flag & CD_FLAG_EXTERNAL) {
4439 return;
4440 }
4441
4442 if (!external) {
4443 external = MEM_new<CustomDataExternal>(__func__);
4444 data->external = external;
4445 }
4446 STRNCPY(external->filepath, filepath);
4447
4449}
4450
4452 ID *id,
4453 const eCustomDataType type,
4454 const int totelem)
4455{
4456 CustomDataExternal *external = data->external;
4457
4458 int layer_index = CustomData_get_active_layer_index(data, type);
4459 if (layer_index == -1) {
4460 return;
4461 }
4462
4463 CustomDataLayer *layer = &data->layers[layer_index];
4464
4465 if (!external) {
4466 return;
4467 }
4468
4469 if (layer->flag & CD_FLAG_EXTERNAL) {
4470 if (!(layer->flag & CD_FLAG_IN_MEMORY)) {
4472 data, "", id, CD_TYPE_AS_MASK(eCustomDataType(layer->type)), totelem);
4473 }
4474
4475 layer->flag &= ~CD_FLAG_EXTERNAL;
4476 }
4477}
4478
4480{
4481 int layer_index = CustomData_get_active_layer_index(data, type);
4482 if (layer_index == -1) {
4483 return false;
4484 }
4485
4486 CustomDataLayer *layer = &data->layers[layer_index];
4487 return (layer->flag & CD_FLAG_EXTERNAL) != 0;
4488}
4489
4491
4492/* -------------------------------------------------------------------- */
4495
4497 void *data_dst,
4498 const void **sources,
4499 const float *weights,
4500 const int count,
4501 const float mix_factor)
4502{
4503 BLI_assert(weights != nullptr);
4504 BLI_assert(count > 0);
4505
4506 /* Fake interpolation, we actually copy highest weighted source to dest.
4507 * Note we also handle bitflags here,
4508 * in which case we rather choose to transfer value of elements totaling
4509 * more than 0.5 of weight. */
4510 int best_src_idx = 0;
4511
4512 const int data_type = laymap->data_type;
4513 const int mix_mode = laymap->mix_mode;
4514
4515 size_t data_size;
4516
4517 cd_interp interp_cd = nullptr;
4518 cd_copy copy_cd = nullptr;
4519
4520 if (!sources) {
4521 /* Not supported here, abort. */
4522 return;
4523 }
4524
4525 if (int(data_type) & CD_FAKE) {
4526 data_size = laymap->data_size;
4527 }
4528 else {
4529 const LayerTypeInfo *type_info = layerType_getInfo(eCustomDataType(data_type));
4530
4531 data_size = size_t(type_info->size);
4532 interp_cd = type_info->interp;
4533 copy_cd = type_info->copy;
4534 }
4535
4536 void *tmp_dst = MEM_new_uninitialized(data_size, __func__);
4537
4538 if (count > 1 && !interp_cd) {
4539 /* We just choose highest weighted source. */
4540 float max_weight = 0.0f;
4541
4542 for (int i = 0; i < count; i++) {
4543 if (weights[i] > max_weight) {
4544 max_weight = weights[i];
4545 best_src_idx = i;
4546 }
4547 }
4548 }
4549
4550 BLI_assert(best_src_idx >= 0);
4551
4552 if (interp_cd) {
4553 interp_cd(sources, weights, count, tmp_dst);
4554 }
4555 /* No interpolation, just copy highest weight source element's data. */
4556 else if (copy_cd) {
4557 copy_cd(sources[best_src_idx], tmp_dst, 1);
4558 }
4559 else {
4560 memcpy(tmp_dst, sources[best_src_idx], data_size);
4561 }
4562
4563 if (!(int(data_type) & CD_FAKE)) {
4564 CustomData_data_mix_value(eCustomDataType(data_type), tmp_dst, data_dst, mix_mode, mix_factor);
4565 }
4566 /* Else we can do nothing by default, needs custom interp func!
4567 * Note this is here only for sake of consistency, not expected to be used much actually? */
4568 else {
4569 if (mix_factor >= 0.5f) {
4570 memcpy(data_dst, tmp_dst, data_size);
4571 }
4572 }
4573
4574 MEM_delete_void(tmp_dst);
4575}
4576
4578 void *data_dst,
4579 const void **sources,
4580 const float *weights,
4581 const int count,
4582 const float mix_factor)
4583{
4584 BLI_assert(weights != nullptr);
4585 BLI_assert(count > 0);
4586
4587 const eCustomDataType data_type = eCustomDataType(laymap->data_type);
4588 BLI_assert(data_type == CD_NORMAL);
4589 const int mix_mode = laymap->mix_mode;
4590
4591 SpaceTransform *space_transform = static_cast<SpaceTransform *>(laymap->interp_data);
4592
4593 const LayerTypeInfo *type_info = layerType_getInfo(data_type);
4594 cd_interp interp_cd = type_info->interp;
4595
4596 float tmp_dst[3];
4597
4598 if (!sources) {
4599 /* Not supported here, abort. */
4600 return;
4601 }
4602
4603 interp_cd(sources, weights, count, tmp_dst);
4604 if (space_transform) {
4605 /* tmp_dst is in source space so far, bring it back in destination space. */
4606 BLI_space_transform_invert_normal(space_transform, tmp_dst);
4607 }
4608
4609 CustomData_data_mix_value(data_type, tmp_dst, data_dst, mix_mode, mix_factor);
4610}
4611
4613{
4614 MeshPairRemapItem *mapit = me_remap->items;
4615 const int totelem = me_remap->items_num;
4616
4617 const int data_type = laymap->data_type;
4618 GVArraySpan data_src_span;
4619 const void *data_src = [&]() -> const void * {
4620 if (std::holds_alternative<const void *>(laymap->data_src)) {
4621 return std::get<const void *>(laymap->data_src);
4622 }
4623 data_src_span = std::get<GVArray>(laymap->data_src);
4624 return data_src_span.data();
4625 }();
4626 void *data_dst = [&]() -> void * {
4627 if (std::holds_alternative<void *>(laymap->data_dst)) {
4628 return std::get<void *>(laymap->data_dst);
4629 }
4630 return std::get<GMutableVArraySpan>(laymap->data_dst).data();
4631 }();
4632
4633 size_t data_step;
4634 size_t data_size;
4635 size_t data_offset;
4636
4638
4639 size_t tmp_buff_size = 32;
4640 const void **tmp_data_src = nullptr;
4641
4642 /* NOTE: null data_src may happen and be valid (see vgroups...). */
4643 if (!data_dst) {
4644 return;
4645 }
4646
4647 if (data_src) {
4648 tmp_data_src = MEM_new_array_uninitialized<const void *>(tmp_buff_size, __func__);
4649 }
4650
4651 if (int(data_type) & CD_FAKE) {
4652 data_step = laymap->elem_size;
4653 data_size = laymap->data_size;
4654 data_offset = laymap->data_offset;
4655 }
4656 else {
4657 const LayerTypeInfo *type_info = layerType_getInfo(eCustomDataType(data_type));
4658
4659 /* NOTE: we can use 'fake' CDLayers for crease :/. */
4660 data_size = size_t(type_info->size);
4661 data_step = laymap->elem_size ? laymap->elem_size : data_size;
4662 data_offset = laymap->data_offset;
4663 }
4664
4666
4667 for (int i = 0; i < totelem; i++, data_dst = POINTER_OFFSET(data_dst, data_step), mapit++) {
4668 const int sources_num = mapit->sources_num;
4669 const float mix_factor = laymap->mix_factor *
4670 (laymap->mix_weights ? laymap->mix_weights[i] : 1.0f);
4671
4672 if (!sources_num) {
4673 /* No sources for this element, skip it. */
4674 continue;
4675 }
4676
4677 if (tmp_data_src) {
4678 if (sources_num > tmp_buff_size) [[unlikely]] {
4679 tmp_buff_size = size_t(sources_num);
4680 tmp_data_src = static_cast<const void **>(MEM_realloc_uninitialized(
4681 (void *)tmp_data_src, sizeof(*tmp_data_src) * tmp_buff_size));
4682 }
4683
4684 for (int j = 0; j < sources_num; j++) {
4685 const size_t src_idx = size_t(mapit->indices_src[j]);
4686 tmp_data_src[j] = POINTER_OFFSET(data_src, (data_step * src_idx) + data_offset);
4687 }
4688 }
4689
4690 interp(laymap,
4691 POINTER_OFFSET(data_dst, data_offset),
4692 tmp_data_src,
4693 mapit->weights_src,
4694 sources_num,
4695 mix_factor);
4696 }
4697
4698 if (laymap->tag_modified_fn) {
4699 laymap->tag_modified_fn();
4700 }
4701 if (std::holds_alternative<GMutableVArraySpan>(laymap->data_dst)) {
4702 std::get<GMutableVArraySpan>(laymap->data_dst).save();
4703 }
4704
4705 MEM_SAFE_DELETE(tmp_data_src);
4706}
4707
4709
4710/* -------------------------------------------------------------------- */
4713
4715 const char **r_struct_name,
4716 int *r_struct_num)
4717{
4718 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
4719
4720 *r_struct_name = typeInfo->structname;
4721 *r_struct_num = typeInfo->structnum;
4722}
4723
4725{
4726 using namespace blender::bke;
4727 for (const CustomDataLayer &layer : Span(data.layers, data.totlayer)) {
4728 if (layer.flag & CD_FLAG_NOCOPY) {
4729 continue;
4730 }
4731 /* Generic attribute types are stored in #AttributeStorage (except for BMesh but that is
4732 * runtime only). */
4734 layers_to_write.append(layer);
4735 layers_to_write.last().flag &= ~CD_FLAG_RUNTIME_NOINTERP;
4736 }
4737 data.totlayer = layers_to_write.size();
4738 data.maxlayer = data.totlayer;
4739 std::fill_n(data.typemap, CD_NUMTYPES, 0);
4740 data.totsize = 0;
4741 if (layers_to_write.is_empty()) {
4742 data.layers = nullptr;
4743 }
4744
4745 /* NOTE: `data->layers` may be null, this happens when adding
4746 * a legacy #MPoly struct to a mesh with no other face attributes.
4747 * This leaves us with no unique ID for DNA to identify the old
4748 * data with when loading the file. */
4749 if (!data.layers && layers_to_write.size() > 0) {
4750 /* We just need an address that's unique. */
4751 data.layers = reinterpret_cast<CustomDataLayer *>(&data.layers);
4752 }
4753}
4754
4755static void write_mdisps(BlendWriter *writer,
4756 const int count,
4757 const MDisps *mdlist,
4758 const int external)
4759{
4760 if (mdlist) {
4761 writer->write_struct_array(count, mdlist);
4762 for (int i = 0; i < count; i++) {
4763 const MDisps *md = &mdlist[i];
4764 if (md->disps) {
4765 if (!external) {
4766 writer->write_float3_array(md->totdisp, &md->disps[0][0]);
4767 }
4768 }
4769
4770 if (md->hidden) {
4771 writer->write_int8_array(BLI_BITMAP_SIZE(md->totdisp) * sizeof(BLI_bitmap),
4772 reinterpret_cast<const int8_t *>(md->hidden));
4773 }
4774 }
4775 }
4776}
4777
4779 int count,
4780 const GridPaintMask *grid_paint_mask)
4781{
4782 if (grid_paint_mask) {
4783 writer->write_struct_array(count, grid_paint_mask);
4784 for (int i = 0; i < count; i++) {
4785 const GridPaintMask *gpm = &grid_paint_mask[i];
4786 if (gpm->data) {
4787 const uint32_t gridsize = uint32_t(CCG_grid_size(gpm->level));
4788 writer->write_float_array(gridsize * gridsize, gpm->data);
4789 }
4790 }
4791 }
4792}
4793
4795 const CustomDataLayer &layer,
4796 const int count)
4797{
4798 switch (layer.type) {
4799 case CD_MDEFORMVERT:
4800 BKE_defvert_blend_write(writer, count, static_cast<const MDeformVert *>(layer.data));
4801 break;
4802 case CD_MDISPS:
4804 writer, count, static_cast<const MDisps *>(layer.data), layer.flag & CD_FLAG_EXTERNAL);
4805 break;
4806 case CD_PAINT_MASK:
4807 writer->write_float_array(count, static_cast<const float *>(layer.data));
4808 break;
4809 case CD_GRID_PAINT_MASK:
4810 write_grid_paint_mask(writer, count, static_cast<const GridPaintMask *>(layer.data));
4811 break;
4812 case CD_PROP_BOOL:
4813 BLI_STATIC_ASSERT(sizeof(bool) == sizeof(uint8_t),
4814 "bool type is expected to have the same size as uint8_t")
4815 writer->write_uint8_array(count, static_cast<const uint8_t *>(layer.data));
4816 break;
4817 default: {
4818 const char *structname;
4819 int structnum;
4820 get_type_file_write_info(eCustomDataType(layer.type), &structname, &structnum);
4821 if (structnum > 0) {
4822 int datasize = structnum * count;
4823 writer->write_struct_array_by_name(structname, datasize, layer.data);
4824 }
4825 else if (!writer->is_undo()) { /* Do not warn on undo. */
4826 printf("%s error: layer '%s':%d - can't be written to file\n",
4827 __func__,
4828 structname,
4829 layer.type);
4830 }
4831 }
4832 }
4833}
4834
4837 Span<CustomDataLayer> layers_to_write,
4838 int count,
4839 eCustomDataMask cddata_mask,
4840 ID *id)
4841{
4842 /* write external customdata (not for undo) */
4843 if (data->external && !writer->is_undo()) {
4844 CustomData_external_write(data, writer->filepath(), id, cddata_mask, count, 0);
4845 }
4846
4847 for (const CustomDataLayer &layer : layers_to_write) {
4848 const size_t size_in_bytes = CustomData_sizeof(eCustomDataType(layer.type)) * count;
4849 writer->write_shared(layer.data, size_in_bytes, layer.sharing_info, [&]() {
4850 blend_write_layer_data(writer, layer, count);
4851 });
4852 }
4853
4854 writer->write_struct_array_at_address(data->totlayer, data->layers, layers_to_write.data());
4855
4856 if (data->external) {
4857 writer->write_struct(data->external);
4858 }
4859}
4860
4862 const int count,
4863 MDisps *mdisps,
4864 const int external)
4865{
4866 if (mdisps) {
4867 for (int i = 0; i < count; i++) {
4868 MDisps &md = mdisps[i];
4869
4870 bool ok = true;
4871 if (md.disps) {
4872 ok &= BLO_read_array(reader, reinterpret_cast<float **>(&md.disps), md.totdisp, 3);
4873 }
4874 if (md.hidden) {
4875 ok &= BLO_read_array(reader,
4876 reinterpret_cast<int8_t **>(&md.hidden),
4877 BLI_BITMAP_SIZE(md.totdisp) * sizeof(BLI_bitmap));
4878 }
4879 if (!external && !ok) {
4880 md.totdisp = 0;
4881 }
4882
4883 if (!external && !md.disps) {
4884 md.totdisp = 0;
4885 }
4886 }
4887 }
4888}
4889
4891 int count,
4892 GridPaintMask *grid_paint_mask)
4893{
4894 if (grid_paint_mask) {
4895 for (int i = 0; i < count; i++) {
4896 GridPaintMask *gpm = &grid_paint_mask[i];
4897 if (gpm->data) {
4898 const int gridsize = CCG_grid_size(gpm->level);
4899 (void)BLO_read_array(reader, &gpm->data, int64_t(gridsize) * gridsize);
4900 }
4901 }
4902 }
4903}
4904
4905static void blend_read_layer_data(BlendDataReader *reader, CustomDataLayer &layer, const int count)
4906{
4907 /* Note: result of #BLO_read_array can be discarded because
4908 * #CustomData_layer_ensure_data_exists allocates the array if missing. */
4909 switch (layer.type) {
4910 case CD_MDEFORMVERT:
4911 if (BLO_read_array(reader, reinterpret_cast<MDeformVert **>(&layer.data), count)) {
4912 BKE_defvert_blend_read(reader, count, static_cast<MDeformVert *>(layer.data));
4913 }
4914 break;
4915 case CD_MDISPS:
4916 if (BLO_read_array(reader, reinterpret_cast<MDisps **>(&layer.data), count)) {
4918 reader, count, static_cast<MDisps *>(layer.data), layer.flag & CD_FLAG_EXTERNAL);
4919 }
4920 break;
4921 case CD_PAINT_MASK:
4922 (void)BLO_read_array(reader, reinterpret_cast<float **>(&layer.data), count);
4923 break;
4924 case CD_GRID_PAINT_MASK:
4925 if (BLO_read_array(reader, reinterpret_cast<GridPaintMask **>(&layer.data), count)) {
4926 blend_read_paint_mask(reader, count, static_cast<GridPaintMask *>(layer.data));
4927 }
4928 break;
4929 case CD_PROP_BOOL:
4930 BLI_STATIC_ASSERT(sizeof(bool) == sizeof(uint8_t),
4931 "bool type is expected to have the same size as uint8_t")
4932 (void)BLO_read_array(reader, reinterpret_cast<uint8_t **>(&layer.data), count);
4933 break;
4934 default: {
4935 const char *structname;
4936 int structnum;
4937 get_type_file_write_info(eCustomDataType(layer.type), &structname, &structnum);
4938 if (structnum > 0) {
4939 const int data_num = structnum * count;
4940 layer.data = BLO_read_struct_by_name_array(reader, structname, data_num, layer.data);
4941 }
4942 else {
4943 /* Can happen with deprecated types of customdata. */
4944 const size_t elem_size = CustomData_sizeof(eCustomDataType(layer.type));
4945 (void)BLO_read_array(
4946 reader, reinterpret_cast<std::byte **>(&layer.data), count, elem_size);
4947 }
4948 }
4949 }
4950
4952 /* Under normal operations, this shouldn't happen, but...
4953 * For a CD_PROP_BOOL example, see #84935.
4954 * For a CD_MLOOPUV example, see #90620. */
4955 CLOG_WARN(&LOG,
4956 "Allocated custom data layer that was not saved correctly for layer.type = %d.",
4957 layer.type);
4958 }
4959}
4960
4962{
4963 BLO_read_array_and_validate_size(reader, &data->layers, &data->totlayer);
4964
4965 /* Annoying workaround for bug #31079 loading legacy files with
4966 * no polygons _but_ have stale custom-data. */
4967 if (count == 0 && data->layers == nullptr && data->totlayer != 0) [[unlikely]] {
4969 return;
4970 }
4971 /* There was a short time (Blender 500 sub 33) where the custom data struct was saved in an
4972 * invalid state (see @11d2f48882). This check is unfortunate, but avoids crashing when trying to
4973 * load the invalid data (see e.g. #143720). */
4974 if (data->layers == nullptr && data->totlayer != 0) [[unlikely]] {
4976 return;
4977 }
4978
4979 BLO_read_struct(reader, CustomDataExternal, &data->external);
4980
4981 int i = 0;
4982 while (i < data->totlayer) {
4983 CustomDataLayer *layer = &data->layers[i];
4984
4985 if (layer->flag & CD_FLAG_EXTERNAL) {
4986 layer->flag &= ~CD_FLAG_IN_MEMORY;
4987 }
4989 layer->sharing_info = nullptr;
4990
4992 layer->sharing_info = BLO_read_shared(
4993 reader, &layer->data, [&]() -> const ImplicitSharingInfo * {
4994 blend_read_layer_data(reader, *layer, count);
4995 if (layer->data == nullptr) {
4996 return nullptr;
4997 }
4999 eCustomDataType(layer->type), layer->data, count);
5000 });
5001 i++;
5002 }
5003 }
5004
5005 /* Ensure allocated size is set to the size of the read array. While this should always be the
5006 * case (see #CustomData_blend_write_prepare), there can be some corruption in rare cases (e.g.
5007 * files saved between ff3d535bc2a63092 and 945f32e66d6ada2a). */
5008 data->maxlayer = data->totlayer;
5009
5011}
5012
5014
5015/* -------------------------------------------------------------------- */
5018
5019#ifndef NDEBUG
5020
5021void CustomData_debug_info_from_layers(const CustomData *data, const char *indent, DynStr *dynstr)
5022{
5023 for (eCustomDataType type = eCustomDataType{}; type < CD_NUMTYPES;
5024 type = eCustomDataType(type + 1))
5025 {
5026 if (CustomData_has_layer(data, type)) {
5027 /* NOTE: doesn't account for multiple layers. */
5028 const char *name = CustomData_layertype_name(type);
5029 const int size = CustomData_sizeof(type);
5030 const void *pt = CustomData_get_layer(data, type);
5031 const char *structname;
5032 int structnum;
5033 get_type_file_write_info(type, &structname, &structnum);
5034 BLI_dynstr_appendf(dynstr,
5035 "%sdict(name='%s', struct='%s', type=%d, ptr='%p', elem=%d),\n",
5036 indent,
5037 name,
5038 structname,
5039 type,
5040 pt,
5041 size);
5042 }
5043 }
5044}
5045
5046#endif /* !NDEBUG */
5047
5049
5050namespace bke {
5051
5052/* -------------------------------------------------------------------- */
5055
5056std::optional<VolumeGridType> custom_data_type_to_volume_grid_type(const eCustomDataType type)
5057{
5058 switch (type) {
5059 case CD_PROP_FLOAT:
5060 return VOLUME_GRID_FLOAT;
5061 case CD_PROP_FLOAT3:
5063 case CD_PROP_INT32:
5064 return VOLUME_GRID_INT;
5065 case CD_PROP_BOOL:
5066 return VOLUME_GRID_BOOLEAN;
5067 default:
5068 return std::nullopt;
5069 }
5070}
5071
5072std::optional<eCustomDataType> volume_grid_type_to_custom_data_type(const VolumeGridType type)
5073{
5074 switch (type) {
5075 case VOLUME_GRID_FLOAT:
5076 return CD_PROP_FLOAT;
5078 return CD_PROP_FLOAT3;
5079 case VOLUME_GRID_INT:
5080 return CD_PROP_INT32;
5082 return CD_PROP_BOOL;
5083 default:
5084 return std::nullopt;
5085 }
5086}
5087
5089
5090} // namespace bke
5091
5093{
5094 return LAYERTYPEINFO[layer->type].size;
5095}
5096
5097void CustomData_count_memory(const CustomData &data, const int totelem, MemoryCounter &memory)
5098{
5099 for (const CustomDataLayer &layer : Span{data.layers, data.totlayer}) {
5100 memory.add_shared(layer.sharing_info, [&](MemoryCounter &shared_memory) {
5101 /* Not quite correct for all types, but this is only a rough approximation anyway. */
5102 const int64_t elem_size = CustomData_get_elem_size(&layer);
5103 shared_memory.add(totelem * elem_size);
5104 });
5105 }
5106}
5107
5108} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define UV_PINNED_NAME
#define CDF_TYPE_MESH
support for deformation groups and hooks.
#define BLI_BITMAP_SIZE(_num)
Definition BLI_bitmap.hh:34
A dynamically sized string ADT.
#define FILE_MAX
#define STR_ELEM(...)
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
Read Blender memory allocation and freeing.
#define PRF_scope(category)
#define INIT_MINMAX2(min, max)
#define BLI_assert_unreachable()
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define CD_MASK_BM_ELEM_PYPTR
#define MAX_CUSTOMDATA_LAYER_NAME
#define CD_MASK_NORMAL
#define CD_MASK_PROP_FLOAT3
#define CD_MASK_ORIGINDEX
#define CD_MASK_MCOL
#define CD_MASK_ORCO
#define CD_MASK_MDEFORMVERT
#define CD_MASK_TESSLOOPNORMAL
#define CD_MASK_PROP_ALL
#define CD_MASK_PROP_INT32_2D
#define CD_MASK_SHAPE_KEYINDEX
#define CD_MASK_MTFACE
#define CD_MASK_ORIGSPACE_MLOOP
#define CD_MASK_CLOTH_ORCO
#define CD_MASK_PROP_INT32
#define CD_MASK_GRID_PAINT_MASK
#define MEM_realloc_uninitialized(vmemh, len)
#define CD_MASK_SHAPEKEY
#define CD_MASK_MFACE
#define MAX_MTFACE
#define CD_MASK_MDISPS
#define CD_MASK_ORIGSPACE
#define CD_MASK_MLOOPTANGENT
#define MAX_CUSTOMDATA_LAYER_NAME_NO_PREFIX
btScalar max
CustomDataLayerImplicitSharing(const void *data, const int totelem, const eCustomDataType type)
const void * data() const
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr const char * c_str() const
constexpr StringRef()
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
void add_shared(const ImplicitSharingInfo *sharing_info, const FunctionRef< void(MemoryCounter &shared_memory)> count_fn)
#define SOURCE_BUF_SIZE
#define CUSTOMDATA_GROW
Definition customdata.cc:77
#define FLT_MAX
#define FLT_MIN
#define layer
#define round
MatBase< 4, 4 > float4x4
const void * ImplicitSharingInfo
#define PRIx64
Definition inttypes.h:133
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
typename DefaultMixerStruct< T >::type DefaultMixer
void free(Subdiv *subdiv)
Definition subdiv.cc:192
bool attribute_name_is_anonymous(const StringRef name)
std::optional< VolumeGridType > custom_data_type_to_volume_grid_type(eCustomDataType type)
std::optional< eCustomDataType > volume_grid_type_to_custom_data_type(VolumeGridType type)
QuaternionBase< float > Quaternion
T interpolate(const T &a, const T &b, const FactorT &t)
void min_max(const T &value, T &min, T &max)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
BMesh Iterators.
void CustomData_MeshMasks_update(CustomData_MeshMasks *mask_dst, const CustomData_MeshMasks *mask_src)
Definition customdata.cc:89
bool CustomData_external_test(CustomData *data, eCustomDataType type)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ CDT_MIX_REPLACE_ABOVE_THRESHOLD
@ CDT_MIX_REPLACE_BELOW_THRESHOLD
MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
bool CustomData_layer_has_interp(const CustomData *data, int layer_n)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void layerDefault_propcol(void *data, const int count)
static void layerFree_grid_paint_mask(void *data, const int count)
void cdf_write_close(CDataFile *cdf)
int CustomData_layertype_layers_max(eCustomDataType type)
bool CustomData_merge_layout(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
bool cdf_read_data(CDataFile *cdf, unsigned int size, void *data)
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
static void layerSwap_flnor(void *data, const int *corner_indices)
__int64 int64_t
Definition BKE_text.h:90
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
static void layerConstruct_mdeformvert(void *data, const int count)
const ImplicitSharingInfo * BLO_read_shared(BlendDataReader *reader, T **data_ptr, FunctionRef< const ImplicitSharingInfo *()> read_fn)
static void layerCopyValue_mloop_origspace(const void *source, void *dest, const int, const float)
static void layerConstruct_mdisps(void *data, const int count)
char * STRNCPY(char(&dst)[N], const char *src)
void * CustomData_bmesh_get(const CustomData *data, void *block, eCustomDataType type)
void(*)(void *data, int count) cd_free
static void layerCopy_origspace_face(const void *source, void *dest, const int count)
void CustomData_bmesh_alloc_block(CustomData *data, void **block)
const CustomData_MeshMasks CD_MASK_BAREMESH
void CustomData_bmesh_set_n(CustomData *data, void *block, eCustomDataType type, int n, const void *source)
bool CustomData_free_layer_active(CustomData *data, eCustomDataType type)
static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
static bool customdata_merge_internal(const CustomData *source, CustomData *dest, const eCustomDataMask mask, const std::optional< eCDAllocType > alloctype, const int totelem)
void CustomData_swap_corners(CustomData *data, int index, const int *corner_indices)
void CustomData_copy_elements(eCustomDataType type, const void *src_data, void *dst_data, int count)
void CustomData_data_mix_value(eCustomDataType type, const void *source, void *dest, int mixmode, float mixfactor)
static void layerInterp_mloopcol(const void **sources, const float *weights, int count, void *dest)
static int CustomData_get_layer_index__notypemap(const CustomData *data, const eCustomDataType type)
static bool cd_layer_find_dupe(CustomData *data, const StringRef name, const eCustomDataType type, const int index)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void * BLO_read_struct_by_name_array(BlendDataReader *reader, StringRef struct_name, int64_t items_num, const void *old_address)
Definition readfile.cc:6521
bool cdf_write_layer(CDataFile *cdf, CDataFileLayer *blay)
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
void * CustomData_get_layer_n_for_write(CustomData *data, eCustomDataType type, int n, int totelem)
static void ensure_layer_data_is_mutable(CustomDataLayer &layer, const int totelem)
bool CustomData_free_layers(CustomData *data, eCustomDataType type)
void CustomData_realloc(CustomData *data, int old_size, int new_size, eCDAllocType alloctype=CD_CONSTRUCT)
void CustomData_free_elem(CustomData *data, int index, int count)
static const char * layerType_getName(const eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static void layerDoMinMax_propfloat2(const void *data, void *vmin, void *vmax)
static void layerDefault_mloopcol(void *data, const int count)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void blend_color_add_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
const void * CustomData_add_layer_named_with_data(CustomData *data, eCustomDataType type, void *layer_data, int totelem, StringRef name, const ImplicitSharingInfo *sharing_info)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
MINLINE void mul_v2_fl(float r[2], float f)
void(*)(const void *source, void *dest, int count) cd_copy
void CustomData_external_add(CustomData *data, ID *id, eCustomDataType type, int totelem, const char *filepath)
static void layerMultiply_mloop_origspace(void *data, const float fac)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void customData_update_offsets(CustomData *data)
BMCustomDataCopyMap CustomData_bmesh_copy_map_calc(const CustomData &src, const CustomData &dst, eCustomDataMask mask_exclude=0)
static void layerCopyValue_mloopcol(const void *source, void *dest, const int mixmode, const float mixfactor)
bool CustomData_has_interp(const CustomData *data)
static void layerInterp_propFloat(const void **sources, const float *weights, const int count, void *dest)
static void layerInterp_propcol(const void **sources, const float *weights, int count, void *dest)
static void layerInterp_propbool(const void **sources, const float *weights, int count, void *dest)
void CustomData_bmesh_interp(CustomData *data, const void **src_blocks, const float *weights, int count, void *dst_block)
bool CustomData_MeshMasks_are_matching(const CustomData_MeshMasks *mask_ref, const CustomData_MeshMasks *mask_required)
Definition customdata.cc:99
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
static void layerDefault_tface(void *data, const int count)
static bool CustomData_layer_ensure_data_exists(CustomDataLayer *layer, size_t count)
void BKE_defvert_blend_read(BlendDataReader *reader, int count, MDeformVert *mdverts)
Definition deform.cc:1621
const ImplicitSharingInfo * CustomData_make_layer_sharing_info(eCustomDataType type, const void *data, int totelem)
static void layerDoMinMax_mloopcol(const void *data, void *vmin, void *vmax)
static void get_type_file_write_info(const eCustomDataType type, const char **r_struct_name, int *r_struct_num)
void BKE_defvert_blend_write(BlendWriter *writer, int count, const MDeformVert *dvlist)
Definition deform.cc:1604
MINLINE void blend_color_sub_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
MINLINE void copy_v3_v3_short(short r[3], const short a[3])
size_t CustomData_get_elem_size(const CustomDataLayer *layer)
MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
bool CustomData_bmesh_merge_layout(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, BMesh *bm, char htype)
static void layerInterp_propfloat4(const void **sources, const float *weights, int count, void *dest)
bool CustomData_data_equals(eCustomDataType type, const void *data1, const void *data2)
static bool layerRead_mdisps(CDataFile *cdf, void *data, const int count)
static void layerCopy_mdeformvert(const void *source, void *dest, const int count)
MINLINE void copy_v4_fl(float r[4], float f)
static size_t layerFilesize_mdisps(CDataFile *, const void *data, const int count)
static void write_mdisps(BlendWriter *writer, const int count, const MDisps *mdlist, const int external)
static void layerMultiply_propfloat4(void *data, const float fac)
static void layerInterp_mcol(const void **sources, const float *weights, const int count, void *dest)
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
int CustomData_sizeof(eCustomDataType type)
bool CustomData_free_layer_named(CustomData *data, StringRef name)
void CustomData_bmesh_copy_block(CustomData &data, void *src_block, void **dst_block)
static void layerCopyValue_propfloat2(const void *source, void *dest, const int mixmode, const float mixfactor)
static int layerMaxNum_tface()
MatBase< float, 4, 4 > float4x4
static void layerCopy_bmesh_elem_py_ptr(const void *, void *dest, const int count)
static void layerInterp_propInt(const void **sources, const float *weights, const int count, void *dest)
void(*)(const void **sources, const float *weights, int count, void *dest) cd_interp
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
int CustomData_get_active_layer_index(const CustomData *data, eCustomDataType type)
bool cdf_write_data(CDataFile *cdf, unsigned int size, const void *data)
void cdf_free(CDataFile *cdf)
void MEM_delete_void(void *vmemh)
constexpr std::enable_if_t< blenlib_internal::IsBoundedArray< T >::value, size_t > BOUNDED_ARRAY_TYPE_SIZE() noexcept
static void layerCopyValue_normal(const void *source, void *dest, const int mixmode, const float mixfactor)
static void layerInterp_mloop_origspace(const void **sources, const float *weights, int count, void *dest)
static void layerMultiply_propfloat2(void *data, const float fac)
static void layerMultiply_mloopcol(void *data, const float fac)
static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES]
void CustomData_external_reload(CustomData *data, ID *id, eCustomDataMask mask, int totelem)
static void layerInterp_mdeformvert(const void **sources, const float *weights, const int count, void *dest)
MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const float src2[4])
bool cdf_read_open(CDataFile *cdf, const char *filepath)
static void blend_write_layer_data(BlendWriter *writer, const CustomDataLayer &layer, const int count)
static void layerInterp_normal(const void **sources, const float *weights, const int count, void *dest)
const BMAllocTemplate bm_mesh_chunksize_default
Definition bmesh_mesh.cc:27
static bool layerEqual_propcol(const void *data1, const void *data2)
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
static const char * LAYERTYPENAMES[CD_NUMTYPES]
void BLI_mempool_free(BLI_mempool *pool, void *addr) ATTR_NONNULL(1
uint64_t eCustomDataMask
static CustomDataLayer * customData_add_layer__internal(CustomData *data, eCustomDataType type, std::optional< eCDAllocType > alloctype, void *layer_data_to_assign, const ImplicitSharingInfo *sharing_info_to_assign, int totelem, const StringRef name)
VecBase< float, 4 > float4
static void layerAdd_mloopcol(void *data1, const void *data2)
const char * CustomData_get_render_layer_name(const CustomData *data, eCustomDataType type)
static void layerInterp_propfloat2(const void **sources, const float *weights, int count, void *dest)
static void layerAdd_propfloat2(void *data1, const void *data2)
static bool layerValidate_propFloat(void *data, const uint totitems, const bool do_fixes)
void * CustomData_get_n_for_write(CustomData *data, eCustomDataType type, int index, int n, int totelem)
static void CustomData_bmesh_set_default_n(CustomData *data, void **block, const int n)
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void CustomData_interp(const CustomData *source, CustomData *dest, const int *src_indices, const float *weights, int count, int dest_index)
int CustomData_get_render_layer(const CustomData *data, eCustomDataType type)
static void customData_resize(CustomData *data, const int grow_amount)
void CustomData_set_layer_render_index(CustomData *data, eCustomDataType type, int n)
unsigned int uint32_t
Definition BKE_text.h:81
void customdata_data_transfer_interp_normal_normals(const CustomDataTransferLayerMap *laymap, void *data_dst, const void **sources, const float *weights, const int count, const float mix_factor)
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], const float src2[4], float t)
static void layerDefault_mcol(void *data, const int count)
const void * CustomData_add_layer_with_data(CustomData *data, eCustomDataType type, void *layer_data, int totelem, const ImplicitSharingInfo *sharing_info)
void CustomData_bmesh_free_block(CustomData *data, void **block)
void(*)(void *data, int count) cd_set_default_value
void CustomData_bmesh_set_default(CustomData *data, void **block)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
bool CustomData_layertype_is_singleton(eCustomDataType type)
VecBase< int32_t, 2 > int2
void CustomData_init_layout_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
VecBase< float, 2 > float2
int multires_mdisp_corners(const MDisps *s)
Definition multires.cc:694
static void layerFree_mdisps(void *data, const int count)
void CustomData_data_set_default_value(eCustomDataType type, void *elem)
MINLINE void add_v2_v2(float r[2], const float a[2])
unsigned char uchar
Definition BKE_text.h:58
void CustomData_bmesh_interp_n(CustomData *data, const void **src_blocks, const float *weights, int count, void *dst_block_ofs, int n)
static void layerInterp_propfloat3(const void **sources, const float *weights, int count, void *dest)
static void customdata_data_transfer_interp_generic(const CustomDataTransferLayerMap *laymap, void *data_dst, const void **sources, const float *weights, const int count, const float mix_factor)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
void CustomData_reset(CustomData *data)
MINLINE void add_v4_v4(float r[4], const float a[4])
void CustomData_blend_write(BlendWriter *writer, CustomData *data, Span< CustomDataLayer > layers_to_write, int count, eCustomDataMask cddata_mask, ID *id)
static void CustomData_external_free(CustomData *data)
BMesh const char void * data
static void layerInterp_shapekey(const void **sources, const float *weights, int count, void *dest)
bool CustomData_bmesh_has_free(const CustomData *data)
@ VOLUME_GRID_VECTOR_FLOAT
void CustomData_data_copy_value(eCustomDataType type, const void *source, void *dest)
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int elem_num, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
void CustomData_set_layer_flag(CustomData *data, eCustomDataType type, eCustomDataLayer_Flag flag)
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, StringRef name)
static void layerSwap_tface(void *data, const int *corner_indices)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
unsigned char uint8_t
Definition BKE_text.h:79
static void blend_read_mdisps(BlendDataReader *reader, const int count, MDisps *mdisps, const int external)
const CustomData_MeshMasks CD_MASK_BMESH
static void layerInterp_propquaternion(const void **sources, const float *weights, int count, void *dest)
static bool layerValidate_propfloat2(void *data, const uint totitems, const bool do_fixes)
static int customData_pad_up_to_alignment(const int value, const int alignment)
bool cdf_read_layer(CDataFile *cdf, const CDataFileLayer *blay)
void CustomData_data_multiply(eCustomDataType type, void *data, float fac)
static void * copy_layer_data(const eCustomDataType type, const void *data, const int totelem)
CDataFileLayer * cdf_layer_find(CDataFile *cdf, int type, const char *name)
CDataFileLayer * cdf_layer_add(CDataFile *cdf, int type, const char *name, size_t datasize)
static void customdata_external_filename(char filepath[FILE_MAX], StringRefNull basepath, ID *id, CustomDataExternal *external)
void CustomData_blend_write_prepare(CustomData &data, Vector< CustomDataLayer, 16 > &layers_to_write)
void CustomData_ensure_data_is_mutable(CustomDataLayer *layer, int totelem)
static void layerDoMinMax_propcol(const void *data, void *vmin, void *vmax)
bool CustomData_free_layer(CustomData *data, eCustomDataType type, int index)
eCustomDataMask CD_TYPE_AS_MASK(eCustomDataType type)
static bool layerEqual_mloopcol(const void *data1, const void *data2)
static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
static void layerDoMinMax_mloop_origspace(const void *data, void *vmin, void *vmax)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
void minmax_v4v4_v4(float min[4], float max[4], const float vec[4])
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
int CustomData_number_of_anonymous_layers(const CustomData *data, eCustomDataType type)
bool CustomData_verify_versions(CustomData *data, int index)
bool CustomData_merge(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
MINLINE void zero_v3(float r[3])
CDataFile * cdf_create(int type)
void CustomData_external_remove(CustomData *data, ID *id, eCustomDataType type, int totelem)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
static bool customdata_typemap_is_valid(const CustomData *data)
static void write_grid_paint_mask(BlendWriter *writer, int count, const GridPaintMask *grid_paint_mask)
static bool layer_is_mutable(CustomDataLayer &layer)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static void layerCopy_propString(const void *source, void *dest, const int count)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], float t)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void CustomData_set_layer_render(CustomData *data, eCustomDataType type, int n)
const void * CustomData_get_layer_n(const CustomData *data, eCustomDataType type, int n)
const CustomData_MeshMasks CD_MASK_DERIVEDMESH
void customData_mask_layers__print(const CustomData_MeshMasks *mask)
MINLINE float normalize_v3(float n[3])
void CustomData_count_memory(const CustomData &data, int totelem, MemoryCounter &memory)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
int CustomData_get_layer_index_n(const CustomData *data, eCustomDataType type, int n)
int CustomData_name_maxncpy_calc(StringRef name)
static bool layerValidate_propfloat3(void *data, const uint totitems, const bool do_fixes)
MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const float src2[4])
static void layerInitMinMax_propcol(void *vmin, void *vmax)
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
static void layerInterp_tface(const void **sources, const float *weights, const int count, void *dest)
const CustomData_MeshMasks CD_MASK_MESH
VecBase< int16_t, 2 > short2
void CustomData_free(CustomData *data)
static void layerDefault_propfloat4x4(void *data, const int count)
const char * CustomData_layertype_name(eCustomDataType type)
void CustomData_update_typemap(CustomData *data)
@ BLI_MEMPOOL_NOP
const char * CustomData_get_layer_name(const CustomData *data, eCustomDataType type, int n)
int CustomData_get_render_layer_index(const CustomData *data, eCustomDataType type)
static void layerCopy_propFloat(const void *source, void *dest, const int count)
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, StringRef name, int totelem)
void * CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
static void layerFree_mdeformvert(void *data, const int count)
int CCG_grid_size(const int level)
Definition BKE_ccg.hh:106
static void layerInitMinMax_propfloat2(void *vmin, void *vmax)
void CustomData_bmesh_init_pool(CustomData *data, int totelem, char htype)
void CustomData_set_layer_active(CustomData *data, eCustomDataType type, int n)
void CustomData_set_layer_unique_name(CustomData *data, int index)
void CustomData_bmesh_free_block_data(CustomData *data, void *block)
static void blend_read_layer_data(BlendDataReader *reader, CustomDataLayer &layer, const int count)
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
static void layerConstruct_grid_paint_mask(void *data, const int count)
MINLINE unsigned char round_fl_to_uchar_clamp(float a)
static CLG_LogRef LOG
Definition logging.cpp:12
void(*)(const CustomDataTransferLayerMap *laymap, void *dest, const void **sources, const float *weights, int count, float mix_factor) cd_datatransfer_interp
static void layerSwap_origspace_face(void *data, const int *corner_indices)
static void layerInterp_origspace_face(const void **sources, const float *weights, const int count, void *dest)
static void layerAdd_mloop_origspace(void *data1, const void *data2)
static void layerSwap_mdisps(void *data, const int *ci)
static bool layerWrite_mdisps(CDataFile *cdf, const void *data, const int count)
void CustomData_copy_layer_type_data(const CustomData *source, CustomData *destination, eCustomDataType type, int source_index, int destination_index, int count)
bool cdf_write_open(CDataFile *cdf, const char *filepath)
void * BLI_mempool_alloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
void CustomData_debug_info_from_layers(const CustomData *data, const char *indent, DynStr *dynstr)
static bool layerEqual_mloop_origspace(const void *data1, const void *data2)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void CustomData_set_layer_active_index(CustomData *data, eCustomDataType type, int n)
bool(*)(void *item, uint totitems, bool do_fixes) cd_validate
static void layerDefault_origindex(void *data, const int count)
static void layerMultiply_propcol(void *data, const float fac)
static bool layerEqual_propfloat2(const void *data1, const void *data2)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
void * CustomData_get_for_write(CustomData *data, int index, eCustomDataType type, int totelem)
static void layerDefault_origspace_face(void *data, const int count)
bool CustomData_has_math(const CustomData *data)
MINLINE void mul_v4_fl(float r[4], float f)
VecBase< float, 3 > float3
void CustomData_ensure_layers_are_mutable(CustomData *data, int totelem)
static void layerCopy_mdisps(const void *source, void *dest, const int count)
int CustomData_get_named_layer_index_notype(const CustomData *data, StringRef name)
const CustomData_MeshMasks CD_MASK_EVERYTHING
MINLINE void mul_v3_v3v3(float r[3], const float v1[3], const float v2[3])
void CustomData_copy_data_layer(const CustomData *source, CustomData *dest, int src_layer_index, int dst_layer_index, int src_index, int dst_index, int count)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void CustomData_external_read(CustomData *data, StringRefNull basepath, ID *id, eCustomDataMask mask, int totelem)
static void free_layer_data(const eCustomDataType type, const void *data, const int totelem)
bool CustomData_set_layer_name(CustomData *data, eCustomDataType type, int n, StringRef name)
static void layerCopyValue_propcol(const void *source, void *dest, const int mixmode, const float mixfactor)
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, StringRef name)
static void layerAdd_propcol(void *data1, const void *data2)
void cdf_read_close(CDataFile *cdf)
const CustomData_MeshMasks CD_MASK_BAREMESH_ORIGINDEX
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
void CustomData_data_initminmax(eCustomDataType type, void *min, void *max)
static void layerFree_bmesh_elem_py_ptr(void *data, const int count)
MINLINE void zero_v2(float r[2])
int CustomData_number_of_layers_typemask(const CustomData *data, eCustomDataMask mask)
static void layerSwap_mcol(void *data, const int *corner_indices)
MINLINE void blend_color_interpolate_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], float ft)
static void layerMultiply_propfloat3(void *data, const float fac)
void CustomData_set_only_copy(const CustomData *data, eCustomDataMask mask)
void CustomData_data_add(eCustomDataType type, void *data1, const void *data2)
void bpy_bm_generic_invalidate(struct BPy_BMGeneric *)
static void layerDefault_propquaternion(void *data, const int count)
static void layerCopy_grid_paint_mask(const void *source, void *dest, const int count)
bool CustomData_layer_has_math(const CustomData *data, int layer_n)
static void layerAdd_propfloat4(void *data1, const void *data2)
void CustomData_data_transfer(const MeshPairRemap *me_remap, CustomDataTransferLayerMap *laymap)
MINLINE void blend_color_mul_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
signed char int8_t
Definition BKE_text.h:76
static void layerCopy_tface(const void *source, void *dest, const int count)
void CustomData_data_dominmax(eCustomDataType type, const void *data, void *min, void *max)
bool CustomData_layertype_is_dynamic(eCustomDataType type)
void BLI_space_transform_invert_normal(const struct SpaceTransform *data, float no[3])
static void customData_free_layer__internal(CustomDataLayer *layer)
void CustomData_external_write(CustomData *data, StringRefNull basepath, ID *id, eCustomDataMask mask, int totelem, int free)
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)
static void layerAdd_propfloat3(void *data1, const void *data2)
void * CustomData_bmesh_get_n(const CustomData *data, void *block, eCustomDataType type, int n)
void CustomData_blend_read(BlendDataReader *reader, CustomData *data, int count)
static void blend_read_paint_mask(BlendDataReader *reader, int count, GridPaintMask *grid_paint_mask)
static const LayerTypeInfo * layerType_getInfo(const eCustomDataType type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
static Quaternion identity()
Vector< TrivialDefault > trivial_defaults
Vector< TrivialCopy > trivial_copies
void write_struct_array_by_name(const char *struct_name, int64_t array_size, const void *data, BlendStructWriterFn fn=nullptr)
void write_shared(const void *data, size_t approximate_size_in_bytes, const ImplicitSharingInfo *sharing_info, FunctionRef< void()> write_fn)
void write_uint8_array(int64_t num, const uint8_t *data)
void write_int8_array(int64_t num, const int8_t *data)
void write_float_array(int64_t num, const float *data)
void write_float3_array(int64_t num, const float *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
void write_struct_array_at_address(const int64_t array_size, const void *address, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
StringRefNull filepath() const
const ImplicitSharingInfoHandle * sharing_info
std::function< void()> tag_modified_fn
std::variant< const void *, GVArray > data_src
std::variant< void *, GMutableVArraySpan > data_dst
struct BLI_mempool * pool
CustomDataExternal * external
bool(*) read(CDataFile *cdf, void *data, int count)
const char * structname
void(*) construct(void *data, int count)
const char * defaultname
void(*) swap(void *data, const int *corner_indices)
void(*) initminmax(void *min, void *max)
void(*) add(void *data1, const void *data2)
bool(*) equal(const void *data1, const void *data2)
void(*) copyvalue(const void *source, void *dest, int mixmode, const float mixfactor)
void(*) multiply(void *data, float fac)
bool(*) write(CDataFile *cdf, const void *data, int count)
cd_set_default_value set_default_value
size_t(*) filesize(CDataFile *cdf, const void *data, int count)
void(*) dominmax(const void *data1, void *min, void *max)
struct MDeformWeight * dw
unsigned int * hidden
MeshPairRemapItem * items
#define N_(msgid)