[Bf-blender-cvs] [e618d82] master: Fix T38054: High CPU usage with many objects

Sergey Sharybin noreply at git.blender.org
Mon Jan 13 11:03:30 CET 2014


Commit: e618d8238e0e065bdfd2d9332ba7231e870a7e7d
Author: Sergey Sharybin
Date:   Thu Jan 9 19:13:26 2014 +0600
https://developer.blender.org/rBe618d8238e0e065bdfd2d9332ba7231e870a7e7d

Fix T38054: High CPU usage with many objects

This is a regression since threaded dependency graph landed to master.
Root of the issue goes to the loads of graph preparation being done
even if there's nothing to be updated.

The idea of this change is to use ID type recalc bits to determine
whether there're objects to be updated. Generally speaking, we now
check object and object data datablocks with DAG_id_type_tagged()
and if there's no such IDs tagged we skip the whole task pool creation
and so,

The only difficult aspect was that in some circumstances it was possible
that there are tagged objects but nothing in ID recalc bit fields.

There were several different circumstances when it was possible:

* When one assigns object->recalc flag directly DAG flush didn't
  set corresponding bits to ID recalc bits. Partially it is fixed
  by making it so flush will set bitfield, but also for object
  types there's no reason to assign recalc flag directly. Using
  generic DAG_id_type_tag works almost the same fast as direct
  assignment, ensures all the bitflags are set properly and for the
  long run it seems it's what we would actually want to.

* DAG_on_visible_update() didn't set recalc bits at all.

* Some areas were checking for object->recalc != 0, however it is was
  possible that object recalc flag contains PSYS_RECALC_CHILD which
  was never cleaned from there.

  No idea why would we need to assign such a flag when enabling
  scene simplification, this is to be investigated separately.

* It is possible that scene_update_post and frame_update_post handlers
  will modify objects. The issue is that DAG_ids_clear_recalc is called
  just after callbacks, which leaves objects with recalc flags but no
  corresponding bit in ID recalc bitfield. This leads to some kind of
  regression when using ID type tag fields to check whether there objects
  to be updated internally comparing threaded DAG with legacy one.

  For now let's have a workaround which will preserve tag for ID_OB
  if there're objects with OB_RECALC_ALL bits. This keeps behavior
  unchanged comparing with 2.69 release.

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

M	source/blender/blenkernel/intern/depsgraph.c
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenkernel/intern/scene.c
M	source/blender/editors/mesh/editmesh_utils.c
M	source/blender/editors/transform/transform_conversions.c
M	source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
M	source/blender/makesrna/intern/rna_scene.c
M	source/blender/render/intern/source/convertblender.c

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

