Skip to content

Commit

Permalink
Adopted C# 8's expanded unmanaged constraint. Buffer<T> and related c…
Browse files Browse the repository at this point in the history
…ollections updated. Simplified some stuff allowed by unmanaged.
  • Loading branch information
RossNordby committed Jan 27, 2020
1 parent ed92cbd commit 3a1fcf1
Show file tree
Hide file tree
Showing 49 changed files with 247 additions and 230 deletions.
4 changes: 2 additions & 2 deletions BepuPhysics/Bodies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Bodies
/// Gets a reference to the active set, stored in the index 0 of the Sets buffer.
/// </summary>
/// <returns>Reference to the active body set.</returns>
public unsafe ref BodySet ActiveSet { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref Unsafe.As<byte, BodySet>(ref *Sets.Memory); } }
public unsafe ref BodySet ActiveSet { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref *Sets.Memory; } }

//TODO: Having Inertias publicly exposed seems like a recipe for confusion, given its ephemeral nature. We may want to explicitly delete it after frame execution and
//never expose it. If the user really wants an up to date world space inertia, it's pretty easy for them to build it from the local inertia and orientation anyway.
Expand Down Expand Up @@ -1191,7 +1191,7 @@ unsafe void ResizeHandles(int newCapacity)
if (HandleToLocation.Length > oldCapacity)
{
Unsafe.InitBlockUnaligned(
((BodyLocation*)HandleToLocation.Memory) + oldCapacity, 0xFF,
HandleToLocation.Memory + oldCapacity, 0xFF,
(uint)(sizeof(BodyLocation) * (HandleToLocation.Length - oldCapacity)));
}
}
Expand Down
16 changes: 8 additions & 8 deletions BepuPhysics/BodyDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static BodyDescription CreateDynamic(in Vector3 position, in BodyInertia
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexDynamic<TConvexShape>(
in RigidPose pose, in BodyVelocity velocity, float mass, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
var description = new BodyDescription
{
Expand All @@ -162,7 +162,7 @@ public static BodyDescription CreateConvexDynamic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexDynamic<TConvexShape>(
in Vector3 position, in BodyVelocity velocity, float mass, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexDynamic(new RigidPose(position), velocity, mass, shapes, shape);
}
Expand All @@ -178,7 +178,7 @@ public static BodyDescription CreateConvexDynamic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexDynamic<TConvexShape>(
in RigidPose pose, float mass, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexDynamic(pose, default, mass, shapes, shape);
}
Expand All @@ -194,7 +194,7 @@ public static BodyDescription CreateConvexDynamic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexDynamic<TConvexShape>(
in Vector3 position, float mass, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexDynamic(new RigidPose(position), default, mass, shapes, shape);
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public static BodyDescription CreateKinematic(in Vector3 position, in Collidable
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexKinematic<TConvexShape>(
in RigidPose pose, in BodyVelocity velocity, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
var description = new BodyDescription
{
Expand All @@ -283,7 +283,7 @@ public static BodyDescription CreateConvexKinematic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexKinematic<TConvexShape>(
in Vector3 position, in BodyVelocity velocity, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexKinematic(new RigidPose(position), velocity, shapes, shape);
}
Expand All @@ -298,7 +298,7 @@ public static BodyDescription CreateConvexKinematic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexKinematic<TConvexShape>(
in RigidPose pose, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexKinematic(pose, default, shapes, shape);
}
Expand All @@ -313,7 +313,7 @@ public static BodyDescription CreateConvexKinematic<TConvexShape>(
/// <returns>Constructed description for the body.</returns>
public static BodyDescription CreateConvexKinematic<TConvexShape>(
in Vector3 position, Shapes shapes, in TConvexShape shape)
where TConvexShape : struct, IConvexShape
where TConvexShape : unmanaged, IConvexShape
{
return CreateConvexKinematic(new RigidPose(position), default, shapes, shape);
}
Expand Down
2 changes: 1 addition & 1 deletion BepuPhysics/BodyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BepuPhysics
/// </summary>
/// <typeparam name="T">Type of the data to store.</typeparam>
/// <remarks>This is built for use cases relying on random access like the narrow phase. For maximum performance with sequential access, an index-aligned structure would be better.</remarks>
public class BodyProperty<T> : IDisposable where T : struct
public class BodyProperty<T> : IDisposable where T : unmanaged
{
Bodies bodies;
BufferPool pool;
Expand Down
2 changes: 1 addition & 1 deletion BepuPhysics/Collidables/BigCompound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ unsafe struct LeafTester<TRayHitHandler> : IRayLeafTester where TRayHitHandler :
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public LeafTester(in Buffer<CompoundChild> children, Shapes shapes, in TRayHitHandler handler, in Matrix3x3 orientation, in RayData originalRay)
{
Children = (CompoundChild*)children.Memory;
Children = children.Memory;
Shapes = shapes;
HitRotator.HitHandler = handler;
HitRotator.Orientation = orientation;
Expand Down
6 changes: 3 additions & 3 deletions BepuPhysics/Collidables/BoundingBoxBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public unsafe BoundingBoxBatcher(Bodies bodies, Shapes shapes, BroadPhase broadP
maximumBatchIndex = -1;
}

public unsafe void ExecuteConvexBatch<TShape, TShapeWide>(ConvexShapeBatch<TShape, TShapeWide> shapeBatch) where TShape : struct, IConvexShape where TShapeWide : struct, IShapeWide<TShape>
public unsafe void ExecuteConvexBatch<TShape, TShapeWide>(ConvexShapeBatch<TShape, TShapeWide> shapeBatch) where TShape : unmanaged, IConvexShape where TShapeWide : unmanaged, IShapeWide<TShape>
{
var instanceBundle = default(BoundingBoxInstanceWide<TShape, TShapeWide>);
if (instanceBundle.Shape.InternalAllocationSize > 0) //TODO: Check to make sure the JIT omits the branch.
Expand Down Expand Up @@ -184,7 +184,7 @@ public unsafe void ExecuteConvexBatch<TShape, TShapeWide>(ConvexShapeBatch<TShap
}

public unsafe void ExecuteHomogeneousCompoundBatch<TShape, TChildShape, TChildShapeWide>(HomogeneousCompoundShapeBatch<TShape, TChildShape, TChildShapeWide> shapeBatch)
where TShape : struct, IHomogeneousCompoundShape<TChildShape, TChildShapeWide>
where TShape : unmanaged, IHomogeneousCompoundShape<TChildShape, TChildShapeWide>
where TChildShape : IConvexShape
where TChildShapeWide : IShapeWide<TChildShape>
{
Expand Down Expand Up @@ -212,7 +212,7 @@ public unsafe void ExecuteHomogeneousCompoundBatch<TShape, TChildShape, TChildSh
}
}

public unsafe void ExecuteCompoundBatch<TShape>(CompoundShapeBatch<TShape> shapeBatch) where TShape : struct, ICompoundShape
public unsafe void ExecuteCompoundBatch<TShape>(CompoundShapeBatch<TShape> shapeBatch) where TShape : unmanaged, ICompoundShape
{
ref var batch = ref batches[shapeBatch.TypeId];
ref var activeSet = ref bodies.ActiveSet;
Expand Down
4 changes: 2 additions & 2 deletions BepuPhysics/Collidables/CompoundHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CompoundBuilder(BufferPool pool, Shapes shapes, int builderCapacity)
/// <param name="localPose">Pose of the shape in the compound's local space.</param>
/// <param name="weight">Weight of the shape. If the compound is interpreted as a dynamic, this will be used as the mass and scales the inertia tensor.
/// Otherwise, it is used for recentering.</param>
public void Add<TShape>(in TShape shape, in RigidPose localPose, float weight) where TShape : struct, IConvexShape
public void Add<TShape>(in TShape shape, in RigidPose localPose, float weight) where TShape : unmanaged, IConvexShape
{
ref var child = ref Children.Allocate(Pool);
child.LocalPose = localPose;
Expand All @@ -67,7 +67,7 @@ public void Add<TShape>(in TShape shape, in RigidPose localPose, float weight) w
/// <param name="shape">Shape to add.</param>
/// <param name="localPose">Pose of the shape in the compound's local space.</param>
/// <param name="weight">Weight of the shape. If the compound is interpreted as a dynamic, this will be used as the mass. Otherwise, it is used for recentering.</param>
public void AddForKinematic<TShape>(in TShape shape, in RigidPose localPose, float weight) where TShape : struct, IConvexShape
public void AddForKinematic<TShape>(in TShape shape, in RigidPose localPose, float weight) where TShape : unmanaged, IConvexShape
{
ref var child = ref Children.Allocate(Pool);
child.LocalPose = localPose;
Expand Down
4 changes: 2 additions & 2 deletions BepuPhysics/Collidables/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public unsafe void TestLeaf(int leafIndex, RayData* rayData, float* maximumT)
public unsafe void RayTest<TRayHitHandler>(in RigidPose pose, in RayData ray, ref float maximumT, ref TRayHitHandler hitHandler) where TRayHitHandler : struct, IShapeRayHitHandler
{
HitLeafTester<TRayHitHandler> leafTester;
leafTester.Triangles = (Triangle*)Triangles.Memory;
leafTester.Triangles = Triangles.Memory;
leafTester.HitHandler = hitHandler;
Matrix3x3.CreateFromQuaternion(pose.Orientation, out leafTester.Orientation);
leafTester.InverseScale = inverseScale;
Expand All @@ -208,7 +208,7 @@ public unsafe void RayTest<TRayHitHandler>(in RigidPose pose, in RayData ray, re
public unsafe void RayTest<TRayHitHandler>(in RigidPose pose, ref RaySource rays, ref TRayHitHandler hitHandler) where TRayHitHandler : struct, IShapeRayHitHandler
{
HitLeafTester<TRayHitHandler> leafTester;
leafTester.Triangles = (Triangle*)Triangles.Memory;
leafTester.Triangles = Triangles.Memory;
leafTester.HitHandler = hitHandler;
Matrix3x3.CreateFromQuaternion(pose.Orientation, out leafTester.Orientation);
Matrix3x3.Transpose(leafTester.Orientation, out var inverseOrientation);
Expand Down
14 changes: 7 additions & 7 deletions BepuPhysics/Collidables/Shapes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void ResizeIdPool(int targetIdCapacity)

}

public abstract class ShapeBatch<TShape> : ShapeBatch where TShape : struct, IShape//TODO: When blittable is supported, shapes should be made blittable. We store them in buffers.
public abstract class ShapeBatch<TShape> : ShapeBatch where TShape : unmanaged, IShape
{
internal Buffer<TShape> shapes;

Expand Down Expand Up @@ -199,8 +199,8 @@ public override void Dispose()


public class ConvexShapeBatch<TShape, TShapeWide> : ShapeBatch<TShape>
where TShape : struct, IConvexShape
where TShapeWide : struct, IShapeWide<TShape>
where TShape : unmanaged, IConvexShape
where TShapeWide : unmanaged, IShapeWide<TShape>
{
public ConvexShapeBatch(BufferPool pool, int initialShapeCount) : base(pool, initialShapeCount)
{
Expand Down Expand Up @@ -262,7 +262,7 @@ protected override void Dispose(int index, BufferPool pool)
}


public class HomogeneousCompoundShapeBatch<TShape, TChildShape, TChildShapeWide> : ShapeBatch<TShape> where TShape : struct, IHomogeneousCompoundShape<TChildShape, TChildShapeWide>
public class HomogeneousCompoundShapeBatch<TShape, TChildShape, TChildShapeWide> : ShapeBatch<TShape> where TShape : unmanaged, IHomogeneousCompoundShape<TChildShape, TChildShapeWide>
where TChildShape : IConvexShape
where TChildShapeWide : IShapeWide<TChildShape>
{
Expand Down Expand Up @@ -303,7 +303,7 @@ public override void RayTest<TRayHitHandler>(int shapeIndex, in RigidPose pose,
}
}

public class CompoundShapeBatch<TShape> : ShapeBatch<TShape> where TShape : struct, ICompoundShape
public class CompoundShapeBatch<TShape> : ShapeBatch<TShape> where TShape : unmanaged, ICompoundShape
{
Shapes shapeBatches;

Expand Down Expand Up @@ -390,14 +390,14 @@ public void UpdateBounds(in RigidPose pose, ref TypedIndex shapeIndex, out Bound
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref TShape GetShape<TShape>(int shapeIndex) where TShape : struct, IShape
public ref TShape GetShape<TShape>(int shapeIndex) where TShape : unmanaged, IShape
{
var typeId = default(TShape).TypeId;
return ref Unsafe.As<ShapeBatch, ShapeBatch<TShape>>(ref batches[typeId])[shapeIndex];
}


public TypedIndex Add<TShape>(in TShape shape) where TShape : struct, IShape
public TypedIndex Add<TShape>(in TShape shape) where TShape : unmanaged, IShape
{
var typeId = default(TShape).TypeId;
if (RegisteredTypeSpan <= typeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public PairContinuation(int pairId)
public int ChildIndex { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return (int)(Packed & ((1 << 18) - 1)); } }
}

public struct BatcherContinuations<T> where T : struct, ICollisionTestContinuation
public struct BatcherContinuations<T> where T : unmanaged, ICollisionTestContinuation
{
public Buffer<T> Continuations;
public IdPool IdPool;
Expand Down
Loading

0 comments on commit 3a1fcf1

Please sign in to comment.