Blender  V3.3
DerivedMesh.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include <climits>
9 #include <cstring>
10 
11 #include "MEM_guardedalloc.h"
12 
13 #include "DNA_cloth_types.h"
14 #include "DNA_customdata_types.h"
15 #include "DNA_key_types.h"
16 #include "DNA_material_types.h"
17 #include "DNA_mesh_types.h"
18 #include "DNA_meshdata_types.h"
19 #include "DNA_object_types.h"
20 #include "DNA_scene_types.h"
21 
22 #include "BLI_array.h"
23 #include "BLI_bitmap.h"
24 #include "BLI_blenlib.h"
25 #include "BLI_linklist.h"
26 #include "BLI_math.h"
27 #include "BLI_math_vec_types.hh"
28 #include "BLI_task.h"
29 #include "BLI_task.hh"
30 #include "BLI_utildefines.h"
31 #include "BLI_vector.hh"
32 
33 #include "BKE_DerivedMesh.h"
34 #include "BKE_bvhutils.h"
35 #include "BKE_colorband.h"
36 #include "BKE_deform.h"
37 #include "BKE_editmesh.h"
38 #include "BKE_geometry_set.hh"
40 #include "BKE_key.h"
41 #include "BKE_layer.h"
42 #include "BKE_lib_id.h"
43 #include "BKE_material.h"
44 #include "BKE_mesh.h"
45 #include "BKE_mesh_iterators.h"
46 #include "BKE_mesh_mapping.h"
47 #include "BKE_mesh_runtime.h"
48 #include "BKE_mesh_tangent.h"
49 #include "BKE_mesh_wrapper.h"
50 #include "BKE_modifier.h"
51 #include "BKE_multires.h"
52 #include "BKE_object.h"
53 #include "BKE_object_deform.h"
54 #include "BKE_paint.h"
55 #include "BKE_subdiv_modifier.h"
56 
57 #include "BLI_sys_types.h" /* for intptr_t support */
58 
59 #include "BKE_shrinkwrap.h"
60 #include "DEG_depsgraph.h"
61 #include "DEG_depsgraph_query.h"
62 
63 #include "CLG_log.h"
64 
65 #ifdef WITH_OPENSUBDIV
66 # include "DNA_userdef_types.h"
67 #endif
68 
69 using blender::float3;
71 
72 /* very slow! enable for testing only! */
73 //#define USE_MODIFIER_VALIDATE
74 
75 #ifdef USE_MODIFIER_VALIDATE
76 # define ASSERT_IS_VALID_MESH(mesh) \
77  (BLI_assert((mesh == nullptr) || (BKE_mesh_is_valid(mesh) == true)))
78 #else
79 # define ASSERT_IS_VALID_MESH(mesh)
80 #endif
81 
82 static ThreadRWMutex loops_cache_lock = PTHREAD_RWLOCK_INITIALIZER;
83 
84 static void mesh_init_origspace(Mesh *mesh);
85 static void editbmesh_calc_modifier_final_normals(Mesh *mesh_final,
86  const CustomData_MeshMasks *final_datamask);
88  Mesh *mesh_final, const CustomData_MeshMasks *final_datamask);
89 
90 /* -------------------------------------------------------------------- */
91 
93 {
95 
96  if (!mvert) {
97  mvert = (MVert *)CustomData_add_layer(
98  &dm->vertData, CD_MVERT, CD_CALLOC, nullptr, dm->getNumVerts(dm));
100  dm->copyVertArray(dm, mvert);
101  }
102 
103  return mvert;
104 }
105 
107 {
108  MEdge *medge = (MEdge *)CustomData_get_layer(&dm->edgeData, CD_MEDGE);
109 
110  if (!medge) {
111  medge = (MEdge *)CustomData_add_layer(
112  &dm->edgeData, CD_MEDGE, CD_CALLOC, nullptr, dm->getNumEdges(dm));
114  dm->copyEdgeArray(dm, medge);
115  }
116 
117  return medge;
118 }
119 
121 {
122  MLoop *mloop = (MLoop *)CustomData_get_layer(&dm->loopData, CD_MLOOP);
123 
124  if (!mloop) {
125  mloop = (MLoop *)CustomData_add_layer(
126  &dm->loopData, CD_MLOOP, CD_CALLOC, nullptr, dm->getNumLoops(dm));
128  dm->copyLoopArray(dm, mloop);
129  }
130 
131  return mloop;
132 }
133 
135 {
136  MPoly *mpoly = (MPoly *)CustomData_get_layer(&dm->polyData, CD_MPOLY);
137 
138  if (!mpoly) {
139  mpoly = (MPoly *)CustomData_add_layer(
140  &dm->polyData, CD_MPOLY, CD_CALLOC, nullptr, dm->getNumPolys(dm));
142  dm->copyPolyArray(dm, mpoly);
143  }
144 
145  return mpoly;
146 }
147 
149 {
150  MVert *tmp = (MVert *)MEM_malloc_arrayN(
151  dm->getNumVerts(dm), sizeof(*tmp), "dm_dupVertArray tmp");
152 
153  if (tmp) {
154  dm->copyVertArray(dm, tmp);
155  }
156 
157  return tmp;
158 }
159 
161 {
162  MEdge *tmp = (MEdge *)MEM_malloc_arrayN(
163  dm->getNumEdges(dm), sizeof(*tmp), "dm_dupEdgeArray tmp");
164 
165  if (tmp) {
166  dm->copyEdgeArray(dm, tmp);
167  }
168 
169  return tmp;
170 }
171 
173 {
174  MLoop *tmp = (MLoop *)MEM_malloc_arrayN(
175  dm->getNumLoops(dm), sizeof(*tmp), "dm_dupLoopArray tmp");
176 
177  if (tmp) {
178  dm->copyLoopArray(dm, tmp);
179  }
180 
181  return tmp;
182 }
183 
185 {
186  MPoly *tmp = (MPoly *)MEM_malloc_arrayN(
187  dm->getNumPolys(dm), sizeof(*tmp), "dm_dupPolyArray tmp");
188 
189  if (tmp) {
190  dm->copyPolyArray(dm, tmp);
191  }
192 
193  return tmp;
194 }
195 
197 {
198  const int numlooptris = poly_to_tri_count(dm->getNumPolys(dm), dm->getNumLoops(dm));
199  BLI_assert(ELEM(dm->looptris.num, 0, numlooptris));
200  return numlooptris;
201 }
202 
204 {
205  MLoopTri *looptri;
206 
208  looptri = dm->looptris.array;
210 
211  if (looptri != nullptr) {
212  BLI_assert(dm->getNumLoopTri(dm) == dm->looptris.num);
213  }
214  else {
216  /* We need to ensure array is still nullptr inside mutex-protected code,
217  * some other thread might have already recomputed those looptris. */
218  if (dm->looptris.array == nullptr) {
219  dm->recalcLoopTri(dm);
220  }
221  looptri = dm->looptris.array;
223  }
224  return looptri;
225 }
226 
228 {
229  /* default function implementations */
238 
240 
241  /* subtypes handle getting actual data */
243 
248 }
249 
252  int numVerts,
253  int numEdges,
254  int numTessFaces,
255  int numLoops,
256  int numPolys)
257 {
258  dm->type = type;
259  dm->numVertData = numVerts;
260  dm->numEdgeData = numEdges;
261  dm->numTessFaceData = numTessFaces;
262  dm->numLoopData = numLoops;
263  dm->numPolyData = numPolys;
264 
265  DM_init_funcs(dm);
266 
267  dm->needsFree = 1;
268 
269  /* Don't use #CustomData_reset because we don't want to touch custom-data. */
275 }
276 
278  DerivedMesh *source,
280  int numVerts,
281  int numEdges,
282  int numTessFaces,
283  int numLoops,
284  int numPolys)
285 {
287  CustomData_copy(&source->vertData, &dm->vertData, mask->vmask, CD_CALLOC, numVerts);
288  CustomData_copy(&source->edgeData, &dm->edgeData, mask->emask, CD_CALLOC, numEdges);
289  CustomData_copy(&source->faceData, &dm->faceData, mask->fmask, CD_CALLOC, numTessFaces);
290  CustomData_copy(&source->loopData, &dm->loopData, mask->lmask, CD_CALLOC, numLoops);
291  CustomData_copy(&source->polyData, &dm->polyData, mask->pmask, CD_CALLOC, numPolys);
292 
293  dm->cd_flag = source->cd_flag;
294 
295  dm->type = type;
296  dm->numVertData = numVerts;
297  dm->numEdgeData = numEdges;
298  dm->numTessFaceData = numTessFaces;
299  dm->numLoopData = numLoops;
300  dm->numPolyData = numPolys;
301 
302  DM_init_funcs(dm);
303 
304  dm->needsFree = 1;
305 }
306 
308 {
309  if (dm->needsFree) {
315 
317  dm->looptris.num = 0;
318  dm->looptris.num_alloc = 0;
319 
320  return true;
321  }
322 
328 
329  return false;
330 }
331 
332 void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
333 {
334  CustomData_free(&target->loopData, source->numLoopData);
335  CustomData_free(&target->polyData, source->numPolyData);
336 
337  CustomData_copy(&source->loopData,
338  &target->loopData,
340  CD_DUPLICATE,
341  source->numLoopData);
342  CustomData_copy(&source->polyData,
343  &target->polyData,
345  CD_DUPLICATE,
346  source->numPolyData);
347 
348  target->numLoopData = source->numLoopData;
349  target->numPolyData = source->numPolyData;
350 
351  if (!CustomData_has_layer(&target->polyData, CD_MPOLY)) {
352  MPoly *mpoly;
353  MLoop *mloop;
354 
355  mloop = source->dupLoopArray(source);
356  mpoly = source->dupPolyArray(source);
357  CustomData_add_layer(&target->loopData, CD_MLOOP, CD_ASSIGN, mloop, source->numLoopData);
358  CustomData_add_layer(&target->polyData, CD_MPOLY, CD_ASSIGN, mpoly, source->numPolyData);
359  }
360 }
361 
363 {
364  const unsigned int totpoly = dm->numPolyData;
365  const unsigned int totloop = dm->numLoopData;
366  const int looptris_num = poly_to_tri_count(totpoly, totloop);
367 
368  BLI_assert(dm->looptris.array_wip == nullptr);
369 
371 
372  if ((looptris_num > dm->looptris.num_alloc) || (looptris_num < dm->looptris.num_alloc * 2) ||
373  (totpoly == 0)) {
375  dm->looptris.num_alloc = 0;
376  dm->looptris.num = 0;
377  }
378 
379  if (totpoly) {
380  if (dm->looptris.array_wip == nullptr) {
382  looptris_num, sizeof(*dm->looptris.array_wip), __func__);
383  dm->looptris.num_alloc = looptris_num;
384  }
385 
386  dm->looptris.num = looptris_num;
387  }
388 }
389 
391 {
392  /* Just a shallow wrapper around #BKE_keyblock_convert_from_mesh,
393  * that ensures both evaluated mesh and original one has same number of vertices. */
394 
395  const int totvert = me_deformed->totvert;
396 
397  if (totvert == 0 || me->totvert == 0 || me->totvert != totvert) {
398  return;
399  }
400 
401  BKE_keyblock_convert_from_mesh(me_deformed, me->key, kb);
402 }
403 
405 {
406  CustomData_set_only_copy(&dm->vertData, mask->vmask);
407  CustomData_set_only_copy(&dm->edgeData, mask->emask);
408  CustomData_set_only_copy(&dm->faceData, mask->fmask);
409  /* this wasn't in 2.63 and is disabled for 2.64 because it gives problems with
410  * weight paint mode when there are modifiers applied, needs further investigation,
411  * see replies to r50969, Campbell */
412 #if 0
413  CustomData_set_only_copy(&dm->loopData, mask->lmask);
414  Custom(&dm->polyData, mask->pmask);
415 #endif
416 }
417 
419 {
423  /* this wasn't in 2.63 and is disabled for 2.64 because it gives problems with
424  * weight paint mode when there are modifiers applied, needs further investigation,
425  * see replies to r50969, Campbell */
426 #if 0
429 #endif
430 }
431 
433 {
434  if (type == CD_MVERT) {
435  return dm->getVertArray(dm);
436  }
437 
438  return CustomData_get_layer(&dm->vertData, type);
439 }
440 
442 {
443  if (type == CD_MEDGE) {
444  return dm->getEdgeArray(dm);
445  }
446 
447  return CustomData_get_layer(&dm->edgeData, type);
448 }
449 
451 {
452  return CustomData_get_layer(&dm->polyData, type);
453 }
454 
456 {
457  return CustomData_get_layer(&dm->loopData, type);
458 }
459 
461  DerivedMesh *source, DerivedMesh *dest, int source_index, int dest_index, int count)
462 {
463  CustomData_copy_data(&source->vertData, &dest->vertData, source_index, dest_index, count);
464 }
465 
467  DerivedMesh *dest,
468  int *src_indices,
469  float *weights,
470  int count,
471  int dest_index)
472 {
474  &source->vertData, &dest->vertData, src_indices, weights, nullptr, count, dest_index);
475 }
476 
478 {
479  BMIter iter;
480  BMVert *eve;
481  float(*orco)[3];
482  int i;
483 
484  /* these may not really be the orco's, but it's only for preview.
485  * could be solver better once, but isn't simple */
486 
487  orco = (float(*)[3])MEM_malloc_arrayN(em->bm->totvert, sizeof(float[3]), "BMEditMesh Orco");
488 
489  BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
490  copy_v3_v3(orco[i], eve->co);
491  }
492 
493  return orco;
494 }
495 
496 /* orco custom data layer */
497 static float (*get_orco_coords(Object *ob, BMEditMesh *em, int layer, int *free))[3]
498 {
499  *free = 0;
500 
501  if (layer == CD_ORCO) {
502  /* get original coordinates */
503  *free = 1;
504 
505  if (em) {
506  return get_editbmesh_orco_verts(em);
507  }
508  return BKE_mesh_orco_verts_get(ob);
509  }
510  if (layer == CD_CLOTH_ORCO) {
511  /* apply shape key for cloth, this should really be solved
512  * by a more flexible customdata system, but not simple */
513  if (!em) {
515  ob, eModifierType_Cloth);
516  if (clmd) {
518  clmd->sim_parms->shapekey_rest);
519 
520  if (kb && kb->data) {
521  return (float(*)[3])kb->data;
522  }
523  }
524  }
525 
526  return nullptr;
527  }
528 
529  return nullptr;
530 }
531 
532 static Mesh *create_orco_mesh(Object *ob, Mesh *me, BMEditMesh *em, int layer)
533 {
534  Mesh *mesh;
535  float(*orco)[3];
536  int free;
537 
538  if (em) {
539  mesh = BKE_mesh_from_bmesh_for_eval_nomain(em->bm, nullptr, me);
541  }
542  else {
543  mesh = BKE_mesh_copy_for_eval(me, true);
544  }
545 
546  orco = get_orco_coords(ob, em, layer, &free);
547 
548  if (orco) {
550  if (free) {
551  MEM_freeN(orco);
552  }
553  }
554 
555  return mesh;
556 }
557 
558 static void add_orco_mesh(Object *ob, BMEditMesh *em, Mesh *mesh, Mesh *mesh_orco, int layer)
559 {
560  float(*orco)[3], (*layerorco)[3];
561  int totvert, free;
562 
563  totvert = mesh->totvert;
564 
565  if (mesh_orco) {
566  free = 1;
567 
568  if (mesh_orco->totvert == totvert) {
569  orco = BKE_mesh_vert_coords_alloc(mesh_orco, nullptr);
570  }
571  else {
572  orco = BKE_mesh_vert_coords_alloc(mesh, nullptr);
573  }
574  }
575  else {
576  /* TODO(sybren): totvert should potentially change here, as ob->data
577  * or em may have a different number of vertices than dm. */
578  orco = get_orco_coords(ob, em, layer, &free);
579  }
580 
581  if (orco) {
582  if (layer == CD_ORCO) {
583  BKE_mesh_orco_verts_transform((Mesh *)ob->data, orco, totvert, 0);
584  }
585 
586  if (!(layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer))) {
587  CustomData_add_layer(&mesh->vdata, layer, CD_CALLOC, nullptr, mesh->totvert);
589 
590  layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer);
591  }
592 
593  memcpy(layerorco, orco, sizeof(float[3]) * totvert);
594  if (free) {
595  MEM_freeN(orco);
596  }
597  }
598 }
599 
600 static bool mesh_has_modifier_final_normals(const Mesh *mesh_input,
601  const CustomData_MeshMasks *final_datamask,
602  Mesh *mesh_final)
603 {
604  /* Test if mesh has the required loop normals, in case an additional modifier
605  * evaluation from another instance or from an operator requests it but the
606  * initial normals were not loop normals. */
607  const bool calc_loop_normals = ((mesh_input->flag & ME_AUTOSMOOTH) != 0 ||
608  (final_datamask->lmask & CD_MASK_NORMAL) != 0);
609 
610  return (!calc_loop_normals || CustomData_has_layer(&mesh_final->ldata, CD_NORMAL));
611 }
612 
613 static void mesh_calc_modifier_final_normals(const Mesh *mesh_input,
614  const CustomData_MeshMasks *final_datamask,
615  const bool sculpt_dyntopo,
616  Mesh *mesh_final)
617 {
618  /* Compute normals. */
619  const bool calc_loop_normals = ((mesh_input->flag & ME_AUTOSMOOTH) != 0 ||
620  (final_datamask->lmask & CD_MASK_NORMAL) != 0);
621 
622  /* Needed as `final_datamask` is not preserved outside modifier stack evaluation. */
623  SubsurfRuntimeData *subsurf_runtime_data = mesh_final->runtime.subsurf_runtime_data;
624  if (subsurf_runtime_data) {
625  subsurf_runtime_data->calc_loop_normals = calc_loop_normals;
626  }
627 
628  if (calc_loop_normals) {
629  /* Compute loop normals (NOTE: will compute poly and vert normals as well, if needed!). In case
630  * of deferred CPU subdivision, this will be computed when the wrapper is generated. */
631  if (!subsurf_runtime_data || subsurf_runtime_data->resolution == 0) {
632  BKE_mesh_calc_normals_split(mesh_final);
633  }
634  }
635  else {
636  if (sculpt_dyntopo == false) {
637  /* without this, drawing ngon tri's faces will show ugly tessellated face
638  * normals and will also have to calculate normals on the fly, try avoid
639  * this where possible since calculating polygon normals isn't fast,
640  * note that this isn't a problem for subsurf (only quads) or edit-mode
641  * which deals with drawing differently. */
643  }
644 
645  /* Some modifiers, like data-transfer, may generate those data as temp layer,
646  * we do not want to keep them, as they are used by display code when available
647  * (i.e. even if auto-smooth is disabled). */
648  if (CustomData_has_layer(&mesh_final->ldata, CD_NORMAL)) {
649  CustomData_free_layers(&mesh_final->ldata, CD_NORMAL, mesh_final->totloop);
650  }
651  }
652 }
653 
654 /* Does final touches to the final evaluated mesh, making sure it is perfectly usable.
655  *
656  * This is needed because certain information is not passed along intermediate meshes allocated
657  * during stack evaluation.
658  */
659 static void mesh_calc_finalize(const Mesh *mesh_input, Mesh *mesh_eval)
660 {
661  /* Make sure the name is the same. This is because mesh allocation from template does not
662  * take care of naming. */
663  BLI_strncpy(mesh_eval->id.name, mesh_input->id.name, sizeof(mesh_eval->id.name));
664  /* Make evaluated mesh to share same edit mesh pointer as original and copied meshes. */
665  mesh_eval->edit_mesh = mesh_input->edit_mesh;
666 }
667 
669  const CustomData_MeshMasks *cd_mask_finalize)
670 {
671  if (me_eval->runtime.wrapper_type_finalize & (1 << ME_WRAPPER_TYPE_BMESH)) {
672  editbmesh_calc_modifier_final_normals(me_eval, cd_mask_finalize);
674  }
676 }
677 
687  const ModifierEvalContext &mectx,
688  Mesh *input_mesh,
689  GeometrySet &geometry_set)
690 {
691  Mesh *mesh_output = nullptr;
693  if (mti->modifyGeometrySet == nullptr) {
694  mesh_output = BKE_modifier_modify_mesh(md, &mectx, input_mesh);
695  }
696  else {
697  /* For performance reasons, this should be called by the modifier and/or nodes themselves at
698  * some point. */
699  BKE_mesh_wrapper_ensure_mdata(input_mesh);
700 
701  /* Adds a new mesh component to the geometry set based on the #input_mesh. */
702  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
703  /* Replace only the mesh rather than the whole component, because the entire #MeshComponent
704  * might have been replaced by data from a different object in the node tree, which means the
705  * component contains vertex group name data for that object that should not be removed. */
706  mesh_component.replace(input_mesh, GeometryOwnershipType::Editable);
707 
708  /* Let the modifier change the geometry set. */
709  mti->modifyGeometrySet(md, &mectx, &geometry_set);
710 
711  /* Release the mesh from the geometry set again. */
712  if (geometry_set.has<MeshComponent>()) {
713  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
714  if (mesh_component.get_for_read() != input_mesh) {
715  /* Make sure the mesh component actually owns the mesh before taking over ownership. */
716  mesh_component.ensure_owns_direct_data();
717  }
718  mesh_output = mesh_component.release();
719  }
720 
721  /* Return an empty mesh instead of null. */
722  if (mesh_output == nullptr) {
723  mesh_output = BKE_mesh_new_nomain(0, 0, 0, 0, 0);
724  BKE_mesh_copy_parameters_for_eval(mesh_output, input_mesh);
725  }
726  }
727 
728  return mesh_output;
729 }
730 
732  const Scene *scene,
733  Object *ob,
734  const bool use_deform,
735  const bool need_mapping,
736  const CustomData_MeshMasks *dataMask,
737  const bool use_cache,
738  const bool allow_shared_mesh,
739  /* return args */
740  Mesh **r_deform,
741  Mesh **r_final,
742  GeometrySet **r_geometry_set)
743 {
744  using namespace blender::bke;
745  /* Input and final mesh. Final mesh is only created the moment the first
746  * constructive modifier is executed, or a deform modifier needs normals
747  * or certain data layers. */
748  Mesh *mesh_input = (Mesh *)ob->data;
750  Mesh *mesh_final = nullptr;
751  Mesh *mesh_deform = nullptr;
752  /* This geometry set contains the non-mesh data that might be generated by modifiers. */
753  GeometrySet geometry_set_final;
754 
756 
757  /* Deformed vertex locations array. Deform only modifier need this type of
758  * float array rather than MVert*. Tracked along with mesh_final as an
759  * optimization to avoid copying coordinates back and forth if there are
760  * multiple sequential deform only modifiers. */
761  float(*deformed_verts)[3] = nullptr;
762  int num_deformed_verts = mesh_input->totvert;
763  bool isPrevDeform = false;
764 
765  /* Mesh with constructive modifiers but no deformation applied. Tracked
766  * along with final mesh if undeformed / orco coordinates are requested
767  * for texturing. */
768  Mesh *mesh_orco = nullptr;
769  Mesh *mesh_orco_cloth = nullptr;
770 
771  /* Modifier evaluation modes. */
772  const bool use_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
773  const int required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
774 
775  /* Sculpt can skip certain modifiers. */
776  const bool has_multires = BKE_sculpt_multires_active(scene, ob) != nullptr;
777  bool multires_applied = false;
778  const bool sculpt_mode = ob->mode & OB_MODE_SCULPT && ob->sculpt && !use_render;
779  const bool sculpt_dyntopo = (sculpt_mode && ob->sculpt->bm) && !use_render;
780 
781  /* Modifier evaluation contexts for different types of modifiers. */
782  ModifierApplyFlag apply_render = use_render ? MOD_APPLY_RENDER : (ModifierApplyFlag)0;
783  ModifierApplyFlag apply_cache = use_cache ? MOD_APPLY_USECACHE : (ModifierApplyFlag)0;
784  const ModifierEvalContext mectx = {
785  depsgraph, ob, (ModifierApplyFlag)(apply_render | apply_cache)};
786  const ModifierEvalContext mectx_orco = {
787  depsgraph, ob, (ModifierApplyFlag)(apply_render | MOD_APPLY_ORCO)};
788 
789  /* Get effective list of modifiers to execute. Some effects like shape keys
790  * are added as virtual modifiers before the user created modifiers. */
791  VirtualModifierData virtualModifierData;
792  ModifierData *firstmd = BKE_modifiers_get_virtual_modifierlist(ob, &virtualModifierData);
793  ModifierData *md = firstmd;
794 
795  /* Preview colors by modifiers such as dynamic paint, to show the results
796  * even if the resulting data is not used in a material. Only in object mode.
797  * TODO: this is broken, not drawn by the drawn manager. */
798  const bool do_mod_mcol = (ob->mode == OB_MODE_OBJECT);
799  ModifierData *previewmd = nullptr;
800  CustomData_MeshMasks previewmask = {0};
801  if (do_mod_mcol) {
802  /* Find the last active modifier generating a preview, or nullptr if none. */
803  /* XXX Currently, DPaint modifier just ignores this.
804  * Needs a stupid hack...
805  * The whole "modifier preview" thing has to be (re?)designed, anyway! */
806  previewmd = BKE_modifier_get_last_preview(scene, md, required_mode);
807  }
808 
809  /* Compute accumulated datamasks needed by each modifier. It helps to do
810  * this fine grained so that for example vertex groups are preserved up to
811  * an armature modifier, but not through a following subsurf modifier where
812  * subdividing them is expensive. */
813  CustomData_MeshMasks final_datamask = *dataMask;
815  scene, ob, md, &final_datamask, required_mode, previewmd, &previewmask);
816  CDMaskLink *md_datamask = datamasks;
817  /* XXX Always copying POLYINDEX, else tessellated data are no more valid! */
819 
820  /* Clear errors before evaluation. */
822 
824  if (mesh_final == nullptr) {
825  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
826  ASSERT_IS_VALID_MESH(mesh_final);
827  }
828  MutableAttributeAccessor attributes = mesh_attributes_for_write(*mesh_final);
829  SpanAttributeWriter<float3> rest_positions =
830  attributes.lookup_or_add_for_write_only_span<float3>("rest_position", ATTR_DOMAIN_POINT);
831  if (rest_positions) {
832  attributes.lookup<float3>("position").materialize(rest_positions.span);
833  rest_positions.finish();
834  }
835  }
836 
837  /* Apply all leading deform modifiers. */
838  if (use_deform) {
839  for (; md; md = md->next, md_datamask = md_datamask->next) {
841 
842  if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
843  continue;
844  }
845 
846  if (mti->type == eModifierTypeType_OnlyDeform && !sculpt_dyntopo) {
847  if (!deformed_verts) {
848  deformed_verts = BKE_mesh_vert_coords_alloc(mesh_input, &num_deformed_verts);
849  }
850  else if (isPrevDeform && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
851  if (mesh_final == nullptr) {
852  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
853  ASSERT_IS_VALID_MESH(mesh_final);
854  }
855  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
856  }
857 
858  BKE_modifier_deform_verts(md, &mectx, mesh_final, deformed_verts, num_deformed_verts);
859 
860  isPrevDeform = true;
861  }
862  else {
863  break;
864  }
865  }
866 
867  /* Result of all leading deforming modifiers is cached for
868  * places that wish to use the original mesh but with deformed
869  * coordinates (like vertex paint). */
870  if (r_deform) {
871  mesh_deform = BKE_mesh_copy_for_eval(mesh_input, true);
872 
873  if (deformed_verts) {
874  BKE_mesh_vert_coords_apply(mesh_deform, deformed_verts);
875  }
876  }
877  }
878 
879  /* Apply all remaining constructive and deforming modifiers. */
880  bool have_non_onlydeform_modifiers_appled = false;
881  for (; md; md = md->next, md_datamask = md_datamask->next) {
883 
884  if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
885  continue;
886  }
887 
888  if (mti->type == eModifierTypeType_OnlyDeform && !use_deform) {
889  continue;
890  }
891 
893  have_non_onlydeform_modifiers_appled) {
894  BKE_modifier_set_error(ob, md, "Modifier requires original data, bad stack position");
895  continue;
896  }
897 
898  if (sculpt_mode && (!has_multires || multires_applied || sculpt_dyntopo)) {
899  bool unsupported = false;
900 
901  if (md->type == eModifierType_Multires && ((MultiresModifierData *)md)->sculptlvl == 0) {
902  /* If multires is on level 0 skip it silently without warning message. */
903  if (!sculpt_dyntopo) {
904  continue;
905  }
906  }
907 
908  if (sculpt_dyntopo) {
909  unsupported = true;
910  }
911 
913  unsupported |= (mti->type != eModifierTypeType_OnlyDeform);
914  }
915 
916  unsupported |= multires_applied;
917 
918  if (unsupported) {
919  if (sculpt_dyntopo) {
920  BKE_modifier_set_error(ob, md, "Not supported in dyntopo");
921  }
922  else {
923  BKE_modifier_set_error(ob, md, "Not supported in sculpt mode");
924  }
925  continue;
926  }
927  }
928 
929  if (need_mapping && !BKE_modifier_supports_mapping(md)) {
930  continue;
931  }
932 
933  /* Add orco mesh as layer if needed by this modifier. */
934  if (mesh_final && mesh_orco && mti->requiredDataMask) {
936  mti->requiredDataMask(ob, md, &mask);
937  if (mask.vmask & CD_MASK_ORCO) {
938  add_orco_mesh(ob, nullptr, mesh_final, mesh_orco, CD_ORCO);
939  }
940  }
941 
942  /* How to apply modifier depends on (a) what we already have as
943  * a result of previous modifiers (could be a Mesh or just
944  * deformed vertices) and (b) what type the modifier is. */
945  if (mti->type == eModifierTypeType_OnlyDeform) {
946  /* No existing verts to deform, need to build them. */
947  if (!deformed_verts) {
948  if (mesh_final) {
949  /* Deforming a mesh, read the vertex locations
950  * out of the mesh and deform them. Once done with this
951  * run of deformers verts will be written back. */
952  deformed_verts = BKE_mesh_vert_coords_alloc(mesh_final, &num_deformed_verts);
953  }
954  else {
955  deformed_verts = BKE_mesh_vert_coords_alloc(mesh_input, &num_deformed_verts);
956  }
957  }
958  /* if this is not the last modifier in the stack then recalculate the normals
959  * to avoid giving bogus normals to the next modifier see: T23673. */
960  else if (isPrevDeform && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
961  if (mesh_final == nullptr) {
962  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
963  ASSERT_IS_VALID_MESH(mesh_final);
964  }
965  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
966  }
967  BKE_modifier_deform_verts(md, &mectx, mesh_final, deformed_verts, num_deformed_verts);
968  }
969  else {
970  bool check_for_needs_mapping = false;
971  /* apply vertex coordinates or build a Mesh as necessary */
972  if (mesh_final != nullptr) {
973  if (have_non_onlydeform_modifiers_appled == false) {
974  /* If we only deformed, we won't have initialized #CD_ORIGINDEX.
975  * as this is the only part of the function that initializes mapping. */
976  check_for_needs_mapping = true;
977  }
978  }
979  else {
980  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
981  ASSERT_IS_VALID_MESH(mesh_final);
982  check_for_needs_mapping = true;
983  }
984 
985  if (deformed_verts) {
986  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
987  }
988 
989  have_non_onlydeform_modifiers_appled = true;
990 
991  /* determine which data layers are needed by following modifiers */
992  CustomData_MeshMasks nextmask = md_datamask->next ? md_datamask->next->mask : final_datamask;
993 
994  if (check_for_needs_mapping) {
995  /* Initialize original indices the first time we evaluate a
996  * constructive modifier. Modifiers will then do mapping mostly
997  * automatic by copying them through CustomData_copy_data along
998  * with other data.
999  *
1000  * These are created when either requested by evaluation, or if
1001  * following modifiers requested them. */
1002  if (need_mapping ||
1003  ((nextmask.vmask | nextmask.emask | nextmask.pmask) & CD_MASK_ORIGINDEX)) {
1004  /* calc */
1006  &mesh_final->vdata, CD_ORIGINDEX, CD_CALLOC, nullptr, mesh_final->totvert);
1008  &mesh_final->edata, CD_ORIGINDEX, CD_CALLOC, nullptr, mesh_final->totedge);
1010  &mesh_final->pdata, CD_ORIGINDEX, CD_CALLOC, nullptr, mesh_final->totpoly);
1011 
1012  /* Not worth parallelizing this,
1013  * gives less than 0.1% overall speedup in best of best cases... */
1014  range_vn_i((int *)CustomData_get_layer(&mesh_final->vdata, CD_ORIGINDEX),
1015  mesh_final->totvert,
1016  0);
1017  range_vn_i((int *)CustomData_get_layer(&mesh_final->edata, CD_ORIGINDEX),
1018  mesh_final->totedge,
1019  0);
1020  range_vn_i((int *)CustomData_get_layer(&mesh_final->pdata, CD_ORIGINDEX),
1021  mesh_final->totpoly,
1022  0);
1023  }
1024  }
1025 
1026  /* set the Mesh to only copy needed data */
1027  CustomData_MeshMasks mask = md_datamask->mask;
1028  /* needMapping check here fixes bug T28112, otherwise it's
1029  * possible that it won't be copied */
1030  CustomData_MeshMasks_update(&mask, &append_mask);
1031  if (need_mapping) {
1032  mask.vmask |= CD_MASK_ORIGINDEX;
1033  mask.emask |= CD_MASK_ORIGINDEX;
1034  mask.pmask |= CD_MASK_ORIGINDEX;
1035  }
1036  mesh_set_only_copy(mesh_final, &mask);
1037 
1038  /* add cloth rest shape key if needed */
1039  if (mask.vmask & CD_MASK_CLOTH_ORCO) {
1040  add_orco_mesh(ob, nullptr, mesh_final, mesh_orco, CD_CLOTH_ORCO);
1041  }
1042 
1043  /* add an origspace layer if needed */
1044  if ((md_datamask->mask.lmask) & CD_MASK_ORIGSPACE_MLOOP) {
1045  if (!CustomData_has_layer(&mesh_final->ldata, CD_ORIGSPACE_MLOOP)) {
1047  &mesh_final->ldata, CD_ORIGSPACE_MLOOP, CD_CALLOC, nullptr, mesh_final->totloop);
1048  mesh_init_origspace(mesh_final);
1049  }
1050  }
1051 
1053  md, mectx, mesh_final, geometry_set_final);
1054  ASSERT_IS_VALID_MESH(mesh_next);
1055 
1056  if (mesh_next) {
1057  /* if the modifier returned a new mesh, release the old one */
1058  if (mesh_final != mesh_next) {
1059  BLI_assert(mesh_final != mesh_input);
1060  BKE_id_free(nullptr, mesh_final);
1061  }
1062  mesh_final = mesh_next;
1063 
1064  if (deformed_verts) {
1065  MEM_freeN(deformed_verts);
1066  deformed_verts = nullptr;
1067  }
1068  }
1069 
1070  /* create an orco mesh in parallel */
1071  if (nextmask.vmask & CD_MASK_ORCO) {
1072  if (!mesh_orco) {
1073  mesh_orco = create_orco_mesh(ob, mesh_input, nullptr, CD_ORCO);
1074  }
1075 
1076  nextmask.vmask &= ~CD_MASK_ORCO;
1077  CustomData_MeshMasks temp_cddata_masks = {0};
1078  temp_cddata_masks.vmask = CD_MASK_ORIGINDEX;
1079  temp_cddata_masks.emask = CD_MASK_ORIGINDEX;
1080  temp_cddata_masks.fmask = CD_MASK_ORIGINDEX;
1081  temp_cddata_masks.pmask = CD_MASK_ORIGINDEX;
1082 
1083  if (mti->requiredDataMask != nullptr) {
1084  mti->requiredDataMask(ob, md, &temp_cddata_masks);
1085  }
1086  CustomData_MeshMasks_update(&temp_cddata_masks, &nextmask);
1087  mesh_set_only_copy(mesh_orco, &temp_cddata_masks);
1088 
1089  mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco);
1090  ASSERT_IS_VALID_MESH(mesh_next);
1091 
1092  if (mesh_next) {
1093  /* if the modifier returned a new mesh, release the old one */
1094  if (mesh_orco != mesh_next) {
1095  BLI_assert(mesh_orco != mesh_input);
1096  BKE_id_free(nullptr, mesh_orco);
1097  }
1098 
1099  mesh_orco = mesh_next;
1100  }
1101  }
1102 
1103  /* create cloth orco mesh in parallel */
1104  if (nextmask.vmask & CD_MASK_CLOTH_ORCO) {
1105  if (!mesh_orco_cloth) {
1106  mesh_orco_cloth = create_orco_mesh(ob, mesh_input, nullptr, CD_CLOTH_ORCO);
1107  }
1108 
1109  nextmask.vmask &= ~CD_MASK_CLOTH_ORCO;
1110  nextmask.vmask |= CD_MASK_ORIGINDEX;
1111  nextmask.emask |= CD_MASK_ORIGINDEX;
1112  nextmask.pmask |= CD_MASK_ORIGINDEX;
1113  mesh_set_only_copy(mesh_orco_cloth, &nextmask);
1114 
1115  mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco_cloth);
1116  ASSERT_IS_VALID_MESH(mesh_next);
1117 
1118  if (mesh_next) {
1119  /* if the modifier returned a new mesh, release the old one */
1120  if (mesh_orco_cloth != mesh_next) {
1121  BLI_assert(mesh_orco != mesh_input);
1122  BKE_id_free(nullptr, mesh_orco_cloth);
1123  }
1124 
1125  mesh_orco_cloth = mesh_next;
1126  }
1127  }
1128 
1129  /* in case of dynamic paint, make sure preview mask remains for following modifiers */
1130  /* XXX Temp and hackish solution! */
1131  if (md->type == eModifierType_DynamicPaint) {
1132  append_mask.lmask |= CD_MASK_PREVIEW_MLOOPCOL;
1133  }
1134 
1135  mesh_final->runtime.deformed_only = false;
1136  }
1137 
1138  isPrevDeform = (mti->type == eModifierTypeType_OnlyDeform);
1139 
1140  if (sculpt_mode && md->type == eModifierType_Multires) {
1141  multires_applied = true;
1142  }
1143  }
1144 
1145  BLI_linklist_free((LinkNode *)datamasks, nullptr);
1146 
1147  for (md = firstmd; md; md = md->next) {
1149  }
1150 
1151  /* Yay, we are done. If we have a Mesh and deformed vertices,
1152  * we need to apply these back onto the Mesh. If we have no
1153  * Mesh then we need to build one. */
1154  if (mesh_final == nullptr) {
1155  if (deformed_verts == nullptr && allow_shared_mesh) {
1156  mesh_final = mesh_input;
1157  }
1158  else {
1159  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
1160  }
1161  }
1162  if (deformed_verts) {
1163  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
1164  MEM_freeN(deformed_verts);
1165  deformed_verts = nullptr;
1166  }
1167 
1168  /* Denotes whether the object which the modifier stack came from owns the mesh or whether the
1169  * mesh is shared across multiple objects since there are no effective modifiers. */
1170  const bool is_own_mesh = (mesh_final != mesh_input);
1171 
1172  /* Add orco coordinates to final and deformed mesh if requested. */
1173  if (final_datamask.vmask & CD_MASK_ORCO) {
1174  /* No need in ORCO layer if the mesh was not deformed or modified: undeformed mesh in this case
1175  * matches input mesh. */
1176  if (is_own_mesh) {
1177  add_orco_mesh(ob, nullptr, mesh_final, mesh_orco, CD_ORCO);
1178  }
1179 
1180  if (mesh_deform) {
1181  add_orco_mesh(ob, nullptr, mesh_deform, nullptr, CD_ORCO);
1182  }
1183  }
1184 
1185  if (mesh_orco) {
1186  BKE_id_free(nullptr, mesh_orco);
1187  }
1188  if (mesh_orco_cloth) {
1189  BKE_id_free(nullptr, mesh_orco_cloth);
1190  }
1191 
1192  /* Remove temporary data layer only needed for modifier evaluation.
1193  * Save some memory, and ensure GPU subdivision does not need to deal with this. */
1194  CustomData_free_layers(&mesh_final->vdata, CD_CLOTH_ORCO, mesh_final->totvert);
1195 
1196  /* Compute normals. */
1197  if (is_own_mesh) {
1198  mesh_calc_modifier_final_normals(mesh_input, &final_datamask, sculpt_dyntopo, mesh_final);
1199  mesh_calc_finalize(mesh_input, mesh_final);
1200  }
1201  else {
1202  Mesh_Runtime *runtime = &mesh_input->runtime;
1203  if (runtime->mesh_eval == nullptr) {
1204  BLI_assert(runtime->eval_mutex != nullptr);
1205  BLI_mutex_lock((ThreadMutex *)runtime->eval_mutex);
1206  if (runtime->mesh_eval == nullptr) {
1207  /* Not yet finalized by any instance, do it now
1208  * Isolate since computing normals is multithreaded and we are holding a lock. */
1210  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
1212  mesh_input, &final_datamask, sculpt_dyntopo, mesh_final);
1213  mesh_calc_finalize(mesh_input, mesh_final);
1214  runtime->mesh_eval = mesh_final;
1215  });
1216  }
1217  else {
1218  /* Already finalized by another instance, reuse. */
1219  mesh_final = runtime->mesh_eval;
1220  }
1222  }
1223  else if (!mesh_has_modifier_final_normals(mesh_input, &final_datamask, runtime->mesh_eval)) {
1224  /* Modifier stack was (re-)evaluated with a request for additional normals
1225  * different than the instanced mesh, can't instance anymore now. */
1226  mesh_final = BKE_mesh_copy_for_eval(mesh_input, true);
1227  mesh_calc_modifier_final_normals(mesh_input, &final_datamask, sculpt_dyntopo, mesh_final);
1228  mesh_calc_finalize(mesh_input, mesh_final);
1229  }
1230  else {
1231  /* Already finalized by another instance, reuse. */
1232  mesh_final = runtime->mesh_eval;
1233  }
1234  }
1235 
1236  /* Return final mesh */
1237  *r_final = mesh_final;
1238  if (r_deform) {
1239  *r_deform = mesh_deform;
1240  }
1241  if (r_geometry_set) {
1242  *r_geometry_set = new GeometrySet(std::move(geometry_set_final));
1243  }
1244 }
1245 
1246 float (*editbmesh_vert_coords_alloc(BMEditMesh *em, int *r_vert_len))[3]
1247 {
1248  BMIter iter;
1249  BMVert *eve;
1250  float(*cos)[3];
1251  int i;
1252 
1253  *r_vert_len = em->bm->totvert;
1254 
1255  cos = (float(*)[3])MEM_malloc_arrayN(em->bm->totvert, sizeof(float[3]), "vertexcos");
1256 
1257  BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
1258  copy_v3_v3(cos[i], eve->co);
1259  }
1260 
1261  return cos;
1262 }
1263 
1265  const Object *ob,
1266  ModifierData *md,
1267  bool has_prev_mesh)
1268 {
1270  const int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1271 
1272  if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
1273  return false;
1274  }
1275 
1276  if ((mti->flags & eModifierTypeFlag_RequiresOriginalData) && has_prev_mesh) {
1277  BKE_modifier_set_error(ob, md, "Modifier requires original data, bad stack position");
1278  return false;
1279  }
1280 
1281  return true;
1282 }
1283 
1285  const CustomData_MeshMasks *final_datamask)
1286 {
1287  const bool calc_loop_normals = ((mesh_final->flag & ME_AUTOSMOOTH) != 0 ||
1288  (final_datamask->lmask & CD_MASK_NORMAL) != 0);
1289 
1290  SubsurfRuntimeData *subsurf_runtime_data = mesh_final->runtime.subsurf_runtime_data;
1291  if (subsurf_runtime_data) {
1292  subsurf_runtime_data->calc_loop_normals = calc_loop_normals;
1293  }
1294 
1295  if (calc_loop_normals) {
1296  /* Compute loop normals. In case of deferred CPU subdivision, this will be computed when the
1297  * wrapper is generated. */
1298  if (!subsurf_runtime_data || subsurf_runtime_data->resolution == 0) {
1299  BKE_mesh_calc_normals_split(mesh_final);
1300  }
1301  }
1302  else {
1303  /* Same as mesh_calc_modifiers. If using loop normals, poly nors have already been computed. */
1305 
1306  /* Some modifiers, like data-transfer, may generate those data, we do not want to keep them,
1307  * as they are used by display code when available (i.e. even if autosmooth is disabled). */
1308  if (CustomData_has_layer(&mesh_final->ldata, CD_NORMAL)) {
1309  CustomData_free_layers(&mesh_final->ldata, CD_NORMAL, mesh_final->totloop);
1310  }
1311  }
1312 }
1313 
1315  Mesh *mesh_final, const CustomData_MeshMasks *final_datamask)
1316 {
1317  if (mesh_final->runtime.wrapper_type != ME_WRAPPER_TYPE_MDATA) {
1318  /* Generated at draw time. */
1319  mesh_final->runtime.wrapper_type_finalize = (1 << mesh_final->runtime.wrapper_type);
1320  return;
1321  }
1322 
1323  editbmesh_calc_modifier_final_normals(mesh_final, final_datamask);
1324 }
1325 
1327  const Scene *scene,
1328  Object *ob,
1329  BMEditMesh *em_input,
1330  const CustomData_MeshMasks *dataMask,
1331  /* return args */
1332  Mesh **r_cage,
1333  Mesh **r_final,
1334  GeometrySet **r_geometry_set)
1335 {
1336  /* Input and final mesh. Final mesh is only created the moment the first
1337  * constructive modifier is executed, or a deform modifier needs normals
1338  * or certain data layers. */
1339  Mesh *mesh_input = (Mesh *)ob->data;
1340  Mesh *mesh_final = nullptr;
1341  Mesh *mesh_cage = nullptr;
1342  /* This geometry set contains the non-mesh data that might be generated by modifiers. */
1343  GeometrySet geometry_set_final;
1344 
1345  /* Deformed vertex locations array. Deform only modifier need this type of
1346  * float array rather than MVert*. Tracked along with mesh_final as an
1347  * optimization to avoid copying coordinates back and forth if there are
1348  * multiple sequential deform only modifiers. */
1349  float(*deformed_verts)[3] = nullptr;
1350  int num_deformed_verts = 0;
1351  bool isPrevDeform = false;
1352 
1353  /* Mesh with constructive modifiers but no deformation applied. Tracked
1354  * along with final mesh if undeformed / orco coordinates are requested
1355  * for texturing. */
1356  Mesh *mesh_orco = nullptr;
1357 
1358  /* Modifier evaluation modes. */
1359  const int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
1360 
1361  const bool use_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
1362  /* Modifier evaluation contexts for different types of modifiers. */
1363  ModifierApplyFlag apply_render = use_render ? MOD_APPLY_RENDER : (ModifierApplyFlag)0;
1364  const ModifierEvalContext mectx = {
1365  depsgraph, ob, (ModifierApplyFlag)(MOD_APPLY_USECACHE | apply_render)};
1366  const ModifierEvalContext mectx_orco = {depsgraph, ob, MOD_APPLY_ORCO};
1367 
1368  /* Get effective list of modifiers to execute. Some effects like shape keys
1369  * are added as virtual modifiers before the user created modifiers. */
1370  VirtualModifierData virtualModifierData;
1371  ModifierData *md = BKE_modifiers_get_virtual_modifierlist(ob, &virtualModifierData);
1372 
1373  /* Compute accumulated datamasks needed by each modifier. It helps to do
1374  * this fine grained so that for example vertex groups are preserved up to
1375  * an armature modifier, but not through a following subsurf modifier where
1376  * subdividing them is expensive. */
1377  CustomData_MeshMasks final_datamask = *dataMask;
1379  scene, ob, md, &final_datamask, required_mode, nullptr, nullptr);
1380  CDMaskLink *md_datamask = datamasks;
1381  CustomData_MeshMasks append_mask = CD_MASK_BAREMESH;
1382 
1383  /* Evaluate modifiers up to certain index to get the mesh cage. */
1384  int cageIndex = BKE_modifiers_get_cage_index(scene, ob, nullptr, true);
1385  if (r_cage && cageIndex == -1) {
1387  em_input, &final_datamask, nullptr, mesh_input);
1388  }
1389 
1390  /* The mesh from edit mode should not have any original index layers already, since those
1391  * are added during evaluation when necessary and are redundant on an original mesh. */
1392  BLI_assert(CustomData_get_layer(&em_input->bm->pdata, CD_ORIGINDEX) == nullptr &&
1393  CustomData_get_layer(&em_input->bm->edata, CD_ORIGINDEX) == nullptr &&
1394  CustomData_get_layer(&em_input->bm->pdata, CD_ORIGINDEX) == nullptr);
1395 
1396  /* Clear errors before evaluation. */
1398 
1399  for (int i = 0; md; i++, md = md->next, md_datamask = md_datamask->next) {
1401 
1402  if (!editbmesh_modifier_is_enabled(scene, ob, md, mesh_final != nullptr)) {
1403  continue;
1404  }
1405 
1406  /* Add an orco mesh as layer if needed by this modifier. */
1407  if (mesh_final && mesh_orco && mti->requiredDataMask) {
1408  CustomData_MeshMasks mask = {0};
1409  mti->requiredDataMask(ob, md, &mask);
1410  if (mask.vmask & CD_MASK_ORCO) {
1411  add_orco_mesh(ob, em_input, mesh_final, mesh_orco, CD_ORCO);
1412  }
1413  }
1414 
1415  /* How to apply modifier depends on (a) what we already have as
1416  * a result of previous modifiers (could be a mesh or just
1417  * deformed vertices) and (b) what type the modifier is. */
1418  if (mti->type == eModifierTypeType_OnlyDeform) {
1419  /* No existing verts to deform, need to build them. */
1420  if (!deformed_verts) {
1421  if (mesh_final) {
1422  /* Deforming a derived mesh, read the vertex locations
1423  * out of the mesh and deform them. Once done with this
1424  * run of deformers verts will be written back. */
1425  deformed_verts = BKE_mesh_vert_coords_alloc(mesh_final, &num_deformed_verts);
1426  }
1427  else {
1428  deformed_verts = editbmesh_vert_coords_alloc(em_input, &num_deformed_verts);
1429  }
1430  }
1431  else if (isPrevDeform && mti->dependsOnNormals && mti->dependsOnNormals(md)) {
1432  if (mesh_final == nullptr) {
1433  mesh_final = BKE_mesh_from_bmesh_for_eval_nomain(em_input->bm, nullptr, mesh_input);
1435  ASSERT_IS_VALID_MESH(mesh_final);
1436  }
1437  BLI_assert(deformed_verts != nullptr);
1438  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
1439  }
1440 
1441  if (mti->deformVertsEM) {
1443  md, &mectx, em_input, mesh_final, deformed_verts, num_deformed_verts);
1444  }
1445  else {
1446  BKE_modifier_deform_verts(md, &mectx, mesh_final, deformed_verts, num_deformed_verts);
1447  }
1448  }
1449  else {
1450  /* apply vertex coordinates or build a DerivedMesh as necessary */
1451  if (mesh_final) {
1452  if (deformed_verts) {
1453  Mesh *mesh_tmp = BKE_mesh_copy_for_eval(mesh_final, false);
1454  if (mesh_final != mesh_cage) {
1455  BKE_id_free(nullptr, mesh_final);
1456  }
1457  mesh_final = mesh_tmp;
1458  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
1459  }
1460  else if (mesh_final == mesh_cage) {
1461  /* 'me' may be changed by this modifier, so we need to copy it. */
1462  mesh_final = BKE_mesh_copy_for_eval(mesh_final, false);
1463  }
1464  }
1465  else {
1467  em_input, nullptr, deformed_verts, mesh_input);
1468  deformed_verts = nullptr;
1469  }
1470 
1471  /* create an orco derivedmesh in parallel */
1472  CustomData_MeshMasks mask = md_datamask->mask;
1473  if (mask.vmask & CD_MASK_ORCO) {
1474  if (!mesh_orco) {
1475  mesh_orco = create_orco_mesh(ob, mesh_input, em_input, CD_ORCO);
1476  }
1477 
1478  mask.vmask &= ~CD_MASK_ORCO;
1479  mask.vmask |= CD_MASK_ORIGINDEX;
1480  mask.emask |= CD_MASK_ORIGINDEX;
1481  mask.pmask |= CD_MASK_ORIGINDEX;
1482  mesh_set_only_copy(mesh_orco, &mask);
1483 
1484  Mesh *mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco);
1485  ASSERT_IS_VALID_MESH(mesh_next);
1486 
1487  if (mesh_next) {
1488  /* if the modifier returned a new dm, release the old one */
1489  if (mesh_orco && mesh_orco != mesh_next) {
1490  BKE_id_free(nullptr, mesh_orco);
1491  }
1492  mesh_orco = mesh_next;
1493  }
1494  }
1495 
1496  /* set the DerivedMesh to only copy needed data */
1497  CustomData_MeshMasks_update(&mask, &append_mask);
1498  /* XXX WHAT? overwrites mask ??? */
1499  /* CD_MASK_ORCO may have been cleared above */
1500  mask = md_datamask->mask;
1501  mask.vmask |= CD_MASK_ORIGINDEX;
1502  mask.emask |= CD_MASK_ORIGINDEX;
1503  mask.pmask |= CD_MASK_ORIGINDEX;
1504 
1505  mesh_set_only_copy(mesh_final, &mask);
1506 
1507  if (mask.lmask & CD_MASK_ORIGSPACE_MLOOP) {
1508  if (!CustomData_has_layer(&mesh_final->ldata, CD_ORIGSPACE_MLOOP)) {
1510  &mesh_final->ldata, CD_ORIGSPACE_MLOOP, CD_CALLOC, nullptr, mesh_final->totloop);
1511  mesh_init_origspace(mesh_final);
1512  }
1513  }
1514 
1516  md, mectx, mesh_final, geometry_set_final);
1517  ASSERT_IS_VALID_MESH(mesh_next);
1518 
1519  if (mesh_next) {
1520  if (mesh_final && mesh_final != mesh_next) {
1521  BKE_id_free(nullptr, mesh_final);
1522  }
1523  mesh_final = mesh_next;
1524 
1525  if (deformed_verts) {
1526  MEM_freeN(deformed_verts);
1527  deformed_verts = nullptr;
1528  }
1529  }
1530  mesh_final->runtime.deformed_only = false;
1531  }
1532 
1533  if (r_cage && i == cageIndex) {
1534  if (mesh_final && deformed_verts) {
1535  mesh_cage = BKE_mesh_copy_for_eval(mesh_final, false);
1536  BKE_mesh_vert_coords_apply(mesh_cage, deformed_verts);
1537  }
1538  else if (mesh_final) {
1539  mesh_cage = mesh_final;
1540  }
1541  else {
1542  Mesh *me_orig = mesh_input;
1543  /* Modifying the input mesh is weak, however as there can only be one object in edit mode
1544  * even if multiple are sharing the same mesh this should be thread safe. */
1545  if ((me_orig->id.tag & LIB_TAG_COPIED_ON_WRITE) && (ob->mode & OB_MODE_EDIT)) {
1546  if (!BKE_mesh_runtime_ensure_edit_data(me_orig)) {
1548  }
1549  me_orig->runtime.edit_data->vertexCos = (const float(*)[3])MEM_dupallocN(deformed_verts);
1550  }
1552  em_input,
1553  &final_datamask,
1554  deformed_verts ? (const float(*)[3])MEM_dupallocN(deformed_verts) : nullptr,
1555  mesh_input);
1556  }
1557  }
1558 
1559  isPrevDeform = (mti->type == eModifierTypeType_OnlyDeform);
1560  }
1561 
1562  BLI_linklist_free((LinkNode *)datamasks, nullptr);
1563 
1564  /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
1565  * to apply these back onto the DerivedMesh. If we have no DerivedMesh
1566  * then we need to build one. */
1567  if (mesh_final) {
1568  if (deformed_verts) {
1569  Mesh *mesh_tmp = BKE_mesh_copy_for_eval(mesh_final, false);
1570  if (mesh_final != mesh_cage) {
1571  BKE_id_free(nullptr, mesh_final);
1572  }
1573  mesh_final = mesh_tmp;
1574  BKE_mesh_vert_coords_apply(mesh_final, deformed_verts);
1575  }
1576  }
1577  else if (!deformed_verts && mesh_cage) {
1578  /* cage should already have up to date normals */
1579  mesh_final = mesh_cage;
1580  }
1581  else {
1582  /* this is just a copy of the editmesh, no need to calc normals */
1584  em_input, &final_datamask, deformed_verts, mesh_input);
1585  deformed_verts = nullptr;
1586  }
1587 
1588  if (deformed_verts) {
1589  MEM_freeN(deformed_verts);
1590  }
1591 
1592  /* Add orco coordinates to final and deformed mesh if requested. */
1593  if (final_datamask.vmask & CD_MASK_ORCO) {
1594  /* FIXME(Campbell): avoid the need to convert to mesh data just to add an orco layer. */
1595  BKE_mesh_wrapper_ensure_mdata(mesh_final);
1596 
1597  add_orco_mesh(ob, em_input, mesh_final, mesh_orco, CD_ORCO);
1598  }
1599 
1600  if (mesh_orco) {
1601  BKE_id_free(nullptr, mesh_orco);
1602  }
1603 
1604  /* Compute normals. */
1605  editbmesh_calc_modifier_final_normals_or_defer(mesh_final, &final_datamask);
1606  if (mesh_cage && (mesh_cage != mesh_final)) {
1607  editbmesh_calc_modifier_final_normals_or_defer(mesh_cage, &final_datamask);
1608  }
1609 
1610  /* Return final mesh. */
1611  *r_final = mesh_final;
1612  if (r_cage) {
1613  *r_cage = mesh_cage;
1614  }
1615  if (r_geometry_set) {
1616  *r_geometry_set = new GeometrySet(std::move(geometry_set_final));
1617  }
1618 }
1619 
1620 static void mesh_build_extra_data(struct Depsgraph *depsgraph, Object *ob, Mesh *mesh_eval)
1621 {
1622  uint32_t eval_flags = DEG_get_eval_flags_for_id(depsgraph, &ob->id);
1623 
1624  if (eval_flags & DAG_EVAL_NEED_SHRINKWRAP_BOUNDARY) {
1626  }
1627 }
1628 
1630  const Scene *scene,
1631  Object *ob,
1632  const CustomData_MeshMasks *dataMask,
1633  const bool need_mapping)
1634 {
1635 #if 0 /* XXX This is already taken care of in #mesh_calc_modifiers... */
1636  if (need_mapping) {
1637  /* Also add the flag so that it is recorded in lastDataMask. */
1638  dataMask->vmask |= CD_MASK_ORIGINDEX;
1639  dataMask->emask |= CD_MASK_ORIGINDEX;
1640  dataMask->pmask |= CD_MASK_ORIGINDEX;
1641  }
1642 #endif
1643 
1644  Mesh *mesh_eval = nullptr, *mesh_deform_eval = nullptr;
1645  GeometrySet *geometry_set_eval = nullptr;
1647  scene,
1648  ob,
1649  true,
1650  need_mapping,
1651  dataMask,
1652  true,
1653  true,
1654  &mesh_deform_eval,
1655  &mesh_eval,
1656  &geometry_set_eval);
1657 
1658  /* The modifier stack evaluation is storing result in mesh->runtime.mesh_eval, but this result
1659  * is not guaranteed to be owned by object.
1660  *
1661  * Check ownership now, since later on we can not go to a mesh owned by someone else via
1662  * object's runtime: this could cause access freed data on depsgraph destruction (mesh who owns
1663  * the final result might be freed prior to object). */
1664  Mesh *mesh = (Mesh *)ob->data;
1665  const bool is_mesh_eval_owned = (mesh_eval != mesh->runtime.mesh_eval);
1666  BKE_object_eval_assign_data(ob, &mesh_eval->id, is_mesh_eval_owned);
1667 
1668  /* Add the final mesh as a non-owning component to the geometry set. */
1669  MeshComponent &mesh_component = geometry_set_eval->get_component_for_write<MeshComponent>();
1670  mesh_component.replace(mesh_eval, GeometryOwnershipType::Editable);
1671  ob->runtime.geometry_set_eval = geometry_set_eval;
1672 
1673  ob->runtime.mesh_deform_eval = mesh_deform_eval;
1674  ob->runtime.last_data_mask = *dataMask;
1675  ob->runtime.last_need_mapping = need_mapping;
1676 
1677  BKE_object_boundbox_calc_from_mesh(ob, mesh_eval);
1678 
1679  /* Make sure that drivers can target shapekey properties.
1680  * Note that this causes a potential inconsistency, as the shapekey may have a
1681  * different topology than the evaluated mesh. */
1682  BLI_assert(mesh->key == nullptr || DEG_is_evaluated_id(&mesh->key->id));
1683  mesh_eval->key = mesh->key;
1684 
1685  if ((ob->mode & OB_MODE_ALL_SCULPT) && ob->sculpt) {
1686  if (DEG_is_active(depsgraph)) {
1688  }
1689  }
1690 
1691  mesh_build_extra_data(depsgraph, ob, mesh_eval);
1692 }
1693 
1695  const Scene *scene,
1696  Object *obedit,
1697  BMEditMesh *em,
1698  CustomData_MeshMasks *dataMask)
1699 {
1700  Mesh *mesh = static_cast<Mesh *>(obedit->data);
1701  Mesh *me_cage;
1702  Mesh *me_final;
1703  GeometrySet *non_mesh_components;
1704 
1706  depsgraph, scene, obedit, em, dataMask, &me_cage, &me_final, &non_mesh_components);
1707 
1708  /* The modifier stack result is expected to share edit mesh pointer with the input.
1709  * This is similar `mesh_calc_finalize()`. */
1710  BKE_mesh_free_editmesh(me_final);
1711  BKE_mesh_free_editmesh(me_cage);
1712  me_final->edit_mesh = me_cage->edit_mesh = em;
1713 
1714  /* Object has edit_mesh but is not in edit mode (object shares mesh datablock with another object
1715  * with is in edit mode).
1716  * Convert edit mesh to mesh until the draw manager can draw mesh wrapper which is not in the
1717  * edit mode. */
1718  if (!(obedit->mode & OB_MODE_EDIT)) {
1720  if (me_final != me_cage) {
1722  }
1723  }
1724 
1725  const bool is_mesh_eval_owned = (me_final != mesh->runtime.mesh_eval);
1726  BKE_object_eval_assign_data(obedit, &me_final->id, is_mesh_eval_owned);
1727 
1728  obedit->runtime.editmesh_eval_cage = me_cage;
1729 
1730  obedit->runtime.geometry_set_eval = non_mesh_components;
1731 
1732  BKE_object_boundbox_calc_from_mesh(obedit, me_final);
1733 
1734  obedit->runtime.last_data_mask = *dataMask;
1735 }
1736 
1738  Object *ob,
1739  CustomData_MeshMasks *r_mask,
1740  bool *r_need_mapping)
1741 {
1743 
1745 
1746  if (r_need_mapping) {
1747  *r_need_mapping = false;
1748  }
1749 
1750  /* Must never access original objects when dependency graph is not active: it might be already
1751  * freed. */
1752  if (!DEG_is_active(depsgraph)) {
1753  return;
1754  }
1755 
1756  Object *actob = view_layer->basact ? DEG_get_original_object(view_layer->basact->object) :
1757  nullptr;
1758  if (DEG_get_original_object(ob) == actob) {
1759  bool editing = BKE_paint_select_face_test(actob);
1760 
1761  /* weight paint and face select need original indices because of selection buffer drawing */
1762  if (r_need_mapping) {
1763  *r_need_mapping = (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT | OB_MODE_VERTEX_PAINT)));
1764  }
1765 
1766  /* check if we need tfaces & mcols due to face select or texture paint */
1767  if ((ob->mode & OB_MODE_TEXTURE_PAINT) || editing) {
1769  r_mask->fmask |= CD_MASK_MTFACE;
1770  }
1771 
1772  /* check if we need mcols due to vertex paint or weightpaint */
1773  if (ob->mode & OB_MODE_VERTEX_PAINT) {
1774  r_mask->lmask |= CD_MASK_PROP_BYTE_COLOR;
1775  }
1776 
1777  if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1778  r_mask->vmask |= CD_MASK_MDEFORMVERT;
1779  }
1780 
1781  if (ob->mode & OB_MODE_EDIT) {
1782  r_mask->vmask |= CD_MASK_MVERT_SKIN;
1783  }
1784  }
1785 }
1786 
1788  const Scene *scene,
1789  Object *ob,
1790  const CustomData_MeshMasks *dataMask)
1791 {
1792  BLI_assert(ob->type == OB_MESH);
1793 
1794  /* Evaluated meshes aren't supposed to be created on original instances. If you do,
1795  * they aren't cleaned up properly on mode switch, causing crashes, e.g T58150. */
1797 
1799  if (DEG_is_active(depsgraph)) {
1801  }
1802 
1803  /* NOTE: Access the `edit_mesh` after freeing the derived caches, so that `ob->data` is restored
1804  * to the pre-evaluated state. This is because the evaluated state is not necessarily sharing the
1805  * `edit_mesh` pointer with the input. For example, if the object is first evaluated in the
1806  * object mode, and then user in another scene moves object to edit mode. */
1807  BMEditMesh *em = ((Mesh *)ob->data)->edit_mesh;
1808 
1809  bool need_mapping;
1810  CustomData_MeshMasks cddata_masks = *dataMask;
1811  object_get_datamask(depsgraph, ob, &cddata_masks, &need_mapping);
1812 
1813  if (em) {
1814  editbmesh_build_data(depsgraph, scene, ob, em, &cddata_masks);
1815  }
1816  else {
1817  mesh_build_data(depsgraph, scene, ob, &cddata_masks, need_mapping);
1818  }
1819 }
1820 
1821 /***/
1822 
1824  const Scene *scene,
1825  Object *ob,
1826  const CustomData_MeshMasks *dataMask)
1827 {
1828  /* This function isn't thread-safe and can't be used during evaluation. */
1830 
1831  /* Evaluated meshes aren't supposed to be created on original instances. If you do,
1832  * they aren't cleaned up properly on mode switch, causing crashes, e.g T58150. */
1834 
1835  /* if there's no evaluated mesh or the last data mask used doesn't include
1836  * the data we need, rebuild the derived mesh
1837  */
1838  bool need_mapping;
1839  CustomData_MeshMasks cddata_masks = *dataMask;
1840  object_get_datamask(depsgraph, ob, &cddata_masks, &need_mapping);
1841 
1842  Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
1843  if ((mesh_eval == nullptr) ||
1844  !CustomData_MeshMasks_are_matching(&(ob->runtime.last_data_mask), &cddata_masks) ||
1845  (need_mapping && !ob->runtime.last_need_mapping)) {
1846  CustomData_MeshMasks_update(&cddata_masks, &ob->runtime.last_data_mask);
1847 
1848  makeDerivedMesh(depsgraph, scene, ob, dataMask);
1849 
1850  mesh_eval = BKE_object_get_evaluated_mesh(ob);
1851  }
1852 
1854 
1855  return mesh_eval;
1856 }
1857 
1859  const Scene *scene,
1860  Object *ob,
1861  const CustomData_MeshMasks *dataMask)
1862 {
1863  BMEditMesh *em = ((Mesh *)ob->data)->edit_mesh;
1864  if (em != nullptr) {
1865  /* There is no such a concept as deformed mesh in edit mode.
1866  * Explicitly disallow this request so that the evaluated result is not modified with evaluated
1867  * result from the wrong mode. */
1868  BLI_assert_msg(0, "Request of derformed mesh of object which is in edit mode");
1869  return nullptr;
1870  }
1871 
1872  /* This function isn't thread-safe and can't be used during evaluation. */
1874 
1875  /* Evaluated meshes aren't supposed to be created on original instances. If you do,
1876  * they aren't cleaned up properly on mode switch, causing crashes, e.g T58150. */
1878 
1879  /* if there's no derived mesh or the last data mask used doesn't include
1880  * the data we need, rebuild the derived mesh
1881  */
1882  bool need_mapping;
1883 
1884  CustomData_MeshMasks cddata_masks = *dataMask;
1885  object_get_datamask(depsgraph, ob, &cddata_masks, &need_mapping);
1886 
1887  if (!ob->runtime.mesh_deform_eval ||
1888  !CustomData_MeshMasks_are_matching(&(ob->runtime.last_data_mask), &cddata_masks) ||
1889  (need_mapping && !ob->runtime.last_need_mapping)) {
1890  CustomData_MeshMasks_update(&cddata_masks, &ob->runtime.last_data_mask);
1892  depsgraph, scene, ob, &cddata_masks, need_mapping || ob->runtime.last_need_mapping);
1893  }
1894 
1895  return ob->runtime.mesh_deform_eval;
1896 }
1897 
1899  const Scene *scene,
1900  Object *ob,
1901  const CustomData_MeshMasks *dataMask)
1902 {
1903  Mesh *result;
1905  depsgraph, scene, ob, true, false, dataMask, false, false, nullptr, &result, nullptr);
1906  return result;
1907 }
1908 
1910  const Scene *scene,
1911  Object *ob,
1912  const CustomData_MeshMasks *dataMask)
1913 {
1914  Mesh *result;
1916  depsgraph, scene, ob, false, false, dataMask, false, false, nullptr, &result, nullptr);
1917  return result;
1918 }
1919 
1921  const Scene *scene,
1922  Object *ob,
1923  const CustomData_MeshMasks *dataMask)
1924 {
1925  Mesh *result;
1927  depsgraph, scene, ob, false, false, dataMask, false, false, nullptr, &result, nullptr);
1928  return result;
1929 }
1930 
1931 /***/
1932 
1934  const Scene *scene,
1935  Object *obedit,
1936  BMEditMesh *em,
1937  const CustomData_MeshMasks *dataMask)
1938 {
1939  CustomData_MeshMasks cddata_masks = *dataMask;
1940 
1941  /* if there's no derived mesh or the last data mask used doesn't include
1942  * the data we need, rebuild the derived mesh
1943  */
1944  object_get_datamask(depsgraph, obedit, &cddata_masks, nullptr);
1945 
1946  if (!obedit->runtime.editmesh_eval_cage ||
1947  !CustomData_MeshMasks_are_matching(&(obedit->runtime.last_data_mask), &cddata_masks)) {
1948  editbmesh_build_data(depsgraph, scene, obedit, em, &cddata_masks);
1949  }
1950 
1951  return obedit->runtime.editmesh_eval_cage;
1952 }
1953 
1955  const Scene *scene,
1956  Object *obedit,
1957  const CustomData_MeshMasks *dataMask)
1958 {
1959  BLI_assert((obedit->id.tag & LIB_TAG_COPIED_ON_WRITE) == 0);
1960  const Scene *scene_eval = (const Scene *)DEG_get_evaluated_id(depsgraph, (ID *)&scene->id);
1961  Object *obedit_eval = (Object *)DEG_get_evaluated_id(depsgraph, &obedit->id);
1962  BMEditMesh *em_eval = BKE_editmesh_from_object(obedit_eval);
1963  return editbmesh_get_eval_cage(depsgraph, scene_eval, obedit_eval, em_eval, dataMask);
1964 }
1965 
1966 /***/
1967 
1968 /* same as above but for vert coords */
1972 };
1973 
1974 static void make_vertexcos__mapFunc(void *userData,
1975  int index,
1976  const float co[3],
1977  const float UNUSED(no[3]))
1978 {
1979  MappedUserData *mappedData = (MappedUserData *)userData;
1980 
1981  if (BLI_BITMAP_TEST(mappedData->vertex_visit, index) == 0) {
1982  /* we need coord from prototype vertex, not from copies,
1983  * assume they stored in the beginning of vertex array stored in DM
1984  * (mirror modifier for eg does this) */
1985  copy_v3_v3(mappedData->vertexcos[index], co);
1986  BLI_BITMAP_ENABLE(mappedData->vertex_visit, index);
1987  }
1988 }
1989 
1990 void mesh_get_mapped_verts_coords(Mesh *me_eval, float (*r_cos)[3], const int totcos)
1991 {
1992  if (me_eval->runtime.deformed_only == false) {
1993  MappedUserData userData;
1994  memset(r_cos, 0, sizeof(*r_cos) * totcos);
1995  userData.vertexcos = r_cos;
1996  userData.vertex_visit = BLI_BITMAP_NEW(totcos, "vertexcos flags");
1998  MEM_freeN(userData.vertex_visit);
1999  }
2000  else {
2001  MVert *mv = me_eval->mvert;
2002  for (int i = 0; i < totcos; i++, mv++) {
2003  copy_v3_v3(r_cos[i], mv->co);
2004  }
2005  }
2006 }
2007 
2009 {
2010  const float default_osf[4][2] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2011 
2014  const int numpoly = mesh->totpoly;
2015  // const int numloop = mesh->totloop;
2016  MVert *mv = mesh->mvert;
2017  MLoop *ml = mesh->mloop;
2018  MPoly *mp = mesh->mpoly;
2019  int i, j, k;
2020 
2022 
2023  for (i = 0; i < numpoly; i++, mp++) {
2024  OrigSpaceLoop *lof = lof_array + mp->loopstart;
2025 
2026  if (ELEM(mp->totloop, 3, 4)) {
2027  for (j = 0; j < mp->totloop; j++, lof++) {
2028  copy_v2_v2(lof->uv, default_osf[j]);
2029  }
2030  }
2031  else {
2032  MLoop *l = &ml[mp->loopstart];
2033  float p_nor[3], co[3];
2034  float mat[3][3];
2035 
2036  float min[2] = {FLT_MAX, FLT_MAX}, max[2] = {-FLT_MAX, -FLT_MAX};
2037  float translate[2], scale[2];
2038 
2039  BKE_mesh_calc_poly_normal(mp, l, mv, p_nor);
2040  axis_dominant_v3_to_m3(mat, p_nor);
2041 
2042  vcos_2d.resize(mp->totloop);
2043  for (j = 0; j < mp->totloop; j++, l++) {
2044  mul_v3_m3v3(co, mat, mv[l->v].co);
2045  copy_v2_v2(vcos_2d[j], co);
2046 
2047  for (k = 0; k < 2; k++) {
2048  if (co[k] > max[k]) {
2049  max[k] = co[k];
2050  }
2051  else if (co[k] < min[k]) {
2052  min[k] = co[k];
2053  }
2054  }
2055  }
2056 
2057  /* Brings min to (0, 0). */
2058  negate_v2_v2(translate, min);
2059 
2060  /* Scale will bring max to (1, 1). */
2061  sub_v2_v2v2(scale, max, min);
2062  if (scale[0] == 0.0f) {
2063  scale[0] = 1e-9f;
2064  }
2065  if (scale[1] == 0.0f) {
2066  scale[1] = 1e-9f;
2067  }
2068  invert_v2(scale);
2069 
2070  /* Finally, transform all vcos_2d into ((0, 0), (1, 1))
2071  * square and assign them as origspace. */
2072  for (j = 0; j < mp->totloop; j++, lof++) {
2073  add_v2_v2v2(lof->uv, vcos_2d[j], translate);
2074  mul_v2_v2(lof->uv, scale);
2075  }
2076  }
2077  }
2078 
2080 }
typedef float(TangentPoint)[2]
DerivedMeshType
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:27
void CustomData_free(struct CustomData *data, int totelem)
Definition: customdata.cc:2373
const CustomData_MeshMasks CD_MASK_BAREMESH_ORIGINDEX
Definition: customdata.cc:2058
@ CD_ASSIGN
@ CD_CALLOC
@ CD_DUPLICATE
void CustomData_copy(const struct CustomData *source, struct CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
void CustomData_free_layers(struct CustomData *data, int type, int totelem)
Definition: customdata.cc:2904
bool CustomData_has_layer(const struct CustomData *data, int type)
void CustomData_MeshMasks_update(CustomData_MeshMasks *mask_dst, const CustomData_MeshMasks *mask_src)
Definition: customdata.cc:77
bool CustomData_MeshMasks_are_matching(const CustomData_MeshMasks *mask_ref, const CustomData_MeshMasks *mask_required)
Definition: customdata.cc:87
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.cc:2051
void CustomData_interp(const struct CustomData *source, struct CustomData *dest, const int *src_indices, const float *weights, const float *sub_weights, int count, int dest_index)
void CustomData_set_only_copy(const struct CustomData *data, eCustomDataMask mask)
void * CustomData_get_layer(const struct CustomData *data, int type)
void CustomData_free_temporary(struct CustomData *data, int totelem)
Definition: customdata.cc:3041
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.cc:2776
void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.cc:2626
const CustomData_MeshMasks CD_MASK_DERIVEDMESH
Definition: customdata.cc:2077
void CustomData_copy_data(const struct CustomData *source, struct CustomData *dest, int source_index, int dest_index, int count)
support for deformation groups and hooks.
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:58
struct KeyBlock * BKE_keyblock_from_key(struct Key *key, int index)
Definition: key.c:1913
void BKE_keyblock_convert_from_mesh(const struct Mesh *me, const struct Key *key, struct KeyBlock *kb)
struct Key * BKE_key_from_object(struct Object *ob)
Definition: key.c:1803
void BKE_id_free(struct Main *bmain, void *idv)
General operations, lookup, etc. for materials.
struct Mesh * BKE_mesh_copy_for_eval(const struct Mesh *source, bool reference)
float(* BKE_mesh_orco_verts_get(struct Object *ob))[3]
Definition: mesh.cc:1314
void BKE_mesh_tessface_clear(struct Mesh *mesh)
Definition: mesh.cc:1654
void BKE_mesh_copy_parameters_for_eval(struct Mesh *me_dst, const struct Mesh *me_src)
void BKE_mesh_free_editmesh(struct Mesh *mesh)
Definition: mesh.cc:169
void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float(*vert_coords)[3])
Definition: mesh.cc:1834
void BKE_mesh_assert_normals_dirty_or_calculated(const struct Mesh *mesh)
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.cc:991
void BKE_mesh_update_customdata_pointers(struct Mesh *me, bool do_ensure_tess_cd)
Definition: mesh.cc:874
struct Mesh * BKE_mesh_from_bmesh_for_eval_nomain(struct BMesh *bm, const struct CustomData_MeshMasks *cd_mask_extra, const struct Mesh *me_settings)
void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly, const struct MLoop *loopstart, const struct MVert *mvarray, float r_no[3])
void BKE_mesh_calc_normals_split(struct Mesh *mesh)
Definition: mesh.cc:1911
void BKE_mesh_orco_verts_transform(struct Mesh *me, float(*orco)[3], int totvert, int invert)
Definition: mesh.cc:1331
float(* BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3]
void BKE_mesh_ensure_default_orig_index_customdata(struct Mesh *mesh)
Definition: mesh.cc:1194
void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh)
void BKE_mesh_foreach_mapped_vert(struct Mesh *mesh, void(*func)(void *userData, int index, const float co[3], const float no[3]), void *userData, MeshForeachFlag flag)
@ MESH_FOREACH_NOP
bool BKE_mesh_runtime_reset_edit_data(struct Mesh *mesh)
bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh)
void BKE_mesh_wrapper_ensure_mdata(struct Mesh *me)
Definition: mesh_wrapper.cc:94
struct Mesh * BKE_mesh_wrapper_from_editmesh_with_coords(struct BMEditMesh *em, const struct CustomData_MeshMasks *cd_mask_extra, const float(*vert_coords)[3], const struct Mesh *me_settings)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
void BKE_modifier_deform_vertsEM(ModifierData *md, const struct ModifierEvalContext *ctx, struct BMEditMesh *em, struct Mesh *me, float(*vertexCos)[3], int numVerts)
bool BKE_modifier_is_enabled(const struct Scene *scene, struct ModifierData *md, int required_mode)
struct Mesh * BKE_modifier_modify_mesh(ModifierData *md, const struct ModifierEvalContext *ctx, struct Mesh *me)
@ eModifierTypeFlag_RequiresOriginalData
Definition: BKE_modifier.h:84
void BKE_modifier_deform_verts(ModifierData *md, const struct ModifierEvalContext *ctx, struct Mesh *me, float(*vertexCos)[3], int numVerts)
struct ModifierData * BKE_modifier_get_last_preview(const struct Scene *scene, struct ModifierData *md, int required_mode)
struct ModifierData * BKE_modifiers_get_virtual_modifierlist(const struct Object *ob, struct VirtualModifierData *data)
struct ModifierData * BKE_modifiers_findby_type(const struct Object *ob, ModifierType type)
bool BKE_modifier_supports_mapping(struct ModifierData *md)
@ eModifierTypeType_OnlyDeform
Definition: BKE_modifier.h:44
void BKE_modifier_free_temporary_data(struct ModifierData *md)
void BKE_modifier_set_error(const struct Object *ob, struct ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_modifiers_clear_errors(struct Object *ob)
struct CDMaskLink * BKE_modifier_calc_data_masks(const struct Scene *scene, struct Object *ob, struct ModifierData *md, struct CustomData_MeshMasks *final_datamask, int required_mode, ModifierData *previewmd, const struct CustomData_MeshMasks *previewmask)
ModifierApplyFlag
Definition: BKE_modifier.h:113
@ MOD_APPLY_USECACHE
Definition: BKE_modifier.h:118
@ MOD_APPLY_RENDER
Definition: BKE_modifier.h:115
@ MOD_APPLY_ORCO
Definition: BKE_modifier.h:120
int BKE_modifiers_get_cage_index(const struct Scene *scene, struct Object *ob, int *r_lastPossibleCageIndex, bool is_virtual)
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_evaluated_mesh(const struct Object *object)
void BKE_object_free_derived_caches(struct Object *ob)
Definition: object.cc:1774
void BKE_object_boundbox_calc_from_mesh(struct Object *ob, const struct Mesh *me_eval)
void BKE_object_eval_assign_data(struct Object *object, struct ID *data, bool is_owned)
Definition: object.cc:1745
Functions for dealing with objects and deform verts, used by painting and tools.
void BKE_sculpt_update_object_before_eval(const struct Scene *scene, struct Object *ob_eval)
struct MultiresModifierData * BKE_sculpt_multires_active(const struct Scene *scene, struct Object *ob)
bool BKE_paint_select_face_test(struct Object *ob)
Definition: paint.c:980
void BKE_sculpt_update_object_after_eval(struct Depsgraph *depsgraph, struct Object *ob_eval)
Definition: paint.c:1869
void BKE_shrinkwrap_compute_boundary_data(struct Mesh *mesh)
Definition: shrinkwrap.c:323
A (mainly) macro array library.
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:64
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:102
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
Definition: math_geom.c:3527
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:897
MINLINE void mul_v2_v2(float r[2], const float a[2])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v2_v2(float r[2], const float a[2])
void range_vn_i(int *array_tar, int size, int start)
Definition: math_vector.c:1021
void copy_vn_i(int *array_tar, int size, int val)
Definition: math_vector.c:1223
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void invert_v2(float r[2])
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
pthread_rwlock_t ThreadRWMutex
Definition: BLI_threads.h:125
#define THREAD_LOCK_READ
Definition: BLI_threads.h:120
#define THREAD_LOCK_WRITE
Definition: BLI_threads.h:121
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition: threads.cc:488
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:373
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:378
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition: threads.cc:498
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:82
#define SWAP(type, a, b)
#define UNUSED(x)
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
bool DEG_is_active(const struct Depsgraph *depsgraph)
Definition: depsgraph.cc:312
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:46
bool DEG_is_evaluating(const struct Depsgraph *depsgraph)
Definition: depsgraph.cc:306
@ DAG_EVAL_NEED_SHRINKWRAP_BOUNDARY
Definition: DEG_depsgraph.h:57
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
struct Object * DEG_get_original_object(struct Object *object)
struct ID * DEG_get_evaluated_id(const struct Depsgraph *depsgraph, struct ID *id)
struct ViewLayer * DEG_get_evaluated_view_layer(const struct Depsgraph *graph)
uint32_t DEG_get_eval_flags_for_id(const struct Depsgraph *graph, const struct ID *id)
bool DEG_is_evaluated_id(const struct ID *id)
void DEG_get_customdata_mask_for_object(const struct Depsgraph *graph, struct Object *object, struct CustomData_MeshMasks *r_mask)
@ LIB_TAG_COPIED_ON_WRITE_EVAL_RESULT
Definition: DNA_ID.h:730
@ LIB_TAG_COPIED_ON_WRITE
Definition: DNA_ID.h:720
#define CD_MASK_PROP_BYTE_COLOR
#define CD_MASK_NORMAL
@ CD_FLAG_TEMPORARY
#define CD_MASK_ORIGINDEX
#define CD_MASK_ORCO
#define CD_MASK_MDEFORMVERT
#define CD_MASK_MVERT_SKIN
#define CD_MASK_MTFACE
#define CD_MASK_ORIGSPACE_MLOOP
@ CD_ORIGINDEX
@ CD_NUMTYPES
@ CD_ORIGSPACE_MLOOP
@ CD_MEDGE
@ CD_CLOTH_ORCO
@ CD_MVERT
#define CD_MASK_CLOTH_ORCO
#define CD_MASK_PREVIEW_MLOOPCOL
#define CD_MASK_MLOOPUV
@ ME_WRAPPER_TYPE_MDATA
@ ME_WRAPPER_TYPE_BMESH
@ ME_AUTOSMOOTH
@ eModifierMode_Render
@ eModifierMode_Editmode
@ eModifierMode_Realtime
ModifierType
@ eModifierType_Cloth
@ eModifierType_DynamicPaint
@ eModifierType_Multires
@ OB_MODE_EDIT
@ OB_MODE_WEIGHT_PAINT
@ OB_MODE_SCULPT
@ OB_MODE_TEXTURE_PAINT
@ OB_MODE_OBJECT
@ OB_MODE_VERTEX_PAINT
#define OB_MODE_ALL_SCULPT
Object is a sort of wrapper for general info.
@ OB_MODIFIER_FLAG_ADD_REST_POSITION
@ OB_MESH
@ SCULPT_ONLY_DEFORM
static float(* get_editbmesh_orco_verts(BMEditMesh *em))[3]
Definition: DerivedMesh.cc:477
static ThreadRWMutex loops_cache_lock
Definition: DerivedMesh.cc:82
void mesh_get_mapped_verts_coords(Mesh *me_eval, float(*r_cos)[3], const int totcos)
static float(* get_orco_coords(Object *ob, BMEditMesh *em, int layer, int *free))[3]
Definition: DerivedMesh.cc:497
Mesh * mesh_get_eval_deform(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static void mesh_build_extra_data(struct Depsgraph *depsgraph, Object *ob, Mesh *mesh_eval)
void makeDerivedMesh(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static MEdge * dm_getEdgeArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:106
Mesh * editbmesh_get_eval_cage_from_orig(struct Depsgraph *depsgraph, const Scene *scene, Object *obedit, const CustomData_MeshMasks *dataMask)
static void mesh_calc_modifier_final_normals(const Mesh *mesh_input, const CustomData_MeshMasks *final_datamask, const bool sculpt_dyntopo, Mesh *mesh_final)
Definition: DerivedMesh.cc:613
static void mesh_calc_finalize(const Mesh *mesh_input, Mesh *mesh_eval)
Definition: DerivedMesh.cc:659
void * DM_get_loop_data_layer(DerivedMesh *dm, int type)
Definition: DerivedMesh.cc:455
Mesh * mesh_create_eval_no_deform(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
void * DM_get_poly_data_layer(DerivedMesh *dm, int type)
Definition: DerivedMesh.cc:450
static MEdge * dm_dupEdgeArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:160
static MVert * dm_dupVertArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:148
void BKE_mesh_runtime_eval_to_meshkey(Mesh *me_deformed, Mesh *me, KeyBlock *kb)
Definition: DerivedMesh.cc:390
static void editbmesh_calc_modifiers(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, BMEditMesh *em_input, const CustomData_MeshMasks *dataMask, Mesh **r_cage, Mesh **r_final, GeometrySet **r_geometry_set)
static void mesh_init_origspace(Mesh *mesh)
float(* editbmesh_vert_coords_alloc(BMEditMesh *em, int *r_vert_len))[3]
void DM_init(DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges, int numTessFaces, int numLoops, int numPolys)
Definition: DerivedMesh.cc:250
static MLoop * dm_dupLoopArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:172
static void mesh_calc_modifiers(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, const bool use_deform, const bool need_mapping, const CustomData_MeshMasks *dataMask, const bool use_cache, const bool allow_shared_mesh, Mesh **r_deform, Mesh **r_final, GeometrySet **r_geometry_set)
Definition: DerivedMesh.cc:731
static int dm_getNumLoopTri(DerivedMesh *dm)
Definition: DerivedMesh.cc:196
void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest, int *src_indices, float *weights, int count, int dest_index)
Definition: DerivedMesh.cc:466
bool DM_release(DerivedMesh *dm)
Definition: DerivedMesh.cc:307
void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type, int numVerts, int numEdges, int numTessFaces, int numLoops, int numPolys)
Definition: DerivedMesh.cc:277
void DM_ensure_looptri_data(DerivedMesh *dm)
Definition: DerivedMesh.cc:362
static void make_vertexcos__mapFunc(void *userData, int index, const float co[3], const float UNUSED(no[3]))
static MPoly * dm_getPolyArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:134
Mesh * editbmesh_get_eval_cage(struct Depsgraph *depsgraph, const Scene *scene, Object *obedit, BMEditMesh *em, const CustomData_MeshMasks *dataMask)
static void editbmesh_calc_modifier_final_normals_or_defer(Mesh *mesh_final, const CustomData_MeshMasks *final_datamask)
bool editbmesh_modifier_is_enabled(const Scene *scene, const Object *ob, ModifierData *md, bool has_prev_mesh)
void * DM_get_edge_data_layer(DerivedMesh *dm, int type)
Definition: DerivedMesh.cc:441
static bool mesh_has_modifier_final_normals(const Mesh *mesh_input, const CustomData_MeshMasks *final_datamask, Mesh *mesh_final)
Definition: DerivedMesh.cc:600
static void mesh_build_data(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask, const bool need_mapping)
static MPoly * dm_dupPolyArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:184
static void object_get_datamask(const Depsgraph *depsgraph, Object *ob, CustomData_MeshMasks *r_mask, bool *r_need_mapping)
static MVert * dm_getVertArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:92
void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
Definition: DerivedMesh.cc:332
void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest, int source_index, int dest_index, int count)
Definition: DerivedMesh.cc:460
void DM_init_funcs(DerivedMesh *dm)
Definition: DerivedMesh.cc:227
static void mesh_set_only_copy(Mesh *mesh, const CustomData_MeshMasks *mask)
Definition: DerivedMesh.cc:418
static void add_orco_mesh(Object *ob, BMEditMesh *em, Mesh *mesh, Mesh *mesh_orco, int layer)
Definition: DerivedMesh.cc:558
static void editbmesh_calc_modifier_final_normals(Mesh *mesh_final, const CustomData_MeshMasks *final_datamask)
static const MLoopTri * dm_getLoopTriArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:203
Mesh * mesh_create_eval_no_deform_render(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
#define ASSERT_IS_VALID_MESH(mesh)
Definition: DerivedMesh.cc:79
static Mesh * modifier_modify_mesh_and_geometry_set(ModifierData *md, const ModifierEvalContext &mectx, Mesh *input_mesh, GeometrySet &geometry_set)
Definition: DerivedMesh.cc:686
void * DM_get_vert_data_layer(DerivedMesh *dm, int type)
Definition: DerivedMesh.cc:432
static MLoop * dm_getLoopArray(DerivedMesh *dm)
Definition: DerivedMesh.cc:120
static Mesh * create_orco_mesh(Object *ob, Mesh *me, BMEditMesh *em, int layer)
Definition: DerivedMesh.cc:532
Mesh * mesh_get_eval_final(struct Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static void editbmesh_build_data(struct Depsgraph *depsgraph, const Scene *scene, Object *obedit, BMEditMesh *em, CustomData_MeshMasks *dataMask)
Mesh * mesh_create_eval_final(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
void BKE_mesh_wrapper_deferred_finalize_mdata(Mesh *me_eval, const CustomData_MeshMasks *cd_mask_finalize)
Definition: DerivedMesh.cc:668
void DM_set_only_copy(DerivedMesh *dm, const CustomData_MeshMasks *mask)
Definition: DerivedMesh.cc:404
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_VERTS_OF_MESH
ATTR_WARN_UNUSED_RESULT const BMLoop * l
const Mesh * get_for_read() const
void replace(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void ensure_owns_direct_data() override
void resize(const int64_t new_size)
Definition: BLI_vector.hh:353
GAttributeReader lookup(const AttributeIDRef &attribute_id) const
GSpanAttributeWriter lookup_or_add_for_write_only_span(const AttributeIDRef &attribute_id, const eAttrDomain domain, const eCustomDataType data_type)
Scene scene
const Depsgraph * depsgraph
SyclQueue void * dest
int count
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:34
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
MutableAttributeAccessor mesh_attributes_for_write(Mesh &mesh)
void isolate_task(const Function &function)
Definition: BLI_task.hh:125
vec_base< float, 3 > float3
#define min(a, b)
Definition: sort.c:35
unsigned int uint32_t
Definition: stdint.h:80
struct BMesh * bm
Definition: BKE_editmesh.h:40
struct BMVert * v
Definition: bmesh_class.h:153
float co[3]
Definition: bmesh_class.h:87
CustomData edata
Definition: bmesh_class.h:337
CustomData pdata
Definition: bmesh_class.h:337
struct Object * object
struct ClothSimSettings * sim_parms
struct MLoop *(* getLoopArray)(DerivedMesh *dm)
CustomData faceData
void *(* getVertDataArray)(DerivedMesh *dm, int type)
CustomData vertData
struct MPoly *(* dupPolyArray)(DerivedMesh *dm)
struct MVert *(* getVertArray)(DerivedMesh *dm)
void(* copyLoopArray)(DerivedMesh *dm, struct MLoop *r_loop)
void(* copyEdgeArray)(DerivedMesh *dm, struct MEdge *r_edge)
int(* getNumVerts)(DerivedMesh *dm)
const struct MLoopTri *(* getLoopTriArray)(DerivedMesh *dm)
int(* getNumLoopTri)(DerivedMesh *dm)
void(* copyPolyArray)(DerivedMesh *dm, struct MPoly *r_poly)
int(* getNumPolys)(DerivedMesh *dm)
struct MEdge *(* dupEdgeArray)(DerivedMesh *dm)
int(* getNumEdges)(DerivedMesh *dm)
struct DerivedMesh::@16 looptris
void *(* getLoopDataArray)(DerivedMesh *dm, int type)
CustomData polyData
struct MLoop *(* dupLoopArray)(DerivedMesh *dm)
struct MVert *(* dupVertArray)(DerivedMesh *dm)
struct MLoopTri * array_wip
void(* copyVertArray)(DerivedMesh *dm, struct MVert *r_vert)
void *(* getPolyDataArray)(DerivedMesh *dm, int type)
void *(* getEdgeDataArray)(DerivedMesh *dm, int type)
struct MEdge *(* getEdgeArray)(DerivedMesh *dm)
CustomData edgeData
struct MPoly *(* getPolyArray)(DerivedMesh *dm)
CustomData loopData
void(* recalcLoopTri)(DerivedMesh *dm)
struct MLoopTri * array
int(* getNumLoops)(DerivedMesh *dm)
DerivedMeshType type
const float(* vertexCos)[3]
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) const
Definition: DNA_ID.h:368
int tag
Definition: DNA_ID.h:387
char name[66]
Definition: DNA_ID.h:378
void * data
Definition: DNA_key_types.h:50
ID id
Definition: DNA_key_types.h:63
BLI_bitmap * vertex_visit
float(* vertexcos)[3]
struct SubsurfRuntimeData * subsurf_runtime_data
struct Mesh * mesh_eval
struct EditMeshData * edit_data
char wrapper_type_finalize
void * eval_mutex
struct BMEditMesh * edit_mesh
CustomData vdata
struct MVert * mvert
uint16_t flag
int totedge
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
CustomData pdata
CustomData fdata
int totpoly
CustomData edata
int totloop
struct Key * key
struct MPoly * mpoly
CustomData ldata
struct ModifierData * next
void(* modifyGeometrySet)(struct ModifierData *md, const struct ModifierEvalContext *ctx, struct GeometrySet *geometry_set)
Definition: BKE_modifier.h:240
ModifierTypeFlag flags
Definition: BKE_modifier.h:161
void(* deformVertsEM)(struct ModifierData *md, const struct ModifierEvalContext *ctx, struct BMEditMesh *editData, struct Mesh *mesh, float(*vertexCos)[3], int numVerts)
Definition: BKE_modifier.h:202
ModifierTypeType type
Definition: BKE_modifier.h:160
void(* requiredDataMask)(struct Object *ob, struct ModifierData *md, struct CustomData_MeshMasks *r_cddata_masks)
Definition: BKE_modifier.h:268
bool(* dependsOnNormals)(struct ModifierData *md)
Definition: BKE_modifier.h:316
CustomData_MeshMasks last_data_mask
struct Mesh * editmesh_eval_cage
struct Mesh * mesh_deform_eval
struct GeometrySet * geometry_set_eval
Object_Runtime runtime
uint8_t modifier_flag
struct SculptSession * sculpt
void * data
struct ToolSettings * toolsettings
struct BMesh * bm
Definition: BKE_paint.h:539
struct Base * basact
float max