diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index 71d03179..ffd497a6 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.1 8.0 enable diff --git a/src/ComputeSharp/Extensions/Primitives/VectorExtensions.cs b/src/ComputeSharp/Extensions/Primitives/VectorExtensions.cs new file mode 100644 index 00000000..a5b5c1ab --- /dev/null +++ b/src/ComputeSharp/Extensions/Primitives/VectorExtensions.cs @@ -0,0 +1,81 @@ +using System; +using System.Diagnostics.Contracts; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the , and types + /// + public static class VectorExtensions + { + /// + /// Converts a array into a with items of type + /// + /// The input array + /// A with items of type , mapping the original array + [Pure] + public static Span AsFloat2Span(this Vector2[] array) => array.AsSpan().As(); + + /// + /// Converts a into a with items of type + /// + /// The input + /// A with items of type , mapping the original + [Pure] + public static Span AsFloat2Span(this Span span) => span.As(); + + /// + /// Converts a array into a with items of type + /// + /// The input array + /// A with items of type , mapping the original array + [Pure] + public static Span AsFloat3Span(this Vector3[] array) => array.AsSpan().As(); + + /// + /// Converts a into a with items of type + /// + /// The input + /// A with items of type , mapping the original + [Pure] + public static Span AsFloat3Span(this Span span) => span.As(); + + /// + /// Converts a array into a with items of type + /// + /// The input array + /// A with items of type , mapping the original array + [Pure] + public static Span AsFloat4Span(this Vector4[] array) => array.AsSpan().As(); + + /// + /// Converts a into a with items of type + /// + /// The input + /// A with items of type , mapping the original + [Pure] + public static Span AsFloat4Span(this Span span) => span.As(); + + /// + /// Converts a value with items of type to one with items of type + /// + /// The type of the input items + /// The type of items to convert to + /// The input + /// A with items of type + [Pure] + private static Span As(this Span span) + where TFrom : unmanaged + where TTo : unmanaged + { + + if (span.Length == 0) return Span.Empty; + + ref TTo r0 = ref Unsafe.As(ref span.GetPinnableReference()); + return MemoryMarshal.CreateSpan(ref r0, span.Length); + } + } +}