From 207a26006a223a093f57fed7e653e550094492f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 12:48:42 +0200 Subject: [PATCH] Added BufferExtensions class --- src/ComputeSharp.Graphics/Buffers/Buffer2.cs | 2 +- .../Buffers/BufferExtensions.cs | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs index 26f4d91d..24d8873d 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer2.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer2.cs @@ -19,7 +19,7 @@ namespace ComputeSharp.Graphics.Buffers /// The associated with the current instance /// The number of items to store in the current buffer /// The heap type for the current buffer - public Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) + internal Buffer2(GraphicsDevice device, int size, HeapType heapType) : base(device) { Size = size; ElementSizeInBytes = Unsafe.SizeOf(); diff --git a/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs new file mode 100644 index 00000000..318698bb --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/BufferExtensions.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using SharpDX.Direct3D12; + +namespace ComputeSharp.Graphics.Buffers +{ + /// + /// A with extension methods for the type to allocate buffers + /// + public static class BufferExtensions + { + /// + /// 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 read write instance of size + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, int size) where T : unmanaged + { + return new Buffer2(device, size, HeapType.Default); + } + + /// + /// 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 Buffer2 AllocateReadWriteBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Default); + 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 Buffer2 AllocateConstantBuffer(this GraphicsDevice device, Span span) where T : unmanaged + { + Buffer2 buffer = new Buffer2(device, span.Length, HeapType.Upload); + buffer.SetData(span); + + return buffer; + } + } +}