[Bf-blender-cvs] [d7dc751a942] soc-2021-simulation-display: Fix memory leak in non primitive collision shape

soumya pochiraju noreply at git.blender.org
Sat Sep 11 08:31:58 CEST 2021


Commit: d7dc751a9426a5bce0c4f50b62bed47bd1da85c5
Author: soumya pochiraju
Date:   Sat Sep 11 11:57:40 2021 +0530
Branches: soc-2021-simulation-display
https://developer.blender.org/rBd7dc751a9426a5bce0c4f50b62bed47bd1da85c5

Fix memory leak in non primitive collision shape

- The previous commit didn't fix the leak as the mesh data was not being freed every draw call
- Now the mesh is generated during evaluation along with the physics shape.
- The collisions shape draw data mesh is stored in the shared struct, along with the physics references

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

M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/draw/engines/overlay/overlay_extra.c
M	source/blender/draw/intern/draw_cache.c
M	source/blender/makesdna/DNA_rigidbody_types.h

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

diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index d0e86c193dc..56caf7e1ec2 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -201,11 +201,10 @@ void BKE_rigidbody_free_object(Object *ob, RigidBodyWorld *rbw)
       rbo->shared->physics_shape = NULL;
     }
 
-    if (rbo->col_shape_draw_data) {
-        BKE_mesh_free(rbo->col_shape_draw_data);
-        BKE_id_free(NULL, rbo->col_shape_draw_data);
-       // MEM_freeN(rbo->col_shape_draw_data);
-        rbo->col_shape_draw_data = NULL;
+    if (rbo->shared->col_shape_draw_data) {
+        BKE_mesh_free(rbo->shared->col_shape_draw_data);
+        BKE_id_free(NULL, rbo->shared->col_shape_draw_data);
+        rbo->shared->col_shape_draw_data = NULL;
     }
 
     MEM_freeN(rbo->shared);
@@ -639,13 +638,19 @@ static void rigidbody_validate_sim_shape(RigidBodyWorld *rbw, Object *ob, bool r
       RB_shape_delete(rbo->shared->physics_shape);
     }
     /* Delete old debug drawing mesh data if it exists. */
-    if (rbo->col_shape_draw_data) {
-        BKE_mesh_free(rbo->col_shape_draw_data);
-        BKE_id_free(NULL, rbo->col_shape_draw_data);
-       // MEM_freeN(rbo->col_shape_draw_data);
-        rbo->col_shape_draw_data = NULL;
+    if (rbo->shared->col_shape_draw_data) {
+        BKE_mesh_free(rbo->shared->col_shape_draw_data);
+        BKE_id_free(NULL, rbo->shared->col_shape_draw_data);
+        rbo->shared->col_shape_draw_data = NULL;
     }
     rbo->shared->physics_shape = new_shape;
+
+    if(rbo->shape == RB_SHAPE_CONVEXH) {
+        BKE_rigidbody_store_convex_hull_draw_data(ob);
+    }
+    if(rbo->shape == RB_SHAPE_TRIMESH) {
+        BKE_rigidbody_store_trimesh_draw_data(ob);
+    }
   }
 }
 
@@ -1320,7 +1325,7 @@ RigidBodyOb *BKE_rigidbody_create_object(Scene *scene, Object *ob, short type)
   zero_v3(rbo->pvel);
   zero_v3(rbo->vel);
 
