diff options
author | Brecht Van Lommel <brecht@blender.org> | 2021-06-14 16:50:24 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2021-06-15 18:28:44 +0300 |
commit | fcc844f8fbd0d10aeb5012c0b25babe76c278e9e (patch) | |
tree | cac9fac5b9350a8a2b839188332ba4ef63f2aa44 /source/blender/blenkernel/intern/bvhutils.c | |
parent | b3f0dc29070c28f19773fd185dae10f004a4f23e (diff) |
BLI: use explicit task isolation, no longer part of parallel operations
After looking into task isolation issues with Sergey, we couldn't find the
reason behind the deadlocks that we are getting in T87938 and a Sprite Fright
file involving motion blur renders.
There is no apparent place where we adding or waiting on tasks in a task group
from different isolation regions, which is what is known to cause problems. Yet
it still hangs. Either we do not understand some limitation of TBB isolation,
or there is a bug in TBB, but we could not figure it out.
Instead the idea is to use isolation only where we know we need it: when
holding a mutex lock and then doing some multithreaded operation within that
locked region. Three places where we do this now:
* Generated images
* Cached BVH tree building
* OpenVDB lazy grid loading
Compared to the more automatic approach previously used, there is the downside
that it is easy to miss places where we need isolation. Yet doing it more
automatically is also causing unexpected issue and bugs that we found no
solution for, so this seems better.
Patch implemented by Sergey and me.
Differential Revision: https://developer.blender.org/D11603
Diffstat (limited to 'source/blender/blenkernel/intern/bvhutils.c')
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.c | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c index bc63e423c09..116e6279657 100644 --- a/source/blender/blenkernel/intern/bvhutils.c +++ b/source/blender/blenkernel/intern/bvhutils.c @@ -31,6 +31,7 @@ #include "BLI_linklist.h" #include "BLI_math.h" +#include "BLI_task.h" #include "BLI_threads.h" #include "BLI_utildefines.h" @@ -160,6 +161,26 @@ void bvhcache_free(BVHCache *bvh_cache) MEM_freeN(bvh_cache); } +/* BVH tree balancing inside a mutex lock must be run in isolation. Balancing + * is multithreaded, and we do not want the current thread to start another task + * that may involve acquiring the same mutex lock that it is waiting for. */ +static void bvhtree_balance_isolated(void *userdata) +{ + BLI_bvhtree_balance((BVHTree *)userdata); +} + +static void bvhtree_balance(BVHTree *tree, const bool isolate) +{ + if (tree) { + if (isolate) { + BLI_task_isolate(bvhtree_balance_isolated, tree); + } + else { + BLI_bvhtree_balance(tree); + } + } +} + /** \} */ /* -------------------------------------------------------------------- */ /** \name Local Callbacks @@ -566,7 +587,6 @@ static BVHTree *bvhtree_from_editmesh_verts_create_tree(float epsilon, BLI_bvhtree_insert(tree, i, eve->co, 1); } BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active); - BLI_bvhtree_balance(tree); } return tree; @@ -600,7 +620,6 @@ static BVHTree *bvhtree_from_mesh_verts_create_tree(float epsilon, BLI_bvhtree_insert(tree, i, vert[i].co, 1); } BLI_assert(BLI_bvhtree_get_len(tree) == verts_num_active); - BLI_bvhtree_balance(tree); } } @@ -649,6 +668,7 @@ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data, if (data->cached == false) { tree = bvhtree_from_editmesh_verts_create_tree( epsilon, tree_type, axis, em, verts_mask, verts_num_active); + bvhtree_balance(tree, true); /* Save on cache for later use */ /* printf("BVHTree built and saved on cache\n"); */ @@ -660,6 +680,7 @@ BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data, else { tree = bvhtree_from_editmesh_verts_create_tree( epsilon, tree_type, axis, em, verts_mask, verts_num_active); + bvhtree_balance(tree, false); } if (tree) { @@ -711,6 +732,7 @@ BVHTree *bvhtree_from_mesh_verts_ex(BVHTreeFromMesh *data, if (in_cache == false) { tree = bvhtree_from_mesh_verts_create_tree( epsilon, tree_type, axis, vert, verts_num, verts_mask, verts_num_active); + bvhtree_balance(tree, bvh_cache_p != NULL); if (bvh_cache_p) { /* Save on cache for later use */ @@ -771,7 +793,6 @@ static BVHTree *bvhtree_from_editmesh_edges_create_tree(float epsilon, BLI_bvhtree_insert(tree, i, co[0], 2); } BLI_assert(BLI_bvhtree_get_len(tree) == edges_num_active); - BLI_bvhtree_balance(tree); } return tree; @@ -809,7 +830,6 @@ static BVHTree *bvhtree_from_mesh_edges_create_tree(const MVert *vert, BLI_bvhtree_insert(tree, i, co[0], 2); } - BLI_bvhtree_balance(tree); } } @@ -861,7 +881,7 @@ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data, if (data->cached == false) { tree = bvhtree_from_editmesh_edges_create_tree( epsilon, tree_type, axis, em, edges_mask, edges_num_active); - + bvhtree_balance(tree, true); /* Save on cache for later use */ /* printf("BVHTree built and saved on cache\n"); */ bvhcache_insert(bvh_cache, tree, bvh_cache_type); @@ -872,6 +892,7 @@ BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data, else { tree = bvhtree_from_editmesh_edges_create_tree( epsilon, tree_type, axis, em, edges_mask, edges_num_active); + bvhtree_balance(tree, false); } if (tree) { @@ -928,12 +949,17 @@ BVHTree *bvhtree_from_mesh_edges_ex(BVHTreeFromMesh *data, vert, edge, edges_num, edges_mask, edges_num_active, epsilon, tree_type, axis); if (bvh_cache_p) { + bvhtree_balance(tree, true); + BVHCache *bvh_cache = *bvh_cache_p; /* Save on cache for later use */ /* printf("BVHTree built and saved on cache\n"); */ bvhcache_insert(bvh_cache, tree, bvh_cache_type); in_cache = true; } + else { + bvhtree_balance(tree, false); + } } if (bvh_cache_p) { @@ -994,7 +1020,6 @@ static BVHTree *bvhtree_from_mesh_faces_create_tree(float epsilon, } } BLI_assert(BLI_bvhtree_get_len(tree) == faces_num_active); - BLI_bvhtree_balance(tree); } } @@ -1057,6 +1082,7 @@ BVHTree *bvhtree_from_mesh_faces_ex(BVHTreeFromMesh *data, if (in_cache == false) { tree = bvhtree_from_mesh_faces_create_tree( epsilon, tree_type, axis, vert, face, numFaces, faces_mask, faces_num_active); + bvhtree_balance(tree, bvh_cache_p != NULL); if (bvh_cache_p) { /* Save on cache for later use */ @@ -1127,7 +1153,6 @@ static BVHTree *bvhtree_from_editmesh_looptri_create_tree(float epsilon, } } BLI_assert(BLI_bvhtree_get_len(tree) == looptri_num_active); - BLI_bvhtree_balance(tree); } } @@ -1173,7 +1198,6 @@ static BVHTree *bvhtree_from_mesh_looptri_create_tree(float epsilon, } } BLI_assert(BLI_bvhtree_get_len(tree) == looptri_num_active); - BLI_bvhtree_balance(tree); } } @@ -1229,6 +1253,7 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data, bool in_cache = bvhcache_find( bvh_cache_p, bvh_cache_type, &tree, &lock_started, mesh_eval_mutex); BVHCache *bvh_cache = *bvh_cache_p; + bvhtree_balance(tree, true); if (in_cache == false) { tree = bvhtree_from_editmesh_looptri_create_tree( @@ -1243,6 +1268,7 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data, else { tree = bvhtree_from_editmesh_looptri_create_tree( epsilon, tree_type, axis, em, looptri_mask, looptri_num_active); + bvhtree_balance(tree, false); } if (tree) { @@ -1303,6 +1329,8 @@ BVHTree *bvhtree_from_mesh_looptri_ex(BVHTreeFromMesh *data, looptri_mask, looptri_num_active); + bvhtree_balance(tree, bvh_cache_p != NULL); + if (bvh_cache_p) { BVHCache *bvh_cache = *bvh_cache_p; bvhcache_insert(bvh_cache, tree, bvh_cache_type); @@ -1742,7 +1770,7 @@ BVHTree *BKE_bvhtree_from_pointcloud_get(BVHTreeFromPointCloud *data, BLI_bvhtree_insert(tree, i, pointcloud->co[i], 1); } BLI_assert(BLI_bvhtree_get_len(tree) == pointcloud->totpoint); - BLI_bvhtree_balance(tree); + bvhtree_balance(tree, false); data->coords = pointcloud->co; data->tree = tree; |