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/blenkernel/intern/softbody.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/blenkernel/intern/softbody.c')
-rw-r--r-- | source/blender/blenkernel/intern/softbody.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c index afb8d5cb9f8..a426782af04 100644 --- a/source/blender/blenkernel/intern/softbody.c +++ b/source/blender/blenkernel/intern/softbody.c @@ -567,7 +567,7 @@ static void ccd_update_deflector_hash(Depsgraph *depsgraph, static int count_mesh_quads(Mesh *me) { int a, result = 0; - const MPoly *mp = me->mpoly; + const MPoly *mp = BKE_mesh_polygons(me); if (mp) { for (a = me->totpoly; a > 0; a--, mp++) { @@ -591,8 +591,8 @@ static void add_mesh_quad_diag_springs(Object *ob) nofquads = count_mesh_quads(me); if (nofquads) { - const MLoop *mloop = me->mloop; - const MPoly *mp = me->mpoly; + const MLoop *mloop = BKE_mesh_loops(me); + const MPoly *mp = BKE_mesh_polygons(me); BodySpring *bs; /* resize spring-array to hold additional quad springs */ @@ -2632,6 +2632,7 @@ static void springs_from_mesh(Object *ob) BodyPoint *bp; int a; float scale = 1.0f; + const MVert *vertices = BKE_mesh_vertices(me); sb = ob->soft; if (me && sb) { @@ -2642,7 +2643,7 @@ static void springs_from_mesh(Object *ob) if (me->totvert) { bp = ob->soft->bpoint; for (a = 0; a < me->totvert; a++, bp++) { - copy_v3_v3(bp->origS, me->mvert[a].co); + copy_v3_v3(bp->origS, vertices[a].co); mul_m4_v3(ob->obmat, bp->origS); } } @@ -2663,7 +2664,7 @@ static void mesh_to_softbody(Object *ob) { SoftBody *sb; Mesh *me = ob->data; - MEdge *medge = me->medge; + const MEdge *medge = BKE_mesh_edges(me); BodyPoint *bp; BodySpring *bs; int a, totedge; @@ -2683,10 +2684,11 @@ static void mesh_to_softbody(Object *ob) sb = ob->soft; bp = sb->bpoint; - defgroup_index = me->dvert ? (sb->vertgroup - 1) : -1; - defgroup_index_mass = me->dvert ? BKE_id_defgroup_name_index(&me->id, sb->namedVG_Mass) : -1; - defgroup_index_spring = me->dvert ? BKE_id_defgroup_name_index(&me->id, sb->namedVG_Spring_K) : - -1; + const MDeformVert *dvert = BKE_mesh_deform_verts(me); + + defgroup_index = dvert ? (sb->vertgroup - 1) : -1; + defgroup_index_mass = dvert ? BKE_id_defgroup_name_index(&me->id, sb->namedVG_Mass) : -1; + defgroup_index_spring = dvert ? BKE_id_defgroup_name_index(&me->id, sb->namedVG_Spring_K) : -1; for (a = 0; a < me->totvert; a++, bp++) { /* get scalar values needed *per vertex* from vertex group functions, @@ -2699,7 +2701,7 @@ static void mesh_to_softbody(Object *ob) BLI_assert(bp->goal == sb->defgoal); } if ((ob->softflag & OB_SB_GOAL) && (defgroup_index != -1)) { - bp->goal *= BKE_defvert_find_weight(&me->dvert[a], defgroup_index); + bp->goal *= BKE_defvert_find_weight(&dvert[a], defgroup_index); } /* to proof the concept @@ -2707,11 +2709,11 @@ static void mesh_to_softbody(Object *ob) */ if (defgroup_index_mass != -1) { - bp->mass *= BKE_defvert_find_weight(&me->dvert[a], defgroup_index_mass); + bp->mass *= BKE_defvert_find_weight(&dvert[a], defgroup_index_mass); } if (defgroup_index_spring != -1) { - bp->springweight *= BKE_defvert_find_weight(&me->dvert[a], defgroup_index_spring); + bp->springweight *= BKE_defvert_find_weight(&dvert[a], defgroup_index_spring); } } @@ -2753,19 +2755,23 @@ static void mesh_faces_to_scratch(Object *ob) MLoopTri *looptri, *lt; BodyFace *bodyface; int a; + const MVert *vertices = BKE_mesh_vertices(me); + const MPoly *polygons = BKE_mesh_polygons(me); + const MLoop *loops = BKE_mesh_loops(me); + /* Allocate and copy faces. */ sb->scratch->totface = poly_to_tri_count(me->totpoly, me->totloop); looptri = lt = MEM_mallocN(sizeof(*looptri) * sb->scratch->totface, __func__); - BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, looptri); + BKE_mesh_recalc_looptri(loops, polygons, vertices, me->totloop, me->totpoly, looptri); bodyface = sb->scratch->bodyface = MEM_mallocN(sizeof(BodyFace) * sb->scratch->totface, "SB_body_Faces"); for (a = 0; a < sb->scratch->totface; a++, lt++, bodyface++) { - bodyface->v1 = me->mloop[lt->tri[0]].v; - bodyface->v2 = me->mloop[lt->tri[1]].v; - bodyface->v3 = me->mloop[lt->tri[2]].v; + bodyface->v1 = loops[lt->tri[0]].v; + bodyface->v2 = loops[lt->tri[1]].v; + bodyface->v3 = loops[lt->tri[2]].v; zero_v3(bodyface->ext_force); bodyface->ext_force[0] = bodyface->ext_force[1] = bodyface->ext_force[2] = 0.0f; bodyface->flag = 0; |