Skip to content

Commit

Permalink
Port to Framework/NET45
Browse files Browse the repository at this point in the history
* Get rid of netstandard abuse, as far as compiler errors are concerned
* Replace Buffer.MemoryCopy with Unsafe.CopyBlock for NET45
  • Loading branch information
ezdiy committed Aug 5, 2020
1 parent 8384869 commit ba59c97
Show file tree
Hide file tree
Showing 37 changed files with 150 additions and 102 deletions.
44 changes: 34 additions & 10 deletions BepuPhysics/Bodies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ public void GatherInertia(ref Vector<int> references, int count,
Debug.Assert(count >= 0 && count <= Vector<float>.Count);
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references);
inertiaA = default;
for (int i = 0; i < count; ++i)
{
GatherInertiaForBody(ref Inertias[Unsafe.Add(ref baseIndexA, i)], ref GatherScatter.GetOffsetInstance(ref inertiaA, i));
Expand All @@ -567,6 +568,8 @@ public void GatherInertia(ref TwoBodyReferences references, int count, out BodyI
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);
inertiaA = default;
inertiaB = default;
for (int i = 0; i < count; ++i)
{
GatherInertiaForBody(ref Inertias[Unsafe.Add(ref baseIndexA, i)], ref GatherScatter.GetOffsetInstance(ref inertiaA, i));
Expand All @@ -590,6 +593,9 @@ public void GatherInertia(ref ThreeBodyReferences references, int count, out Bod
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);
ref var baseIndexC = ref Unsafe.As<Vector<int>, int>(ref references.IndexC);
inertiaA = default;
inertiaB = default;
inertiaC = default;
for (int i = 0; i < count; ++i)
{
GatherInertiaForBody(ref Inertias[Unsafe.Add(ref baseIndexA, i)], ref GatherScatter.GetOffsetInstance(ref inertiaA, i));
Expand All @@ -616,6 +622,10 @@ public void GatherInertia(ref FourBodyReferences references, int count, out Body
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);
ref var baseIndexC = ref Unsafe.As<Vector<int>, int>(ref references.IndexC);
ref var baseIndexD = ref Unsafe.As<Vector<int>, int>(ref references.IndexD);
inertiaA = default;
inertiaB = default;
inertiaC = default;
inertiaD = default;
for (int i = 0; i < count; ++i)
{
GatherInertiaForBody(ref Inertias[Unsafe.Add(ref baseIndexA, i)], ref GatherScatter.GetOffsetInstance(ref inertiaA, i));
Expand All @@ -642,6 +652,8 @@ public void GatherOrientation(ref TwoBodyReferences references, int count,
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);

ref var poses = ref ActiveSet.Poses;
orientationA = default;
orientationB = default;
for (int i = 0; i < count; ++i)
{
ref var indexA = ref Unsafe.Add(ref baseIndexA, i);
Expand All @@ -667,6 +679,7 @@ public void GatherOrientation(ref Vector<int> references, int count,
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references);

ref var poses = ref ActiveSet.Poses;
orientation = default;
for (int i = 0; i < count; ++i)
{
ref var indexA = ref Unsafe.Add(ref baseIndexA, i);
Expand Down Expand Up @@ -695,6 +708,8 @@ public void GatherPose(ref Vector<int> references, int count, out Vector3Wide po
ref var baseIndex = ref Unsafe.As<Vector<int>, int>(ref references);

ref var poses = ref ActiveSet.Poses;
position = default;
orientation = default;
for (int i = 0; i < count; ++i)
{
ref var indexA = ref Unsafe.Add(ref baseIndex, i);
Expand Down Expand Up @@ -722,7 +737,9 @@ public void GatherPose(ref TwoBodyReferences references, int count,
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);

Vector3Wide positionA, positionB;
Vector3Wide positionA = default, positionB = default;
orientationA = default;
orientationB = default;
ref var poses = ref ActiveSet.Poses;
for (int i = 0; i < count; ++i)
{
Expand Down Expand Up @@ -761,7 +778,7 @@ public void GatherOffsets(ref TwoBodyReferences references, int count, out Vecto
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);

Vector3Wide positionA, positionB;
Vector3Wide positionA = default, positionB = default;
ref var poses = ref ActiveSet.Poses;
for (int i = 0; i < count; ++i)
{
Expand All @@ -788,7 +805,7 @@ public void GatherOffsets(ref ThreeBodyReferences references, int count, out Vec
ref var baseIndexB = ref Unsafe.As<Vector<int>, int>(ref references.IndexB);
ref var baseIndexC = ref Unsafe.As<Vector<int>, int>(ref references.IndexC);

Vector3Wide positionA, positionB, positionC;
Vector3Wide positionA = default, positionB = default, positionC = default;
ref var poses = ref ActiveSet.Poses;
for (int i = 0; i < count; ++i)
{
Expand Down Expand Up @@ -818,7 +835,7 @@ public void GatherOffsets(ref FourBodyReferences references, int count, out Vect
ref var baseIndexC = ref Unsafe.As<Vector<int>, int>(ref references.IndexC);
ref var baseIndexD = ref Unsafe.As<Vector<int>, int>(ref references.IndexD);

Vector3Wide positionA, positionB, positionC, positionD;
Vector3Wide positionA = default, positionB = default, positionC = default, positionD = default;
ref var poses = ref ActiveSet.Poses;
for (int i = 0; i < count; ++i)
{
Expand Down Expand Up @@ -857,6 +874,7 @@ public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVeloci
{
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndex = ref Unsafe.As<Vector<int>, int>(ref references);
velocities = default;
for (int i = 0; i < count; ++i)
{
GatherVelocities(ref sourceVelocities, ref velocities, ref baseIndex, i);
Expand All @@ -871,8 +889,9 @@ public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVeloci
/// <param name="velocitiesA">Gathered velocities of A bodies.</param>
/// <param name="velocitiesB">Gathered velocities of B bodies.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref TwoBodyReferences references, int count, out BodyVelocities velocitiesA, out BodyVelocities velocitiesB)
{
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref TwoBodyReferences references, int count, out BodyVelocities velocitiesA, out BodyVelocities velocitiesB) {
velocitiesA = default;
velocitiesB = default;
Debug.Assert(count >= 0 && count <= Vector<float>.Count);
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
Expand All @@ -894,8 +913,10 @@ public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVeloci
/// <param name="velocitiesC">Gathered velocities of C bodies.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref ThreeBodyReferences references, int count,
out BodyVelocities velocitiesA, out BodyVelocities velocitiesB, out BodyVelocities velocitiesC)
{
out BodyVelocities velocitiesA, out BodyVelocities velocitiesB, out BodyVelocities velocitiesC) {
velocitiesA = default;
velocitiesB = default;
velocitiesC = default;
Debug.Assert(count >= 0 && count <= Vector<float>.Count);
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
Expand All @@ -920,8 +941,11 @@ public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVeloci
/// <param name="velocitiesD">Gathered velocities of D bodies.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref FourBodyReferences references, int count,
out BodyVelocities velocitiesA, out BodyVelocities velocitiesB, out BodyVelocities velocitiesC, out BodyVelocities velocitiesD)
{
out BodyVelocities velocitiesA, out BodyVelocities velocitiesB, out BodyVelocities velocitiesC, out BodyVelocities velocitiesD) {
velocitiesA = default;
velocitiesB = default;
velocitiesC = default;
velocitiesD = default;
Debug.Assert(count >= 0 && count <= Vector<float>.Count);
//Grab the base references for the body indices. Note that we make use of the references memory layout again.
ref var baseIndexA = ref Unsafe.As<Vector<int>, int>(ref references.IndexA);
Expand Down
12 changes: 6 additions & 6 deletions BepuPhysics/BodyProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static void TransformByInverse(in Vector3 v, in RigidPose pose, out Vecto
/// </summary>
/// <param name="pose">Pose to invert.</param>
/// <param name="inverse">Inverse of the pose.</param>
public static void Invert(in RigidPose pose, out RigidPose inverse)
{
public static void Invert(in RigidPose pose, out RigidPose inverse) {
inverse = default;
QuaternionEx.Conjugate(pose.Orientation, out inverse.Orientation);
QuaternionEx.Transform(-pose.Position, inverse.Orientation, out inverse.Position);
}
Expand All @@ -81,8 +81,8 @@ public static void Invert(in RigidPose pose, out RigidPose inverse)
/// <param name="b">Second transform to concatenate.</param>
/// <param name="result">Result of the concatenation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MultiplyWithoutOverlap(in RigidPose a, in RigidPose b, out RigidPose result)
{
public static void MultiplyWithoutOverlap(in RigidPose a, in RigidPose b, out RigidPose result) {
result = default;
QuaternionEx.ConcatenateWithoutOverlap(a.Orientation, b.Orientation, out result.Orientation);
QuaternionEx.Transform(a.Position, b.Orientation, out var rotatedTranslationA);
result.Position = rotatedTranslationA + b.Position;
Expand Down Expand Up @@ -135,8 +135,8 @@ public static void WriteFirst(in RigidPose pose, ref RigidPoses poses)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadFirst(in RigidPoses poses, out RigidPose pose)
{
public static void ReadFirst(in RigidPoses poses, out RigidPose pose) {
pose = default;
Vector3Wide.ReadFirst(poses.Position, out pose.Position);
QuaternionWide.ReadFirst(poses.Orientation, out pose.Orientation);
}
Expand Down
4 changes: 2 additions & 2 deletions BepuPhysics/Collidables/Compound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static bool ValidateChildIndices(ref Buffer<CompoundChild> children, Shap
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GetRotatedChildPose(in RigidPose localPose, in Quaternion orientation, out RigidPose rotatedChildPose)
{
public static void GetRotatedChildPose(in RigidPose localPose, in Quaternion orientation, out RigidPose rotatedChildPose) {
rotatedChildPose = default;
QuaternionEx.ConcatenateWithoutOverlap(localPose.Orientation, orientation, out rotatedChildPose.Orientation);
QuaternionEx.Transform(localPose.Position, orientation, out rotatedChildPose.Position);
}
Expand Down
18 changes: 11 additions & 7 deletions BepuPhysics/Collidables/ConvexHull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,10 @@ public void Broadcast(in ConvexHull shape)
Hulls[i] = shape;
}

public void GetBounds(ref QuaternionWide orientations, int countInBundle, out Vector<float> maximumRadius, out Vector<float> maximumAngularExpansion, out Vector3Wide min, out Vector3Wide max)
{
public void GetBounds(ref QuaternionWide orientations, int countInBundle, out Vector<float> maximumRadius, out Vector<float> maximumAngularExpansion, out Vector3Wide min, out Vector3Wide max) {
maximumRadius = default;
min = default;
max = default;
for (int i = 0; i < countInBundle; ++i)
{
Vector3Wide.Broadcast(new Vector3(float.MaxValue), out var minWide);
Expand Down Expand Up @@ -366,8 +368,10 @@ public void GetBounds(ref QuaternionWide orientations, int countInBundle, out Ve
maximumAngularExpansion = maximumRadius;
}

public void RayTest(ref RigidPoses poses, ref RayWide rayWide, out Vector<int> intersected, out Vector<float> t, out Vector3Wide normal)
{
public void RayTest(ref RigidPoses poses, ref RayWide rayWide, out Vector<int> intersected, out Vector<float> t, out Vector3Wide normal) {
intersected = default;
t = default;
normal = default;
Debug.Assert(Hulls.Length > 0 && Hulls.Length <= Vector<float>.Count);
for (int i = 0; i < Hulls.Length; ++i)
{
Expand All @@ -391,7 +395,7 @@ public void RayTest(ref RigidPoses poses, ref RayWide rayWide, out Vector<int> i
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EstimateEpsilonScale(in Vector<int> terminatedLanes, out Vector<float> epsilonScale)
{
Vector3Wide bundle;
Vector3Wide bundle = default;
for (int i = 0; i < Vector<float>.Count; ++i)
{
if (terminatedLanes[i] < 0)
Expand Down Expand Up @@ -420,8 +424,8 @@ public struct ConvexHullSupportFinder : ISupportFinder<ConvexHull, ConvexHullWid
public bool HasMargin => false;

//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ComputeLocalSupport(in ConvexHullWide shape, in Vector3Wide direction, in Vector<int> terminatedLanes, out Vector3Wide support)
{
public void ComputeLocalSupport(in ConvexHullWide shape, in Vector3Wide direction, in Vector<int> terminatedLanes, out Vector3Wide support) {
support = default;
Helpers.FillVectorWithLaneIndices(out var indexOffsets);
for (int slotIndex = 0; slotIndex < Vector<float>.Count; ++slotIndex)
{
Expand Down
9 changes: 9 additions & 0 deletions BepuPhysics/CollisionDetection/CollisionBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ unsafe void CacheShapes(ref CollisionTaskReference reference, void* shapeA, void
cachedShapeA = batch.Shapes.Allocate(shapeSizeA);
cachedShapeB = batch.Shapes.Allocate(shapeSizeB);
//TODO: Given the size of these copies, it's not clear that this copy implementation is ideal. Wouldn't worry too much about it.
#if NET45
Unsafe.CopyBlockUnaligned(cachedShapeA, shapeA, (uint)shapeSizeA);
Unsafe.CopyBlockUnaligned(cachedShapeB, shapeB, (uint)shapeSizeB);
#else
Buffer.MemoryCopy(shapeA, cachedShapeA, shapeSizeA, shapeSizeA);
Buffer.MemoryCopy(shapeB, cachedShapeB, shapeSizeB, shapeSizeB);
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -261,7 +266,11 @@ public unsafe void CacheShapeB(int shapeTypeA, int shapeTypeB, void* shapeDataB,
Debug.Assert(batch.Shapes.ByteCount == 0);
}
cachedShapeDataB = batch.Shapes.Allocate(shapeSizeB);
#if NET45
Unsafe.CopyBlockUnaligned(cachedShapeDataB, shapeDataB, (uint)shapeDataB);
#else
Buffer.MemoryCopy(shapeDataB, cachedShapeDataB, shapeSizeB, shapeSizeB);
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public struct BoxConvexHullTester : IPairTester<BoxWide, ConvexHullWide, Convex4
{
public int BatchSize => 16;

public unsafe void Test(ref BoxWide a, ref ConvexHullWide b, ref Vector<float> speculativeMargin, ref Vector3Wide offsetB, ref QuaternionWide orientationA, ref QuaternionWide orientationB, int pairCount, out Convex4ContactManifoldWide manifold)
{
public unsafe void Test(ref BoxWide a, ref ConvexHullWide b, ref Vector<float> speculativeMargin, ref Vector3Wide offsetB, ref QuaternionWide orientationA, ref QuaternionWide orientationB, int pairCount, out Convex4ContactManifoldWide manifold) {
manifold = default;
Matrix3x3Wide.CreateFromQuaternion(orientationA, out var boxOrientation);
Matrix3x3Wide.CreateFromQuaternion(orientationB, out var hullOrientation);
Matrix3x3Wide.MultiplyByTransposeWithoutOverlap(boxOrientation, hullOrientation, out var hullLocalBoxOrientation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void TryAddInteriorPoint(in Vector2Wide point, in Vector<int> featureId,
var contained = Vector.BitwiseAnd(allowContact, Vector.BitwiseAnd(
Vector.BitwiseAnd(Vector.GreaterThanOrEqual(edge0010Dot, edge0010PlaneMin), Vector.LessThanOrEqual(edge0010Dot, edge0010PlaneMax)),
Vector.BitwiseAnd(Vector.GreaterThanOrEqual(edge1011Dot, edge1011PlaneMin), Vector.LessThanOrEqual(edge1011Dot, edge1011PlaneMax))));
ManifoldCandidate candidate;
ManifoldCandidate candidate = default;
candidate.X = point.X;
candidate.Y = point.Y;
candidate.FeatureId = featureId;
Expand All @@ -106,8 +106,8 @@ static void TryAddInteriorPoint(in Vector2Wide point, in Vector<int> featureId,
public unsafe void Test(
ref BoxWide a, ref CylinderWide b, ref Vector<float> speculativeMargin,
ref Vector3Wide offsetB, ref QuaternionWide orientationA, ref QuaternionWide orientationB, int pairCount,
out Convex4ContactManifoldWide manifold)
{
out Convex4ContactManifoldWide manifold) {
manifold = default;
Matrix3x3Wide.CreateFromQuaternion(orientationA, out var worldRA);
Matrix3x3Wide.CreateFromQuaternion(orientationB, out var worldRB);
//Work in b's local space.
Expand Down
Loading

0 comments on commit ba59c97

Please sign in to comment.