Commit: 1f7013fb90b30d1bfcbc832f91bf18d707eaae8c
Author: Aras Pranckevicius
Date:   Sun Jan 30 15:03:31 2022 -0500
Branches: master
https://developer.blender.org/rB1f7013fb90b30d1bfcbc832f91bf18d707eaae8c

Speed up the new OBJ exporter via bigger write buffer and parallelization.

This is a patch from Aras Pranckevicius, D13927. See that patch for full
details. On Windows, the many small fprintfs were taking up a large amount
of time and significant speedup comes from using snprintf into chained buffers,
and writing them all out later.
On both Windows and Linux, parallelizing the processing by Object can also lead
to a significant increase in speed.
The 3.0 splash screen scene exports 8 times faster than the current C++ exporter
on a Windows machine with 32 threads, and 5.8 times faster on a Linux machine
with 48 threads.

There is admittedly more memory usage for this, but it is still using 25 times
less memory than the old python exporter on the 3.0 splash screen scene, so
this seems an acceptable tradeoff. If use cases come up for exporting obj files
that exceed the memory size of users, a flag could be added to not parallelize
and write the buffers out every so often.

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

M       source/blender/io/wavefront_obj/CMakeLists.txt
M       source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M       source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
M       source/blender/io/wavefront_obj/exporter/obj_export_io.hh
M       source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
M       source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
M       source/blender/io/wavefront_obj/exporter/obj_exporter.cc
M       source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc

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

diff --git a/source/blender/io/wavefront_obj/CMakeLists.txt 
b/source/blender/io/wavefront_obj/CMakeLists.txt
index 296dd70b5a2..0b1be7946cf 100644
--- a/source/blender/io/wavefront_obj/CMakeLists.txt
+++ b/source/blender/io/wavefront_obj/CMakeLists.txt
@@ -56,6 +56,12 @@ set(LIB
   bf_blenkernel
 )
 
