diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions2.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions2.cs new file mode 100644 index 00000000..523f171f --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions2.cs @@ -0,0 +1,203 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp +{ + /// + /// A with extension methods for the type to allocate buffers. + /// + public static class BufferExtensions2 + { + /// + /// 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 ConstantBuffer2 AllocateConstantBuffer(this GraphicsDevice2 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 with the data to copy on the allocated buffer. + /// A constant instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ConstantBuffer2 AllocateConstantBuffer(this GraphicsDevice2 device, Span span) + where T : unmanaged + { + ConstantBuffer2 buffer = new(device, span.Length); + + buffer.SetData(span); + + 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 with the data to copy on the allocated buffer. + /// A constant instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ConstantBuffer2 AllocateConstantBuffer(this GraphicsDevice2 device, HlslBuffer2 buffer) + where T : unmanaged + { + ConstantBuffer2 constantBuffer = new(device, buffer.Size); + + constantBuffer.SetData(buffer); + + return constantBuffer; + } + + /// + /// Allocates a new readonly buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The size of the buffer to allocate. + /// A zeroed instance of size . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer2 AllocateReadOnlyBuffer(this GraphicsDevice2 device, int size) + where T : unmanaged + { + return new(device, size); + } + + /// + /// Allocates a new readonly 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 ReadOnlyBuffer2 AllocateReadOnlyBuffer(this GraphicsDevice2 device, T[] array) + where T : unmanaged + { + return device.AllocateReadOnlyBuffer(array.AsSpan()); + } + + /// + /// Allocates a new readonly buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The input with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer2 AllocateReadOnlyBuffer(this GraphicsDevice2 device, Span span) + where T : unmanaged + { + ReadOnlyBuffer2 buffer = new(device, span.Length); + + buffer.SetData(span); + + return buffer; + } + + /// + /// Allocates a new readonly buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The input with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlyBuffer2 AllocateReadOnlyBuffer(this GraphicsDevice2 device, HlslBuffer2 buffer) + where T : unmanaged + { + ReadOnlyBuffer2 readWriteBuffer = new(device, buffer.Size); + + readWriteBuffer.SetData(buffer); + + return readWriteBuffer; + } + + /// + /// 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 size of the buffer to allocate. + /// A zeroed instance of size . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer2 AllocateReadWriteBuffer(this GraphicsDevice2 device, int size) + where T : unmanaged + { + return new(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 ReadWriteBuffer2 AllocateReadWriteBuffer(this GraphicsDevice2 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 with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer2 AllocateReadWriteBuffer(this GraphicsDevice2 device, Span span) + where T : unmanaged + { + ReadWriteBuffer2 buffer = new(device, span.Length); + + buffer.SetData(span); + + return buffer; + } + + /// + /// 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 with the data to copy on the allocated buffer. + /// A read write instance with the contents of the input . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadWriteBuffer2 AllocateReadWriteBuffer(this GraphicsDevice2 device, HlslBuffer2 buffer) + where T : unmanaged + { + ReadWriteBuffer2 readWriteBuffer = new (device, buffer.Size); + + readWriteBuffer.SetData(buffer); + + return readWriteBuffer; + } + } +}