diff --git a/source/blender/blenkernel/intern/depsgraph.c b/source/blender/blenkernel/intern/depsgraph.c
index 13f0c29..0132745 100644
--- a/source/blender/blenkernel/intern/depsgraph.c
+++ b/source/blender/blenkernel/intern/depsgraph.c
@@ -2031,7 +2031,7 @@ void DAG_scene_update_flags(Main *bmain, Scene *scene, unsigned int lay, const s
 		/* test: set time flag, to disable baked systems to update */
 		for (SETLOOPER(scene, sce_iter, base)) {
 			ob = base->object;
-			if (ob->recalc)
+			if (ob->recalc & OB_RECALC_ALL)
 				ob->recalc |= OB_RECALC_TIME;
 		}
 
@@ -2115,10 +2115,14 @@ static void dag_group_on_visible_update(Group *group)
 	group->id.flag |= LIB_DOIT;
 
 	for (go = group->gobject.first; go; go = go->next) {
-		if (ELEM6(go->ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
+		if (ELEM6(go->ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
 			go->ob->recalc |= OB_RECALC_DATA;
-		if (go->ob->proxy_from)
+			lib_id_recalc_tag(G.main, &go->ob->id);
+		}
+		if (go->ob->proxy_from) {
 			go->ob->recalc |= OB_RECALC_OB;
+			lib_id_recalc_tag(G.main, &go->ob->id);
+		}
 
 		if (go->ob->dup_group)
 			dag_group_on_visible_update(go->ob->dup_group);
@@ -2156,10 +2160,14 @@ void DAG_on_visible_update(Main *bmain, const short do_time)
 			oblay = (node) ? node->lay : ob->lay;
 
 			if ((oblay & lay) & ~scene->lay_updated) {
-				if (ELEM6(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE))
+				if (ELEM6(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_LATTICE)) {
 					ob->recalc |= OB_RECALC_DATA;
-				if (ob->proxy && (ob->proxy_group == NULL))
+					lib_id_recalc_tag(bmain, &ob->id);
+				}
+				if (ob->proxy && (ob->proxy_group == NULL)) {
 					ob->proxy->recalc |= OB_RECALC_DATA;
+					lib_id_recalc_tag(bmain, &ob->id);
+				}
 				if (ob->dup_group) 
 					dag_group_on_visible_update(ob->dup_group);
 			}
@@ -2210,6 +2218,13 @@ static void dag_id_flush_update(Main *bmain, Scene *sce, ID *id)
 		ob = (Object *)id;
 		BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
 
+		/* So if someone tagged object recalc directly,
+		 * id_tag_update biffield stays relevant
+		 */
+		if (ob->recalc & OB_RECALC_ALL) {
+			DAG_id_type_tag(bmain, GS(id->name));
+		}
+
 		if (ob->recalc & OB_RECALC_DATA) {
 			/* all users of this ob->data should be checked */
 			id = ob->data;
@@ -2311,6 +2326,7 @@ static void dag_id_flush_update(Main *bmain, Scene *sce, ID *id)
 					          CONSTRAINT_TYPE_OBJECTSOLVER))
 					{
 						obt->recalc |= OB_RECALC_OB;
+						lib_id_recalc_tag(bmain, &obt->id);
 						break;
 					}
 				}
@@ -2427,12 +2443,34 @@ void DAG_ids_check_recalc(Main *bmain, Scene *scene, int time)
 	dag_editors_scene_update(bmain, scene, (updated || time));
 }
 
+/* It is possible that scene_update_post and frame_update_post handlers
+ * will modify objects. The issue is that DAG_ids_clear_recalc is called
+ * just after callbacks, which leaves objects with recalc flags but no
+ * corresponding bit in ID recalc bitfield. This leads to some kind of
+ * regression when using ID type tag fields to check whether there objects
+ * to be updated internally comparing threaded DAG with legacy one.
+ *
+ * For now let's have a workaround which will preserve tag for ID_OB
+ * if there're objects with OB_RECALC_ALL bits. This keeps behavior
+ * unchanged comparing with 2.69 release.
+ *
+ * TODO(sergey): Need to get rid of such a workaround.
+ *
+ *                                                 - sergey -
+ */
+
+#define POST_UPDATE_HANDLER_WORKAROUND
+
 void DAG_ids_clear_recalc(Main *bmain)
 {
 	ListBase *lbarray[MAX_LIBARRAY];
 	bNodeTree *ntree;
 	int a;
 
+#ifdef POST_UPDATE_HANDLER_WORKAROUND
+	bool have_updated_objects = false;
+#endif
+
 	/* loop over all ID types */
 	a  = set_listbasepointers(bmain, lbarray);
 
@@ -2447,6 +2485,15 @@ void DAG_ids_clear_recalc(Main *bmain)
 				if (id->flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA))
 					id->flag &= ~(LIB_ID_RECALC | LIB_ID_RECALC_DATA);
 
+#ifdef POST_UPDATE_HANDLER_WORKAROUND
+				if (GS(id->name) == ID_OB) {
+					Object *object = (Object *) id;
+					if (object->recalc & OB_RECALC_ALL) {
+						have_updated_objects = true;
+					}
+				}
+#endif
+
 				/* some ID's contain semi-datablock nodetree */
 				ntree = ntreeFromID(id);
 				if (ntree && (ntree->id.flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA)))
@@ -2456,6 +2503,12 @@ void DAG_ids_clear_recalc(Main *bmain)
 	}
 
 	memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));
+
+#ifdef POST_UPDATE_HANDLER_WORKAROUND
+	if (have_updated_objects) {
+		DAG_id_type_tag(bmain, ID_OB);
+	}
+#endif
 }
 
 void DAG_id_tag_update_ex(Main *bmain, ID *id, short flag)
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 821df59..f226419 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -553,6 +553,8 @@ void BKE_main_lib_objects_recalc_all(Main *bmain)
 	for (ob = bmain->object.first; ob; ob = ob->id.next)
 		if (ob->id.lib)
 			ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
