From 4c5eeb9a9a18b3fa77ab3ba2347a1527b548a50a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 22:21:07 +0100 Subject: [PATCH] Refactored public Texture2D APIs --- .../Buffers/Abstract/Texture2D{T}.cs | 251 +------------- .../Buffers/ConstantBuffer{T}.cs | 3 +- .../Buffers/Views/BufferDebugView2D{T}.cs | 9 +- .../Buffers/Views/BufferDebugView{T}.cs | 3 +- src/ComputeSharp/ComputeSharp.csproj | 1 + .../Extensions/Texture2DExtensions.cs | 314 ++++++++++++++++++ 6 files changed, 326 insertions(+), 255 deletions(-) create mode 100644 src/ComputeSharp/Extensions/Texture2DExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 8f422a98..780e81de 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{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; @@ -99,141 +96,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. - [Pure] - public T[,] GetData() - { - T[,] data = new T[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. - 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)); - } - } - - /// - /// 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. - public void GetData(T[] destination) - { - GetData(destination.AsSpan(), 0, 0, Width, Height); - } - - /// - /// 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 horizontal range of items to copy. - /// The vertical range of items to copy. - public void GetData(T[] destination, Range x, Range y) - { - GetData(destination.AsSpan(), x, y); - } - - /// - /// 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 horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The width of the memory area to copy. - /// The height of the memory area to copy. - public void GetData(T[] destination, int x, int y, int width, int height) - { - GetData(destination.AsSpan(), x, y, width, height); - } - - /// - /// 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, Width, Height); - } - - /// - /// 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. - public void GetData(T[] destination, int offset, Range x, Range y) - { - GetData(destination.AsSpan(offset), x, y); - } - - /// - /// 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 width of the memory area to copy. - /// The height of the memory area to copy. - public void GetData(T[] destination, int offset, int x, int y, int width, int height) - { - GetData(destination.AsSpan(offset), x, y, width, height); - } - - /// - /// Reads the contents of the current instance and writes them into a target . - /// The input data will be read from the start of the texture. - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0, 0, Width, Height); - } - - /// - /// 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 of items to copy. - /// The vertical range of items to copy. - public void GetData(Span destination, Range x, Range y) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - - GetData(destination, offsetX, offsetY, width, height); - } - - /// - /// 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 width of the memory area to copy. - /// The height of the memory area to copy. - public void GetData(Span destination, int x, int y, int width, int height) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -300,117 +162,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } } - /// - /// Writes the contents of a given array to the current instance. - /// - /// 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)); - } - } - - /// - /// 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, Width, Height); - } - - /// - /// 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. - public void SetData(T[] source, Range x, Range y) - { - SetData(source.AsSpan(), x, y); - } - - /// - /// 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 width of the memory area to write to. - /// The height of the memory area to write to. - public void SetData(T[] source, int x, int y, int width, int height) - { - SetData(source.AsSpan(), x, y, width, height); - } - - /// - /// 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. - public void SetData(T[] source, int offset, Range x, Range y) - { - SetData(source.AsSpan(offset), x, y); - } - - /// - /// 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 width of the memory area to write to. - /// The height of the memory area to write to. - public void SetData(T[] source, int offset, int x, int y, int width, int height) - { - SetData(source.AsSpan(offset), x, y, width, height); - } - - /// - /// 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, Width, Height); - } - - /// - /// 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. - public void SetData(ReadOnlySpan source, Range x, Range y) - { - var (offsetX, width) = x.GetOffsetAndLength(Width); - var (offsetY, height) = y.GetOffsetAndLength(Height); - - SetData(source, offsetX, offsetY, width, height); - } - - /// - /// 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 width of the memory area to write to. - /// The height of the memory area to write to. - public void SetData(ReadOnlySpan source, int x, int y, int width, int height) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 0c8d2e08..732c78f2 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs index c3061ea1..720d46df 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView2D{T}.cs @@ -16,7 +16,14 @@ namespace ComputeSharp.Graphics.Buffers.Views /// The input instance with the items to display. public BufferDebugView2D(Texture2D? texture) { - Items = texture?.GetData(); + if (texture is not null) + { + var items = new T[texture.Height, texture.Width]; + + texture.GetData(ref items[0, 0], items.Length, 0, 0, texture.Height, texture.Width); + + Items = items; + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs index 76b35a60..59950292 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp.Graphics.Buffers.Views @@ -22,7 +21,7 @@ namespace ComputeSharp.Graphics.Buffers.Views { var items = GC.AllocateUninitializedArray(buffer.Length); - buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0); + buffer.GetData(ref items[0], buffer.Length, 0); Items = items; } diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index e2f82a06..a4aa8e9e 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -4,6 +4,7 @@ net5.0-windows10.0.19041.0 preview enable + true diff --git a/src/ComputeSharp/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Extensions/Texture2DExtensions.cs new file mode 100644 index 00000000..4c1b03e2 --- /dev/null +++ b/src/ComputeSharp/Extensions/Texture2DExtensions.cs @@ -0,0 +1,314 @@ +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 Texture2DExtensions + { + /// + /// 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 texture. + [Pure] + public static T[,] GetData(this Texture2D texture) + where T : unmanaged + { + T[,] data = new T[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. + public static void GetData(this Texture2D texture, T[,] destination) + where T : unmanaged + { + Guard.IsEqualTo(destination.GetLength(0), texture.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), texture.Width, nameof(destination)); + + texture.GetData(ref destination[0, 0], destination.Length, 0, 0, texture.Width, texture.Height); + } + + /// + /// 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. + public static void GetData(this Texture2D texture, T[] destination) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), 0, 0, texture.Width, texture.Height); + } + + /// + /// 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 horizontal range of items to copy. + /// The vertical range of items to copy. + public static void GetData(this Texture2D texture, T[] destination, Range x, Range y) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), x, y); + } + + /// + /// 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 horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void GetData(this Texture2D texture, T[] destination, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(destination.AsSpan(), x, y, width, height); + } + + /// + /// 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 Texture2D texture, T[] destination, int offset) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), 0, 0, texture.Width, texture.Height); + } + + /// + /// 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. + public static void GetData(this Texture2D texture, T[] destination, int offset, Range x, Range y) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y); + } + + /// + /// 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 width of the memory area to copy. + /// The height of the memory area to copy. + public static void GetData(this Texture2D texture, T[] destination, int offset, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(destination.AsSpan(offset), x, y, width, height); + } + + /// + /// Reads the contents of the current instance and writes them into a target . + /// The input data will be read from the start of the texture. + /// + /// 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 Texture2D texture, Span destination) + where T : unmanaged + { + texture.GetData(destination, 0, 0, texture.Width, texture.Height); + } + + /// + /// 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 of items to copy. + /// The vertical range of items to copy. + public static void GetData(this Texture2D texture, Span destination, Range x, Range y) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + + texture.GetData(destination, offsetX, offsetY, width, height); + } + + /// + /// 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 width of the memory area to copy. + /// The height of the memory area to copy. + public static void GetData(this Texture2D texture, Span destination, int x, int y, int width, int height) + where T : unmanaged + { + texture.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); + } + + /// + /// 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 unsafe void SetData(this Texture2D texture, T[,] source) + where T : unmanaged + { + Guard.IsEqualTo(source.GetLength(0), texture.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), texture.Width, nameof(source)); + + texture.SetData(ref source[0, 0], source.Length, 0, 0, texture.Width, texture.Height); + } + + /// + /// 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 Texture2D texture, T[] source) + where T : unmanaged + { + texture.SetData(source.AsSpan(), 0, 0, texture.Width, texture.Height); + } + + /// + /// 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. + public static void SetData(this Texture2D texture, T[] source, Range x, Range y) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y); + } + + /// + /// 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 width of the memory area to write to. + /// The height of the memory area to write to. + public static void SetData(this Texture2D texture, T[] source, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(source.AsSpan(), x, y, width, height); + } + + /// + /// 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. + public static void SetData(this Texture2D texture, T[] source, int offset, Range x, Range y) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y); + } + + /// + /// 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 width of the memory area to write to. + /// The height of the memory area to write to. + public static void SetData(this Texture2D texture, T[] source, int offset, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(source.AsSpan(offset), x, y, width, height); + } + + /// + /// 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 Texture2D texture, ReadOnlySpan source) + where T : unmanaged + { + texture.SetData(source, 0, 0, texture.Width, texture.Height); + } + + /// + /// 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. + public static void SetData(this Texture2D texture, ReadOnlySpan source, Range x, Range y) + where T : unmanaged + { + var (offsetX, width) = x.GetOffsetAndLength(texture.Width); + var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + + texture.SetData(source, offsetX, offsetY, width, height); + } + + /// + /// 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 width of the memory area to write to. + /// The height of the memory area to write to. + public static void SetData(this Texture2D texture, ReadOnlySpan source, int x, int y, int width, int height) + where T : unmanaged + { + texture.SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); + } + } +}