Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Tönne <lukas.toenne@gmail.com>2022-07-16 10:39:05 +0300
committerLukas Tönne <lukas.toenne@gmail.com>2022-07-16 10:39:05 +0300
commit6da390066cc4158676aac6161387b7ad73017edc (patch)
treec1a0d8b424685d365d6739a98d5e61a0a62ea9cf
parent60407c2e8c15864ed2245534dc6e84a8a966910b (diff)
Curve sculpting: Fix threading issues in constraint solver.
The constraint solver gets a span of changed curve indices. The comb brush collects these in a thread-local vector, which is only valid in a `parallel_for` loop. The BVH storage also needs to be local to the function, otherwise parallel calls to the constraint solver will free the shared storage at the end of the task.
-rw-r--r--source/blender/editors/sculpt_paint/curves_sculpt_comb.cc21
-rw-r--r--source/blender/editors/sculpt_paint/curves_sculpt_constraints.cc22
-rw-r--r--source/blender/editors/sculpt_paint/curves_sculpt_intern.hh3
3 files changed, 23 insertions, 23 deletions
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc b/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
index b9598c51b18..3180bb374c3 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
@@ -161,16 +161,17 @@ struct CombOperationExecutor {
BLI_assert_unreachable();
}
- Vector<int> &local_changed_curves = changed_curves.local();
- self_->constraint_solver_.find_contact_points(ctx_.depsgraph,
- object_,
- curves_,
- curves_id_->surface,
- transforms_,
- orig_positions_,
- local_changed_curves);
-
- self_->constraint_solver_.solve_constraints(curves_, local_changed_curves);
+ threading::parallel_for_each(changed_curves, [&](const Vector<int> &changed_curves) {
+ self_->constraint_solver_.find_contact_points(ctx_.depsgraph,
+ object_,
+ curves_,
+ curves_id_->surface,
+ transforms_,
+ orig_positions_,
+ changed_curves);
+
+ self_->constraint_solver_.solve_constraints(curves_, changed_curves);
+ });
curves_->tag_positions_changed();
DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY);
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_constraints.cc b/source/blender/editors/sculpt_paint/curves_sculpt_constraints.cc
index 98019c32013..76687cf4266 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_constraints.cc
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_constraints.cc
@@ -59,15 +59,17 @@ void CurvesConstraintSolver::find_contact_points(const Depsgraph *depsgraph,
const float curves_to_surface_scale = mat4_to_scale(transforms.curves_to_surface.ptr());
const float surface_to_curves_scale = mat4_to_scale(transforms.curves_to_surface.ptr());
- VArray<float> radius = curves->attributes().lookup_or_default<float>(
- "radius", ATTR_DOMAIN_POINT, 0.0f);
-
const Mesh *surface = static_cast<Mesh *>(surface_ob->data);
Span<MLoopTri> surface_looptris = {BKE_mesh_runtime_looptri_ensure(surface),
BKE_mesh_runtime_looptri_len(surface)};
- BKE_bvhtree_from_mesh_get(&surface_bvh_, surface, BVHTREE_FROM_LOOPTRI, 2);
- BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh_); });
+ /** BVH tree of the surface mesh for finding collisions. */
+ BVHTreeFromMesh surface_bvh;
+ BKE_bvhtree_from_mesh_get(&surface_bvh, surface, BVHTREE_FROM_LOOPTRI, 2);
+ BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh); });
+
+ VArray<float> radius = curves->attributes().lookup_or_default<float>(
+ "radius", ATTR_DOMAIN_POINT, 0.0f);
threading::parallel_for(changed_curves.index_range(), 256, [&](const IndexRange range) {
for (const int curve_i : changed_curves.slice(range)) {
@@ -85,13 +87,13 @@ void CurvesConstraintSolver::find_contact_points(const Depsgraph *depsgraph,
float margin_su = curves_to_surface_scale * margin;
float hit_dist_su = normalize_v3(dir_su) + margin_su;
BLI_bvhtree_ray_cast_all_cpp(
- *surface_bvh_.tree,
+ *surface_bvh.tree,
start_su,
dir_su,
margin_su,
hit_dist_su,
[&](const int triangle_i, const BVHTreeRay &ray, BVHTreeRayHit &hit) {
- surface_bvh_.raycast_callback(&surface_bvh_, triangle_i, &ray, &hit);
+ surface_bvh.raycast_callback(&surface_bvh, triangle_i, &ray, &hit);
if (hit.index >= 0) {
const float dist_cu = surface_to_curves_scale * hit.dist;
@@ -115,9 +117,9 @@ void CurvesConstraintSolver::find_contact_points(const Depsgraph *depsgraph,
}
if (insert_i >= 0) {
contacts[insert_i] = Contact{dist_cu,
- transforms.surface_to_curves_normal *
- float3{hit.no},
- transforms.surface_to_curves * float3{hit.co}};
+ transforms.surface_to_curves_normal *
+ float3{hit.no},
+ transforms.surface_to_curves * float3{hit.co}};
}
}
});
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_intern.hh b/source/blender/editors/sculpt_paint/curves_sculpt_intern.hh
index ac593f9c628..936ee75850a 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_intern.hh
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_intern.hh
@@ -111,9 +111,6 @@ class CurvesConstraintSolver {
/** Length of each segment indexed by the index of the first point in the segment. */
Array<float> segment_lengths_cu_;
- /** BVH tree of the surface mesh for finding collisions. */
- BVHTreeFromMesh surface_bvh_;
-
struct Contact {
float dist_;
float3 normal_;