Browse Source

Changed RestoreMaterialState and RestoreSubShapeState to use pointers instead of vectors to allow loading shapes with fewer memory allocations (#12)

jrouwe 3 years ago
parent
commit
b8953791f3

+ 3 - 3
Jolt/Physics/Collision/Shape/CompoundShape.cpp

@@ -329,10 +329,10 @@ void CompoundShape::SaveSubShapeState(ShapeList &outSubShapes) const
 		outSubShapes.push_back(shape.mShape);
 		outSubShapes.push_back(shape.mShape);
 }
 }
 
 
-void CompoundShape::RestoreSubShapeState(const ShapeList &inSubShapes)
+void CompoundShape::RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes)
 { 
 { 
-	JPH_ASSERT(inSubShapes.size() == mSubShapes.size());
-	for (size_t i = 0; i < mSubShapes.size(); ++i)
+	JPH_ASSERT(mSubShapes.size() == inNumShapes);
+	for (uint i = 0; i < inNumShapes; ++i)
 		mSubShapes[i].mShape = inSubShapes[i];
 		mSubShapes[i].mShape = inSubShapes[i];
 }
 }
 
 

+ 1 - 1
Jolt/Physics/Collision/Shape/CompoundShape.h

@@ -290,7 +290,7 @@ public:
 	// See Shape
 	// See Shape
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const override;
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const override;
-	virtual void					RestoreSubShapeState(const ShapeList &inSubShapes) override;
+	virtual void					RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes) override;
 
 
 	// See Shape::GetStatsRecursive
 	// See Shape::GetStatsRecursive
 	virtual Stats					GetStatsRecursive(VisitedShapes &ioVisitedShapes) const override;
 	virtual Stats					GetStatsRecursive(VisitedShapes &ioVisitedShapes) const override;

+ 2 - 2
Jolt/Physics/Collision/Shape/ConvexShape.cpp

@@ -576,9 +576,9 @@ void ConvexShape::SaveMaterialState(PhysicsMaterialList &outMaterials) const
 	outMaterials = { mMaterial }; 
 	outMaterials = { mMaterial }; 
 }
 }
 
 
-void ConvexShape::RestoreMaterialState(const PhysicsMaterialList &inMaterials) 
+void ConvexShape::RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) 
 { 
 { 
-	JPH_ASSERT(inMaterials.size() == 1); 
+	JPH_ASSERT(inNumMaterials == 1); 
 	mMaterial = inMaterials[0]; 
 	mMaterial = inMaterials[0]; 
 }
 }
 
 

+ 1 - 1
Jolt/Physics/Collision/Shape/ConvexShape.h

@@ -148,7 +148,7 @@ public:
 	// See Shape
 	// See Shape
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
-	virtual void					RestoreMaterialState(const PhysicsMaterialList &inMaterials) override;
+	virtual void					RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
 
 
 protected:
 protected:
 	// See: Shape::RestoreBinaryState
 	// See: Shape::RestoreBinaryState

+ 2 - 2
Jolt/Physics/Collision/Shape/DecoratedShape.cpp

@@ -64,9 +64,9 @@ void DecoratedShape::SaveSubShapeState(ShapeList &outSubShapes) const
 	outSubShapes.push_back(mInnerShape);
 	outSubShapes.push_back(mInnerShape);
 }
 }
 
 
-void DecoratedShape::RestoreSubShapeState(const ShapeList &inSubShapes)
+void DecoratedShape::RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes)
 { 
 { 
-	JPH_ASSERT(inSubShapes.size() == 1);
+	JPH_ASSERT(inNumShapes == 1);
 	mInnerShape = inSubShapes[0];
 	mInnerShape = inSubShapes[0];
 }
 }
 
 

+ 1 - 1
Jolt/Physics/Collision/Shape/DecoratedShape.h

@@ -51,7 +51,7 @@ public:
 
 
 	// See Shape
 	// See Shape
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const override;
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const override;
-	virtual void					RestoreSubShapeState(const ShapeList &inSubShapes) override;
+	virtual void					RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes) override;
 
 
 	// See Shape::GetStatsRecursive
 	// See Shape::GetStatsRecursive
 	virtual Stats					GetStatsRecursive(VisitedShapes &ioVisitedShapes) const override;
 	virtual Stats					GetStatsRecursive(VisitedShapes &ioVisitedShapes) const override;

