From 37424c3692b279930d99da72b9a36a9a683831e1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 29 Aug 2019 01:35:33 +0200 Subject: [PATCH] Added SpanExtensions class for 2D arrays --- .../Extensions/SpanExtensions.cs | 93 +++++++++++++++++++ .../Buffers/Extensions/BufferExtensions.cs | 13 +-- 2 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 src/ComputeSharp.Core/Extensions/SpanExtensions.cs diff --git a/src/ComputeSharp.Core/Extensions/SpanExtensions.cs b/src/ComputeSharp.Core/Extensions/SpanExtensions.cs new file mode 100644 index 00000000..ab17a146 --- /dev/null +++ b/src/ComputeSharp.Core/Extensions/SpanExtensions.cs @@ -0,0 +1,93 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; + +namespace ComputeSharp.Core +{ + /// + /// A that contains extension methods for the + /// + public static class SpanExtensions + { + /// + /// Creates a from a given 2D array + /// + /// The type of items in the input array + /// The input 2D array + /// A that maps all the items of the input array + [Pure] + public static Span AsSpan(this T[,] array) + { + ref T r0 = ref array[0, 0]; + + return MemoryMarshal.CreateSpan(ref r0, array.Length); + } + + /// + /// Creates a from a given 2D array + /// + /// The type of items in the input array + /// The input 2D array + /// The index of the initial row to map from the input array + /// A that maps the desired items of the input array + [Pure] + public static Span AsSpan(this T[,] array, Index startIndex) + { + int + rows = array.GetLength(0), + offset = startIndex.GetOffset(rows), + interval = rows - offset; + ref T r0 = ref array[offset, 0]; + + return MemoryMarshal.CreateSpan(ref r0, interval * array.GetLength(1)); + } + + /// + /// Creates a from a given 2D array + /// + /// The type of items in the input array + /// The input 2D array + /// The index of the initial row to map from the input array + /// A that maps the desired items of the input array + [Pure] + public static Span AsSpan(this T[,] array, int start) + { + int interval = array.GetLength(0) - start; + ref T r0 = ref array[start, 0]; + + return MemoryMarshal.CreateSpan(ref r0, interval * array.GetLength(1)); + } + + /// + /// Creates a from a given 2D array + /// + /// The type of items in the input array + /// The input 2D array + /// The index of the initial row to map from the input array + /// The number of rows to map from the input array + /// A that maps the desired items of the input array + [Pure] + public static Span AsSpan(this T[,] array, int start, int length) + { + ref T r0 = ref array[start, 0]; + + return MemoryMarshal.CreateSpan(ref r0, length * array.GetLength(1)); + } + + /// + /// Creates a from a given 2D array + /// + /// The type of items in the input array + /// The input 2D array + /// The range of rows to map from the input array + /// A that maps the desired items of the input array + [Pure] + public static Span AsSpan(this T[,] array, Range range) + { + var indices = range.GetOffsetAndLength(array.GetLength(0)); + ref T r0 = ref array[indices.Offset, 0]; + + return MemoryMarshal.CreateSpan(ref r0, indices.Length * array.GetLength(1)); + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index 85f0c8fd..34fe732b 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -2,6 +2,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.Core; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; @@ -37,9 +38,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged { - Span span = MemoryMarshal.CreateSpan(ref array[0, 0], array.Length); - - return device.AllocateConstantBuffer(span); + return device.AllocateConstantBuffer(array.AsSpan()); } /// @@ -115,9 +114,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged { - Span span = MemoryMarshal.CreateSpan(ref array[0, 0], array.Length); - - return device.AllocateReadOnlyBuffer(span); + return device.AllocateReadOnlyBuffer(array.AsSpan()); } /// @@ -193,9 +190,7 @@ namespace ComputeSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged { - Span span = MemoryMarshal.CreateSpan(ref array[0, 0], array.Length); - - return device.AllocateReadWriteBuffer(span); + return device.AllocateReadWriteBuffer(array.AsSpan()); } ///