Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2022-01-13 23:37:58 +0300
committerHans Goudey <h.goudey@me.com>2022-01-13 23:38:25 +0300
commitcfa53e0fbeed7178c7876413e2010fd3347d7f72 (patch)
treef5cdc39134a3477614ae98699f90da3722161ed0 /source/blender/modifiers
parent800fc17367480b82e07570d5234726aa61c38563 (diff)
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a contiguous array of float vectors in a custom data layer, how face normals are currently stored. The main interface is documented in `BKE_mesh.h`. Vertex and face normals are now calculated on-demand and cached, retrieved with an "ensure" function. Since the logical state of a mesh is now "has normals when necessary", they can be retrieved from a `const` mesh. The goal is to use on-demand calculation for all derived data, but leave room for eager calculation for performance purposes (modifier evaluation is threaded, but viewport data generation is not). **Benefits** This moves us closer to a SoA approach rather than the current AoS paradigm. Accessing a contiguous `float3` is much more efficient than retrieving data from a larger struct. The memory requirements for accessing only normals or vertex locations are smaller, and at the cost of more memory usage for just normals, they now don't have to be converted between float and short, which also simplifies code In the future, the remaining items can be removed from `MVert`, leaving only `float3`, which has similar benefits (see T93602). Removing the combination of derived and original data makes it conceptually simpler to only calculate normals when necessary. This is especially important now that we have more opportunities for temporary meshes in geometry nodes. **Performance** In addition to the theoretical future performance improvements by making `MVert == float3`, I've done some basic performance testing on this patch directly. The data is fairly rough, but it gives an idea about where things stand generally. - Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms), showing that accessing just `MVert` is now more efficient. - Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight change that at least shows there is no regression. - Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small but observable speedup. - Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms), shows that using normals in geometry nodes is faster. - Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms), shows that calculating normals is slightly faster now. - File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB), Normals are not saved in files, which can help with large meshes. As for memory usage, it may be slightly more in some cases, but I didn't observe any difference in the production files I tested. **Tests** Some modifiers and cycles test results need to be updated with this commit, for two reasons: - The subdivision surface modifier is not responsible for calculating normals anymore. In master, the modifier creates different normals than the result of the `Mesh` normal calculation, so this is a bug fix. - There are small differences in the results of some modifiers that use normals because they are not converted to and from `short` anymore. **Future improvements** - Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier already retrieves normals if they are needed anyway. - Copy normals as part of a better CoW system for attributes. - Make more areas use lazy instead of eager normal calculation. - Remove `BKE_mesh_normals_tag_dirty` in more places since that is now the default state of a new mesh. - Possibly apply a similar change to derived face corner normals. Differential Revision: https://developer.blender.org/D12770
Diffstat (limited to 'source/blender/modifiers')
-rw-r--r--source/blender/modifiers/intern/MOD_array.c40
-rw-r--r--source/blender/modifiers/intern/MOD_build.c4
-rw-r--r--source/blender/modifiers/intern/MOD_displace.c9
-rw-r--r--source/blender/modifiers/intern/MOD_explode.c2
-rw-r--r--source/blender/modifiers/intern/MOD_mask.cc10
-rw-r--r--source/blender/modifiers/intern/MOD_normal_edit.c47
-rw-r--r--source/blender/modifiers/intern/MOD_ocean.c2
-rw-r--r--source/blender/modifiers/intern/MOD_screw.c6
-rw-r--r--source/blender/modifiers/intern/MOD_solidify_extrude.c57
-rw-r--r--source/blender/modifiers/intern/MOD_solidify_nonmanifold.c10
-rw-r--r--source/blender/modifiers/intern/MOD_util.c4
-rw-r--r--source/blender/modifiers/intern/MOD_wave.c8
-rw-r--r--source/blender/modifiers/intern/MOD_weighted_normal.c27
13 files changed, 89 insertions, 137 deletions
diff --git a/source/blender/modifiers/intern/MOD_array.c b/source/blender/modifiers/intern/MOD_array.c
index fa5149a45ba..56db68b163c 100644
--- a/source/blender/modifiers/intern/MOD_array.c
+++ b/source/blender/modifiers/intern/MOD_array.c
@@ -306,14 +306,14 @@ static void mesh_merge_transform(Mesh *result,
mul_m4_v3(cap_offset, mv->co);
/* Reset MVert flags for caps */
mv->flag = mv->bweight = 0;
+ }
- /* We have to correct normals too, if we do not tag them as dirty later! */
- if (!recalc_normals_later) {
- float no[3];
- normal_short_to_float_v3(no, mv->no);
- mul_mat3_m4_v3(cap_offset, no);
- normalize_v3(no);
- normal_float_to_short_v3(mv->no, no);
+ /* We have to correct normals too, if we do not tag them as dirty later! */
+ if (!recalc_normals_later) {
+ float(*dst_vert_normals)[3] = BKE_mesh_vertex_normals_for_write(result);
+ for (i = 0; i < cap_nverts; i++) {
+ mul_mat3_m4_v3(cap_offset, dst_vert_normals[cap_verts_index + i]);
+ normalize_v3(dst_vert_normals[cap_verts_index + i]);
}
}
@@ -370,7 +370,7 @@ static Mesh *arrayModifier_doArray(ArrayModifierData *amd,
Mesh *mesh)
{
const MVert *src_mvert;
- MVert *mv, *mv_prev, *result_dm_verts;
+ MVert *result_dm_verts;
MEdge *me;
MLoop *ml;
@@ -582,6 +582,14 @@ static Mesh *arrayModifier_doArray(ArrayModifierData *amd,
first_chunk_nverts = chunk_nverts;
unit_m4(current_offset);
+ const float(*src_vert_normals)[3] = NULL;
+ float(*dst_vert_normals)[3] = NULL;
+ if (!use_recalc_normals) {
+ src_vert_normals = BKE_mesh_vertex_normals_ensure(mesh);
+ dst_vert_normals = BKE_mesh_vertex_normals_for_write(result);
+ BKE_mesh_vertex_normals_clear_dirty(result);
+ }
+
for (c = 1; c < count; c++) {
/* copy customdata to new geometry */
CustomData_copy_data(&mesh->vdata, &result->vdata, 0, c * chunk_nverts, chunk_nverts);
@@ -589,23 +597,21 @@ static Mesh *arrayModifier_doArray(ArrayModifierData *amd,
CustomData_copy_data(&mesh->ldata, &result->ldata, 0, c * chunk_nloops, chunk_nloops);
CustomData_copy_data(&mesh->pdata, &result->pdata, 0, c * chunk_npolys, chunk_npolys);
- mv_prev = result_dm_verts;
- mv = mv_prev + c * chunk_nverts;
+ const int vert_offset = c * chunk_nverts;
/* recalculate cumulative offset here */
mul_m4_m4m4(current_offset, current_offset, offset);
/* apply offset to all new verts */
- for (i = 0; i < chunk_nverts; i++, mv++, mv_prev++) {
- mul_m4_v3(current_offset, mv->co);
+ for (i = 0; i < chunk_nverts; i++) {
+ const int i_dst = vert_offset + i;
+ mul_m4_v3(current_offset, result_dm_verts[i_dst].co);
/* We have to correct normals too, if we do not tag them as dirty! */
if (!use_recalc_normals) {
- float no[3];
- normal_short_to_float_v3(no, mv->no);
- mul_mat3_m4_v3(current_offset, no);
- normalize_v3(no);
- normal_float_to_short_v3(mv->no, no);
+ copy_v3_v3(dst_vert_normals[i_dst], src_vert_normals[i]);
+ mul_mat3_m4_v3(current_offset, dst_vert_normals[i_dst]);
+ normalize_v3(dst_vert_normals[i_dst]);
}
}
diff --git a/source/blender/modifiers/intern/MOD_build.c b/source/blender/modifiers/intern/MOD_build.c
index 6cd8d70383d..86f0df1418b 100644
--- a/source/blender/modifiers/intern/MOD_build.c
+++ b/source/blender/modifiers/intern/MOD_build.c
@@ -280,9 +280,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct
MEM_freeN(edgeMap);
MEM_freeN(faceMap);
- if (mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL) {
- BKE_mesh_normals_tag_dirty(result);
- }
+ BKE_mesh_normals_tag_dirty(result);
/* TODO(sybren): also copy flags & tags? */
return result;
diff --git a/source/blender/modifiers/intern/MOD_displace.c b/source/blender/modifiers/intern/MOD_displace.c
index 07da18f990d..010292d2ebb 100644
--- a/source/blender/modifiers/intern/MOD_displace.c
+++ b/source/blender/modifiers/intern/MOD_displace.c
@@ -177,6 +177,7 @@ typedef struct DisplaceUserdata {
float (*vertexCos)[3];
float local_mat[4][4];
MVert *mvert;
+ const float (*vert_normals)[3];
float (*vert_clnors)[3];
} DisplaceUserdata;
@@ -194,7 +195,6 @@ static void displaceModifier_do_task(void *__restrict userdata,
bool use_global_direction = data->use_global_direction;
float(*tex_co)[3] = data->tex_co;
float(*vertexCos)[3] = data->vertexCos;
- MVert *mvert = data->mvert;
float(*vert_clnors)[3] = data->vert_clnors;
const float delta_fixed = 1.0f -
@@ -272,9 +272,7 @@ static void displaceModifier_do_task(void *__restrict userdata,
add_v3_v3(vertexCos[iter], local_vec);
break;
case MOD_DISP_DIR_NOR:
- vertexCos[iter][0] += delta * (mvert[iter].no[0] / 32767.0f);
- vertexCos[iter][1] += delta * (mvert[iter].no[1] / 32767.0f);
- vertexCos[iter][2] += delta * (mvert[iter].no[2] / 32767.0f);
+ madd_v3_v3fl(vertexCos[iter], data->vert_normals[iter], delta);
break;
case MOD_DISP_DIR_CLNOR:
madd_v3_v3fl(vertexCos[iter], vert_clnors[iter], delta);
@@ -363,6 +361,9 @@ static void displaceModifier_do(DisplaceModifierData *dmd,
data.vertexCos = vertexCos;
copy_m4_m4(data.local_mat, local_mat);
data.mvert = mvert;
+ if (direction == MOD_DISP_DIR_NOR) {
+ data.vert_normals = BKE_mesh_vertex_normals_ensure(mesh);
+ }
data.vert_clnors = vert_clnors;
if (tex_target != NULL) {
data.pool = BKE_image_pool_new();
diff --git a/source/blender/modifiers/intern/MOD_explode.c b/source/blender/modifiers/intern/MOD_explode.c
index 493b59b3a1a..68d6b4a3626 100644
--- a/source/blender/modifiers/intern/MOD_explode.c
+++ b/source/blender/modifiers/intern/MOD_explode.c
@@ -1118,7 +1118,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd,
/* finalization */
BKE_mesh_calc_edges_tessface(explode);
BKE_mesh_convert_mfaces_to_mpolys(explode);
- explode->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
+ BKE_mesh_normals_tag_dirty(explode);
if (psmd->psys->lattice_deform_data) {
BKE_lattice_deform_data_destroy(psmd->psys->lattice_deform_data);
diff --git a/source/blender/modifiers/intern/MOD_mask.cc b/source/blender/modifiers/intern/MOD_mask.cc
index 28ab25f6a42..0de8b26a1b7 100644
--- a/source/blender/modifiers/intern/MOD_mask.cc
+++ b/source/blender/modifiers/intern/MOD_mask.cc
@@ -427,16 +427,6 @@ static void add_interp_verts_copy_edges_to_new_mesh(const Mesh &src_mesh,
MVert &v2 = src_mesh.mvert[e_src.v2];
interp_v3_v3v3(v.co, v1.co, v2.co, fac);
-
- float no1[3];
- float no2[3];
- normal_short_to_float_v3(no1, v1.no);
- normal_short_to_float_v3(no2, v2.no);
- mul_v3_fl(no1, weights[0]);
- madd_v3_v3fl(no1, no2, weights[1]);
- normalize_v3(no1);
- normal_float_to_short_v3(v.no, no1);
-
vert_index++;
}
}
diff --git a/source/blender/modifiers/intern/MOD_normal_edit.c b/source/blender/modifiers/intern/MOD_normal_edit.c
index db2eedf9c02..61099fedf46 100644
--- a/source/blender/modifiers/intern/MOD_normal_edit.c
+++ b/source/blender/modifiers/intern/MOD_normal_edit.c
@@ -227,7 +227,7 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
Mesh *mesh,
short (*clnors)[2],
float (*loopnors)[3],
- float (*polynors)[3],
+ const float (*polynors)[3],
const short mix_mode,
const float mix_factor,
const float mix_limit,
@@ -334,14 +334,16 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
}
if (do_polynors_fix &&
- polygons_check_flip(mloop, nos, &mesh->ldata, mpoly, polynors, num_polys)) {
+ polygons_check_flip(
+ mloop, nos, &mesh->ldata, mpoly, BKE_mesh_poly_normals_for_write(mesh), num_polys)) {
/* XXX TODO: is this still needed? */
// mesh->dirty |= DM_DIRTY_TESS_CDLAYERS;
/* We need to recompute vertex normals! */
- BKE_mesh_calc_normals(mesh);
+ BKE_mesh_normals_tag_dirty(mesh);
}
BKE_mesh_normals_loop_custom_set(mvert,
+ BKE_mesh_vertex_normals_ensure(mesh),
num_verts,
medge,
num_edges,
@@ -349,7 +351,7 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
nos,
num_loops,
mpoly,
- (const float(*)[3])polynors,
+ polynors,
num_polys,
clnors);
@@ -364,7 +366,7 @@ static void normalEditModifier_do_directional(NormalEditModifierData *enmd,
Mesh *mesh,
short (*clnors)[2],
float (*loopnors)[3],
- float (*polynors)[3],
+ const float (*polynors)[3],
const short mix_mode,
const float mix_factor,
const float mix_limit,
@@ -449,11 +451,13 @@ static void normalEditModifier_do_directional(NormalEditModifierData *enmd,
}
if (do_polynors_fix &&
- polygons_check_flip(mloop, nos, &mesh->ldata, mpoly, polynors, num_polys)) {
+ polygons_check_flip(
+ mloop, nos, &mesh->ldata, mpoly, BKE_mesh_poly_normals_for_write(mesh), num_polys)) {
BKE_mesh_normals_tag_dirty(mesh);
}
BKE_mesh_normals_loop_custom_set(mvert,
+ BKE_mesh_vertex_normals_ensure(mesh),
num_verts,
medge,
num_edges,
@@ -461,7 +465,7 @@ static void normalEditModifier_do_directional(NormalEditModifierData *enmd,
nos,
num_loops,
mpoly,
- (const float(*)[3])polynors,
+ polynors,
num_polys,
clnors);
@@ -545,26 +549,10 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
float(*loopnors)[3] = NULL;
short(*clnors)[2] = NULL;
- float(*polynors)[3];
-
CustomData *ldata = &result->ldata;
- /* Compute poly (always needed) and vert normals. */
- CustomData *pdata = &result->pdata;
- polynors = CustomData_get_layer(pdata, CD_NORMAL);
- if (!polynors) {
- polynors = CustomData_add_layer(pdata, CD_NORMAL, CD_CALLOC, NULL, num_polys);
- CustomData_set_layer_flag(pdata, CD_NORMAL, CD_FLAG_TEMPORARY);
- }
- if (result->runtime.cd_dirty_vert & CD_MASK_NORMAL) {
- BKE_mesh_calc_normals_poly_and_vertex(
- mvert, num_verts, mloop, num_loops, mpoly, num_polys, polynors, NULL);
- }
- else {
- BKE_mesh_calc_normals_poly(mvert, num_verts, mloop, num_loops, mpoly, num_polys, polynors);
- }
-
- result->runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
+ const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh);
+ const float(*poly_normals)[3] = BKE_mesh_poly_normals_ensure(mesh);
clnors = CustomData_get_layer(ldata, CD_CUSTOMLOOPNORMAL);
if (use_current_clnors) {
@@ -572,6 +560,7 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
loopnors = MEM_malloc_arrayN((size_t)num_loops, sizeof(*loopnors), __func__);
BKE_mesh_normals_loop_split(mvert,
+ vert_normals,
num_verts,
medge,
num_edges,
@@ -579,7 +568,7 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
loopnors,
num_loops,
mpoly,
- (const float(*)[3])polynors,
+ poly_normals,
num_polys,
true,
result->smoothresh,
@@ -601,7 +590,7 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
result,
clnors,
loopnors,
- polynors,
+ poly_normals,
enmd->mix_mode,
enmd->mix_factor,
enmd->mix_limit,
@@ -624,7 +613,7 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
result,
clnors,
loopnors,
- polynors,
+ poly_normals,
enmd->mix_mode,
enmd->mix_factor,
enmd->mix_limit,
@@ -641,8 +630,6 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
num_polys);
}
- /* Currently Modifier stack assumes there is no poly normal data passed around... */
- CustomData_free_layers(pdata, CD_NORMAL, num_polys);
MEM_SAFE_FREE(loopnors);
result->runtime.is_original = false;
diff --git a/source/blender/modifiers/intern/MOD_ocean.c b/source/blender/modifiers/intern/MOD_ocean.c
index 4566cf93dd7..d821caf25a7 100644
--- a/source/blender/modifiers/intern/MOD_ocean.c
+++ b/source/blender/modifiers/intern/MOD_ocean.c
@@ -376,7 +376,7 @@ static Mesh *doOcean(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mes
if (omd->geometry_mode == MOD_OCEAN_GEOM_GENERATE) {
result = generate_ocean_geometry(omd, mesh, resolution);
- BKE_mesh_ensure_normals(result);
+ BKE_mesh_normals_tag_dirty(result);
}
else if (omd->geometry_mode == MOD_OCEAN_GEOM_DISPLACE) {
result = (Mesh *)BKE_id_copy_ex(NULL, &mesh->id, NULL, LIB_ID_COPY_LOCALIZE);
diff --git a/source/blender/modifiers/intern/MOD_screw.c b/source/blender/modifiers/intern/MOD_screw.c
index f24f6951690..f5db3bced7a 100644
--- a/source/blender/modifiers/intern/MOD_screw.c
+++ b/source/blender/modifiers/intern/MOD_screw.c
@@ -395,6 +395,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
medge_orig = mesh->medge;
mvert_new = result->mvert;
+ float(*vert_normals_new)[3] = BKE_mesh_vertex_normals_for_write(result);
+ BKE_mesh_vertex_normals_clear_dirty(result);
mpoly_new = result->mpoly;
mloop_new = result->mloop;
medge_new = result->medge;
@@ -835,7 +837,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
}
normalize_v3(vc->no);
- normal_float_to_short_v3(mvert_new[i].no, vc->no);
+ copy_v3_v3(vert_normals_new[i], vc->no);
/* Done with normals */
}
@@ -884,7 +886,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
mul_v3_m3v3(nor_tx, mat3, vert_connect[j].no);
/* set the normal now its transformed */
- normal_float_to_short_v3(mv_new->no, nor_tx);
+ copy_v3_v3(vert_normals_new[mv_new - mvert_new], nor_tx);
}
/* set location */
diff --git a/source/blender/modifiers/intern/MOD_solidify_extrude.c b/source/blender/modifiers/intern/MOD_solidify_extrude.c
index 20d65b9d9f8..fa8d08bf839 100644
--- a/source/blender/modifiers/intern/MOD_solidify_extrude.c
+++ b/source/blender/modifiers/intern/MOD_solidify_extrude.c
@@ -43,20 +43,6 @@
#endif
/* -------------------------------------------------------------------- */
-/** \name Local Utilities
- * \{ */
-
-/* specific function for solidify - define locally */
-BLI_INLINE void madd_v3v3short_fl(float r[3], const short a[3], const float f)
-{
- r[0] += (float)a[0] * f;
- r[1] += (float)a[1] * f;
- r[2] += (float)a[2] * f;
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
/** \name High Quality Normal Calculation Function
* \{ */
@@ -81,20 +67,18 @@ BLI_INLINE bool edgeref_is_init(const EdgeFaceRef *edge_ref)
* \param poly_nors: Precalculated face normals.
* \param r_vert_nors: Return vert normals.
*/
-static void mesh_calc_hq_normal(Mesh *mesh, float (*poly_nors)[3], float (*r_vert_nors)[3])
+static void mesh_calc_hq_normal(Mesh *mesh, const float (*poly_nors)[3], float (*r_vert_nors)[3])
{
int i, numVerts, numEdges, numPolys;
MPoly *mpoly, *mp;
MLoop *mloop, *ml;
MEdge *medge, *ed;
- MVert *mvert, *mv;
numVerts = mesh->totvert;
numEdges = mesh->totedge;
numPolys = mesh->totpoly;
mpoly = mesh->mpoly;
medge = mesh->medge;
- mvert = mesh->mvert;
mloop = mesh->mloop;
/* we don't want to overwrite any referenced layers */
@@ -105,7 +89,6 @@ static void mesh_calc_hq_normal(Mesh *mesh, float (*poly_nors)[3], float (*r_ver
cddm->mvert = mv;
#endif
- mv = mvert;
mp = mpoly;
{
@@ -171,9 +154,10 @@ static void mesh_calc_hq_normal(Mesh *mesh, float (*poly_nors)[3], float (*r_ver
}
/* normalize vertex normals and assign */
- for (i = 0; i < numVerts; i++, mv++) {
+ const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh);
+ for (i = 0; i < numVerts; i++) {
if (normalize_v3(r_vert_nors[i]) == 0.0f) {
- normal_short_to_float_v3(r_vert_nors[i], mv->no);
+ copy_v3_v3(r_vert_nors[i], vert_normals[i]);
}
}
}
@@ -220,7 +204,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
int *edge_order = NULL;
float(*vert_nors)[3] = NULL;
- float(*poly_nors)[3] = NULL;
+ const float(*poly_nors)[3] = NULL;
const bool need_poly_normals = (smd->flag & MOD_SOLIDIFY_NORMAL_CALC) ||
(smd->flag & MOD_SOLIDIFY_EVEN) ||
@@ -249,6 +233,8 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
/* array size is doubled in case of using a shell */
const uint stride = do_shell ? 2 : 1;
+ const float(*mesh_vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh);
+
MOD_get_vgroup(ctx->object, mesh, smd->defgrp_name, &dvert, &defgrp_index);
orig_mvert = mesh->mvert;
@@ -258,14 +244,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
if (need_poly_normals) {
/* calculate only face normals */
- poly_nors = MEM_malloc_arrayN(numPolys, sizeof(*poly_nors), __func__);
- BKE_mesh_calc_normals_poly(orig_mvert,
- (int)numVerts,
- orig_mloop,
- (int)numLoops,
- orig_mpoly,
- (int)numPolys,
- poly_nors);
+ poly_nors = BKE_mesh_poly_normals_ensure(mesh);
}
STACK_INIT(new_vert_arr, numVerts * 2);
@@ -636,7 +615,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
madd_v3_v3fl(mv->co, vert_nors[i], ofs_new_vgroup);
}
else {
- madd_v3v3short_fl(mv->co, mv->no, ofs_new_vgroup / 32767.0f);
+ madd_v3_v3fl(mv->co, mesh_vert_normals[i], ofs_new_vgroup);
}
}
}
@@ -687,7 +666,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
madd_v3_v3fl(mv->co, vert_nors[i], ofs_new_vgroup);
}
else {
- madd_v3v3short_fl(mv->co, mv->no, ofs_new_vgroup / 32767.0f);
+ madd_v3_v3fl(mv->co, mesh_vert_normals[i], ofs_new_vgroup);
}
}
}
@@ -740,7 +719,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
if (vert_nors == NULL) {
vert_nors = MEM_malloc_arrayN(numVerts, sizeof(float[3]), "mod_solid_vno");
for (i = 0, mv = mvert; i < numVerts; i++, mv++) {
- normal_short_to_float_v3(vert_nors[i], mv->no);
+ copy_v3_v3(vert_nors[i], mesh_vert_normals[i]);
}
}
@@ -995,8 +974,8 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
uint i;
/* flip vertex normals for copied verts */
mv = mvert + numVerts;
- for (i = 0; i < numVerts; i++, mv++) {
- negate_v3_short(mv->no);
+ for (i = 0; i < numVerts; i++) {
+ negate_v3((float *)mesh_vert_normals[i]);
}
}
@@ -1201,7 +1180,6 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
ed = medge + (numEdges * stride);
for (i = 0; i < rimVerts; i++, ed++, ed_orig++) {
float nor_cpy[3];
- short *nor_short;
int k;
/* NOTE: only the first vertex (lower half of the index) is calculated. */
@@ -1209,11 +1187,10 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
normalize_v3_v3(nor_cpy, edge_vert_nos[ed_orig->v1]);
for (k = 0; k < 2; k++) { /* loop over both verts of the edge */
- nor_short = mvert[*(&ed->v1 + k)].no;
- normal_short_to_float_v3(nor, nor_short);
+ copy_v3_v3(nor, mesh_vert_normals[*(&ed->v1 + k)]);
add_v3_v3(nor, nor_cpy);
normalize_v3(nor);
- normal_float_to_short_v3(nor_short, nor);
+ copy_v3_v3((float *)mesh_vert_normals[*(&ed->v1 + k)], nor);
}
}
@@ -1232,10 +1209,6 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex
MEM_freeN(old_vert_arr);
}
- if (poly_nors) {
- MEM_freeN(poly_nors);
- }
-
return result;
}
diff --git a/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c b/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c
index 997f5943060..1aa52d44509 100644
--- a/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c
+++ b/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c
@@ -158,7 +158,6 @@ Mesh *MOD_solidify_nonmanifold_modifyMesh(ModifierData *md,
const uint numVerts = (uint)mesh->totvert;
const uint numEdges = (uint)mesh->totedge;
const uint numPolys = (uint)mesh->totpoly;
- const uint numLoops = (uint)mesh->totloop;
if (numPolys == 0 && numVerts != 0) {
return mesh;
@@ -170,8 +169,6 @@ Mesh *MOD_solidify_nonmanifold_modifyMesh(ModifierData *md,
const short mat_ofs = mat_nrs > 1 ? smd->mat_ofs : 0;
const short mat_ofs_rim = mat_nrs > 1 ? smd->mat_ofs_rim : 0;
- float(*poly_nors)[3] = NULL;
-
/* #ofs_front and #ofs_back are the offset from the original
* surface along the normal, where #oft_front is along the positive
* and #oft_back is along the negative normal. */
@@ -217,10 +214,9 @@ Mesh *MOD_solidify_nonmanifold_modifyMesh(ModifierData *md,
#define MOD_SOLIDIFY_EMPTY_TAG ((uint)-1)
- /* Calculate only face normals. */
- poly_nors = MEM_malloc_arrayN(numPolys, sizeof(*poly_nors), __func__);
- BKE_mesh_calc_normals_poly(
- orig_mvert, (int)numVerts, orig_mloop, (int)numLoops, orig_mpoly, (int)numPolys, poly_nors);
+ /* Calculate only face normals. Copied because they are modified directly below. */
+ float(*poly_nors)[3] = MEM_malloc_arrayN(numPolys, sizeof(float[3]), __func__);
+ memcpy(poly_nors, BKE_mesh_poly_normals_ensure(mesh), sizeof(float[3]) * numPolys);
NewFaceRef *face_sides_arr = MEM_malloc_arrayN(
numPolys * 2, sizeof(*face_sides_arr), "face_sides_arr in solidify");
diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c
index 16ef65f7838..d04aee91c71 100644
--- a/source/blender/modifiers/intern/MOD_util.c
+++ b/source/blender/modifiers/intern/MOD_util.c
@@ -234,9 +234,11 @@ Mesh *MOD_deform_mesh_eval_get(Object *ob,
}
}
+ /* TODO: Remove this "use_normals" argument, since the caller should retrieve normals afterwards
+ * if necessary. */
if (use_normals) {
if (LIKELY(mesh)) {
- BKE_mesh_ensure_normals(mesh);
+ BKE_mesh_vertex_normals_ensure(mesh);
}
}
diff --git a/source/blender/modifiers/intern/MOD_wave.c b/source/blender/modifiers/intern/MOD_wave.c
index 03f8e3a1dfb..b7ab5dac388 100644
--- a/source/blender/modifiers/intern/MOD_wave.c
+++ b/source/blender/modifiers/intern/MOD_wave.c
@@ -163,8 +163,10 @@ static void waveModifier_do(WaveModifierData *md,
float falloff_fac = 1.0f; /* when falloff == 0.0f this stays at 1.0f */
const bool invert_group = (wmd->flag & MOD_WAVE_INVERT_VGROUP) != 0;
+ const float(*vert_normals)[3] = NULL;
if ((wmd->flag & MOD_WAVE_NORM) && (mesh != NULL)) {
mvert = mesh->mvert;
+ vert_normals = BKE_mesh_vertex_normals_ensure(mesh);
}
if (wmd->objectcenter != NULL) {
@@ -288,13 +290,13 @@ static void waveModifier_do(WaveModifierData *md,
if (mvert) {
/* move along normals */
if (wmd->flag & MOD_WAVE_NORM_X) {
- co[0] += (lifefac * amplit) * mvert[i].no[0] / 32767.0f;
+ co[0] += (lifefac * amplit) * vert_normals[i][0];
}
if (wmd->flag & MOD_WAVE_NORM_Y) {
- co[1] += (lifefac * amplit) * mvert[i].no[1] / 32767.0f;
+ co[1] += (lifefac * amplit) * vert_normals[i][1];
}
if (wmd->flag & MOD_WAVE_NORM_Z) {
- co[2] += (lifefac * amplit) * mvert[i].no[2] / 32767.0f;
+ co[2] += (lifefac * amplit) * vert_normals[i][2];
}
}
else {
diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c
index 1ee64b935b7..bfe389eb080 100644
--- a/source/blender/modifiers/intern/MOD_weighted_normal.c
+++ b/source/blender/modifiers/intern/MOD_weighted_normal.c
@@ -85,6 +85,7 @@ typedef struct WeightedNormalData {
const int numPolys;
MVert *mvert;
+ const float (*vert_normals)[3];
MEdge *medge;
MLoop *mloop;
@@ -93,7 +94,7 @@ typedef struct WeightedNormalData {
const float split_angle;
MPoly *mpoly;
- float (*polynors)[3];
+ const float (*polynors)[3];
int *poly_strength;
MDeformVert *dvert;
@@ -143,7 +144,7 @@ static void aggregate_item_normal(WeightedNormalModifierData *wnmd,
const float curr_val,
const bool use_face_influence)
{
- float(*polynors)[3] = wn_data->polynors;
+ const float(*polynors)[3] = wn_data->polynors;
MDeformVert *dvert = wn_data->dvert;
const int defgrp_index = wn_data->defgrp_index;
@@ -206,7 +207,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
int *loop_to_poly = wn_data->loop_to_poly;
MPoly *mpoly = wn_data->mpoly;
- float(*polynors)[3] = wn_data->polynors;
+ const float(*polynors)[3] = wn_data->polynors;
int *poly_strength = wn_data->poly_strength;
MDeformVert *dvert = wn_data->dvert;
@@ -234,6 +235,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
* we do not actually care about computed loop_normals for now... */
loop_normals = MEM_calloc_arrayN((size_t)numLoops, sizeof(*loop_normals), __func__);
BKE_mesh_normals_loop_split(mvert,
+ wn_data->vert_normals,
numVerts,
medge,
numEdges,
@@ -360,6 +362,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
}
BKE_mesh_normals_loop_custom_set(mvert,
+ wn_data->vert_normals,
numVerts,
medge,
numEdges,
@@ -390,6 +393,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
}
BKE_mesh_normals_loop_custom_from_vertices_set(mvert,
+ wn_data->vert_normals,
vert_normals,
numVerts,
medge,
@@ -407,6 +411,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
loop_normals = MEM_calloc_arrayN((size_t)numLoops, sizeof(*loop_normals), __func__);
BKE_mesh_normals_loop_split(mvert,
+ wn_data->vert_normals,
numVerts,
medge,
numEdges,
@@ -430,6 +435,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
}
BKE_mesh_normals_loop_custom_set(mvert,
+ wn_data->vert_normals,
numVerts,
medge,
numEdges,
@@ -609,15 +615,6 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
weight = (weight - 1) * 25;
}
- CustomData *pdata = &result->pdata;
- float(*polynors)[3] = CustomData_get_layer(pdata, CD_NORMAL);
- if (!polynors) {
- polynors = CustomData_add_layer(pdata, CD_NORMAL, CD_CALLOC, NULL, numPolys);
- CustomData_set_layer_flag(pdata, CD_NORMAL, CD_FLAG_TEMPORARY);
- }
- BKE_mesh_calc_normals_poly_and_vertex(
- mvert, numVerts, mloop, numLoops, mpoly, numPolys, polynors, NULL);
-
const float split_angle = mesh->smoothresh;
short(*clnors)[2];
CustomData *ldata = &result->ldata;
@@ -641,6 +638,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
.numPolys = numPolys,
.mvert = mvert,
+ .vert_normals = BKE_mesh_vertex_normals_ensure(result),
.medge = medge,
.mloop = mloop,
@@ -649,7 +647,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
.split_angle = split_angle,
.mpoly = mpoly,
- .polynors = polynors,
+ .polynors = BKE_mesh_poly_normals_ensure(mesh),
.poly_strength = CustomData_get_layer_named(
&result->pdata, CD_PROP_INT32, MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID),
@@ -677,9 +675,6 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *
MEM_SAFE_FREE(wn_data.mode_pair);
MEM_SAFE_FREE(wn_data.items_data);
- /* Currently Modifier stack assumes there is no poly normal data passed around... */
- CustomData_free_layers(pdata, CD_NORMAL, numPolys);
-
result->runtime.is_original = false;
return result;