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/io/wavefront_obj/exporter/obj_export_mesh.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/io/wavefront_obj/exporter/obj_export_mesh.cc')
-rw-r--r-- | source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc | 79 |
1 files changed, 45 insertions, 34 deletions
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc index a888c6d11a2..272afe4a843 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc @@ -188,12 +188,15 @@ void OBJMesh::ensure_mesh_edges() const void OBJMesh::calc_smooth_groups(const bool use_bitflags) { - poly_smooth_groups_ = BKE_mesh_calc_smoothgroups(export_mesh_eval_->medge, - export_mesh_eval_->totedge, - export_mesh_eval_->mpoly, - export_mesh_eval_->totpoly, - export_mesh_eval_->mloop, - export_mesh_eval_->totloop, + const Span<MEdge> edges = export_mesh_eval_->edges(); + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const Span<MLoop> loops = export_mesh_eval_->loops(); + poly_smooth_groups_ = BKE_mesh_calc_smoothgroups(edges.data(), + edges.size(), + polys.data(), + polys.size(), + loops.data(), + loops.size(), &tot_smooth_groups_, use_bitflags); } @@ -239,7 +242,8 @@ const Material *OBJMesh::get_object_material(const int16_t mat_nr) const bool OBJMesh::is_ith_poly_smooth(const int poly_index) const { - return export_mesh_eval_->mpoly[poly_index].flag & ME_SMOOTH; + const Span<MPoly> polygons = export_mesh_eval_->polygons(); + return polygons[poly_index].flag & ME_SMOOTH; } const char *OBJMesh::get_object_name() const @@ -264,7 +268,8 @@ const char *OBJMesh::get_object_material_name(const int16_t mat_nr) const float3 OBJMesh::calc_vertex_coords(const int vert_index, const float scaling_factor) const { float3 r_coords; - copy_v3_v3(r_coords, export_mesh_eval_->mvert[vert_index].co); + const Span<MVert> verts = export_mesh_eval_->vertices(); + copy_v3_v3(r_coords, verts[vert_index].co); mul_m4_v3(world_and_axes_transform_, r_coords); mul_v3_fl(r_coords, scaling_factor); return r_coords; @@ -272,8 +277,10 @@ float3 OBJMesh::calc_vertex_coords(const int vert_index, const float scaling_fac Vector<int> OBJMesh::calc_poly_vertex_indices(const int poly_index) const { - const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index]; - const MLoop *mloop = &export_mesh_eval_->mloop[mpoly.loopstart]; + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const Span<MLoop> loops = export_mesh_eval_->loops(); + const MPoly &mpoly = polys[poly_index]; + const MLoop *mloop = &loops[mpoly.loopstart]; const int totloop = mpoly.totloop; Vector<int> r_poly_vertex_indices(totloop); for (int loop_index = 0; loop_index < totloop; loop_index++) { @@ -284,9 +291,8 @@ Vector<int> OBJMesh::calc_poly_vertex_indices(const int poly_index) const void OBJMesh::store_uv_coords_and_indices() { - const MPoly *mpoly = export_mesh_eval_->mpoly; - const MLoop *mloop = export_mesh_eval_->mloop; - const int totpoly = export_mesh_eval_->totpoly; + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const Span<MLoop> loops = export_mesh_eval_->loops(); const int totvert = export_mesh_eval_->totvert; const MLoopUV *mloopuv = static_cast<const MLoopUV *>( CustomData_get_layer(&export_mesh_eval_->ldata, CD_MLOOPUV)); @@ -297,9 +303,9 @@ void OBJMesh::store_uv_coords_and_indices() const float limit[2] = {STD_UV_CONNECT_LIMIT, STD_UV_CONNECT_LIMIT}; UvVertMap *uv_vert_map = BKE_mesh_uv_vert_map_create( - mpoly, nullptr, mloop, mloopuv, totpoly, totvert, limit, false, false); + polys.data(), nullptr, loops.data(), mloopuv, polys.size(), totvert, limit, false, false); - uv_indices_.resize(totpoly); + uv_indices_.resize(polys.size()); /* At least total vertices of a mesh will be present in its texture map. So * reserve minimum space early. */ uv_coords_.reserve(totvert); @@ -311,11 +317,11 @@ void OBJMesh::store_uv_coords_and_indices() if (uv_vert->separate) { tot_uv_vertices_ += 1; } - const int vertices_in_poly = mpoly[uv_vert->poly_index].totloop; + const int vertices_in_poly = polys[uv_vert->poly_index].totloop; /* Store UV vertex coordinates. */ uv_coords_.resize(tot_uv_vertices_); - const int loopstart = mpoly[uv_vert->poly_index].loopstart; + const int loopstart = polys[uv_vert->poly_index].loopstart; Span<float> vert_uv_coords(mloopuv[loopstart + uv_vert->loop_of_poly_index].uv, 2); uv_coords_[tot_uv_vertices_ - 1] = float2(vert_uv_coords[0], vert_uv_coords[1]); @@ -341,10 +347,11 @@ Span<int> OBJMesh::calc_poly_uv_indices(const int poly_index) const float3 OBJMesh::calc_poly_normal(const int poly_index) const { float3 r_poly_normal; - const MPoly &poly = export_mesh_eval_->mpoly[poly_index]; - const MLoop &mloop = export_mesh_eval_->mloop[poly.loopstart]; - const MVert &mvert = *(export_mesh_eval_->mvert); - BKE_mesh_calc_poly_normal(&poly, &mloop, &mvert, r_poly_normal); + const Span<MVert> verts = export_mesh_eval_->vertices(); + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const Span<MLoop> loops = export_mesh_eval_->loops(); + const MPoly &poly = polys[poly_index]; + BKE_mesh_calc_poly_normal(&poly, &loops[poly.loopstart], verts.data(), r_poly_normal); mul_m3_v3(world_and_axes_normal_transform_, r_poly_normal); normalize_v3(r_poly_normal); return r_poly_normal; @@ -368,6 +375,8 @@ static float3 round_float3_to_n_digits(const float3 &v, int round_digits) void OBJMesh::store_normal_coords_and_indices() { + const Span<MPoly> polys = export_mesh_eval_->polygons(); + /* We'll round normal components to 4 digits. * This will cover up some minor differences * between floating point calculations on different platforms. @@ -383,7 +392,7 @@ void OBJMesh::store_normal_coords_and_indices() const float(*lnors)[3] = static_cast<const float(*)[3]>( CustomData_get_layer(&export_mesh_eval_->ldata, CD_NORMAL)); for (int poly_index = 0; poly_index < export_mesh_eval_->totpoly; ++poly_index) { - const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index]; + const MPoly &mpoly = polys[poly_index]; bool need_per_loop_normals = lnors != nullptr || (mpoly.flag & ME_SMOOTH); if (need_per_loop_normals) { for (int loop_of_poly = 0; loop_of_poly < mpoly.totloop; ++loop_of_poly) { @@ -427,7 +436,8 @@ Vector<int> OBJMesh::calc_poly_normal_indices(const int poly_index) const if (loop_to_normal_index_.is_empty()) { return {}; } - const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index]; + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const MPoly &mpoly = polys[poly_index]; const int totloop = mpoly.totloop; Vector<int> r_poly_normal_indices(totloop); for (int poly_loop_index = 0; poly_loop_index < totloop; poly_loop_index++) { @@ -450,23 +460,23 @@ int16_t OBJMesh::get_poly_deform_group_index(const int poly_index, { BLI_assert(poly_index < export_mesh_eval_->totpoly); BLI_assert(group_weights.size() == BKE_object_defgroup_count(&export_object_eval_)); - - const MDeformVert *dvert_layer = static_cast<const MDeformVert *>( - CustomData_get_layer(&export_mesh_eval_->vdata, CD_MDEFORMVERT)); - if (!dvert_layer) { + const Span<MPoly> polys = export_mesh_eval_->polygons(); + const Span<MLoop> loops = export_mesh_eval_->loops(); + const Span<MDeformVert> dverts = export_mesh_eval_->deform_verts(); + if (dverts.is_empty()) { return NOT_FOUND; } group_weights.fill(0); bool found_any_group = false; - const MPoly &mpoly = export_mesh_eval_->mpoly[poly_index]; - const MLoop *mloop = &export_mesh_eval_->mloop[mpoly.loopstart]; + const MPoly &mpoly = polys[poly_index]; + const MLoop *mloop = &loops[mpoly.loopstart]; for (int loop_i = 0; loop_i < mpoly.totloop; ++loop_i, ++mloop) { - const MDeformVert &dvert = dvert_layer[mloop->v]; - for (int weight_i = 0; weight_i < dvert.totweight; ++weight_i) { - const auto group = dvert.dw[weight_i].def_nr; + const MDeformVert &dv = dverts[mloop->v]; + for (int weight_i = 0; weight_i < dv.totweight; ++weight_i) { + const auto group = dv.dw[weight_i].def_nr; if (group < group_weights.size()) { - group_weights[group] += dvert.dw[weight_i].weight; + group_weights[group] += dv.dw[weight_i].weight; found_any_group = true; } } @@ -490,7 +500,8 @@ const char *OBJMesh::get_poly_deform_group_name(const int16_t def_group_index) c std::optional<std::array<int, 2>> OBJMesh::calc_loose_edge_vert_indices(const int edge_index) const { - const MEdge &edge = export_mesh_eval_->medge[edge_index]; + const Span<MEdge> edges = export_mesh_eval_->edges(); + const MEdge &edge = edges[edge_index]; if (edge.flag & ME_LOOSEEDGE) { return std::array<int, 2>{static_cast<int>(edge.v1), static_cast<int>(edge.v2)}; } |