BulletSim: add locking around Meshmerizer use to eliminate possible race

condition when extracting the convex hulls.
user_profiles
Robert Adams 2013-05-23 14:40:16 -07:00
parent 5efce21abc
commit 29b3b44fab
1 changed files with 170 additions and 158 deletions

View File

@ -441,10 +441,14 @@ public class BSShapeMesh : BSShape
{ {
BulletShape newShape = new BulletShape(); BulletShape newShape = new BulletShape();
IMesh meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod, IMesh meshData = null;
lock (physicsScene.mesher)
{
meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod,
false, // say it is not physical so a bounding box is not built false, // say it is not physical so a bounding box is not built
false // do not cache the mesh and do not use previously built versions false // do not cache the mesh and do not use previously built versions
); );
}
if (meshData != null) if (meshData != null)
{ {
@ -576,17 +580,30 @@ public class BSShapeHull : BSShape
BulletShape newShape = new BulletShape(); BulletShape newShape = new BulletShape();
newShape.shapeKey = newHullKey; newShape.shapeKey = newHullKey;
IMesh meshData = null;
List<List<OMV.Vector3>> allHulls = null;
lock (physicsScene.mesher)
{
// Pass true for physicalness as this prevents the creation of bounding box which is not needed // Pass true for physicalness as this prevents the creation of bounding box which is not needed
IMesh meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod, true /* isPhysical */, false /* shouldCache */); meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod, true /* isPhysical */, false /* shouldCache */);
// If there is hull data in the mesh asset, build the hull from that // If we should use the asset's hull info, fetch it out of the locked mesher
if (meshData != null && BSParam.ShouldUseAssetHulls) if (meshData != null && BSParam.ShouldUseAssetHulls)
{ {
Meshmerizer realMesher = physicsScene.mesher as Meshmerizer; Meshmerizer realMesher = physicsScene.mesher as Meshmerizer;
if (realMesher != null) if (realMesher != null)
{ {
List<List<OMV.Vector3>> allHulls = realMesher.GetConvexHulls(size); allHulls = realMesher.GetConvexHulls(size);
if (allHulls != null) }
if (allHulls == null)
{
physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,assetHulls,noAssetHull", prim.LocalID);
}
}
}
// If there is hull data in the mesh asset, build the hull from that
if (allHulls != null && BSParam.ShouldUseAssetHulls)
{ {
int hullCount = allHulls.Count; int hullCount = allHulls.Count;
int totalVertices = 1; // include one for the count of the hulls int totalVertices = 1; // include one for the count of the hulls
@ -623,8 +640,7 @@ public class BSShapeHull : BSShape
physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,assetHulls,hulls={1},totVert={2},shape={3}", physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,assetHulls,hulls={1},totVert={2},shape={3}",
prim.LocalID, hullCount, totalVertices, newShape); prim.LocalID, hullCount, totalVertices, newShape);
} }
}
}
// If no hull specified in the asset and we should use Bullet's HACD approximation... // If no hull specified in the asset and we should use Bullet's HACD approximation...
if (!newShape.HasPhysicalShape && BSParam.ShouldUseBulletHACD) if (!newShape.HasPhysicalShape && BSParam.ShouldUseBulletHACD)
{ {
@ -655,11 +671,9 @@ public class BSShapeHull : BSShape
} }
physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,bulletHACD,exit,hasBody={1}", prim.LocalID, newShape.HasPhysicalShape); physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,bulletHACD,exit,hasBody={1}", prim.LocalID, newShape.HasPhysicalShape);
} }
// If no hull specified, use our HACD hull approximation.
if (!newShape.HasPhysicalShape) // If no other hull specifications, use our HACD hull approximation.
{ if (!newShape.HasPhysicalShape && meshData != null)
// Build a new hull in the physical world using the C# HACD algorigthm.
if (meshData != null)
{ {
if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.Fetched) if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.Fetched)
{ {
@ -768,8 +782,6 @@ public class BSShapeHull : BSShape
// create the hull data structure in Bullet // create the hull data structure in Bullet
newShape = physicsScene.PE.CreateHullShape(physicsScene.World, hullCount, convHulls); newShape = physicsScene.PE.CreateHullShape(physicsScene.World, hullCount, convHulls);
} }
newShape.shapeKey = newHullKey;
}
return newShape; return newShape;
} }
// Callback from convex hull creater with a newly created hull. // Callback from convex hull creater with a newly created hull.