From a6baf7beaebbd0315f418d94640f0fd538add9d1 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 26 Apr 2023 23:50:19 -0400 Subject: [PATCH] BLI: Allow different integer types when filling span indices --- .../blender/blenkernel/intern/curves_geometry.cc | 2 +- source/blender/blenlib/BLI_span.hh | 5 +++-- source/blender/blenlib/tests/BLI_span_test.cc | 2 +- .../tests/FN_multi_function_test_common.hh | 2 +- source/blender/geometry/intern/set_curve_type.cc | 14 ++++++++------ source/blender/geometry/intern/trim_curves.cc | 2 +- .../nodes/geometry/nodes/node_geo_curve_sample.cc | 14 +++++++------- .../nodes/node_geo_curve_set_handle_type.cc | 4 ++-- .../nodes/geometry/nodes/node_geo_extrude_mesh.cc | 2 +- .../nodes/geometry/nodes/node_geo_proximity.cc | 6 +++--- .../geometry/nodes/node_geo_sample_nearest.cc | 2 +- .../geometry/nodes/node_geo_set_curve_normal.cc | 2 +- .../nodes/geometry/nodes/node_geo_set_material.cc | 2 +- 13 files changed, 31 insertions(+), 28 deletions(-) diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc index 637fdc78d95..828018fe633 100644 --- a/source/blender/blenkernel/intern/curves_geometry.cc +++ b/source/blender/blenkernel/intern/curves_geometry.cc @@ -281,7 +281,7 @@ void CurvesGeometry::fill_curve_types(const IndexMask selection, const CurveType } } /* A potential performance optimization is only counting the changed indices. */ - this->curve_types_for_write().fill_indices(selection, type); + this->curve_types_for_write().fill_indices(selection.indices(), type); this->update_curve_types(); this->tag_topology_changed(); } diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh index 1ca371a2f46..ddbe42b6706 100644 --- a/source/blender/blenlib/BLI_span.hh +++ b/source/blender/blenlib/BLI_span.hh @@ -551,9 +551,10 @@ template class MutableSpan { * Replace a subset of all elements with the given value. This invokes undefined behavior when * one of the indices is out of bounds. */ - constexpr void fill_indices(Span indices, const T &value) + template constexpr void fill_indices(Span indices, const T &value) { - for (int64_t i : indices) { + static_assert(std::is_integral_v); + for (IndexT i : indices) { BLI_assert(i < size_); data_[i] = value; } diff --git a/source/blender/blenlib/tests/BLI_span_test.cc b/source/blender/blenlib/tests/BLI_span_test.cc index 1fcbdfae567..d3189a8e3c1 100644 --- a/source/blender/blenlib/tests/BLI_span_test.cc +++ b/source/blender/blenlib/tests/BLI_span_test.cc @@ -225,7 +225,7 @@ TEST(span, FillIndices) { std::array a = {0, 0, 0, 0, 0}; MutableSpan a_span(a); - a_span.fill_indices({0, 2, 3}, 1); + a_span.fill_indices(Span({0, 2, 3}), 1); EXPECT_EQ(a[0], 1); EXPECT_EQ(a[1], 0); EXPECT_EQ(a[2], 1); diff --git a/source/blender/functions/tests/FN_multi_function_test_common.hh b/source/blender/functions/tests/FN_multi_function_test_common.hh index 84861ee440c..7c8f7919f36 100644 --- a/source/blender/functions/tests/FN_multi_function_test_common.hh +++ b/source/blender/functions/tests/FN_multi_function_test_common.hh @@ -178,7 +178,7 @@ class OptionalOutputsFunction : public MultiFunction { { if (params.single_output_is_required(0, "Out 1")) { MutableSpan values = params.uninitialized_single_output(0, "Out 1"); - values.fill_indices(mask, 5); + values.fill_indices(mask.indices(), 5); } MutableSpan values = params.uninitialized_single_output(1, "Out 2"); for (const int i : mask) { diff --git a/source/blender/geometry/intern/set_curve_type.cc b/source/blender/geometry/intern/set_curve_type.cc index f5e7f06971e..f8de0b12622 100644 --- a/source/blender/geometry/intern/set_curve_type.cc +++ b/source/blender/geometry/intern/set_curve_type.cc @@ -518,8 +518,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs( }; auto catmull_rom_to_nurbs = [&](IndexMask selection) { - dst_curves.nurbs_orders_for_write().fill_indices(selection, 4); - dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER); + dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4); + dst_curves.nurbs_knots_modes_for_write().fill_indices(selection.indices(), + NURBS_KNOT_MODE_BEZIER); fill_weights_if_necessary(selection); threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { @@ -544,7 +545,7 @@ static bke::CurvesGeometry convert_curves_to_nurbs( }; auto poly_to_nurbs = [&](IndexMask selection) { - dst_curves.nurbs_orders_for_write().fill_indices(selection, 4); + dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4); bke::curves::copy_point_data( src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions); fill_weights_if_necessary(selection); @@ -553,7 +554,7 @@ static bke::CurvesGeometry convert_curves_to_nurbs( * start/end. */ if (src_cyclic.is_single()) { dst_curves.nurbs_knots_modes_for_write().fill_indices( - selection, + selection.indices(), src_cyclic.get_internal_single() ? NURBS_KNOT_MODE_NORMAL : NURBS_KNOT_MODE_ENDPOINT); } else { @@ -576,8 +577,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs( const Span src_handles_l = src_curves.handle_positions_left(); const Span src_handles_r = src_curves.handle_positions_right(); - dst_curves.nurbs_orders_for_write().fill_indices(selection, 4); - dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER); + dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4); + dst_curves.nurbs_knots_modes_for_write().fill_indices(selection.indices(), + NURBS_KNOT_MODE_BEZIER); fill_weights_if_necessary(selection); threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { diff --git a/source/blender/geometry/intern/trim_curves.cc b/source/blender/geometry/intern/trim_curves.cc index 4923ff1a59c..1f4ea939fa0 100644 --- a/source/blender/geometry/intern/trim_curves.cc +++ b/source/blender/geometry/intern/trim_curves.cc @@ -1065,7 +1065,7 @@ bke::CurvesGeometry trim_curves(const bke::CurvesGeometry &src_curves, else { /* Only trimmed curves are no longer cyclic. */ if (bke::SpanAttributeWriter cyclic = dst_attributes.lookup_for_write_span("cyclic")) { - cyclic.span.fill_indices(selection, false); + cyclic.span.fill_indices(selection.indices(), false); cyclic.finish(); } diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc index 0f1f9661c4f..5c099943d07 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc @@ -282,13 +282,13 @@ class SampleCurveFunction : public mf::MultiFunction { auto return_default = [&]() { if (!sampled_positions.is_empty()) { - sampled_positions.fill_indices(mask, {0, 0, 0}); + sampled_positions.fill_indices(mask.indices(), {0, 0, 0}); } if (!sampled_tangents.is_empty()) { - sampled_tangents.fill_indices(mask, {0, 0, 0}); + sampled_tangents.fill_indices(mask.indices(), {0, 0, 0}); } if (!sampled_normals.is_empty()) { - sampled_normals.fill_indices(mask, {0, 0, 0}); + sampled_normals.fill_indices(mask.indices(), {0, 0, 0}); } }; @@ -325,18 +325,18 @@ class SampleCurveFunction : public mf::MultiFunction { auto fill_invalid = [&](const IndexMask mask) { if (!sampled_positions.is_empty()) { - sampled_positions.fill_indices(mask, float3(0)); + sampled_positions.fill_indices(mask.indices(), float3(0)); } if (!sampled_tangents.is_empty()) { - sampled_tangents.fill_indices(mask, float3(0)); + sampled_tangents.fill_indices(mask.indices(), float3(0)); } if (!sampled_normals.is_empty()) { - sampled_normals.fill_indices(mask, float3(0)); + sampled_normals.fill_indices(mask.indices(), float3(0)); } if (!sampled_values.is_empty()) { attribute_math::convert_to_static_type(source_data_->type(), [&](auto dummy) { using T = decltype(dummy); - sampled_values.typed().fill_indices(mask, {}); + sampled_values.typed().fill_indices(mask.indices(), {}); }); } }; diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_set_handle_type.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_set_handle_type.cc index d06ebd796e6..e1e9d164075 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_curve_set_handle_type.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_curve_set_handle_type.cc @@ -63,10 +63,10 @@ static void set_handle_type(bke::CurvesGeometry &curves, const IndexMask selection = evaluator.get_evaluated_selection_as_mask(); if (mode & GEO_NODE_CURVE_HANDLE_LEFT) { - curves.handle_types_left_for_write().fill_indices(selection, new_handle_type); + curves.handle_types_left_for_write().fill_indices(selection.indices(), new_handle_type); } if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) { - curves.handle_types_right_for_write().fill_indices(selection, new_handle_type); + curves.handle_types_right_for_write().fill_indices(selection.indices(), new_handle_type); } /* Eagerly calculate automatically derived handle positions if necessary. */ diff --git a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc index cf9161c5b7c..e2255606cce 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc @@ -82,7 +82,7 @@ static void save_selection_as_attribute(Mesh &mesh, attribute.span.slice(selection.as_range()).fill(true); } else { - attribute.span.fill_indices(selection, true); + attribute.span.fill_indices(selection.indices(), true); } attribute.finish(); diff --git a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc index 99b9832913c..225c9aa44d4 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc @@ -162,7 +162,7 @@ class ProximityFunction : public mf::MultiFunction { * comparison per vertex, so it's likely not worth it. */ MutableSpan distances = params.uninitialized_single_output(2, "Distance"); - distances.fill_indices(mask, FLT_MAX); + distances.fill_indices(mask.indices(), FLT_MAX); bool success = false; if (target_.has_mesh()) { @@ -177,10 +177,10 @@ class ProximityFunction : public mf::MultiFunction { if (!success) { if (!positions.is_empty()) { - positions.fill_indices(mask, float3(0)); + positions.fill_indices(mask.indices(), float3(0)); } if (!distances.is_empty()) { - distances.fill_indices(mask, 0.0f); + distances.fill_indices(mask.indices(), 0.0f); } return; } diff --git a/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc b/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc index 53a78778e96..652368ea51f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_sample_nearest.cc @@ -255,7 +255,7 @@ class SampleNearestFunction : public mf::MultiFunction { const VArray &positions = params.readonly_single_input(0, "Position"); MutableSpan indices = params.uninitialized_single_output(1, "Index"); if (!src_component_) { - indices.fill_indices(mask, 0); + indices.fill_indices(mask.indices(), 0); return; } diff --git a/source/blender/nodes/geometry/nodes/node_geo_set_curve_normal.cc b/source/blender/nodes/geometry/nodes/node_geo_set_curve_normal.cc index 17e85843091..9ba6c336508 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_set_curve_normal.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_set_curve_normal.cc @@ -35,7 +35,7 @@ static void set_normal_mode(bke::CurvesGeometry &curves, evaluator.set_selection(selection_field); evaluator.evaluate(); const IndexMask selection = evaluator.get_evaluated_selection_as_mask(); - curves.normal_mode_for_write().fill_indices(selection, mode); + curves.normal_mode_for_write().fill_indices(selection.indices(), mode); curves.tag_normals_changed(); } diff --git a/source/blender/nodes/geometry/nodes/node_geo_set_material.cc b/source/blender/nodes/geometry/nodes/node_geo_set_material.cc index dc8bd9ee9c1..027f695d0e9 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_set_material.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_set_material.cc @@ -53,7 +53,7 @@ static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Mate MutableAttributeAccessor attributes = mesh.attributes_for_write(); SpanAttributeWriter material_indices = attributes.lookup_or_add_for_write_span( "material_index", ATTR_DOMAIN_FACE); - material_indices.span.fill_indices(selection, new_material_index); + material_indices.span.fill_indices(selection.indices(), new_material_index); material_indices.finish(); }