+ 2 - 2
Jolt/Physics/Collision/Shape/HeightFieldShape.cpp

@@ -1708,9 +1708,9 @@ void HeightFieldShape::SaveMaterialState(PhysicsMaterialList &outMaterials) cons
 	outMaterials = mMaterials;
 	outMaterials = mMaterials;
 }
 }
 
 
-void HeightFieldShape::RestoreMaterialState(const PhysicsMaterialList &inMaterials) 
+void HeightFieldShape::RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) 
 { 
 { 
-	mMaterials = inMaterials;
+	mMaterials.assign(inMaterials, inMaterials + inNumMaterials);
 }
 }
 
 
 Shape::Stats HeightFieldShape::GetStats() const 
 Shape::Stats HeightFieldShape::GetStats() const 

+ 1 - 1
Jolt/Physics/Collision/Shape/HeightFieldShape.h

@@ -165,7 +165,7 @@ public:
 	// See Shape
 	// See Shape
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
-	virtual void					RestoreMaterialState(const PhysicsMaterialList &inMaterials) override;
+	virtual void					RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
 
 
 	// See Shape::GetStats
 	// See Shape::GetStats
 	virtual Stats					GetStats() const override;
 	virtual Stats					GetStats() const override;

+ 2 - 2
Jolt/Physics/Collision/Shape/MeshShape.cpp

@@ -1067,9 +1067,9 @@ void MeshShape::SaveMaterialState(PhysicsMaterialList &outMaterials) const
 	outMaterials = mMaterials;
 	outMaterials = mMaterials;
 }
 }
 
 
-void MeshShape::RestoreMaterialState(const PhysicsMaterialList &inMaterials) 
+void MeshShape::RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) 
 { 
 { 
-	mMaterials = inMaterials;
+	mMaterials.assign(inMaterials, inMaterials + inNumMaterials);
 }
 }
 
 
 Shape::Stats MeshShape::GetStats() const
 Shape::Stats MeshShape::GetStats() const

+ 1 - 1
Jolt/Physics/Collision/Shape/MeshShape.h

@@ -108,7 +108,7 @@ public:
 	// See Shape
 	// See Shape
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
-	virtual void					RestoreMaterialState(const PhysicsMaterialList &inMaterials) override;
+	virtual void					RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
 
 
 	// See Shape::GetStats
 	// See Shape::GetStats
 	virtual Stats					GetStats() const override;
 	virtual Stats					GetStats() const override;

+ 2 - 2
Jolt/Physics/Collision/Shape/Shape.cpp

@@ -217,7 +217,7 @@ Shape::ShapeResult Shape::sRestoreWithChildren(StreamIn &inStream, IDToShapeMap
 			return sub_shape_result;
 			return sub_shape_result;
 		sub_shapes.push_back(sub_shape_result.Get());
 		sub_shapes.push_back(sub_shape_result.Get());
 	}
 	}
-	result.Get()->RestoreSubShapeState(sub_shapes);
+	result.Get()->RestoreSubShapeState(sub_shapes.data(), (uint)sub_shapes.size());
 
 
 	// Read the materials
 	// Read the materials
 	inStream.Read(len);
 	inStream.Read(len);
@@ -259,7 +259,7 @@ Shape::ShapeResult Shape::sRestoreWithChildren(StreamIn &inStream, IDToShapeMap
 
 
 		materials.push_back(material);
 		materials.push_back(material);
 	}
 	}
-	result.Get()->RestoreMaterialState(materials);
+	result.Get()->RestoreMaterialState(materials.data(), (uint)materials.size());
 
 
 	return result;
 	return result;
 }
 }

+ 6 - 4
Jolt/Physics/Collision/Shape/Shape.h

