From 9e728efca5dd24773852d6836331d1e990545078 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jan 2021 20:23:59 +0100 Subject: [PATCH] Added TransferBuffer allocation extensions --- .../Extensions/GraphicsDeviceExtensions.cs | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index fdb156ba..7ace8078 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -976,6 +976,104 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new upload buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The length of the buffer to allocate. + /// The allocation mode to use for the new resource. + /// A zeroed instance of size . + [Pure] + public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + where T : unmanaged + { + return new(device, length, allocationMode); + } + + /// + /// 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] + public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, T[] array) + where T : unmanaged + { + return device.AllocateUploadBuffer(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] + public static UploadBuffer AllocateUploadBuffer(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged + { + UploadBuffer buffer = new(device, span.Length, AllocationMode.Default); + + span.CopyTo(buffer.Span); + + return buffer; + } + + /// + /// Allocates a new upload buffer with the specified parameters. + /// + /// The type of items to store in the buffer. + /// The instance to use to allocate the buffer. + /// The length of the buffer to allocate. + /// The allocation mode to use for the new resource. + /// A zeroed instance of size . + [Pure] + public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, int length, AllocationMode allocationMode = AllocationMode.Default) + where T : unmanaged + { + return new(device, length, allocationMode); + } + + /// + /// 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 array. + [Pure] + public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, StructuredBuffer source) + where T : unmanaged + { + ReadBackBuffer buffer = new(device, source.Length, AllocationMode.Default); + + source.GetData(buffer, 0, buffer.Length, 0); + + 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] + public static ReadBackBuffer AllocateReadBackBuffer(this GraphicsDevice device, ReadOnlySpan span) + where T : unmanaged + { + ReadBackBuffer buffer = new(device, span.Length, AllocationMode.Default); + + span.CopyTo(buffer.Span); + + return buffer; + } + /// /// Compiles and runs the input shader on a target instance, with the specified parameters. ///