+
+	DAG_id_type_tag(bmain, ID_OB);
 }
 
 /**
diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c
index e489759..4d8075e 100644
--- a/source/blender/blenkernel/intern/scene.c
+++ b/source/blender/blenkernel/intern/scene.c
@@ -1365,18 +1365,80 @@ static void print_threads_statistics(ThreadedObjectUpdateState *state)
 #endif
 }
 
-static void scene_update_objects(EvaluationContext *eval_ctx, Scene *scene, Scene *scene_parent)
+static bool scene_need_update_objects(Main *bmain)
+{
+	return
+		/* Object datablocks themselves (for OB_RECALC_OB) */
+		DAG_id_type_tagged(bmain, ID_OB) ||
+
+		/* Objects data datablocks (for OB_RECALC_DATA) */
+		DAG_id_type_tagged(bmain, ID_ME)  ||  /* Mesh */
+		DAG_id_type_tagged(bmain, ID_CU)  ||  /* Curve */
+		DAG_id_type_tagged(bmain, ID_MB)  ||  /* MetaBall */
+		DAG_id_type_tagged(bmain, ID_LA)  ||  /* Lamp */
+		DAG_id_type_tagged(bmain, ID_LT)  ||  /* Lattice */
+		DAG_id_type_tagged(bmain, ID_CA)  ||  /* Camera */
+		DAG_id_type_tagged(bmain, ID_KE)  ||  /* KE */
+		DAG_id_type_tagged(bmain, ID_SPK) ||  /* Speaker */
+		DAG_id_type_tagged(bmain, ID_AR);     /* Armature */
+}
+
+static void scene_update_objects(EvaluationContext *eval_ctx, Main *bmain, Scene *scene, Scene *scene_parent)
 {
 	TaskScheduler *task_scheduler = BLI_task_scheduler_get();
 	TaskPool *task_pool;
 	ThreadedObjectUpdateState state;
 
+	/* Early check for whether we need to invoke all the task-based
+	 * tihngs (spawn new ppol, traverse dependency graph and so on).
+	 *
+	 * Basically if there's no ID datablocks tagged for update which
+	 * corresponds to object->recalc flags (which are checked in
+	 * BKE_object_handle_update() then we do nothing here.
+	 */
+	if (!scene_need_update_objects(bmain)) {
+		/* For debug builds we check whether early return didn't give
+		 * us any regressions in terms of missing updates.
+		 *
+		 * TODO(sergey): Remove once we're sure the check above is correct.
+		 */
+#ifndef NDEBUG
+		Base *base;
+
+		for (base = scene->base.first; base; base = base->next) {
+			Object *object = base->object;
+
+			BLI_assert((object->recalc & OB_RECALC_ALL) == 0);
+
+			if (object->proxy) {
+				BLI_assert((object->proxy->recalc & OB_RECALC_ALL) == 0);
+			}
+
+			if (object->dup_group && (object->transflag & OB_DUPLIGROUP)) {
+				GroupObject *go;
+				for (go = object->dup_group->gobject.first; go; go = go->next) {
+					if (go->ob) {
+						BLI_assert((go->ob->recalc & OB_RECALC_ALL) == 0);
+					}
+				}
+			}
+		}
+#endif
+
+		return;
+	}
+
 	state.eval_ctx = eval_ctx;
 	state.scene = scene;
 	state.scene_parent = scene_parent;
-	memset(state.statistics, 0, sizeof(state.statistics));
-	state.has_updated_objects = false;
-	state.base_time = PIL_check_seconds_timer();
+
+	/* Those are only needed when blender is run with --debug argument. */
+	if (G.debug & G_DEBUG) {
+		memset(state.statistics, 0, sizeof(state.statistics));
+		state.has_updated_objects = false;
+		state.base_time = PIL_check_seconds_timer();
+	}
+
 #ifdef MBALL_SINGLETHREAD_HACK
 	state.has_mballs = false;
 #endif
@@ -1408,7 +1470,7 @@ static void scene_update_tagged_recursive(EvaluationContext *eval_ctx, Main *bma
 		scene_update_tagged_recursive(eval_ctx, bmain, scene->set, scene_parent);
 
 	/* scene objects */
-	scene_update_objects(eval_ctx, scene, scene_parent);
+	scene_update_objects(eval_ctx, bmain, scene, scene_parent);
 
 	/* scene drivers... */
 	scene_update_drivers(bmain, scene);
diff --git a/source/blender/editors/mesh/editmesh_utils.c b/source/blender/editors/mesh/editmesh_utils.c
index 8475512..9f70135 100644
--- a/source/blender/editors/mesh/editmesh_utils.c
+++ b/source/blender/editors/mesh/editmesh_utils.c
@@ -113,7 +113,10 @@ void EDBM_mesh_ensure_valid_dm_hack(Scene *scene, BMEditMesh *em)
 	if ((((ID *)em->ob->data)->flag & LIB_ID_RECALC) ||
 	    (em->ob->recalc & OB_RECALC_DATA))
 	{
-		em->ob->recalc |= OB_RECALC_DATA;  /* since we may not have done selection flushing */
+		/* since we may not have done selection flushing */
+		if ((em->ob->recalc & OB_RECALC_DATA) == 0) {
+			DAG_id_tag_update(&em->ob->id, OB_RECALC_DATA);
+		}
 		BKE_object_handle_update(G.main->eval_ctx, scene, em->ob);
 	}
 }
diff --git a/source/blender/editors/transform/transform_conversions.c b/source/blender/editors/transform/transform_conversions.c
index 2c001ca..55b114b 100644
--- a/source/blender/editors/transform/transform_conversions.c
+++ b/source/blender/editors/transform/transform_conversions.c
@@ -4943,8 +4943,7 @@ static void set_trans_object_base_flags(TransInfo *t)
 					base->flag |= BA_WAS_SEL;
 				}
 			}
-			/* used for flush, depgraph will change recalcs if needed :) */
-			ob->recalc |= OB_RECALC_OB;
+			DAG_id_tag_update(&ob->id, OB_RECALC_OB);
 		}
 	}
 
@@ -5018,8 +5017,7 @@ static int count_proportional_objects(TransInfo *t)
 		    (BASE_EDITABLE_BGMODE(v3d, scene, base)))
 		{
 
-			/* used for flush, depgraph will change recalcs if needed :) */
-			ob->recalc |= OB_RECALC_OB;
+			DAG_id_tag_update(&ob->

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list