From 90e12e823ff0b1940e8ca8131191914a3a2be007 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 27 Oct 2020 11:34:37 +0100 Subject: Fix T81854: crash when undoing switch between sculpt and edit mode. The logic of `BKE_sculpt_update_object_for_edit` was not correct. such low-level functions should typically never preform depsgraph evaluation themselves, they should be able to rely on getting a fully evaluated depsgraph and just get needed data from there. Supporting that required fixing other broken code higher in the callstack, namely: * `ED_object_sculptmode_enter_ex` was freeing evaluated data, for no valid reason it would seem. * `sculpt_undosys_step_decode` was ensuring an evaluated depsgraph **before** calling `ED_object_mode_generic_exit`, which would invalidate a lot of evaluated data. Note that it is fairly difficult to track down all code paths leading to `BKE_sculpt_update_object_for_edit`, so there may be still cases where this gets called with improperly evaluated depsgraph. Reviewed By: sergey Maniphest Tasks: T81854 Differential Revision: https://developer.blender.org/D9270 --- source/blender/editors/sculpt_paint/sculpt.c | 6 ++---- source/blender/editors/sculpt_paint/sculpt_undo.c | 8 ++++++-- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'source/blender/editors/sculpt_paint') diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c index bd381a7b60c..fafa87e910c 100644 --- a/source/blender/editors/sculpt_paint/sculpt.c +++ b/source/blender/editors/sculpt_paint/sculpt.c @@ -8133,6 +8133,7 @@ static void SCULPT_OT_symmetrize(wmOperatorType *ot) /**** Toggle operator for turning sculpt mode on or off ****/ +/** \warning Expects a fully evaluated depsgraph. */ static void sculpt_init_session(Depsgraph *depsgraph, Scene *scene, Object *ob) { /* Create persistent sculpt mode data. */ @@ -8197,6 +8198,7 @@ void ED_object_sculptmode_enter_ex(Main *bmain, if (flush_recalc) { DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY); + BKE_scene_graph_evaluated_ensure(depsgraph, bmain); } /* Create sculpt mode session data. */ @@ -8204,10 +8206,6 @@ void ED_object_sculptmode_enter_ex(Main *bmain, BKE_sculptsession_free(ob); } - /* Make sure derived final from original object does not reference possibly - * freed memory. */ - BKE_object_free_derived_caches(ob); - /* Copy the current mesh visibility to the Face Sets. */ BKE_sculpt_face_sets_ensure_from_base_mesh_visibility(me); diff --git a/source/blender/editors/sculpt_paint/sculpt_undo.c b/source/blender/editors/sculpt_paint/sculpt_undo.c index f1410ffad1b..f4f30c903aa 100644 --- a/source/blender/editors/sculpt_paint/sculpt_undo.c +++ b/source/blender/editors/sculpt_paint/sculpt_undo.c @@ -1477,8 +1477,6 @@ static void sculpt_undosys_step_decode( { Scene *scene = CTX_data_scene(C); ViewLayer *view_layer = CTX_data_view_layer(C); - /* Sculpt needs evaluated state. */ - BKE_scene_view_layer_graph_evaluated_ensure(bmain, scene, view_layer); Object *ob = OBACT(view_layer); if (ob && (ob->type == OB_MESH)) { if (ob->mode & OB_MODE_SCULPT) { @@ -1486,6 +1484,12 @@ static void sculpt_undosys_step_decode( } else { ED_object_mode_generic_exit(bmain, depsgraph, scene, ob); + + /* Sculpt needs evaluated state. + * Note: needs to be done here, as #ED_object_mode_generic_exit will usually invalidate + * (some) evaluated data. */ + BKE_scene_graph_evaluated_ensure(depsgraph, bmain); + Mesh *me = ob->data; /* Don't add sculpt topology undo steps when reading back undo state. * The undo steps must enter/exit for us. */ -- cgit v1.2.3