diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index 70dacfb5..14767a10 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace ComputeSharp.Graphics.Buffers.Extensions { @@ -23,6 +24,38 @@ namespace ComputeSharp.Graphics.Buffers.Extensions return new ReadWriteBuffer(device, size); } + /// + /// Allocates a new read write buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input array with the data to copy on the allocated buffer + /// A read write instance with the contents of the input array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[] array) where T : unmanaged + { + return device.AllocateReadWriteBuffer(array.AsSpan()); + } + + /// + /// Allocates a new read write buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input 2D array with the data to copy on the allocated buffer + /// A read write instance with the contents of the input 2D array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged + { + fixed (T* p = array) + { + Span span = new Span(p, array.Length); + return device.AllocateReadWriteBuffer(span); + } + } + /// /// Allocates a new read write buffer with the specified parameters /// @@ -40,6 +73,38 @@ namespace ComputeSharp.Graphics.Buffers.Extensions return buffer; } + /// + /// Allocates a new constant buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input array with the data to copy on the allocated buffer + /// A constant instance with the contents of the input array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, T[] array) where T : unmanaged + { + return device.AllocateConstantBuffer(array.AsSpan()); + } + + /// + /// Allocates a new constant buffer with the specified parameters + /// + /// The type of items to store in the buffer + /// The instance to use to allocate the buffer + /// The input 2D array with the data to copy on the allocated buffer + /// A constant instance with the contents of the input 2D array + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ReadOnlyBuffer AllocateConstantBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged + { + fixed (T* p = array) + { + Span span = new Span(p, array.Length); + return device.AllocateConstantBuffer(span); + } + } + /// /// Allocates a new constant buffer with the specified parameters ///