From 848ced8fe67ae9fad12671524c4dd6966284e9f0 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 3 Sep 2025 04:29:08 +0200 Subject: [PATCH] Curves: Parallelize nurbs custom knots offsets cache calculation Build the size for each curve in parallel, then accumulate the sizes to offsets afterwards. This way it's easy to parallel. Also only count custom knots for NURBS. Pull Request: https://projects.blender.org/blender/blender/pulls/145581 --- .../blenkernel/intern/curves_geometry.cc | 28 ++++++++++++------- .../tests/grease_pencil_merge_test.cc | 7 +++++ .../tests/GEO_realize_instances_test.cc | 1 + 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc index aa392951a4d..24b2277126f 100644 --- a/source/blender/blenkernel/intern/curves_geometry.cc +++ b/source/blender/blenkernel/intern/curves_geometry.cc @@ -548,23 +548,31 @@ IndexMask CurvesGeometry::nurbs_custom_knot_curves(IndexMaskMemory &memory) cons OffsetIndices CurvesGeometry::nurbs_custom_knots_by_curve() const { const CurvesGeometryRuntime &runtime = *this->runtime; - if (this->is_empty()) { + if (!this->has_curve_with_type(CURVE_TYPE_NURBS)) { return {}; } runtime.custom_knot_offsets_cache.ensure([&](Vector &r_data) { - r_data.resize(this->curve_num + 1, 0); + r_data.resize(this->curve_num + 1); - const OffsetIndices points_by_curve = this->points_by_curve(); + const OffsetIndices points_by_curve = this->points_by_curve(); + const VArray curve_types = this->curve_types(); const VArray knot_modes = this->nurbs_knots_modes(); const VArray orders = this->nurbs_orders(); - int knot_count = 0; - for (const int curve : this->curves_range()) { - knot_count += knot_modes[curve] == NURBS_KNOT_MODE_CUSTOM ? - points_by_curve[curve].size() + orders[curve] : - 0; - r_data[curve + 1] = knot_count; - } + threading::parallel_for(this->curves_range(), 1024, [&](const IndexRange range) { + for (const int curve : range) { + if (curve_types[curve] != CURVE_TYPE_NURBS) { + r_data[curve] = 0; + continue; + } + if (knot_modes[curve] != NURBS_KNOT_MODE_CUSTOM) { + r_data[curve] = 0; + continue; + } + r_data[curve] = points_by_curve[curve].size() + orders[curve]; + } + }); + offset_indices::accumulate_counts_to_offsets(r_data.as_mutable_span()); }); return OffsetIndices(runtime.custom_knot_offsets_cache.data()); } diff --git a/source/blender/editors/grease_pencil/tests/grease_pencil_merge_test.cc b/source/blender/editors/grease_pencil/tests/grease_pencil_merge_test.cc index 781b8498771..3fd5c762114 100644 --- a/source/blender/editors/grease_pencil/tests/grease_pencil_merge_test.cc +++ b/source/blender/editors/grease_pencil/tests/grease_pencil_merge_test.cc @@ -190,21 +190,28 @@ TEST(grease_pencil_merge, merge_keyframes) Drawing *drawing = grease_pencil.insert_frame(layer1, 0); drawing->strokes_for_write().resize(10, 2); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer2, 0); drawing->strokes_for_write().resize(20, 3); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer2, 2); drawing->strokes_for_write().resize(30, 4); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer3, 0); drawing->strokes_for_write().resize(40, 5); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer3, 3); drawing->strokes_for_write().resize(50, 6); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer4, 1); drawing->strokes_for_write().resize(60, 7); + drawing->strokes_for_write().update_curve_types(); drawing = grease_pencil.insert_frame(layer4, 3); drawing->strokes_for_write().resize(70, 8); + drawing->strokes_for_write().update_curve_types(); GreasePencil *merged_grease_pencil = BKE_grease_pencil_new_nomain(); BKE_grease_pencil_copy_parameters(grease_pencil, *merged_grease_pencil); diff --git a/source/blender/geometry/tests/GEO_realize_instances_test.cc b/source/blender/geometry/tests/GEO_realize_instances_test.cc index 536f393de65..465fb6d3a77 100644 --- a/source/blender/geometry/tests/GEO_realize_instances_test.cc +++ b/source/blender/geometry/tests/GEO_realize_instances_test.cc @@ -44,6 +44,7 @@ static void create_test_curves(bke::CurvesGeometry &curves, Span offsets) curves.resize(points_num, curves_num); curves.offsets_for_write().copy_from(offsets); + curves.update_curve_types(); /* Attribute storing original indices to test point remapping. */ SpanAttributeWriter test_indices_writer =