Commit: 0845dc0eece716947148ecc11a2276b0f5634f65
Author: Bastien Montagne
Date:   Tue Oct 27 11:34:37 2020 +0100
Branches: blender-v2.83-release
https://developer.blender.org/rB0845dc0eece716947148ecc11a2276b0f5634f65

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

===================================================================

M       source/blender/blenkernel/intern/paint.c
M       source/blender/editors/sculpt_paint/sculpt.c
M       source/blender/editors/sculpt_paint/sculpt_undo.c

===================================================================

diff --git a/source/blender/blenkernel/intern/paint.c 
b/source/blender/blenkernel/intern/paint.c
index 0c9b1721f79..e66bc3f7ba0 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -1664,19 +1664,18 @@ void BKE_sculpt_update_object_after_eval(Depsgraph 
*depsgraph, Object *ob_eval)
   sculpt_update_object(depsgraph, ob_orig, me_eval, false, false);
 }
 
+/** \warning Expects a fully evaluated depsgraph. */
 void BKE_sculpt_update_object_for_edit(Depsgraph *depsgraph,
                                        Object *ob_orig,
                                        bool need_pmap,
                                        bool need_mask)
 {
-  /* Update from sculpt operators and undo, to update sculpt session
-   * and PBVH after edits. */
-  Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
-  Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob_orig);
-  Mesh *me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, 
&CD_MASK_BAREMESH);
-
   BLI_assert(ob_orig == DEG_get_original_object(ob_orig));
 
+  Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob_orig);
+  Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
+  BLI_assert(me_eval != NULL);
+
   sculpt_update_object(depsgraph, ob_orig, me_eval, need_pmap, need_mask);
 }
 
diff --git a/source/blender/editors/sculpt_paint/sculpt.c 
b/source/blender/editors/sculpt_paint/sculpt.c
index 1cac680a56f..06b5de9719e 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -7524,6 +7524,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. */
@@ -7587,6 +7588,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. */
@@ -7594,10 +7596,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);
-
   sculpt_init_session(depsgraph, scene, ob);
 
   /* Mask layer is required. */
diff --git a/source/blender/editors/sculpt_paint/sculpt_undo.c 
b/source/blender/editors/sculpt_paint/sculpt_undo.c
index 3d774350110..b8fd7d494bf 100644
--- a/source/blender/editors/sculpt_paint/sculpt_undo.c
+++ b/source/blender/editors/sculpt_paint/sculpt_undo.c
@@ -1443,8 +1443,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) {
@@ -1452,6 +1450,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. */

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to