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/freestyle/intern/blender_interface | |
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/freestyle/intern/blender_interface')
-rw-r--r-- | source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp | 35 | ||||
-rw-r--r-- | source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp | 26 |
2 files changed, 30 insertions, 31 deletions
diff --git a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp index f82d6f6164b..1aa3f2cc54a 100644 --- a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp +++ b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp @@ -14,6 +14,8 @@ #include <sstream> +using blender::Span; + namespace Freestyle { BlenderFileLoader::BlenderFileLoader(Render *re, ViewLayer *view_layer, Depsgraph *depsgraph) @@ -378,9 +380,12 @@ int BlenderFileLoader::testDegenerateTriangle(float v1[3], float v2[3], float v3 static bool testEdgeMark(Mesh *me, const FreestyleEdge *fed, const MLoopTri *lt, int i) { - MLoop *mloop = &me->mloop[lt->tri[i]]; - MLoop *mloop_next = &me->mloop[lt->tri[(i + 1) % 3]]; - MEdge *medge = &me->medge[mloop->e]; + const Span<MEdge> edges = me->edges(); + const Span<MLoop> loops = me->loops(); + + const MLoop *mloop = &loops[lt->tri[i]]; + const MLoop *mloop_next = &loops[lt->tri[(i + 1) % 3]]; + const MEdge *medge = &edges[mloop->e]; if (!ELEM(mloop_next->v, medge->v1, medge->v2)) { /* Not an edge in the original mesh before triangulation. */ @@ -394,10 +399,15 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) { char *name = ob->id.name + 2; + const Span<MVert> mesh_verts = me->vertices(); + const Span<MPoly> mesh_polys = me->polygons(); + const Span<MLoop> mesh_loops = me->loops(); + // Compute loop triangles int tottri = poly_to_tri_count(me->totpoly, me->totloop); MLoopTri *mlooptri = (MLoopTri *)MEM_malloc_arrayN(tottri, sizeof(*mlooptri), __func__); - BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, mlooptri); + BKE_mesh_recalc_looptri( + mesh_loops.data(), mesh_polys.data(), mesh_verts.data(), me->totloop, me->totpoly, mlooptri); // Compute loop normals BKE_mesh_calc_normals_split(me); @@ -408,9 +418,6 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) } // Get other mesh data - MVert *mvert = me->mvert; - MLoop *mloop = me->mloop; - MPoly *mpoly = me->mpoly; const FreestyleEdge *fed = (FreestyleEdge *)CustomData_get_layer(&me->edata, CD_FREESTYLE_EDGE); const FreestyleFace *ffa = (FreestyleFace *)CustomData_get_layer(&me->pdata, CD_FREESTYLE_FACE); @@ -435,9 +442,9 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) for (int a = 0; a < tottri; a++) { const MLoopTri *lt = &mlooptri[a]; - copy_v3_v3(v1, mvert[mloop[lt->tri[0]].v].co); - copy_v3_v3(v2, mvert[mloop[lt->tri[1]].v].co); - copy_v3_v3(v3, mvert[mloop[lt->tri[2]].v].co); + copy_v3_v3(v1, mesh_verts[mesh_loops[lt->tri[0]].v].co); + copy_v3_v3(v2, mesh_verts[mesh_loops[lt->tri[1]].v].co); + copy_v3_v3(v3, mesh_verts[mesh_loops[lt->tri[2]].v].co); mul_m4_v3(obmat, v1); mul_m4_v3(obmat, v2); @@ -506,12 +513,12 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) // by the near and far view planes. for (int a = 0; a < tottri; a++) { const MLoopTri *lt = &mlooptri[a]; - const MPoly *mp = &mpoly[lt->poly]; + const MPoly *mp = &mesh_polys[lt->poly]; Material *mat = BKE_object_material_get(ob, material_indices[lt->poly] + 1); - copy_v3_v3(v1, mvert[mloop[lt->tri[0]].v].co); - copy_v3_v3(v2, mvert[mloop[lt->tri[1]].v].co); - copy_v3_v3(v3, mvert[mloop[lt->tri[2]].v].co); + copy_v3_v3(v1, mesh_verts[mesh_loops[lt->tri[0]].v].co); + copy_v3_v3(v2, mesh_verts[mesh_loops[lt->tri[1]].v].co); + copy_v3_v3(v3, mesh_verts[mesh_loops[lt->tri[2]].v].co); mul_m4_v3(obmat, v1); mul_m4_v3(obmat, v2); diff --git a/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp b/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp index dd041abfd1f..0b035a08433 100644 --- a/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp +++ b/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp @@ -576,36 +576,28 @@ void BlenderStrokeRenderer::GenerateStrokeMesh(StrokeGroup *group, bool hasTex) mesh->totloop = group->totloop; mesh->totcol = group->materials.size(); - mesh->mvert = (MVert *)CustomData_add_layer( + MVert *vertices = (MVert *)CustomData_add_layer( &mesh->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, mesh->totvert); - mesh->medge = (MEdge *)CustomData_add_layer( + MEdge *edges = (MEdge *)CustomData_add_layer( &mesh->edata, CD_MEDGE, CD_SET_DEFAULT, nullptr, mesh->totedge); - mesh->mpoly = (MPoly *)CustomData_add_layer( + MPoly *polys = (MPoly *)CustomData_add_layer( &mesh->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, mesh->totpoly); - mesh->mloop = (MLoop *)CustomData_add_layer( + MLoop *loops = (MLoop *)CustomData_add_layer( &mesh->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, mesh->totloop); int *material_indices = (int *)CustomData_add_layer_named( &mesh->pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, mesh->totpoly, "material_index"); - MVert *vertices = mesh->mvert; - MEdge *edges = mesh->medge; - MPoly *polys = mesh->mpoly; - MLoop *loops = mesh->mloop; MLoopUV *loopsuv[2] = {nullptr}; if (hasTex) { // First UV layer - CustomData_add_layer_named( - &mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, uvNames[0]); + loopsuv[0] = static_cast<MLoopUV *>(CustomData_add_layer_named( + &mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, uvNames[0])); CustomData_set_layer_active(&mesh->ldata, CD_MLOOPUV, 0); - BKE_mesh_update_customdata_pointers(mesh, true); - loopsuv[0] = mesh->mloopuv; // Second UV layer - CustomData_add_layer_named( - &mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, uvNames[1]); + loopsuv[1] = static_cast<MLoopUV *>(CustomData_add_layer_named( + &mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, uvNames[1])); CustomData_set_layer_active(&mesh->ldata, CD_MLOOPUV, 1); - BKE_mesh_update_customdata_pointers(mesh, true); - loopsuv[1] = mesh->mloopuv; } // colors and transparency (the latter represented by grayscale colors) @@ -613,7 +605,7 @@ void BlenderStrokeRenderer::GenerateStrokeMesh(StrokeGroup *group, bool hasTex) &mesh->ldata, CD_PROP_BYTE_COLOR, CD_SET_DEFAULT, nullptr, mesh->totloop, "Color"); MLoopCol *transp = (MLoopCol *)CustomData_add_layer_named( &mesh->ldata, CD_PROP_BYTE_COLOR, CD_SET_DEFAULT, nullptr, mesh->totloop, "Alpha"); - mesh->mloopcol = colors; + CustomData_set_layer_active(&mesh->ldata, CD_PROP_BYTE_COLOR, 0); mesh->mat = (Material **)MEM_mallocN(sizeof(Material *) * mesh->totcol, "MaterialList"); for (const auto item : group->materials.items()) { |