[Bf-blender-cvs] [04d1bdbf362] sculpt-dev: Sculpt: memory fixes

Joseph Eagar noreply at git.blender.org
Wed Nov 3 06:04:26 CET 2021


Commit: 04d1bdbf362585b9087509e722e8097d9ae0c81c
Author: Joseph Eagar
Date:   Tue Nov 2 21:55:18 2021 -0700
Branches: sculpt-dev
https://developer.blender.org/rB04d1bdbf362585b9087509e722e8097d9ae0c81c

Sculpt: memory fixes

* Removed a pointer from a sculpt cloth struct.
  Due to padding this doubled the size of the
  struct.  Hopefully this will be nicer on the L3 cache.
* Fixed a nasty memory leak in the smoothing code with multires.

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

M	source/blender/blenkernel/BKE_paint.h
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_cloth.c
M	source/blender/editors/sculpt_paint/sculpt_dyntopo.c
M	source/blender/editors/sculpt_paint/sculpt_filter_mesh.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_smooth.c

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

diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 2a974c0841c..042e71c6dcd 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -302,6 +302,8 @@ typedef enum eSculptClothConstraintType {
   SCULPT_CLOTH_CONSTRAINT_PIN = 3,
 } eSculptClothConstraintType;
 
+#define CLOTH_NO_POS_PTR
+
 typedef struct SculptClothConstraint {
   signed char ctype, thread_nr;
 
@@ -328,18 +330,30 @@ typedef struct SculptClothConstraint {
 
   struct {
     int index;
-    float *co;
+#ifndef CLOTH_NO_POS_PTR
+    float *position;
+#endif
   } elems[];
 } SculptClothConstraint;
 
-#define MAKE_CONSTRAINT_STRUCT(totelem) \
-  signed char ctype, thread_nr; \
-  short node; \
-  float strength; \
-  struct { \
-    int index; \
-    float *position; \
-  } elems[totelem]
+#ifndef CLOTH_NO_POS_PTR
+#  define MAKE_CONSTRAINT_STRUCT(totelem) \
+    signed char ctype, thread_nr; \
+    short node; \
+    float strength; \
+    struct { \
+      int index; \
+      float *position; \
+    } elems[totelem]
+#else
+#  define MAKE_CONSTRAINT_STRUCT(totelem) \
+    signed char ctype, thread_nr; \
+    short node; \
+    float strength; \
+    struct { \
+      int index; \
+    } elems[totelem]
+#endif
 
 typedef struct SculptClothLengthConstraint {
   MAKE_CONSTRAINT_STRUCT(2);
@@ -375,12 +389,6 @@ typedef struct SculptClothSimulation {
    */
   int tot_constraint_tasks;
 
-  /* Position anchors for deformation brushes. These positions are modified by the brush and the
-   * final positions of the simulated vertices are updated with constraints that use these points
-   * as targets. */
-  float (*deformation_pos)[3];
-  float *deformation_strength;
-
   float mass;
   float damping;
   float softbody_strength;
@@ -392,9 +400,17 @@ typedef struct SculptClothSimulation {
   float sim_falloff;
 
   float (*acceleration)[3];
+
   float (*pos)[3];
   float (*init_pos)[3];
   float (*softbody_pos)[3];
+
+  /* Position anchors for deformation brushes. These positions are modified by the brush and the
+   * final positions of the simulated vertices are updated with constraints that use these points
+   * as targets. */
+  float (*deformation_pos)[3];
+  float *deformation_strength;
+
   float (*prev_pos)[3];
   float (*last_iteration_pos)[3];
   float (*init_normal)[3];
@@ -602,7 +618,9 @@ enum {
   SCULPT_SCL_LAYER_DISP,
   SCULPT_SCL_LAYER_STROKE_ID,
   SCULPT_SCL_ORIG_FSETS,
-  SCULPT_SCL_LAYER_MAX
+  SCULPT_SCL_SMOOTH_VEL,
+  SCULPT_SCL_SMOOTH_BDIS,
+  SCULPT_SCL_LAYER_MAX,
 };
 
 typedef struct SculptSession {
diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c
index 19ccb4b4ac9..9d8d12348e2 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -6945,6 +6945,22 @@ void SCULPT_cache_free(SculptSession *ss, StrokeCache *cache)
   MEM_freeN(cache);
 }
 
+void SCULPT_release_customlayers(SculptSession *ss, bool non_customdata_only)
+{
+  for (int i = 0; i < SCULPT_SCL_LAYER_MAX; i++) {
+    if (ss->custom_layers[i]) {
+      if (non_customdata_only && !ss->custom_layers[i]->params.simple_array) {
+        continue;
+      }
+
+      SCULPT_temp_customlayer_release(ss, ss->custom_layers[i]);
+
+      MEM_freeN(ss->custom_layers[i]);
+      ss->custom_layers[i] = NULL;
+    }
+  }
+}
+
 void SCULPT_clear_scl_pointers(SculptSession *ss)
 {
   for (int i = 0; i < SCULPT_SCL_LAYER_MAX; i++) {
diff --git a/source/blender/editors/sculpt_paint/sculpt_cloth.c b/source/blender/editors/sculpt_paint/sculpt_cloth.c
index a4fcc2c44b6..3f058df49a3 100644
--- a/source/blender/editors/sculpt_paint/sculpt_cloth.c
+++ b/source/blender/editors/sculpt_paint/sculpt_cloth.c
@@ -127,6 +127,26 @@ struct {
 #endif
 };
 
+/* clang-format off */
+enum {
+  CLOTH_POS_POS,
+  CLOTH_POS_INIT,
+  CLOTH_POS_SOFT,
+  CLOTH_POS_DEF
+};
+/* clang-format on */
+
+#ifdef CLOTH_NO_POS_PTR
+#  define PACK_POS_TYPE(index, type) ((index) | ((type) << 26))
+#  define UNPACK_POS_TYPE(index) ((index) >> 26)
+#  define UNPACK_POS_INDEX(index) ((index) & ~(3 << 26))
+#  define GET_POS_PTR(index) (&cloth_sim->pos)[UNPACK_POS_TYPE(index)][UNPACK_POS_INDEX(index)]
+//#  define GET_POS_PTR_TYPE(index, type) (&cloth_sim->pos)[type][index]
+#else
+#  define PACK_POS_TYPE(index, type) index
+#  define UNPACK_POS_INDEX(index) index
+#endif
+
 /* C port of:
    https://github.com/InteractiveComputerGraphics/PositionBasedDynamics/blob/master/PositionBasedDynamics/PositionBasedDynamics.cpp
   MIT license
@@ -254,17 +274,25 @@ static bool calc_bending_gradients(const SculptClothBendConstraint *constraint,
   return true;
 }
 #  else
-static bool calc_bending_gradients(const SculptClothBendConstraint *constraint,
+static bool calc_bending_gradients(const SculptClothSimulation *cloth_sim,
+                                   const SculptClothBendConstraint *constraint,
                                    float gradients[4][3])
 {
   // derivatives from Bridson, Simulation of Clothing with Folds and Wrinkles
   // his modes correspond to the derivatives of the bending angle arccos(n1 dot n2) with correct
   // scaling
   const float invMass0 = 1.0f, invMass1 = 1.0f, invMass2 = 1.0f, invMass3 = 1.0f;
+#    ifndef CLOTH_NO_POS_PTR
   float *p0 = constraint->elems[0].position;
   float *p1 = constraint->elems[1].position;
   float *p2 = constraint->elems[2].position;
   float *p3 = constraint->elems[3].position;
+#    else
+  float *p0 = GET_POS_PTR(constraint->elems[0].index);
+  float *p1 = GET_POS_PTR(constraint->elems[1].index);
+  float *p2 = GET_POS_PTR(constraint->elems[2].index);
+  float *p3 = GET_POS_PTR(constraint->elems[3].index);
+#    endif
 
   float *d0 = gradients[0];
   float *d1 = gradients[1];
@@ -602,17 +630,19 @@ static void cloth_brush_add_bend_constraint(SculptSession *ss,
   v3 = BKE_pbvh_table_index_to_vertex(ss->pbvh, v3i);
   v4 = BKE_pbvh_table_index_to_vertex(ss->pbvh, v4i);
 
-  bend_constraint->elems[0].index = v1i;
-  bend_constraint->elems[1].index = v2i;
-  bend_constraint->elems[2].index = v3i;
-  bend_constraint->elems[3].index = v4i;
+  bend_constraint->elems[0].index = PACK_POS_TYPE(v1i, CLOTH_POS_POS);
+  bend_constraint->elems[1].index = PACK_POS_TYPE(v2i, CLOTH_POS_POS);
+  bend_constraint->elems[2].index = PACK_POS_TYPE(v3i, CLOTH_POS_POS);
+  bend_constraint->elems[3].index = PACK_POS_TYPE(v4i, CLOTH_POS_POS);
 
   bend_constraint->node = node_index;
 
+#  ifndef CLOTH_NO_POS_PTR
   bend_constraint->elems[0].position = cloth_sim->pos[v1i];
   bend_constraint->elems[1].position = cloth_sim->pos[v2i];
   bend_constraint->elems[2].position = cloth_sim->pos[v3i];
   bend_constraint->elems[3].position = cloth_sim->pos[v4i];
+#  endif
 
   const float *co1, *co2, *co3, *co4;
 
@@ -642,12 +672,12 @@ static void cloth_brush_add_bend_constraint(SculptSession *ss,
 }
 #endif
 
-static void cloth_brush_add_length_constraint(SculptSession *ss,
-                                              SculptClothSimulation *cloth_sim,
-                                              const int node_index,
-                                              const int v1i,
-                                              const int v2i,
-                                              const bool use_persistent)
+ATTR_NO_OPT static void cloth_brush_add_length_constraint(SculptSession *ss,
+                                                          SculptClothSimulation *cloth_sim,
+                                                          const int node_index,
+                                                          const int v1i,
+                                                          const int v2i,
+                                                          const bool use_persistent)
 {
   SculptClothLengthConstraint *length_constraint = cloth_add_constraint(cloth_sim, CON_LENGTH);
   SculptVertRef v1, v2;
@@ -655,13 +685,15 @@ static void cloth_brush_add_length_constraint(SculptSession *ss,
   v1 = BKE_pbvh_table_index_to_vertex(ss->pbvh, v1i);
   v2 = BKE_pbvh_table_index_to_vertex(ss->pbvh, v2i);
 
-  length_constraint->elems[0].index = v1i;
-  length_constraint->elems[1].index = v2i;
+  length_constraint->elems[0].index = PACK_POS_TYPE(v1i, CLOTH_POS_POS);
+  length_constraint->elems[1].index = PACK_POS_TYPE(v2i, CLOTH_POS_POS);
 
   length_constraint->node = node_index;
 
+#ifndef CLOTH_NO_POS_PTR
   length_constraint->elems[0].position = cloth_sim->pos[v1i];
   length_constraint->elems[1].position = cloth_sim->pos[v2i];
+#endif
 
   length_constraint->type = SCULPT_CLOTH_CONSTRAINT_STRUCTURAL;
 
@@ -689,13 +721,15 @@ static void cloth_brush_add_softbody_constraint(SculptClothSimulation *cloth_sim
 {
   SculptClothLengthConstraint *length_constraint = cloth_add_constraint(cloth_sim, CON_LENGTH);
 
-  length_constraint->elems[0].index = v;
-  length_constraint->elems[1].index = v;
+  length_constraint->elems[0].index = PACK_POS_TYPE(v, CLOTH_POS_POS);
+  length_constraint->elems[1].index = PACK_POS_TYPE(v, CLOTH_POS_SOFT);
 
   length_constraint->node = node_index;
 
+#ifndef CLOTH_NO_POS_PTR
   length_constraint->elems[0].position = cloth_sim->pos[v];
   length_constraint->elems[1].position = cloth_sim->softbody_pos[v];
+#endif
 
   length_constraint->type = SCULPT_CLOTH_CONSTRAINT_SOFTBODY;
 
@@ -713,13 +747,15 @@ static void cloth_brush_add_pin_constraint(SculptClothSimulation *cloth_sim,
 {
   SculptClothLengthConstraint *length_constraint = cloth_add_constraint(cloth_sim, CON_LENGTH);
 
-  length_constraint->elems[0].index = v;
-  length_constraint->elems[1].index = v;
+  length_constraint->elems[0].index = PACK_POS_TYPE(v, CLOTH_POS_POS);
+  length_constraint->elems[1].index = PACK_POS_TYPE(v, CLOTH_POS_INIT);
 
   length_constraint->node = node_index;
 
+#ifndef CLOTH_NO_POS_PTR
   length_constraint->elems[0].position = cloth_sim->pos[v];
   length_constraint->elems[1].position = cloth_sim->init_pos[v];
+#endif
 
   length_constraint->type = SCULPT_CLOTH_CONSTRAINT_PIN;
 
@@ -730,22 +766,24 @@ static void cloth_brush_add_pin_constraint(SculptClothSimulation *cloth_sim,
   cloth_brush_reallocate_constraints(cloth_sim);
 }
 
-stat

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list