[Bf-blender-cvs] [196d6166e51] master: Refactor CustomData interpolation code.

Bastien Montagne noreply at git.blender.org
Thu Oct 8 18:32:07 CEST 2020


Commit: 196d6166e51dc9496a8a678c329758134988eee3
Author: Bastien Montagne
Date:   Mon Oct 5 18:50:38 2020 +0200
Branches: master
https://developer.blender.org/rB196d6166e51dc9496a8a678c329758134988eee3

Refactor CustomData interpolation code.

- Move some security checks outside of `interp` callbacks.
  Namely, that we do get interpolation weights, and have something to
  interpolate.
  Some callbacks where not checking on those anyway, safer to move that
  up into calling code.

- Cleanup usage of sub-weights, lots of interpolation callbacks wher
  actually using those completely wrong.

- Change default behavior when no weights are given to higher-level API
  functions: prevriously, each callback was responsible to handle that
  case (and one did not even do it!), they were switching to purely
  additive behavior then.
  Instead, we now default to expected simple average of source values.

Note that the only real important change here is defaulting to actual
average of source value when no inertpolation weights are given (afaik,
this only happens in Weld modifier code).

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

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

M	source/blender/blenkernel/intern/customdata.c

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

diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c
index 2a4aeb81b0c..75237d77ae2 100644
--- a/source/blender/blenkernel/intern/customdata.c
+++ b/source/blender/blenkernel/intern/customdata.c
@@ -255,15 +255,11 @@ static void layerInterp_mdeformvert(const void **sources,
   struct MDeformWeight_Link *node;
   int i, j, totweight;
 
-  if (count <= 0) {
-    return;
-  }
-
   /* build a list of unique def_nrs for dest */
   totweight = 0;
   for (i = 0; i < count; i++) {
     const MDeformVert *source = sources[i];
-    float interp_weight = weights ? weights[i] : 1.0f;
+    float interp_weight = weights[i];
 
     for (j = 0; j < source->totweight; j++) {
       MDeformWeight *dw = &source->dw[j];
@@ -424,23 +420,19 @@ static void layerInterp_tface(
   float uv[4][2] = {{0.0f}};
   const float *sub_weight;
 
-  if (count <= 0) {
-    return;
-  }
-
   sub_weight = sub_weights;
   for (i = 0; i < count; i++) {
-    float weight = weights ? weights[i] : 1;
+    const float interp_weight = weights[i];
     const MTFace *src = sources[i];
 
     for (j = 0; j < 4; j++) {
       if (sub_weights) {
         for (k = 0; k < 4; k++, sub_weight++) {
-          madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
+          madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * interp_weight);
         }
       }
       else {
-        madd_v2_v2fl(uv[j], src->uv[j], weight);
+        madd_v2_v2fl(uv[j], src->uv[j], interp_weight);
       }
     }
   }
@@ -529,23 +521,19 @@ static void layerInterp_origspace_face(
   float uv[4][2] = {{0.0f}};
   const float *sub_weight;
 
-  if (count <= 0) {
-    return;
-  }
-
   sub_weight = sub_weights;
   for (i = 0; i < count; i++) {
-    float weight = weights ? weights[i] : 1;
+    const float interp_weight = weights[i];
     const OrigSpaceFace *src = sources[i];
 
     for (j = 0; j < 4; j++) {
       if (sub_weights) {
         for (k = 0; k < 4; k++, sub_weight++) {
-          madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
+          madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * interp_weight);
         }
       }
       else {
-        madd_v2_v2fl(uv[j], src->uv[j], weight);
+        madd_v2_v2fl(uv[j], src->uv[j], interp_weight);
       }
     }
   }
@@ -690,21 +678,17 @@ static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), const void *data, int
 
   return size;
 }
-static void layerInterp_paint_mask(
-    const void **sources, const float *weights, const float *sub_weights, int count, void *dest)
+static void layerInterp_paint_mask(const void **sources,
+                                   const float *weights,
+                                   const float *UNUSED(sub_weights),
+                                   int count,
+                                   void *dest)
 {
   float mask = 0.0f;
-  const float *sub_weight = sub_weights;
   for (int i = 0; i < count; i++) {
-    float weight = weights ? weights[i] : 1.0f;
+    const float interp_weight = weights[i];
     const float *src = sources[i];
-    if (sub_weights) {
-      mask += (*src) * (*sub_weight) * weight;
-      sub_weight++;
-    }
-    else {
-      mask += (*src) * weight;
-    }
+    mask += (*src) * interp_weight;
   }
   *(float *)dest = mask;
 }
@@ -885,8 +869,11 @@ static void layerDefault_mloopcol(void *data, int count)
   }
 }
 
