[Bf-blender-cvs] [54f15a66a07] soc-2020-io-performance: Move uv_indices in OBJMesh. Save some memory.

Ankit Meel noreply at git.blender.org
Tue Sep 1 15:21:16 CEST 2020


Commit: 54f15a66a07a68c7db210ade79151c6262321e57
Author: Ankit Meel
Date:   Tue Sep 1 17:49:04 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB54f15a66a07a68c7db210ade79151c6262321e57

Move uv_indices in OBJMesh. Save some memory.

Since OBJMesh has dynamically allocated space in smooth groups
and uv_indices (which can be a lot), destruct objects that have
been written.

Use `const` at some places.

===================================================================

M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
M	source/blender/io/wavefront_obj/intern/obj_exporter.cc

===================================================================

diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
index dd14daf600d..e7855b7556d 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
@@ -23,8 +23,6 @@
 
 #include "BKE_blender_version.h"
 
-#include "BLI_array.hh"
-
 #include "obj_export_file_writer.hh"
 #include "obj_export_mesh.hh"
 #include "obj_export_mtl.hh"
@@ -177,11 +175,11 @@ void OBJWriter::write_vertex_coords(const OBJMesh &obj_mesh_data) const
  * Write UV vertex coordinates for all vertices as vt u v .
  * \note UV indices are stored here, but written later.
  */
