[Bf-blender-cvs] [a99dcab148e] blender2.8: Depsgraph: cache collision relations, for performance and stability.

Brecht Van Lommel noreply at git.blender.org
Mon Jun 25 14:13:21 CEST 2018


Commit: a99dcab148ed209409f3b2479ada12d869ae84b6
Author: Brecht Van Lommel
Date:   Fri Jun 22 14:42:03 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBa99dcab148ed209409f3b2479ada12d869ae84b6

Depsgraph: cache collision relations, for performance and stability.

Same reasoning as effector relations in earlier commit.

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

M	source/blender/blenkernel/BKE_cloth.h
M	source/blender/blenkernel/BKE_collision.h
M	source/blender/blenkernel/BKE_effect.h
M	source/blender/blenkernel/intern/cloth.c
M	source/blender/blenkernel/intern/collision.c
M	source/blender/blenkernel/intern/dynamicpaint.c
M	source/blender/blenkernel/intern/effect.c
M	source/blender/blenkernel/intern/particle_system.c
M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/blenkernel/intern/smoke.c
M	source/blender/blenkernel/intern/softbody.c
M	source/blender/depsgraph/DEG_depsgraph_build.h
M	source/blender/depsgraph/DEG_depsgraph_physics.h
M	source/blender/depsgraph/intern/builder/deg_builder_relations.cc
M	source/blender/depsgraph/intern/builder/deg_builder_relations.h
M	source/blender/depsgraph/intern/depsgraph.cc
M	source/blender/depsgraph/intern/depsgraph.h
M	source/blender/depsgraph/intern/depsgraph_build.cc
M	source/blender/depsgraph/intern/depsgraph_intern.h
M	source/blender/depsgraph/intern/depsgraph_physics.cc
M	source/blender/modifiers/intern/MOD_cloth.c
M	source/blender/modifiers/intern/MOD_dynamicpaint.c
M	source/blender/modifiers/intern/MOD_smoke.c
M	source/blender/modifiers/intern/MOD_softbody.c
M	source/blender/physics/BPH_mass_spring.h
M	source/blender/physics/CMakeLists.txt
M	source/blender/physics/intern/BPH_mass_spring.cpp
M	source/blender/physics/intern/hair_volume.cpp

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

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index 154875e5745..244054712f1 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -210,10 +210,10 @@ typedef struct ColliderContacts {
 } ColliderContacts;
 
 // needed for implicit.c
-int cloth_bvh_objcollision (struct Scene *scene, struct Object *ob, struct ClothModifierData *clmd, float step, float dt );
-int cloth_points_objcollision(struct Scene *scene, struct Object *ob, struct ClothModifierData *clmd, float step, float dt);
+int cloth_bvh_objcollision (struct Depsgraph *depsgraph, struct Object *ob, struct ClothModifierData *clmd, float step, float dt );
+int cloth_points_objcollision(struct Depsgraph *depsgraph, struct Object *ob, struct ClothModifierData *clmd, float step, float dt);
 
