[Bf-blender-cvs] [00cb3527902] master: Fix T52149: LoopTriArray computation was not correctly protected against concurrency.

Bastien Montagne noreply at git.blender.org
Fri Aug 11 17:09:02 CEST 2017


Commit: 00cb3527902b11b5f136432e8670e299789b6716
Author: Bastien Montagne
Date:   Fri Aug 11 16:51:19 2017 +0200
Branches: master
https://developer.blender.org/rB00cb3527902b11b5f136432e8670e299789b6716

Fix T52149: LoopTriArray computation was not correctly protected against concurrency.

Note: this commit seems to work as expected (also with transform
snapping etc.). However, it is rather unsafe - not enough for 2.79 at
least, unless we get much more testing on it. It also depends on three
previous ones.

Note that using a global lock here is far from ideal, we should rather
have a lock per DM, but that will do for now, whole DM thing is doomed
to oblivion anyway in 2.8.

Also, we may need a `DM_DIRTY_LOOPTRIS` dirty flag at some point. Looks
like we can survive without it for now though... Probably because cached
looptris are never copied accross DM's?

===================================================================

M	source/blender/blenkernel/BKE_DerivedMesh.h
M	source/blender/blenkernel/intern/DerivedMesh.c
M	source/blender/blenkernel/intern/subsurf_ccg.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_DerivedMesh.h b/source/blender/blenkernel/BKE_DerivedMesh.h
index 5a4fdcf2510..e2577689101 100644
--- a/source/blender/blenkernel/BKE_DerivedMesh.h
+++ b/source/blender/blenkernel/BKE_DerivedMesh.h
@@ -220,7 +220,7 @@ struct DerivedMesh {
 	/** Recalculates mesh tessellation */
 	void (*recalcTessellation)(DerivedMesh *dm);
 
-	/** Loop tessellation cache */
+	/** Loop tessellation cache (WARNING! Only call inside threading-protected code!) */
 	void (*recalcLoopTri)(DerivedMesh *dm);
 	/** accessor functions */
 	const struct MLoopTri *(*getLoopTriArray)(DerivedMesh * dm);
diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c
index 7a05ab4530c..e53f14291b7 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.c
+++ b/source/blender/blenkernel/intern/DerivedMesh.c
@@ -93,6 +93,10 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm);
 #  define ASSERT_IS_VALID_DM(dm)
 #endif
 
+
+static ThreadMutex loops_cache_lock = BLI_MUTEX_INITIALIZER;
+
+
 static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *ob);
 static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid);
 
@@ -241,7 +245,13 @@ static const MLoopTri *dm_getLoopTriArray(DerivedMesh *dm)
 		BLI_assert(dm->getNumLoopTri(dm) == dm->looptris.num);
 	}
 	else {
-		dm->recalcLoopTri(dm);
+		BLI_mutex_lock(&loops_cache_lock);
+		/* We need to ensure array is still NULL inside mutex-protected code, some other thread might have already
+		 * recomputed those looptris. */
+		if (dm->looptris.array == NULL) {
+			dm->recalcLoopTri(dm);
+		}
+		BLI_mutex_unlock(&loops_cache_lock);
 	}
 	return dm->looptris.array;
 }
diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c
index 47dea5ea282..ff0682258c6 100644
--- a/source/blender/blenkernel/intern/subsurf_ccg.c
+++ b/source/blender/blenkernel/intern/subsurf_ccg.c
@@ -4474,10 +4474,10 @@ static void ccgDM_recalcTessellation(DerivedMesh *UNUSED(dm))
 	/* Nothing to do: CCG handles creating its own tessfaces */
 }
 
+/* WARNING! *MUST* be called in an 'loops_cache_rwlock' protected thread context! */
 static void ccgDM_recalcLoopTri(DerivedMesh *dm)
 {
-	BLI_rw_mutex_lock(&loops_cache_rwlock, THREAD_LOCK_WRITE);
-	MLoopTri *mlooptri;
+	MLoopTri *mlooptri = dm->looptris.array;
 	const int tottri = dm->numPolyData * 2;
 	int i, poly_index;
 
@@ -4502,7 +4502,6 @@ static void ccgDM_recalcLoopTri(DerivedMesh *dm)
 		lt->tri[2] = (poly_index * 4) + 2;
 		lt->poly = poly_index;
 	}
-	BLI_rw_mutex_unlock(&loops_cache_rwlock);
 }
 
 static void ccgDM_calcNormals(DerivedMesh *dm)




More information about the Bf-blender-cvs mailing list