-void OBJWriter::write_uv_coords(OBJMesh &obj_mesh_data, Vector<Vector<uint>> &uv_indices) const
+void OBJWriter::write_uv_coords(OBJMesh &obj_mesh_data) const
 {
   Vector<std::array<float, 2>> uv_coords;
+  obj_mesh_data.store_uv_coords_and_indices(uv_coords);
 
-  obj_mesh_data.store_uv_coords_and_indices(uv_coords, uv_indices);
   for (const std::array<float, 2> &uv_vertex : uv_coords) {
     fprintf(outfile_, "vt %f %f\n", uv_vertex[0], uv_vertex[1]);
   }
@@ -290,7 +288,7 @@ void OBJWriter::write_vertex_group(const OBJMesh &obj_mesh_data,
  * indices and face normal indices. Also write groups: smooth, vertex, material.
  *  \note UV indices are stored while writing UV vertices.
  */
-void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data, Span<Vector<uint>> uv_indices)
+void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data)
 {
   /* -1 has no significant value, it can be any negative number. */
   int last_face_smooth_group = -1;
@@ -353,7 +351,7 @@ void OBJWriter::write_poly_elements(const OBJMesh &obj_mesh_data, Span<Vector<ui
     write_smooth_group(obj_mesh_data, i, last_face_smooth_group);
     write_vertex_group(obj_mesh_data, i, last_face_vertex_group);
     write_poly_material(obj_mesh_data, i, last_face_mat_nr);
-    (this->*func_vert_uv_normal_indices)(vertex_indices, uv_indices[i], normal_indices, totloop);
+    (this->*func_vert_uv_normal_indices)(vertex_indices, obj_mesh_data.uv_indices(i), normal_indices, totloop);
   }
 }
 
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
index 693e80ecc5a..296dd97374b 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
@@ -71,7 +71,7 @@ class OBJWriter {
   void write_object_name(const OBJMesh &obj_mesh_data) const;
   void write_mtllib(const char *obj_filepath) const;
   void write_vertex_coords(const OBJMesh &obj_mesh_data) const;
-  void write_uv_coords(OBJMesh &obj_mesh_data, Vector<Vector<uint>> &uv_indices) const;
+  void write_uv_coords(OBJMesh &obj_mesh_data) const;
   void write_poly_normals(OBJMesh &obj_mesh_data) const;
   void write_smooth_group(const OBJMesh &obj_mesh_data,
                           uint poly_index,
@@ -82,7 +82,7 @@ class OBJWriter {
   void write_vertex_group(const OBJMesh &obj_mesh_data,
                           const uint poly_index,
                           short &r_last_face_vertex_group) const;
-  void write_poly_elements(const OBJMesh &obj_mesh_data, Span<Vector<uint>> uv_indices);
+  void write_poly_elements(const OBJMesh &obj_mesh_data);
   void write_loose_edges(const OBJMesh &obj_mesh_data) const;
   void write_nurbs_curve(const OBJCurve &obj_nurbs_data) const;
 
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
index dbe43df02e7..8e80f41df9e 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -157,6 +157,16 @@ uint OBJMesh::tot_uv_vertices() const
   return tot_uv_vertices_;
 }
 
+/**
+ * UV vertex indices of one polygon.
+ */
+Span<uint> OBJMesh::uv_indices(const int poly_index) const
+{
+  BLI_assert(poly_index < export_mesh_eval_->totpoly);
+  BLI_assert(poly_index < uv_indices_.size());
+  return uv_indices_[poly_index];
+}
+
 uint OBJMesh::tot_edges() const
 {
   return export_mesh_eval_->totedge;
@@ -183,7 +193,7 @@ uint OBJMesh::tot_smooth_groups() const
 /**
  * Return smooth group of the polygon at the given index.
  */
-int OBJMesh::ith_smooth_group(int poly_index) const
+int OBJMesh::ith_smooth_group(const int poly_index) const
 {
   /* Calculate smooth groups first. */
   BLI_assert(tot_smooth_groups_ != -1);
@@ -300,10 +310,10 @@ void OBJMesh::calc_poly_vertex_indices(const uint poly_index,
 }
 
 /**
- * Store UV vertex coordinates of an object as well as their indices.
+ * Fill UV vertex coordinates of an object in the given buffer. Also, store the
+ * UV vertex indices in the member variable.
  */
-void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords,
-                                          Vector<Vector<uint>> &r_uv_indices)
+void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords)
 {
   const MPoly *mpoly = export_mesh_eval_->mpoly;
   const MLoop *mloop = export_mesh_eval_->mloop;
@@ -320,7 +330,7 @@ void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coo
   UvVertMap *uv_vert_map = BKE_mesh_uv_vert_map_create(
       mpoly, mloop, mloopuv, totpoly, totvert, limit, false, false);
 
-  r_uv_indices.resize(totpoly);
+  uv_indices_.resize(totpoly);
   /* At least total vertices of a mesh will be present in its texture map. So
    * reserve minimum space early. */
   r_uv_coords.reserve(totvert);
@@ -344,8 +354,8 @@ void OBJMesh::store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coo
       r_uv_coords[tot_uv_vertices_ - 1][0] = vert_uv_coords[0];
       r_uv_coords[tot_uv_vertices_ - 1][1] = vert_uv_coords[1];
 
-      r_uv_indices[uv_vert->poly_index].resize(vertices_in_poly);
-      r_uv_indices[uv_vert->poly_index][uv_vert->loop_of_poly_index] = tot_uv_vertices_;
+      uv_indices_[uv_vert->poly_index].resize(vertices_in_poly);
+      uv_indices_[uv_vert->poly_index][uv_vert->loop_of_poly_index] = tot_uv_vertices_;
     }
   }
   BKE_mesh_uv_vert_map_free(uv_vert_map);
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
index 07c14213fde..b5fc8cf980a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.hh
@@ -25,7 +25,6 @@
 
 #include <optional>
 
-#include "BLI_array.hh"
 #include "BLI_float3.hh"
 #include "BLI_utility_mixins.hh"
 #include "BLI_vector.hh"
@@ -58,6 +57,10 @@ class OBJMesh : NonMovable, NonCopyable {
    * Total UV vertices in a mesh's texture map.
    */
   uint tot_uv_vertices_ = 0;
+  /**
+   * Per face per vertex UV vertex indices. Make sure to fill them while writing UV coordinates.
+   */
+  Vector<Vector<uint>> uv_indices_;
   /**
    * Total smooth groups in an object.
    */
@@ -74,10 +77,11 @@ class OBJMesh : NonMovable, NonCopyable {
   uint tot_vertices() const;
   uint tot_polygons() const;
   uint tot_uv_vertices() const;
+  Span<uint> uv_indices(const int poly_index) const;
   uint tot_edges() const;
   short tot_col() const;
   uint tot_smooth_groups() const;
-  int ith_smooth_group(int poly_index) const;
+  int ith_smooth_group(const int poly_index) const;
 
   void ensure_mesh_normals() const;
   void ensure_mesh_edges() const;
@@ -94,8 +98,7 @@ class OBJMesh : NonMovable, NonCopyable {
 
   float3 calc_vertex_coords(const uint vert_index, const float scaling_factor) const;
   void calc_poly_vertex_indices(const uint poly_index, Vector<uint> &r_poly_vertex_indices) const;
-  void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords,
-                                   Vector<Vector<uint>> &r_uv_indices);
+  void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords);
   float3 calc_poly_normal(const uint poly_index) const;
   float3 calc_vertex_normal(const uint vert_index) const;
   const char *get_poly_deform_group_name(const uint poly_index, short &r_last_vertex_group) const;
diff --git a/source/blender/io/wavefront_obj/intern/obj_exporter.cc b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
index 67f646379da..907c22afd7a 100644
--- a/source/blender/io/wavefront_obj/intern/obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
@@ -127,12 +127,14 @@ static void export_frame(ViewLayer *view_layer,
     /* Create an empty MTL file in the beginning, to be appended later. */
     frame_writer.write_mtllib(filepath);
   }
-  for (std::unique_ptr<OBJMesh> &mesh_to_export : exportable_as_mesh) {
+  for (int i = 0; i < exportable_as_mesh.size(); i++) {
+    /* Smooth groups and UV vertex indices may take huge memory, so remove objects right
+     * after they're written. */
+    const std::unique_ptr<OBJMesh> mesh_to_export = std::move(exportable_as_mesh[i]);
     frame_writer.write_object_name(*mesh_to_export);
     frame_writer.write_vertex_coords(*mesh_to_export);
 
     if (mesh_to_export->tot_polygons() > 0) {
-      Vector<Vector<uint>> uv_indices;
       if (export_params.export_smooth_groups) {
         mesh_to_export->calc_smooth_groups(export_params.smooth_groups_bitflags);
       }
@@ -140,20 +142,23 @@ static void export_frame(ViewLayer *view_layer,
         frame_writer.write_poly_normals(*mesh_to_export);
       }
       if (export_params.export_uv) {
-        frame_writer.write_uv_coords(*mesh_to_export, uv_indices);
+        frame_writer.write_uv_coords(*mesh_to_export);
       }
       if (export_params.export_materials) {
         MTLWriter mtl_writer(filepath);
         mtl_writer.append_materials(*mesh_to_export);
       }
-      frame_writer.write_poly_elements(*mesh_to_export, uv_indices);
+      frame_writer.write_poly_elements(*mesh_to_export);
     }
     frame_writer.write_loose_edges(*mesh_to_export);
 
     frame_writer.update_index_offsets(*mesh_to_export);
   }
+
   /* Export nurbs in parm form, not as vertices and edges. */
-  for (std::unique_ptr<OBJCurve> &nurbs_to_exp

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list