[Bf-blender-cvs] [743aa693270] temp-angavrilov-constraints: Surface Deform: optimize memory allocation in the evaluation code.
Alexander Gavrilov
noreply at git.blender.org
Thu Jan 7 19:49:42 CET 2021
Commit: 743aa693270f87dfe007d138291f332f49c2c726
Author: Alexander Gavrilov
Date: Thu Jan 7 20:31:06 2021 +0300
Branches: temp-angavrilov-constraints
https://developer.blender.org/rB743aa693270f87dfe007d138291f332f49c2c726
Surface Deform: optimize memory allocation in the evaluation code.
Using malloc to allocate a temporary array for each vertex,
which most commonly contains just 4 elements, is not efficient.
Checking the mode with a switch is also better.
Finally, a one line UI fix: the strength option is not bind specific.
Differential Revision: https://developer.blender.org/D10040
===================================================================
M source/blender/modifiers/intern/MOD_surfacedeform.c
===================================================================
diff --git a/source/blender/modifiers/intern/MOD_surfacedeform.c b/source/blender/modifiers/intern/MOD_surfacedeform.c
index 64fad1b370b..0fad78683eb 100644
--- a/source/blender/modifiers/intern/MOD_surfacedeform.c
+++ b/source/blender/modifiers/intern/MOD_surfacedeform.c
@@ -20,6 +20,7 @@
* \ingroup modifiers
*/
+#include "BLI_alloca.h"
#include "BLI_math.h"
#include "BLI_math_geom.h"
#include "BLI_task.h"
@@ -1218,7 +1219,16 @@ static void deformVert(void *__restrict userdata,
for (int j = 0; j < num_binds; j++) {
max_verts = MAX2(max_verts, sdbind[j].numverts);
}
- float(*coords_buffer)[3] = MEM_malloc_arrayN(max_verts, sizeof(*coords_buffer), __func__);
+
+ const bool big_buffer = max_verts > 256;
+ float(*coords_buffer)[3];
+
+ if (UNLIKELY(big_buffer)) {
+ coords_buffer = MEM_malloc_arrayN(max_verts, sizeof(*coords_buffer), __func__);
+ }
+ else {
+ coords_buffer = BLI_array_alloca(coords_buffer, max_verts);
+ }
for (int j = 0; j < num_binds; j++, sdbind++) {
for (int k = 0; k < sdbind->numverts; k++) {
@@ -1228,28 +1238,32 @@ static void deformVert(void *__restrict userdata,
normal_poly_v3(norm, coords_buffer, sdbind->numverts);
zero_v3(temp);
- /* ---------- looptri mode ---------- */
- if (sdbind->mode == MOD_SDEF_MODE_LOOPTRI) {
- madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[0]], sdbind->vert_weights[0]);
- madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[1]], sdbind->vert_weights[1]);
- madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[2]], sdbind->vert_weights[2]);
- }
- else {
+ switch (sdbind->mode) {
+ /* ---------- looptri mode ---------- */
+ case MOD_SDEF_MODE_LOOPTRI: {
+ madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[0]], sdbind->vert_weights[0]);
+ madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[1]], sdbind->vert_weights[1]);
+ madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[2]], sdbind->vert_weights[2]);
+ break;
+ }
+
/* ---------- ngon mode ---------- */
- if (sdbind->mode == MOD_SDEF_MODE_NGON) {
+ case MOD_SDEF_MODE_NGON: {
for (int k = 0; k < sdbind->numverts; k++) {
madd_v3_v3fl(temp, coords_buffer[k], sdbind->vert_weights[k]);
}
+ break;
}
/* ---------- centroid mode ---------- */
- else if (sdbind->mode == MOD_SDEF_MODE_CENTROID) {
+ case MOD_SDEF_MODE_CENTROID: {
float cent[3];
mid_v3_v3_array(cent, coords_buffer, sdbind->numverts);
madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[0]], sdbind->vert_weights[0]);
madd_v3_v3fl(temp, data->targetCos[sdbind->vert_inds[1]], sdbind->vert_weights[1]);
madd_v3_v3fl(temp, cent, sdbind->vert_weights[2]);
+ break;
}
}
@@ -1263,7 +1277,10 @@ static void deformVert(void *__restrict userdata,
/* Add the offset to start coord multiplied by the strength and weight values. */
madd_v3_v3fl(vertexCos, offset, data->strength * weight);
- MEM_freeN(coords_buffer);
+
+ if (UNLIKELY(big_buffer)) {
+ MEM_freeN(coords_buffer);
+ }
}
static void surfacedeformModifier_do(ModifierData *md,
@@ -1442,9 +1459,9 @@ static void panel_draw(const bContext *UNUSED(C), Panel *panel)
col = uiLayoutColumn(layout, false);
uiLayoutSetActive(col, !is_bound);
uiItemR(col, ptr, "target", 0, NULL, ICON_NONE);
-
uiItemR(col, ptr, "falloff", 0, NULL, ICON_NONE);
- uiItemR(col, ptr, "strength", 0, NULL, ICON_NONE);
+
+ uiItemR(layout, ptr, "strength", 0, NULL, ICON_NONE);
modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", NULL);
More information about the Bf-blender-cvs
mailing list