-static void layerInterp_mloopcol(
-    const void **sources, const float *weights, const float *sub_weights, int count, void *dest)
+static void layerInterp_mloopcol(const void **sources,
+                                 const float *weights,
+                                 const float *UNUSED(sub_weights),
+                                 int count,
+                                 void *dest)
 {
   MLoopCol *mc = dest;
   struct {
@@ -896,23 +883,13 @@ static void layerInterp_mloopcol(
     float b;
   } col = {0};
 
-  const float *sub_weight = sub_weights;
   for (int i = 0; i < count; i++) {
-    float weight = weights ? weights[i] : 1;
+    const float interp_weight = weights[i];
     const MLoopCol *src = sources[i];
-    if (sub_weights) {
-      col.r += src->r * (*sub_weight) * weight;
-      col.g += src->g * (*sub_weight) * weight;
-      col.b += src->b * (*sub_weight) * weight;
-      col.a += src->a * (*sub_weight) * weight;
-      sub_weight++;
-    }
-    else {
-      col.r += src->r * weight;
-      col.g += src->g * weight;
-      col.b += src->b * weight;
-      col.a += src->a * weight;
-    }
+    col.r += src->r * interp_weight;
+    col.g += src->g * interp_weight;
+    col.b += src->b * interp_weight;
+    col.a += src->a * interp_weight;
   }
 
   /* Subdivide smooth or fractal can cause problems without clamping
@@ -986,34 +963,23 @@ static void layerAdd_mloopuv(void *data1, const void *data2)
   add_v2_v2(l1->uv, l2->uv);
 }
 
-static void layerInterp_mloopuv(
-    const void **sources, const float *weights, const float *sub_weights, int count, void *dest)
+static void layerInterp_mloopuv(const void **sources,
+                                const float *weights,
+                                const float *UNUSED(sub_weights),
+                                int count,
+                                void *dest)
 {
   float uv[2];
   int flag = 0;
 
   zero_v2(uv);
 
-  if (sub_weights) {
-    const float *sub_weight = sub_weights;
-    for (int i = 0; i < count; i++) {
-      float weight = (weights ? weights[i] : 1.0f) * (*sub_weight);
-      const MLoopUV *src = sources[i];
-      madd_v2_v2fl(uv, src->uv, weight);
-      if (weight > 0.0f) {
-        flag |= src->flag;
-      }
-      sub_weight++;
-    }
-  }
-  else {
-    for (int i = 0; i < count; i++) {
-      float weight = weights ? weights[i] : 1;
-      const MLoopUV *src = sources[i];
-      madd_v2_v2fl(uv, src->uv, weight);
-      if (weight > 0.0f) {
-        flag |= src->flag;
-      }
+  for (int i = 0; i < count; i++) {
+    const float interp_weight = weights[i];
+    const MLoopUV *src = sources[i];
+    madd_v2_v2fl(uv, src->uv, interp_weight);
+    if (interp_weight > 0.0f) {
+      flag |= src->flag;
     }
   }
 
@@ -1088,27 +1054,19 @@ static void layerAdd_mloop_origspace(void *data1, const void *data2)
   add_v2_v2(l1->uv, l2->uv);
 }
 
-static void layerInterp_mloop_origspace(
-    const void **sources, const float *weights, const float *sub_weights, int count, void *dest)
+static void layerInterp_mloop_origspace(const void **sources,
+                                        const float *weights,
+                                        const float *UNUSED(sub_weights),
+                                        int count,
+                                        void *dest)
 {
   float uv[2];
   zero_v2(uv);
 
-  if (sub_weights) {
-    const float *sub_weight = sub_weights;
-    for (int i = 0; i < count; i++) {
-      float weight = weights ? weights[i] : 1.0f;
-      const OrigSpaceLoop *src = sources[i];
-      madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
-      sub_weight++;
-    }
-  }
-  else {
-    for (int i = 0; i < count; i++) {
-      float weight = weights ? weights[i] : 1.0f;
-      const OrigSpaceLoop *src = sources[i];
-      madd_v2_v2fl(uv, src->uv, weight);
-    }
+  for (int i = 0; i < count; i++) {
+    const float interp_weight = weights[i];
+    const OrigSpaceLoop *src = sources[i];
+    madd_v2_v2fl(uv, src->uv, interp_weight);
   }
 
   /* Delay writing to the destination in case dest is in sources. */
@@ -1127,19 +1085,15 @@ static void layerInterp_mcol(
     float b;
   } col[4] = {{0.0f}};
 
-  if (count <= 0) {
-    return;
-  }
-
   const float *sub_weight = sub_weights;
   for (int i = 0; i < count; i++) {
-    float weight = weights ? weights[i] : 1;
+    const float interp_weight = weights[i];
 
     for (int j = 0; j < 4; j++) {
       if (sub_weights) {
         const MCol *src = sources[i];
         for (int k = 0; k < 4; k++, sub_weight++, src++) {
-          const float w = (*sub_weight) * weight;
+          const float w = (*sub_weight) * interp_weight;
           col[j].a += src->a * w;
           col[j].r += src->r * w;
           col[j].g += src->g * w;
@@ -1148,10 +1102,10 @@ static void layerInterp_mcol(
       }
       else {
         const MCol *src = sources[i];
-        col[j].a += src[j].a * weight;
-        col[j].r += src[j].r * weight;
-        col[j].g += src[j].g * weight;
-        col[j].b += src[j].b * weight;
+        col[j].a += src[j].a * interp_weight;
+        col[j].r += src[j].r * interp_weight;
+        col[j].g += src[j].g * interp_weight;
+        col[j].b += src[j].b * interp_weight;
       }
     }
   }
@@ -1210,15 +1164,9 @@ static void layerInterp_bweight(const void **sources,
 
   float f = 0.0f;
 
-  if (weights) {
-    for (int i = 0; i < count; i++) {
-      f += *in[i] * weights[i];
-    }
-  }
-  else {
-    for (int i = 0; i < count; i++) {
-      f += *in[i];
-    }
+  for (int i = 0; i < count; i++) {
+    const float interp_weight = weights[i];
+    f += *in[i] * interp_weight;
   }
 
   /* Delay writing to the destination in case dest is in sources. */
@@ -1240,15 +1188,9 @@ static void layerInterp_shapekey(const void **sources,
   float co[3];
   zero_v3(co);
 
-  if (weights) {
-    for (int i = 0; i < count; i++) {
-      madd_v3_v3fl(co, in[i], weights[i]);
-    }
-  }
-  else {
-    for (int i = 0; i < count; i++) {
-      add_v3_v3(co, in[i]);
-    }
+  for (int i = 0; i < count; i++) {
+    const float interp_weight = weights[i];
+    madd_v3_v3fl(co, in[i], interp_weight);
   }
 
   /* Delay writing to the destination in case dest is in sources. */
@@ -1282,10 +1224,10 @@ static void layerInterp_mvert_skin(const void **sources,
   zero_v3(radius);
 
   for (int i = 0; i < count; i++) {
+    const float interp_weight = weights[i];
     const MVertSkin *vs_src = sources[i];
-    float w = weights ? weights[i] : 1.0f;
 
-    madd_v3_v3fl(radius, vs_src->radius, w);
+    madd_v3_v3fl(radius, vs_src->radius, interp_weight);
   }
 
   /* Delay writing to the destination in case dest is in sources. */
@@ -1415,22 +1357,18 @@ static void layerDefault_propcol(void *data, int count)
   }
 }
 
-static void layerInterp_propcol(
-    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list