Geometry Nodes: Avoid mesh copy in some cases

Accessing a mesh with write access can be costly if it is used
elsewhere at the same time because of copy-on-write. When always did
that at the end of the modifier calculation, but it's trivial to only
do that when we might need actually use the mesh to add original
index layers.
This commit is contained in:
Hans Goudey 2022-12-19 14:24:49 -06:00
parent 5fe297df48
commit 4ae0da1bbc
1 changed files with 18 additions and 18 deletions

View File

@ -1282,29 +1282,29 @@ static void modifyGeometry(ModifierData *md,
bool use_orig_index_verts = false;
bool use_orig_index_edges = false;
bool use_orig_index_polys = false;
if (geometry_set.has_mesh()) {
const Mesh &mesh = *geometry_set.get_mesh_for_read();
use_orig_index_verts = CustomData_has_layer(&mesh.vdata, CD_ORIGINDEX);
use_orig_index_edges = CustomData_has_layer(&mesh.edata, CD_ORIGINDEX);
use_orig_index_polys = CustomData_has_layer(&mesh.pdata, CD_ORIGINDEX);
if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
use_orig_index_verts = CustomData_has_layer(&mesh->vdata, CD_ORIGINDEX);
use_orig_index_edges = CustomData_has_layer(&mesh->edata, CD_ORIGINDEX);
use_orig_index_polys = CustomData_has_layer(&mesh->pdata, CD_ORIGINDEX);
}
geometry_set = compute_geometry(
tree, *lf_graph_info, *output_node, std::move(geometry_set), nmd, ctx);
if (geometry_set.has_mesh()) {
/* Add #CD_ORIGINDEX layers if they don't exist already. This is required because the
* #eModifierTypeFlag_SupportsMapping flag is set. If the layers did not exist before, it is
* assumed that the output mesh does not have a mapping to the original mesh. */
Mesh &mesh = *geometry_set.get_mesh_for_write();
if (use_orig_index_verts) {
CustomData_add_layer(&mesh.vdata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh.totvert);
}
if (use_orig_index_edges) {
CustomData_add_layer(&mesh.edata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh.totedge);
}
if (use_orig_index_polys) {
CustomData_add_layer(&mesh.pdata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh.totpoly);
if (use_orig_index_verts || use_orig_index_edges || use_orig_index_polys) {
if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
/* Add #CD_ORIGINDEX layers if they don't exist already. This is required because the
* #eModifierTypeFlag_SupportsMapping flag is set. If the layers did not exist before, it is
* assumed that the output mesh does not have a mapping to the original mesh. */
if (use_orig_index_verts) {
CustomData_add_layer(&mesh->vdata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh->totvert);
}
if (use_orig_index_edges) {
CustomData_add_layer(&mesh->edata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh->totedge);
}
if (use_orig_index_polys) {
CustomData_add_layer(&mesh->pdata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, mesh->totpoly);
}
}
}
}