using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp { /// /// A with extension methods for the type to allocate buffers /// public static class BufferExtensions { /// /// 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 ConstantBuffer 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 ConstantBuffer 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 with the data to copy on the allocated buffer /// A constant instance with the contents of the input [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged { ConstantBuffer buffer = new ConstantBuffer(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 ConstantBuffer AllocateConstantBuffer(this GraphicsDevice device, HlslBuffer buffer) where T : unmanaged { ConstantBuffer constantBuffer = new ConstantBuffer(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 ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, int size) where T : unmanaged { return new ReadOnlyBuffer(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 ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice 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 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 ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice 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 ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, Span span) where T : unmanaged { ReadOnlyBuffer buffer = new ReadOnlyBuffer(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 ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, HlslBuffer buffer) where T : unmanaged { ReadOnlyBuffer readWriteBuffer = new ReadOnlyBuffer(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 ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged { 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 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 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 ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged { ReadWriteBuffer buffer = new ReadWriteBuffer(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 ReadWriteBuffer AllocateReadWriteBuffer(this GraphicsDevice device, HlslBuffer buffer) where T : unmanaged { ReadWriteBuffer readWriteBuffer = new ReadWriteBuffer(device, buffer.Size); readWriteBuffer.SetData(buffer); return readWriteBuffer; } } }