Jelajahi Sumber

alloca -> JPH_STACK_ALLOC

Using vector in MutableCompoundShape instead of custom calls to realloc
Jorrit Rouwe 3 tahun lalu
induk
melakukan
8456ffa4e5

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

@@ -1058,8 +1058,8 @@ public:
 
 		// Allocate space for vertices and 'no collision' flags
 		int array_size = Square(block_size_plus_1);
-		Vec3 *vertices = reinterpret_cast<Vec3 *>(alloca(array_size * sizeof(Vec3)));
-		bool *no_collision = reinterpret_cast<bool *>(alloca(array_size * sizeof(bool)));
+		Vec3 *vertices = reinterpret_cast<Vec3 *>(JPH_STACK_ALLOC(array_size * sizeof(Vec3)));
+		bool *no_collision = reinterpret_cast<bool *>(JPH_STACK_ALLOC(array_size * sizeof(bool)));
 
 		// Splat offsets
 		Vec4 ox = mShape->mOffset.SplatX();

+ 5 - 15
Jolt/Physics/Collision/Shape/MutableCompoundShape.cpp

@@ -54,12 +54,6 @@ MutableCompoundShape::MutableCompoundShape(const MutableCompoundShapeSettings &i
 	outResult.Set(this);
 }
 
-MutableCompoundShape::~MutableCompoundShape()
-{
-	// Free our bounds
-	free(mSubShapeBounds);
-}
-
 void MutableCompoundShape::AdjustCenterOfMass()
 {
 	// First calculate the delta of the center of mass
@@ -88,7 +82,7 @@ void MutableCompoundShape::CalculateLocalBounds()
 	if (num_blocks > 0)
 	{
 		// Initialize min/max for first block
-		const Bounds *bounds = mSubShapeBounds;
+		const Bounds *bounds = mSubShapeBounds.data();
 		Vec4 min_x = bounds->mMinX;
 		Vec4 min_y = bounds->mMinY;
 		Vec4 min_z = bounds->mMinZ;
@@ -130,12 +124,8 @@ void MutableCompoundShape::EnsureSubShapeBoundsCapacity()
 {
 	// Check if we have enough space
 	uint new_capacity = ((uint)mSubShapes.size() + 3) >> 2;
-	if (mSubShapeBoundsCapacity < new_capacity)
-	{
-		uint new_size = new_capacity * sizeof(Bounds);
-		mSubShapeBounds = reinterpret_cast<Bounds *>(realloc(mSubShapeBounds, new_size));
-		mSubShapeBoundsCapacity = new_capacity;
-	}
+	if (mSubShapeBounds.size() < new_capacity)
+		mSubShapeBounds.resize(new_capacity);
 }
 
 void MutableCompoundShape::CalculateSubShapeBounds(uint inStartIdx, uint inNumber)
@@ -525,7 +515,7 @@ void MutableCompoundShape::SaveBinaryState(StreamOut &inStream) const
 
 	// Write bounds
 	uint bounds_size = (((uint)mSubShapes.size() + 3) >> 2) * sizeof(Bounds);
-	inStream.WriteBytes(mSubShapeBounds, bounds_size);
+	inStream.WriteBytes(mSubShapeBounds.data(), bounds_size);
 }
 
 void MutableCompoundShape::RestoreBinaryState(StreamIn &inStream)
@@ -537,7 +527,7 @@ void MutableCompoundShape::RestoreBinaryState(StreamIn &inStream)
 
 	// Read bounds
 	uint bounds_size = (((uint)mSubShapes.size() + 3) >> 2) * sizeof(Bounds);
-	inStream.ReadBytes(mSubShapeBounds, bounds_size);
+	inStream.ReadBytes(mSubShapeBounds.data(), bounds_size);
 }
 
 void MutableCompoundShape::sRegister()

+ 2 - 4
Jolt/Physics/Collision/Shape/MutableCompoundShape.h

@@ -32,7 +32,6 @@ public:
 	/// Constructor
 									MutableCompoundShape() : CompoundShape(EShapeSubType::MutableCompound) { }
 									MutableCompoundShape(const MutableCompoundShapeSettings &inSettings, ShapeResult &outResult);
-	virtual							~MutableCompoundShape() override;
 		
 	// See Shape::CastRay
 	virtual bool					CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
@@ -54,7 +53,7 @@ public:
 	virtual void					SaveBinaryState(StreamOut &inStream) const override;
 
 	// See Shape::GetStats
-	virtual Stats					GetStats() const override								{ return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mSubShapeBoundsCapacity * sizeof(Bounds), 0); }
+	virtual Stats					GetStats() const override								{ return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mSubShapeBounds.size() * sizeof(Bounds), 0); }
 
 	///@{
 	/// @name Mutating shapes. Note that this is not thread safe, so you need to ensure that any bodies that use this shape are locked at the time of modification using BodyLockWrite. After modification you need to call BodyInterface::NotifyShapeChanged to update the broadphase and collision caches.
@@ -151,8 +150,7 @@ private:
 		Vec4						mMaxZ;
 	};
 
-	Bounds *						mSubShapeBounds = nullptr;									///< Bounding boxes of all sub shapes in SOA format (in blocks of 4 boxes), MinX 0..3, MinY 0..3, MinZ 0..3, MaxX 0..3, MaxY 0..3, MaxZ 0..3, MinX 4..7, MinY 4..7, ...
-	uint							mSubShapeBoundsCapacity = 0;								///< Number of Bounds structures in mSubShapeBounds
+	vector<Bounds>					mSubShapeBounds;											///< Bounding boxes of all sub shapes in SOA format (in blocks of 4 boxes), MinX 0..3, MinY 0..3, MinZ 0..3, MaxX 0..3, MaxY 0..3, MaxZ 0..3, MinX 4..7, MinY 4..7, ...
 };
 
 JPH_NAMESPACE_END

+ 1 - 1
Jolt/Physics/Vehicle/VehicleConstraint.cpp

@@ -223,7 +223,7 @@ void VehicleConstraint::OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem
 void VehicleConstraint::BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) 
 {
 	// Find dynamic bodies that our wheels are touching
-	BodyID *body_ids = (BodyID *)alloca((mWheels.size() + 1) * sizeof(BodyID));
+	BodyID *body_ids = (BodyID *)JPH_STACK_ALLOC((mWheels.size() + 1) * sizeof(BodyID));
 	int num_bodies = 0;
 	bool needs_to_activate = false;
 	for (const Wheel *w : mWheels)