From e0e6afb76604f89ceb68582876addeb2f23e6ca3 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 16 Jan 2023 13:14:03 -0600 Subject: [PATCH] Geometry Nodes: Parallelize mesh and curve edit hint transformation Previously transforming and translating meshes (used by the object info and transform geometry nodes) was single threaded. Now use the same code path as other geometry types which already includes multithreading. I observed a 5x performance improvement for a 4 million vert mesh on a Ryzen 7950x. --- .../nodes/node_geo_transform_geometry.cc | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/source/blender/nodes/geometry/nodes/node_geo_transform_geometry.cc b/source/blender/nodes/geometry/nodes/node_geo_transform_geometry.cc index 425546c3406..281ffc768ef 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_transform_geometry.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_transform_geometry.cc @@ -55,13 +55,15 @@ static void transform_positions(MutableSpan positions, const float4x4 &m static void translate_mesh(Mesh &mesh, const float3 translation) { if (!math::is_zero(translation)) { - BKE_mesh_translate(&mesh, translation, false); + translate_positions(mesh.vert_positions_for_write(), translation); + BKE_mesh_tag_coords_changed_uniformly(&mesh); } } static void transform_mesh(Mesh &mesh, const float4x4 &transform) { - BKE_mesh_transform(&mesh, transform.values, false); + transform_positions(mesh.vert_positions_for_write(), transform); + BKE_mesh_tag_coords_changed(&mesh); } static void translate_pointcloud(PointCloud &pointcloud, const float3 translation) @@ -162,16 +164,17 @@ static void translate_volume(GeoNodeExecParams ¶ms, static void transform_curve_edit_hints(bke::CurvesEditHints &edit_hints, const float4x4 &transform) { if (edit_hints.positions.has_value()) { - for (float3 &pos : *edit_hints.positions) { - pos = transform * pos; - } + transform_positions(*edit_hints.positions, transform); } float3x3 deform_mat; copy_m3_m4(deform_mat.values, transform.values); if (edit_hints.deform_mats.has_value()) { - for (float3x3 &mat : *edit_hints.deform_mats) { - mat = deform_mat * mat; - } + MutableSpan deform_mats = *edit_hints.deform_mats; + threading::parallel_for(deform_mats.index_range(), 1024, [&](const IndexRange range) { + for (const int64_t i : range) { + deform_mats[i] = deform_mat * deform_mats[i]; + } + }); } else { edit_hints.deform_mats.emplace(edit_hints.curves_id_orig.geometry.point_num, deform_mat); @@ -181,9 +184,7 @@ static void transform_curve_edit_hints(bke::CurvesEditHints &edit_hints, const f static void translate_curve_edit_hints(bke::CurvesEditHints &edit_hints, const float3 &translation) { if (edit_hints.positions.has_value()) { - for (float3 &pos : *edit_hints.positions) { - pos += translation; - } + translate_positions(*edit_hints.positions, translation); } }