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/cloth.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/cloth.c')
-rw-r--r-- | source/blender/blenkernel/intern/cloth.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index 8622174231c..ef3d71cdacf 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -97,7 +97,7 @@ static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon) } } else { - MEdge *edges = cloth->edges; + const MEdge *edges = cloth->edges; for (int i = 0; i < cloth->primitive_num; i++) { float co[2][3]; @@ -177,7 +177,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self) } else { if (verts) { - MEdge *edges = cloth->edges; + const MEdge *edges = cloth->edges; for (i = 0; i < cloth->primitive_num; i++) { float co[2][3]; @@ -258,7 +258,7 @@ static int do_step_cloth( cloth = clmd->clothObject; verts = cloth->verts; - mvert = result->mvert; + mvert = BKE_mesh_vertices_for_write(result); vert_mass_changed = verts->mass != clmd->sim_parms->mass; /* force any pinned verts to their constrained location. */ @@ -713,7 +713,6 @@ static bool cloth_from_object( Object *ob, ClothModifierData *clmd, Mesh *mesh, float UNUSED(framenr), int first) { int i = 0; - MVert *mvert = NULL; ClothVertex *verts = NULL; const float(*shapekey_rest)[3] = NULL; const float tnull[3] = {0, 0, 0}; @@ -755,7 +754,7 @@ static bool cloth_from_object( shapekey_rest = CustomData_get_layer(&mesh->vdata, CD_CLOTH_ORCO); } - mvert = mesh->mvert; + MVert *mvert = BKE_mesh_vertices_for_write(mesh); verts = clmd->clothObject->verts; @@ -824,7 +823,7 @@ static bool cloth_from_object( static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh *mesh) { - const MLoop *mloop = mesh->mloop; + const MLoop *mloop = BKE_mesh_loops(mesh); const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh); const unsigned int mvert_num = mesh->totvert; const unsigned int looptri_num = mesh->runtime.looptris.len; @@ -859,7 +858,7 @@ static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh *mes } BKE_mesh_runtime_verttri_from_looptri(clmd->clothObject->tri, mloop, looptri, looptri_num); - clmd->clothObject->edges = mesh->medge; + clmd->clothObject->edges = BKE_mesh_edges(mesh); /* Free the springs since they can't be correct if the vertices * changed. @@ -1150,7 +1149,7 @@ static void cloth_update_springs(ClothModifierData *clmd) static void cloth_update_verts(Object *ob, ClothModifierData *clmd, Mesh *mesh) { unsigned int i = 0; - MVert *mvert = mesh->mvert; + const MVert *mvert = BKE_mesh_vertices(mesh); ClothVertex *verts = clmd->clothObject->verts; /* vertex count is already ensured to match */ @@ -1165,7 +1164,7 @@ static Mesh *cloth_make_rest_mesh(ClothModifierData *clmd, Mesh *mesh) { Mesh *new_mesh = BKE_mesh_copy_for_eval(mesh, false); ClothVertex *verts = clmd->clothObject->verts; - MVert *mvert = new_mesh->mvert; + MVert *mvert = BKE_mesh_vertices_for_write(mesh); /* vertex count is already ensured to match */ for (unsigned i = 0; i < mesh->totvert; i++, verts++) { @@ -1459,9 +1458,9 @@ static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh) unsigned int numedges = (unsigned int)mesh->totedge; unsigned int numpolys = (unsigned int)mesh->totpoly; float shrink_factor; - const MEdge *medge = mesh->medge; - const MPoly *mpoly = mesh->mpoly; - const MLoop *mloop = mesh->mloop; + const MEdge *medge = BKE_mesh_edges(mesh); + const MPoly *mpoly = BKE_mesh_polygons(mesh); + const MLoop *mloop = BKE_mesh_loops(mesh); int index2 = 0; /* our second vertex index */ LinkNodePair *edgelist = NULL; EdgeSet *edgeset = NULL; |