2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 /** \file \ingroup edmesh
20 #include "MEM_guardedalloc.h"
24 #include "DNA_mesh_types.h"
25 #include "DNA_meshdata_types.h"
26 #include "DNA_object_types.h"
27 #include "DNA_key_types.h"
28 #include "DNA_layer_types.h"
30 #include "BLI_listbase.h"
31 #include "BLI_array_utils.h"
33 #include "BKE_context.h"
35 #include "BKE_layer.h"
37 #include "BKE_editmesh.h"
38 #include "BKE_undo_system.h"
40 #include "DEG_depsgraph.h"
42 #include "ED_object.h"
50 #define USE_ARRAY_STORE
52 #ifdef USE_ARRAY_STORE
53 // # define DEBUG_PRINT
54 // # define DEBUG_TIME
56 # include "PIL_time_utildefines.h"
59 # include "BLI_array_store.h"
60 # include "BLI_array_store_utils.h"
61 /* check on best size later... */
62 # define ARRAY_CHUNK_SIZE 256
64 # define USE_ARRAY_STORE_THREAD
67 #ifdef USE_ARRAY_STORE_THREAD
68 # include "BLI_task.h"
71 /** We only need this locally. */
72 static CLG_LogRef LOG = {"ed.undo.mesh"};
74 /* -------------------------------------------------------------------- */
75 /** \name Undo Conversion
78 #ifdef USE_ARRAY_STORE
80 /* Single linked list of layers stored per type */
81 typedef struct BArrayCustomData {
82 struct BArrayCustomData *next;
84 int states_len; /* number of layers for each type */
85 BArrayState *states[0];
90 typedef struct UndoMesh {
95 * this isn't a prefect solution, if you edit keys and change shapes this works well (fixing [#32442]),
96 * but editing shape keys, going into object mode, removing or changing their order,
97 * then go back into editmode and undo will give issues - where the old index will be out of sync
98 * with the new object index.
100 * There are a few ways this could be made to work but for now its a known limitation with mixing
101 * object and editmode operations - Campbell */
104 #ifdef USE_ARRAY_STORE
105 /* NULL arrays are considered empty */
106 struct { /* most data is stored as 'custom' data */
107 BArrayCustomData *vdata, *edata, *ldata, *pdata;
108 BArrayState **keyblocks;
109 BArrayState *mselect;
111 #endif /* USE_ARRAY_STORE */
117 #ifdef USE_ARRAY_STORE
119 /** \name Array Store
123 struct BArrayStore_AtSize bs_stride;
126 /* We could have the undo API pass in the previous state, for now store a local list */
127 ListBase local_links;
129 #ifdef USE_ARRAY_STORE_THREAD
133 } um_arraystore = {{NULL}};
135 static void um_arraystore_cd_compact(
136 struct CustomData *cdata, const size_t data_len,
138 const BArrayCustomData *bcd_reference,
139 BArrayCustomData **r_bcd_first)
147 const BArrayCustomData *bcd_reference_current = bcd_reference;
148 BArrayCustomData *bcd = NULL, *bcd_first = NULL, *bcd_prev = NULL;
149 for (int layer_start = 0, layer_end; layer_start < cdata->totlayer; layer_start = layer_end) {
150 const CustomDataType type = cdata->layers[layer_start].type;
152 layer_end = layer_start + 1;
153 while ((layer_end < cdata->totlayer) &&
154 (type == cdata->layers[layer_end].type))
159 const int stride = CustomData_sizeof(type);
160 BArrayStore *bs = create ? BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE) : NULL;
161 const int layer_len = layer_end - layer_start;
164 if (bcd_reference_current && (bcd_reference_current->type == type)) {
165 /* common case, the reference is aligned */
168 bcd_reference_current = NULL;
170 /* do a full lookup when un-alligned */
172 const BArrayCustomData *bcd_iter = bcd_reference;
174 if (bcd_iter->type == type) {
175 bcd_reference_current = bcd_iter;
178 bcd_iter = bcd_iter->next;
185 bcd = MEM_callocN(sizeof(BArrayCustomData) + (layer_len * sizeof(BArrayState *)), __func__);
188 bcd->states_len = layer_end - layer_start;
191 bcd_prev->next = bcd;
200 CustomDataLayer *layer = &cdata->layers[layer_start];
201 for (int i = 0; i < layer_len; i++, layer++) {
204 BArrayState *state_reference =
205 (bcd_reference_current && i < bcd_reference_current->states_len) ?
206 bcd_reference_current->states[i] : NULL;
207 bcd->states[i] = BLI_array_store_state_add(
208 bs, layer->data, (size_t)data_len * stride, state_reference);
211 bcd->states[i] = NULL;
216 MEM_freeN(layer->data);
222 if (bcd_reference_current) {
223 bcd_reference_current = bcd_reference_current->next;
229 *r_bcd_first = bcd_first;
234 * \note There is no room for data going out of sync here.
235 * The layers and the states are stored together so this can be kept working.
237 static void um_arraystore_cd_expand(
238 const BArrayCustomData *bcd, struct CustomData *cdata, const size_t data_len)
240 CustomDataLayer *layer = cdata->layers;
242 const int stride = CustomData_sizeof(bcd->type);
243 for (int i = 0; i < bcd->states_len; i++) {
244 BLI_assert(bcd->type == layer->type);
245 if (bcd->states[i]) {
247 layer->data = BLI_array_store_state_data_get_alloc(bcd->states[i], &state_len);
248 BLI_assert(stride * data_len == state_len);
249 UNUSED_VARS_NDEBUG(stride, data_len);
260 static void um_arraystore_cd_free(BArrayCustomData *bcd)
263 BArrayCustomData *bcd_next = bcd->next;
264 const int stride = CustomData_sizeof(bcd->type);
265 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
266 for (int i = 0; i < bcd->states_len; i++) {
267 if (bcd->states[i]) {
268 BLI_array_store_state_remove(bs, bcd->states[i]);
277 * \param create: When false, only free the arrays.
278 * This is done since when reading from an undo state, they must be temporarily expanded.
279 * then discarded afterwards, having this argument avoids having 2x code paths.
281 static void um_arraystore_compact_ex(
282 UndoMesh *um, const UndoMesh *um_ref,
287 um_arraystore_cd_compact(&me->vdata, me->totvert, create, um_ref ? um_ref->store.vdata : NULL, &um->store.vdata);
288 um_arraystore_cd_compact(&me->edata, me->totedge, create, um_ref ? um_ref->store.edata : NULL, &um->store.edata);
289 um_arraystore_cd_compact(&me->ldata, me->totloop, create, um_ref ? um_ref->store.ldata : NULL, &um->store.ldata);
290 um_arraystore_cd_compact(&me->pdata, me->totpoly, create, um_ref ? um_ref->store.pdata : NULL, &um->store.pdata);
292 if (me->key && me->key->totkey) {
293 const size_t stride = me->key->elemsize;
294 BArrayStore *bs = create ? BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE) : NULL;
296 um->store.keyblocks = MEM_mallocN(me->key->totkey * sizeof(*um->store.keyblocks), __func__);
298 KeyBlock *keyblock = me->key->block.first;
299 for (int i = 0; i < me->key->totkey; i++, keyblock = keyblock->next) {
301 BArrayState *state_reference =
302 (um_ref && um_ref->me.key && (i < um_ref->me.key->totkey)) ?
303 um_ref->store.keyblocks[i] : NULL;
304 um->store.keyblocks[i] = BLI_array_store_state_add(
305 bs, keyblock->data, (size_t)keyblock->totelem * stride,
309 if (keyblock->data) {
310 MEM_freeN(keyblock->data);
311 keyblock->data = NULL;
316 if (me->mselect && me->totselect) {
317 BLI_assert(create == (um->store.mselect == NULL));
319 BArrayState *state_reference = um_ref ? um_ref->store.mselect : NULL;
320 const size_t stride = sizeof(*me->mselect);
321 BArrayStore *bs = BLI_array_store_at_size_ensure(&um_arraystore.bs_stride, stride, ARRAY_CHUNK_SIZE);
322 um->store.mselect = BLI_array_store_state_add(
323 bs, me->mselect, (size_t)me->totselect * stride, state_reference);
326 /* keep me->totselect for validation */
327 MEM_freeN(me->mselect);
332 um_arraystore.users += 1;
335 BKE_mesh_update_customdata_pointers(me, false);
339 * Move data from allocated arrays to de-duplicated states and clear arrays.
341 static void um_arraystore_compact(UndoMesh *um, const UndoMesh *um_ref)
343 um_arraystore_compact_ex(um, um_ref, true);
346 static void um_arraystore_compact_with_info(UndoMesh *um, const UndoMesh *um_ref)
349 size_t size_expanded_prev, size_compacted_prev;
350 BLI_array_store_at_size_calc_memory_usage(&um_arraystore.bs_stride, &size_expanded_prev, &size_compacted_prev);
354 TIMEIT_START(mesh_undo_compact);
357 um_arraystore_compact(um, um_ref);
360 TIMEIT_END(mesh_undo_compact);
365 size_t size_expanded, size_compacted;
366 BLI_array_store_at_size_calc_memory_usage(&um_arraystore.bs_stride, &size_expanded, &size_compacted);
368 const double percent_total = size_expanded ?
369 (((double)size_compacted / (double)size_expanded) * 100.0) : -1.0;
371 size_t size_expanded_step = size_expanded - size_expanded_prev;
372 size_t size_compacted_step = size_compacted - size_compacted_prev;
373 const double percent_step = size_expanded_step ?
374 (((double)size_compacted_step / (double)size_expanded_step) * 100.0) : -1.0;
376 printf("overall memory use: %.8f%% of expanded size\n", percent_total);
377 printf("step memory use: %.8f%% of expanded size\n", percent_step);
382 #ifdef USE_ARRAY_STORE_THREAD
386 const UndoMesh *um_ref; /* can be NULL */
388 static void um_arraystore_compact_cb(TaskPool *__restrict UNUSED(pool),
390 int UNUSED(threadid))
392 struct UMArrayData *um_data = taskdata;
393 um_arraystore_compact_with_info(um_data->um, um_data->um_ref);
396 #endif /* USE_ARRAY_STORE_THREAD */
399 * Remove data we only expanded for temporary use.
401 static void um_arraystore_expand_clear(UndoMesh *um)
403 um_arraystore_compact_ex(um, NULL, false);
406 static void um_arraystore_expand(UndoMesh *um)
410 um_arraystore_cd_expand(um->store.vdata, &me->vdata, me->totvert);
411 um_arraystore_cd_expand(um->store.edata, &me->edata, me->totedge);
412 um_arraystore_cd_expand(um->store.ldata, &me->ldata, me->totloop);
413 um_arraystore_cd_expand(um->store.pdata, &me->pdata, me->totpoly);
415 if (um->store.keyblocks) {
416 const size_t stride = me->key->elemsize;
417 KeyBlock *keyblock = me->key->block.first;
418 for (int i = 0; i < me->key->totkey; i++, keyblock = keyblock->next) {
419 BArrayState *state = um->store.keyblocks[i];
421 keyblock->data = BLI_array_store_state_data_get_alloc(state, &state_len);
422 BLI_assert(keyblock->totelem == (state_len / stride));
423 UNUSED_VARS_NDEBUG(stride);
427 if (um->store.mselect) {
428 const size_t stride = sizeof(*me->mselect);
429 BArrayState *state = um->store.mselect;
431 me->mselect = BLI_array_store_state_data_get_alloc(state, &state_len);
432 BLI_assert(me->totselect == (state_len / stride));
433 UNUSED_VARS_NDEBUG(stride);
436 /* not essential, but prevents accidental dangling pointer access */
437 BKE_mesh_update_customdata_pointers(me, false);
440 static void um_arraystore_free(UndoMesh *um)
444 um_arraystore_cd_free(um->store.vdata);
445 um_arraystore_cd_free(um->store.edata);
446 um_arraystore_cd_free(um->store.ldata);
447 um_arraystore_cd_free(um->store.pdata);
449 if (um->store.keyblocks) {
450 const size_t stride = me->key->elemsize;
451 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
452 for (int i = 0; i < me->key->totkey; i++) {
453 BArrayState *state = um->store.keyblocks[i];
454 BLI_array_store_state_remove(bs, state);
456 MEM_freeN(um->store.keyblocks);
457 um->store.keyblocks = NULL;
460 if (um->store.mselect) {
461 const size_t stride = sizeof(*me->mselect);
462 BArrayStore *bs = BLI_array_store_at_size_get(&um_arraystore.bs_stride, stride);
463 BArrayState *state = um->store.mselect;
464 BLI_array_store_state_remove(bs, state);
465 um->store.mselect = NULL;
468 um_arraystore.users -= 1;
470 BLI_assert(um_arraystore.users >= 0);
472 if (um_arraystore.users == 0) {
474 printf("mesh undo store: freeing all data!\n");
476 BLI_array_store_at_size_clear(&um_arraystore.bs_stride);
478 #ifdef USE_ARRAY_STORE_THREAD
479 BLI_task_pool_free(um_arraystore.task_pool);
480 um_arraystore.task_pool = NULL;
488 #endif /* USE_ARRAY_STORE */
492 /* undo simply makes copies of a bmesh */
493 static void *undomesh_from_editmesh(UndoMesh *um, BMEditMesh *em, Key *key)
495 BLI_assert(BLI_array_is_zeroed(um, 1));
496 #ifdef USE_ARRAY_STORE_THREAD
497 /* changes this waits is low, but must have finished */
498 if (um_arraystore.task_pool) {
499 BLI_task_pool_work_and_wait(um_arraystore.task_pool);
502 /* make sure shape keys work */
503 um->me.key = key ? BKE_key_copy_nolib(key) : NULL;
505 /* BM_mesh_validate(em->bm); */ /* for troubleshooting */
508 NULL, em->bm, &um->me, (&(struct BMeshToMeshParams){
509 /* Undo code should not be manipulating 'G_MAIN->object' hooks/vertex-parent. */
510 .calc_object_remap = false,
511 .cd_mask_extra = CD_MASK_SHAPE_KEYINDEX,
514 um->selectmode = em->selectmode;
515 um->shapenr = em->bm->shapenr;
517 #ifdef USE_ARRAY_STORE
519 /* We could be more clever here,
520 * the previous undo state may be from a separate mesh. */
521 const UndoMesh *um_ref = um_arraystore.local_links.last ?
522 ((LinkData *)um_arraystore.local_links.last)->data : NULL;
525 BLI_addtail(&um_arraystore.local_links, BLI_genericNodeN(um));
527 #ifdef USE_ARRAY_STORE_THREAD
528 if (um_arraystore.task_pool == NULL) {
529 TaskScheduler *scheduler = BLI_task_scheduler_get();
530 um_arraystore.task_pool = BLI_task_pool_create_background(scheduler, NULL);
533 struct UMArrayData *um_data = MEM_mallocN(sizeof(*um_data), __func__);
535 um_data->um_ref = um_ref;
538 um_arraystore.task_pool,
539 um_arraystore_compact_cb, um_data, true, TASK_PRIORITY_LOW);
541 um_arraystore_compact_with_info(um, um_ref);
549 static void undomesh_to_editmesh(UndoMesh *um, BMEditMesh *em, Mesh *obmesh)
554 Key *key = obmesh->key;
556 #ifdef USE_ARRAY_STORE
557 #ifdef USE_ARRAY_STORE_THREAD
558 /* changes this waits is low, but must have finished */
559 BLI_task_pool_work_and_wait(um_arraystore.task_pool);
563 TIMEIT_START(mesh_undo_expand);
566 um_arraystore_expand(um);
569 TIMEIT_END(mesh_undo_expand);
571 #endif /* USE_ARRAY_STORE */
573 const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(&um->me);
575 em->bm->shapenr = um->shapenr;
581 &((struct BMeshCreateParams){.use_toolflags = true,}));
584 bm, &um->me, (&(struct BMeshFromMeshParams){
585 .calc_face_normal = true, .active_shapekey = um->shapenr,
588 em_tmp = BKE_editmesh_create(bm, true);
591 em->selectmode = um->selectmode;
592 bm->selectmode = um->selectmode;
595 bm->spacearr_dirty = BM_SPACEARR_DIRTY_ALL;
597 /* T35170: Restore the active key on the RealMesh. Otherwise 'fake' offset propagation happens
598 * if the active is a basis for any other. */
599 if (key && (key->type == KEY_RELATIVE)) {
600 /* Since we can't add, remove or reorder keyblocks in editmode, it's safe to assume
601 * shapenr from restored bmesh and keyblock indices are in sync. */
602 const int kb_act_idx = ob->shapenr - 1;
604 /* If it is, let's patch the current mesh key block to its restored value.
605 * Else, the offsets won't be computed and it won't matter. */
606 if (BKE_keyblock_is_basis(key, kb_act_idx)) {
607 KeyBlock *kb_act = BLI_findlink(&key->block, kb_act_idx);
609 if (kb_act->totelem != um->me.totvert) {
610 /* The current mesh has some extra/missing verts compared to the undo, adjust. */
611 MEM_SAFE_FREE(kb_act->data);
612 kb_act->data = MEM_mallocN((size_t)(key->elemsize * bm->totvert), __func__);
613 kb_act->totelem = um->me.totvert;
616 BKE_keyblock_update_from_mesh(&um->me, kb_act);
620 ob->shapenr = um->shapenr;
624 #ifdef USE_ARRAY_STORE
625 um_arraystore_expand_clear(um);
629 static void undomesh_free_data(UndoMesh *um)
633 #ifdef USE_ARRAY_STORE
635 #ifdef USE_ARRAY_STORE_THREAD
636 /* changes this waits is low, but must have finished */
637 BLI_task_pool_work_and_wait(um_arraystore.task_pool);
640 /* we need to expand so any allocations in custom-data are freed with the mesh */
641 um_arraystore_expand(um);
644 LinkData *link = BLI_findptr(&um_arraystore.local_links, um, offsetof(LinkData, data));
645 BLI_remlink(&um_arraystore.local_links, link);
648 um_arraystore_free(um);
652 BKE_key_free(me->key);
659 static Object *editmesh_object_from_context(bContext *C)
661 Object *obedit = CTX_data_edit_object(C);
662 if (obedit && obedit->type == OB_MESH) {
663 Mesh *me = obedit->data;
664 if (me->edit_btmesh != NULL) {
673 /* -------------------------------------------------------------------- */
674 /** \name Implements ED Undo System
676 * \note This is similar for all edit-mode types.
679 typedef struct MeshUndoStep_Elem {
680 struct MeshUndoStep_Elem *next, *prev;
681 UndoRefID_Object obedit_ref;
685 typedef struct MeshUndoStep {
687 struct UndoIDPtrMap *id_map;
688 MeshUndoStep_Elem *elems;
692 static bool mesh_undosys_poll(bContext *C)
694 return editmesh_object_from_context(C) != NULL;
697 static bool mesh_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
699 MeshUndoStep *us = (MeshUndoStep *)us_p;
701 /* Important not to use the 3D view when getting objects because all objects
702 * outside of this list will be moved out of edit-mode when reading back undo steps. */
703 ViewLayer *view_layer = CTX_data_view_layer(C);
704 uint objects_len = 0;
705 Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, NULL, &objects_len);
707 us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
708 us->elems_len = objects_len;
710 for (uint i = 0; i < objects_len; i++) {
711 Object *ob = objects[i];
712 MeshUndoStep_Elem *elem = &us->elems[i];
714 elem->obedit_ref.ptr = ob;
715 Mesh *me = elem->obedit_ref.ptr->data;
716 undomesh_from_editmesh(&elem->data, me->edit_btmesh, me->key);
717 us->step.data_size += elem->data.undo_size;
723 static void mesh_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
725 MeshUndoStep *us = (MeshUndoStep *)us_p;
727 /* Load all our objects into edit-mode, clear everything else. */
728 ED_undo_object_editmode_restore_helper(C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
730 BLI_assert(mesh_undosys_poll(C));
732 for (uint i = 0; i < us->elems_len; i++) {
733 MeshUndoStep_Elem *elem = &us->elems[i];
734 Object *obedit = elem->obedit_ref.ptr;
735 Mesh *me = obedit->data;
736 if (me->edit_btmesh == NULL) {
737 /* Should never fail, may not crash but can give odd behavior. */
738 CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
739 us_p->name, obedit->id.name);
742 BMEditMesh *em = me->edit_btmesh;
743 undomesh_to_editmesh(&elem->data, em, obedit->data);
744 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
747 /* The first element is always active */
748 ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
750 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
753 static void mesh_undosys_step_free(UndoStep *us_p)
755 MeshUndoStep *us = (MeshUndoStep *)us_p;
757 for (uint i = 0; i < us->elems_len; i++) {
758 MeshUndoStep_Elem *elem = &us->elems[i];
759 undomesh_free_data(&elem->data);
761 MEM_freeN(us->elems);
763 if (us->id_map != NULL) {
764 BKE_undosys_ID_map_destroy(us->id_map);
768 static void mesh_undosys_foreach_ID_ref(
769 UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
771 MeshUndoStep *us = (MeshUndoStep *)us_p;
773 for (uint i = 0; i < us->elems_len; i++) {
774 MeshUndoStep_Elem *elem = &us->elems[i];
775 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
778 if (us->id_map != NULL) {
779 BKE_undosys_ID_map_foreach_ID_ref(us->id_map, foreach_ID_ref_fn, user_data);
783 /* Export for ED_undo_sys. */
784 void ED_mesh_undosys_type(UndoType *ut)
786 ut->name = "Edit Mesh";
787 ut->poll = mesh_undosys_poll;
788 ut->step_encode = mesh_undosys_step_encode;
789 ut->step_decode = mesh_undosys_step_decode;
790 ut->step_free = mesh_undosys_step_free;
792 ut->step_foreach_ID_ref = mesh_undosys_foreach_ID_ref;
794 ut->use_context = true;
796 ut->step_size = sizeof(MeshUndoStep);