-  rbo->col_shape_draw_data = NULL;
+  rbo->shared->col_shape_draw_data = NULL;
 
   /* use triangle meshes for passive objects
    * use convex hulls for active objects since dynamic triangle meshes are very unstable
@@ -2620,7 +2625,7 @@ void BKE_rigidbody_store_convex_hull_draw_data(Object *ob) {
     }
     MEM_freeN(mloop_src);
     plConvexHullDelete(hull);
-    ob->rigidbody_object->col_shape_draw_data = hull_draw_data;
+    ob->rigidbody_object->shared->col_shape_draw_data = hull_draw_data;
 
 }
 
@@ -2665,7 +2670,7 @@ void BKE_rigidbody_store_trimesh_draw_data(Object *ob) {
         BKE_mesh_convert_mfaces_to_mpolys(trimesh_draw_data);
         BKE_mesh_calc_edges(trimesh_draw_data, false, false);
 
-        ob->rigidbody_object->col_shape_draw_data = trimesh_draw_data;
+        ob->rigidbody_object->shared->col_shape_draw_data = trimesh_draw_data;
 
     }
 
diff --git a/source/blender/draw/engines/overlay/overlay_extra.c b/source/blender/draw/engines/overlay/overlay_extra.c
index 9a48ac3a39f..cad9247ef17 100644
--- a/source/blender/draw/engines/overlay/overlay_extra.c
+++ b/source/blender/draw/engines/overlay/overlay_extra.c
@@ -373,20 +373,14 @@ static void OVERLAY_non_primitive_collision_shape(OVERLAY_ExtraCallBuffers *cb,
 {
 
     if(ob->rigidbody_object){
-          if(ob->rigidbody_object->col_shape_draw_data == NULL) {
-              switch(ob->rigidbody_object->shape) {
-                case RB_SHAPE_CONVEXH:
-                  BKE_rigidbody_store_convex_hull_draw_data(ob);
-                  break;
-                case RB_SHAPE_TRIMESH:
-                  BKE_rigidbody_store_trimesh_draw_data(ob);
-                  break;
-              }
-           DRW_cache_non_primitive_col_shape_store_ob(ob);
+          if(ob->rigidbody_object->shared->col_shape_draw_data == NULL) {
+
           }
-          GPUBatch *geom = DRW_cache_non_primitive_col_shape_get(ob);
-          if(geom){
-              OVERLAY_extra_wire(cb, geom, ob->obmat, color);
+          else {
+            GPUBatch *geom = DRW_cache_non_primitive_col_shape_get(ob);
+            if(geom){
+                OVERLAY_extra_wire(cb, geom, ob->obmat, color);
+            }
           }
     }
 
diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c
index 761935a187b..4d249f47a6c 100644
--- a/source/blender/draw/intern/draw_cache.c
+++ b/source/blender/draw/intern/draw_cache.c
@@ -157,19 +157,16 @@ static struct DRWShapeCache {
   GPUBatch *drw_particle_axis;
   GPUBatch *drw_gpencil_dummy_quad;
   GPUBatch *drw_sphere_lod[DRW_LOD_MAX];
-  struct GSet *non_primitive_col_shapes;
 } SHC = {NULL};
 
 void DRW_shape_cache_free(void)
 {
-  uint i = (sizeof(SHC) - sizeof(GSet*)) / sizeof(GPUBatch *);
+  uint i = sizeof(SHC) / sizeof(GPUBatch *);
   GPUBatch **batch = (GPUBatch **)&SHC;
   while (i--) {
     GPU_BATCH_DISCARD_SAFE(*batch);
     batch++;
   }
-  BLI_gset_free(SHC.non_primitive_col_shapes, (void (*)(void *key))DRW_cache_non_primitive_col_shape_free);
-  SHC.non_primitive_col_shapes = NULL;
 
 }
 
@@ -3613,42 +3610,24 @@ GPUBatch *DRW_cache_cursor_get(bool crosshair_lines)
   return *drw_cursor;
 }
 
-/* Store the object so it can be freed later. */
-void DRW_cache_non_primitive_col_shape_store_ob(Object *ob)
-{
-    if(!SHC.non_primitive_col_shapes) {
-        SHC.non_primitive_col_shapes = BLI_gset_ptr_new(__func__);
-    }
-    BLI_gset_add(SHC.non_primitive_col_shapes, ob);
-}
-
 GPUBatch *DRW_cache_non_primitive_col_shape_get(Object *ob)
 {
 
     GPUBatch *geom = NULL;
-    if(ob->rigidbody_object->col_shape_draw_data != NULL){
+    if(ob->rigidbody_object->shared->col_shape_draw_data != NULL){
         const DRWContextState *draw_ctx = DRW_context_state_get();
-        DRW_mesh_batch_cache_validate(ob->rigidbody_object->col_shape_draw_data);
+        DRW_mesh_batch_cache_validate(ob->rigidbody_object->shared->col_shape_draw_data);
 
-        geom = DRW_mesh_batch_cache_get_all_edges(ob->rigidbody_object->col_shape_draw_data);
+        geom = DRW_mesh_batch_cache_get_all_edges(ob->rigidbody_object->shared->col_shape_draw_data);
 
         struct TaskGraph *task_graph = BLI_task_graph_create();
-        DRW_mesh_batch_cache_create_requested(task_graph, ob, ob->rigidbody_object->col_shape_draw_data, draw_ctx->scene, false, false);
+        DRW_mesh_batch_cache_create_requested(task_graph, ob, ob->rigidbody_object->shared->col_shape_draw_data, draw_ctx->scene, false, false);
         BLI_task_graph_work_and_wait(task_graph);
         BLI_task_graph_free(task_graph);
     }
     return geom;
 }
 
-void DRW_cache_non_primitive_col_shape_free(Object *ob)
-{
-    if (ob->rigidbody_object->col_shape_draw_data != NULL) {
-        BKE_mesh_free(ob->rigidbody_object->col_shape_draw_data);
-        BKE_id_free(NULL, ob->rigidbody_object->col_shape_draw_data);
-        ob->rigidbody_object->col_shape_draw_data = NULL;
-    }
-}
-
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/makesdna/DNA_rigidbody_types.h b/source/blender/makesdna/DNA_rigidbody_types.h
index 76a7962ec8e..2315ffd4b7c 100644
--- a/source/blender/makesdna/DNA_rigidbody_types.h
+++ b/source/blender/makesdna/DNA_rigidbody_types.h
@@ -120,6 +120,8 @@ typedef struct RigidBodyOb_Shared {
   void *physics_object;
   /** Collision shape used by physics sim (i.e. btCollisionShape). */
   void *physics_shape;
+  /** Mesh used to store non pprimitive collision shapes for debug drawing. */
+  Mesh *col_shape_draw_data;
 } RigidBodyOb_Shared;
 
 /* RigidBodyObject (rbo)
@@ -193,8 +195,6 @@ typedef struct RigidBodyOb {
   /** Previous velocity, to calculate acceleration. */
   float pvel[3];
 
-  Mesh *col_shape_draw_data;
-
 } RigidBodyOb;
 
 /* Participation types for RigidBodyOb */



More information about the Bf-blender-cvs mailing list