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/mesh_remesh_voxel.cc | |
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/mesh_remesh_voxel.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_remesh_voxel.cc | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc index 5a4fd0d7d96..1b583b6a851 100644 --- a/source/blender/blenkernel/intern/mesh_remesh_voxel.cc +++ b/source/blender/blenkernel/intern/mesh_remesh_voxel.cc @@ -61,14 +61,15 @@ static Mesh *remesh_quadriflow(const Mesh *input_mesh, void (*update_cb)(void *, float progress, int *cancel), void *update_cb_data) { - /* Ensure that the triangulated mesh data is up to data */ + const Span<MVert> input_verts = input_mesh->vertices(); + const Span<MLoop> input_loops = input_mesh->loops(); const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(input_mesh); /* Gather the required data for export to the internal quadriflow mesh format. */ MVertTri *verttri = (MVertTri *)MEM_callocN( sizeof(*verttri) * BKE_mesh_runtime_looptri_len(input_mesh), "remesh_looptri"); BKE_mesh_runtime_verttri_from_looptri( - verttri, input_mesh->mloop, looptri, BKE_mesh_runtime_looptri_len(input_mesh)); + verttri, input_loops.data(), looptri, BKE_mesh_runtime_looptri_len(input_mesh)); const int totfaces = BKE_mesh_runtime_looptri_len(input_mesh); const int totverts = input_mesh->totvert; @@ -76,7 +77,7 @@ static Mesh *remesh_quadriflow(const Mesh *input_mesh, Array<int> faces(totfaces * 3); for (const int i : IndexRange(totverts)) { - verts[i] = input_mesh->mvert[i].co; + verts[i] = input_verts[i].co; } for (const int i : IndexRange(totfaces)) { @@ -123,20 +124,23 @@ static Mesh *remesh_quadriflow(const Mesh *input_mesh, /* Construct the new output mesh */ Mesh *mesh = BKE_mesh_new_nomain(qrd.out_totverts, 0, 0, qrd.out_totfaces * 4, qrd.out_totfaces); + MutableSpan<MVert> mesh_verts = mesh->vertices_for_write(); + MutableSpan<MPoly> polys = mesh->polygons_for_write(); + MutableSpan<MLoop> loops = mesh->loops_for_write(); for (const int i : IndexRange(qrd.out_totverts)) { - copy_v3_v3(mesh->mvert[i].co, &qrd.out_verts[i * 3]); + copy_v3_v3(mesh_verts[i].co, &qrd.out_verts[i * 3]); } for (const int i : IndexRange(qrd.out_totfaces)) { - MPoly &poly = mesh->mpoly[i]; + MPoly &poly = polys[i]; const int loopstart = i * 4; poly.loopstart = loopstart; poly.totloop = 4; - mesh->mloop[loopstart].v = qrd.out_faces[loopstart]; - mesh->mloop[loopstart + 1].v = qrd.out_faces[loopstart + 1]; - mesh->mloop[loopstart + 2].v = qrd.out_faces[loopstart + 2]; - mesh->mloop[loopstart + 3].v = qrd.out_faces[loopstart + 3]; + loops[loopstart].v = qrd.out_faces[loopstart]; + loops[loopstart + 1].v = qrd.out_faces[loopstart + 1]; + loops[loopstart + 2].v = qrd.out_faces[loopstart + 2]; + loops[loopstart + 3].v = qrd.out_faces[loopstart + 3]; } BKE_mesh_calc_edges(mesh, false, false); @@ -186,7 +190,8 @@ Mesh *BKE_mesh_remesh_quadriflow(const Mesh *mesh, static openvdb::FloatGrid::Ptr remesh_voxel_level_set_create(const Mesh *mesh, const float voxel_size) { - Span<MLoop> mloop{mesh->mloop, mesh->totloop}; + const Span<MVert> verts = mesh->vertices(); + const Span<MLoop> loops = mesh->loops(); Span<MLoopTri> looptris{BKE_mesh_runtime_looptri_ensure(mesh), BKE_mesh_runtime_looptri_len(mesh)}; @@ -194,14 +199,14 @@ static openvdb::FloatGrid::Ptr remesh_voxel_level_set_create(const Mesh *mesh, std::vector<openvdb::Vec3I> triangles(looptris.size()); for (const int i : IndexRange(mesh->totvert)) { - const float3 co = mesh->mvert[i].co; + const float3 co = verts[i].co; points[i] = openvdb::Vec3s(co.x, co.y, co.z); } for (const int i : IndexRange(looptris.size())) { const MLoopTri &loop_tri = looptris[i]; triangles[i] = openvdb::Vec3I( - mloop[loop_tri.tri[0]].v, mloop[loop_tri.tri[1]].v, mloop[loop_tri.tri[2]].v); + loops[loop_tri.tri[0]].v, loops[loop_tri.tri[1]].v, loops[loop_tri.tri[2]].v); } openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform( @@ -225,34 +230,34 @@ static Mesh *remesh_voxel_volume_to_mesh(const openvdb::FloatGrid::Ptr level_set Mesh *mesh = BKE_mesh_new_nomain( vertices.size(), 0, 0, quads.size() * 4 + tris.size() * 3, quads.size() + tris.size()); - MutableSpan<MVert> mverts{mesh->mvert, mesh->totvert}; - MutableSpan<MLoop> mloops{mesh->mloop, mesh->totloop}; - MutableSpan<MPoly> mpolys{mesh->mpoly, mesh->totpoly}; + MutableSpan<MVert> mesh_verts = mesh->vertices_for_write(); + MutableSpan<MPoly> mesh_polys = mesh->polygons_for_write(); + MutableSpan<MLoop> mesh_loops = mesh->loops_for_write(); - for (const int i : mverts.index_range()) { - copy_v3_v3(mverts[i].co, float3(vertices[i].x(), vertices[i].y(), vertices[i].z())); + for (const int i : mesh_verts.index_range()) { + copy_v3_v3(mesh_verts[i].co, float3(vertices[i].x(), vertices[i].y(), vertices[i].z())); } for (const int i : IndexRange(quads.size())) { - MPoly &poly = mpolys[i]; + MPoly &poly = mesh_polys[i]; const int loopstart = i * 4; poly.loopstart = loopstart; poly.totloop = 4; - mloops[loopstart].v = quads[i][0]; - mloops[loopstart + 1].v = quads[i][3]; - mloops[loopstart + 2].v = quads[i][2]; - mloops[loopstart + 3].v = quads[i][1]; + mesh_loops[loopstart].v = quads[i][0]; + mesh_loops[loopstart + 1].v = quads[i][3]; + mesh_loops[loopstart + 2].v = quads[i][2]; + mesh_loops[loopstart + 3].v = quads[i][1]; } const int triangle_loop_start = quads.size() * 4; for (const int i : IndexRange(tris.size())) { - MPoly &poly = mpolys[quads.size() + i]; + MPoly &poly = mesh_polys[quads.size() + i]; const int loopstart = triangle_loop_start + i * 3; poly.loopstart = loopstart; poly.totloop = 3; - mloops[loopstart].v = tris[i][2]; - mloops[loopstart + 1].v = tris[i][1]; - mloops[loopstart + 2].v = tris[i][0]; + mesh_loops[loopstart].v = tris[i][2]; + mesh_loops[loopstart + 1].v = tris[i][1]; + mesh_loops[loopstart + 2].v = tris[i][0]; } BKE_mesh_calc_edges(mesh, false, false); |