-void cloth_find_point_contacts(struct Scene *scene, struct Object *ob, struct ClothModifierData *clmd, float step, float dt,
+void cloth_find_point_contacts(struct Depsgraph *depsgraph, struct Object *ob, struct ClothModifierData *clmd, float step, float dt,
                                ColliderContacts **r_collider_contacts, int *r_totcolliders);
 void cloth_free_contacts(ColliderContacts *collider_contacts, int totcolliders);
 
diff --git a/source/blender/blenkernel/BKE_collision.h b/source/blender/blenkernel/BKE_collision.h
index 2392c92bd84..a488851513c 100644
--- a/source/blender/blenkernel/BKE_collision.h
+++ b/source/blender/blenkernel/BKE_collision.h
@@ -49,6 +49,7 @@ struct MFace;
 struct MVert;
 struct Object;
 struct Scene;
+struct Depsgraph;
 struct MVertTri;
 
 ////////////////////////////////////////
@@ -143,14 +144,29 @@ void collision_move_object(struct CollisionModifierData *collmd, float step, flo
 
 void collision_get_collider_velocity(float vel_old[3], float vel_new[3], struct CollisionModifierData *collmd, struct CollPair *collpair);
 
-/////////////////////////////////////////////////
-// used in effect.c
-/////////////////////////////////////////////////
 
-/* explicit control over layer mask and dupli recursion */
-struct Object **get_collisionobjects_ext(struct Scene *scene, struct Object *self, struct Collection *collection, unsigned int *numcollobj, unsigned int modifier_type, bool dupli);
+/* Collision relations for dependency graph build. */
+
+typedef struct CollisionRelation {
+	struct CollisionRelation *next, *prev;
+	struct Object *ob;
+} CollisionRelation;
+
+struct ListBase *BKE_collision_relations_create(
+        struct Depsgraph *depsgraph,
+        struct Collection *collection,
+        unsigned int modifier_type);
+void BKE_collision_relations_free(struct ListBase *relations);
+
+/* Collision object lists for physics simulation evaluation. */
 
-struct Object **get_collisionobjects(struct Scene *scene, struct Object *self, struct Collection *collection, unsigned int *numcollobj, unsigned int modifier_type);
+struct Object **BKE_collision_objects_create(
+        struct Depsgraph *depsgraph,
+        struct Object *self,
+        struct Collection *collection,
+        unsigned int *numcollobj,
+        unsigned int modifier_type);
+void BKE_collision_objects_free(struct Object **objects);
 
 typedef struct ColliderCache {
 	struct ColliderCache *next, *prev;
@@ -158,8 +174,11 @@ typedef struct ColliderCache {
 	struct CollisionModifierData *collmd;
 } ColliderCache;
 
-struct ListBase *get_collider_cache(struct Scene *scene, struct Object *self, struct Collection *collection);
-void free_collider_cache(struct ListBase **colliders);
+struct ListBase *BKE_collider_cache_create(
+        struct Depsgraph *scene,
+        struct Object *self,
+        struct Collection *collection);
+void BKE_collider_cache_free(struct ListBase **colliders);
 
 /////////////////////////////////////////////////
 
diff --git a/source/blender/blenkernel/BKE_effect.h b/source/blender/blenkernel/BKE_effect.h
index a0af34d59e6..484cf957be1 100644
--- a/source/blender/blenkernel/BKE_effect.h
+++ b/source/blender/blenkernel/BKE_effect.h
@@ -130,7 +130,6 @@ void BKE_effector_relations_free(struct ListBase *lb);
 
 struct ListBase *BKE_effectors_create(
         struct Depsgraph *depsgraph,
-        struct Scene *scene,
         struct Object *ob_src,
         struct ParticleSystem *psys_src,
         struct EffectorWeights *weights);
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index bfe59e5366d..680c6860f4c 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -350,7 +350,7 @@ static int do_init_cloth(Object *ob, ClothModifierData *clmd, Mesh *result, int
 	return 1;
 }
 
-static int do_step_cloth(struct Depsgraph *depsgraph, Scene *scene, Object *ob, ClothModifierData *clmd, Mesh *result, int framenr)
+static int do_step_cloth(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, Mesh *result, int framenr)
 {
 	ClothVertex *verts = NULL;
 	Cloth *cloth;
@@ -375,7 +375,7 @@ static int do_step_cloth(struct Depsgraph *depsgraph, Scene *scene, Object *ob,
 		mul_m4_v3(ob->obmat, verts->xconst);
 	}
 
-	effectors = BKE_effectors_create(depsgraph, scene, ob, NULL, clmd->sim_parms->effector_weights);
+	effectors = BKE_effectors_create(depsgraph, ob, NULL, clmd->sim_parms->effector_weights);
 
 	if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH )
 		cloth_update_verts ( ob, clmd, result );
@@ -391,7 +391,7 @@ static int do_step_cloth(struct Depsgraph *depsgraph, Scene *scene, Object *ob,
 	// TIMEIT_START(cloth_step)
 
 	/* call the solver. */
-	ret = BPH_cloth_solve(scene, ob, framenr, clmd, effectors);
+	ret = BPH_cloth_solve(depsgraph, ob, framenr, clmd, effectors);
 
 	// TIMEIT_END(cloth_step)
 
@@ -405,7 +405,7 @@ static int do_step_cloth(struct Depsgraph *depsgraph, Scene *scene, Object *ob,
 /************************************************
  * clothModifier_do - main simulation function
  ************************************************/
-void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh, float (*vertexCos)[3])
+void clothModifier_do(ClothModifierData *clmd, Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh, float (*vertexCos)[3])
 {
 	PointCache *cache;
 	PTCacheID pid;
@@ -492,7 +492,7 @@ void clothModifier_do(ClothModifierData *clmd, struct Depsgraph *depsgraph, Scen
 	/* do simulation */
 	BKE_ptcache_validate(cache, framenr);
 
-	if (!do_step_cloth(depsgraph, scene, ob, clmd, mesh, framenr)) {
+	if (!do_step_cloth(depsgraph, ob, clmd, mesh, framenr)) {
 		BKE_ptcache_invalidate(cache);
 	}
 	else
diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c
index b41c4633ccb..f250ffdfaeb 100644
--- a/source/blender/blenkernel/intern/collision.c
+++ b/source/blender/blenkernel/intern/collision.c
@@ -58,6 +58,10 @@
 #include "BLI_kdopbvh.h"
 #include "BKE_collision.h"
 
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_physics.h"
+#include "DEG_depsgraph_query.h"
+
 #ifdef WITH_ELTOPO
 #include "eltopo-capi.h"
 #endif
@@ -479,147 +483,161 @@ static CollPair* cloth_collision(ModifierData *md1, ModifierData *md2,
 	return collpair;
 }
 
-static void add_collision_object(Object ***objs, unsigned int *numobj, unsigned int *maxobj, Object *ob, Object *self, int level, unsigned int modifier_type)
+static void add_collision_object(ListBase *relations, Object *ob, int level, unsigned int modifier_type)
 {
 	CollisionModifierData *cmd= NULL;
 
-	if (ob == self)
-		return;
-
 	/* only get objects with collision modifier */
 	if (((modifier_type == eModifierType_Collision) && ob->pd && ob->pd->deflect) || (modifier_type != eModifierType_Collision))
 		cmd= (CollisionModifierData *)modifiers_findByType(ob, modifier_type);
 
 	if (cmd) {
-		/* extend array */
-		if (*numobj >= *maxobj) {
-			*maxobj *= 2;
-			*objs= MEM_reallocN(*objs, sizeof(Object *)*(*maxobj));
-		}
-
-		(*objs)[*numobj] = ob;
-		(*numobj)++;
+		CollisionRelation *relation = MEM_callocN(sizeof(CollisionRelation), "CollisionRelation");
+		relation->ob = ob;
+		BLI_addtail(relations, relation);
 	}
 
 	/* objects in dupli groups, one level only for now */
+	/* TODO: this doesn't really work, we are not taking into account the
+	 * dupli transforms and can get objects in the list multiple times. */
 	if (ob->dup_group && level == 0) {
 		Collection *collection= ob->dup_group;
 
 		/* add objects */
 		FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(collection, object)
 		{
-			add_collision_object(objs, numobj, maxobj, object, self, level+1, modifier_type);
+			add_collision_object(relations, object, level+1, modifier_type);
 		}
 		FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
 	}
 }
 
-// return all collision objects in scene
-// collision object will exclude self
-Object **get_collisionobjects_ext(Scene *scene, Object *self, Collection *collection, unsigned int *numcollobj, unsigned int modifier_type, bool dupli)
+/* Create list of collision relations in the collection or entire scene.
+ * This is used by the depsgraph to build relations, as well as faster
+ * lookup of colliders during evaluation. */
+ListBase *BKE_collision_relations_create(Depsgraph *depsgraph, Collection *collection, unsigned int modifier_type)
 {
-	Object **objs;
-	unsigned int numobj= 0, maxobj= 100;
-	int level = dupli ? 0 : 1;
-
-	objs= MEM_callocN(sizeof(Object *)*maxobj, "CollisionObjectsArray");
+	ListBase *relations = MEM_callocN(sizeof(ListBase), "CollisionRelation list");
+	int level = 0;
 
 	/* gather all collision objects */
 	if (collection) {
 		/* use specified collection */
 		FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(collection, object)
 		{
-			add_collision_object(&objs, &numobj, &maxobj, object, self, level, modifier_type);
+			add_collision_object(relations, object, level, modifier_type);
 		}
 		FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
 	}
 	else {
+		Scene *scene = DEG_get_input_scene(depsgraph);
 		Scene *sce_iter;
 		Base *base;
 		/* add objects in same layer in scene */
 		for (SETLOOPER(scene, sce_iter, base)) {
 			if ((base->flag & BASE_VISIBLED) != 0) {
-				add_collision_object(&objs, &numobj, &maxobj, base->ob

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list