@@ -41,8 +41,10 @@ using CollidePointCollector = CollisionCollector<CollidePointResult, CollisionCo
 using CollideShapeCollector = CollisionCollector<CollideShapeResult, CollisionCollectorTraitsCollideShape>;
 using CollideShapeCollector = CollisionCollector<CollideShapeResult, CollisionCollectorTraitsCollideShape>;
 using TransformedShapeCollector = CollisionCollector<TransformedShape, CollisionCollectorTraitsCollideShape>;
 using TransformedShapeCollector = CollisionCollector<TransformedShape, CollisionCollectorTraitsCollideShape>;
 
 
-using ShapeList = vector<RefConst<Shape>>;
-using PhysicsMaterialList = vector<RefConst<PhysicsMaterial>>;
+using ShapeRefC = RefConst<Shape>;
+using ShapeList = vector<ShapeRefC>;
+using PhysicsMaterialRefC = RefConst<PhysicsMaterial>;
+using PhysicsMaterialList = vector<PhysicsMaterialRefC>;
 
 
 /// Shapes are categorized in groups, each shape can return which group it belongs to through its Shape::GetType function.
 /// Shapes are categorized in groups, each shape can return which group it belongs to through its Shape::GetType function.
 enum class EShapeType
 enum class EShapeType
@@ -240,13 +242,13 @@ public:
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const			{ }
 	virtual void					SaveMaterialState(PhysicsMaterialList &outMaterials) const			{ }
 
 
 	/// Restore the material references after calling sRestoreFromBinaryState. Note that the exact same materials need to be provided in the same order as returned by SaveMaterialState.
 	/// Restore the material references after calling sRestoreFromBinaryState. Note that the exact same materials need to be provided in the same order as returned by SaveMaterialState.
-	virtual void					RestoreMaterialState(const PhysicsMaterialList &inMaterials)		{ JPH_ASSERT(inMaterials.empty()); }
+	virtual void					RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) { JPH_ASSERT(inNumMaterials == 0); }
 
 
 	/// Outputs the shape references that this shape has to outSubShapes.
 	/// Outputs the shape references that this shape has to outSubShapes.
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const					{ }
 	virtual void					SaveSubShapeState(ShapeList &outSubShapes) const					{ }
 
 
 	/// Restore the shape references after calling sRestoreFromBinaryState. Note that the exact same shapes need to be provided in the same order as returned by SaveSubShapeState.
 	/// Restore the shape references after calling sRestoreFromBinaryState. Note that the exact same shapes need to be provided in the same order as returned by SaveSubShapeState.
-	virtual void					RestoreSubShapeState(const ShapeList &inSubShapes)					{ JPH_ASSERT(inSubShapes.empty()); }
+	virtual void					RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes) { JPH_ASSERT(inNumShapes == 0); }
 
 
 	using ShapeToIDMap = unordered_map<const Shape *, uint32>;
 	using ShapeToIDMap = unordered_map<const Shape *, uint32>;
 	using MaterialToIDMap = unordered_map<const PhysicsMaterial *, uint32>;
 	using MaterialToIDMap = unordered_map<const PhysicsMaterial *, uint32>;

+ 1 - 1
Samples/Tests/Shapes/MutableCompoundShapeTest.cpp

@@ -163,7 +163,7 @@ void MutableCompoundShapeTest::RestoreState(StateRecorder &inStream)
 
 
 			// Restore the pointers to the sub compound
 			// Restore the pointers to the sub compound
 			ShapeList sub_shapes(shape->GetNumSubShapes(), mSubCompound);
 			ShapeList sub_shapes(shape->GetNumSubShapes(), mSubCompound);
-			shape->RestoreSubShapeState(sub_shapes);
+			shape->RestoreSubShapeState(sub_shapes.data(), (uint)sub_shapes.size());
 
 
 			// Update the shape (we're under lock protection, so use the no lock interface)
 			// Update the shape (we're under lock protection, so use the no lock interface)
 			mPhysicsSystem->GetBodyInterfaceNoLock().SetShape(id, shape, false, EActivation::DontActivate);
 			mPhysicsSystem->GetBodyInterfaceNoLock().SetShape(id, shape, false, EActivation::DontActivate);