From 9806d02279fe8d9313632d214ddc98d9bb397587 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:40:15 +0100 Subject: [PATCH] Refactored public Texture3D APIs --- .../Buffers/Abstract/Texture3D{T}.cs | 249 +------------- .../Extensions/Texture3DExtensions.cs | 303 ++++++++++++++++++ 2 files changed, 304 insertions(+), 248 deletions(-) create mode 100644 src/ComputeSharp/Extensions/Texture3DExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 2c8029bf..c86d5618 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,7 +1,4 @@ -using System; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; @@ -108,124 +105,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Reads the contents of the current instance and returns an array. - /// - /// A array with the contents of the current buffer. - /// - /// The returned array will be using the same memory layout as the texture, that is, each 2D plane - /// in the 3D volume represented by the texture is contiguous in memory, and planes are stacked in - /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. - /// - [Pure] - public T[,,] GetData() - { - T[,,] data = new T[Depth, Height, Width]; - - GetData(data); - - return data; - } - - /// - /// Reads the contents of the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// - /// The input 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 [, , ]. - /// - public void GetData(T[,,] destination) - { - Guard.IsEqualTo(destination.GetLength(0), Depth, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(2), Width, nameof(destination)); - - fixed (T* p = destination) - { - GetData(new Span(p, destination.Length)); - } - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - public void GetData(T[] destination, int offset) - { - GetData(destination.AsSpan(offset), 0, 0, 0, Width, Height, Depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal range of items to copy. - /// The vertical range of items to copy. - /// The depthwise range of items to copy. - public void GetData(T[] destination, int offset, Range x, Range y, Range z) - { - GetData(destination.AsSpan(offset), x, y, z); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. - /// The width of the memory area to copy. - /// The height of the memory area to copy. - /// The depth of the memory area to copy. - public void GetData(T[] destination, int offset, int x, int y, int z, int width, int height, int depth) - { - GetData(destination.AsSpan(offset), x, y, z, width, height, depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0, 0, 0, Width, Height, Depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The horizontal range in the source texture. - /// The vertical range in the source texture. - /// The depthwise range in the source texture. - public void GetData(Span destination, Range x, Range y, Range z) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - var (offsetZ, depth) = z.GetOffsetAndLength(Depth); - - GetData(destination, offsetX, offsetY, offsetZ, width, height, depth); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. - /// The width of the memory area to copy. - /// The height of the memory area to copy. - /// The depth of the memory area to copy. - public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -299,132 +178,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } } - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - /// - /// 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 [, , ]. - /// - public void SetData(T[,,] source) - { - Guard.IsEqualTo(source.GetLength(0), Depth, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(2), Width, nameof(source)); - - fixed (T* p = source) - { - SetData(new Span(p, source.Length)); - } - } - - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[] source) - { - SetData(source.AsSpan(), 0, 0, 0, Width, Height, Depth); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(T[] source, Range x, Range y, Range z) - { - SetData(source.AsSpan(), x, y, z); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. - /// The width of the memory area to write to. - /// The height of the memory area to write to. - /// The depth of the memory area to write to. - public void SetData(T[] source, int x, int y, int z, int width, int height, int depth) - { - SetData(source.AsSpan(), x, y, z, width, height, depth); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(T[] source, int offset, Range x, Range y, Range z) - { - SetData(source.AsSpan(offset), x, y, z); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. - /// The width of the memory area to write to. - /// The height of the memory area to write to. - /// The depth of the memory area to write to. - public void SetData(T[] source, int offset, int x, int y, int z, int width, int height, int depth) - { - SetData(source.AsSpan(offset), x, y, z, width, height, depth); - } - - /// - /// Writes the contents of a given to the current instance. - /// The input data will be written to the start of the texture, and all input items will be copied. - /// - /// The input to read data from. - public void SetData(ReadOnlySpan source) - { - SetData(source, 0, 0, 0, Width, Height, Depth); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The horizontal range of items to write. - /// The vertical range of items to write. - /// The depthwise range of items to write. - public void SetData(ReadOnlySpan source, Range x, Range y, Range z) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - var (offsetZ, depth) = z.GetOffsetAndLength(Depth); - - SetData(source, offsetX, offsetY, offsetZ, width, height, depth); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offseet in the destination texture. - /// The width of the memory area to write to. - /// The height of the memory area to write to. - /// The depth of the memory area to write to. - public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// diff --git a/src/ComputeSharp/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Extensions/Texture3DExtensions.cs new file mode 100644 index 00000000..850378a5 --- /dev/null +++ b/src/ComputeSharp/Extensions/Texture3DExtensions.cs @@ -0,0 +1,303 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Buffers.Abstract; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp +{ + /// + /// A that contains extension methods for the type. + /// + public static class Texture3DExtensions + { + /// + /// Reads the contents of the current instance and returns an array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// A array with the contents of the current buffer. + /// + /// The returned array will be using the same memory layout as the texture, that is, each 2D plane + /// in the 3D volume represented by the texture is contiguous in memory, and planes are stacked in + /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. + /// + [Pure] + public static T[,,] GetData(this Texture3D texture) + where T : unmanaged + { + T[,,] data = new T[texture.Depth, texture.Height, texture.Width]; + + texture.GetData(data); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// + /// The input 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 [, , ]. + /// + public static void GetData(this Texture3D texture, T[,,] destination) + where T : unmanaged + { + Guard.IsEqualTo(destination.GetLength(0), texture.Depth, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), texture.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(2), texture.Width, nameof(destination)); + + texture.GetData(ref destination[0, 0, 0], destination.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + public static void GetData(this Texture3D texture, T[] destination, int offset) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The horizontal range of items to copy. + /// The vertical range of items to copy. + /// The depthwise range of items to copy. + public static void GetData(this Texture3D texture, T[] destination, int offset, Range x, Range y, Range z) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, z); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void GetData(this Texture3D texture, T[] destination, int offset, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + public static void GetData(this Texture3D texture, Span destination) + where T : unmanaged + { + texture.GetData(destination, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + /// The horizontal range in the source texture. + /// The vertical range in the source texture. + /// The depthwise range in the source texture. + public static void GetData(this Texture3D texture, Span destination, Range x, Range y, Range z) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + + texture.GetData(destination, offsetX, offsetY, offsetZ, width, height, depth); + } + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The input to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void GetData(this Texture3D texture, Span destination, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// + /// 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 [, , ]. + /// + public static void SetData(this Texture3D texture, T[,,] source) + where T : unmanaged + { + Guard.IsEqualTo(source.GetLength(0), texture.Depth, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), texture.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(2), texture.Width, nameof(source)); + + texture.SetData(ref source[0, 0, 0], source.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given array to the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + public static void SetData(this Texture3D texture, T[] source) + where T : unmanaged + { + texture.SetData(source.AsSpan(), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, T[] source, Range x, Range y, Range z) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The depth of the memory area to write to. + public static void SetData(this Texture3D texture, T[] source, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, T[] source, int offset, Range x, Range y, Range z) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, z); + } + + /// + /// Writes the contents of a given array to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The depth of the memory area to write to. + public static void SetData(this Texture3D texture, T[] source, int offset, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, z, width, height, depth); + } + + /// + /// Writes the contents of a given to the current instance. + /// The input data will be written to the start of the texture, and all input items will be copied. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + public static void SetData(this Texture3D texture, ReadOnlySpan source) + where T : unmanaged + { + texture.SetData(source, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + /// The horizontal range of items to write. + /// The vertical range of items to write. + /// The depthwise range of items to write. + public static void SetData(this Texture3D texture, ReadOnlySpan source, Range x, Range y, Range z) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + + texture.SetData(source, offsetX, offsetY, offsetZ, width, height, depth); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offseet in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The depth of the memory area to write to. + public static void SetData(this Texture3D texture, ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + texture.SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); + } + } +}