From 4ee036178c4da7cd451e644b746613c8afd53cf7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 15 Aug 2019 16:13:16 +0200 Subject: [PATCH] Added extension APIs to create readonly buffers --- .../Buffers/Extensions/BufferExtensions.cs | 212 ++++++++++++------ 1 file changed, 146 insertions(+), 66 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs index 708bdb95..5d1112ea 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/BufferExtensions.cs @@ -12,6 +12,152 @@ namespace ComputeSharp /// 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 unsafe ConstantBuffer 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 + /// + /// 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 unsafe ReadOnlyBuffer AllocateReadOnlyBuffer(this GraphicsDevice device, T[,] array) where T : unmanaged + { + fixed (T* p = array) + { + Span span = new Span(p, array.Length); + return device.AllocateReadOnlyBuffer(span); + } + } + + /// + /// 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 /// @@ -91,71 +237,5 @@ namespace ComputeSharp return readWriteBuffer; } - - /// - /// 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 unsafe ConstantBuffer 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 - /// - /// 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; - } } }