From 1de7a7c9c0ed1043938917e2566ba64641ac8f0b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 19:41:19 +0100 Subject: [PATCH] Added overloads to Texture3D APIs --- .../Buffers/Abstract/Texture2D{T}.cs | 6 + .../Buffers/Abstract/Texture3D{T}.cs | 217 +++++++++++++++++- 2 files changed, 222 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 1636198d..1140055e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -119,6 +119,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to write data to. public void GetData(T[,] destination) { + Guard.IsEqualTo(destination.GetLength(0), Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), Width, nameof(destination)); + fixed (T* p = destination) { GetData(new Span(p, destination.Length)); @@ -278,6 +281,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to read data from. public void SetData(T[,] source) { + Guard.IsEqualTo(source.GetLength(0), Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), Width, nameof(source)); + fixed (T* p = source) { SetData(new Span(p, source.Length)); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 36fe6f9b..08063ddd 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; @@ -106,6 +107,109 @@ 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, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + 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 . /// @@ -178,7 +282,118 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - /// Writes the contents of a given to a specified area of the current instance. + /// Writes the contents of a given array to the current instance. + /// + /// The input array to read data from. + /// + /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, unlike .NET arrays + /// which are traditionally of size [H, W, D], the input one needs to have a layout of [D, H, W]. + /// + 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.