+if(WITH_TBB)
+  add_definitions(-DWITH_TBB)
+  list(APPEND INC_SYS ${TBB_INCLUDE_DIRS})
+  list(APPEND LIB ${TBB_LIBRARIES})
+endif()
+
 blender_add_lib(bf_wavefront_obj "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
 
 if(WITH_GTESTS)
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc 
b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
index d31353c4a76..4f5321019d5 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
@@ -52,68 +52,75 @@ const char *DEFORM_GROUP_DISABLED = "off";
  * So an empty material name is written. */
 const char *MATERIAL_GROUP_DISABLED = "";
 
-void OBJWriter::write_vert_uv_normal_indices(Span<int> vert_indices,
+void OBJWriter::write_vert_uv_normal_indices(FormatHandler<eFileType::OBJ> &fh,
+                                             const IndexOffsets &offsets,
+                                             Span<int> vert_indices,
                                              Span<int> uv_indices,
                                              Span<int> normal_indices) const
 {
   BLI_assert(vert_indices.size() == uv_indices.size() &&
              vert_indices.size() == normal_indices.size());
-  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    file_handler_->write<eOBJSyntaxElement::vertex_uv_normal_indices>(
-        vert_indices[j] + index_offsets_.vertex_offset + 1,
-        uv_indices[j] + index_offsets_.uv_vertex_offset + 1,
-        normal_indices[j] + index_offsets_.normal_offset + 1);
+    fh.write<eOBJSyntaxElement::vertex_uv_normal_indices>(
+        vert_indices[j] + offsets.vertex_offset + 1,
+        uv_indices[j] + offsets.uv_vertex_offset + 1,
+        normal_indices[j] + offsets.normal_offset + 1);
   }
-  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
+  fh.write<eOBJSyntaxElement::poly_element_end>();
 }
 
-void OBJWriter::write_vert_normal_indices(Span<int> vert_indices,
+void OBJWriter::write_vert_normal_indices(FormatHandler<eFileType::OBJ> &fh,
+                                          const IndexOffsets &offsets,
+                                          Span<int> vert_indices,
                                           Span<int> /*uv_indices*/,
                                           Span<int> normal_indices) const
 {
   BLI_assert(vert_indices.size() == normal_indices.size());
-  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    file_handler_->write<eOBJSyntaxElement::vertex_normal_indices>(
-        vert_indices[j] + index_offsets_.vertex_offset + 1,
-        normal_indices[j] + index_offsets_.normal_offset + 1);
+    fh.write<eOBJSyntaxElement::vertex_normal_indices>(vert_indices[j] + 
offsets.vertex_offset + 1,
+                                                       normal_indices[j] + 
offsets.normal_offset +
+                                                           1);
   }
-  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
+  fh.write<eOBJSyntaxElement::poly_element_end>();
 }
 
-void OBJWriter::write_vert_uv_indices(Span<int> vert_indices,
+void OBJWriter::write_vert_uv_indices(FormatHandler<eFileType::OBJ> &fh,
+                                      const IndexOffsets &offsets,
+                                      Span<int> vert_indices,
                                       Span<int> uv_indices,
                                       Span<int> /*normal_indices*/) const
 {
   BLI_assert(vert_indices.size() == uv_indices.size());
-  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write<eOBJSyntaxElement::poly_element_begin>();
   for (int j = 0; j < vert_indices.size(); j++) {
-    file_handler_->write<eOBJSyntaxElement::vertex_uv_indices>(
-        vert_indices[j] + index_offsets_.vertex_offset + 1,
-        uv_indices[j] + index_offsets_.uv_vertex_offset + 1);
+    fh.write<eOBJSyntaxElement::vertex_uv_indices>(vert_indices[j] + 
offsets.vertex_offset + 1,
+                                                   uv_indices[j] + 
offsets.uv_vertex_offset + 1);
   }
-  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
+  fh.write<eOBJSyntaxElement::poly_element_end>();
 }
 
-void OBJWriter::write_vert_indices(Span<int> vert_indices,
+void OBJWriter::write_vert_indices(FormatHandler<eFileType::OBJ> &fh,
+                                   const IndexOffsets &offsets,
+                                   Span<int> vert_indices,
                                    Span<int> /*uv_indices*/,
                                    Span<int> /*normal_indices*/) const
 {
-  file_handler_->write<eOBJSyntaxElement::poly_element_begin>();
+  fh.write<eOBJSyntaxElement::poly_element_begin>();
   for (const int vert_index : vert_indices) {
-    file_handler_->write<eOBJSyntaxElement::vertex_indices>(vert_index +
-                                                            
index_offsets_.vertex_offset + 1);
+    fh.write<eOBJSyntaxElement::vertex_indices>(vert_index + 
offsets.vertex_offset + 1);
   }
-  file_handler_->write<eOBJSyntaxElement::poly_element_end>();
+  fh.write<eOBJSyntaxElement::poly_element_end>();
 }
 
 void OBJWriter::write_header() const
 {
   using namespace std::string_literals;
-  file_handler_->write<eOBJSyntaxElement::string>("# Blender "s + 
BKE_blender_version_string() +
-                                                  "\n");
-  file_handler_->write<eOBJSyntaxElement::string>("# www.blender.org\n");
+  FormatHandler<eFileType::OBJ> fh;
+  fh.write<eOBJSyntaxElement::string>("# Blender "s + 
BKE_blender_version_string() + "\n");
+  fh.write<eOBJSyntaxElement::string>("# www.blender.org\n");
+  fh.write_to_file(outfile_);
 }
 
 void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
@@ -122,10 +129,13 @@ void OBJWriter::write_mtllib_name(const StringRefNull 
mtl_filepath) const
   char mtl_file_name[FILE_MAXFILE];
   char mtl_dir_name[FILE_MAXDIR];
   BLI_split_dirfile(mtl_filepath.data(), mtl_dir_name, mtl_file_name, 
FILE_MAXDIR, FILE_MAXFILE);
-  file_handler_->write<eOBJSyntaxElement::mtllib>(mtl_file_name);
+  FormatHandler<eFileType::OBJ> fh;
+  fh.write<eOBJSyntaxElement::mtllib>(mtl_file_name);
+  fh.write_to_file(outfile_);
 }
 
-void OBJWriter::write_object_group(const OBJMesh &obj_mesh_data) const
+void OBJWriter::write_object_group(FormatHandler<eFileType::OBJ> &fh,
+                                   const OBJMesh &obj_mesh_data) const
 {
   /* "o object_name" is not mandatory. A valid .OBJ file may contain neither
    * "o name" nor "g group_name". */
@@ -138,54 +148,52 @@ void OBJWriter::write_object_group(const OBJMesh 
&obj_mesh_data) const
   const char *object_material_name = obj_mesh_data.get_object_material_name(0);
   if (export_params_.export_materials && export_params_.export_material_groups 
&&
       object_material_name) {
-    file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + 
object_mesh_name +
-                                                          "_" + 
object_material_name);
-    return;
+    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + 
object_mesh_name + "_" +
+                                              object_material_name);
+  }
+  else {
+    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + 
object_mesh_name);
   }
