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 8c1305e
Show file tree
Hide file tree
Showing 37 changed files with 157 additions and 117 deletions.
65 changes: 41 additions & 24 deletions BepuPhysics/Bodies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ private static void GatherInertiaForBody(ref BodyInertia source, ref BodyInertia
/// <param name="inertiaB">Gathered inertia of body B.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherInertia(ref Vector<int> references, int count,
out BodyInertias inertiaA)
{
out BodyInertias inertiaA) {
inertiaA = 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);
Expand All @@ -561,8 +561,9 @@ public void GatherInertia(ref Vector<int> references, int count,
/// <param name="inertiaA">Gathered inertia of body A.</param>
/// <param name="inertiaB">Gathered inertia of body B.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherInertia(ref TwoBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB)
{
public void GatherInertia(ref TwoBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB) {
inertiaA = default;
inertiaB = 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 @@ -583,8 +584,10 @@ public void GatherInertia(ref TwoBodyReferences references, int count, out BodyI
/// <param name="inertiaB">Gathered inertia of body B.</param>
/// <param name="inertiaC">Gathered inertia of body C.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherInertia(ref ThreeBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB, out BodyInertias inertiaC)
{
public void GatherInertia(ref ThreeBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB, out BodyInertias inertiaC) {
inertiaA = default;
inertiaB = default;
inertiaC = 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 @@ -608,8 +611,11 @@ public void GatherInertia(ref ThreeBodyReferences references, int count, out Bod
/// <param name="inertiaC">Gathered inertia of body C.</param>
/// <param name="inertiaD">Gathered inertia of body D.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherInertia(ref FourBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB, out BodyInertias inertiaC, out BodyInertias inertiaD)
{
public void GatherInertia(ref FourBodyReferences references, int count, out BodyInertias inertiaA, out BodyInertias inertiaB, out BodyInertias inertiaC, out BodyInertias inertiaD) {
inertiaA = default;
inertiaB = default;
inertiaC = default;
inertiaD = 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 @@ -634,8 +640,9 @@ public void GatherInertia(ref FourBodyReferences references, int count, out Body
/// <param name="orientationB">Gathered orientation of body B.</param>
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherOrientation(ref TwoBodyReferences references, int count,
out QuaternionWide orientationA, out QuaternionWide orientationB)
{
out QuaternionWide orientationA, out QuaternionWide orientationB) {
orientationA = default;
orientationB = 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 @@ -660,8 +667,8 @@ public void GatherOrientation(ref TwoBodyReferences references, int count,
/// <param name="orientation">Gathered orientation of bodies in the bundle.</param>
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GatherOrientation(ref Vector<int> references, int count,
out QuaternionWide orientation)
{
out QuaternionWide orientation) {
orientation = 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);
Expand Down Expand Up @@ -695,6 +702,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 +731,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 +772,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 +799,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 +829,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 @@ -853,8 +864,8 @@ private static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sources, re
/// <param name="count">Number of bodies in the bundle.</param>
/// <param name="velocities">Gathered velocities.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref Vector<int> references, int count, out BodyVelocities velocities)
{
public static unsafe void GatherVelocities(ref Buffer<BodyVelocity> sourceVelocities, ref Vector<int> references, int count, out BodyVelocities velocities) {
velocities = default;
//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);
for (int i = 0; i < count; ++i)
Expand All @@ -871,8 +882,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 +906,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 +934,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
Loading

0 comments on commit 8c1305e

Please sign in to comment.