[Bf-blender-cvs] [0a2a006] master: Collision: skip expensive BVH update if the collider doesn't move.

Alexander Gavrilov noreply at git.blender.org
Fri Oct 7 21:57:33 CEST 2016


Commit: 0a2a006775543292786e8642b20a594771fdc81c
Author: Alexander Gavrilov
Date:   Fri Oct 7 13:27:11 2016 +0300
Branches: master
https://developer.blender.org/rB0a2a006775543292786e8642b20a594771fdc81c

Collision: skip expensive BVH update if the collider doesn't move.

Since the collision modifier cannot be disabled, it causes a constant
hit on the viewport animation playback FPS. Most of this overhead can
be automatically removed in the case when the collider is static.

The updates are only skipped when the collider was stationary during
the preceding update as well, so the state is stored in a field.
Knowing that the collider is static can also be used to disable similar
BVH updates for substeps in the actual cloth simulation code.

Differential Revision: https://developer.blender.org/D2277

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

M	source/blender/blenkernel/intern/collision.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/modifiers/intern/MOD_collision.c

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

diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c
index 35a7aaf..18ca140 100644
--- a/source/blender/blenkernel/intern/collision.c
+++ b/source/blender/blenkernel/intern/collision.c
@@ -71,6 +71,15 @@ void collision_move_object(CollisionModifierData *collmd, float step, float prev
 	float tv[3] = {0, 0, 0};
 	unsigned int i = 0;
 
+	/* the collider doesn't move this frame */
+	if (collmd->is_static) {
+		for (i = 0; i < collmd->mvert_num; i++) {
+			zero_v3(collmd->current_v[i].co);
+		}
+
+		return;
+	}
+
 	for (i = 0; i < collmd->mvert_num; i++) {
 		sub_v3_v3v3(tv, collmd->xnew[i].co, collmd->x[i].co);
 		VECADDS(collmd->current_x[i].co, collmd->x[i].co, tv, prevstep);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index d86a89a..139cb84 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5192,6 +5192,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 			collmd->time_x = collmd->time_xnew = -1000;
 			collmd->mvert_num = 0;
 			collmd->tri_num = 0;
+			collmd->is_static = false;
 			collmd->bvhtree = NULL;
 			collmd->tri = NULL;
 			
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index 0424dc9..9187b76 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -621,6 +621,9 @@ typedef struct CollisionModifierData {
 	unsigned int mvert_num;
 	unsigned int tri_num;
 	float time_x, time_xnew;    /* cfra time of modifier */
+	char is_static;             /* collider doesn't move this frame, i.e. x[].co==xnew[].co */
+	char pad[7];
+
 	struct BVHTree *bvhtree;    /* bounding volume hierarchy for this cloth object */
 } CollisionModifierData;
 
diff --git a/source/blender/modifiers/intern/MOD_collision.c b/source/blender/modifiers/intern/MOD_collision.c
index ff0c650..e7ff0a9 100644
--- a/source/blender/modifiers/intern/MOD_collision.c
+++ b/source/blender/modifiers/intern/MOD_collision.c
@@ -60,6 +60,7 @@ static void initData(ModifierData *md)
 	collmd->time_x = collmd->time_xnew = -1000;
 	collmd->mvert_num = 0;
 	collmd->tri_num = 0;
+	collmd->is_static = false;
 	collmd->bvhtree = NULL;
 }
 
@@ -87,6 +88,7 @@ static void freeData(ModifierData *md)
 		collmd->time_x = collmd->time_xnew = -1000;
 		collmd->mvert_num = 0;
 		collmd->tri_num = 0;
+		collmd->is_static = false;
 	}
 }
 
@@ -169,6 +171,7 @@ static void deformVerts(ModifierData *md, Object *ob,
 				        ob->pd->pdef_sboft);
 
 				collmd->time_x = collmd->time_xnew = current_time;
+				collmd->is_static = true;
 			}
 			else if (mvert_num == collmd->mvert_num) {
 				/* put positions to old positions */
@@ -179,14 +182,19 @@ static void deformVerts(ModifierData *md, Object *ob,
 
 				memcpy(collmd->xnew, dm->getVertArray(dm), mvert_num * sizeof(MVert));
 
+				bool is_static = true;
+
 				for (i = 0; i < mvert_num; i++) {
 					/* we save global positions */
 					mul_m4_v3(ob->obmat, collmd->xnew[i].co);
+
+					/* detect motion */
+					is_static = is_static && equals_v3v3(collmd->x[i].co, collmd->xnew[i].co);
 				}
-				
+
 				memcpy(collmd->current_xnew, collmd->x, mvert_num * sizeof(MVert));
 				memcpy(collmd->current_x, collmd->x, mvert_num * sizeof(MVert));
-				
+
 				/* check if GUI setting has changed for bvh */
 				if (collmd->bvhtree) {
 					if (ob->pd->pdef_sboft != BLI_bvhtree_get_epsilon(collmd->bvhtree)) {
@@ -206,7 +214,7 @@ static void deformVerts(ModifierData *md, Object *ob,
 					        collmd->tri, collmd->tri_num,
 					        ob->pd->pdef_sboft);
 				}
-				else {
+				else if (!collmd->is_static || !is_static) {
 					/* recalc static bounding boxes */
 					bvhtree_update_from_mvert(
 					        collmd->bvhtree,
@@ -214,7 +222,8 @@ static void deformVerts(ModifierData *md, Object *ob,
 					        collmd->tri, collmd->tri_num,
 					        true);
 				}
-				
+
+				collmd->is_static = is_static;
 				collmd->time_xnew = current_time;
 			}
 			else if (mvert_num != collmd->mvert_num) {




More information about the Bf-blender-cvs mailing list