Pārlūkot izejas kodu

Removed some heap allocs on startup (#1092)

These were bypassing the custom memory allocator
Jorrit Rouwe 1 gadu atpakaļ
vecāks
revīzija
d7619bd5ec

+ 26 - 16
Jolt/Math/Vec3.cpp

@@ -4,20 +4,32 @@
 
 #include <Jolt/Jolt.h>
 
-#include <Jolt/Core/UnorderedSet.h>
 #include <Jolt/Math/Vec3.h>
 
 JPH_NAMESPACE_BEGIN
 
-static void sCreateVertices(std::unordered_set<Vec3> &ioVertices, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel)
+static void sAddVertex(StaticArray<Vec3, 1026> &ioVertices, Vec3Arg inVertex)
+{
+	bool found = false;
+	for (const Vec3 &v : ioVertices)
+		if (v == inVertex)
+		{
+			found = true;
+			break;
+		}
+	if (!found)
+		ioVertices.push_back(inVertex);
+}
+
+static void sCreateVertices(StaticArray<Vec3, 1026> &ioVertices, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel)
 {
 	Vec3 center1 = (inDir1 + inDir2).Normalized();
 	Vec3 center2 = (inDir2 + inDir3).Normalized();
 	Vec3 center3 = (inDir3 + inDir1).Normalized();
 
-	ioVertices.insert(center1);
-	ioVertices.insert(center2);
-	ioVertices.insert(center3);
+	sAddVertex(ioVertices, center1);
+	sAddVertex(ioVertices, center2);
+	sAddVertex(ioVertices, center3);
 
 	if (inLevel > 0)
 	{
@@ -29,19 +41,19 @@ static void sCreateVertices(std::unordered_set<Vec3> &ioVertices, Vec3Arg inDir1
 	}
 }
 
-const Array<Vec3, std::allocator<Vec3>> Vec3::sUnitSphere = []() {
+const StaticArray<Vec3, 1026> Vec3::sUnitSphere = []() {
 
 	const int level = 3;
 
-	std::unordered_set<Vec3> verts;
+	StaticArray<Vec3, 1026> verts;
 
 	// Add unit axis
-	verts.insert(Vec3::sAxisX());
-	verts.insert(-Vec3::sAxisX());
-	verts.insert(Vec3::sAxisY());
-	verts.insert(-Vec3::sAxisY());
-	verts.insert(Vec3::sAxisZ());
-	verts.insert(-Vec3::sAxisZ());
+	verts.push_back(Vec3::sAxisX());
+	verts.push_back(-Vec3::sAxisX());
+	verts.push_back(Vec3::sAxisY());
+	verts.push_back(-Vec3::sAxisY());
+	verts.push_back(Vec3::sAxisZ());
+	verts.push_back(-Vec3::sAxisZ());
 
 	// Subdivide
 	sCreateVertices(verts, Vec3::sAxisX(), Vec3::sAxisY(), Vec3::sAxisZ(), level);
@@ -53,9 +65,7 @@ const Array<Vec3, std::allocator<Vec3>> Vec3::sUnitSphere = []() {
 	sCreateVertices(verts, Vec3::sAxisX(), -Vec3::sAxisY(), -Vec3::sAxisZ(), level);
 	sCreateVertices(verts, -Vec3::sAxisX(), -Vec3::sAxisY(), -Vec3::sAxisZ(), level);
 
-	Array<Vec3, std::allocator<Vec3>> array;
-	array.assign(verts.begin(), verts.end());
-	return array;
+	return verts;
 }();
 
 JPH_NAMESPACE_END

+ 2 - 1
Jolt/Math/Vec3.h

@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <Jolt/Core/StaticArray.h>
 #include <Jolt/Math/Float3.h>
 #include <Jolt/Math/Swizzle.h>
 #include <Jolt/Math/MathTypes.h>
@@ -104,7 +105,7 @@ public:
 	static JPH_INLINE Vec3		sUnitSpherical(float inTheta, float inPhi);
 
 	/// A set of vectors uniformly spanning the surface of a unit sphere, usable for debug purposes
-	JPH_EXPORT static const Array<Vec3, std::allocator<Vec3>> sUnitSphere;
+	JPH_EXPORT static const StaticArray<Vec3, 1026> sUnitSphere;
 
 	/// Get random unit vector
 	template <class Random>

+ 6 - 6
Jolt/Physics/Collision/Shape/CapsuleShape.cpp

@@ -33,20 +33,20 @@ JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(CapsuleShapeSettings)
 
 static const int cCapsuleDetailLevel = 2;
 
-static const Array<Vec3, std::allocator<Vec3>> sCapsuleTopTriangles = []() {
-	Array<Vec3, std::allocator<Vec3>> verts;
+static const StaticArray<Vec3, 192> sCapsuleTopTriangles = []() {
+	StaticArray<Vec3, 192> verts;
 	GetTrianglesContextVertexList::sCreateHalfUnitSphereTop(verts, cCapsuleDetailLevel);
 	return verts;
 }();
 
-static const Array<Vec3, std::allocator<Vec3>> sCapsuleMiddleTriangles = []() {
-	Array<Vec3, std::allocator<Vec3>> verts;
+static const StaticArray<Vec3, 96> sCapsuleMiddleTriangles = []() {
+	StaticArray<Vec3, 96> verts;
 	GetTrianglesContextVertexList::sCreateUnitOpenCylinder(verts, cCapsuleDetailLevel);
 	return verts;
 }();
 
-static const Array<Vec3, std::allocator<Vec3>> sCapsuleBottomTriangles = []() {
-	Array<Vec3, std::allocator<Vec3>> verts;
+static const StaticArray<Vec3, 192> sCapsuleBottomTriangles = []() {
+	StaticArray<Vec3, 192> verts;
 	GetTrianglesContextVertexList::sCreateHalfUnitSphereBottom(verts, cCapsuleDetailLevel);
 	return verts;
 }();

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

@@ -33,10 +33,10 @@ JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(ConvexShapeSettings)
 	JPH_ADD_ATTRIBUTE(ConvexShapeSettings, mMaterial)
 }
 
-const Array<Vec3, std::allocator<Vec3>> ConvexShape::sUnitSphereTriangles = []() {
+const StaticArray<Vec3, 384> ConvexShape::sUnitSphereTriangles = []() {
 	const int level = 2;
 
-	Array<Vec3, std::allocator<Vec3>> verts;
+	StaticArray<Vec3, 384> verts;
 	GetTrianglesContextVertexList::sCreateHalfUnitSphereTop(verts, level);
 	GetTrianglesContextVertexList::sCreateHalfUnitSphereBottom(verts, level);
 	return verts;

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

@@ -132,7 +132,7 @@ protected:
 	virtual void					RestoreBinaryState(StreamIn &inStream) override;
 
 	/// Vertex list that forms a unit sphere
-	static const Array<Vec3, std::allocator<Vec3>> sUnitSphereTriangles;
+	static const StaticArray<Vec3, 384> sUnitSphereTriangles;
 
 private:
 	// Class for GetTrianglesStart/Next

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

@@ -45,8 +45,8 @@ static const Vec3 cTopFace[] =
 	Vec3(-cSin45,	1.0f,	cSin45)
 };
 
-static const Array<Vec3, std::allocator<Vec3>> sUnitCylinderTriangles = []() {
-	Array<Vec3, std::allocator<Vec3>> verts;
+static const StaticArray<Vec3, 96> sUnitCylinderTriangles = []() {
+	StaticArray<Vec3, 96> verts;
 
 	const Vec3 bottom_offset(0.0f, -2.0f, 0.0f);
 

+ 8 - 4
Jolt/Physics/Collision/Shape/GetTrianglesContext.h

@@ -68,7 +68,8 @@ public:
 	}
 
 	/// Helper function that creates a vertex list of a half unit sphere (top part)
-	static void		sCreateHalfUnitSphereTop(Array<Vec3, std::allocator<Vec3>> &ioVertices, int inDetailLevel)
+	template <class A>
+	static void		sCreateHalfUnitSphereTop(A &ioVertices, int inDetailLevel)
 	{
 		sCreateUnitSphereHelper(ioVertices,  Vec3::sAxisX(),  Vec3::sAxisY(),  Vec3::sAxisZ(), inDetailLevel);
 		sCreateUnitSphereHelper(ioVertices,  Vec3::sAxisY(), -Vec3::sAxisX(),  Vec3::sAxisZ(), inDetailLevel);
@@ -77,7 +78,8 @@ public:
 	}
 
 	/// Helper function that creates a vertex list of a half unit sphere (bottom part)
-	static void		sCreateHalfUnitSphereBottom(Array<Vec3, std::allocator<Vec3>> &ioVertices, int inDetailLevel)
+	template <class A>
+	static void		sCreateHalfUnitSphereBottom(A &ioVertices, int inDetailLevel)
 	{
 		sCreateUnitSphereHelper(ioVertices, -Vec3::sAxisX(), -Vec3::sAxisY(),  Vec3::sAxisZ(), inDetailLevel);
 		sCreateUnitSphereHelper(ioVertices, -Vec3::sAxisY(),  Vec3::sAxisX(),  Vec3::sAxisZ(), inDetailLevel);
@@ -86,7 +88,8 @@ public:
 	}
 
 	/// Helper function that creates an open cylinder of half height 1 and radius 1
-	static void		sCreateUnitOpenCylinder(Array<Vec3, std::allocator<Vec3>> &ioVertices, int inDetailLevel)
+	template <class A>
+	static void		sCreateUnitOpenCylinder(A &ioVertices, int inDetailLevel)
 	{
 		const Vec3 bottom_offset(0.0f, -2.0f, 0.0f);
 		int num_verts = 4 * (1 << inDetailLevel);
@@ -112,7 +115,8 @@ public:
 
 private:
 	/// Recursive helper function for creating a sphere
-	static void		sCreateUnitSphereHelper(Array<Vec3, std::allocator<Vec3>> &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, int inLevel)
+	template <class A>
+	static void		sCreateUnitSphereHelper(A &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, int inLevel)
 	{
 		Vec3 center1 = (inV1 + inV2).Normalized();
 		Vec3 center2 = (inV2 + inV3).Normalized();