[Bf-blender-cvs] [ddf86fb] particles_refactor: Use the mempool in RigidBodyWorld instead of allocating memory inside the Bullet CAPI.

Lukas Tönne noreply at git.blender.org
Tue Apr 22 12:06:32 CEST 2014


Commit: ddf86fb42c0396a0e6ea21e7f1583bffd56c71f0
Author: Lukas Tönne
Date:   Tue Dec 31 10:39:57 2013 +0100
https://developer.blender.org/rBddf86fb42c0396a0e6ea21e7f1583bffd56c71f0

Use the mempool in RigidBodyWorld instead of allocating memory inside
the Bullet CAPI.

NOTE: this is broken atm because to free an Object's rigid body data
the RBW needs to be passed along, which would require a Scene argument
in the blenkernel function. This will get fixed by moving ownership of
the rigid body away from object and into the RBW memory management sync
procedure.

Conflicts:
	intern/rigidbody/RBI_api.h

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

M	intern/rigidbody/RBI_api.h
M	intern/rigidbody/rb_bullet_api.cpp
M	source/blender/blenkernel/BKE_rigidbody.h
M	source/blender/blenkernel/intern/rigidbody.c

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

diff --git a/intern/rigidbody/RBI_api.h b/intern/rigidbody/RBI_api.h
index f1c2c02..9dd112c 100644
--- a/intern/rigidbody/RBI_api.h
+++ b/intern/rigidbody/RBI_api.h
@@ -123,10 +123,10 @@ void RB_world_convex_sweep_test(
 /* ............ */
 
 /* Create new RigidBody instance */
-rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const float rot[4]);
+void RB_body_init(rbRigidBody *object, rbCollisionShape *shape, const float loc[3], const float rot[4]);
 
 /* Delete the given RigidBody instance */
-void RB_body_delete(rbRigidBody *body);
+void RB_body_free(rbRigidBody *object);
 
 /* Settings ------------------------- */
 
diff --git a/intern/rigidbody/rb_bullet_api.cpp b/intern/rigidbody/rb_bullet_api.cpp
index 074d363..ea9e188 100644
--- a/intern/rigidbody/rb_bullet_api.cpp
+++ b/intern/rigidbody/rb_bullet_api.cpp
@@ -327,7 +327,7 @@ void RB_world_convex_sweep_test(
 
 /* ............ */
 
-rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const float rot[4])
+void RB_body_init(rbRigidBody *object, rbCollisionShape *shape, const float loc[3], const float rot[4])
 {
 	/* current transform */
 	btTransform trans;
@@ -337,16 +337,14 @@ rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const floa
 	/* create motionstate, which is necessary for interpolation (includes reverse playback) */
 	btDefaultMotionState *motionState = new btDefaultMotionState(trans);
 	
-	/* make rigidbody */
 	btRigidBody::btRigidBodyConstructionInfo rbInfo(1.0f, motionState, shape->cshape);
 	
-	rbRigidBody *object = new rbRigidBody(rbInfo);
+	/* make rigidbody, using placement new to initialize given memory buffer */
+	new (object) rbRigidBody(rbInfo);
 	object->body.setUserPointer(object);
-	
-	return object;
 }
 
-void RB_body_delete(rbRigidBody *object)
+void RB_body_free(rbRigidBody *object)
 {
 	btRigidBody *body = &object->body;
 	
@@ -366,7 +364,8 @@ void RB_body_delete(rbRigidBody *object)
 		body->removeConstraintRef(con);
 	}
 	
-	delete object;
+	/* only call destructor, memory management happens externally */
+	object->~rbRigidBody();
 }
 
 /* Settings ------------------------- */
diff --git a/source/blender/blenkernel/BKE_rigidbody.h b/source/blender/blenkernel/BKE_rigidbody.h
index 86be3bf..9159353 100644
--- a/source/blender/blenkernel/BKE_rigidbody.h
+++ b/source/blender/blenkernel/BKE_rigidbody.h
@@ -45,7 +45,7 @@ struct Group;
 /* Memory Management */
 
 void BKE_rigidbody_free_world(struct RigidBodyWorld *rbw);
-void BKE_rigidbody_free_object(struct Object *ob);
+void BKE_rigidbody_free_object(RigidBodyWorld *rbw, struct Object *ob);
 void BKE_rigidbody_free_constraint(struct Object *ob);
 
 /* ...... */
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index 761b727..5418e00 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -129,7 +129,7 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
 }
 
 /* Free RigidBody settings and sim instances */
-void BKE_rigidbody_free_object(Object *ob)
+void BKE_rigidbody_free_object(RigidBodyWorld *rbw, Object *ob)
 {
 	RigidBodyOb *rbo = (ob) ? ob->rigidbody_object : NULL;
 
@@ -139,7 +139,8 @@ void BKE_rigidbody_free_object(Object *ob)
 
 	/* free physics references */
 	if (rbo->physics_object) {
-		RB_body_delete(rbo->physics_object);
+		RB_body_free(rbo->physics_object);
+		BLI_mempool_free(rbw->body_pool, rbo->physics_object);
 		rbo->physics_object = NULL;
 	}
 
@@ -489,14 +490,22 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
 		RB_dworld_remove_body(rbw->physics_world, rbo->physics_object);
 	}
 	if (!rbo->physics_object || rebuild) {
-		/* remove rigid body if it already exists before creating a new one */
 		if (rbo->physics_object) {
-			RB_body_delete(rbo->physics_object);
+			/* remove rigid body if it already exists before creating a new one
+			 * no need to call BLI_mempool_free, we can reuse the memory right away
+			 */
+			RB_body_free(rbo->physics_object);
+		}
+		else {
+			/* only allocate if no rigid body exists yet,
+			 * otherwise previous memory is reused
+			 */
+			rbo->physics_object = BLI_mempool_alloc(rbw->body_pool);
 		}
 
 		mat4_to_loc_quat(loc, rot, ob->obmat);
 
-		rbo->physics_object = RB_body_new(rbo->physics_shape, loc, rot);
+		RB_body_init(rbo->physics_object, rbo->physics_shape, loc, rot);
 
 		RB_body_set_friction(rbo->physics_object, rbo->friction);
 		RB_body_set_restitution(rbo->physics_object, rbo->restitution);




More information about the Bf-blender-cvs mailing list