* Box Simd

* Add 256 bit version of GetAABB

* Add AABB bechmarks

* No real diff between 128 & 256, so removing 256

| Method     | Mean      | Error     | StdDev    | Ratio |
|----------- |----------:|----------:|----------:|------:|
| GetAABB    | 5.8107 ns | 0.0154 ns | 0.0137 ns |  1.00 |
| GetAABB128 | 0.4927 ns | 0.0003 ns | 0.0002 ns |  0.08 |
| GetAABB256 | 0.4332 ns | 0.0006 ns | 0.0006 ns |  0.07 |

* Add Box2Rotated.Transform Benchmark

* Results

20% faster and much smaller code. Also I don't think it inlined RotateVec

* Add Matrix3x2Helper.TransformBox() benchmark

new:

| Method    | Mean     | Error     | StdDev    | Code Size |
|---------- |---------:|----------:|----------:|----------:|
| Transform | 2.463 ns | 0.0766 ns | 0.0679 ns |     216 B |

old:
| Method    | Mean     | Error     | StdDev    | Median   | Code Size |
|---------- |---------:|----------:|----------:|---------:|----------:|
| Transform | 9.469 ns | 0.2140 ns | 0.5408 ns | 9.206 ns |     621 B |

* Fix polygon constructor

* SlimPolygonBenchmark

* use new SimdHelper for other methods

* Fix bugs

* Use new methods

* Simd SlimPolygon.ComputeAABB

* Move simd transform to physics

* Cleanup

* Remove uneccesary Unsafe.SkipInit

* These tests all work on master

* Add Transform.MulSimd test

* Add SlimPolygon constructor tests

* Add ComputeAABB test

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2025-11-10 20:30:08 +13:00
committed by GitHub
parent fe19ff9bd5
commit 3f19d25018
21 changed files with 638 additions and 193 deletions

View File

@@ -161,47 +161,33 @@ internal partial class Clyde
}
/// <summary>
/// This is effectively a specialized combination of a <see cref="Matrix3.TransformBox(in Box2Rotated)"/> and <see cref="Box2Rotated.CalcBoundingBox()"/>.
/// This is effectively a specialized combination of a <see cref="Matrix3Helpers.TransformBox(Matrix3x2, in Box2)"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Box2 TransformCenteredBox(in Box2 box, float angle, in Vector2 offset, in Vector2 scale)
internal static unsafe Box2 TransformCenteredBox(in Box2 box, float angle, in Vector2 offset, in Vector2 scale)
{
// This function is for sprites, which flip the y axis, so here we flip the definition of t and b relative to the normal function.
DebugTools.Assert(scale.Y < 0);
var boxVec = Unsafe.As<Box2, Vector128<float>>(ref Unsafe.AsRef(in box));
var sin = Vector128.Create(MathF.Sin(angle));
var cos = Vector128.Create(MathF.Cos(angle));
var allX = Vector128.Shuffle(boxVec, Vector128.Create(0, 0, 2, 2));
var allY = Vector128.Shuffle(boxVec, Vector128.Create(1, 3, 3, 1));
var modX = allX * cos - allY * sin;
var modY = allX * sin + allY * cos;
var boxX = Vector128.Shuffle(boxVec, Vector128.Create(0, 0, 2, 2));
var boxY = Vector128.Shuffle(boxVec, Vector128.Create(1, 3, 3, 1));
var x = boxX * cos - boxY * sin;
var y = boxX * sin + boxY * cos;
var lbrt = SimdHelpers.GetAABB(x, y);
// This function is for sprites, which flip the y-axis via the scale, so we need to flip t & b.
DebugTools.Assert(scale.Y < 0);
lbrt = Vector128.Shuffle(lbrt, Vector128.Create(0,3,2,1));
var offsetVec = Unsafe.As<Vector2, Vector128<float>>(ref Unsafe.AsRef(in offset)); // upper undefined
var scaleVec = Unsafe.As<Vector2, Vector128<float>>(ref Unsafe.AsRef(in scale)); // upper undefined
offsetVec = Vector128.Shuffle(offsetVec, Vector128.Create(0, 1, 0, 1));
scaleVec = Vector128.Shuffle(scaleVec, Vector128.Create(0, 1, 0, 1));
Vector128<float> lbrt;
if (Sse.IsSupported)
{
var lrlr = SimdHelpers.MinMaxHorizontalSse(modX);
var btbt = SimdHelpers.MaxMinHorizontalSse(modY);
lbrt = Sse.UnpackLow(lrlr, btbt);
}
else
{
var l = SimdHelpers.MinHorizontal128(allX);
var b = SimdHelpers.MaxHorizontal128(allY);
var r = SimdHelpers.MaxHorizontal128(allX);
var t = SimdHelpers.MinHorizontal128(allY);
lbrt = SimdHelpers.MergeRows128(l, b, r, t);
}
// offset and scale box.
// note that the scaling here is scaling the whole space, not jut the box. I.e., the centre of the box is changing
lbrt = (lbrt + offsetVec) * scaleVec;
return Unsafe.As<Vector128<float>, Box2>(ref lbrt);
}