diff options
author | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
commit | 05952aa94d33eeb504fa63618ba35c2bcc8bd19b (patch) | |
tree | c9ec37adf20c3c37ccaab44869220dcbe8e987a3 /source/blender/modifiers/intern/MOD_weighted_normal.c | |
parent | 63cfc8f9f6d623f33b50c5c07976af2b22845713 (diff) |
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
Diffstat (limited to 'source/blender/modifiers/intern/MOD_weighted_normal.c')
-rw-r--r-- | source/blender/modifiers/intern/MOD_weighted_normal.c | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c index 5b5d464a710..c6c0ce58463 100644 --- a/source/blender/modifiers/intern/MOD_weighted_normal.c +++ b/source/blender/modifiers/intern/MOD_weighted_normal.c @@ -71,20 +71,20 @@ typedef struct WeightedNormalData { const int loops_num; const int polys_num; - MVert *mvert; + const MVert *mvert; const float (*vert_normals)[3]; MEdge *medge; - MLoop *mloop; + const MLoop *mloop; short (*clnors)[2]; const bool has_clnors; /* True if clnors already existed, false if we had to create them. */ const float split_angle; - MPoly *mpoly; + const MPoly *mpoly; const float (*polynors)[3]; const int *poly_strength; - MDeformVert *dvert; + const MDeformVert *dvert; const int defgrp_index; const bool use_invert_vgroup; @@ -133,7 +133,7 @@ static void aggregate_item_normal(WeightedNormalModifierData *wnmd, { const float(*polynors)[3] = wn_data->polynors; - MDeformVert *dvert = wn_data->dvert; + const MDeformVert *dvert = wn_data->dvert; const int defgrp_index = wn_data->defgrp_index; const bool use_invert_vgroup = wn_data->use_invert_vgroup; @@ -186,18 +186,18 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd, const int loops_num = wn_data->loops_num; const int polys_num = wn_data->polys_num; - MVert *mvert = wn_data->mvert; + const MVert *mvert = wn_data->mvert; MEdge *medge = wn_data->medge; - MLoop *mloop = wn_data->mloop; + const MLoop *mloop = wn_data->mloop; short(*clnors)[2] = wn_data->clnors; int *loop_to_poly = wn_data->loop_to_poly; - MPoly *mpoly = wn_data->mpoly; + const MPoly *mpoly = wn_data->mpoly; const float(*polynors)[3] = wn_data->polynors; const int *poly_strength = wn_data->poly_strength; - MDeformVert *dvert = wn_data->dvert; + const MDeformVert *dvert = wn_data->dvert; const short mode = wn_data->mode; ModePair *mode_pair = wn_data->mode_pair; @@ -243,7 +243,7 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd, /* In this first loop, we assign each WeightedNormalDataAggregateItem * to its smooth fan of loops (aka lnor space). */ - MPoly *mp; + const MPoly *mp; int mp_index; int item_index; for (mp = mpoly, mp_index = 0, item_index = 0; mp_index < polys_num; mp++, mp_index++) { @@ -446,11 +446,11 @@ static void wn_face_area(WeightedNormalModifierData *wnmd, WeightedNormalData *w { const int polys_num = wn_data->polys_num; - MVert *mvert = wn_data->mvert; - MLoop *mloop = wn_data->mloop; - MPoly *mpoly = wn_data->mpoly; + const MVert *mvert = wn_data->mvert; + const MLoop *mloop = wn_data->mloop; + const MPoly *mpoly = wn_data->mpoly; - MPoly *mp; + const MPoly *mp; int mp_index; ModePair *face_area = MEM_malloc_arrayN((size_t)polys_num, sizeof(*face_area), __func__); @@ -472,11 +472,11 @@ static void wn_corner_angle(WeightedNormalModifierData *wnmd, WeightedNormalData const int loops_num = wn_data->loops_num; const int polys_num = wn_data->polys_num; - MVert *mvert = wn_data->mvert; - MLoop *mloop = wn_data->mloop; - MPoly *mpoly = wn_data->mpoly; + const MVert *mvert = wn_data->mvert; + const MLoop *mloop = wn_data->mloop; + const MPoly *mpoly = wn_data->mpoly; - MPoly *mp; + const MPoly *mp; int mp_index; int *loop_to_poly = MEM_malloc_arrayN((size_t)loops_num, sizeof(*loop_to_poly), __func__); @@ -484,7 +484,7 @@ static void wn_corner_angle(WeightedNormalModifierData *wnmd, WeightedNormalData ModePair *corner_angle = MEM_malloc_arrayN((size_t)loops_num, sizeof(*corner_angle), __func__); for (mp_index = 0, mp = mpoly; mp_index < polys_num; mp_index++, mp++) { - MLoop *ml_start = &mloop[mp->loopstart]; + const MLoop *ml_start = &mloop[mp->loopstart]; float *index_angle = MEM_malloc_arrayN((size_t)mp->totloop, sizeof(*index_angle), __func__); BKE_mesh_calc_poly_angles(mp, ml_start, mvert, index_angle); @@ -513,11 +513,11 @@ static void wn_face_with_angle(WeightedNormalModifierData *wnmd, WeightedNormalD const int loops_num = wn_data->loops_num; const int polys_num = wn_data->polys_num; - MVert *mvert = wn_data->mvert; - MLoop *mloop = wn_data->mloop; - MPoly *mpoly = wn_data->mpoly; + const MVert *mvert = wn_data->mvert; + const MLoop *mloop = wn_data->mloop; + const MPoly *mpoly = wn_data->mpoly; - MPoly *mp; + const MPoly *mp; int mp_index; int *loop_to_poly = MEM_malloc_arrayN((size_t)loops_num, sizeof(*loop_to_poly), __func__); @@ -525,7 +525,7 @@ static void wn_face_with_angle(WeightedNormalModifierData *wnmd, WeightedNormalD ModePair *combined = MEM_malloc_arrayN((size_t)loops_num, sizeof(*combined), __func__); for (mp_index = 0, mp = mpoly; mp_index < polys_num; mp_index++, mp++) { - MLoop *ml_start = &mloop[mp->loopstart]; + const MLoop *ml_start = &mloop[mp->loopstart]; float face_area = BKE_mesh_calc_poly_area(mp, ml_start, mvert); float *index_angle = MEM_malloc_arrayN((size_t)mp->totloop, sizeof(*index_angle), __func__); @@ -579,11 +579,10 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * const int edges_num = result->totedge; const int loops_num = result->totloop; const int polys_num = result->totpoly; - - MEdge *medge = result->medge; - MPoly *mpoly = result->mpoly; - MVert *mvert = result->mvert; - MLoop *mloop = result->mloop; + const MVert *mvert = BKE_mesh_vertices(result); + MEdge *medge = BKE_mesh_edges_for_write(result); + const MPoly *mpoly = BKE_mesh_polygons(result); + const MLoop *mloop = BKE_mesh_loops(result); /* Right now: * If weight = 50 then all faces are given equal weight. @@ -613,7 +612,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * &result->ldata, CD_CUSTOMLOOPNORMAL, CD_SET_DEFAULT, NULL, loops_num); } - MDeformVert *dvert; + const MDeformVert *dvert; int defgrp_index; MOD_get_vgroup(ctx->object, mesh, wnmd->defgrp_name, &dvert, &defgrp_index); |