Merge pull request #372 from Sergio0694/dev/texture1d

Add Texture1D<T> and Texture1D<T, TPixel> types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-09-01 03:10:50 +02:00
コミット cbd385242a
49ファイル変更
+4623 -11

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

+3
ファイルの表示
@@ -10,6 +10,9 @@ partial class HlslKnownMethods
{
return new Dictionary<string, string?>
{
[$"ComputeSharp.ReadOnlyTexture1D`2.Sample({typeof(float).FullName})"] = null,
[$"ComputeSharp.IReadOnlyTexture1D`1.Sample({typeof(float).FullName})"] = null,
[$"ComputeSharp.IReadOnlyNormalizedTexture1D`1.Sample({typeof(float).FullName})"] = null,
[$"ComputeSharp.ReadOnlyTexture2D`2.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2",
[$"ComputeSharp.ReadOnlyTexture2D`2.Sample({typeof(Float2).FullName})"] = null,
[$"ComputeSharp.IReadOnlyTexture2D`1.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2",
+4
ファイルの表示
@@ -38,21 +38,25 @@ partial class HlslKnownProperties
return new Dictionary<string, (int, int)>
{
["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0),
["ComputeSharp.Resources.Texture1D`1.Width"] = (1, 0),
["ComputeSharp.Resources.Texture2D`1.Width"] = (2, 0),
["ComputeSharp.Resources.Texture2D`1.Height"] = (2, 1),
["ComputeSharp.Resources.Texture3D`1.Width"] = (3, 0),
["ComputeSharp.Resources.Texture3D`1.Height"] = (3, 1),
["ComputeSharp.Resources.Texture3D`1.Depth"] = (3, 2),
["ComputeSharp.IReadOnlyTexture1D`1.Width"] = (1, 0),
["ComputeSharp.IReadOnlyTexture2D`1.Width"] = (2, 0),
["ComputeSharp.IReadOnlyTexture2D`1.Height"] = (2, 1),
["ComputeSharp.IReadOnlyTexture3D`1.Width"] = (3, 0),
["ComputeSharp.IReadOnlyTexture3D`1.Height"] = (3, 1),
["ComputeSharp.IReadOnlyTexture3D`1.Depth"] = (3, 2),
["ComputeSharp.IReadOnlyNormalizedTexture1D`1.Width"] = (1, 0),
["ComputeSharp.IReadOnlyNormalizedTexture2D`1.Width"] = (2, 0),
["ComputeSharp.IReadOnlyNormalizedTexture2D`1.Height"] = (2, 1),
["ComputeSharp.IReadOnlyNormalizedTexture3D`1.Width"] = (3, 0),
["ComputeSharp.IReadOnlyNormalizedTexture3D`1.Height"] = (3, 1),
["ComputeSharp.IReadOnlyNormalizedTexture3D`1.Depth"] = (3, 2),
["ComputeSharp.IReadWriteNormalizedTexture1D`1.Width"] = (1, 0),
["ComputeSharp.IReadWriteNormalizedTexture2D`1.Width"] = (2, 0),
["ComputeSharp.IReadWriteNormalizedTexture2D`1.Height"] = (2, 1),
["ComputeSharp.IReadWriteNormalizedTexture3D`1.Width"] = (3, 0),
+21
ファイルの表示
@@ -55,12 +55,16 @@ partial class HlslKnownTypes
return typeName switch
{
"ComputeSharp.ReadOnlyBuffer`1" or
"ComputeSharp.ReadOnlyTexture1D`1" or
"ComputeSharp.ReadOnlyTexture1D`2" or
"ComputeSharp.ReadOnlyTexture2D`1" or
"ComputeSharp.ReadOnlyTexture2D`2" or
"ComputeSharp.ReadOnlyTexture3D`1" or
"ComputeSharp.ReadOnlyTexture3D`2" or
"ComputeSharp.IReadOnlyTexture1D`1" or
"ComputeSharp.IReadOnlyTexture2D`1" or
"ComputeSharp.IReadOnlyTexture3D`1" => true,
"ComputeSharp.IReadOnlyNormalizedTexture1D`1" or
"ComputeSharp.IReadOnlyNormalizedTexture2D`1" or
"ComputeSharp.IReadOnlyNormalizedTexture3D`1" => true,
_ => false
@@ -77,10 +81,13 @@ partial class HlslKnownTypes
return typeName switch
{
"ComputeSharp.ReadWriteBuffer`1" or
"ComputeSharp.ReadWriteTexture1D`1" or
"ComputeSharp.ReadWriteTexture1D`2" or
"ComputeSharp.ReadWriteTexture2D`1" or
"ComputeSharp.ReadWriteTexture2D`2" or
"ComputeSharp.ReadWriteTexture3D`1" or
"ComputeSharp.ReadWriteTexture3D`2" or
"ComputeSharp.IReadWriteNormalizedTexture1D`1" or
"ComputeSharp.IReadWriteNormalizedTexture2D`1" or
"ComputeSharp.IReadWriteNormalizedTexture3D`1" => true,
_ => false
@@ -99,16 +106,23 @@ partial class HlslKnownTypes
"ComputeSharp.ConstantBuffer`1" or
"ComputeSharp.ReadOnlyBuffer`1" or
"ComputeSharp.ReadWriteBuffer`1" or
"ComputeSharp.ReadOnlyTexture1D`1" or
"ComputeSharp.ReadOnlyTexture1D`2" or
"ComputeSharp.ReadOnlyTexture2D`1" or
"ComputeSharp.ReadOnlyTexture2D`2" or
"ComputeSharp.ReadWriteTexture1D`1" or
"ComputeSharp.ReadWriteTexture1D`2" or
"ComputeSharp.ReadWriteTexture2D`1" or
"ComputeSharp.ReadWriteTexture2D`2" or
"ComputeSharp.ReadOnlyTexture3D`1" or
"ComputeSharp.ReadOnlyTexture3D`2" or
"ComputeSharp.ReadWriteTexture3D`1" or
"ComputeSharp.ReadWriteTexture3D`2" or
"ComputeSharp.IReadOnlyTexture1D`1" or
"ComputeSharp.IReadOnlyTexture2D`1" or
"ComputeSharp.IReadOnlyTexture3D`1" or
"ComputeSharp.IReadOnlyNormalizedTexture1D`1" or
"ComputeSharp.IReadWriteNormalizedTexture1D`1" or
"ComputeSharp.IReadOnlyNormalizedTexture2D`1" or
"ComputeSharp.IReadWriteNormalizedTexture2D`1" or
"ComputeSharp.IReadOnlyNormalizedTexture3D`1" or
@@ -143,6 +157,10 @@ partial class HlslKnownTypes
"ComputeSharp.ConstantBuffer`1" => mappedElementType,
"ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mappedElementType}>",
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mappedElementType}>",
"ComputeSharp.ReadOnlyTexture1D`1" => $"Texture1D<{mappedElementType}>",
"ComputeSharp.ReadOnlyTexture1D`2" => $"Texture1D<unorm {mappedElementType}>",
"ComputeSharp.ReadWriteTexture1D`1" => $"RWTexture1D<{mappedElementType}>",
"ComputeSharp.ReadWriteTexture1D`2" => $"RWTexture1D<unorm {mappedElementType}>",
"ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mappedElementType}>",
"ComputeSharp.ReadOnlyTexture2D`2" => $"Texture2D<unorm {mappedElementType}>",
"ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mappedElementType}>",
@@ -151,8 +169,11 @@ partial class HlslKnownTypes
"ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D<unorm {mappedElementType}>",
"ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mappedElementType}>",
"ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D<unorm {mappedElementType}>",
"ComputeSharp.IReadOnlyTexture1D`1" => $"Texture1D<{mappedElementType}>",
"ComputeSharp.IReadOnlyTexture2D`1" => $"Texture2D<{mappedElementType}>",
"ComputeSharp.IReadOnlyTexture3D`1" => $"Texture3D<{mappedElementType}>",
"ComputeSharp.IReadOnlyNormalizedTexture1D`1" => $"Texture1D<unorm {mappedElementType}>",
"ComputeSharp.IReadWriteNormalizedTexture1D`1" => $"RWTexture1D<unorm {mappedElementType}>",
"ComputeSharp.IReadOnlyNormalizedTexture2D`1" => $"Texture2D<unorm {mappedElementType}>",
"ComputeSharp.IReadWriteNormalizedTexture2D`1" => $"RWTexture2D<unorm {mappedElementType}>",
"ComputeSharp.IReadOnlyNormalizedTexture3D`1" => $"Texture3D<unorm {mappedElementType}>",
+24
ファイルの表示
@@ -55,6 +55,18 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException
throw Create(buffer, buffer.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
/// <param name="texture">The input <see cref="Texture1D{T}"/> that was used.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
internal static void Throw<T>(Texture1D<T> texture, GraphicsDevice device)
where T : unmanaged
{
throw Create(texture, texture.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
@@ -91,6 +103,18 @@ public sealed class GraphicsDeviceMismatchException : InvalidOperationException
throw Create(buffer, buffer.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
/// <param name="texture">The input <see cref="TransferTexture1D{T}"/> that was used.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
internal static void Throw<T>(TransferTexture1D<T> texture, GraphicsDevice device)
where T : unmanaged
{
throw Create(texture, texture.GraphicsDevice, device);
}
/// <summary>
/// Throws a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
/// </summary>
+11
ファイルの表示
@@ -33,6 +33,7 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException
builder.AppendLine(rank switch
{
1 => $"{nameof(GraphicsDevice.IsReadOnlyTexture1DSupportedForType)}<T>() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture1DSupportedForType)}<T>().",
2 => $"{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}<T>() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}<T>().",
3 => $"{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}<T>() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}<T>().",
_ => ThrowHelper.ThrowArgumentException<string>("Invalid texture rank.")
@@ -43,6 +44,16 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException
return new(builder.ToString());
}
/// <summary>
/// Throws a new <see cref="UnsupportedTextureTypeException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the texture that couldn't be created.</typeparam>
internal static void ThrowForTexture1D<T>()
where T : unmanaged
{
throw Create(1, typeof(T));
}
/// <summary>
/// Throws a new <see cref="UnsupportedTextureTypeException"/> instance from the specified parameters.
/// </summary>
+412
ファイルの表示
@@ -160,6 +160,196 @@ partial class GraphicsDeviceExtensions
return readWriteBuffer;
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadOnlyTexture1D{T}"/> instance of size [<paramref name="width"/>].</returns>
public static ReadOnlyTexture1D<T> AllocateReadOnlyTexture1D<T>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T> AllocateReadOnlyTexture1D<T>(this GraphicsDevice device, T[] source, int width)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadOnlyTexture1D<T>(source.AsSpan(0, width));
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T> AllocateReadOnlyTexture1D<T>(this GraphicsDevice device, T[] source, int offset, int width)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadOnlyTexture1D<T>(source.AsSpan(offset, width));
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadOnlyTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T> AllocateReadOnlyTexture1D<T>(this GraphicsDevice device, T[] source)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
ReadOnlyTexture1D<T> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
public static ReadOnlyTexture1D<T> AllocateReadOnlyTexture1D<T>(this GraphicsDevice device, ReadOnlySpan<T> source)
where T : unmanaged
{
Guard.IsNotNull(device);
ReadOnlyTexture1D<T> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance of size [<paramref name="width"/>].</returns>
public static ReadOnlyTexture1D<T, TPixel> AllocateReadOnlyTexture1D<T, TPixel>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T, TPixel> AllocateReadOnlyTexture1D<T, TPixel>(this GraphicsDevice device, T[] source, int width)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadOnlyTexture1D<T, TPixel>(source.AsSpan(0, width));
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T, TPixel> AllocateReadOnlyTexture1D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadOnlyTexture1D<T, TPixel>(source.AsSpan(offset, width));
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadOnlyTexture1D<T, TPixel> AllocateReadOnlyTexture1D<T, TPixel>(this GraphicsDevice device, T[] source)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
ReadOnlyTexture1D<T, TPixel> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new readonly 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <returns>A <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
public static ReadOnlyTexture1D<T, TPixel> AllocateReadOnlyTexture1D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
ReadOnlyTexture1D<T, TPixel> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new readonly 2D texture with the specified parameters.
/// </summary>
@@ -651,6 +841,196 @@ partial class GraphicsDeviceExtensions
return readWriteBuffer;
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadWriteTexture1D{T}"/> instance of size [<paramref name="width"/>].</returns>
public static ReadWriteTexture1D<T> AllocateReadWriteTexture1D<T>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T> AllocateReadWriteTexture1D<T>(this GraphicsDevice device, T[] source, int width)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadWriteTexture1D<T>(source.AsSpan(0, width));
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T> AllocateReadWriteTexture1D<T>(this GraphicsDevice device, T[] source, int offset, int width)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadWriteTexture1D<T>(source.AsSpan(offset, width));
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadWriteTexture1D{T}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T> AllocateReadWriteTexture1D<T>(this GraphicsDevice device, T[] source)
where T : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
ReadWriteTexture1D<T> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
public static ReadWriteTexture1D<T> AllocateReadWriteTexture1D<T>(this GraphicsDevice device, ReadOnlySpan<T> source)
where T : unmanaged
{
Guard.IsNotNull(device);
ReadWriteTexture1D<T> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A zeroed <see cref="ReadWriteTexture1D{T, TPixel}"/> instance of size [<paramref name="width"/>].</returns>
public static ReadWriteTexture1D<T, TPixel> AllocateReadWriteTexture1D<T, TPixel>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T, TPixel> AllocateReadWriteTexture1D<T, TPixel>(this GraphicsDevice device, T[] source, int width)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadWriteTexture1D<T, TPixel>(source.AsSpan(0, width));
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <param name="offset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="width">The width of the texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T, TPixel> AllocateReadWriteTexture1D<T, TPixel>(this GraphicsDevice device, T[] source, int offset, int width)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
return device.AllocateReadWriteTexture1D<T, TPixel>(source.AsSpan(offset, width));
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <typeparamref name="T"/> array with the data to copy on the allocated texture.</param>
/// <returns>A read write <see cref="ReadWriteTexture1D{T, TPixel}"/> instance with the contents of the input array.</returns>
public static ReadWriteTexture1D<T, TPixel> AllocateReadWriteTexture1D<T, TPixel>(this GraphicsDevice device, T[] source)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
Guard.IsNotNull(source);
ReadWriteTexture1D<T, TPixel> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new writeable 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> with the data to copy on the allocated texture.</param>
/// <returns>A <see cref="ReadWriteTexture1D{T, TPixel}"/> instance with the contents of the input <see cref="ReadOnlySpan{T}"/>.</returns>
public static ReadWriteTexture1D<T, TPixel> AllocateReadWriteTexture1D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<T> source)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(device);
ReadWriteTexture1D<T, TPixel> texture = new(device, source.Length, AllocationMode.Default);
texture.CopyFrom(source);
return texture;
}
/// <summary>
/// Allocates a new writeable 2D texture with the specified parameters.
/// </summary>
@@ -1084,6 +1464,22 @@ partial class GraphicsDeviceExtensions
return new(device, length, allocationMode);
}
/// <summary>
/// Allocates a new upload 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="UploadTexture1D{T}"/> instance of size [<paramref name="width"/>].</returns>
public static UploadTexture1D<T> AllocateUploadTexture1D<T>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new upload 2D texture with the specified parameters.
/// </summary>
@@ -1136,6 +1532,22 @@ partial class GraphicsDeviceExtensions
return new(device, length, allocationMode);
}
/// <summary>
/// Allocates a new readback 1D texture with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of items to store in the buffer.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="ReadBackTexture1D{T}"/> instance of size [<paramref name="width"/>].</returns>
public static ReadBackTexture1D<T> AllocateReadBackTexture1D<T>(this GraphicsDevice device, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
Guard.IsNotNull(device);
return new(device, width, allocationMode);
}
/// <summary>
/// Allocates a new readback 2D texture with the specified parameters.
/// </summary>
+51
ファイルの表示
@@ -90,6 +90,57 @@ internal static unsafe class AllocatorExtensions
return allocation.Move();
}
/// <summary>
/// Creates a resource for a given 1D texture type.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <param name="pool">The <see cref="D3D12MA_Pool"/> instance to use, if any.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ComPtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
public static ComPtr<D3D12MA_Allocation> CreateResource(
this ref D3D12MA_Allocator allocator,
D3D12MA_Pool* pool,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr<D3D12MA_Allocation> allocation = default;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags);
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = pool;
allocator.CreateResource(
&allocationDesc,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
allocation.GetAddressOf(),
null,
null).Assert();
return allocation.Move();
}
/// <summary>
/// Creates a resource for a given 2D texture type.
/// </summary>
+92
ファイルの表示
@@ -117,6 +117,66 @@ internal static unsafe class ID3D12DeviceExtensions
return d3D12Resource.Move();
}
/// <summary>
/// Creates a committed resource for a given 1D texture type.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="isCacheCoherentUMA">Indicates whether or not the current device has a cache coherent UMA architecture.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ID3D12Resource"/> reference for the current texture.</returns>
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3D12Device,
ResourceType resourceType,
DXGI_FORMAT dxgiFormat,
uint width,
bool isCacheCoherentUMA,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
};
using ComPtr<ID3D12Resource> d3D12Resource = default;
D3D12_HEAP_PROPERTIES d3D12HeapProperties;
d3D12HeapProperties.CreationNodeMask = 1;
d3D12HeapProperties.VisibleNodeMask = 1;
if (isCacheCoherentUMA)
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else
{
d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
}
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags);
d3D12Device.CreateCommittedResource(
&d3D12HeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
Windows.__uuidof<ID3D12Resource>(),
d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
/// <summary>
/// Creates a committed resource for a given 2D texture type.
/// </summary>
@@ -551,6 +611,38 @@ internal static unsafe class ID3D12DeviceExtensions
return d3D12GraphicsCommandList.Move();
}
/// <summary>
/// Gets the layout data for a target resource.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to get the layout info.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="d3D12PlacedSubresourceFootprint">The resulting layout info for the resource.</param>
/// <param name="rowSizeInBytes">The size in bytes of each row in the resource.</param>
/// <param name="totalSizeInBytes">The total number of bytes for the resource.</param>
public static void GetCopyableFootprint(
this ref ID3D12Device d3D12Device,
DXGI_FORMAT dxgiFormat,
uint width,
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint,
out ulong rowSizeInBytes,
out ulong totalSizeInBytes)
{
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width);
ulong a;
ulong b;
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* p = &d3D12PlacedSubresourceFootprint)
{
uint _;
d3D12Device.GetCopyableFootprints(&d3D12ResourceDescription, 0, 1, 0, p, &_, &a, &b);
}
rowSizeInBytes = a;
totalSizeInBytes = b;
}
/// <summary>
/// Gets the layout data for a target resource.
/// </summary>
+60
ファイルの表示
@@ -0,0 +1,60 @@
using CommunityToolkit.Diagnostics;
using ComputeSharp.Resources;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="ReadBackTexture1D{T}"/> type.
/// </summary>
public static class ReadBackTexture1DExtensions
{
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
public static void CopyFrom<T>(this ReadBackTexture1D<T> destination, Texture1D<T> source)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this ReadBackTexture1D<T> destination, Texture1D<T> source, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this ReadBackTexture1D<T> destination, Texture1D<T> source, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width);
}
}
+144
ファイルの表示
@@ -0,0 +1,144 @@
using System;
using CommunityToolkit.Diagnostics;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="ReadWriteTexture1D{T}"/> and <see cref="ReadWriteTexture1D{T, TPixel}"/> types.
/// </summary>
public static class ReadWriteTexture1DExtensions
{
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture1D{T}"/> instance for the input resource.
/// </summary>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to create a wrapper for.</param>
/// <returns>An <see cref="IReadOnlyTexture1D{T}"/> instance wrapping the current resource.</returns>
/// <remarks>
/// <para>The returned instance can be used in a shader to enable texture sampling.</para>
/// <para>
/// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so,
/// use <see cref="ComputeContextExtensions.Transition(in ComputeContext, ReadWriteTexture1D{float}, ResourceState)"/>,
/// and specify <see cref="ResourceState.ReadOnly"/>. After that, this method can be used to get a readonly wrapper for
/// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader
/// being dispatched through that same <see cref="ComputeContext"/>, as it will not work if the texture changes state later on.
/// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same
/// API as before but specifying <see cref="ResourceState.ReadWrite"/>. Failing to do so results in undefined behavior.
/// </para>
/// </remarks>
/// <exception cref="ObjectDisposedException">Thrown if the current instance or its associated device are disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current instance is not in a readonly state.</exception>
public static IReadOnlyTexture1D<float> AsReadOnly(this ReadWriteTexture1D<float> texture)
{
Guard.IsNotNull(texture);
return texture.AsReadOnly();
}
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture1D{T}"/> instance for the input resource.
/// </summary>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to create a wrapper for.</param>
/// <returns>An <see cref="IReadOnlyTexture1D{T}"/> instance wrapping the current resource.</returns>
/// <remarks>
/// <para>The returned instance can be used in a shader to enable texture sampling.</para>
/// <para>
/// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so,
/// use <see cref="ComputeContextExtensions.Transition(in ComputeContext, ReadWriteTexture1D{Float2}, ResourceState)"/>,
/// and specify <see cref="ResourceState.ReadOnly"/>. After that, this method can be used to get a readonly wrapper for
/// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader
/// being dispatched through that same <see cref="ComputeContext"/>, as it will not work if the texture changes state later on.
/// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same
/// API as before but specifying <see cref="ResourceState.ReadWrite"/>. Failing to do so results in undefined behavior.
/// </para>
/// </remarks>
/// <exception cref="ObjectDisposedException">Thrown if the current instance or its associated device are disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current instance is not in a readonly state.</exception>
public static IReadOnlyTexture1D<Float2> AsReadOnly(this ReadWriteTexture1D<Float2> texture)
{
Guard.IsNotNull(texture);
return texture.AsReadOnly();
}
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture1D{T}"/> instance for the input resource.
/// </summary>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to create a wrapper for.</param>
/// <returns>An <see cref="IReadOnlyTexture1D{T}"/> instance wrapping the current resource.</returns>
/// <remarks>
/// <para>The returned instance can be used in a shader to enable texture sampling.</para>
/// <para>
/// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so,
/// use <see cref="ComputeContextExtensions.Transition(in ComputeContext, ReadWriteTexture1D{Float3}, ResourceState)"/>,
/// and specify <see cref="ResourceState.ReadOnly"/>. After that, this method can be used to get a readonly wrapper for
/// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader
/// being dispatched through that same <see cref="ComputeContext"/>, as it will not work if the texture changes state later on.
/// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same
/// API as before but specifying <see cref="ResourceState.ReadWrite"/>. Failing to do so results in undefined behavior.
/// </para>
/// </remarks>
/// <exception cref="ObjectDisposedException">Thrown if the current instance or its associated device are disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current instance is not in a readonly state.</exception>
public static IReadOnlyTexture1D<Float3> AsReadOnly(this ReadWriteTexture1D<Float3> texture)
{
Guard.IsNotNull(texture);
return texture.AsReadOnly();
}
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyTexture1D{T}"/> instance for the input resource.
/// </summary>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to create a wrapper for.</param>
/// <returns>An <see cref="IReadOnlyTexture1D{T}"/> instance wrapping the current resource.</returns>
/// <remarks>
/// <para>The returned instance can be used in a shader to enable texture sampling.</para>
/// <para>
/// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so,
/// use <see cref="ComputeContextExtensions.Transition(in ComputeContext, ReadWriteTexture1D{Float4}, ResourceState)"/>,
/// and specify <see cref="ResourceState.ReadOnly"/>. After that, this method can be used to get a readonly wrapper for
/// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader
/// being dispatched through that same <see cref="ComputeContext"/>, as it will not work if the texture changes state later on.
/// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same
/// API as before but specifying <see cref="ResourceState.ReadWrite"/>. Failing to do so results in undefined behavior.
/// </para>
/// </remarks>
/// <exception cref="ObjectDisposedException">Thrown if the current instance or its associated device are disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current instance is not in a readonly state.</exception>
public static IReadOnlyTexture1D<Float4> AsReadOnly(this ReadWriteTexture1D<Float4> texture)
{
Guard.IsNotNull(texture);
return texture.AsReadOnly();
}
/// <summary>
/// Retrieves a wrapping <see cref="IReadOnlyNormalizedTexture1D{TPixel}"/> instance for the input resource.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T, TPixel}"/> instance to create a wrapper for.</param>
/// <returns>An <see cref="IReadOnlyNormalizedTexture1D{TPixel}"/> instance wrapping the current resource.</returns>
/// <remarks>
/// <para>The returned instance can be used in a shader to enable texture sampling.</para>
/// <para>
/// This is an advanced API that can only be used after the current instance has been transitioned to be in a readonly state. To do so,
/// use <see cref="ComputeContextExtensions.Transition{T, TPixel}(in ComputeContext, ReadWriteTexture1D{T, TPixel}, ResourceState)"/>,
/// and specify <see cref="ResourceState.ReadOnly"/>. After that, this method can be used to get a readonly wrapper for
/// the current texture to use in a shader. This instance should not be cached or reused, but just passed directly to a shader
/// being dispatched through that same <see cref="ComputeContext"/>, as it will not work if the texture changes state later on.
/// Before the end of that list of operations, the texture also needs to be transitioned back to writeable state, using the same
/// API as before but specifying <see cref="ResourceState.ReadWrite"/>. Failing to do so results in undefined behavior.
/// </para>
/// </remarks>
/// <exception cref="ObjectDisposedException">Thrown if the current instance or its associated device are disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current instance is not in a readonly state.</exception>
public static IReadOnlyNormalizedTexture1D<TPixel> AsReadOnly<T, TPixel>(this ReadWriteTexture1D<T, TPixel> texture)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
return texture.AsReadOnly();
}
}
+544
ファイルの表示
@@ -0,0 +1,544 @@
using System;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Resources;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="Texture1D{T}"/> type.
/// </summary>
public static class Texture1DExtensions
{
/// <summary>
/// Reads the contents of the current <see cref="Texture1D{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the current texture.</returns>
public static T[] ToArray<T>(this Texture1D<T> source)
where T : unmanaged
{
Guard.IsNotNull(source);
T[] data = new T[source.Width];
source.CopyTo(data);
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture1D{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the current texture.</returns>
public static unsafe T[] ToArray<T>(this Texture1D<T> source, int x, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsGreaterThanOrEqualTo(width, 0);
T[] data = new T[width];
fixed (T* p = data)
{
source.CopyTo(new Span<T>(p, width), x, width);
}
return data;
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(), 0, source.Width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{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>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination, Range x)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(), x);
}
#endif
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(), sourceOffsetX, width);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination, int destinationOffset)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(destinationOffset), 0, source.Width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
/// <param name="x">The horizontal range of items to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination, int destinationOffset, Range x)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(destinationOffset), x);
}
#endif
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, T[] destination, int destinationOffset, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, width);
}
/// <summary>
/// Reads the contents of the current <see cref="Texture1D{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="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public static void CopyTo<T>(this Texture1D<T> source, Span<T> destination)
where T : unmanaged
{
Guard.IsNotNull(source);
source.CopyTo(destination, 0, source.Width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{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="source">The input <see cref="Texture1D{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>
public static void CopyTo<T>(this Texture1D<T> source, Span<T> destination, Range x)
where T : unmanaged
{
Guard.IsNotNull(source);
var (offsetX, width) = x.GetOffsetAndLength(source.Width);
source.CopyTo(destination, offsetX, width);
}
#endif
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{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="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, Span<T> destination, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
public static void CopyTo<T>(this Texture1D<T> source, Texture1D<T> destination)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, Texture1D<T> destination, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, Texture1D<T> destination, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
public static void CopyTo<T>(this Texture1D<T> source, ReadBackTexture1D<T> destination)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, ReadBackTexture1D<T> destination, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of a <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this Texture1D<T> source, ReadBackTexture1D<T> destination, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, T[] source)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source.AsSpan(), 0, destination.Width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{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>
public static void CopyFrom<T>(this Texture1D<T> destination, T[] source, Range x)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source.AsSpan(), x);
}
#endif
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, T[] source, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source.AsSpan(), destinationOffsetX, width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="sourceOffset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="x">The horizontal range of items to write.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, T[] source, int sourceOffset, Range x)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source.AsSpan(sourceOffset), x);
}
#endif
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
/// <param name="sourceOffset">The starting offset within <paramref name="source"/> to read data from.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, T[] source, int sourceOffset, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, width);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="Texture1D{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="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, ReadOnlySpan<T> source)
where T : unmanaged
{
Guard.IsNotNull(destination);
destination.CopyFrom(source, 0, destination.Width);
}
#if NET6_0_OR_GREATER
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{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>
public static void CopyFrom<T>(this Texture1D<T> destination, ReadOnlySpan<T> source, Range x)
where T : unmanaged
{
Guard.IsNotNull(destination);
var (offsetX, width) = x.GetOffsetAndLength(destination.Width);
destination.CopyFrom(source, offsetX, width);
}
#endif
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, ReadOnlySpan<T> source, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, width);
}
/// <summary>
/// Reads the contents of an <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, Texture1D<T> source)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of an <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, Texture1D<T> source, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of an <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="Texture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, Texture1D<T> source, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, width);
}
/// <summary>
/// Reads the contents of an <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, UploadTexture1D<T> source)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
source.CopyTo(destination, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of an <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, UploadTexture1D<T> source, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of an <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyFrom<T>(this Texture1D<T> destination, UploadTexture1D<T> source, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(destination);
Guard.IsNotNull(source);
destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, width);
}
}
+60
ファイルの表示
@@ -0,0 +1,60 @@
using CommunityToolkit.Diagnostics;
using ComputeSharp.Resources;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="UploadTexture1D{T}"/> type.
/// </summary>
public static class UploadTexture1DExtensions
{
/// <summary>
/// Reads the contents of a <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
public static void CopyTo<T>(this UploadTexture1D<T> source, Texture1D<T> destination)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
destination.CopyFrom(source, 0, 0, source.Width);
}
/// <summary>
/// Reads the contents of a <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this UploadTexture1D<T> source, Texture1D<T> destination, int sourceOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
destination.CopyFrom(source, sourceOffsetX, 0, width);
}
/// <summary>
/// Reads the contents of a <see cref="UploadTexture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
public static void CopyTo<T>(this UploadTexture1D<T> source, Texture1D<T> destination, int sourceOffsetX, int destinationOffsetX, int width)
where T : unmanaged
{
Guard.IsNotNull(source);
Guard.IsNotNull(destination);
destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, width);
}
}
+34
ファイルの表示
@@ -261,6 +261,40 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
return d3D12OptionsData.DoublePrecisionFloatShaderOps;
}
/// <summary>
/// Checks whether the current device supports the creation of
/// <see cref="ReadOnlyTexture1D{T}"/> resources for a specified type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of values to check support for.</typeparam>
/// <returns>Whether <see cref="ReadOnlyTexture1D{T}"/> instances can be created by the current device.</returns>
public bool IsReadOnlyTexture1DSupportedForType<T>()
where T : unmanaged
{
using var _0 = GetReferenceTrackingLease();
ThrowIfDeviceLost();
return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE1D);
}
/// <summary>
/// Checks whether the current device supports the creation of
/// <see cref="ReadWriteTexture1D{T}"/> resources for a specified type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of values to check support for.</typeparam>
/// <returns>Whether <see cref="ReadWriteTexture1D{T}"/> instances can be created by the current device.</returns>
public bool IsReadWriteTexture1DSupportedForType<T>()
where T : unmanaged
{
using var _0 = GetReferenceTrackingLease();
ThrowIfDeviceLost();
return this.d3D12Device.Get()->IsDxgiFormatSupported(
DXGIFormatHelper.GetForType<T>(),
D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW);
}
/// <summary>
/// Checks whether the current device supports the creation of
/// <see cref="ReadOnlyTexture2D{T}"/> resources for a specified type <typeparamref name="T"/>.
+113
ファイルの表示
@@ -76,6 +76,119 @@ public static class GraphicsResourceHelper
return *(ulong*)&d3D12GpuDescriptorHandle;
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
/// <typeparam name="T">The type of values stored in the input texture.</typeparam>
/// <param name="texture">The input <see cref="ReadOnlyTexture1D{T}"/> instance to check.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <returns>The GPU descriptor handle for the texture.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong ValidateAndGetGpuDescriptorHandle<T>(ReadOnlyTexture1D<T> texture, GraphicsDevice device)
where T : unmanaged
{
Guard.IsNotNull(texture);
using var _0 = texture.GetReferenceTrackingLease();
texture.ThrowIfDeviceMismatch(device);
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
return *(ulong*)&d3D12GpuDescriptorHandle;
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
/// <typeparam name="T">The type of values stored in the input texture.</typeparam>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to check.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <returns>The GPU descriptor handle for the texture.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong ValidateAndGetGpuDescriptorHandle<T>(ReadWriteTexture1D<T> texture, GraphicsDevice device)
where T : unmanaged
{
Guard.IsNotNull(texture);
using var _0 = texture.GetReferenceTrackingLease();
texture.ThrowIfDeviceMismatch(device);
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle;
return *(ulong*)&d3D12GpuDescriptorHandle;
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
/// <typeparam name="T">The type of values stored in the input texture.</typeparam>
/// <param name="texture">The input <see cref="IReadOnlyTexture1D{T}"/> instance to check.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <returns>The GPU descriptor handle for the texture.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong ValidateAndGetGpuDescriptorHandle<T>(IReadOnlyTexture1D<T> texture, GraphicsDevice device)
where T : unmanaged
{
Guard.IsNotNull(texture);
if (texture is IGraphicsResource resource)
{
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device);
return *(ulong*)&d3D12GpuDescriptorHandle;
}
return ThrowHelper.ThrowArgumentException<ulong>("The input texture is not a valid instance.");
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
/// <typeparam name="TPixel">The type of normalized values stored in the input texture.</typeparam>
/// <param name="texture">The input <see cref="IReadOnlyNormalizedTexture1D{TPixel}"/> instance to check.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <returns>The GPU descriptor handle for the texture.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong ValidateAndGetGpuDescriptorHandle<TPixel>(IReadOnlyNormalizedTexture1D<TPixel> texture, GraphicsDevice device)
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
if (texture is IGraphicsResource resource)
{
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device);
return *(ulong*)&d3D12GpuDescriptorHandle;
}
return ThrowHelper.ThrowArgumentException<ulong>("The input texture is not a valid instance.");
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
/// <typeparam name="TPixel">The type of normalized values stored in the input texture.</typeparam>
/// <param name="texture">The input <see cref="IReadWriteNormalizedTexture1D{TPixel}"/> instance to check.</param>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance in use.</param>
/// <returns>The GPU descriptor handle for the texture.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong ValidateAndGetGpuDescriptorHandle<TPixel>(IReadWriteNormalizedTexture1D<TPixel> texture, GraphicsDevice device)
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
if (texture is IGraphicsResource resource)
{
D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device);
return *(ulong*)&d3D12GpuDescriptorHandle;
}
return ThrowHelper.ThrowArgumentException<ulong>("The input texture is not a valid instance.");
}
/// <summary>
/// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle.
/// </summary>
+39
ファイルの表示
@@ -57,6 +57,24 @@ public static unsafe class InteropServices
buffer.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture1D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <exception cref="ObjectDisposedException">The <paramref name="texture"/> instance has been disposed.</exception>
/// <exception cref="Win32Exception">Thrown if the <c>IUnknown::QueryInterface</c> call doesn't return <c>S_OK</c>.</exception>
public static void GetID3D12Resource<T>(Texture1D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
using var _0 = texture.GetReferenceTrackingLease();
texture.D3D12Resource->QueryInterface(riid, ppvObject).Assert();
}
/// <summary>
/// Gets the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
@@ -187,6 +205,27 @@ public static unsafe class InteropServices
return buffer.D3D12Resource->QueryInterface(riid, ppvObject);
}
/// <summary>
/// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="texture">The input <see cref="Texture1D{T}"/> instance in use.</param>
/// <param name="riid">A reference to the interface identifier (IID) of the resource interface being queried for.</param>
/// <param name="ppvObject">The address of a pointer to an interface with the IID specified in <paramref name="riid"/>.</param>
/// <returns>
/// <c>S_OK</c> if the interface is supported, and <c>E_NOINTERFACE</c> otherwise.
/// If ppvObject (the address) is nullptr, then this method returns <c>E_POINTER</c>.
/// </returns>
/// <exception cref="ObjectDisposedException">The <paramref name="texture"/> instance has been disposed.</exception>
public static int TryGetID3D12Resource<T>(Texture1D<T> texture, Guid* riid, void** ppvObject)
where T : unmanaged
{
using var _0 = texture.GetReferenceTrackingLease();
return texture.D3D12Resource->QueryInterface(riid, ppvObject);
}
/// <summary>
/// Tries to get the underlying COM object for a given resource, as a specified interface. This method invokes
/// <see href="https://docs.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void)">IUnknown::QueryInterface</see>.
+598
ファイルの表示
@@ -0,0 +1,598 @@
using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Graphics.Resources.Helpers;
using ComputeSharp.Graphics.Resources.Interop;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES;
using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION;
using static TerraFX.Interop.DirectX.D3D12_UAV_DIMENSION;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
#pragma warning disable CS0618
namespace ComputeSharp.Resources;
/// <summary>
/// A <see langword="class"/> representing a typed 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public unsafe abstract class Texture1D<T> : NativeObject, IGraphicsResource, GraphicsResourceHelper.IGraphicsResource
where T : unmanaged
{
#if NET6_0_OR_GREATER
/// <summary>
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
/// </summary>
private ComPtr<D3D12MA_Allocation> allocation;
#endif
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource.
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles;
/// <summary>
/// The <see cref="D3D12_COMMAND_LIST_TYPE"/> value to use for copy operations.
/// </summary>
private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType;
/// <summary>
/// The <see cref="D3D12_PLACED_SUBRESOURCE_FOOTPRINT"/> description for the current resource.
/// </summary>
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
/// <summary>
/// The current <see cref="D3D12_RESOURCE_STATES"/> value for the current resource.
/// </summary>
private D3D12_RESOURCE_STATES d3D12ResourceState;
/// <summary>
/// Creates a new <see cref="Texture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
private protected Texture1D(GraphicsDevice device, int width, ResourceType resourceType, AllocationMode allocationMode, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport)
{
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION);
using var _0 = device.GetReferenceTrackingLease();
device.ThrowIfDeviceLost();
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), d3D12FormatSupport))
{
UnsupportedTextureTypeException.ThrowForTexture1D<T>();
}
GraphicsDevice = device;
#if NET6_0_OR_GREATER
this.allocation = device.Allocator->CreateResource(
device.Pool,
resourceType,
allocationMode,
DXGIFormatHelper.GetForType<T>(),
(uint)width,
out this.d3D12ResourceState);
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
#else
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(
resourceType,
DXGIFormatHelper.GetForType<T>(),
(uint)width,
device.IsCacheCoherentUMA,
out this.d3D12ResourceState);
#endif
this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON
? D3D12_COMMAND_LIST_TYPE_COPY
: D3D12_COMMAND_LIST_TYPE_COMPUTE;
device.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType<T>(),
(uint)width,
out this.d3D12PlacedSubresourceFootprint,
out _,
out _);
device.RegisterAllocatedResource();
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
switch (resourceType)
{
case ResourceType.ReadOnly:
device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType<T>(), D3D12_UAV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
break;
}
this.d3D12Resource.Get()->SetName(this);
}
/// <inheritdoc/>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the width of the current texture.
/// </summary>
public int Width => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Width;
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Gets the <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target memory area.
/// </summary>
/// <param name="destination">The target memory area to write data to.</param>
/// <param name="size">The size of the target memory area to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
internal void CopyTo(ref T destination, int size, int sourceOffsetX, int width)
{
Guard.IsInRange(sourceOffsetX, 0, Width);
Guard.IsBetweenOrEqualTo(width, 1, Width);
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
Guard.IsGreaterThanOrEqualTo(size, width);
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
GraphicsDevice.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType<T>(),
(uint)width,
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintDestination,
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
GraphicsDevice.Pool,
ResourceType.ReadBack,
AllocationMode.Default,
totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
#else
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
ResourceType.ReadBack,
totalSizeInBytes,
GraphicsDevice.IsCacheCoherentUMA);
#endif
using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType))
{
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: d3D12Resource.Get(),
&d3D12PlacedSubresourceFootprintDestination,
destinationX: 0,
destinationY: 0,
destinationZ: 0,
d3D12ResourceSource: D3D12Resource,
sourceX: (uint)sourceOffsetX,
sourceY: 0,
sourceZ: 0,
(uint)width,
height: 1,
depth: 1);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
}
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
fixed (void* destinationPointer = &destination)
{
Buffer.MemoryCopy(
source: resource.Pointer,
destination: destinationPointer,
destinationSizeInBytes: rowSizeInBytes,
sourceBytesToCopy: rowSizeInBytes);
}
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <param name="destination">The target <see cref="Texture1D{T}"/> instance to write data to.</param>
/// <param name="destinationOffsetX">The horizontal offset within <paramref name="destination"/>.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
internal void CopyTo(Texture1D<T> destination, int sourceOffsetX, int destinationOffsetX, int width)
{
Guard.IsInRange(sourceOffsetX, 0, Width);
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
Guard.IsBetweenOrEqualTo(width, 1, Width);
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
using var _2 = destination.GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
destination.ThrowIfDeviceMismatch(GraphicsDevice);
D3D12_COMMAND_LIST_TYPE d3D12CommandListType =
this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE
? D3D12_COMMAND_LIST_TYPE_COMPUTE
: D3D12_COMMAND_LIST_TYPE_COPY;
using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: destination.D3D12Resource,
(uint)destinationOffsetX,
destinationY: 0,
destinationZ: 0,
d3D12ResourceSource: D3D12Resource,
(uint)sourceOffsetX,
sourceY: 0,
sourceZ: 0,
(uint)width,
height: 1,
depth: 1);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture1D{T}"/> instance and writes them into a target <see cref="ReadBackTexture1D{T}"/> instance.
/// </summary>
/// <param name="destination">The target <see cref="ReadBackTexture1D{T}"/> instance to write data to.</param>
/// <param name="sourceOffsetX">The horizontal offset in the source texture.</param>
/// <param name="destinationOffsetX">The horizontal offset within <paramref name="destination"/>.</param>
/// <param name="width">The width of the memory area to copy.</param>
internal void CopyTo(ReadBackTexture1D<T> destination, int sourceOffsetX, int destinationOffsetX, int width)
{
Guard.IsInRange(sourceOffsetX, 0, Width);
Guard.IsInRange(destinationOffsetX, 0, destination.Width);
Guard.IsBetweenOrEqualTo(width, 1, Width);
Guard.IsBetweenOrEqualTo(width, 1, destination.Width);
Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX));
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX));
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
using var _2 = destination.GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
destination.ThrowIfDeviceMismatch(GraphicsDevice);
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
{
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: destination.D3D12Resource,
d3D12PlacedSubresourceFootprintDestination,
(uint)destinationOffsetX,
destinationY: 0,
destinationZ: 0,
d3D12ResourceSource: D3D12Resource,
(uint)sourceOffsetX,
sourceY: 0,
sourceZ: 0,
(uint)width,
height: 1,
depth: 1);
}
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <param name="source">The input memory area to read data from.</param>
/// <param name="size">The size of the memory area to read data from.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
internal void CopyFrom(ref T source, int size, int destinationOffsetX, int width)
{
Guard.IsInRange(destinationOffsetX, 0, Width);
Guard.IsBetweenOrEqualTo(width, 1, Width);
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
Guard.IsGreaterThanOrEqualTo(size, width);
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
GraphicsDevice.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType<T>(),
(uint)width,
out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprintSource,
out ulong rowSizeInBytes,
out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
using ComPtr<D3D12MA_Allocation> allocation = GraphicsDevice.Allocator->CreateResource(
GraphicsDevice.Pool,
ResourceType.Upload,
AllocationMode.Default,
totalSizeInBytes);
using ComPtr<ID3D12Resource> d3D12Resource = new(allocation.Get()->GetResource());
#else
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
ResourceType.Upload,
totalSizeInBytes,
GraphicsDevice.IsCacheCoherentUMA);
#endif
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
fixed (void* sourcePointer = &source)
{
Buffer.MemoryCopy(
source: sourcePointer,
destination: resource.Pointer,
destinationSizeInBytes: rowSizeInBytes,
sourceBytesToCopy: rowSizeInBytes);
}
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: D3D12Resource,
destinationX: (uint)destinationOffsetX,
destinationY: 0,
destinationZ: 0,
d3D12ResourceSource: d3D12Resource.Get(),
&d3D12PlacedSubresourceFootprintSource,
sourceX: 0,
sourceY: 0,
sourceZ: 0,
(uint)width,
height: 1,
depth: 1);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// Writes the contents of a given <see cref="UploadTexture1D{T}"/> instance to a specified area of the current <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="UploadTexture1D{T}"/> instance to read data from.</param>
/// <param name="sourceOffsetX">The horizontal offset within <paramref name="source"/>.</param>
/// <param name="destinationOffsetX">The horizontal offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
internal void CopyFrom(UploadTexture1D<T> source, int sourceOffsetX, int destinationOffsetX, int width)
{
Guard.IsInRange(sourceOffsetX, 0, source.Width);
Guard.IsInRange(destinationOffsetX, 0, Width);
Guard.IsBetweenOrEqualTo(width, 1, Width);
Guard.IsBetweenOrEqualTo(width, 1, source.Width);
Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX));
Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX));
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
using var _2 = source.GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
source.ThrowIfDeviceMismatch(GraphicsDevice);
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
{
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: D3D12Resource,
(uint)destinationOffsetX,
destinationY: 0,
destinationZ: 0,
d3D12ResourceSource: source.D3D12Resource,
d3D12PlacedSubresourceFootprintSource,
(uint)sourceOffsetX,
sourceY: 0,
sourceZ: 0,
(uint)width,
height: 1,
depth: 1);
}
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
copyCommandList.D3D12GraphicsCommandList->TransitionBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
private protected override void OnDispose()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
this.allocation.Dispose();
#endif
if (GraphicsDevice is GraphicsDevice device)
{
device.UnregisterAllocatedResource();
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles);
}
}
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if the current resource is not in a readonly state.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ThrowIfIsNotInReadOnlyState()
{
if (this.d3D12ResourceState == D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
{
static void Throw()
{
throw new InvalidOperationException(
"The texture is not currently in readonly mode. This API can only be used when creating a compute graph with " +
"the ComputeContext type, and after having used ComputeContext.Transition() to change the state of the texture.");
}
Throw();
}
}
/// <summary>
/// Throws a <see cref="GraphicsDeviceMismatchException"/> if the target device doesn't match the current one.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
{
if (GraphicsDevice != device)
{
GraphicsDeviceMismatchException.Throw(this, device);
}
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice, out bool)"/>
internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
using var _0 = GetReferenceTrackingLease();
ThrowIfDeviceMismatch(device);
isNormalized = DXGIFormatHelper.IsNormalizedType<T>();
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice, out Lease)"/>
internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease)
{
lease = GetReferenceTrackingLease();
ThrowIfDeviceMismatch(device);
return D3D12Resource;
}
/// <inheritdoc cref="GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice, ResourceState, out ID3D12Resource*, out Lease)"/>
internal (D3D12_RESOURCE_STATES Before, D3D12_RESOURCE_STATES After) ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease)
{
lease = GetReferenceTrackingLease();
ThrowIfDeviceMismatch(device);
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore = this.d3D12ResourceState;
D3D12_RESOURCE_STATES d3D12ResourceStatesAfter = ResourceStateHelper.GetD3D12ResourceStates(resourceState);
this.d3D12ResourceState = d3D12ResourceStatesAfter;
d3D12Resource = D3D12Resource;
return (d3D12ResourceStatesBefore, d3D12ResourceStatesAfter);
}
/// <inheritdoc/>
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
using var _0 = GetReferenceTrackingLease();
ThrowIfDeviceMismatch(device);
return D3D12GpuDescriptorHandle;
}
/// <inheritdoc/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized);
}
/// <inheritdoc/>
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease)
{
return ValidateAndGetID3D12Resource(device, out lease);
}
/// <inheritdoc/>
(D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease)
{
return ValidateAndGetID3D12ResourceAndTransitionStates(device, resourceState, out d3D12Resource, out lease);
}
}
+1 -1
ファイルの表示
@@ -65,8 +65,8 @@ public unsafe abstract class Texture2D<T> : NativeObject, IGraphicsResource, Gra
/// Creates a new <see cref="Texture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="d3D12FormatSupport">The format support for the current texture type.</param>
+1 -1
ファイルの表示
@@ -65,8 +65,8 @@ public unsafe abstract class Texture3D<T> : NativeObject, IGraphicsResource, Gra
/// Creates a new <see cref="Texture3D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
+207
ファイルの表示
@@ -0,0 +1,207 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
namespace ComputeSharp.Resources;
/// <summary>
/// A <see langword="class"/> representing a typed 1D texture stored on on CPU memory, that can be used to transfer data to/from the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
public unsafe abstract class TransferTexture1D<T> : NativeObject, IGraphicsResource, IMemoryOwner<T>
where T : unmanaged
{
#if NET6_0_OR_GREATER
/// <summary>
/// The <see cref="D3D12MA_Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>.
/// </summary>
private ComPtr<D3D12MA_Allocation> allocation;
#endif
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The pointer to the start of the mapped buffer data.
/// </summary>
private readonly T* mappedData;
/// <summary>
/// The <see cref="D3D12_PLACED_SUBRESOURCE_FOOTPRINT"/> description for the current resource.
/// </summary>
private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint;
/// <summary>
/// Creates a new <see cref="TransferTexture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
private protected TransferTexture1D(GraphicsDevice device, int width, ResourceType resourceType, AllocationMode allocationMode)
{
Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION);
using var _0 = device.GetReferenceTrackingLease();
device.ThrowIfDeviceLost();
if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType<T>(), D3D12_FORMAT_SUPPORT1_TEXTURE1D))
{
UnsupportedTextureTypeException.ThrowForTexture1D<T>();
}
GraphicsDevice = device;
device.D3D12Device->GetCopyableFootprint(
DXGIFormatHelper.GetForType<T>(),
(uint)width,
out this.d3D12PlacedSubresourceFootprint,
out _,
out ulong totalSizeInBytes);
#if NET6_0_OR_GREATER
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
#else
this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA);
#endif
device.RegisterAllocatedResource();
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
this.d3D12Resource.Get()->SetName(this);
}
/// <inheritdoc/>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the width of the current texture.
/// </summary>
public int Width => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Width;
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Gets the <see cref="D3D12_PLACED_SUBRESOURCE_FOOTPRINT"/> value for the current resource.
/// </summary>
internal ref readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT D3D12PlacedSubresourceFootprint => ref this.d3D12PlacedSubresourceFootprint;
/// <inheritdoc/>
public Memory<T> Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
using var _0 = GetReferenceTrackingLease();
return new MemoryManager(this).Memory;
}
}
/// <inheritdoc/>
public Span<T> Span
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
using var _0 = GetReferenceTrackingLease();
return new(this.mappedData, Width);
}
}
/// <inheritdoc/>
private protected sealed override void OnDispose()
{
this.d3D12Resource.Dispose();
#if NET6_0_OR_GREATER
this.allocation.Dispose();
#endif
if (GraphicsDevice is GraphicsDevice device)
{
device.UnregisterAllocatedResource();
}
}
/// <summary>
/// Throws a <see cref="GraphicsDeviceMismatchException"/> if the target device doesn't match the current one.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
{
if (GraphicsDevice != device)
{
GraphicsDeviceMismatchException.Throw(this, device);
}
}
/// <summary>
/// A <see cref="MemoryManager{T}"/> implementation wrapping a <see cref="TransferTexture1D{T}"/> instance.
/// </summary>
private sealed class MemoryManager : MemoryManager<T>
{
/// <summary>
/// The <see cref="TransferTexture1D{T}"/> in use.
/// </summary>
private readonly TransferTexture1D<T> buffer;
/// <summary>
/// Creates a new <see cref="MemoryManager"/> instance for a given buffer.
/// </summary>
/// <param name="buffer">The <see cref="TransferTexture1D{T}"/> in use.</param>
public MemoryManager(TransferTexture1D<T> buffer)
{
this.buffer = buffer;
}
/// <inheritdoc/>
public override Memory<T> Memory
{
get => CreateMemory(this.buffer.Width);
}
/// <inheritdoc/>
public override Span<T> GetSpan()
{
return this.buffer.Span;
}
/// <inheritdoc/>
public override MemoryHandle Pin(int elementIndex = 0)
{
Guard.IsEqualTo(elementIndex, 0);
using var _0 = this.buffer.GetReferenceTrackingLease();
return new(this.buffer.mappedData);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}
+1 -1
ファイルの表示
@@ -44,8 +44,8 @@ public unsafe abstract class TransferTexture2D<T> : NativeObject, IGraphicsResou
/// Creates a new <see cref="TransferTexture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
private protected TransferTexture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode)
+1 -1
ファイルの表示
@@ -44,8 +44,8 @@ public unsafe abstract class TransferTexture3D<T> : NativeObject, IGraphicsResou
/// Creates a new <see cref="TransferTexture3D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="ComputeSharp.GraphicsDevice"/> associated with the current instance.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="depth">The depth of the texture.</param>
/// <param name="resourceType">The resource type for the current texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
+33
ファイルの表示
@@ -0,0 +1,33 @@
using System.Diagnostics;
namespace ComputeSharp.Resources.Debug;
/// <summary>
/// A debug proxy used to display items in a <see cref="Texture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items to display.</typeparam>
internal sealed class Texture1DDebugView<T>
where T : unmanaged
{
/// <summary>
/// Initializes a new instance of the <see cref="Texture1DDebugView{T}"/> class with the specified parameters.
/// </summary>
/// <param name="texture">The input <see cref="Texture1D{T}"/> instance with the items to display.</param>
public Texture1DDebugView(Texture1D<T>? texture)
{
if (texture is not null)
{
T[] items = new T[texture.Width];
texture.CopyTo(ref items[0], items.Length, 0, texture.Width);
Items = items;
}
}
/// <summary>
/// Gets the items to display for the current instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public T[]? Items { get; }
}
+26
ファイルの表示
@@ -0,0 +1,26 @@
using System.Diagnostics;
namespace ComputeSharp.Resources.Debug;
/// <summary>
/// A debug proxy used to display items in a <see cref="TransferTexture1D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items to display.</typeparam>
internal sealed class TransferTexture1DDebugView<T>
where T : unmanaged
{
/// <summary>
/// Initializes a new instance of the <see cref="TransferTexture1DDebugView{T}"/> class with the specified parameters.
/// </summary>
/// <param name="texture">The input <see cref="TransferTexture1D{T}"/> instance with the items to display.</param>
public TransferTexture1DDebugView(TransferTexture1D<T>? texture)
{
Items = texture?.Span.ToArray();
}
/// <summary>
/// Gets the items to display for the current instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public T[]? Items { get; }
}
@@ -0,0 +1,29 @@
namespace ComputeSharp;
/// <summary>
/// An interface representing a typed readonly 1D texture containing normalized pixel data stored on GPU memory.
/// This interface can only be used to wrap <see cref="ReadOnlyTexture1D{T, TPixel}"/> instances.
/// </summary>
/// <typeparam name="TPixel">The type of normalized pixels used on the GPU side.</typeparam>
public interface IReadOnlyNormalizedTexture1D<TPixel> : IGraphicsResource
where TPixel : unmanaged
{
/// <summary>
/// Gets the width of the current texture.
/// </summary>
int Width { get; }
/// <summary>
/// Gets a single <typeparamref name="TPixel"/> value from the current readonly texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
ref readonly TPixel this[int x] { get; }
/// <summary>
/// Retrieves a single <typeparamref name="TPixel"/> value from the current readonly texture with linear sampling.
/// </summary>
/// <param name="u">The horizontal normalized offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
ref readonly TPixel Sample(float u);
}
+29
ファイルの表示
@@ -0,0 +1,29 @@
namespace ComputeSharp;
/// <summary>
/// An interface representing a typed readonly 1D texture containing raw data stored on GPU memory.
/// This interface can only be used to wrap <see cref="ReadOnlyTexture1D{T}"/> instances.
/// </summary>
/// <typeparam name="T">The type of raw data used on the GPU side.</typeparam>
public interface IReadOnlyTexture1D<T> : IGraphicsResource
where T : unmanaged
{
/// <summary>
/// Gets the width of the current texture.
/// </summary>
int Width { get; }
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current readonly texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
ref readonly T this[int x] { get; }
/// <summary>
/// Retrieves a single <typeparamref name="T"/> value from the current readonly texture with linear sampling.
/// </summary>
/// <param name="u">The horizontal normalized offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
ref readonly T Sample(float u);
}
@@ -0,0 +1,22 @@
namespace ComputeSharp;
/// <summary>
/// An interface representing a typed writeable 1D texture containing normalized pixel data stored on GPU memory.
/// This interface can only be used to wrap <see cref="ReadWriteTexture1D{T, TPixel}"/> instances.
/// </summary>
/// <typeparam name="TPixel">The type of normalized pixels used on the GPU side.</typeparam>
public interface IReadWriteNormalizedTexture1D<TPixel> : IGraphicsResource
where TPixel : unmanaged
{
/// <summary>
/// Gets the width of the current texture.
/// </summary>
int Width { get; }
/// <summary>
/// Gets a single <typeparamref name="TPixel"/> value from the current writeable texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
ref TPixel this[int x] { get; }
}
+33
ファイルの表示
@@ -0,0 +1,33 @@
using System.Diagnostics;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed 1D texture stored on CPU memory, that can be used to transfer back from the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(TransferTexture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadBackTexture1D<T> : TransferTexture1D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadBackTexture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadBackTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.ReadBack, allocationMode)
{
}
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadBackTexture1D<{typeof(T)}>[{Width}]";
}
}
+43
ファイルの表示
@@ -0,0 +1,43 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed readonly 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
[DebuggerTypeProxy(typeof(Texture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadOnlyTexture1D<T, TPixel> : Texture1D<T>, IReadOnlyNormalizedTexture1D<TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture1D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadOnlyTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D)
{
}
/// <inheritdoc/>
public ref readonly TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadOnlyTexture1D<T, TPixel>)}[{typeof(int)}]");
/// <inheritdoc/>
public ref readonly TPixel Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadOnlyTexture1D<T, TPixel>)}.{nameof(Sample)}({typeof(float)})");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadOnlyTexture1D<{typeof(T)}, {typeof(TPixel)}>[{Width}]";
}
}
+42
ファイルの表示
@@ -0,0 +1,42 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed readonly 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(Texture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadOnlyTexture1D<T> : Texture1D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadOnlyTexture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadOnlyTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.ReadOnly, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D)
{
}
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current readonly texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
public ref readonly T this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadOnlyTexture1D<T>)}[{typeof(int)}]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadOnlyTexture1D<{typeof(T)}>[{Width}]";
}
}
+146
ファイルの表示
@@ -0,0 +1,146 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION;
#pragma warning disable CS0618
namespace ComputeSharp;
/// <inheritdoc/>
partial class ReadWriteTexture1D<T, TPixel>
{
/// <summary>
/// The wrapping <see cref="ReadOnly"/> instance, if available.
/// </summary>
private ReadOnly? readOnlyWrapper;
/// <inheritdoc cref="ReadWriteTexture1DExtensions.AsReadOnly{T, TPixel}(ReadWriteTexture1D{T, TPixel})"/>
internal IReadOnlyNormalizedTexture1D<TPixel> AsReadOnly()
{
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
ThrowIfIsNotInReadOnlyState();
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
if (readOnlyWrapper is not null)
{
return readOnlyWrapper;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static ReadOnly InitializeWrapper(ReadWriteTexture1D<T, TPixel> texture)
{
return texture.readOnlyWrapper = new(texture);
}
return InitializeWrapper(this);
}
/// <inheritdoc/>
private protected override void OnDispose()
{
base.OnDispose();
this.readOnlyWrapper?.Dispose();
}
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture1D{T, TPixel}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyNormalizedTexture1D<TPixel>, GraphicsResourceHelper.IGraphicsResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture1D{T, TPixel}"/> instance being wrapped.
/// </summary>
private readonly ReadWriteTexture1D<T, TPixel> owner;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource.
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles;
/// <summary>
/// Creates a new <see cref="ReadOnly"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="ReadWriteTexture1D{T, TPixel}"/> instance to wrap.</param>
public ReadOnly(ReadWriteTexture1D<T, TPixel> owner)
{
owner.DangerousAddRef();
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
}
/// <inheritdoc/>
public ref readonly TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D<T, TPixel>.ReadOnly)}[{typeof(int)}]");
/// <inheritdoc/>
public ref readonly TPixel Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D<T, TPixel>.ReadOnly)}.{nameof(Sample)}({typeof(float)})");
/// <inheritdoc/>
public int Width => this.owner.Width;
/// <inheritdoc/>
public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice;
/// <inheritdoc/>
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
using var _0 = GetReferenceTrackingLease();
using var _1 = this.owner.GetReferenceTrackingLease();
this.owner.ThrowIfDeviceMismatch(device);
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
}
/// <inheritdoc/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease)
{
lease = GetReferenceTrackingLease();
using var _1 = this.owner.GetReferenceTrackingLease();
this.owner.ThrowIfDeviceMismatch(device);
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
(D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
private protected override void OnDispose()
{
this.owner.DangerousRelease();
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles);
}
}
}
}
+40
ファイルの表示
@@ -0,0 +1,40 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed read write 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
[DebuggerTypeProxy(typeof(Texture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed partial class ReadWriteTexture1D<T, TPixel> : Texture1D<T>, IReadWriteNormalizedTexture1D<TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteTexture1D{T, TPixel}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadWriteTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)
{
}
/// <inheritdoc/>
public ref TPixel this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture2D<T, TPixel>)}[{typeof(int)}]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadWriteTexture1D<{typeof(T)}, {typeof(TPixel)}>[{Width}]";
}
}
+146
ファイルの表示
@@ -0,0 +1,146 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using static TerraFX.Interop.DirectX.D3D12_SRV_DIMENSION;
#pragma warning disable CS0618
namespace ComputeSharp;
/// <inheritdoc/>
partial class ReadWriteTexture1D<T>
{
/// <summary>
/// The wrapping <see cref="ReadOnly"/> instance, if available.
/// </summary>
private ReadOnly? readOnlyWrapper;
/// <inheritdoc cref="ReadWriteTexture1DExtensions.AsReadOnly(ReadWriteTexture1D{float})"/>
public IReadOnlyTexture1D<T> AsReadOnly()
{
using var _0 = GraphicsDevice.GetReferenceTrackingLease();
using var _1 = GetReferenceTrackingLease();
GraphicsDevice.ThrowIfDeviceLost();
ThrowIfIsNotInReadOnlyState();
ReadOnly? readOnlyWrapper = this.readOnlyWrapper;
if (readOnlyWrapper is not null)
{
return readOnlyWrapper;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static ReadOnly InitializeWrapper(ReadWriteTexture1D<T> texture)
{
return texture.readOnlyWrapper = new(texture);
}
return InitializeWrapper(this);
}
/// <inheritdoc/>
private protected override void OnDispose()
{
base.OnDispose();
this.readOnlyWrapper?.Dispose();
}
/// <summary>
/// A wrapper for a <see cref="ReadWriteTexture1D{T}"/> resource that has been temporarily transitioned to readonly.
/// </summary>
private sealed unsafe class ReadOnly : NativeObject, IReadOnlyTexture1D<T>, GraphicsResourceHelper.IGraphicsResource
{
/// <summary>
/// The owning <see cref="ReadWriteTexture1D{T}"/> instance being wrapped.
/// </summary>
private readonly ReadWriteTexture1D<T> owner;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource.
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles;
/// <summary>
/// Creates a new <see cref="ReadOnly"/> instance with the specified parameters.
/// </summary>
/// <param name="owner">The owning <see cref="ReadWriteTexture1D{T}"/> instance to wrap.</param>
public ReadOnly(ReadWriteTexture1D<T> owner)
{
owner.DangerousAddRef();
this.owner = owner;
owner.GraphicsDevice.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
owner.GraphicsDevice.D3D12Device->CreateShaderResourceView(owner.D3D12Resource, DXGIFormatHelper.GetForType<T>(), D3D12_SRV_DIMENSION_TEXTURE1D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
}
/// <inheritdoc/>
public ref readonly T this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D<T>.ReadOnly)}[{typeof(int)}]");
/// <inheritdoc/>
public ref readonly T Sample(float u) => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D<T>.ReadOnly)}.{nameof(Sample)}({typeof(float)})");
/// <inheritdoc/>
public int Width => this.owner.Width;
/// <inheritdoc/>
public GraphicsDevice GraphicsDevice => this.owner.GraphicsDevice;
/// <inheritdoc/>
D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device)
{
using var _0 = GetReferenceTrackingLease();
using var _1 = this.owner.GetReferenceTrackingLease();
this.owner.ThrowIfDeviceMismatch(device);
return this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle;
}
/// <inheritdoc/>
(D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device, out Lease lease)
{
lease = GetReferenceTrackingLease();
using var _1 = this.owner.GetReferenceTrackingLease();
this.owner.ThrowIfDeviceMismatch(device);
return this.owner.D3D12Resource;
}
/// <inheritdoc/>
(D3D12_RESOURCE_STATES, D3D12_RESOURCE_STATES) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12ResourceAndTransitionStates(GraphicsDevice device, ResourceState resourceState, out ID3D12Resource* d3D12Resource, out Lease lease)
{
throw new NotSupportedException("This operation cannot be performaned on a readonly wrapper.");
}
/// <inheritdoc/>
private protected override void OnDispose()
{
this.owner.DangerousRelease();
if (this.owner.GraphicsDevice is GraphicsDevice device)
{
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles);
}
}
}
}
+42
ファイルの表示
@@ -0,0 +1,42 @@
using System.Diagnostics;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed read write 1D texture stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(Texture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed partial class ReadWriteTexture1D<T> : Texture1D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadWriteTexture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadWriteTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.ReadWrite, allocationMode, D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)
{
}
/// <summary>
/// Gets a single <typeparamref name="T"/> value from the current writeable texture.
/// </summary>
/// <param name="x">The horizontal offset of the value to get.</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else.</remarks>
public ref T this[int x] => throw new InvalidExecutionContextException($"{typeof(ReadWriteTexture1D<T>)}[{typeof(int)}]");
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadWriteTexture1D<{typeof(T)}>[{Width}]";
}
}
+33
ファイルの表示
@@ -0,0 +1,33 @@
using System.Diagnostics;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Debug;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> representing a typed 1D texture stored on CPU memory, that can be used to transfer data to the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(TransferTexture1DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class UploadTexture1D<T> : TransferTexture1D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="UploadTexture1D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal UploadTexture1D(GraphicsDevice device, int width, AllocationMode allocationMode)
: base(device, width, ResourceType.Upload, allocationMode)
{
}
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.UploadTexture1D<{typeof(T)}>[{Width}]";
}
}
+234 -1
ファイルの表示
@@ -30,6 +30,22 @@ public static class ComputeContextExtensions
context.Barrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to insert the resource barrier.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to insert the barrier for.</param>
public static unsafe void Barrier<T>(this in ComputeContext context, ReadWriteTexture1D<T> texture)
where T : unmanaged
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
@@ -62,6 +78,24 @@ public static class ComputeContextExtensions
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to insert the resource barrier.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T,TPixel}"/> instance to insert the barrier for.</param>
public static unsafe void Barrier<T, TPixel>(this in ComputeContext context, ReadWriteTexture1D<T, TPixel> texture)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
@@ -98,6 +132,22 @@ public static class ComputeContextExtensions
context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to insert the resource barrier.</param>
/// <param name="texture">The input <see cref="IReadWriteNormalizedTexture1D{TPixel}"/> instance to insert the barrier for.</param>
public static unsafe void Barrier<TPixel>(this in ComputeContext context, IReadWriteNormalizedTexture1D<TPixel> texture)
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)));
}
/// <summary>
/// Inserts a resource barrier for a specific resource.
/// </summary>
@@ -146,6 +196,22 @@ public static class ComputeContextExtensions
context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to clear.</param>
public static unsafe void Clear<T>(this in ComputeContext context, ReadWriteTexture1D<T> texture)
where T : unmanaged
{
Guard.IsNotNull(texture);
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
@@ -178,6 +244,24 @@ public static class ComputeContextExtensions
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T,TPixel}"/> instance to clear.</param>
public static unsafe void Clear<T, TPixel>(this in ComputeContext context, ReadWriteTexture1D<T, TPixel> texture)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
@@ -214,6 +298,24 @@ public static class ComputeContextExtensions
context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to clear the resource.</param>
/// <param name="texture">The input <see cref="IReadWriteNormalizedTexture1D{TPixel}"/> instance to clear.</param>
public static unsafe void Clear<TPixel>(this in ComputeContext context, IReadWriteNormalizedTexture1D<TPixel> texture)
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
using NativeObject.Lease lease = default;
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Clears a specific resource.
/// </summary>
@@ -250,6 +352,25 @@ public static class ComputeContextExtensions
context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)), handles.Gpu, handles.Cpu, true);
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T,TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<T, TPixel>(this in ComputeContext context, ReadWriteTexture1D<T, TPixel> texture, T value)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel()));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
@@ -288,6 +409,29 @@ public static class ComputeContextExtensions
context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel()));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
/// <typeparam name="TPixel">The type of pixels stored on the texture.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to fill the resource.</param>
/// <param name="texture">The input <see cref="IReadWriteNormalizedTexture1D{TPixel}"/> instance to fill.</param>
/// <param name="value">The value to use to fill <paramref name="texture"/>.</param>
public static unsafe void Fill<TPixel>(this in ComputeContext context, IReadWriteNormalizedTexture1D<TPixel> texture, TPixel value)
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _);
using NativeObject.Lease lease = default;
context.Fill(
((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice, out Unsafe.AsRef(in lease)),
handles.Gpu,
handles.Cpu,
DXGIFormatHelper.ExtendToNormalizedValue(value));
}
/// <summary>
/// Fills a specific texture with a given value.
/// </summary>
@@ -427,6 +571,95 @@ public static class ComputeContextExtensions
context.Run(texture, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D<float> texture, ResourceState resourceState)
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out Unsafe.AsRef(in lease));
context.Transition(d3D12Resource, states.Before, states.After);
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D<Float2> texture, ResourceState resourceState)
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out Unsafe.AsRef(in lease));
context.Transition(d3D12Resource, states.Before, states.After);
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D<Float3> texture, ResourceState resourceState)
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out Unsafe.AsRef(in lease));
context.Transition(d3D12Resource, states.Before, states.After);
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition(this in ComputeContext context, ReadWriteTexture1D<Float4> texture, ResourceState resourceState)
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out Unsafe.AsRef(in lease));
context.Transition(d3D12Resource, states.Before, states.After);
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture1D{T,TPixel}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition<T, TPixel>(this in ComputeContext context, ReadWriteTexture1D<T, TPixel> texture, ResourceState resourceState)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
Guard.IsNotNull(texture);
using NativeObject.Lease lease = default;
var states = texture.ValidateAndGetID3D12ResourceAndTransitionStates(context.GraphicsDevice, resourceState, out ID3D12Resource* d3D12Resource, out Unsafe.AsRef(in lease));
context.Transition(d3D12Resource, states.Before, states.After);
}
/// <summary>
/// Transitions the state of a specific resource.
/// </summary>
@@ -590,7 +823,7 @@ public static class ComputeContextExtensions
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to transition the resource.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture2D{T,TPixel}"/> instance to transition.</param>
/// <param name="texture">The input <see cref="ReadWriteTexture3D{T,TPixel}"/> instance to transition.</param>
/// <param name="resourceState">The state to transition the input resource to.</param>
public static unsafe void Transition<T, TPixel>(this in ComputeContext context, ReadWriteTexture3D<T, TPixel> texture, ResourceState resourceState)
where T : unmanaged, IPixel<T, TPixel>
+81
ファイルの表示
@@ -30,6 +30,48 @@ public static class GraphicsDeviceExtensions
};
}
/// <summary>
/// Allocates a new <see cref="Texture1D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture1D{T}"/> instance of the requested size.</returns>
public static Texture1D<T> AllocateTexture1D<T>(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<>) => device.AllocateReadOnlyTexture1D<T>(width, allocationMode),
_ when type == typeof(ReadWriteTexture1D<>) => device.AllocateReadWriteTexture1D<T>(width, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Texture1D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="Texture1D{T}"/> instance of the requested size.</returns>
public static Texture1D<T> AllocateTexture1D<T, TPixel>(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<,>) => device.AllocateReadOnlyTexture1D<T, TPixel>(width, allocationMode),
_ when type == typeof(ReadWriteTexture1D<,>) => device.AllocateReadWriteTexture1D<T, TPixel>(width, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Texture2D{T}"/> instance of the specified type.
/// </summary>
@@ -140,6 +182,26 @@ public static class GraphicsDeviceExtensions
};
}
/// <summary>
/// Allocates a new <see cref="TransferTexture1D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <returns>A <see cref="TransferTexture1D{T}"/> instance of the requested size.</returns>
public static TransferTexture1D<T> AllocateTransferTexture1D<T>(this GraphicsDevice device, Type type, int width, AllocationMode allocationMode = AllocationMode.Default)
where T : unmanaged
{
return type switch
{
_ when type == typeof(UploadTexture1D<>) => device.AllocateUploadTexture1D<T>(width, allocationMode),
_ when type == typeof(ReadBackTexture1D<>) => device.AllocateReadBackTexture1D<T>(width, allocationMode),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="TransferTexture2D{T}"/> instance of the specified type.
/// </summary>
@@ -203,6 +265,25 @@ public static class GraphicsDeviceExtensions
};
}
/// <summary>
/// Allocates a new <see cref="Texture1D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <returns>A <see cref="Texture1D{T}"/> instance of the requested type.</returns>
public static Texture1D<T> AllocateTexture1D<T>(this GraphicsDevice device, Type type, T[] data)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture1D<>) => device.AllocateReadOnlyTexture1D(data),
_ when type == typeof(ReadWriteTexture1D<>) => device.AllocateReadWriteTexture1D(data),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Texture2D{T}"/> instance of the specified type.
/// </summary>
+228
ファイルの表示
@@ -72,6 +72,234 @@ public partial class ResourceDimensionsTests
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadOnlyTexture1D_T1_AsReadOnlyTexture1D(Device device, int axis0)
{
using ReadOnlyTexture1D<float> resource = device.Get().AllocateReadOnlyTexture1D<float>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadOnlyTexture1D_T1_AsReadOnlyTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadOnlyTexture1D_T1_AsReadOnlyTexture1DShader : IComputeShader
{
public readonly ReadOnlyTexture1D<float> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadWriteTexture1D_T1_AsReadWriteTexture1D(Device device, int axis0)
{
using ReadWriteTexture1D<float> resource = device.Get().AllocateReadWriteTexture1D<float>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadWriteTexture1D_T1_AsReadWriteTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadWriteTexture1D_T1_AsReadWriteTexture1DShader : IComputeShader
{
public readonly ReadWriteTexture1D<float> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadWriteTexture1D_T1_AsIReadOnlyTexture1D(Device device, int axis0)
{
using ReadWriteTexture1D<float> resource = device.Get().AllocateReadWriteTexture1D<float>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(resource, ResourceState.ReadOnly);
context.For(1, new ReadWriteTexture1D_T1_AsIReadOnlyTexture1DShader(resource.AsReadOnly(), result));
}
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadWriteTexture1D_T1_AsIReadOnlyTexture1DShader : IComputeShader
{
public readonly IReadOnlyTexture1D<float> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadOnlyTexture1D_T2_AsReadOnlyTexture1D(Device device, int axis0)
{
using ReadOnlyTexture1D<Rgba32, float4> resource = device.Get().AllocateReadOnlyTexture1D<Rgba32, float4>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadOnlyTexture1D_T2_AsReadOnlyTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadOnlyTexture1D_T2_AsReadOnlyTexture1DShader : IComputeShader
{
public readonly ReadOnlyTexture1D<Rgba32, float4> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadOnlyTexture1D_T2_AsIReadOnlyNormalizedTexture1D(Device device, int axis0)
{
using ReadOnlyTexture1D<Rgba32, float4> resource = device.Get().AllocateReadOnlyTexture1D<Rgba32, float4>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadOnlyTexture1D_T2_AsIReadOnlyNormalizedTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadOnlyTexture1D_T2_AsIReadOnlyNormalizedTexture1DShader : IComputeShader
{
public readonly IReadOnlyNormalizedTexture1D<float4> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadWriteTexture1D_T2_AsReadWriteTexture1D(Device device, int axis0)
{
using ReadWriteTexture1D<Rgba32, float4> resource = device.Get().AllocateReadWriteTexture1D<Rgba32, float4>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadWriteTexture1D_T2_AsReadWriteTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadWriteTexture1D_T2_AsReadWriteTexture1DShader : IComputeShader
{
public readonly ReadWriteTexture1D<Rgba32, float4> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8)]
[Data(64)]
[Data(128)]
[Data(376)]
public void ReadWriteTexture1D_T2_AsIReadWriteNormalizedTexture1D(Device device, int axis0)
{
using ReadWriteTexture1D<Rgba32, float4> resource = device.Get().AllocateReadWriteTexture1D<Rgba32, float4>(axis0);
using ReadWriteBuffer<int> result = device.Get().AllocateReadWriteBuffer<int>(1);
device.Get().For(1, new ReadWriteTexture1D_T2_AsIReadWriteNormalizedTexture1DShader(resource, result));
int[] dimensions = result.ToArray();
CollectionAssert.AreEqual(
expected: new[] { axis0 },
actual: dimensions);
}
[AutoConstructor]
internal readonly partial struct ReadWriteTexture1D_T2_AsIReadWriteNormalizedTexture1DShader : IComputeShader
{
public readonly IReadWriteNormalizedTexture1D<float4> source;
public readonly ReadWriteBuffer<int> result;
public void Execute()
{
result[0] = source.Width;
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(8, 12)]
+8
ファイルの表示
@@ -20,6 +20,10 @@ var typesMap = new Dictionary<string, string[]>
{
["ReadOnlyBuffer<float>"] = new[] { "Length" },
["ReadWriteBuffer<float>"] = new[] { "Length" },
["ReadOnlyTexture1D<float>"] = new[] { "Width" },
["ReadWriteTexture1D<float>"] = new[] { "Width" },
["ReadOnlyTexture1D<Rgba32, float4>"] = new[] { "Width" },
["ReadWriteTexture1D<Rgba32, float4>"] = new[] { "Width" },
["ReadOnlyTexture2D<float>"] = new[] { "Width", "Height" },
["ReadWriteTexture2D<float>"] = new[] { "Width", "Height" },
["ReadOnlyTexture2D<Rgba32, float4>"] = new[] { "Width", "Height" },
@@ -33,6 +37,9 @@ var typesMap = new Dictionary<string, string[]>
// Map of all resource types that also have a supported interface
var interfacesMap = new Dictionary<string, string>
{
["ReadWriteTexture1D<float>"] = "IReadOnlyTexture1D<float>",
["ReadOnlyTexture1D<Rgba32, float4>"] = "IReadOnlyNormalizedTexture1D<float4>",
["ReadWriteTexture1D<Rgba32, float4>"] = "IReadWriteNormalizedTexture1D<float4>",
["ReadWriteTexture2D<float>"] = "IReadOnlyTexture2D<float>",
["ReadOnlyTexture2D<Rgba32, float4>"] = "IReadOnlyNormalizedTexture2D<float4>",
["ReadWriteTexture2D<Rgba32, float4>"] = "IReadWriteNormalizedTexture2D<float4>",
@@ -44,6 +51,7 @@ var interfacesMap = new Dictionary<string, string>
// Map of all resource that need a readonly wrapper
var readonlyWrappersMap = new HashSet<string>
{
"IReadOnlyTexture1D<float>",
"IReadOnlyTexture2D<float>",
"IReadOnlyTexture3D<float>"
};
+165
ファイルの表示
@@ -0,0 +1,165 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using CommunityToolkit.HighPerformance;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using ComputeSharp.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Tests;
partial class Texture1DTests
{
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<,>))]
[Resource(typeof(ReadWriteTexture1D<,>))]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device, Type textureType)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
using Texture1D<T> texture = device.Get().AllocateTexture1D<T, TPixel>(textureType, 128);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 128);
Assert.AreSame(texture.GraphicsDevice, device.Get());
if (textureType == typeof(ReadOnlyTexture1D<,>))
{
Assert.IsTrue(texture is ReadOnlyTexture1D<T, TPixel>);
}
else
{
Assert.IsTrue(texture is ReadWriteTexture1D<T, TPixel>);
}
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device, textureType);
}
[CombinatorialTestMethod]
[AllDevices]
public void SampleFromSourceTexture_ComputeShader(Device device)
{
_ = device.Get();
string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets");
using var original = Image.Load<ImageSharpRgba32>(Path.Combine(imagingPath, "city.jpg"));
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"));
if (!original.DangerousTryGetSinglePixelMemory(out Memory<ImageSharpRgba32> originalMemory))
{
Assert.Inconclusive();
}
Span<Rgba32> row = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(originalMemory.Span.Slice(0, original.Width));
using ReadOnlyTexture1D<Rgba32, float4> source = device.Get().AllocateReadOnlyTexture1D<Rgba32, float4>(row);
using ReadWriteTexture1D<Rgba32, float4> destination = device.Get().AllocateReadWriteTexture1D<Rgba32, float4>(sampled.Width);
device.Get().For(sampled.Width, new SamplingComputeShader(source, destination));
Rgba32[] processed = destination.ToArray();
if (!sampled.DangerousTryGetSinglePixelMemory(out Memory<ImageSharpRgba32> sampledMemory))
{
Assert.Inconclusive();
}
using var sampledRow = Image.WrapMemory(sampledMemory.Slice(0, sampled.Width), sampled.Width, 1);
using var processedRow = Image.WrapMemory(processed.AsMemory().Cast<Rgba32, ImageSharpRgba32>(), sampled.Width, 1);
TolerantImageComparer.AssertEqual(sampledRow, processedRow, 0.0000017f);
}
[AutoConstructor]
public readonly partial struct SamplingComputeShader : IComputeShader
{
public readonly IReadOnlyNormalizedTexture1D<float4> source;
public readonly IReadWriteNormalizedTexture1D<float4> destination;
public void Execute()
{
destination[ThreadIds.X] = source.Sample(ThreadIds.Normalized.X);
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
public void AsReadOnly_MultipleCalls_SameInstance(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
using ReadWriteTexture1D<T, TPixel> texture = device.Get().AllocateReadWriteTexture1D<T, TPixel>(64);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(texture, ResourceState.ReadOnly);
IReadOnlyNormalizedTexture1D<TPixel> wrapper1 = texture.AsReadOnly();
Assert.IsNotNull(wrapper1);
IReadOnlyNormalizedTexture1D<TPixel> wrapper2 = texture.AsReadOnly();
Assert.IsNotNull(wrapper2);
Assert.AreSame(wrapper1, wrapper2);
context.Transition(texture, ResourceState.ReadWrite);
}
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
[ExpectedException(typeof(InvalidOperationException))]
public void AsReadOnly_InvalidState_ThrowInvalidOperationException(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
using ReadWriteTexture1D<T, TPixel> texture = device.Get().AllocateReadWriteTexture1D<T, TPixel>(64);
_ = texture.AsReadOnly();
Assert.Fail();
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
}
+154
ファイルの表示
@@ -0,0 +1,154 @@
using System;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
partial class Texture1DTests
{
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(Float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(Float2))]
[Data(typeof(Rg32), typeof(Float2))]
[Data(typeof(Rgba32), typeof(Float4))]
[Data(typeof(Rgba64), typeof(Float4))]
public void Dispatch_NormalizedTexture1D(Device device, Type t, Type tPixel)
{
if (t == typeof(Bgra32) && tPixel == typeof(Float4))
{
using ReadOnlyTexture1D<Bgra32, Float4> source = device.Get().AllocateReadOnlyTexture1D<Bgra32, Float4>(128);
using ReadWriteTexture1D<Bgra32, Float4> destination = device.Get().AllocateReadWriteTexture1D<Bgra32, Float4>(128);
device.Get().For(128, new Shader_Unorm_Bgra32_Float4(source, destination));
}
if (t == typeof(R16) && tPixel == typeof(float))
{
using ReadOnlyTexture1D<R16, float> source = device.Get().AllocateReadOnlyTexture1D<R16, float>(128);
using ReadWriteTexture1D<R16, float> destination = device.Get().AllocateReadWriteTexture1D<R16, float>(128);
device.Get().For(128, new Shader_Unorm_R16_float(source, destination));
}
if (t == typeof(R8) && tPixel == typeof(float))
{
using ReadOnlyTexture1D<R8, float> source = device.Get().AllocateReadOnlyTexture1D<R8, float>(128);
using ReadWriteTexture1D<R8, float> destination = device.Get().AllocateReadWriteTexture1D<R8, float>(128);
device.Get().For(128, new Shader_Unorm_R8_float(source, destination));
}
if (t == typeof(Rg16) && tPixel == typeof(Float2))
{
using ReadOnlyTexture1D<Rg16, Float2> source = device.Get().AllocateReadOnlyTexture1D<Rg16, Float2>(128);
using ReadWriteTexture1D<Rg16, Float2> destination = device.Get().AllocateReadWriteTexture1D<Rg16, Float2>(128);
device.Get().For(128, new Shader_Unorm_Rg16_Float2(source, destination));
}
if (t == typeof(Rg32) && tPixel == typeof(Float2))
{
using ReadOnlyTexture1D<Rg32, Float2> source = device.Get().AllocateReadOnlyTexture1D<Rg32, Float2>(128);
using ReadWriteTexture1D<Rg32, Float2> destination = device.Get().AllocateReadWriteTexture1D<Rg32, Float2>(128);
device.Get().For(128, new Shader_Unorm_Rg32_Float2(source, destination));
}
if (t == typeof(Rgba32) && tPixel == typeof(Float4))
{
using ReadOnlyTexture1D<Rgba32, Float4> source = device.Get().AllocateReadOnlyTexture1D<Rgba32, Float4>(128);
using ReadWriteTexture1D<Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture1D<Rgba32, Float4>(128);
device.Get().For(128, new Shader_Unorm_Rgba32_Float4(source, destination));
}
if (t == typeof(Rgba64) && tPixel == typeof(Float4))
{
using ReadOnlyTexture1D<Rgba64, Float4> source = device.Get().AllocateReadOnlyTexture1D<Rgba64, Float4>(128);
using ReadWriteTexture1D<Rgba64, Float4> destination = device.Get().AllocateReadWriteTexture1D<Rgba64, Float4>(128);
device.Get().For(128, new Shader_Unorm_Rgba64_Float4(source, destination));
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_Bgra32_Float4 : IComputeShader
{
public readonly ReadOnlyTexture1D<Bgra32, Float4> source;
public readonly ReadWriteTexture1D<Bgra32, Float4> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_R16_float : IComputeShader
{
public readonly ReadOnlyTexture1D<R16, float> source;
public readonly ReadWriteTexture1D<R16, float> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_R8_float : IComputeShader
{
public readonly ReadOnlyTexture1D<R8, float> source;
public readonly ReadWriteTexture1D<R8, float> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_Rg16_Float2 : IComputeShader
{
public readonly ReadOnlyTexture1D<Rg16, Float2> source;
public readonly ReadWriteTexture1D<Rg16, Float2> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_Rg32_Float2 : IComputeShader
{
public readonly ReadOnlyTexture1D<Rg32, Float2> source;
public readonly ReadWriteTexture1D<Rg32, Float2> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_Rgba32_Float4 : IComputeShader
{
public readonly ReadOnlyTexture1D<Rgba32, Float4> source;
public readonly ReadWriteTexture1D<Rgba32, Float4> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
[AutoConstructor]
internal readonly partial struct Shader_Unorm_Rgba64_Float4 : IComputeShader
{
public readonly ReadOnlyTexture1D<Rgba64, Float4> source;
public readonly ReadWriteTexture1D<Rgba64, Float4> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
}
+81
ファイルの表示
@@ -0,0 +1,81 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".g.cs"#>
<#
var typesMap = new Dictionary<string, string[]>
{
["Bgra32"] = new[] { "Float4" },
["R16"] = new[] { "float" },
["R8"] = new[] { "float" },
["Rg16"] = new[] { "Float2" },
["Rg32"] = new[] { "Float2" },
["Rgba32"] = new[] { "Float4" },
["Rgba64"] = new[] { "Float4" }
};
IEnumerable<(string T, string TPixel)> EnumerateTypes()
{
foreach (var pair in typesMap)
foreach (var tPixel in pair.Value)
yield return (pair.Key, tPixel);
}
#>
using System;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
partial class Texture1DTests
{
[CombinatorialTestMethod]
[AllDevices]
<#
foreach (var (t, tPixel) in EnumerateTypes())
{
#>
[Data(typeof(<#=t#>), typeof(<#=tPixel#>))]
<#
}
#>
public void Dispatch_NormalizedTexture1D(Device device, Type t, Type tPixel)
{
<#
foreach (var (t, tPixel) in EnumerateTypes())
{
#>
if (t == typeof(<#=t#>) && tPixel == typeof(<#=tPixel#>))
{
using ReadOnlyTexture1D<<#=t#>, <#=tPixel#>> source = device.Get().AllocateReadOnlyTexture1D<<#=t#>, <#=tPixel#>>(128);
using ReadWriteTexture1D<<#=t#>, <#=tPixel#>> destination = device.Get().AllocateReadWriteTexture1D<<#=t#>, <#=tPixel#>>(128);
device.Get().For(128, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination));
}
<#
}
#>
}
<#
foreach (var (t, tPixel) in EnumerateTypes())
{
#>
[AutoConstructor]
internal readonly partial struct Shader_Unorm_<#=t#>_<#=tPixel#> : IComputeShader
{
public readonly ReadOnlyTexture1D<<#=t#>, <#=tPixel#>> source;
public readonly ReadWriteTexture1D<<#=t#>, <#=tPixel#>> destination;
public void Execute()
{
destination[ThreadIds.X] = (source[ThreadIds.X] + destination[ThreadIds.X]) / 2;
}
}
<#
}
#>
}
+406
ファイルの表示
@@ -0,0 +1,406 @@
using System;
using System.Linq;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests;
[TestClass]
[TestCategory("Texture1D")]
public partial class Texture1DTests
{
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(AllocationMode.Default)]
[Data(AllocationMode.Clear)]
public void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode)
{
using Texture1D<float> texture = device.Get().AllocateTexture1D<float>(textureType, 128, allocationMode);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 128);
Assert.AreSame(texture.GraphicsDevice, device.Get());
if (allocationMode == AllocationMode.Clear)
{
foreach (float x in texture.ToArray())
{
Assert.AreEqual(x, 0f);
}
}
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(-14253)]
[Data(-1)]
[Data(0)]
[Data(-14)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width)
{
using Texture1D<float> texture = device.Get().AllocateTexture1D<float>(textureType, width);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
public void Allocate_FromArray(Device device, Type textureType)
{
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
using Texture1D<float> texture = device.Get().AllocateTexture1D(textureType, data);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 128);
Assert.AreSame(texture.GraphicsDevice, device.Get());
float[] result = texture.ToArray();
Assert.IsNotNull(result);
Assert.AreEqual(data.Length, result.Length);
Assert.IsTrue(data.SequenceEqual(result));
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Device device, Type textureType)
{
using Texture1D<float> texture = device.Get().AllocateTexture1D<float>(textureType, 10);
texture.Dispose();
_ = texture.ToArray();
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(0, 64)]
[Data(0, 64)]
[Data(14, 50)]
[Data(10, 54)]
[Data(20, 32)]
[Data(60, 4)]
[Data(0, 64)]
[Data(63, 1)]
[Data(2, 60)]
public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int width)
{
float[] array = Enumerable.Range(0, 256).Select(static i => (float)i).ToArray();
using Texture1D<float> texture = device.Get().AllocateTexture1D(textureType, array);
float[] result = texture.ToArray(x, width);
Assert.AreEqual(width, result.Length);
Span<float> expected = array.AsSpan(x, width);
CollectionAssert.AreEqual(expected.ToArray(), result);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(-1, 20)]
[Data(20, -1)]
[Data(64, 1)]
[Data(12, 200)]
[Data(80, 40)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int width)
{
using Texture1D<float> texture = device.Get().AllocateTexture1D<float>(textureType, 64);
_ = texture.ToArray(x, width);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(0, 64)]
[Data(0, 64)]
[Data(14, 50)]
[Data(10, 54)]
[Data(20, 32)]
[Data(60, 4)]
[Data(0, 64)]
[Data(63, 1)]
[Data(2, 60)]
public void GetData_RangeToVoid_Ok(Device device, Type textureType, int x, int width)
{
float[] array = Enumerable.Range(0, 512).Select(static i => (float)i).ToArray();
using Texture1D<float> texture = device.Get().AllocateTexture1D(textureType, array);
float[] result = new float[width];
texture.CopyTo(result, x, width);
Span<float> expected = array.AsSpan(x, width);
CollectionAssert.AreEqual(expected.ToArray(), result);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[Data(0, 500)]
[Data(-1, 50)]
[Data(12, -1)]
[Data(128, 1)]
[Data(12, -20)]
[Data(0, -40)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToVoid_Fail(Device device, Type textureType, int x, int width)
{
float[] array = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
using Texture1D<float> texture = device.Get().AllocateTexture1D(textureType, array);
float[] result = new float[128];
texture.CopyTo(result, x, width);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[AdditionalResource(typeof(ReadOnlyTexture1D<>))]
[AdditionalResource(typeof(ReadWriteTexture1D<>))]
[Data(1024, 1024, 0, 0, 1024)]
[Data(1024, 1024, 0, 256, 480)]
[Data(1024, 1024, 127, 256, 480)]
[Data(1024, 1024, 256, 0, 480)]
[Data(512, 1024, 29, 127, 320)]
[Data(512, 256, 120, 0, 256)]
public void CopyTo_TextureToVoid_Ok(
Device device,
Type sourceType,
Type destinationType,
int sourceWidth,
int destinationWidth,
int sourceOffsetX,
int destinationOffsetX,
int copyWidth)
{
float[] array = Enumerable.Range(0, sourceWidth).Select(static i => (float)i).ToArray();
using Texture1D<float> source = device.Get().AllocateTexture1D(sourceType, array);
using Texture1D<float> destination = device.Get().AllocateTexture1D<float>(destinationType, destinationWidth, AllocationMode.Clear);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, copyWidth);
ReadOnlySpan<float> expected = array;
ReadOnlySpan<float> result = destination.ToArray();
for (int i = 0; i < destinationWidth; i++)
{
if (i >= destinationOffsetX &&
i < destinationOffsetX + copyWidth)
{
int sourceX = i - destinationOffsetX + sourceOffsetX;
Assert.AreEqual(expected[sourceX], result[i]);
}
else
{
Assert.AreEqual(0, result[i]);
}
}
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[AdditionalResource(typeof(ReadOnlyTexture1D<>))]
[AdditionalResource(typeof(ReadWriteTexture1D<>))]
[Data(1024, 1024, 0, 0, 2055)]
[Data(1024, 1024, 0, 0, 3920)]
[Data(1024, 1024, 0, 0, -1)]
[Data(1024, 1024, 0, 0, -2)]
[Data(1024, 1024, 0, 0, int.MaxValue)]
[Data(512, 1024, 0, 768, 768)]
[Data(512, 1024, 0, 256, 768)]
[Data(512, 1024, 0, 620, 480)]
[Data(512, 512, 450, 0, 128)]
[Data(512, 512, 0, 450, 128)]
[Data(512, 512, -1, 0, 128)]
[Data(512, 512, 0, -1, 128)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CopyTo_TextureToVoid_Fail(
Device device,
Type sourceType,
Type destinationType,
int sourceWidth,
int destinationWidth,
int sourceOffsetX,
int destinationOffsetX,
int copyWidth)
{
using Texture1D<float> source = device.Get().AllocateTexture1D<float>(sourceType, sourceWidth);
using Texture1D<float> destination = device.Get().AllocateTexture1D<float>(destinationType, destinationWidth, AllocationMode.Clear);
source.CopyTo(destination, sourceOffsetX, destinationOffsetX, copyWidth);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[AdditionalResource(typeof(ReadOnlyTexture1D<>))]
[AdditionalResource(typeof(ReadWriteTexture1D<>))]
[Data(1024, 1024, 0, 0, 1024)]
[Data(1024, 1024, 127, 256, 480)]
[Data(1024, 1024, 0, 127, 480)]
[Data(1024, 1024, 122, 329, 480)]
[Data(512, 1024, 122, 256, 320)]
[Data(512, 256, 0, 0, 256)]
[Data(512, 1024, 0, 512, 512)]
[Data(512, 1024, 0, 0, 512)]
public void CopyFrom_TextureToVoid_Ok(
Device device,
Type sourceType,
Type destinationType,
int sourceWidth,
int destinationWidth,
int sourceOffsetX,
int destinationOffsetX,
int copyWidth)
{
float[] array = Enumerable.Range(0, sourceWidth).Select(static i => (float)i).ToArray();
using Texture1D<float> source = device.Get().AllocateTexture1D(sourceType, array);
using Texture1D<float> destination = device.Get().AllocateTexture1D<float>(destinationType, destinationWidth, AllocationMode.Clear);
destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, copyWidth);
ReadOnlySpan<float> expected = array;
ReadOnlySpan<float> result = destination.ToArray();
for (int i = 0; i < destinationWidth; i++)
{
if (i >= destinationOffsetX &&
i < destinationOffsetX + copyWidth)
{
int sourceX = i - destinationOffsetX + sourceOffsetX;
Assert.AreEqual(expected[sourceX], result[i]);
}
else
{
Assert.AreEqual(0, result[i]);
}
}
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture1D<>))]
[Resource(typeof(ReadWriteTexture1D<>))]
[AdditionalResource(typeof(ReadOnlyTexture1D<>))]
[AdditionalResource(typeof(ReadWriteTexture1D<>))]
[Data(1024, 1024, 1024, 0, 2055)]
[Data(1024, 1024, 1024, 0, 3920)]
[Data(1024, 1024, 1024, 0, -1)]
[Data(1024, 1024, 1024, 0, int.MaxValue)]
[Data(512, 1024, 0, 0, 768)]
[Data(512, 1024, 0, 0, 620)]
[Data(1024, 512, 512, 0, 768)]
[Data(1024, 512, 512, 0, 620)]
[Data(512, 512, 450, 0, 128)]
[Data(512, 512, 0, 450, 128)]
[Data(512, 512, -1, 0, 128)]
[Data(512, 512, 0, -1, 128)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CopyFrom_TextureToVoid_Fail(
Device device,
Type sourceType,
Type destinationType,
int sourceWidth,
int destinationWidth,
int sourceOffsetX,
int destinationOffsetX,
int copyWidth)
{
using Texture1D<float> source = device.Get().AllocateTexture1D<float>(sourceType, sourceWidth);
using Texture1D<float> destination = device.Get().AllocateTexture1D<float>(destinationType, destinationWidth, AllocationMode.Clear);
destination.CopyFrom(source, sourceOffsetX, destinationOffsetX, copyWidth);
}
[CombinatorialTestMethod]
[AllDevices]
public void Dispatch_ReadOnlyTexture1D(Device device)
{
int[] data = Enumerable.Range(0, 256).ToArray();
using ReadOnlyTexture1D<int> source = device.Get().AllocateReadOnlyTexture1D(data);
using ReadWriteBuffer<int> destination = device.Get().AllocateReadWriteBuffer<int>(data.Length);
device.Get().For(source.Width, new ReadOnlyTexture1DKernel(source, destination));
int[] result = destination.ToArray();
CollectionAssert.AreEqual(data, result);
}
[AutoConstructor]
internal readonly partial struct ReadOnlyTexture1DKernel : IComputeShader
{
public readonly ReadOnlyTexture1D<int> source;
public readonly ReadWriteBuffer<int> destination;
public void Execute()
{
destination[ThreadIds.X] = source[ThreadIds.X];
}
}
[CombinatorialTestMethod]
[AllDevices]
public void Dispatch_ReadWriteTexture1D(Device device)
{
int[] data = Enumerable.Range(0,256).ToArray();
using ReadWriteTexture1D<int> source = device.Get().AllocateReadWriteTexture1D(data);
using ReadWriteTexture1D<int> destination = device.Get().AllocateReadWriteTexture1D<int>(256);
device.Get().For(source.Width, new ReadWriteTexture1DKernel(source, destination));
int[] result = new int[data.Length];
destination.CopyTo(result);
CollectionAssert.AreEqual(data, result);
}
[AutoConstructor]
internal readonly partial struct ReadWriteTexture1DKernel : IComputeShader
{
public readonly ReadWriteTexture1D<int> source;
public readonly ReadWriteTexture1D<int> destination;
public void Execute()
{
destination[ThreadIds.X] = source[ThreadIds.X];
}
}
}
+1 -1
ファイルの表示
@@ -11,7 +11,7 @@ using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Tests;
public partial class Texture2DTests
partial class Texture2DTests
{
[CombinatorialTestMethod]
[AllDevices]
+1 -1
ファイルの表示
@@ -4,7 +4,7 @@ using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
public partial class Texture2DTests
partial class Texture2DTests
{
[CombinatorialTestMethod]
[AllDevices]
+1 -1
ファイルの表示
@@ -29,7 +29,7 @@ using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
public partial class Texture2DTests
partial class Texture2DTests
{
[CombinatorialTestMethod]
[AllDevices]
+1 -1
ファイルの表示
@@ -7,7 +7,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests;
public partial class Texture3DTests
partial class Texture3DTests
{
[CombinatorialTestMethod]
[AllDevices]
+1 -1
ファイルの表示
@@ -4,7 +4,7 @@ using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
public partial class Texture3DTests
partial class Texture3DTests
{
[CombinatorialTestMethod]
[AllDevices]
+1 -1
ファイルの表示
@@ -29,7 +29,7 @@ using ComputeSharp.Tests.Extensions;
namespace ComputeSharp.Tests;
public partial class Texture3DTests
partial class Texture3DTests
{
[CombinatorialTestMethod]
[AllDevices]
+175
ファイルの表示
@@ -0,0 +1,175 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests;
[TestClass]
[TestCategory("TransferTexture1D")]
public partial class TransferTexture1DTests
{
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(UploadTexture1D<>))]
[Resource(typeof(ReadBackTexture1D<>))]
[Data(AllocationMode.Default)]
[Data(AllocationMode.Clear)]
public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode)
{
using TransferTexture1D<float> texture = device.Get().AllocateTransferTexture1D<float>(textureType, 256, allocationMode);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 256);
Assert.AreEqual(texture.Memory.Length, 256);
Assert.AreEqual(texture.Span.Length, 256);
Assert.AreSame(texture.GraphicsDevice, device.Get());
if (allocationMode == AllocationMode.Clear)
{
foreach (float x in texture.Memory.Span)
{
Assert.AreEqual(x, 0f);
}
foreach (float x in texture.Span)
{
Assert.AreEqual(x, 0f);
}
}
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(UploadTexture1D<>))]
[Resource(typeof(ReadBackTexture1D<>))]
[Data(-14253)]
[Data(-1)]
[Data(0)]
[Data(-14)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width)
{
using TransferTexture1D<float> texture = device.Get().AllocateTransferTexture1D<float>(textureType, width);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(UploadTexture1D<>))]
[Resource(typeof(ReadBackTexture1D<>))]
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Device device, Type textureType)
{
using TransferTexture1D<float> texture = device.Get().AllocateTransferTexture1D<float>(textureType, 32);
texture.Dispose();
_ = texture.Span;
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void Allocate_UploadTexture1D_Copy_Full(Device device)
{
using UploadTexture1D<int> uploadTexture1D = device.Get().AllocateUploadTexture1D<int>(256);
new Random(42).NextBytes(uploadTexture1D.Span.AsBytes());
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D<int>(uploadTexture1D.Width);
uploadTexture1D.CopyTo(readOnlyTexture1D);
int[] result = readOnlyTexture1D.ToArray();
Assert.AreEqual(uploadTexture1D.Width, result.Length);
fixed (int* _ = result)
{
Assert.IsTrue(uploadTexture1D.Span.SequenceEqual(new Span<int>(Unsafe.AsPointer(ref result[0]), result.Length)));
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 2048)]
[Data(128, 1813)]
[Data(0, 2000)]
[Data(97, 512)]
public unsafe void Allocate_UploadTexture1D_Copy_Range(Device device, int x, int width)
{
using UploadTexture1D<int> uploadTexture1D = device.Get().AllocateUploadTexture1D<int>(2048);
new Random(42).NextBytes(uploadTexture1D.Span.AsBytes());
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D<int>(uploadTexture1D.Width);
uploadTexture1D.CopyTo(readOnlyTexture1D, x, width);
int[] result = readOnlyTexture1D.ToArray();
fixed (void* _ = result)
{
Span<int> sourceRow = uploadTexture1D.Span.Slice(x, width);
Span<int> destinationRow = new Span<int>(Unsafe.AsPointer(ref result[0]), result.Length).Slice(0, width);
Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void Allocate_ReadBackTexture1D_Copy_Full(Device device)
{
int[] source = new int[256];
fixed (int* p = source)
{
new Random(42).NextBytes(new Span<int>(p, source.Length).AsBytes());
}
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source);
using ReadBackTexture1D<int> readBackTexture1D = device.Get().AllocateReadBackTexture1D<int>(256);
readOnlyTexture1D.CopyTo(readBackTexture1D);
Assert.AreEqual(readBackTexture1D.Width, 256);
fixed (int* p = source)
{
Assert.IsTrue(readBackTexture1D.Span.SequenceEqual(new Span<int>(Unsafe.AsPointer(ref source[0]), source.Length)));
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(0, 2048)]
[Data(128, 1813)]
[Data(0, 2000)]
[Data(97, 512)]
public unsafe void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int width)
{
int[] source = new int[2048];
fixed (int* p = source)
{
new Random(42).NextBytes(new Span<int>(p, source.Length).AsBytes());
}
using ReadOnlyTexture1D<int> readOnlyTexture1D = device.Get().AllocateReadOnlyTexture1D(source);
using ReadBackTexture1D<int> readBackTexture1D = device.Get().AllocateReadBackTexture1D<int>(2048);
readOnlyTexture1D.CopyTo(readBackTexture1D, x, width);
fixed (int* p = source)
{
Span<int> sourceRow = new Span<int>(Unsafe.AsPointer(ref source[0]), source.Length).Slice(x, width);
Span<int> destinationRow = readBackTexture1D.Span.Slice(0, width);
Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
}
}
}