Blender V4.3
mesh_data_update.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <climits>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_cloth_types.h"
16#include "DNA_key_types.h"
17#include "DNA_material_types.h"
18#include "DNA_mesh_types.h"
19#include "DNA_meshdata_types.h"
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
23#include "BLI_bitmap.h"
24#include "BLI_blenlib.h"
25#include "BLI_linklist.h"
26#include "BLI_math_geom.h"
27#include "BLI_math_matrix.h"
29#include "BLI_span.hh"
30#include "BLI_task.h"
31#include "BLI_task.hh"
32#include "BLI_utildefines.h"
33#include "BLI_vector.hh"
34
35#include "BKE_bvhutils.hh"
36#include "BKE_deform.hh"
37#include "BKE_editmesh.hh"
38#include "BKE_editmesh_cache.hh"
39#include "BKE_geometry_set.hh"
41#include "BKE_key.hh"
42#include "BKE_layer.hh"
43#include "BKE_lib_id.hh"
44#include "BKE_material.h"
45#include "BKE_mesh.hh"
46#include "BKE_mesh_iterators.hh"
47#include "BKE_mesh_mapping.hh"
48#include "BKE_mesh_runtime.hh"
49#include "BKE_mesh_tangent.hh"
50#include "BKE_mesh_wrapper.hh"
51#include "BKE_modifier.hh"
52#include "BKE_multires.hh"
53#include "BKE_object.hh"
54#include "BKE_object_deform.h"
55#include "BKE_object_types.hh"
56#include "BKE_paint.hh"
58
59#include "BLI_sys_types.h" /* for intptr_t support */
60
61#include "BKE_shrinkwrap.hh"
62#include "DEG_depsgraph.hh"
64
65#include "CLG_log.h"
66
67#ifdef WITH_OPENSUBDIV
68# include "DNA_userdef_types.h"
69#endif
70
71namespace blender::bke {
72
73/* very slow! enable for testing only! */
74// #define USE_MODIFIER_VALIDATE
75
76#ifdef USE_MODIFIER_VALIDATE
77# define ASSERT_IS_VALID_MESH(mesh) \
78 (BLI_assert((mesh == nullptr) || (BKE_mesh_is_valid(mesh) == true)))
79#else
80# define ASSERT_IS_VALID_MESH(mesh)
81#endif
82
83static void mesh_init_origspace(Mesh &mesh);
84
85void mesh_eval_to_meshkey(const Mesh *me_deformed, Mesh *mesh, KeyBlock *kb)
86{
87 /* Just a shallow wrapper around #BKE_keyblock_convert_from_mesh,
88 * that ensures both evaluated mesh and original one has same number of vertices. */
89
90 const int totvert = me_deformed->verts_num;
91
92 if (totvert == 0 || mesh->verts_num == 0 || mesh->verts_num != totvert) {
93 return;
94 }
95
96 BKE_keyblock_convert_from_mesh(me_deformed, mesh->key, kb);
97}
98
99static void mesh_set_only_copy(Mesh *mesh, const CustomData_MeshMasks *mask)
100{
101 CustomData_set_only_copy(&mesh->vert_data, mask->vmask);
102 CustomData_set_only_copy(&mesh->edge_data, mask->emask);
103 CustomData_set_only_copy(&mesh->fdata_legacy, mask->fmask);
104 /* this wasn't in 2.63 and is disabled for 2.64 because it gives problems with
105 * weight paint mode when there are modifiers applied, needs further investigation,
106 * see replies to r50969, Campbell */
107#if 0
108 CustomData_set_only_copy(&mesh->ldata, mask->lmask);
109 CustomData_set_only_copy(&mesh->pdata, mask->pmask);
110#endif
111}
112
113/* orco custom data layer */
115 const BMEditMesh *em,
116 eCustomDataType layer_type,
117 Array<float3> &storage)
118{
119 if (layer_type == CD_ORCO) {
120
121 if (em) {
122 storage = BM_mesh_vert_coords_alloc(em->bm);
123 return storage;
124 }
125 storage = BKE_mesh_orco_verts_get(&ob);
126 return storage;
127 }
128 if (layer_type == CD_CLOTH_ORCO) {
129 /* apply shape key for cloth, this should really be solved
130 * by a more flexible customdata system, but not simple */
131 if (!em) {
134 if (clmd && clmd->sim_parms->shapekey_rest) {
136 BKE_key_from_object(const_cast<Object *>(&ob)), clmd->sim_parms->shapekey_rest);
137
138 if (kb && kb->data) {
139 return {static_cast<const float3 *>(kb->data), kb->totelem};
140 }
141 }
142 }
143
144 return {};
145 }
146
147 return {};
148}
149
150static Mesh *create_orco_mesh(const Object &ob,
151 const Mesh &mesh,
152 const BMEditMesh *em,
153 eCustomDataType layer)
154{
155 Mesh *orco_mesh;
156 if (em) {
157 orco_mesh = BKE_mesh_from_bmesh_for_eval_nomain(em->bm, nullptr, &mesh);
159 }
160 else {
161 orco_mesh = BKE_mesh_copy_for_eval(mesh);
162 }
163
164 Array<float3> storage;
165 const Span<float3> orco = get_orco_coords(ob, em, layer, storage);
166
167 if (!orco.is_empty()) {
168 orco_mesh->vert_positions_for_write().copy_from(orco);
169 orco_mesh->tag_positions_changed();
170 }
171
172 return orco_mesh;
173}
174
176{
177 void *data = CustomData_get_layer_for_write(&mesh.vert_data, layer, mesh.verts_num);
178 if (!data) {
179 data = CustomData_add_layer(&mesh.vert_data, layer, CD_CONSTRUCT, mesh.verts_num);
180 }
181 return MutableSpan(reinterpret_cast<float3 *>(data), mesh.verts_num);
182}
183
184static void add_orco_mesh(Object &ob,
185 const BMEditMesh *em,
186 Mesh &mesh,
187 const Mesh *mesh_orco,
188 const eCustomDataType layer)
189{
190 const int totvert = mesh.verts_num;
191
192 MutableSpan<float3> layer_orco;
193 if (mesh_orco) {
194 layer_orco = orco_coord_layer_ensure(mesh, layer);
195
196 if (mesh_orco->verts_num == totvert) {
197 layer_orco.copy_from(mesh_orco->vert_positions());
198 }
199 else {
200 layer_orco.copy_from(mesh.vert_positions());
201 }
202 }
203 else {
204 /* TODO(@sybren): `totvert` should potentially change here, as `ob->data`
205 * or `em` may have a different number of vertices than the evaluated `mesh`. */
206 Array<float3> storage;
207 const Span<float3> orco = get_orco_coords(ob, em, layer, storage);
208 if (!orco.is_empty()) {
209 layer_orco = orco_coord_layer_ensure(mesh, layer);
210 layer_orco.copy_from(orco);
211 }
212 }
213
214 if (!layer_orco.is_empty()) {
215 if (layer == CD_ORCO) {
216 BKE_mesh_orco_verts_transform((Mesh *)ob.data, layer_orco, false);
217 }
218 }
219}
220
227static void mesh_calc_finalize(const Mesh &mesh_input, Mesh &mesh_eval)
228{
229 /* Make sure the name is the same. This is because mesh allocation from template does not
230 * take care of naming. */
231 STRNCPY(mesh_eval.id.name, mesh_input.id.name);
232 /* Make evaluated mesh to share same edit mesh pointer as original and copied meshes. */
233 mesh_eval.runtime->edit_mesh = mesh_input.runtime->edit_mesh;
234}
235
245 const ModifierEvalContext &mectx,
246 Mesh *input_mesh,
247 GeometrySet &geometry_set)
248{
249 Mesh *mesh_output = nullptr;
251 if (mti->modify_geometry_set == nullptr) {
252 mesh_output = BKE_modifier_modify_mesh(md, &mectx, input_mesh);
253 }
254 else {
255 /* For performance reasons, this should be called by the modifier and/or nodes themselves at
256 * some point. */
258
259 /* Replace only the mesh rather than the whole component, because the entire #MeshComponent
260 * might have been replaced by data from a different object in the node tree, which means the
261 * component contains vertex group name data for that object that should not be removed. */
262 geometry_set.replace_mesh(input_mesh, GeometryOwnershipType::Editable);
263
264 /* Let the modifier change the geometry set. */
265 mti->modify_geometry_set(md, &mectx, &geometry_set);
266
267 /* Release the mesh from the geometry set again. */
268 if (geometry_set.has<MeshComponent>()) {
269 MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
270 if (mesh_component.get() != input_mesh) {
271 /* Make sure the mesh component actually owns the mesh before taking over ownership. */
272 mesh_component.ensure_owns_direct_data();
273 }
274 mesh_output = mesh_component.release();
275 }
276 /* Need to ensure that non-mesh data is also owned by the geometry set. Otherwise it might be
277 * freed while there is still a reference to it in the geometry. */
278 geometry_set.ensure_owns_direct_data();
279
280 /* Return an empty mesh instead of null. */
281 if (mesh_output == nullptr) {
282 mesh_output = BKE_mesh_new_nomain(0, 0, 0, 0);
283 BKE_mesh_copy_parameters_for_eval(mesh_output, input_mesh);
284 }
285 }
286
287 return mesh_output;
288}
289
290static void set_rest_position(Mesh &mesh)
291{
292 MutableAttributeAccessor attributes = mesh.attributes_for_write();
293 const AttributeReader positions = attributes.lookup<float3>("position");
294 attributes.remove("rest_position");
295 if (positions) {
296 if (positions.sharing_info && positions.varray.is_span()) {
297 attributes.add<float3>("rest_position",
299 AttributeInitShared(positions.varray.get_internal_span().data(),
300 *positions.sharing_info));
301 }
302 else {
303 attributes.add<float3>(
304 "rest_position", AttrDomain::Point, AttributeInitVArray(positions.varray));
305 }
306 }
307}
308
309static void mesh_calc_modifiers(Depsgraph &depsgraph,
310 const Scene &scene,
311 Object &ob,
312 const bool use_deform,
313 const bool need_mapping,
314 const CustomData_MeshMasks &dataMask,
315 const bool use_cache,
316 const bool allow_shared_mesh,
317 /* return args */
318 Mesh **r_deform,
319 Mesh **r_final,
320 GeometrySet **r_geometry_set)
321{
322 /* Input mesh shouldn't be modified. */
323 Mesh &mesh_input = *static_cast<Mesh *>(ob.data);
324 /* The final mesh is the result of calculating all enabled modifiers. */
325 Mesh *mesh = nullptr;
326 /* The result of calculating all leading deform modifiers. */
327 Mesh *mesh_deform = nullptr;
328 /* This geometry set contains the non-mesh data that might be generated by modifiers. */
329 GeometrySet geometry_set_final;
330
332
333 /* Mesh with constructive modifiers but no deformation applied. Tracked
334 * along with final mesh if undeformed / orco coordinates are requested
335 * for texturing. */
336 Mesh *mesh_orco = nullptr;
337 Mesh *mesh_orco_cloth = nullptr;
338
339 /* Modifier evaluation modes. */
340 const bool use_render = (DEG_get_mode(&depsgraph) == DAG_EVAL_RENDER);
341 const int required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
342
343 /* Sculpt can skip certain modifiers. */
344 const bool has_multires = BKE_sculpt_multires_active(&scene, &ob) != nullptr;
345 bool multires_applied = false;
346 const bool sculpt_mode = ob.mode & OB_MODE_SCULPT && ob.sculpt && !use_render;
347 const bool sculpt_dyntopo = (sculpt_mode && ob.sculpt->bm) && !use_render;
348
349 /* Modifier evaluation contexts for different types of modifiers. */
350 ModifierApplyFlag apply_render = use_render ? MOD_APPLY_RENDER : ModifierApplyFlag(0);
351 ModifierApplyFlag apply_cache = use_cache ? MOD_APPLY_USECACHE : ModifierApplyFlag(0);
352 const ModifierEvalContext mectx = {&depsgraph, &ob, apply_render | apply_cache};
353 const ModifierEvalContext mectx_orco = {&depsgraph, &ob, apply_render | MOD_APPLY_ORCO};
354
355 /* Get effective list of modifiers to execute. Some effects like shape keys
356 * are added as virtual modifiers before the user created modifiers. */
357 VirtualModifierData virtual_modifier_data;
358 ModifierData *firstmd = BKE_modifiers_get_virtual_modifierlist(&ob, &virtual_modifier_data);
359 ModifierData *md = firstmd;
360
361 /* Compute accumulated datamasks needed by each modifier. It helps to do
362 * this fine grained so that for example vertex groups are preserved up to
363 * an armature modifier, but not through a following subsurf modifier where
364 * subdividing them is expensive. */
365 CustomData_MeshMasks final_datamask = dataMask;
366 CDMaskLink *datamasks = BKE_modifier_calc_data_masks(&scene, md, &final_datamask, required_mode);
367 CDMaskLink *md_datamask = datamasks;
368 /* XXX Always copying POLYINDEX, else tessellated data are no more valid! */
370
371 /* Clear errors before evaluation. */
373
375 if (mesh == nullptr) {
376 mesh = BKE_mesh_copy_for_eval(mesh_input);
378 }
379 set_rest_position(*mesh);
380 }
381
382 /* Apply all leading deform modifiers. */
383 if (use_deform) {
384 for (; md; md = md->next, md_datamask = md_datamask->next) {
386
387 if (!BKE_modifier_is_enabled(&scene, md, required_mode)) {
388 continue;
389 }
390
391 if (mti->type == ModifierTypeType::OnlyDeform && !sculpt_dyntopo) {
392 ScopedModifierTimer modifier_timer{*md};
393 if (!mesh) {
394 mesh = BKE_mesh_copy_for_eval(mesh_input);
396 }
397
398 if (mti->required_data_mask) {
400 mti->required_data_mask(md, &mask);
401 if (mask.vmask & CD_MASK_ORCO) {
402 add_orco_mesh(ob, nullptr, *mesh, nullptr, CD_ORCO);
403 }
404 }
405
406 BKE_modifier_deform_verts(md, &mectx, mesh, mesh->vert_positions_for_write());
407 }
408 else {
409 break;
410 }
411 }
412
413 /* Result of all leading deforming modifiers is cached for
414 * places that wish to use the original mesh but with deformed
415 * coordinates (like vertex paint). */
416 if (r_deform) {
417 mesh_deform = BKE_mesh_copy_for_eval(mesh ? *mesh : mesh_input);
418 }
419 }
420
421 /* Apply all remaining constructive and deforming modifiers. */
422 bool have_non_onlydeform_modifiers_applied = false;
423 for (; md; md = md->next, md_datamask = md_datamask->next) {
425
426 if (!BKE_modifier_is_enabled(&scene, md, required_mode)) {
427 continue;
428 }
429
430 if (mti->type == ModifierTypeType::OnlyDeform && !use_deform) {
431 continue;
432 }
433
435 have_non_onlydeform_modifiers_applied)
436 {
437 BKE_modifier_set_error(&ob, md, "Modifier requires original data, bad stack position");
438 continue;
439 }
440
441 if (sculpt_mode && (!has_multires || multires_applied || sculpt_dyntopo)) {
442 bool unsupported = false;
443
444 if (md->type == eModifierType_Multires && ((MultiresModifierData *)md)->sculptlvl == 0) {
445 /* If multires is on level 0 skip it silently without warning message. */
446 if (!sculpt_dyntopo) {
447 continue;
448 }
449 }
450
451 if (sculpt_dyntopo) {
452 unsupported = true;
453 }
454
455 if (scene.toolsettings->sculpt->flags & SCULPT_ONLY_DEFORM) {
456 unsupported |= (mti->type != ModifierTypeType::OnlyDeform);
457 }
458
459 unsupported |= multires_applied;
460
461 if (unsupported) {
462 if (sculpt_dyntopo) {
463 BKE_modifier_set_error(&ob, md, "Not supported in dyntopo");
464 }
465 else {
466 BKE_modifier_set_error(&ob, md, "Not supported in sculpt mode");
467 }
468 continue;
469 }
470 }
471
472 if (need_mapping && !BKE_modifier_supports_mapping(md)) {
473 continue;
474 }
475
476 ScopedModifierTimer modifier_timer{*md};
477
478 /* Add orco mesh as layer if needed by this modifier. */
479 if (mesh && mesh_orco && mti->required_data_mask) {
480 CustomData_MeshMasks mask = {0};
481 mti->required_data_mask(md, &mask);
482 if (mask.vmask & CD_MASK_ORCO) {
483 add_orco_mesh(ob, nullptr, *mesh, mesh_orco, CD_ORCO);
484 }
485 }
486
488 if (!mesh) {
489 mesh = BKE_mesh_copy_for_eval(mesh_input);
491 }
492 BKE_modifier_deform_verts(md, &mectx, mesh, mesh->vert_positions_for_write());
493 }
494 else {
495 bool check_for_needs_mapping = false;
496 if (mesh != nullptr) {
497 if (have_non_onlydeform_modifiers_applied == false) {
498 /* If we only deformed, we won't have initialized #CD_ORIGINDEX.
499 * as this is the only part of the function that initializes mapping. */
500 check_for_needs_mapping = true;
501 }
502 }
503 else {
504 mesh = BKE_mesh_copy_for_eval(mesh_input);
506 check_for_needs_mapping = true;
507 }
508
509 have_non_onlydeform_modifiers_applied = true;
510
511 /* determine which data layers are needed by following modifiers */
512 CustomData_MeshMasks nextmask = md_datamask->next ? md_datamask->next->mask : final_datamask;
513
514 if (check_for_needs_mapping) {
515 /* Initialize original indices the first time we evaluate a
516 * constructive modifier. Modifiers will then do mapping mostly
517 * automatic by copying them through CustomData_copy_data along
518 * with other data.
519 *
520 * These are created when either requested by evaluation, or if
521 * following modifiers requested them. */
522 if (need_mapping ||
523 ((nextmask.vmask | nextmask.emask | nextmask.pmask) & CD_MASK_ORIGINDEX))
524 {
525 /* calc */
526 CustomData_add_layer(&mesh->vert_data, CD_ORIGINDEX, CD_CONSTRUCT, mesh->verts_num);
527 CustomData_add_layer(&mesh->edge_data, CD_ORIGINDEX, CD_CONSTRUCT, mesh->edges_num);
528 CustomData_add_layer(&mesh->face_data, CD_ORIGINDEX, CD_CONSTRUCT, mesh->faces_num);
529
530 /* Not worth parallelizing this,
531 * gives less than 0.1% overall speedup in best of best cases... */
533 &mesh->vert_data, CD_ORIGINDEX, mesh->verts_num),
534 mesh->verts_num,
535 0);
537 &mesh->edge_data, CD_ORIGINDEX, mesh->edges_num),
538 mesh->edges_num,
539 0);
541 &mesh->face_data, CD_ORIGINDEX, mesh->faces_num),
542 mesh->faces_num,
543 0);
544 }
545 }
546
547 /* set the Mesh to only copy needed data */
548 CustomData_MeshMasks mask = md_datamask->mask;
549 /* needMapping check here fixes bug #28112, otherwise it's
550 * possible that it won't be copied */
551 CustomData_MeshMasks_update(&mask, &append_mask);
552 if (need_mapping) {
553 mask.vmask |= CD_MASK_ORIGINDEX;
554 mask.emask |= CD_MASK_ORIGINDEX;
555 mask.pmask |= CD_MASK_ORIGINDEX;
556 }
557 mesh_set_only_copy(mesh, &mask);
558
559 /* add cloth rest shape key if needed */
560 if (mask.vmask & CD_MASK_CLOTH_ORCO) {
561 add_orco_mesh(ob, nullptr, *mesh, mesh_orco, CD_CLOTH_ORCO);
562 }
563
564 /* add an origspace layer if needed */
565 if ((md_datamask->mask.lmask) & CD_MASK_ORIGSPACE_MLOOP) {
566 if (!CustomData_has_layer(&mesh->corner_data, CD_ORIGSPACE_MLOOP)) {
568 &mesh->corner_data, CD_ORIGSPACE_MLOOP, CD_SET_DEFAULT, mesh->corners_num);
569 mesh_init_origspace(*mesh);
570 }
571 }
572
573 Mesh *mesh_next = modifier_modify_mesh_and_geometry_set(md, mectx, mesh, geometry_set_final);
574 ASSERT_IS_VALID_MESH(mesh_next);
575
576 if (mesh_next) {
577 /* if the modifier returned a new mesh, release the old one */
578 if (mesh != mesh_next) {
579 BLI_assert(mesh != &mesh_input);
580 BKE_id_free(nullptr, mesh);
581 }
582 mesh = mesh_next;
583 }
584
585 /* create an orco mesh in parallel */
586 if (nextmask.vmask & CD_MASK_ORCO) {
587 if (!mesh_orco) {
588 mesh_orco = create_orco_mesh(ob, mesh_input, nullptr, CD_ORCO);
589 }
590
591 nextmask.vmask &= ~CD_MASK_ORCO;
592 CustomData_MeshMasks temp_cddata_masks = {0};
593 temp_cddata_masks.vmask = CD_MASK_ORIGINDEX;
594 temp_cddata_masks.emask = CD_MASK_ORIGINDEX;
595 temp_cddata_masks.fmask = CD_MASK_ORIGINDEX;
596 temp_cddata_masks.pmask = CD_MASK_ORIGINDEX;
597
598 if (mti->required_data_mask != nullptr) {
599 mti->required_data_mask(md, &temp_cddata_masks);
600 }
601 CustomData_MeshMasks_update(&temp_cddata_masks, &nextmask);
602 mesh_set_only_copy(mesh_orco, &temp_cddata_masks);
603
604 mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco);
605 ASSERT_IS_VALID_MESH(mesh_next);
606
607 if (mesh_next) {
608 /* if the modifier returned a new mesh, release the old one */
609 if (mesh_orco != mesh_next) {
610 BLI_assert(mesh_orco != &mesh_input);
611 BKE_id_free(nullptr, mesh_orco);
612 }
613
614 mesh_orco = mesh_next;
615 }
616 }
617
618 /* create cloth orco mesh in parallel */
619 if (nextmask.vmask & CD_MASK_CLOTH_ORCO) {
620 if (!mesh_orco_cloth) {
621 mesh_orco_cloth = create_orco_mesh(ob, mesh_input, nullptr, CD_CLOTH_ORCO);
622 }
623
624 nextmask.vmask &= ~CD_MASK_CLOTH_ORCO;
625 nextmask.vmask |= CD_MASK_ORIGINDEX;
626 nextmask.emask |= CD_MASK_ORIGINDEX;
627 nextmask.pmask |= CD_MASK_ORIGINDEX;
628 mesh_set_only_copy(mesh_orco_cloth, &nextmask);
629
630 mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco_cloth);
631 ASSERT_IS_VALID_MESH(mesh_next);
632
633 if (mesh_next) {
634 /* if the modifier returned a new mesh, release the old one */
635 if (mesh_orco_cloth != mesh_next) {
636 BLI_assert(mesh_orco != &mesh_input);
637 BKE_id_free(nullptr, mesh_orco_cloth);
638 }
639
640 mesh_orco_cloth = mesh_next;
641 }
642 }
643
644 mesh->runtime->deformed_only = false;
645 }
646
647 if (sculpt_mode && md->type == eModifierType_Multires) {
648 multires_applied = true;
649 }
650 }
651
652 BLI_linklist_free((LinkNode *)datamasks, nullptr);
653
654 for (md = firstmd; md; md = md->next) {
656 }
657
658 if (mesh == nullptr) {
659 if (allow_shared_mesh) {
660 mesh = &mesh_input;
661 }
662 else {
663 mesh = BKE_mesh_copy_for_eval(mesh_input);
664 }
665 }
666
667 /* Denotes whether the object which the modifier stack came from owns the mesh or whether the
668 * mesh is shared across multiple objects since there are no effective modifiers. */
669 const bool is_own_mesh = (mesh != &mesh_input);
670
671 /* Add orco coordinates to final and deformed mesh if requested. */
672 if (final_datamask.vmask & CD_MASK_ORCO) {
673 /* No need in ORCO layer if the mesh was not deformed or modified: undeformed mesh in this case
674 * matches input mesh. */
675 if (is_own_mesh) {
676 add_orco_mesh(ob, nullptr, *mesh, mesh_orco, CD_ORCO);
677 }
678
679 if (mesh_deform) {
680 add_orco_mesh(ob, nullptr, *mesh_deform, nullptr, CD_ORCO);
681 }
682 }
683
684 if (mesh_orco) {
685 BKE_id_free(nullptr, mesh_orco);
686 }
687 if (mesh_orco_cloth) {
688 BKE_id_free(nullptr, mesh_orco_cloth);
689 }
690
691 /* Remove temporary data layer only needed for modifier evaluation.
692 * Save some memory, and ensure GPU subdivision does not need to deal with this. */
693 CustomData_free_layers(&mesh->vert_data, CD_CLOTH_ORCO, mesh->verts_num);
694
695 /* Compute normals. */
696 if (is_own_mesh) {
697 mesh_calc_finalize(mesh_input, *mesh);
698 }
699 else {
700 MeshRuntime *runtime = mesh_input.runtime;
701 if (runtime->mesh_eval == nullptr) {
702 std::lock_guard lock{mesh_input.runtime->eval_mutex};
703 if (runtime->mesh_eval == nullptr) {
704 /* Not yet finalized by any instance, do it now
705 * Isolate since computing normals is multithreaded and we are holding a lock. */
707 mesh = BKE_mesh_copy_for_eval(mesh_input);
708 mesh_calc_finalize(mesh_input, *mesh);
709 runtime->mesh_eval = mesh;
710 });
711 }
712 else {
713 /* Already finalized by another instance, reuse. */
714 mesh = runtime->mesh_eval;
715 }
716 }
717 else {
718 /* Already finalized by another instance, reuse. */
719 mesh = runtime->mesh_eval;
720 }
721 }
722
723 /* Return final mesh */
724 *r_final = mesh;
725 if (r_deform) {
726 *r_deform = mesh_deform;
727 }
728 if (r_geometry_set) {
729 *r_geometry_set = new GeometrySet(std::move(geometry_set_final));
730 }
731}
732
734 const Object *ob,
735 ModifierData *md,
736 bool has_prev_mesh)
737{
739 const int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
740
741 if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
742 return false;
743 }
744
745 if ((mti->flags & eModifierTypeFlag_RequiresOriginalData) && has_prev_mesh) {
746 BKE_modifier_set_error(ob, md, "Modifier requires original data, bad stack position");
747 return false;
748 }
749
750 return true;
751}
752
754{
755 switch (mesh->runtime->wrapper_type) {
757 if (mesh->runtime->edit_data->vert_positions.is_empty()) {
758 mesh->runtime->edit_data->vert_positions = BM_mesh_vert_coords_alloc(
759 mesh->runtime->edit_mesh->bm);
760 }
761 return mesh->runtime->edit_data->vert_positions;
764 return mesh->vert_positions_for_write();
765 }
767 return {};
768}
769
770static void editbmesh_calc_modifiers(Depsgraph &depsgraph,
771 const Scene &scene,
772 Object &ob,
773 const CustomData_MeshMasks &dataMask,
774 /* return args */
775 Mesh **r_cage,
776 Mesh **r_final,
777 GeometrySet **r_geometry_set)
778{
779 Mesh &mesh_input = *static_cast<Mesh *>(ob.data);
780 BMEditMesh &em_input = *mesh_input.runtime->edit_mesh;
781
782 Mesh *mesh_cage = nullptr;
783 /* This geometry set contains the non-mesh data that might be generated by modifiers. */
784 GeometrySet geometry_set_final;
785
786 /* Mesh with constructive modifiers but no deformation applied. Tracked
787 * along with final mesh if undeformed / orco coordinates are requested
788 * for texturing. */
789 Mesh *mesh_orco = nullptr;
790
791 /* Modifier evaluation modes. */
792 const int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
793
794 const bool use_render = (DEG_get_mode(&depsgraph) == DAG_EVAL_RENDER);
795 /* Modifier evaluation contexts for different types of modifiers. */
796 ModifierApplyFlag apply_render = use_render ? MOD_APPLY_RENDER : ModifierApplyFlag(0);
797 const ModifierEvalContext mectx = {&depsgraph, &ob, MOD_APPLY_USECACHE | apply_render};
798 const ModifierEvalContext mectx_orco = {&depsgraph, &ob, MOD_APPLY_ORCO};
799
800 /* Get effective list of modifiers to execute. Some effects like shape keys
801 * are added as virtual modifiers before the user created modifiers. */
802 VirtualModifierData virtual_modifier_data;
803 ModifierData *md = BKE_modifiers_get_virtual_modifierlist(&ob, &virtual_modifier_data);
804
805 /* Compute accumulated datamasks needed by each modifier. It helps to do
806 * this fine grained so that for example vertex groups are preserved up to
807 * an armature modifier, but not through a following subsurf modifier where
808 * subdividing them is expensive. */
809 CustomData_MeshMasks final_datamask = dataMask;
810 CDMaskLink *datamasks = BKE_modifier_calc_data_masks(&scene, md, &final_datamask, required_mode);
811 CDMaskLink *md_datamask = datamasks;
813
815 mesh_input.runtime->edit_mesh, &final_datamask, &mesh_input);
816
817 int cageIndex = BKE_modifiers_get_cage_index(&scene, &ob, nullptr, true);
818 if (r_cage && cageIndex == -1) {
819 mesh_cage = mesh;
820 }
821
822 /* The mesh from edit mode should not have any original index layers already, since those
823 * are added during evaluation when necessary and are redundant on an original mesh. */
824 BLI_assert(CustomData_get_layer(&em_input.bm->pdata, CD_ORIGINDEX) == nullptr &&
825 CustomData_get_layer(&em_input.bm->edata, CD_ORIGINDEX) == nullptr &&
826 CustomData_get_layer(&em_input.bm->pdata, CD_ORIGINDEX) == nullptr);
827
828 /* Clear errors before evaluation. */
830
833 set_rest_position(*mesh);
834 }
835
836 bool non_deform_modifier_applied = false;
837 for (int i = 0; md; i++, md = md->next, md_datamask = md_datamask->next) {
839 if (!editbmesh_modifier_is_enabled(&scene, &ob, md, non_deform_modifier_applied)) {
840 continue;
841 }
842
843 ScopedModifierTimer modifier_timer{*md};
844
845 /* Add an orco mesh as layer if needed by this modifier. */
846 if (mesh_orco && mti->required_data_mask) {
847 CustomData_MeshMasks mask = {0};
848 mti->required_data_mask(md, &mask);
849 if (mask.vmask & CD_MASK_ORCO) {
850 add_orco_mesh(ob, &em_input, *mesh, mesh_orco, CD_ORCO);
851 }
852 }
853
854 if (mesh == mesh_cage) {
855 /* If the cage mesh has already been assigned, we have passed the cage index in the modifier
856 * list. If the cage and final meshes are still the same, duplicate the final mesh so the
857 * cage mesh isn't modified anymore. */
858 mesh = BKE_mesh_copy_for_eval(*mesh);
859 if (mesh_cage->runtime->edit_mesh) {
860 mesh->runtime->edit_mesh = mesh_cage->runtime->edit_mesh;
861 mesh->runtime->is_original_bmesh = true;
862 mesh->runtime->deformed_only = mesh_cage->runtime->deformed_only;
863 if (mesh_cage->runtime->edit_data) {
864 mesh->runtime->edit_data = std::make_unique<EditMeshData>(
865 *mesh_cage->runtime->edit_data);
866 }
867 }
868 }
869
871 if (mti->deform_verts_EM) {
873 md, &mectx, &em_input, mesh, mesh_wrapper_vert_coords_ensure_for_write(mesh));
875 }
876 else {
878 BKE_modifier_deform_verts(md, &mectx, mesh, mesh->vert_positions_for_write());
879 mesh->tag_positions_changed();
880 }
881 }
882 else {
883 non_deform_modifier_applied = true;
884
885 /* create an orco derivedmesh in parallel */
886 CustomData_MeshMasks mask = md_datamask->mask;
887 if (mask.vmask & CD_MASK_ORCO) {
888 if (!mesh_orco) {
889 mesh_orco = create_orco_mesh(ob, mesh_input, &em_input, CD_ORCO);
890 }
891
892 mask.vmask &= ~CD_MASK_ORCO;
893 mask.vmask |= CD_MASK_ORIGINDEX;
894 mask.emask |= CD_MASK_ORIGINDEX;
895 mask.pmask |= CD_MASK_ORIGINDEX;
896 mesh_set_only_copy(mesh_orco, &mask);
897
898 Mesh *mesh_next = BKE_modifier_modify_mesh(md, &mectx_orco, mesh_orco);
899 ASSERT_IS_VALID_MESH(mesh_next);
900
901 if (mesh_next) {
902 /* if the modifier returned a new dm, release the old one */
903 if (mesh_orco && mesh_orco != mesh_next) {
904 BKE_id_free(nullptr, mesh_orco);
905 }
906 mesh_orco = mesh_next;
907 }
908 }
909
910 /* set the DerivedMesh to only copy needed data */
911 CustomData_MeshMasks_update(&mask, &append_mask);
912 /* XXX WHAT? overwrites mask ??? */
913 /* CD_MASK_ORCO may have been cleared above */
914 mask = md_datamask->mask;
915 mask.vmask |= CD_MASK_ORIGINDEX;
916 mask.emask |= CD_MASK_ORIGINDEX;
917 mask.pmask |= CD_MASK_ORIGINDEX;
918
919 mesh_set_only_copy(mesh, &mask);
920
921 if (mask.lmask & CD_MASK_ORIGSPACE_MLOOP) {
922 if (!CustomData_has_layer(&mesh->corner_data, CD_ORIGSPACE_MLOOP)) {
924 &mesh->corner_data, CD_ORIGSPACE_MLOOP, CD_SET_DEFAULT, mesh->corners_num);
925 mesh_init_origspace(*mesh);
926 }
927 }
928
929 Mesh *mesh_next = modifier_modify_mesh_and_geometry_set(md, mectx, mesh, geometry_set_final);
930 ASSERT_IS_VALID_MESH(mesh_next);
931
932 if (mesh_next) {
933 if (mesh != mesh_next) {
934 BKE_id_free(nullptr, mesh);
935 }
936 mesh = mesh_next;
937 }
938 mesh->runtime->deformed_only = false;
939 }
940
941 if (r_cage && i == cageIndex) {
942 mesh_cage = mesh;
943 }
944 }
945
946 BLI_linklist_free((LinkNode *)datamasks, nullptr);
947
948 /* Add orco coordinates to final and deformed mesh if requested. */
949 if (final_datamask.vmask & CD_MASK_ORCO) {
950 /* FIXME(@ideasman42): avoid the need to convert to mesh data just to add an orco layer. */
952
953 add_orco_mesh(ob, &em_input, *mesh, mesh_orco, CD_ORCO);
954 }
955
956 if (mesh_orco) {
957 BKE_id_free(nullptr, mesh_orco);
958 }
959
960 /* Return final mesh. */
961 *r_final = mesh;
962 if (r_cage) {
963 *r_cage = mesh_cage;
964 }
965 if (r_geometry_set) {
966 *r_geometry_set = new GeometrySet(std::move(geometry_set_final));
967 }
968}
969
970static void mesh_build_extra_data(const Depsgraph &depsgraph,
971 const Object &ob,
972 const Mesh &mesh_eval)
973{
975
976 if (eval_flags & DAG_EVAL_NEED_SHRINKWRAP_BOUNDARY) {
978 }
979}
980
981static void mesh_build_data(Depsgraph &depsgraph,
982 const Scene &scene,
983 Object &ob,
984 const CustomData_MeshMasks &dataMask,
985 const bool need_mapping)
986{
987#if 0 /* XXX This is already taken care of in #mesh_calc_modifiers... */
988 if (need_mapping) {
989 /* Also add the flag so that it is recorded in lastDataMask. */
990 dataMask->vmask |= CD_MASK_ORIGINDEX;
991 dataMask->emask |= CD_MASK_ORIGINDEX;
992 dataMask->pmask |= CD_MASK_ORIGINDEX;
993 }
994#endif
995
996 Mesh *mesh_eval = nullptr, *mesh_deform_eval = nullptr;
997 GeometrySet *geometry_set_eval = nullptr;
999 scene,
1000 ob,
1001 true,
1002 need_mapping,
1003 dataMask,
1004 true,
1005 true,
1006 &mesh_deform_eval,
1007 &mesh_eval,
1008 &geometry_set_eval);
1009
1010 /* The modifier stack evaluation is storing result in mesh->runtime.mesh_eval, but this result
1011 * is not guaranteed to be owned by object.
1012 *
1013 * Check ownership now, since later on we can not go to a mesh owned by someone else via
1014 * object's runtime: this could cause access freed data on depsgraph destruction (mesh who owns
1015 * the final result might be freed prior to object). */
1016 Mesh *mesh = (Mesh *)ob.data;
1017 const bool is_mesh_eval_owned = (mesh_eval != mesh->runtime->mesh_eval);
1018 BKE_object_eval_assign_data(&ob, &mesh_eval->id, is_mesh_eval_owned);
1019
1020 /* Add the final mesh as a non-owning component to the geometry set. */
1021 MeshComponent &mesh_component = geometry_set_eval->get_component_for_write<MeshComponent>();
1022 mesh_component.replace(mesh_eval, GeometryOwnershipType::Editable);
1023 ob.runtime->geometry_set_eval = geometry_set_eval;
1024
1025 ob.runtime->mesh_deform_eval = mesh_deform_eval;
1026 ob.runtime->last_data_mask = dataMask;
1027 ob.runtime->last_need_mapping = need_mapping;
1028
1029 /* Make sure that drivers can target shapekey properties.
1030 * Note that this causes a potential inconsistency, as the shapekey may have a
1031 * different topology than the evaluated mesh. */
1032 BLI_assert(mesh->key == nullptr || DEG_is_evaluated_id(&mesh->key->id));
1033 mesh_eval->key = mesh->key;
1034
1035 if ((ob.mode & OB_MODE_ALL_SCULPT) && ob.sculpt) {
1036 if (DEG_is_active(&depsgraph)) {
1038 }
1039 }
1040
1041 mesh_build_extra_data(depsgraph, ob, *mesh_eval);
1042}
1043
1044static void editbmesh_build_data(Depsgraph &depsgraph,
1045 const Scene &scene,
1046 Object &obedit,
1047 CustomData_MeshMasks &dataMask)
1048{
1049 Mesh *mesh = static_cast<Mesh *>(obedit.data);
1050 Mesh *me_cage;
1051 Mesh *me_final;
1052 GeometrySet *non_mesh_components;
1053
1055 depsgraph, scene, obedit, dataMask, &me_cage, &me_final, &non_mesh_components);
1056
1057 /* The modifier stack result is expected to share edit mesh pointer with the input.
1058 * This is similar `mesh_calc_finalize()`. */
1059 BKE_mesh_free_editmesh(me_final);
1060 BKE_mesh_free_editmesh(me_cage);
1061 me_final->runtime->edit_mesh = me_cage->runtime->edit_mesh = mesh->runtime->edit_mesh;
1062
1063 /* Object has edit_mesh but is not in edit mode (object shares mesh datablock with another object
1064 * with is in edit mode).
1065 * Convert edit mesh to mesh until the draw manager can draw mesh wrapper which is not in the
1066 * edit mode. */
1067 if (!(obedit.mode & OB_MODE_EDIT)) {
1069 if (me_final != me_cage) {
1071 }
1072 }
1073
1074 const bool is_mesh_eval_owned = (me_final != mesh->runtime->mesh_eval);
1075 BKE_object_eval_assign_data(&obedit, &me_final->id, is_mesh_eval_owned);
1076
1077 /* Make sure that drivers can target shapekey properties.
1078 * Note that this causes a potential inconsistency, as the shapekey may have a
1079 * different topology than the evaluated mesh. */
1080 BLI_assert(mesh->key == nullptr || DEG_is_evaluated_id(&mesh->key->id));
1081 me_final->key = mesh->key;
1082
1083 obedit.runtime->editmesh_eval_cage = me_cage;
1084
1085 obedit.runtime->geometry_set_eval = non_mesh_components;
1086
1087 obedit.runtime->last_data_mask = dataMask;
1088}
1089
1090static void object_get_datamask(const Depsgraph &depsgraph,
1091 Object &ob,
1092 CustomData_MeshMasks &r_mask,
1093 bool *r_need_mapping)
1094{
1097
1099
1100 if (r_need_mapping) {
1101 *r_need_mapping = false;
1102 }
1103
1104 /* Must never access original objects when dependency graph is not active: it might be already
1105 * freed. */
1106 if (!DEG_is_active(&depsgraph)) {
1107 return;
1108 }
1109
1110 BKE_view_layer_synced_ensure(scene, view_layer);
1111 Object *actob = BKE_view_layer_active_object_get(view_layer);
1112 if (actob) {
1113 actob = DEG_get_original_object(actob);
1114 }
1115 if (DEG_get_original_object(&ob) == actob) {
1116 bool editing = BKE_paint_select_face_test(actob);
1117
1118 /* weight paint and face select need original indices because of selection buffer drawing */
1119 if (r_need_mapping) {
1120 *r_need_mapping = (editing || (ob.mode & (OB_MODE_WEIGHT_PAINT | OB_MODE_VERTEX_PAINT)));
1121 }
1122
1123 /* Check if we need #MTFace & loop-color due to face select or texture paint. */
1124 if ((ob.mode & OB_MODE_TEXTURE_PAINT) || editing) {
1126 r_mask.fmask |= CD_MASK_MTFACE;
1127 }
1128
1129 /* Check if we need loop-color due to vertex paint or weight-paint. */
1130 if (ob.mode & OB_MODE_VERTEX_PAINT) {
1132 }
1133
1134 if (ob.mode & OB_MODE_WEIGHT_PAINT) {
1135 r_mask.vmask |= CD_MASK_MDEFORMVERT;
1136 }
1137 }
1138
1139 /* Multiple objects can be in edit-mode at once. */
1140 if (actob && (actob->mode & OB_MODE_EDIT)) {
1141 if (ob.mode & OB_MODE_EDIT) {
1142 r_mask.vmask |= CD_MASK_MVERT_SKIN;
1143 }
1144 }
1145}
1146
1148 const Scene &scene,
1149 Object &ob,
1150 const CustomData_MeshMasks &dataMask)
1151{
1152 BLI_assert(ob.type == OB_MESH);
1153
1154 /* Evaluated meshes aren't supposed to be created on original instances. If you do,
1155 * they aren't cleaned up properly on mode switch, causing crashes, e.g #58150. */
1157
1159 if (DEG_is_active(&depsgraph)) {
1161 }
1162
1163 /* NOTE: Access the `edit_mesh` after freeing the derived caches, so that `ob.data` is restored
1164 * to the pre-evaluated state. This is because the evaluated state is not necessarily sharing the
1165 * `edit_mesh` pointer with the input. For example, if the object is first evaluated in the
1166 * object mode, and then user in another scene moves object to edit mode. */
1167 Mesh *mesh = static_cast<Mesh *>(ob.data);
1168
1169 bool need_mapping;
1170 CustomData_MeshMasks cddata_masks = dataMask;
1171 object_get_datamask(depsgraph, ob, cddata_masks, &need_mapping);
1172
1173 if (mesh->runtime->edit_mesh) {
1174 editbmesh_build_data(depsgraph, scene, ob, cddata_masks);
1175 }
1176 else {
1177 mesh_build_data(depsgraph, scene, ob, cddata_masks, need_mapping);
1178 }
1179}
1180
1182 const Scene *scene,
1183 Object *ob,
1184 const CustomData_MeshMasks *dataMask)
1185{
1186 BMEditMesh *em = ((Mesh *)ob->data)->runtime->edit_mesh.get();
1187 if (em != nullptr) {
1188 /* There is no such a concept as deformed mesh in edit mode.
1189 * Explicitly disallow this request so that the evaluated result is not modified with evaluated
1190 * result from the wrong mode. */
1191 BLI_assert_msg(0, "Request of derformed mesh of object which is in edit mode");
1192 return nullptr;
1193 }
1194
1195 /* This function isn't thread-safe and can't be used during evaluation. */
1197
1198 /* Evaluated meshes aren't supposed to be created on original instances. If you do,
1199 * they aren't cleaned up properly on mode switch, causing crashes, e.g #58150. */
1201
1202 /* If there's no evaluated mesh or the last data mask used doesn't include
1203 * the data we need, rebuild the evaluated mesh. */
1204 bool need_mapping;
1205
1206 CustomData_MeshMasks cddata_masks = *dataMask;
1207 object_get_datamask(*depsgraph, *ob, cddata_masks, &need_mapping);
1208
1209 if (!ob->runtime->mesh_deform_eval ||
1210 !CustomData_MeshMasks_are_matching(&(ob->runtime->last_data_mask), &cddata_masks) ||
1211 (need_mapping && !ob->runtime->last_need_mapping))
1212 {
1213 /* FIXME: this block may leak memory (& assert) because it runs #BKE_object_eval_assign_data
1214 * intended only to run during depsgraph-evaluation that overwrites the evaluated mesh
1215 * without freeing beforehand, see: !128228. */
1216 CustomData_MeshMasks_update(&cddata_masks, &ob->runtime->last_data_mask);
1218 *depsgraph, *scene, *ob, cddata_masks, need_mapping || ob->runtime->last_need_mapping);
1219 }
1220
1221 return ob->runtime->mesh_deform_eval;
1222}
1223
1225 const Scene *scene,
1226 Object *ob,
1227 const CustomData_MeshMasks *dataMask)
1228{
1229 Mesh *result;
1231 *depsgraph, *scene, *ob, true, false, *dataMask, false, false, nullptr, &result, nullptr);
1232 return result;
1233}
1234
1236 const Scene *scene,
1237 Object *ob,
1238 const CustomData_MeshMasks *dataMask)
1239{
1240 Mesh *result;
1242 *depsgraph, *scene, *ob, false, false, *dataMask, false, false, nullptr, &result, nullptr);
1243 return result;
1244}
1245
1247 const Scene *scene,
1248 Object *ob,
1249 const CustomData_MeshMasks *dataMask)
1250{
1251 Mesh *result;
1253 *depsgraph, *scene, *ob, false, false, *dataMask, false, false, nullptr, &result, nullptr);
1254 return result;
1255}
1256
1258 const Scene *scene,
1259 Object *obedit,
1260 BMEditMesh * /*em*/,
1261 const CustomData_MeshMasks *dataMask)
1262{
1263 CustomData_MeshMasks cddata_masks = *dataMask;
1264
1265 /* If there's no evaluated mesh or the last data mask used doesn't include
1266 * the data we need, rebuild the evaluated mesh. */
1267 object_get_datamask(*depsgraph, *obedit, cddata_masks, nullptr);
1268
1269 if (!obedit->runtime->editmesh_eval_cage ||
1270 !CustomData_MeshMasks_are_matching(&(obedit->runtime->last_data_mask), &cddata_masks))
1271 {
1272 /* FIXME: this block may leak memory (& assert) because it runs #BKE_object_eval_assign_data
1273 * intended only to run during depsgraph-evaluation that overwrites the evaluated mesh
1274 * without freeing beforehand, see: !128228. */
1275 editbmesh_build_data(*depsgraph, *scene, *obedit, cddata_masks);
1276 }
1277
1278 return obedit->runtime->editmesh_eval_cage;
1279}
1280
1282 const Scene *scene,
1283 Object *obedit,
1284 const CustomData_MeshMasks *dataMask)
1285{
1286 BLI_assert((obedit->id.tag & ID_TAG_COPIED_ON_EVAL) == 0);
1287 const Scene *scene_eval = (const Scene *)DEG_get_evaluated_id(depsgraph, (ID *)&scene->id);
1288 Object *obedit_eval = (Object *)DEG_get_evaluated_id(depsgraph, &obedit->id);
1289 BMEditMesh *em_eval = BKE_editmesh_from_object(obedit_eval);
1290 return editbmesh_get_eval_cage(depsgraph, scene_eval, obedit_eval, em_eval, dataMask);
1291}
1292
1293/* same as above but for vert coords */
1298
1299static void make_vertexcos__mapFunc(void *user_data,
1300 int index,
1301 const float co[3],
1302 const float /*no*/[3])
1303{
1304 MappedUserData *mappedData = (MappedUserData *)user_data;
1305
1306 if (BLI_BITMAP_TEST(mappedData->vertex_visit, index) == 0) {
1307 /* we need coord from prototype vertex, not from copies,
1308 * assume they stored in the beginning of vertex array stored in DM
1309 * (mirror modifier for eg does this) */
1310 copy_v3_v3(mappedData->vertexcos[index], co);
1311 BLI_BITMAP_ENABLE(mappedData->vertex_visit, index);
1312 }
1313}
1314
1316{
1317 if (mesh_eval->runtime->deformed_only == false) {
1318 MappedUserData user_data;
1319 r_cos.fill(float3(0));
1320 user_data.vertexcos = reinterpret_cast<float(*)[3]>(r_cos.data());
1321 user_data.vertex_visit = BLI_BITMAP_NEW(r_cos.size(), "vertexcos flags");
1323 MEM_freeN(user_data.vertex_visit);
1324 }
1325 else {
1326 r_cos.copy_from(BKE_mesh_wrapper_vert_coords(mesh_eval));
1327 }
1328}
1329
1330static void mesh_init_origspace(Mesh &mesh)
1331{
1332 const float default_osf[4][2] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1333
1335 &mesh.corner_data, CD_ORIGSPACE_MLOOP, mesh.corners_num);
1336 const Span<float3> positions = mesh.vert_positions();
1337 const OffsetIndices faces = mesh.faces();
1338 const Span<int> corner_verts = mesh.corner_verts();
1339
1340 int j, k;
1341
1342 Vector<float2, 64> vcos_2d;
1343
1344 for (const int i : faces.index_range()) {
1345 const IndexRange face = faces[i];
1346 OrigSpaceLoop *lof = lof_array + face.start();
1347
1348 if (ELEM(face.size(), 3, 4)) {
1349 for (j = 0; j < face.size(); j++, lof++) {
1350 copy_v2_v2(lof->uv, default_osf[j]);
1351 }
1352 }
1353 else {
1354 float co[3];
1355 float mat[3][3];
1356
1357 float min[2] = {FLT_MAX, FLT_MAX}, max[2] = {-FLT_MAX, -FLT_MAX};
1358 float translate[2], scale[2];
1359
1360 const float3 p_nor = mesh::face_normal_calc(positions, corner_verts.slice(face));
1361
1362 axis_dominant_v3_to_m3(mat, p_nor);
1363
1364 vcos_2d.resize(face.size());
1365 for (j = 0; j < face.size(); j++) {
1366 mul_v3_m3v3(co, mat, positions[corner_verts[face[j]]]);
1367 copy_v2_v2(vcos_2d[j], co);
1368
1369 for (k = 0; k < 2; k++) {
1370 if (co[k] > max[k]) {
1371 max[k] = co[k];
1372 }
1373 else if (co[k] < min[k]) {
1374 min[k] = co[k];
1375 }
1376 }
1377 }
1378
1379 /* Brings min to (0, 0). */
1380 negate_v2_v2(translate, min);
1381
1382 /* Scale will bring max to (1, 1). */
1383 sub_v2_v2v2(scale, max, min);
1384 if (scale[0] == 0.0f) {
1385 scale[0] = 1e-9f;
1386 }
1387 if (scale[1] == 0.0f) {
1388 scale[1] = 1e-9f;
1389 }
1390 invert_v2(scale);
1391
1392 /* Finally, transform all vcos_2d into ((0, 0), (1, 1))
1393 * square and assign them as origspace. */
1394 for (j = 0; j < face.size(); j++, lof++) {
1395 add_v2_v2v2(lof->uv, vcos_2d[j], translate);
1396 mul_v2_v2(lof->uv, scale);
1397 }
1398 }
1399 }
1400
1402}
1403
1404} // namespace blender::bke
void CustomData_set_only_copy(const CustomData *data, eCustomDataMask mask)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
const CustomData_MeshMasks CD_MASK_BAREMESH_ORIGINDEX
@ CD_SET_DEFAULT
@ CD_CONSTRUCT
void CustomData_MeshMasks_update(CustomData_MeshMasks *mask_dst, const CustomData_MeshMasks *mask_src)
Definition customdata.cc:91
bool CustomData_MeshMasks_are_matching(const CustomData_MeshMasks *mask_ref, const CustomData_MeshMasks *mask_required)
const CustomData_MeshMasks CD_MASK_BAREMESH
void CustomData_free_layers(CustomData *data, eCustomDataType type, int totelem)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
support for deformation groups and hooks.
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:63
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
Definition key.cc:2186
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1923
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1820
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void BKE_id_free(Main *bmain, void *idv)
General operations, lookup, etc. for materials.
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
void BKE_mesh_tessface_clear(Mesh *mesh)
blender::Array< blender::float3 > BKE_mesh_orco_verts_get(const Object *ob)
void BKE_mesh_orco_verts_transform(Mesh *mesh, blender::MutableSpan< blender::float3 > orco, bool invert)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
void BKE_mesh_free_editmesh(Mesh *mesh)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
void BKE_mesh_ensure_default_orig_index_customdata(Mesh *mesh)
@ MESH_FOREACH_NOP
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
@ ME_WRAPPER_TYPE_MDATA
@ ME_WRAPPER_TYPE_SUBD
@ ME_WRAPPER_TYPE_BMESH
blender::Span< blender::float3 > BKE_mesh_wrapper_vert_coords(const Mesh *mesh)
Mesh * BKE_mesh_wrapper_from_editmesh(std::shared_ptr< BMEditMesh > em, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
void BKE_mesh_wrapper_tag_positions_changed(Mesh *mesh)
void BKE_modifiers_clear_errors(Object *ob)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
int BKE_modifiers_get_cage_index(const Scene *scene, Object *ob, int *r_lastPossibleCageIndex, bool is_virtual)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
CDMaskLink * BKE_modifier_calc_data_masks(const Scene *scene, ModifierData *md, CustomData_MeshMasks *final_datamask, int required_mode)
@ eModifierTypeFlag_RequiresOriginalData
bool BKE_modifier_supports_mapping(ModifierData *md)
void BKE_modifier_free_temporary_data(ModifierData *md)
Mesh * BKE_modifier_modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_modifier_deform_vertsEM(ModifierData *md, const ModifierEvalContext *ctx, const BMEditMesh *em, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
ModifierApplyFlag
@ MOD_APPLY_USECACHE
@ MOD_APPLY_RENDER
@ MOD_APPLY_ORCO
void BKE_modifier_deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
General operations, lookup, etc. for blender objects.
void BKE_object_eval_assign_data(Object *object, ID *data, bool is_owned)
void BKE_object_free_derived_caches(Object *ob)
Functions for dealing with objects and deform verts, used by painting and tools.
void BKE_sculpt_update_object_after_eval(Depsgraph *depsgraph, Object *ob_eval)
Definition paint.cc:2568
void BKE_sculpt_update_object_before_eval(Object *ob_eval)
Definition paint.cc:2509
MultiresModifierData * BKE_sculpt_multires_active(const Scene *scene, Object *ob)
Definition paint.cc:2316
bool BKE_paint_select_face_test(const Object *ob)
Definition paint.cc:1607
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.h:41
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.h:65
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.h:82
unsigned int BLI_bitmap
Definition BLI_bitmap.h:17
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
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)
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])
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#define ELEM(...)
bool DEG_is_evaluating(const Depsgraph *depsgraph)
Definition depsgraph.cc:312
@ DAG_EVAL_RENDER
@ DAG_EVAL_NEED_SHRINKWRAP_BOUNDARY
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:318
uint32_t DEG_get_eval_flags_for_id(const Depsgraph *graph, const ID *id)
bool DEG_is_evaluated_id(const ID *id)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
ViewLayer * DEG_get_evaluated_view_layer(const Depsgraph *graph)
Object * DEG_get_original_object(Object *object)
ID * DEG_get_evaluated_id(const Depsgraph *depsgraph, ID *id)
void DEG_get_customdata_mask_for_object(const Depsgraph *graph, Object *object, CustomData_MeshMasks *r_mask)
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:964
@ ID_TAG_COPIED_ON_EVAL_FINAL_RESULT
Definition DNA_ID.h:974
#define CD_MASK_PROP_BYTE_COLOR
#define CD_MASK_ORIGINDEX
#define CD_MASK_ORCO
#define CD_MASK_MDEFORMVERT
#define CD_MASK_MVERT_SKIN
#define CD_MASK_PROP_FLOAT2
#define CD_MASK_MTFACE
#define CD_MASK_ORIGSPACE_MLOOP
@ CD_ORIGSPACE_MLOOP
@ CD_CLOTH_ORCO
#define CD_MASK_CLOTH_ORCO
@ eModifierMode_Render
@ eModifierMode_Editmode
@ eModifierMode_Realtime
@ eModifierType_Cloth
@ eModifierType_Multires
@ OB_MODE_EDIT
@ OB_MODE_WEIGHT_PAINT
@ OB_MODE_SCULPT
@ OB_MODE_TEXTURE_PAINT
@ 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
Read Guarded memory(de)allocation.
volatile int lock
Array< float3 > BM_mesh_vert_coords_alloc(BMesh *bm)
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr bool is_empty() const
Definition BLI_span.hh:510
constexpr T * data() const
Definition BLI_span.hh:540
constexpr void fill(const T &value) const
Definition BLI_span.hh:518
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:726
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:138
constexpr bool is_empty() const
Definition BLI_span.hh:261
void resize(const int64_t new_size)
void replace(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Depsgraph * depsgraph
draw_view in_light_buf[] float
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
#define ASSERT_IS_VALID_MESH(mesh)
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
const ShrinkwrapBoundaryData & boundary_cache_ensure(const Mesh &mesh)
static void add_orco_mesh(Object &ob, const BMEditMesh *em, Mesh &mesh, const Mesh *mesh_orco, const eCustomDataType layer)
static Mesh * create_orco_mesh(const Object &ob, const Mesh &mesh, const BMEditMesh *em, eCustomDataType layer)
Mesh * editbmesh_get_eval_cage(Depsgraph *depsgraph, const Scene *scene, Object *obedit, BMEditMesh *em, const CustomData_MeshMasks *dataMask)
static void mesh_build_data(Depsgraph &depsgraph, const Scene &scene, Object &ob, const CustomData_MeshMasks &dataMask, const bool need_mapping)
static void object_get_datamask(const Depsgraph &depsgraph, Object &ob, CustomData_MeshMasks &r_mask, bool *r_need_mapping)
Mesh * mesh_create_eval_no_deform_render(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static void editbmesh_calc_modifiers(Depsgraph &depsgraph, const Scene &scene, Object &ob, const CustomData_MeshMasks &dataMask, Mesh **r_cage, Mesh **r_final, GeometrySet **r_geometry_set)
static void mesh_init_origspace(Mesh &mesh)
Mesh * mesh_create_eval_no_deform(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static Span< float3 > get_orco_coords(const Object &ob, const BMEditMesh *em, eCustomDataType layer_type, Array< float3 > &storage)
static Mesh * modifier_modify_mesh_and_geometry_set(ModifierData *md, const ModifierEvalContext &mectx, Mesh *input_mesh, GeometrySet &geometry_set)
void mesh_eval_to_meshkey(const Mesh *me_deformed, Mesh *mesh, KeyBlock *kb)
static void mesh_calc_finalize(const Mesh &mesh_input, Mesh &mesh_eval)
static void make_vertexcos__mapFunc(void *user_data, int index, const float co[3], const float[3])
bool editbmesh_modifier_is_enabled(const Scene *scene, const Object *ob, ModifierData *md, bool has_prev_mesh)
static void mesh_build_extra_data(const Depsgraph &depsgraph, const Object &ob, const Mesh &mesh_eval)
static void mesh_set_only_copy(Mesh *mesh, const CustomData_MeshMasks *mask)
static MutableSpan< float3 > orco_coord_layer_ensure(Mesh &mesh, const eCustomDataType layer)
Mesh * editbmesh_get_eval_cage_from_orig(Depsgraph *depsgraph, const Scene *scene, Object *obedit, const CustomData_MeshMasks *dataMask)
Mesh * mesh_get_eval_deform(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static void editbmesh_build_data(Depsgraph &depsgraph, const Scene &scene, Object &obedit, CustomData_MeshMasks &dataMask)
void mesh_data_update(Depsgraph &depsgraph, const Scene &scene, Object &ob, const CustomData_MeshMasks &dataMask)
static void mesh_calc_modifiers(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)
void mesh_get_mapped_verts_coords(Mesh *mesh_eval, MutableSpan< float3 > r_cos)
static MutableSpan< float3 > mesh_wrapper_vert_coords_ensure_for_write(Mesh *mesh)
Mesh * mesh_create_eval_final(Depsgraph *depsgraph, const Scene *scene, Object *ob, const CustomData_MeshMasks *dataMask)
static void set_rest_position(Mesh &mesh)
void isolate_task(const Function &function)
Definition BLI_task.hh:226
VecBase< float, 3 > float3
#define min(a, b)
Definition sort.c:32
#define FLT_MAX
Definition stdcycles.h:14
unsigned int uint32_t
Definition stdint.h:80
CustomData edata
CustomData pdata
struct ClothSimSettings * sim_parms
Definition DNA_ID.h:413
int tag
Definition DNA_ID.h:434
char name[66]
Definition DNA_ID.h:425
void * data
MeshRuntimeHandle * runtime
struct Key * key
int verts_num
struct ModifierData * next
void(* modify_geometry_set)(ModifierData *md, const ModifierEvalContext *ctx, blender::bke::GeometrySet *geometry_set)
void(* required_data_mask)(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
ModifierTypeFlag flags
ModifierTypeType type
void(* deform_verts_EM)(ModifierData *md, const ModifierEvalContext *ctx, const BMEditMesh *em, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
ObjectRuntimeHandle * runtime
uint8_t modifier_flag
struct SculptSession * sculpt
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)