-  file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + 
object_mesh_name);
 }
 
-void OBJWriter::write_object_name(const OBJMesh &obj_mesh_data) const
+void OBJWriter::write_object_name(FormatHandler<eFileType::OBJ> &fh,
+                                  const OBJMesh &obj_mesh_data) const
 {
   const char *object_name = obj_mesh_data.get_object_name();
   if (export_params_.export_object_groups) {
-    write_object_group(obj_mesh_data);
+    write_object_group(fh, obj_mesh_data);
     return;
   }
-  file_handler_->write<eOBJSyntaxElement::object_name>(object_name);
+  fh.write<eOBJSyntaxElement::object_name>(object_name);
 }
 
-void OBJWriter::write_vertex_coords(const OBJMesh &obj_mesh_data) const
+void OBJWriter::write_vertex_coords(FormatHandler<eFileType::OBJ> &fh,
+                                    const OBJMesh &obj_mesh_data) const
 {
   const int tot_vertices = obj_mesh_data.tot_vertices();
   for (int i = 0; i < tot_vertices; i++) {
     float3 vertex = obj_mesh_data.calc_vertex_coords(i, 
export_params_.scaling_factor);
-    file_handler_->write<eOBJSyntaxElement::vertex_coords>(vertex[0], 
vertex[1], vertex[2]);
+    fh.write<eOBJSyntaxElement::vertex_coords>(vertex[0], vertex[1], 
vertex[2]);
   }
 }
 
-void OBJWriter::write_uv_coords(OBJMesh &r_obj_mesh_data) const
+void OBJWriter::write_uv_coords(FormatHandler<eFileType::OBJ> &fh, OBJMesh 
&r_obj_mesh_data) const
 {
-  Vector<std::array<float, 2>> uv_coords;
-  /* UV indices are calculated and stored in an OBJMesh member here. */
-  r_obj_mesh_data.store_uv_coords_and_indices(uv_coords);
-
-  for (const std::array<float, 2> &uv_vertex : uv_coords) {
-    file_handler_->write<eOBJSyntaxElement::uv_vertex_coords>(uv_vertex[0], 
uv_vertex[1]);
+  for (const float2 &uv_vertex : r_obj_mesh_data.get_uv_coords()) {
+    fh.write<eOBJSyntaxElement::uv_vertex_coords>(uv_vertex[0], uv_vertex[1]);
   }
 }
 
-void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data)
+void OBJWriter::write_poly_normals(FormatHandler<eFileType::OBJ> &fh, OBJMesh 
&obj_mesh_data)
 {
-  obj_mesh_data.ensure_mesh_normals();
-  Vector<float3> normals;
-  obj_mesh_data.store_normal_coords_and_indices(normals);
-  for (const float3 &normal : normals) {
-    file_handler_->write<eOBJSyntaxElement::normal>(normal[0], normal[1], 
normal[2]);
+  /* Poly normals should be calculated 

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to