Skip to content

Commit

Permalink
Fix poly pooling (#5645)
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth authored Feb 1, 2025
1 parent bdef9e3 commit 1bf0687
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Robust.Shared/Physics/Collision/DistanceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ internal void Set<T>(T shape, int index) where T : IPhysShape
case ShapeType.Polygon:
if (shape is Polygon poly)
{
Vertices = poly.Vertices;
Vertices = poly.Vertices.AsSpan()[..poly.VertexCount];
Radius = poly.Radius;
}
else
{
var polyShape = Unsafe.As<PolygonShape>(shape);
Vertices = polyShape.Vertices;
Vertices = polyShape.Vertices.AsSpan()[..polyShape.VertexCount];
Radius = polyShape.Radius;
}

Expand Down
10 changes: 8 additions & 2 deletions Robust.Shared/Physics/Shapes/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal record struct Polygon : IPhysShape
[DataField]
public Vector2[] Vertices;

public int VertexCount => Vertices.Length;
public byte VertexCount;

public Vector2[] Normals;

Expand All @@ -42,17 +42,19 @@ public Polygon(PolygonShape polyShape)

Array.Copy(polyShape.Vertices, Vertices, Vertices.Length);
Array.Copy(polyShape.Normals, Normals, Vertices.Length);
VertexCount = (byte) Vertices.Length;
}

/// <summary>
/// Manually constructed polygon for internal use to take advantage of pooling.
/// </summary>
internal Polygon(Vector2[] vertices, Vector2[] normals, Vector2 centroid)
internal Polygon(Vector2[] vertices, Vector2[] normals, Vector2 centroid, byte count)
{
Unsafe.SkipInit(out this);
Vertices = vertices;
Normals = normals;
Centroid = centroid;
VertexCount = count;
}

public Polygon(Box2 aabb)
Expand All @@ -72,6 +74,7 @@ public Polygon(Box2 aabb)
Normals[2] = new Vector2(0.0f, 1.0f);
Normals[3] = new Vector2(-1.0f, 0.0f);

VertexCount = 4;
Centroid = aabb.Center;
}

Expand All @@ -89,6 +92,7 @@ public Polygon(Box2Rotated bounds)

CalculateNormals(Normals, 4);

VertexCount = 4;
Centroid = bounds.Center;
}

Expand All @@ -99,11 +103,13 @@ public Polygon(Vector2[] vertices)

if (hull.Count < 3)
{
VertexCount = 0;
Vertices = [];
Normals = [];
return;
}

VertexCount = (byte) vertices.Length;
Vertices = vertices;
Normals = new Vector2[vertices.Length];
Set(hull);
Expand Down
19 changes: 9 additions & 10 deletions Robust.Shared/Physics/Systems/SharedPhysicsSystem.Pool.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System.Buffers;
using System.Numerics;
using Microsoft.Extensions.ObjectPool;
using Robust.Shared.Maths;
using Robust.Shared.Physics.Shapes;

namespace Robust.Shared.Physics.Systems;

public abstract partial class SharedPhysicsSystem
{
private ArrayPool<Vector2> _vectorPool = ArrayPool<Vector2>.Create(4, 128);

/// <summary>
/// Gets a polygon with pooled arrays backing it.
/// </summary>
internal Polygon GetPooled(Box2 box)
{
var vertices = _vectorPool.Rent(4);
var normals = _vectorPool.Rent(4);
var vertices = ArrayPool<Vector2>.Shared.Rent(4);
var normals = ArrayPool<Vector2>.Shared.Rent(4);
var centroid = box.Center;

vertices[0] = box.BottomLeft;
Expand All @@ -28,29 +27,29 @@ internal Polygon GetPooled(Box2 box)
normals[2] = new Vector2(0.0f, 1.0f);
normals[3] = new Vector2(-1.0f, 0.0f);

return new Polygon(vertices, normals, centroid);
return new Polygon(vertices, normals, centroid, 4);
}

internal Polygon GetPooled(Box2Rotated box)
{
var vertices = _vectorPool.Rent(4);
var normals = _vectorPool.Rent(4);
var vertices = ArrayPool<Vector2>.Shared.Rent(4);
var normals = ArrayPool<Vector2>.Shared.Rent(4);
var centroid = box.Center;

vertices[0] = box.BottomLeft;
vertices[1] = box.BottomRight;
vertices[2] = box.TopRight;
vertices[3] = box.TopLeft;

var polygon = new Polygon(vertices, normals, centroid);
var polygon = new Polygon(vertices, normals, centroid, 4);
polygon.CalculateNormals(normals, 4);

return polygon;
}

internal void ReturnPooled(Polygon polygon)
{
_vectorPool.Return(polygon.Vertices);
_vectorPool.Return(polygon.Normals);
ArrayPool<Vector2>.Shared.Return(polygon.Vertices);
ArrayPool<Vector2>.Shared.Return(polygon.Normals);
}
}

0 comments on commit 1bf0687

Please sign in to comment.