Refactored public Texture2D<T> APIs

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-06 22:21:07 +01:00
1 親 0e2d604f47
コミット 4c5eeb9a9a
6ファイル変更
+326 -255

検索条件に一致するファイルはありません

+1 -250
ファイルの表示
@@ -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
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and returns an array.
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer.</returns>
[Pure]
public T[,] GetData()
{
T[,] data = new T[Height, Width];
GetData(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
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<T>(p, destination.Length));
}
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
public void GetData(T[] destination)
{
GetData(destination.AsSpan(), 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(T[] destination, Range x, Range y)
{
GetData(destination.AsSpan(), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(T[] destination, int x, int y, int width, int height)
{
GetData(destination.AsSpan(), x, y, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
public void GetData(T[] destination, int offset)
{
GetData(destination.AsSpan(offset), 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(T[] destination, int offset, Range x, Range y)
{
GetData(destination.AsSpan(offset), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(T[] destination, int offset, int x, int y, int width, int height)
{
GetData(destination.AsSpan(offset), x, y, width, height);
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// The input data will be read from the start of the texture.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public void GetData(Span<T> destination)
{
GetData(destination, 0, 0, Width, Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public void GetData(Span<T> destination, Range x, Range y)
{
var (offsetX, width) = x.GetOffsetAndLength(Width);
var (offsetY, height) = y.GetOffsetAndLength(Height);
GetData(destination, offsetX, offsetY, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(Span<T> destination, int x, int y, int width, int height)
{
GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -300,117 +162,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
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<T>(p, source.Length));
}
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public void SetData(T[] source)
{
SetData(source.AsSpan(), 0, 0, Width, Height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(T[] source, Range x, Range y)
{
SetData(source.AsSpan(), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(T[] source, int x, int y, int width, int height)
{
SetData(source.AsSpan(), x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(T[] source, int offset, Range x, Range y)
{
SetData(source.AsSpan(offset), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(T[] source, int offset, int x, int y, int width, int height)
{
SetData(source.AsSpan(offset), x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="Texture2D{T}"/> instance.
/// The input data will be written to the start of the texture, and all input items will be copied.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public void SetData(ReadOnlySpan<T> source)
{
SetData(source, 0, 0, Width, Height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public void SetData(ReadOnlySpan<T> source, Range x, Range y)
{
var (offsetX, width) = x.GetOffsetAndLength(Width);
var (offsetY, height) = y.GetOffsetAndLength(Height);
SetData(source, offsetX, offsetY, width, height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> source, int x, int y, int width, int height)
{
SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height);
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
+1 -2
ファイルの表示
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Exceptions;
+8 -1
ファイルの表示
@@ -16,7 +16,14 @@ namespace ComputeSharp.Graphics.Buffers.Views
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance with the items to display.</param>
public BufferDebugView2D(Texture2D<T>? 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;
}
}
/// <summary>
+1 -2
ファイルの表示
@@ -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<T>(buffer.Length);
buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0);
buffer.GetData(ref items[0], buffer.Length, 0);
Items = items;
}
+1
ファイルの表示
@@ -4,6 +4,7 @@
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
+314
ファイルの表示
@@ -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
{
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="Texture2D{T}"/> type.
/// </summary>
public static class Texture2DExtensions
{
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the current texture.</returns>
[Pure]
public static T[,] GetData<T>(this Texture2D<T> texture)
where T : unmanaged
{
T[,] data = new T[texture.Height, texture.Width];
texture.GetData(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
public static void GetData<T>(this Texture2D<T> 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);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
public static void GetData<T>(this Texture2D<T> texture, T[] destination)
where T : unmanaged
{
texture.GetData(destination.AsSpan(), 0, 0, texture.Width, texture.Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public static void GetData<T>(this Texture2D<T> texture, T[] destination, Range x, Range y)
where T : unmanaged
{
texture.GetData(destination.AsSpan(), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public static void GetData<T>(this Texture2D<T> texture, T[] destination, int x, int y, int width, int height)
where T : unmanaged
{
texture.GetData(destination.AsSpan(), x, y, width, height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
public static void GetData<T>(this Texture2D<T> texture, T[] destination, int offset)
where T : unmanaged
{
texture.GetData(destination.AsSpan(offset), 0, 0, texture.Width, texture.Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public static void GetData<T>(this Texture2D<T> texture, T[] destination, int offset, Range x, Range y)
where T : unmanaged
{
texture.GetData(destination.AsSpan(offset), x, y);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public static void GetData<T>(this Texture2D<T> 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);
}
/// <summary>
/// Reads the contents of the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// The input data will be read from the start of the texture.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public static void GetData<T>(this Texture2D<T> texture, Span<T> destination)
where T : unmanaged
{
texture.GetData(destination, 0, 0, texture.Width, texture.Height);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
/// <param name="y">The vertical range of items to copy.</param>
public static void GetData<T>(this Texture2D<T> texture, Span<T> 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);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture2D{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
public static void GetData<T>(this Texture2D<T> texture, Span<T> destination, int x, int y, int width, int height)
where T : unmanaged
{
texture.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public static unsafe void SetData<T>(this Texture2D<T> 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);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public static void SetData<T>(this Texture2D<T> texture, T[] source)
where T : unmanaged
{
texture.SetData(source.AsSpan(), 0, 0, texture.Width, texture.Height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public static void SetData<T>(this Texture2D<T> texture, T[] source, Range x, Range y)
where T : unmanaged
{
texture.SetData(source.AsSpan(), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public static void SetData<T>(this Texture2D<T> texture, T[] source, int x, int y, int width, int height)
where T : unmanaged
{
texture.SetData(source.AsSpan(), x, y, width, height);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public static void SetData<T>(this Texture2D<T> texture, T[] source, int offset, Range x, Range y)
where T : unmanaged
{
texture.SetData(source.AsSpan(offset), x, y);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public static void SetData<T>(this Texture2D<T> 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);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="Texture2D{T}"/> instance.
/// The input data will be written to the start of the texture, and all input items will be copied.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public static void SetData<T>(this Texture2D<T> texture, ReadOnlySpan<T> source)
where T : unmanaged
{
texture.SetData(source, 0, 0, texture.Width, texture.Height);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
/// <param name="y">The vertical range of items to write.</param>
public static void SetData<T>(this Texture2D<T> texture, ReadOnlySpan<T> 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);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The target <see cref="Texture2D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
public static void SetData<T>(this Texture2D<T> texture, ReadOnlySpan<T> source, int x, int y, int width, int height)
where T : unmanaged
{
texture.SetData(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height);
}
}
}