From c8af56cbc486e45fe22dae9f927fa5fcc7ea2dd0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 4 Jan 2021 17:57:33 +0100 Subject: [PATCH] Added new GraphicsDevice extensions --- .../Extensions/GraphicsDeviceExtensions.cs | 380 +++++++++++++++++- 1 file changed, 379 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 4da8df60..c0533982 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -228,6 +228,102 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] array, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture2D(array.AsSpan(), width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture2D(array.AsSpan(offset), width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture2D texture = new(device, width, height); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new readonly 3D texture with the specified parameters. /// @@ -322,6 +418,110 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, , ]. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. + /// + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new read write buffer with the specified parameters. /// @@ -472,6 +672,80 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(offset), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + /// /// Allocates a new writeable 2D texture with the specified parameters. /// @@ -481,7 +755,7 @@ namespace ComputeSharp /// The input with the data to copy on the allocated texture. /// The width of the texture. /// The height of the texture. - /// A instance with the contents of the input . + /// A instance with the contents of the input . [Pure] public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) where T : unmanaged, IUnorm @@ -588,6 +862,110 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. + /// + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + /// /// Compiles and runs the input shader on a target instance, with the specified parameters. ///