diff options
author | Aras Pranckevicius <aras@nesnausk.org> | 2022-07-06 13:29:59 +0300 |
---|---|---|
committer | Aras Pranckevicius <aras@nesnausk.org> | 2022-07-06 13:30:15 +0300 |
commit | 94323bb427480aaaeea52f6833173866fbacf2e1 (patch) | |
tree | 3ed0e5fe0692d294ce2b42ab31dc5bfe076e4471 /source/blender/io | |
parent | 4b0e7fe511b14aecad105d83115592f43d27dbc0 (diff) |
IO: speed up import of large Alembic/USD/OBJ scenes by optimizing material assignment
The importer parts that were doing assignment of materials to the
imported objects/meshes were essentially having a quadratic complexity
in terms of scene object count. For each material assigned to each
object, they were scanning the whole scene, checking which other
Objects use the same Mesh data, in order to resize their material
arrays to match the size.
Performance details (Windows, Ryzen 5950X):
- Import OBJ Blender 3.0 splash scene (24k objects): 43.0s -> 32.9s
- Import USD Disney Moana scene (260k objects): saves two hours
(~7400s). Note that later on this crashes when trying to render the
imported result; crashes in the same way/place both in master and
this patch.
Implementation details:
The importers were doing "scan the world" basically twice for each
object, for each material: once when creating a new material slot
(assigns an empty material), and then again when assigning the
material.
However, all these importers (USD, Alembic, OBJ) always create one
Object for one Mesh. So that whole quadratic complexity resulting
from "scan the world for possible other users of this obdata" is
completely not needed; it just never finds anything. So add a new
dedicated function BKE_object_material_assign_single_obdata that skips
the expensive part, but should only be used when the caller knows that
the obdata has exactly one user (the passed object).
Reviewed By: Bastien Montagne, Michael Kowalski
Differential Revision: https://developer.blender.org/D15145
Diffstat (limited to 'source/blender/io')
-rw-r--r-- | source/blender/io/alembic/intern/abc_reader_mesh.cc | 8 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_reader_mesh.cc | 19 | ||||
-rw-r--r-- | source/blender/io/wavefront_obj/importer/obj_import_mesh.cc | 3 |
3 files changed, 9 insertions, 21 deletions
diff --git a/source/blender/io/alembic/intern/abc_reader_mesh.cc b/source/blender/io/alembic/intern/abc_reader_mesh.cc index d8c48357fc0..df3559c108c 100644 --- a/source/blender/io/alembic/intern/abc_reader_mesh.cc +++ b/source/blender/io/alembic/intern/abc_reader_mesh.cc @@ -77,10 +77,8 @@ static void assign_materials(Main *bmain, const std::map<std::string, int> &mat_index_map) { std::map<std::string, int>::const_iterator it; - for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) { - if (!BKE_object_material_slot_add(bmain, ob)) { - return; - } + if (mat_index_map.size() > MAXMAT) { + return; } std::map<std::string, Material *> matname_to_material = build_material_map(bmain); @@ -100,7 +98,7 @@ static void assign_materials(Main *bmain, assigned_mat = mat_iter->second; } - BKE_object_material_assign(bmain, ob, assigned_mat, mat_index, BKE_MAT_ASSIGN_OBDATA); + BKE_object_material_assign_single_obdata(bmain, ob, assigned_mat, mat_index); } } diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index 46749b03169..45657525527 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -116,24 +116,15 @@ static void assign_materials(Main *bmain, return; } - bool can_assign = true; - std::map<pxr::SdfPath, int>::const_iterator it = mat_index_map.begin(); - - int matcount = 0; - for (; it != mat_index_map.end(); ++it, matcount++) { - if (!BKE_object_material_slot_add(bmain, ob)) { - can_assign = false; - break; - } - } - - if (!can_assign) { + if (mat_index_map.size() > MAXMAT) { return; } blender::io::usd::USDMaterialReader mat_reader(params, bmain); - for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) { + for (std::map<pxr::SdfPath, int>::const_iterator it = mat_index_map.begin(); + it != mat_index_map.end(); + ++it) { Material *assigned_mat = find_existing_material( it->first, params, mat_name_to_mat, usd_path_to_mat_name); @@ -170,7 +161,7 @@ static void assign_materials(Main *bmain, } if (assigned_mat) { - BKE_object_material_assign(bmain, ob, assigned_mat, it->second, BKE_MAT_ASSIGN_OBDATA); + BKE_object_material_assign_single_obdata(bmain, ob, assigned_mat, it->second); } else { /* This shouldn't happen. */ diff --git a/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc b/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc index 1e0b36ae4cb..8cef0700b3b 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc @@ -306,8 +306,7 @@ void MeshFromGeometry::create_materials(Main *bmain, if (mat == nullptr) { continue; } - BKE_object_material_slot_add(bmain, obj); - BKE_object_material_assign(bmain, obj, mat, obj->totcol, BKE_MAT_ASSIGN_USERPREF); + BKE_object_material_assign_single_obdata(bmain, obj, mat, obj->totcol + 1); } } |