From 609ca93c73b6ebb77273df8833b1a71162f00ebe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Nov 2021 19:41:22 +0100 Subject: [PATCH 01/19] Refactor Buffer.CopyTo(T[]) extension --- .../Graphics/Extensions/BufferExtensions.cs | 6 ++--- tests/ComputeSharp.Tests/BufferTests.cs | 25 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index edaaac86..f4fc22e9 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -63,15 +63,15 @@ public static class BufferExtensions /// The type of items stored on the buffer. /// The input instance to read data from. /// The input array to write data to. + /// The offset to start reading data from. /// The starting offset within to write data to. - /// The offset to start reading data from. /// The number of items to read. - public static void CopyTo(this Buffer buffer, T[] destination, int destinationOffset, int bufferOffset, int count) + public static void CopyTo(this Buffer buffer, T[] destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Span span = destination.AsSpan(destinationOffset, count); - buffer.CopyTo(span, bufferOffset); + buffer.CopyTo(span, sourceOffset); } /// diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index ebdc8dfe..69895f84 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -184,10 +184,11 @@ public partial class BufferTests [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] [Data(0, 0, 4096)] - [Data(512, 0, 2048)] [Data(0, 512, 2048)] - [Data(127, 1024, 587)] - public void CopyTo_RangeToVoid_Ok(Device device, Type bufferType, int destinationOffset, int bufferOffset, int count) + [Data(512, 0, 2048)] + [Data(1024, 127, 587)] + [Data(65, 127, 587)] + public void CopyTo_RangeToVoid_Ok(Device device, Type bufferType, int sourceOffset, int destinationOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); @@ -195,9 +196,9 @@ public partial class BufferTests float[] result = new float[4096]; - buffer.CopyTo(result, destinationOffset, bufferOffset, count); + buffer.CopyTo(result, sourceOffset, destinationOffset, count); - Assert.IsTrue(array.AsSpan(bufferOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count))); + Assert.IsTrue(array.AsSpan(sourceOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count))); } [CombinatorialTestMethod] @@ -206,13 +207,13 @@ public partial class BufferTests [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] [Data(0, 0, 8196)] - [Data(-12, 0, 1024)] - [Data(0, -56, 1024)] - [Data(512, 0, 4096)] - [Data(12, 1024, 3600)] - [Data(12, 1024, -2096)] + [Data(0, -12, 1024)] + [Data(-56, 0, 1024)] + [Data(0, 512, 4096)] + [Data(1024, 12, 3600)] + [Data(1024, 12, - 2096)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void CopyTo_RangeToVoid_Fail(Device device, Type bufferType, int destinationOffset, int bufferOffset, int count) + public void CopyTo_RangeToVoid_Fail(Device device, Type bufferType, int sourceOffset, int destinationOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); @@ -220,7 +221,7 @@ public partial class BufferTests float[] result = new float[4096]; - buffer.CopyTo(result, destinationOffset, bufferOffset, count); + buffer.CopyTo(result, sourceOffset, destinationOffset, count); } [CombinatorialTestMethod] From 9345e646ef791617883f4fe4aec91e2be3a76dfb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Nov 2021 22:54:41 +0100 Subject: [PATCH 02/19] Refactor 1D MemoryHelper.Copy extensions --- .../Graphics/Helpers/MemoryHelper.cs | 148 ++++++++++-------- .../Resources/Abstract/StructuredBuffer{T}.cs | 84 +++++----- .../Graphics/Resources/ConstantBuffer{T}.cs | 80 +++------- 3 files changed, 157 insertions(+), 155 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs index 71ba72da..8b9597c3 100644 --- a/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/MemoryHelper.cs @@ -9,87 +9,109 @@ namespace ComputeSharp.Graphics.Helpers; internal static class MemoryHelper { /// - /// Copies the content of a source memory area to a target one, accounting for padding. - /// The destination memory area has padding for each element, while the source does not. + /// Copies the content of a source memory area to a target one, accounting for padding if present. /// + /// The type of elements being copied. /// The source memory area to read from. /// The pointer for the destination memory area. - /// The destination offset to start writing data to. - /// The number of items to copy. - /// The padded size of each element. - /// Indicates whether or not the source is padded too. + /// The source offset to start reading data from. + /// The destination offset to start writing data to. + /// The padded size of each element in . + /// The padded size of each element in . + /// The number of items to copy from to . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Copy( void* source, void* destination, - uint offset, - uint length, - uint elementPitchInBytes, - bool isPaddingInSourceToo) + uint sourceElementOffset, + uint destinationElementOffset, + uint sourceElementPitchInBytes, + uint destinationElementPitchInBytes, + uint count) where T : unmanaged { - destination = (byte*)destination + offset * elementPitchInBytes; + // Offset adjustment + source = (byte*)source + sourceElementOffset * sourceElementPitchInBytes; + destination = (byte*)destination + destinationElementOffset * destinationElementPitchInBytes; - if (isPaddingInSourceToo) + if (sourceElementPitchInBytes == (uint)sizeof(T)) { - for (int i = 0; i < length; i++) + if (destinationElementPitchInBytes == (uint)sizeof(T)) { - *(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes]; + // Neither source nor destination have padding + Buffer.MemoryCopy( + source: source, + destination: destination, + destinationSizeInBytes: ulong.MaxValue, + sourceBytesToCopy: count * (uint)sizeof(T)); } + else + { + // Source has no padding, destination does + static void Copy( + void* source, + void* destination, + uint destinationElementPitchInBytes, + uint count) + { + void* sourceEnd = (byte*)source + count * sizeof(T); + + while (source < sourceEnd) + { + *(T*)destination = *(T*)source; + + source = (byte*)source + sizeof(T); + destination = (byte*)destination + destinationElementPitchInBytes; + } + } + + Copy(source, destination, destinationElementPitchInBytes, count); + } + } + else if (destinationElementPitchInBytes == (uint)sizeof(T)) + { + // Source has padding, destination does not + static void Copy( + void* source, + void* destination, + uint sourceElementPitchInBytes, + uint count) + { + void* sourceEnd = (byte*)source + count * sourceElementPitchInBytes; + + while (source < sourceEnd) + { + *(T*)destination = *(T*)source; + + source = (byte*)source + sourceElementPitchInBytes; + destination = (byte*)destination + sizeof(T); + } + } + + Copy(source, destination, sourceElementPitchInBytes, count); } else { - for (int i = 0; i < length; i++) + // Both source and destination have padding + static void Copy( + void* source, + void* destination, + uint sourceElementPitchInBytes, + uint destinationElementPitchInBytes, + uint count) { - *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i]; + void* sourceEnd = (byte*)source + count * sourceElementPitchInBytes; + + while (source < sourceEnd) + { + *(T*)destination = *(T*)source; + + source = (byte*)source + sourceElementPitchInBytes; + destination = (byte*)destination + destinationElementPitchInBytes; + } } - } - } - /// - /// Copies the content of a source memory area to a target one. - /// - /// The source memory area to read from. - /// The source offset to start reading data from. - /// The number of items to copy. - /// The size in bytes of each item to copy. - /// The pointer for the destination memory area. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( - void* source, - uint offset, - uint length, - uint elementSizeInBytes, - void* destination) - { - Buffer.MemoryCopy( - (byte*)source + offset * elementSizeInBytes, - destination, - ulong.MaxValue, - elementSizeInBytes * length); - } - - /// - /// Copies the content of a source memory area to a target one, accounting for padding. - /// The source memory area has padding for each element, while the destination does not. - /// - /// The source memory area to read from. - /// The source offset to start reading data from. - /// The number of items to copy. - /// The padded size of each element. - /// The pointer for the destination memory area. - public static unsafe void Copy( - void* source, - uint offset, - uint length, - uint elementPitchInBytes, - void* destination) - where T : unmanaged - { - source = (byte*)source + offset * elementPitchInBytes; - - for (int i = 0; i < length; i++) - { - ((T*)destination)[i] = *(T*)&((byte*)source)[i * elementPitchInBytes]; + Copy(source, destination, sourceElementPitchInBytes, destinationElementPitchInBytes, count); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 902ed07f..99bb48e4 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -45,12 +45,14 @@ public abstract class StructuredBuffer : Buffer fixed (void* destinationPointer = &destination) { - MemoryHelper.Copy( - resource.Pointer, - (uint)offset, - (uint)length, - (uint)sizeof(T), - destinationPointer); + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: (uint)offset, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } } else @@ -70,12 +72,14 @@ public abstract class StructuredBuffer : Buffer fixed (void* destinationPointer = &destination) { - MemoryHelper.Copy( - resource.Pointer, - 0u, - (uint)length, - (uint)sizeof(T), - destinationPointer); + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: 0, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } } } @@ -105,12 +109,14 @@ public abstract class StructuredBuffer : Buffer { using ID3D12ResourceMap resource = D3D12Resource->Map(); - MemoryHelper.Copy( - resource.Pointer, - (uint)offset, - (uint)length, - (uint)sizeof(T), - destination.MappedData + destinationOffset); + MemoryHelper.Copy( + source: resource.Pointer, + destination: destination.MappedData, + sourceElementOffset: (uint)offset, + destinationElementOffset: (uint)destinationOffset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } else { @@ -141,12 +147,14 @@ public abstract class StructuredBuffer : Buffer fixed (void* sourcePointer = &source) { - MemoryHelper.Copy( - sourcePointer, - (uint)offset, - (uint)length, - (uint)sizeof(T), - resource.Pointer); + MemoryHelper.Copy( + source: sourcePointer, + destination: resource.Pointer, + sourceElementOffset: 0, + destinationElementOffset: (uint)offset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } } else @@ -159,12 +167,14 @@ public abstract class StructuredBuffer : Buffer using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) { - MemoryHelper.Copy( - sourcePointer, - 0u, - (uint)length, - (uint)sizeof(T), - resource.Pointer); + MemoryHelper.Copy( + source: sourcePointer, + destination: resource.Pointer, + sourceElementOffset: 0, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); @@ -199,12 +209,14 @@ public abstract class StructuredBuffer : Buffer { using ID3D12ResourceMap resource = D3D12Resource->Map(); - MemoryHelper.Copy( - source.MappedData, - (uint)sourceOffset, - (uint)length, - (uint)sizeof(T), - (T*)resource.Pointer + offset); + MemoryHelper.Copy( + source: source.MappedData, + destination: resource.Pointer, + sourceElementOffset: (uint)sourceOffset, + destinationElementOffset: (uint)offset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)length); } else { diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 7432bf43..77fd7d81 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -70,24 +70,14 @@ public sealed class ConstantBuffer : Buffer fixed (void* destinationPointer = &destination) { - if (IsPaddingPresent) - { - MemoryHelper.Copy( - resource.Pointer, - (uint)offset, - (uint)size, - (uint)GetPaddedSize(), - destinationPointer); - } - else - { - MemoryHelper.Copy( - resource.Pointer, - (uint)offset, - (uint)size, - (uint)sizeof(T), - destinationPointer); - } + MemoryHelper.Copy( + source: resource.Pointer, + destination: destinationPointer, + sourceElementOffset: (uint)offset, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)GetPaddedSize(), + destinationElementPitchInBytes: (uint)sizeof(T), + count: (uint)size); } } @@ -105,25 +95,14 @@ public sealed class ConstantBuffer : Buffer fixed (void* sourcePointer = &source) { - if (IsPaddingPresent) - { - MemoryHelper.Copy( - sourcePointer, - resource.Pointer, - (uint)offset, - (uint)size, - (uint)GetPaddedSize(), - isPaddingInSourceToo: false); - } - else - { - MemoryHelper.Copy( - sourcePointer, - (uint)offset, - (uint)size, - (uint)sizeof(T), - resource.Pointer); - } + MemoryHelper.Copy( + source: sourcePointer, + destination: resource.Pointer, + sourceElementOffset: 0, + destinationElementOffset: (uint)offset, + sourceElementPitchInBytes: (uint)sizeof(T), + destinationElementPitchInBytes: (uint)GetPaddedSize(), + count: (uint)size); } } @@ -144,25 +123,14 @@ public sealed class ConstantBuffer : Buffer using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map(); using ID3D12ResourceMap destinationMap = D3D12Resource->Map(); - if (IsPaddingPresent) - { - MemoryHelper.Copy( - sourceMap.Pointer, - destinationMap.Pointer, - 0, - (uint)source.Length, - (uint)GetPaddedSize(), - isPaddingInSourceToo: true); - } - else - { - MemoryHelper.Copy( - sourceMap.Pointer, - 0, - (uint)source.Length, - (uint)sizeof(T), - destinationMap.Pointer); - } + MemoryHelper.Copy( + source: sourceMap.Pointer, + destination: destinationMap.Pointer, + sourceElementOffset: 0, + destinationElementOffset: 0, + sourceElementPitchInBytes: (uint)GetPaddedSize(), + destinationElementPitchInBytes: (uint)GetPaddedSize(), + count: (uint)source.Length); } else CopyFromWithCpuBuffer(source); } From c7353473865ffb46eb920459bd7047f8f133daca Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 01:41:18 +0100 Subject: [PATCH 03/19] Add [AdditionalResource] type to test project --- .../Attributes/AdditionalResourceAttribute.cs | 17 +++++ .../CombinatorialTestMethodAttribute.cs | 67 ++++++++++++++++--- 2 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs new file mode 100644 index 00000000..342db016 --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs @@ -0,0 +1,17 @@ +using System; + +namespace ComputeSharp.Tests.Attributes; + +/// +/// An attribute to use with targeting an additional specific resource type. +/// +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class AdditionalResourceAttribute : Attribute +{ + public AdditionalResourceAttribute(Type type) + { + Type = type; + } + + public Type Type { get; } +} diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs index 53dd4561..0e85b112 100644 --- a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs @@ -37,6 +37,7 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes } Type[] resources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); + Type[] additionalResources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); if (devices.Length > 0) @@ -47,21 +48,46 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { foreach (Type type in resources) { - if (data.Length == 0) + if (additionalResources.Length > 0) { - yield return new object[] { device, type }; + foreach (Type additionalType in additionalResources) + { + if (data.Length == 0) + { + yield return new object[] { device, type, additionalType }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type, additionalType }.Concat(items).ToArray(); + } + } + } } else { - foreach (object[] items in data) + if (data.Length == 0) { - yield return new object[] { device, type }.Concat(items).ToArray(); + yield return new object[] { device, type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type }.Concat(items).ToArray(); + } } } } } else { + if (additionalResources.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalResource]"); + } + if (data.Length == 0) { yield return new object[] { device }; @@ -80,21 +106,46 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { foreach (Type type in resources) { - if (data.Length == 0) + if (additionalResources.Length > 0) { - yield return new object[] { type }; + foreach (Type additionalType in additionalResources) + { + if (data.Length == 0) + { + yield return new object[] { type, additionalType }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type, additionalType }.Concat(items).ToArray(); + } + } + } } else { - foreach (object[] items in data) + if (data.Length == 0) { - yield return new object[] { type }.Concat(items).ToArray(); + yield return new object[] { type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type }.Concat(items).ToArray(); + } } } } } else { + if (additionalResources.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalResource]"); + } + if (data.Length == 0) { yield return Array.Empty(); From 5261df4c0e786d45630e75829e3716a357783f30 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 01:41:37 +0100 Subject: [PATCH 04/19] Refactor Buffer copy extensions --- .../Graphics/Extensions/BufferExtensions.cs | 54 +++++++++++++ .../Graphics/Resources/Abstract/Buffer{T}.cs | 40 ++++++---- .../Resources/Abstract/StructuredBuffer{T}.cs | 67 ++++++++++------ .../Graphics/Resources/ConstantBuffer{T}.cs | 77 ++++++++++--------- tests/ComputeSharp.Tests/BufferTests.cs | 52 +++++++++++++ 5 files changed, 213 insertions(+), 77 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index f4fc22e9..ce6cba9f 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -99,6 +99,33 @@ public static class BufferExtensions buffer.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, offset); } + /// + /// Reads the contents of the specified instance and copies them to the target instance. + /// + /// The type of items stored on the buffers. + /// The input instance to read data from. + /// The target instance to write data to. + public static void CopyTo(this Buffer buffer, Buffer destination) + where T : unmanaged + { + buffer.CopyTo(destination, 0, 0, buffer.Length); + } + + /// + /// Reads the contents of the specified range from a instance and copies them to the target instance. + /// + /// The type of items stored on the buffers. + /// The input instance to read data from. + /// The target instance to write data to. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + public static void CopyTo(this Buffer buffer, Buffer destination, int sourceOffset, int destinationOffset, int count) + where T : unmanaged + { + buffer.CopyTo(destination, sourceOffset, destinationOffset, count); + } + /// /// Writes the contents of a given array to a instance. /// @@ -152,4 +179,31 @@ public static class BufferExtensions { buffer.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, offset); } + + /// + /// Writes the contents of the specified instance to the curreent instance. + /// + /// The type of items stored on the buffers. + /// The input instance to write data data to. + /// The source instance to read data from. + public static void CopyFrom(this Buffer buffer, Buffer source) + where T : unmanaged + { + source.CopyTo(buffer, 0, 0, source.Length); + } + + /// + /// Writes the contents of the specified range from a instance and copies them to the current instance. + /// + /// The type of items stored on the buffers. + /// The input instance to write data to. + /// The source instance to read data from. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + public static void CopyFrom(this Buffer buffer, Buffer source, int sourceOffset, int destinationOffset, int count) + where T : unmanaged + { + source.CopyTo(buffer, sourceOffset, destinationOffset, count); + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 03f967c7..8a245512 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -131,34 +131,32 @@ public unsafe abstract class Buffer : NativeObject internal abstract void CopyTo(ref T destination, int length, int offset); /// - /// Writes the contents of a given memory area to a specified area of the current instance. + /// Writes the contents of a given range from the current instance and writes them into a target instance. /// - /// The input memory area to read data from. - /// The length of the input memory area to read data from. - /// The offset to start writing data to. - internal abstract void CopyFrom(ref T source, int length, int offset); + /// The target instance to write data to. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + internal abstract void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length); /// - /// Writes the contents of a given to the current instance. + /// Writes the contents of a given range from the current instance and writes them into a target instance, using a temporary CPU buffer. /// - /// The input to read data from. - public abstract void CopyFrom(Buffer source); - - /// - /// Writes the contents of a given to the current instance, using a temporary CPU buffer. - /// - /// The input to read data from. - protected void CopyFromWithCpuBuffer(Buffer source) + /// The target instance to write data to. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + protected void CopyToWithCpuBuffer(Buffer destination, int sourceOffset, int destinationOffset, int length) { - T[] array = ArrayPool.Shared.Rent(source.Length); + T[] array = ArrayPool.Shared.Rent(length); try { ref T r0 = ref MemoryMarshal.GetArrayDataReference(array); - source.CopyTo(ref r0, source.Length, 0); + CopyTo(ref r0, length, sourceOffset); - CopyFrom(ref r0, source.Length, 0); + destination.CopyFrom(ref r0, length, destinationOffset); } finally { @@ -166,6 +164,14 @@ public unsafe abstract class Buffer : NativeObject } } + /// + /// Writes the contents of a given memory area to a specified area of the current instance. + /// + /// The input memory area to read data from. + /// The length of the input memory area to read data from. + /// The offset to start writing data to. + internal abstract void CopyFrom(ref T source, int length, int offset); + /// protected override void OnDispose() { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 99bb48e4..30a01823 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -36,8 +36,9 @@ public abstract class StructuredBuffer : Buffer ThrowIfDisposed(); + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length)); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); if (GraphicsDevice.IsCacheCoherentUMA) { @@ -84,6 +85,44 @@ public abstract class StructuredBuffer : Buffer } } + /// + internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); + Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length)); + Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset)); + Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); + Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); + Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); + + if (!destination.IsPaddingPresent) + { + nint sourceByteOffset = (nint)sourceOffset * sizeof(T); + nint destinationByteOffset = (nint)destinationOffset * sizeof(T); + nint byteLength = length * sizeof(T); + + // Directly copy the input buffer + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion( + destination.D3D12Resource, + (ulong)destinationByteOffset, + D3D12Resource, + (ulong)sourceByteOffset, + (ulong)byteLength); + + copyCommandList.ExecuteAndWaitForCompletion(); + } + else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length); + } + /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// @@ -138,8 +177,9 @@ public abstract class StructuredBuffer : Buffer ThrowIfDisposed(); + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length)); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); if (GraphicsDevice.IsCacheCoherentUMA) { @@ -230,27 +270,4 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } } - - /// - public override unsafe void CopyFrom(Buffer source) - { - GraphicsDevice.ThrowIfDisposed(); - - ThrowIfDisposed(); - - source.ThrowIfDeviceMismatch(GraphicsDevice); - source.ThrowIfDisposed(); - - Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length)); - - if (!source.IsPaddingPresent) - { - // Directly copy the input buffer - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); - copyCommandList.ExecuteAndWaitForCompletion(); - } - else CopyFromWithCpuBuffer(source); - } } diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 77fd7d81..87a9e671 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -57,14 +57,15 @@ public sealed class ConstantBuffer : Buffer } /// - internal override unsafe void CopyTo(ref T destination, int size, int offset) + internal override unsafe void CopyTo(ref T destination, int length, int offset) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); using ID3D12ResourceMap resource = D3D12Resource->Map(); @@ -77,19 +78,54 @@ public sealed class ConstantBuffer : Buffer destinationElementOffset: 0, sourceElementPitchInBytes: (uint)GetPaddedSize(), destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)size); + count: (uint)length); } } /// - internal override unsafe void CopyFrom(ref T source, int size, int offset) + internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); + Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length)); + Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset)); + Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); + Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); + Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); + + if (destination is ConstantBuffer buffer) + { + using ID3D12ResourceMap sourceMap = D3D12Resource->Map(); + using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map(); + + MemoryHelper.Copy( + source: sourceMap.Pointer, + destination: destinationMap.Pointer, + sourceElementOffset: (uint)sourceOffset, + destinationElementOffset: (uint)destinationOffset, + sourceElementPitchInBytes: (uint)GetPaddedSize(), + destinationElementPitchInBytes: (uint)GetPaddedSize(), + count: (uint)length); + } + else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length); + } + + /// + internal override unsafe void CopyFrom(ref T source, int length, int offset) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size)); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); using ID3D12ResourceMap resource = D3D12Resource->Map(); @@ -102,39 +138,10 @@ public sealed class ConstantBuffer : Buffer destinationElementOffset: (uint)offset, sourceElementPitchInBytes: (uint)sizeof(T), destinationElementPitchInBytes: (uint)GetPaddedSize(), - count: (uint)size); + count: (uint)length); } } - /// - public override unsafe void CopyFrom(Buffer source) - { - GraphicsDevice.ThrowIfDisposed(); - - ThrowIfDisposed(); - - source.ThrowIfDeviceMismatch(GraphicsDevice); - source.ThrowIfDisposed(); - - Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length)); - - if (source is ConstantBuffer buffer) - { - using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map(); - using ID3D12ResourceMap destinationMap = D3D12Resource->Map(); - - MemoryHelper.Copy( - source: sourceMap.Pointer, - destination: destinationMap.Pointer, - sourceElementOffset: 0, - destinationElementOffset: 0, - sourceElementPitchInBytes: (uint)GetPaddedSize(), - destinationElementPitchInBytes: (uint)GetPaddedSize(), - count: (uint)source.Length); - } - else CopyFromWithCpuBuffer(source); - } - /// public override string ToString() { diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 69895f84..2eb9a06e 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -224,6 +224,58 @@ public partial class BufferTests buffer.CopyTo(result, sourceOffset, destinationOffset, count); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [AdditionalResource(typeof(ConstantBuffer<>))] + [AdditionalResource(typeof(ReadOnlyBuffer<>))] + [AdditionalResource(typeof(ReadWriteBuffer<>))] + [Data(0, 0, 4096)] + [Data(0, 512, 2048)] + [Data(512, 0, 2048)] + [Data(1024, 127, 587)] + [Data(65, 127, 587)] + public void CopyTo_BufferToVoid_Ok(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Buffer source = device.Get().AllocateBuffer(sourceType, array); + using Buffer destination = device.Get().AllocateBuffer(destinationType, 4096); + + source.CopyTo(destination, sourceOffset, destinationOffset, count); + + float[] result = destination.ToArray(); + + Assert.IsTrue(array.AsSpan(sourceOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count))); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [AdditionalResource(typeof(ConstantBuffer<>))] + [AdditionalResource(typeof(ReadOnlyBuffer<>))] + [AdditionalResource(typeof(ReadWriteBuffer<>))] + [Data(0, 0, 8196)] + [Data(0, -12, 1024)] + [Data(-56, 0, 1024)] + [Data(0, 512, 4096)] + [Data(1024, 12, 3600)] + [Data(1024, 12, -2096)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyTo_BufferToVoid_Fail(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Buffer source = device.Get().AllocateBuffer(sourceType, array); + using Buffer destination = device.Get().AllocateBuffer(destinationType, 4096); + + source.CopyTo(destination, sourceOffset, destinationOffset, count); + } + [CombinatorialTestMethod] [AllDevices] public void Dispatch_ConstantBuffer(Device device) From ffa4881d0acaf409e2dd81c24e41d9da1adc8fbe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 01:53:01 +0100 Subject: [PATCH 05/19] Refactor signatures and param names in buffer APIs --- .../Graphics/Extensions/BufferExtensions.cs | 92 +++++++++---------- .../Extensions/StructuredBufferExtensions.cs | 14 +-- .../Extensions/UploadBufferExtensions.cs | 14 +-- .../Graphics/Resources/Abstract/Buffer{T}.cs | 26 +++--- .../Resources/Abstract/StructuredBuffer{T}.cs | 78 ++++++++-------- .../Graphics/Resources/ConstantBuffer{T}.cs | 4 +- .../Resources/Debug/BufferDebugView{T}.cs | 2 +- .../ComputeSharp.Tests/TransferBufferTests.cs | 4 +- 8 files changed, 119 insertions(+), 115 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs index ce6cba9f..90707aec 100644 --- a/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/BufferExtensions.cs @@ -15,32 +15,32 @@ public static class BufferExtensions /// Reads the contents of a instance and returns an array. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// A array with the contents of the input buffer. [Pure] - public static T[] ToArray(this Buffer buffer) + public static T[] ToArray(this Buffer source) where T : unmanaged { - return buffer.ToArray(0, buffer.Length); + return source.ToArray(0, source.Length); } /// /// Reads the contents of a instance in a given range and returns an array. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The offset to start reading data from. /// The number of items to read. /// A array with the contents of the specified range from the current buffer. [Pure] - public static T[] ToArray(this Buffer buffer, int offset, int count) + public static T[] ToArray(this Buffer source, int offset, int count) where T : unmanaged { Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); T[] data = GC.AllocateUninitializedArray(count); - buffer.CopyTo(data.AsSpan(), offset); + source.CopyTo(data.AsSpan(), offset); return data; } @@ -49,161 +49,161 @@ public static class BufferExtensions /// Reads the contents of a instance and writes them into a target array. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - public static void CopyTo(this Buffer buffer, T[] destination) + public static void CopyTo(this Buffer source, T[] destination) where T : unmanaged { - buffer.CopyTo(destination.AsSpan(), 0); + source.CopyTo(destination.AsSpan(), 0); } /// /// Reads the contents of the specified range from a instance and writes them into a target array. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this Buffer buffer, T[] destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this Buffer source, T[] destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { Span span = destination.AsSpan(destinationOffset, count); - buffer.CopyTo(span, sourceOffset); + source.CopyTo(span, sourceOffset); } /// /// Reads the contents of a instance and writes them into a target . /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - public static void CopyTo(this Buffer buffer, Span destination) + public static void CopyTo(this Buffer source, Span destination) where T : unmanaged { - buffer.CopyTo(destination, 0); + source.CopyTo(destination, 0); } /// /// Reads the contents of the specified range from a instance and writes them into a target . /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - /// The offset to start reading data from. - public static void CopyTo(this Buffer buffer, Span destination, int offset) + /// The offset to start reading data from. + public static void CopyTo(this Buffer source, Span destination, int sourceOffset) where T : unmanaged { - buffer.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, offset); + source.CopyTo(ref MemoryMarshal.GetReference(destination), sourceOffset, destination.Length); } /// /// Reads the contents of the specified instance and copies them to the target instance. /// /// The type of items stored on the buffers. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this Buffer buffer, Buffer destination) + public static void CopyTo(this Buffer source, Buffer destination) where T : unmanaged { - buffer.CopyTo(destination, 0, 0, buffer.Length); + source.CopyTo(destination, 0, 0, source.Length); } /// /// Reads the contents of the specified range from a instance and copies them to the target instance. /// /// The type of items stored on the buffers. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. /// The offset to start reading data from. /// The starting offset within to write data to. /// The number of items to read. - public static void CopyTo(this Buffer buffer, Buffer destination, int sourceOffset, int destinationOffset, int count) + public static void CopyTo(this Buffer source, Buffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - buffer.CopyTo(destination, sourceOffset, destinationOffset, count); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } /// /// Writes the contents of a given array to a instance. /// /// The type of items stored on the buffer. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - public static void CopyFrom(this Buffer buffer, T[] source) + public static void CopyFrom(this Buffer destination, T[] source) where T : unmanaged { - buffer.CopyFrom(source.AsSpan(), 0); + destination.CopyFrom(source.AsSpan(), 0); } /// /// Writes the contents of a given array to a specified area of a instance. /// /// The type of items stored on the buffer. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. /// The starting offset within to read data from. - /// The offset to start writing data to. + /// The offset to start writing data to. /// The number of items to write. - public static void CopyFrom(this Buffer buffer, T[] source, int sourceOffset, int bufferOffset, int count) + public static void CopyFrom(this Buffer destination, T[] source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { ReadOnlySpan span = source.AsSpan(sourceOffset, count); - buffer.CopyFrom(span, bufferOffset); + destination.CopyFrom(span, destinationOffset); } /// /// Writes the contents of a given to a instance. /// /// The type of items stored on the buffer. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - public static void CopyFrom(this Buffer buffer, ReadOnlySpan source) + public static void CopyFrom(this Buffer destination, ReadOnlySpan source) where T : unmanaged { - buffer.CopyFrom(source, 0); + destination.CopyFrom(source, 0); } /// /// Writes the contents of a given to a specified area of a instance. /// /// The type of items stored on the buffer. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - /// The offset to start writing data to. - public static void CopyFrom(this Buffer buffer, ReadOnlySpan source, int offset) + /// The offset to start writing data to. + public static void CopyFrom(this Buffer destination, ReadOnlySpan source, int destinationOffset) where T : unmanaged { - buffer.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, offset); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), destinationOffset, source.Length); } /// /// Writes the contents of the specified instance to the curreent instance. /// /// The type of items stored on the buffers. - /// The input instance to write data data to. + /// The input instance to write data data to. /// The source instance to read data from. - public static void CopyFrom(this Buffer buffer, Buffer source) + public static void CopyFrom(this Buffer destination, Buffer source) where T : unmanaged { - source.CopyTo(buffer, 0, 0, source.Length); + source.CopyTo(destination, 0, 0, source.Length); } /// /// Writes the contents of the specified range from a instance and copies them to the current instance. /// /// The type of items stored on the buffers. - /// The input instance to write data to. + /// The input instance to write data to. /// The source instance to read data from. /// The offset to start reading data from. - /// The starting offset within to write data to. + /// The starting offset within to write data to. /// The number of items to read. - public static void CopyFrom(this Buffer buffer, Buffer source, int sourceOffset, int destinationOffset, int count) + public static void CopyFrom(this Buffer destination, Buffer source, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - source.CopyTo(buffer, sourceOffset, destinationOffset, count); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index bde72b1f..3438e4ff 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -11,26 +11,26 @@ public static class StructuredBufferExtensions /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this StructuredBuffer buffer, ReadBackBuffer destination) + public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination) where T : unmanaged { - buffer.CopyTo(destination, 0, buffer.Length, 0); + source.CopyTo(destination, 0, 0, source.Length); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. + /// The offset to start reading data from. /// The starting offset within to write data to. - /// The offset to start reading data from. /// The number of items to read. - public static void CopyTo(this StructuredBuffer buffer, ReadBackBuffer destination, int destinationOffset, int bufferOffset, int count) + public static void CopyTo(this StructuredBuffer source, ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - buffer.CopyTo(destination, destinationOffset, count, bufferOffset); + source.CopyTo(destination, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs index 6dc443e8..10054354 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadBufferExtensions.cs @@ -11,26 +11,26 @@ public static class UploadBufferExtensions /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this UploadBuffer buffer, StructuredBuffer destination) + public static void CopyTo(this UploadBuffer source, StructuredBuffer destination) where T : unmanaged { - destination.CopyFrom(buffer, 0, buffer.Length, 0); + destination.CopyFrom(source, 0, 0, source.Length); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the buffer. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. + /// The offset to start reading data from. /// The starting offset within to write data to. - /// The offset to start reading data from. /// The number of items to read. - public static void CopyTo(this UploadBuffer buffer, StructuredBuffer destination, int destinationOffset, int bufferOffset, int count) + public static void CopyTo(this UploadBuffer source, StructuredBuffer destination, int sourceOffset, int destinationOffset, int count) where T : unmanaged { - destination.CopyFrom(buffer, bufferOffset, count, destinationOffset); + destination.CopyFrom(source, sourceOffset, destinationOffset, count); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 8a245512..ce442d70 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -126,9 +126,9 @@ public unsafe abstract class Buffer : NativeObject /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// /// The input memory area to write data to. - /// The length of the memory area to write data to. - /// The offset to start reading data from. - internal abstract void CopyTo(ref T destination, int length, int offset); + /// The offset to start reading data from. + /// The length of the memory area to write data to. + internal abstract void CopyTo(ref T destination, int sourceOffset, int count); /// /// Writes the contents of a given range from the current instance and writes them into a target instance. @@ -136,8 +136,8 @@ public unsafe abstract class Buffer : NativeObject /// The target instance to write data to. /// The offset to start reading data from. /// The starting offset within to write data to. - /// The number of items to read. - internal abstract void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length); + /// The number of items to read. + internal abstract void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count); /// /// Writes the contents of a given range from the current instance and writes them into a target instance, using a temporary CPU buffer. @@ -145,18 +145,18 @@ public unsafe abstract class Buffer : NativeObject /// The target instance to write data to. /// The offset to start reading data from. /// The starting offset within to write data to. - /// The number of items to read. - protected void CopyToWithCpuBuffer(Buffer destination, int sourceOffset, int destinationOffset, int length) + /// The number of items to read. + protected void CopyToWithCpuBuffer(Buffer destination, int sourceOffset, int destinationOffset, int count) { - T[] array = ArrayPool.Shared.Rent(length); + T[] array = ArrayPool.Shared.Rent(count); try { ref T r0 = ref MemoryMarshal.GetArrayDataReference(array); - CopyTo(ref r0, length, sourceOffset); + CopyTo(ref r0, sourceOffset, count); - destination.CopyFrom(ref r0, length, destinationOffset); + destination.CopyFrom(ref r0, destinationOffset, count); } finally { @@ -168,9 +168,9 @@ public unsafe abstract class Buffer : NativeObject /// Writes the contents of a given memory area to a specified area of the current instance. /// /// The input memory area to read data from. - /// The length of the input memory area to read data from. - /// The offset to start writing data to. - internal abstract void CopyFrom(ref T source, int length, int offset); + /// The offset to start writing data to. + /// The length of the input memory area to read data from. + internal abstract void CopyFrom(ref T source, int destinationOffset, int count); /// protected override void OnDispose() diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 30a01823..4992bab5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -30,15 +30,15 @@ public abstract class StructuredBuffer : Buffer } /// - internal override unsafe void CopyTo(ref T destination, int length, int offset) + internal override unsafe void CopyTo(ref T destination, int sourceOffset, int count) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); - Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset)); + Guard.IsBetweenOrEqualTo(count, 0, Length, nameof(count)); + Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset)); + Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); if (GraphicsDevice.IsCacheCoherentUMA) { @@ -49,17 +49,17 @@ public abstract class StructuredBuffer : Buffer MemoryHelper.Copy( source: resource.Pointer, destination: destinationPointer, - sourceElementOffset: (uint)offset, + sourceElementOffset: (uint)sourceOffset, destinationElementOffset: 0, sourceElementPitchInBytes: (uint)sizeof(T), destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); + count: (uint)count); } } else { - nint byteOffset = (nint)offset * sizeof(T); - nint byteLength = length * sizeof(T); + nint byteOffset = (nint)sourceOffset * sizeof(T); + nint byteLength = count * sizeof(T); using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); @@ -80,13 +80,13 @@ public abstract class StructuredBuffer : Buffer destinationElementOffset: 0, sourceElementPitchInBytes: (uint)sizeof(T), destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); + count: (uint)count); } } } /// - internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int length) + internal override unsafe void CopyTo(Buffer destination, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); @@ -95,18 +95,18 @@ public abstract class StructuredBuffer : Buffer destination.ThrowIfDeviceMismatch(GraphicsDevice); destination.ThrowIfDisposed(); - Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length)); - Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length)); + Guard.IsBetweenOrEqualTo(count, 0, Length, nameof(count)); + Guard.IsBetweenOrEqualTo(count, 0, destination.Length, nameof(count)); Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset)); - Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset)); + Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); - Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset)); + Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); if (!destination.IsPaddingPresent) { nint sourceByteOffset = (nint)sourceOffset * sizeof(T); nint destinationByteOffset = (nint)destinationOffset * sizeof(T); - nint byteLength = length * sizeof(T); + nint byteLength = count * sizeof(T); // Directly copy the input buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); @@ -120,17 +120,17 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } - else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length); + else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, count); } /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// /// The target instance to write data to. + /// The offset to start reading data from. /// The starting offset within to write data to. - /// The number of items to read. - /// The offset to start reading data from. - internal unsafe void CopyTo(ReadBackBuffer destination, int destinationOffset, int length, int offset) + /// The number of items to read. + internal unsafe void CopyTo(ReadBackBuffer destination, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); @@ -139,10 +139,12 @@ public abstract class StructuredBuffer : Buffer destination.ThrowIfDeviceMismatch(GraphicsDevice); destination.ThrowIfDisposed(); - Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length)); + Guard.IsBetweenOrEqualTo(count, 0, Length, nameof(count)); + Guard.IsBetweenOrEqualTo(count, 0, destination.Length, nameof(count)); + Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset)); + Guard.IsLessThanOrEqualTo(sourceOffset + count, Length, nameof(sourceOffset)); Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); - Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(length)); + Guard.IsLessThanOrEqualTo(destinationOffset + count, destination.Length, nameof(destinationOffset)); if (GraphicsDevice.IsCacheCoherentUMA) { @@ -151,17 +153,17 @@ public abstract class StructuredBuffer : Buffer MemoryHelper.Copy( source: resource.Pointer, destination: destination.MappedData, - sourceElementOffset: (uint)offset, + sourceElementOffset: (uint)sourceOffset, destinationElementOffset: (uint)destinationOffset, sourceElementPitchInBytes: (uint)sizeof(T), destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); + count: (uint)count); } else { ulong byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T); - ulong byteOffset = (uint)offset * (uint)sizeof(T); - ulong byteLength = (uint)length * (uint)sizeof(T); + ulong byteOffset = (uint)sourceOffset * (uint)sizeof(T); + ulong byteLength = (uint)count * (uint)sizeof(T); using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); @@ -171,7 +173,7 @@ public abstract class StructuredBuffer : Buffer } /// - internal override unsafe void CopyFrom(ref T source, int length, int offset) + internal override unsafe void CopyFrom(ref T source, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); @@ -229,9 +231,9 @@ public abstract class StructuredBuffer : Buffer /// /// The input instance to read data from. /// The starting offset within to read data from. - /// The number of items to read. - /// The offset to start reading writing data to. - internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int length, int offset) + /// The offset to start reading writing data to. + /// The number of items to read. + internal unsafe void CopyFrom(UploadBuffer source, int sourceOffset, int destinationOffset, int count) { GraphicsDevice.ThrowIfDisposed(); @@ -240,10 +242,12 @@ public abstract class StructuredBuffer : Buffer source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); - Guard.IsInRange(offset, 0, Length, nameof(offset)); - Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length)); + Guard.IsBetweenOrEqualTo(count, 0, Length, nameof(count)); + Guard.IsBetweenOrEqualTo(count, 0, source.Length, nameof(count)); Guard.IsInRange(sourceOffset, 0, source.Length, nameof(sourceOffset)); - Guard.IsLessThanOrEqualTo(sourceOffset + length, source.Length, nameof(length)); + Guard.IsLessThanOrEqualTo(sourceOffset + count, source.Length, nameof(sourceOffset)); + Guard.IsInRange(destinationOffset, 0, Length, nameof(destinationOffset)); + Guard.IsLessThanOrEqualTo(destinationOffset + count, Length, nameof(destinationOffset)); if (GraphicsDevice.IsCacheCoherentUMA) { @@ -253,16 +257,16 @@ public abstract class StructuredBuffer : Buffer source: source.MappedData, destination: resource.Pointer, sourceElementOffset: (uint)sourceOffset, - destinationElementOffset: (uint)offset, + destinationElementOffset: (uint)destinationOffset, sourceElementPitchInBytes: (uint)sizeof(T), destinationElementPitchInBytes: (uint)sizeof(T), - count: (uint)length); + count: (uint)count); } else { ulong byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T); - ulong byteOffset = (uint)offset * (uint)sizeof(T); - ulong byteLength = (uint)length * (uint)sizeof(T); + ulong byteOffset = (uint)destinationOffset * (uint)sizeof(T); + ulong byteLength = (uint)count * (uint)sizeof(T); using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); diff --git a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs index 87a9e671..38aa3b47 100644 --- a/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/ConstantBuffer{T}.cs @@ -57,7 +57,7 @@ public sealed class ConstantBuffer : Buffer } /// - internal override unsafe void CopyTo(ref T destination, int length, int offset) + internal override unsafe void CopyTo(ref T destination, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); @@ -117,7 +117,7 @@ public sealed class ConstantBuffer : Buffer } /// - internal override unsafe void CopyFrom(ref T source, int length, int offset) + internal override unsafe void CopyFrom(ref T source, int offset, int length) { GraphicsDevice.ThrowIfDisposed(); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs index 7c761046..44a88976 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs @@ -20,7 +20,7 @@ internal sealed class BufferDebugView { var items = GC.AllocateUninitializedArray(buffer.Length); - buffer.CopyTo(ref items[0], buffer.Length, 0); + buffer.CopyTo(ref items[0], 0, buffer.Length); Items = items; } diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs index d3fb5e34..47cc9a66 100644 --- a/tests/ComputeSharp.Tests/TransferBufferTests.cs +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -124,7 +124,7 @@ public partial class TransferBufferTests using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); - uploadBuffer.CopyTo(readOnlyBuffer, destinationOffset, bufferOffset, count); + uploadBuffer.CopyTo(readOnlyBuffer, bufferOffset, destinationOffset, count); int[] result = readOnlyBuffer.ToArray(destinationOffset, count); @@ -164,7 +164,7 @@ public partial class TransferBufferTests using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); - readOnlyBuffer.CopyTo(readBackBuffer, destinationOffset, bufferOffset, count); + readOnlyBuffer.CopyTo(readBackBuffer, bufferOffset, destinationOffset, count); Assert.AreEqual(source.Length, readBackBuffer.Length); Assert.IsTrue(source.AsSpan(bufferOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count))); From c6577302495b10b285632c881c900018d3979f83 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 01:56:08 +0100 Subject: [PATCH 06/19] Add mirrored StructuredBufferExtensions APIs --- .../Extensions/StructuredBufferExtensions.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs index 3438e4ff..8d97bd5f 100644 --- a/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/StructuredBufferExtensions.cs @@ -33,4 +33,31 @@ public static class StructuredBufferExtensions { source.CopyTo(destination, sourceOffset, destinationOffset, count); } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, source.Length); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input instance to read data from. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + public static void CopyFrom(this StructuredBuffer destination, UploadBuffer source, int sourceOffset, int destinationOffset, int count) + where T : unmanaged + { + source.CopyTo(destination, sourceOffset, destinationOffset, count); + } } From 29944625e0d0934f84ee111e63459d777f747b60 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 02:03:03 +0100 Subject: [PATCH 07/19] Add new ReadBackBufferExtensions type --- .../Extensions/ReadBackBufferExtensions.cs | 36 +++++++ .../ComputeSharp.Tests/TransferBufferTests.cs | 102 +++++++++++++++--- 2 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs new file mode 100644 index 00000000..ba0f8009 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackBufferExtensions.cs @@ -0,0 +1,36 @@ +using ComputeSharp.Resources; + +namespace ComputeSharp; + +/// +/// A that contains extension methods for the type. +/// +public static class ReadBackBufferExtensions +{ + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, source.Length); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input instance to read data from. + /// The offset to start reading data from. + /// The starting offset within to write data to. + /// The number of items to read. + public static void CopyFrom(this ReadBackBuffer destination, StructuredBuffer source, int sourceOffset, int destinationOffset, int count) + where T : unmanaged + { + source.CopyTo(destination, sourceOffset, destinationOffset, count); + } +} diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs index 47cc9a66..1a933637 100644 --- a/tests/ComputeSharp.Tests/TransferBufferTests.cs +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -94,7 +94,7 @@ public partial class TransferBufferTests [CombinatorialTestMethod] [AllDevices] - public void Allocate_UploadBuffer_Copy_Full(Device device) + public void Allocate_UploadBuffer_CopyTo_Full(Device device) { using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); @@ -113,10 +113,10 @@ public partial class TransferBufferTests [CombinatorialTestMethod] [AllDevices] [Data(0, 0, 4096)] - [Data(128, 0, 2048)] [Data(0, 128, 2048)] - [Data(97, 33, 512)] - public void Allocate_UploadBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count) + [Data(128, 0, 2048)] + [Data(33, 97, 512)] + public void Allocate_UploadBuffer_CopyTo_Range(Device device, int sourceOffset, int destinationOffset, int count) { using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); @@ -124,17 +124,17 @@ public partial class TransferBufferTests using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); - uploadBuffer.CopyTo(readOnlyBuffer, bufferOffset, destinationOffset, count); + uploadBuffer.CopyTo(readOnlyBuffer, sourceOffset, destinationOffset, count); int[] result = readOnlyBuffer.ToArray(destinationOffset, count); Assert.AreEqual(result.Length, count); - Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result)); + Assert.IsTrue(uploadBuffer.Span.Slice(sourceOffset, count).SequenceEqual(result)); } [CombinatorialTestMethod] [AllDevices] - public void Allocate_ReadBackBuffer_Copy_Full(Device device) + public void Allocate_ReadBackBuffer_CopyTo_Full(Device device) { int[] source = new int[4096]; @@ -152,10 +152,10 @@ public partial class TransferBufferTests [CombinatorialTestMethod] [AllDevices] [Data(0, 0, 4096)] - [Data(128, 0, 2048)] [Data(0, 128, 2048)] - [Data(97, 33, 512)] - public void Allocate_ReadBackBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count) + [Data(128, 0, 2048)] + [Data(33, 97, 512)] + public void Allocate_ReadBackBuffer_CopyTo_Range(Device device, int sourceOffset, int destinationOffset, int count) { int[] source = new int[4096]; @@ -164,9 +164,87 @@ public partial class TransferBufferTests using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); - readOnlyBuffer.CopyTo(readBackBuffer, bufferOffset, destinationOffset, count); + readOnlyBuffer.CopyTo(readBackBuffer, sourceOffset, destinationOffset, count); Assert.AreEqual(source.Length, readBackBuffer.Length); - Assert.IsTrue(source.AsSpan(bufferOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count))); + Assert.IsTrue(source.AsSpan(sourceOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count))); + } + + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_UploadBuffer_CopyFrom_Full(Device device) + { + using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); + + new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); + + readOnlyBuffer.CopyFrom(uploadBuffer); + + int[] result = readOnlyBuffer.ToArray(); + + Assert.AreEqual(uploadBuffer.Length, result.Length); + Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 4096)] + [Data(0, 128, 2048)] + [Data(128, 0, 2048)] + [Data(33, 97, 512)] + public void Allocate_UploadBuffer_CopyFrom_Range(Device device, int sourceOffset, int destinationOffset, int count) + { + using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); + + new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); + + readOnlyBuffer.CopyFrom(uploadBuffer, sourceOffset, destinationOffset, count); + + int[] result = readOnlyBuffer.ToArray(destinationOffset, count); + + Assert.AreEqual(result.Length, count); + Assert.IsTrue(uploadBuffer.Span.Slice(sourceOffset, count).SequenceEqual(result)); + } + + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_ReadBackBuffer_CopyFrom_Full(Device device) + { + int[] source = new int[4096]; + + new Random(42).NextBytes(source.AsSpan().AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); + + readBackBuffer.CopyFrom(readOnlyBuffer); + + Assert.AreEqual(source.Length, readBackBuffer.Length); + Assert.IsTrue(source.AsSpan().SequenceEqual(readBackBuffer.Span)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 4096)] + [Data(0, 128, 2048)] + [Data(128, 0, 2048)] + [Data(33, 97, 512)] + public void Allocate_ReadBackBuffer_CopyFrom_Range(Device device, int sourceOffset, int destinationOffset, int count) + { + int[] source = new int[4096]; + + new Random(42).NextBytes(source.AsSpan().AsBytes()); + + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); + + readBackBuffer.CopyFrom(readOnlyBuffer, sourceOffset, destinationOffset, count); + + Assert.AreEqual(source.Length, readBackBuffer.Length); + Assert.IsTrue(source.AsSpan(sourceOffset, count).SequenceEqual(readBackBuffer.Span.Slice(destinationOffset, count))); } } From 9a2d5bdced40be61de5c46647624b26877279975 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 02:38:29 +0100 Subject: [PATCH 08/19] Rename parameters in Texture2D copy APIs --- .../Extensions/Texture2DExtensions.cs | 188 +++++++++--------- .../Extensions/UploadTexture2DExtensions.cs | 16 +- .../Resources/Abstract/Texture2D{T}.cs | 104 +++++----- 3 files changed, 154 insertions(+), 154 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index 330a81d2..440b160e 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -16,15 +16,15 @@ public static class Texture2DExtensions /// Reads the contents of the current instance and returns an array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// A array with the contents of the current texture. [Pure] - public static T[,] ToArray(this Texture2D texture) + public static T[,] ToArray(this Texture2D source) where T : unmanaged { - T[,] data = new T[texture.Height, texture.Width]; + T[,] data = new T[source.Height, source.Width]; - texture.CopyTo(data); + source.CopyTo(data); return data; } @@ -33,102 +33,102 @@ public static class Texture2DExtensions /// Reads the contents of the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - public static void CopyTo(this Texture2D texture, T[,] destination) + public static void CopyTo(this Texture2D source, T[,] destination) where T : unmanaged { - Guard.IsEqualTo(destination.GetLength(0), texture.Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), texture.Width, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(0), source.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), source.Width, nameof(destination)); - texture.CopyTo(ref destination[0, 0], destination.Length, 0, 0, texture.Width, texture.Height); + source.CopyTo(ref destination[0, 0], destination.Length, 0, 0, source.Width, source.Height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - public static void CopyTo(this Texture2D texture, T[] destination) + public static void CopyTo(this Texture2D source, T[] destination) where T : unmanaged { - texture.CopyTo(destination.AsSpan(), 0, 0, texture.Width, texture.Height); + source.CopyTo(destination.AsSpan(), 0, 0, source.Width, source.Height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. /// The horizontal range of items to copy. /// The vertical range of items to copy. - public static void CopyTo(this Texture2D texture, T[] destination, Range x, Range y) + public static void CopyTo(this Texture2D source, T[] destination, Range x, Range y) where T : unmanaged { - texture.CopyTo(destination.AsSpan(), x, y); + source.CopyTo(destination.AsSpan(), x, y); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - public static void CopyTo(this Texture2D texture, T[] destination, int x, int y, int width, int height) + public static void CopyTo(this Texture2D source, T[] destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - texture.CopyTo(destination.AsSpan(), x, y, width, height); + source.CopyTo(destination.AsSpan(), sourceOffsetX, sourceOffsetY, width, height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. - public static void CopyTo(this Texture2D texture, T[] destination, int offset) + /// The starting offset within to write data to. + public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), 0, 0, texture.Width, texture.Height); + source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, source.Width, source.Height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. + /// The starting offset within to write data to. /// The horizontal range of items to copy. /// The vertical range of items to copy. - public static void CopyTo(this Texture2D texture, T[] destination, int offset, Range x, Range y) + public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset, Range x, Range y) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), x, y); + source.CopyTo(destination.AsSpan(destinationOffset), x, y); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The starting offset within to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - public static void CopyTo(this Texture2D texture, T[] destination, int offset, int x, int y, int width, int height) + public static void CopyTo(this Texture2D source, T[] destination, int destinationOffset, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), x, y, width, height); + source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, width, height); } /// @@ -136,162 +136,162 @@ public static class Texture2DExtensions /// The input data will be read from the start of the texture. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - public static void CopyTo(this Texture2D texture, Span destination) + public static void CopyTo(this Texture2D source, Span destination) where T : unmanaged { - texture.CopyTo(destination, 0, 0, texture.Width, texture.Height); + source.CopyTo(destination, 0, 0, source.Width, source.Height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. /// The horizontal range of items to copy. /// The vertical range of items to copy. - public static void CopyTo(this Texture2D texture, Span destination, Range x, Range y) + public static void CopyTo(this Texture2D source, Span destination, Range x, Range y) where T : unmanaged { - var (offsetX, width) = x.GetOffsetAndLength(texture.Width); - var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetX, width) = x.GetOffsetAndLength(source.Width); + var (offsetY, height) = y.GetOffsetAndLength(source.Height); - texture.CopyTo(destination, offsetX, offsetY, width, height); + source.CopyTo(destination, offsetX, offsetY, width, height); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - public static void CopyTo(this Texture2D texture, Span destination, int x, int y, int width, int height) + public static void CopyTo(this Texture2D source, Span destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - texture.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, width, height); + source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, width, height); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this Texture2D texture, ReadBackTexture2D destination) + public static void CopyTo(this Texture2D source, ReadBackTexture2D destination) where T : unmanaged { - texture.CopyTo(destination, 0, 0, 0, 0, texture.Width, texture.Height); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - public static void CopyTo(this Texture2D texture, ReadBackTexture2D destination, int x, int y, int width, int height) + public static void CopyTo(this Texture2D source, ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - texture.CopyTo(destination, 0, 0, x, y, width, height); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } /// /// Writes the contents of a given array to the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - public static unsafe void CopyFrom(this Texture2D texture, T[,] source) + public static unsafe void CopyFrom(this Texture2D destination, T[,] source) where T : unmanaged { - Guard.IsEqualTo(source.GetLength(0), texture.Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), texture.Width, nameof(source)); + Guard.IsEqualTo(source.GetLength(0), destination.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), destination.Width, nameof(source)); - texture.CopyFrom(ref source[0, 0], source.Length, 0, 0, texture.Width, texture.Height); + destination.CopyFrom(ref source[0, 0], source.Length, 0, 0, destination.Width, destination.Height); } /// /// Writes the contents of a given array to the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - public static void CopyFrom(this Texture2D texture, T[] source) + public static void CopyFrom(this Texture2D destination, T[] source) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), 0, 0, texture.Width, texture.Height); + destination.CopyFrom(source.AsSpan(), 0, 0, destination.Width, destination.Height); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. - public static void CopyFrom(this Texture2D texture, T[] source, Range x, Range y) + public static void CopyFrom(this Texture2D destination, T[] source, Range x, Range y) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), x, y); + destination.CopyFrom(source.AsSpan(), x, y); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - public static void CopyFrom(this Texture2D texture, T[] source, int x, int y, int width, int height) + public static void CopyFrom(this Texture2D destination, T[] source, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), x, y, width, height); + destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, width, height); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The starting offset within to read data from. + /// The starting offset within to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. - public static void CopyFrom(this Texture2D texture, T[] source, int offset, Range x, Range y) + public static void CopyFrom(this Texture2D destination, T[] source, int sourceOffset, Range x, Range y) where T : unmanaged { - texture.CopyFrom(source.AsSpan(offset), x, y); + destination.CopyFrom(source.AsSpan(sourceOffset), x, y); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. + /// The starting offset within to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - public static void CopyFrom(this Texture2D texture, T[] source, int offset, int x, int y, int width, int height) + public static void CopyFrom(this Texture2D destination, T[] source, int sourceOffset, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - texture.CopyFrom(source.AsSpan(offset), x, y, width, height); + destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, width, height); } /// @@ -299,45 +299,45 @@ public static class Texture2DExtensions /// The input data will be written to the start of the texture, and all input items will be copied. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - public static void CopyFrom(this Texture2D texture, ReadOnlySpan source) + public static void CopyFrom(this Texture2D destination, ReadOnlySpan source) where T : unmanaged { - texture.CopyFrom(source, 0, 0, texture.Width, texture.Height); + destination.CopyFrom(source, 0, 0, destination.Width, destination.Height); } /// /// Writes the contents of a given to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. - public static void CopyFrom(this Texture2D texture, ReadOnlySpan source, Range x, Range y) + public static void CopyFrom(this Texture2D destination, ReadOnlySpan source, Range x, Range y) where T : unmanaged { - var (offsetX, width) = x.GetOffsetAndLength(texture.Width); - var (offsetY, height) = y.GetOffsetAndLength(texture.Height); + var (offsetX, width) = x.GetOffsetAndLength(destination.Width); + var (offsetY, height) = y.GetOffsetAndLength(destination.Height); - texture.CopyFrom(source, offsetX, offsetY, width, height); + destination.CopyFrom(source, offsetX, offsetY, width, height); } /// /// Writes the contents of a given to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - public static void CopyFrom(this Texture2D texture, ReadOnlySpan source, int x, int y, int width, int height) + public static void CopyFrom(this Texture2D destination, ReadOnlySpan source, int destinationOffsetX, int destinationOffsetY, int width, int height) where T : unmanaged { - texture.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, x, y, width, height); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, width, height); } /// diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs index ffb6b4d6..96599509 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs @@ -11,27 +11,27 @@ public static class UploadTexture2DExtensions /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this UploadTexture2D texture, Texture2D destination) + public static void CopyTo(this UploadTexture2D source, Texture2D destination) where T : unmanaged { - destination.CopyFrom(texture, 0, 0, 0, 0, texture.Width, texture.Height); + destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - public static void CopyTo(this UploadTexture2D texture, Texture2D destination, int x, int y, int width, int height) + public static void CopyTo(this UploadTexture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) where T : unmanaged { - destination.CopyFrom(texture, x, y, 0, 0, width, height); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index f6935282..b83a618c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -147,22 +147,22 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// The target memory area to write data to. /// The size of the target memory area to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. - internal void CopyTo(ref T destination, int size, int x, int y, int width, int height) + internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - Guard.IsInRange(x, 0, Width, nameof(x)); - Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); - Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); - Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( @@ -195,8 +195,8 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper destinationY: 0, destinationZ: 0, d3D12ResourceSource: D3D12Resource, - sourceX: (uint)x, - sourceY: (uint)y, + sourceX: (uint)sourceOffsetX, + sourceY: (uint)sourceOffsetY, sourceZ: 0, (uint)width, (uint)height, @@ -227,13 +227,13 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// /// The target instance to write data to. - /// The horizontal offset within . - /// The vertical offset within . - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset within . + /// The vertical offset within . /// The width of the memory area to copy. /// The height of the memory area to copy. - internal void CopyTo(ReadBackTexture2D destination, int destinationX, int destinationY, int sourceX, int sourceY, int width, int height) + internal void CopyTo(ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -242,18 +242,18 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper destination.ThrowIfDeviceMismatch(GraphicsDevice); destination.ThrowIfDisposed(); - Guard.IsInRange(destinationX, 0, destination.Width, nameof(destinationX)); - Guard.IsInRange(destinationY, 0, destination.Height, nameof(destinationY)); - Guard.IsInRange(sourceX, 0, Width, nameof(sourceX)); - Guard.IsInRange(sourceY, 0, Height, nameof(sourceY)); + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(destinationOffsetX, 0, destination.Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, destination.Height, nameof(destinationOffsetY)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); - Guard.IsBetweenOrEqualTo(destinationX + width, 1, destination.Width, nameof(destinationX)); - Guard.IsBetweenOrEqualTo(destinationY + height, 1, destination.Height, nameof(destinationY)); - Guard.IsLessThanOrEqualTo(sourceX + width, Width, nameof(sourceX)); - Guard.IsLessThanOrEqualTo(sourceY + height, Height, nameof(sourceY)); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -267,12 +267,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: destination.D3D12Resource, d3D12PlacedSubresourceFootprintDestination, - (uint)destinationX, - (uint)destinationY, + (uint)destinationOffsetX, + (uint)destinationOffsetY, destinationZ: 0, d3D12ResourceSource: D3D12Resource, - (uint)sourceX, - (uint)sourceY, + (uint)sourceOffsetX, + (uint)sourceOffsetY, sourceZ: 0, (uint)width, (uint)height, @@ -292,22 +292,22 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// The input memory area to read data from. /// The size of the memory area to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - internal void CopyFrom(ref T source, int size, int x, int y, int width, int height) + internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - Guard.IsInRange(x, 0, Width, nameof(x)); - Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(destinationOffsetX, 0, Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, Height, nameof(destinationOffsetY)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); - Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); - Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( @@ -346,8 +346,8 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, - destinationX: (uint)x, - destinationY: (uint)y, + destinationX: (uint)destinationOffsetX, + destinationY: (uint)destinationOffsetY, destinationZ: 0, d3D12ResourceSource: d3D12Resource.Get(), &d3D12PlacedSubresourceFootprintSource, @@ -370,13 +370,13 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// Writes the contents of a given instance to a specified area of the current instance. /// /// The input instance to read data from. - /// The horizontal offset within . - /// The vertical offset within . - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. + /// The horizontal offset within . + /// The vertical offset within . + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - internal void CopyFrom(UploadTexture2D source, int sourceX, int sourceY, int destinationX, int destinationY, int width, int height) + internal void CopyFrom(UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) { GraphicsDevice.ThrowIfDisposed(); @@ -385,18 +385,18 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); - Guard.IsInRange(sourceX, 0, source.Width, nameof(sourceX)); - Guard.IsInRange(sourceY, 0, source.Height, nameof(sourceY)); - Guard.IsInRange(destinationX, 0, Width, nameof(destinationX)); - Guard.IsInRange(destinationY, 0, Height, nameof(destinationY)); + Guard.IsInRange(sourceOffsetX, 0, source.Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, source.Height, nameof(sourceOffsetY)); + Guard.IsInRange(destinationOffsetX, 0, Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, Height, nameof(destinationOffsetY)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(width, 1, source.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, source.Height, nameof(height)); - Guard.IsLessThanOrEqualTo(sourceX + width, source.Width, nameof(sourceX)); - Guard.IsLessThanOrEqualTo(sourceY + height, source.Height, nameof(sourceY)); - Guard.IsLessThanOrEqualTo(destinationX + width, Width, nameof(destinationX)); - Guard.IsLessThanOrEqualTo(destinationY + height, Height, nameof(destinationY)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -409,13 +409,13 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper { copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, - (uint)destinationX, - (uint)destinationY, + (uint)destinationOffsetX, + (uint)destinationOffsetY, destinationZ: 0, d3D12ResourceSource: source.D3D12Resource, d3D12PlacedSubresourceFootprintSource, - (uint)sourceX, - (uint)sourceY, + (uint)sourceOffsetX, + (uint)sourceOffsetY, sourceZ: 0, (uint)width, (uint)height, From 2abd19bd22243299f6623da3a2a7067181f2241b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 02:44:48 +0100 Subject: [PATCH 09/19] Add mirrored Texture2D extensions for transfers --- .../Extensions/Texture2DExtensions.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index 440b160e..db24237f 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -205,6 +205,24 @@ public static class Texture2DExtensions source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyTo(this Texture2D source, ReadBackTexture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } + /// /// Writes the contents of a given array to the current instance. /// @@ -340,6 +358,52 @@ public static class Texture2DExtensions destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, width, height); } + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this Texture2D destination, UploadTexture2D source) + where T : unmanaged + { + destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this Texture2D destination, UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this Texture2D destination, UploadTexture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } + /// /// Saves a texture to a specified file. /// From 16c067ed3492101d88c62b9fd9ad7176383880cc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 02:48:15 +0100 Subject: [PATCH 10/19] Add missing transfer 2D texture copy APIs --- .../Extensions/ReadBackTexture2DExtensions.cs | 47 +++++++++++++++++++ .../Extensions/UploadTexture2DExtensions.cs | 18 +++++++ 2 files changed, 65 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs index 8f08ebb9..8d855bfb 100644 --- a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture2DExtensions.cs @@ -1,5 +1,6 @@ using System; using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Resources; namespace ComputeSharp; @@ -8,6 +9,52 @@ namespace ComputeSharp; /// public static class ReadBackTexture2DExtensions { + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this ReadBackTexture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } + /// /// Saves a texture to a specified file. /// diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs index 96599509..675aab5d 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture2DExtensions.cs @@ -34,4 +34,22 @@ public static class UploadTexture2DExtensions { destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, 0, 0, width, height); } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyTo(this UploadTexture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } } From aef6630709e7e1aef8819885c32f4c83408f4324 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 13:07:35 +0100 Subject: [PATCH 11/19] Add new APIs to copy between Texture2D-s --- .../ID3D12GraphicsCommandListExtensions.cs | 67 +++++- .../Extensions/Texture2DExtensions.cs | 94 +++++++- .../Resources/Abstract/Texture2D{T}.cs | 68 ++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 222 ++++++++++++++++++ 4 files changed, 448 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 0027ade5..25f7a512 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -45,7 +45,9 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions D3D12_BOX d3D12Box; D3D12_BOX* pD3D12Box; - if (sourceX == 0 && sourceY == 0 && sourceZ == 0 && + if (sourceX == 0 && + sourceY == 0 && + sourceZ == 0 && width == d3D12ResourceDescriptionSource.Width && height == d3D12ResourceDescriptionSource.Height && depth == d3D12ResourceDescriptionSource.Depth) @@ -103,7 +105,9 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions D3D12_BOX d3D12Box; D3D12_BOX* pD3D12Box; - if (sourceX == 0 && sourceY == 0 && sourceZ == 0 && + if (sourceX == 0 && + sourceY == 0 && + sourceZ == 0 && width == d3D12PlacedSubresourceFootprintSource->Footprint.Width && height == d3D12PlacedSubresourceFootprintSource->Footprint.Height && depth == d3D12PlacedSubresourceFootprintSource->Footprint.Depth) @@ -125,6 +129,65 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions pD3D12Box); } + /// + /// Copies a texture memory region from one texture to another. + /// + /// The instance in use. + /// The destination (a buffer) to write to. + /// The horizontal offset in the destination buffer. + /// The vertical offset in the destination buffer. + /// The depthwise offset in the destination buffer. + /// The source (a texture) to read from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to read from. + /// The height of the memory area to read from. + /// The depth of the memory area to read from. + public static void CopyTextureRegion( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12ResourceDestination, + uint destinationX, + uint destinationY, + ushort destinationZ, + ID3D12Resource* d3D12ResourceSource, + uint sourceX, + uint sourceY, + ushort sourceZ, + uint width, + uint height, + ushort depth) + { + D3D12_RESOURCE_DESC d3D12ResourceDescriptionSource = d3D12ResourceSource->GetDesc(); + D3D12_TEXTURE_COPY_LOCATION d3D12TextureCopyLocationDestination = new(d3D12ResourceDestination, 0); + D3D12_TEXTURE_COPY_LOCATION d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); + D3D12_BOX d3D12Box; + D3D12_BOX* pD3D12Box; + + if (sourceX == 0 && + sourceY == 0 && + sourceZ == 0 && + width == d3D12ResourceDescriptionSource.Width && + height == d3D12ResourceDescriptionSource.Height && + depth == d3D12ResourceDescriptionSource.Depth) + { + pD3D12Box = null; + } + else + { + d3D12Box = new D3D12_BOX((int)sourceX, (int)sourceY, sourceZ, (int)(sourceX + width), (int)(sourceY + height), sourceZ + depth); + pD3D12Box = &d3D12Box; + } + + d3D12GraphicsCommandList.CopyTextureRegion( + &d3D12TextureCopyLocationDestination, + destinationX, + destinationY, + destinationZ, + &d3D12TextureCopyLocationSource, + pD3D12Box); + } + /// /// Creates a resource barrier to transition a resource to a specific state. /// diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index db24237f..d0a12488 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -177,6 +177,52 @@ public static class Texture2DExtensions source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, width, height); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + public static void CopyTo(this Texture2D source, Texture2D destination) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyTo(this Texture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyTo(this Texture2D source, Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } + /// /// Reads the contents of a instance and writes them into a target instance. /// @@ -358,6 +404,52 @@ public static class Texture2DExtensions destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, width, height); } + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this Texture2D destination, Texture2D source) + where T : unmanaged + { + destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this Texture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, 0, 0, width, height); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + public static void CopyFrom(this Texture2D destination, Texture2D source, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, width, height); + } + /// /// Reads the contents of an instance and writes them into a target instance. /// @@ -367,7 +459,7 @@ public static class Texture2DExtensions public static void CopyFrom(this Texture2D destination, UploadTexture2D source) where T : unmanaged { - destination.CopyFrom(source, 0, 0, 0, 0, source.Width, source.Height); + source.CopyTo(destination, 0, 0, 0, 0, source.Width, source.Height); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index b83a618c..4c2f723e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -223,6 +223,74 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } + /// + /// Reads the contents of the specified range from the current instance and writes them into a target instance. + /// + /// The target instance to write data to. + /// The horizontal offset within . + /// The vertical offset within . + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + internal void CopyTo(Texture2D destination, int sourceOffsetX, int sourceOffsetY, int destinationOffsetX, int destinationOffsetY, int width, int height) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(destinationOffsetX, 0, destination.Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, destination.Height, nameof(destinationOffsetY)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + + 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->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: destination.D3D12Resource, + (uint)destinationOffsetX, + (uint)destinationOffsetY, + destinationZ: 0, + d3D12ResourceSource: D3D12Resource, + (uint)sourceOffsetX, + (uint)sourceOffsetY, + sourceZ: 0, + (uint)width, + (uint)height, + 1); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index a89f3ce4..04c018aa 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -165,6 +165,228 @@ public partial class Texture2DTests texture.CopyTo(result, x, y, width, height); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [AdditionalResource(typeof(ReadOnlyTexture2D<>))] + [AdditionalResource(typeof(ReadWriteTexture2D<>))] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 1024, 1024)] + [Data(1024, 1024, 1024, 1024, 127, 256, 0, 0, 512, 480)] + [Data(1024, 1024, 1024, 1024, 0, 0, 127, 256, 512, 480)] + [Data(1024, 1024, 1024, 1024, 122, 329, 127, 256, 512, 480)] + [Data(512, 480, 1024, 768, 122, 29, 127, 256, 256, 320)] + [Data(512, 480, 256, 256, 122, 120, 0, 0, 256, 256)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 512, 0, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 512, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 512, 512, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 256, 256, 512, 512)] + public void CopyTo_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int destinationWidth, + int destinationHeight, + int sourceOffsetX, + int sourceOffsetY, + int destinationOffsetX, + int destinationOffsetY, + int copyWidth, + int copyHeight) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth).Select(static i => (float)i).ToArray(); + + using Texture2D source = device.Get().AllocateTexture2D(sourceType, array, sourceWidth, sourceHeight); + using Texture2D destination = device.Get().AllocateTexture2D(destinationType, destinationWidth, destinationHeight, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, copyWidth, copyHeight); + + ReadOnlySpan2D expected = new(array, sourceHeight, sourceWidth); + ReadOnlySpan2D result = destination.ToArray(); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [AdditionalResource(typeof(ReadOnlyTexture2D<>))] + [AdditionalResource(typeof(ReadWriteTexture2D<>))] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 2055, 1024)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 1024, 3920)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, -1, 1024)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 512, -2)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 512, int.MaxValue)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 768, 768)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 256, 768)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 620, 256)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 768, 768)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 256, 768)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 620, 256)] + [Data(512, 512, 512, 512, 450, 0, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 450, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 450, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 0, 450, 128, 128)] + [Data(512, 512, 512, 512, -1, 0, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, -1, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, -1, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 0, -1, 128, 128)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyTo_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int destinationWidth, + int destinationHeight, + int sourceOffsetX, + int sourceOffsetY, + int destinationOffsetX, + int destinationOffsetY, + int copyWidth, + int copyHeight) + { + using Texture2D source = device.Get().AllocateTexture2D(sourceType, sourceWidth, sourceHeight); + using Texture2D destination = device.Get().AllocateTexture2D(destinationType, destinationWidth, destinationHeight, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, copyWidth, copyHeight); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [AdditionalResource(typeof(ReadOnlyTexture2D<>))] + [AdditionalResource(typeof(ReadWriteTexture2D<>))] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 1024, 1024)] + [Data(1024, 1024, 1024, 1024, 127, 256, 0, 0, 512, 480)] + [Data(1024, 1024, 1024, 1024, 0, 0, 127, 256, 512, 480)] + [Data(1024, 1024, 1024, 1024, 122, 329, 127, 256, 512, 480)] + [Data(512, 480, 1024, 768, 122, 29, 127, 256, 256, 320)] + [Data(512, 480, 256, 256, 122, 120, 0, 0, 256, 256)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 512, 0, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 512, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 512, 512, 512, 512)] + [Data(512, 512, 1024, 1024, 0, 0, 256, 256, 512, 512)] + public void CopyFrom_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int destinationWidth, + int destinationHeight, + int sourceOffsetX, + int sourceOffsetY, + int destinationOffsetX, + int destinationOffsetY, + int copyWidth, + int copyHeight) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth).Select(static i => (float)i).ToArray(); + + using Texture2D source = device.Get().AllocateTexture2D(sourceType, array, sourceWidth, sourceHeight); + using Texture2D destination = device.Get().AllocateTexture2D(destinationType, destinationWidth, destinationHeight, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, copyWidth, copyHeight); + + ReadOnlySpan2D expected = new(array, sourceHeight, sourceWidth); + ReadOnlySpan2D result = destination.ToArray(); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [AdditionalResource(typeof(ReadOnlyTexture2D<>))] + [AdditionalResource(typeof(ReadWriteTexture2D<>))] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 2055, 1024)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 1024, 3920)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, -1, 1024)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 512, -2)] + [Data(1024, 1024, 1024, 1024, 0, 0, 0, 0, 512, int.MaxValue)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 768, 768)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 256, 768)] + [Data(512, 512, 1024, 1024, 0, 0, 0, 0, 620, 256)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 768, 768)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 256, 768)] + [Data(1024, 1024, 512, 512, 0, 0, 0, 0, 620, 256)] + [Data(512, 512, 512, 512, 450, 0, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 450, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 450, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 0, 450, 128, 128)] + [Data(512, 512, 512, 512, -1, 0, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, -1, 0, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, -1, 0, 128, 128)] + [Data(512, 512, 512, 512, 0, 0, 0, -1, 128, 128)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyFrom_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int destinationWidth, + int destinationHeight, + int sourceOffsetX, + int sourceOffsetY, + int destinationOffsetX, + int destinationOffsetY, + int copyWidth, + int copyHeight) + { + using Texture2D source = device.Get().AllocateTexture2D(sourceType, sourceWidth, sourceHeight); + using Texture2D destination = device.Get().AllocateTexture2D(destinationType, destinationWidth, destinationHeight, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, destinationOffsetX, destinationOffsetY, copyWidth, copyHeight); + } + [CombinatorialTestMethod] [AllDevices] public void Dispatch_ReadOnlyTexture2D(Device device) From d657b9cc9596d0acd78dbc8a6faba255c7890c73 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 13:27:35 +0100 Subject: [PATCH 12/19] Add Texture2D.ToArray overload --- .../Extensions/Texture2DExtensions.cs | 27 ++++++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 50 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs index d0a12488..fe671afa 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs @@ -29,6 +29,33 @@ public static class Texture2DExtensions return data; } + /// + /// Reads the contents of the current instance and returns an array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// A array with the contents of the current texture. + [Pure] + public static unsafe T[,] ToArray(this Texture2D source, int x, int y, int width, int height) + where T : unmanaged + { + Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); + Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + + T[,] data = new T[height, width]; + + fixed (T* p = data) + { + source.CopyTo(new Span(p, width * height), x, y, width, height); + } + + return data; + } + /// /// Reads the contents of the current instance and writes them into a target array. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index 04c018aa..fb2934d3 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -112,6 +112,56 @@ public partial class Texture2DTests texture.Dispose(); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(0, 0, 64, 64)] + [Data(0, 14, 64, 50)] + [Data(14, 0, 50, 64)] + [Data(10, 10, 54, 54)] + [Data(20, 20, 32, 27)] + [Data(60, 0, 4, 64)] + [Data(0, 60, 64, 4)] + [Data(63, 2, 1, 60)] + [Data(2, 63, 60, 1)] + public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int y, int width, int height) + { + float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); + + using Texture2D texture = device.Get().AllocateTexture2D(textureType, array, 64, 64); + + float[,] result = texture.ToArray(x, y, width, height); + + Assert.AreEqual(height, result.GetLength(0)); + Assert.AreEqual(width, result.GetLength(1)); + + Span2D expected = new Span2D(array, 64, 64).Slice(y, x, height, width); + Span2D data = new(result); + + CollectionAssert.AreEqual(expected.ToArray(), data.ToArray()); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(0, -1, 50, 50)] + [Data(-1, 0, 50, 50)] + [Data(12, 0, -1, 50)] + [Data(12, 0, 20, -1)] + [Data(12, 20, 20, 50)] + [Data(12, 20, 60, 20)] + [Data(80, 20, 40, 20)] + [Data(0, 80, 40, 20)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int y, int width, int height) + { + using Texture2D texture = device.Get().AllocateTexture2D(textureType, 64, 64); + + _ = texture.ToArray(x, y, width, height); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture2D<>))] From 453eec3366b6aae8422ca8e90657c23efa37c6dd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 14:18:18 +0100 Subject: [PATCH 13/19] Rename parameters in Texture3D copy APIs --- .../Extensions/Texture3DExtensions.cs | 186 +++++++++--------- .../Resources/Abstract/Texture3D{T}.cs | 152 +++++++------- 2 files changed, 169 insertions(+), 169 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index 436b1be1..23d56dda 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -15,7 +15,7 @@ public static class Texture3DExtensions /// Reads the contents of the current instance and returns an array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// A array with the contents of the current buffer. /// /// The returned array will be using the same memory layout as the texture, that is, each 2D plane @@ -23,12 +23,12 @@ public static class Texture3DExtensions /// the depth dimension. This means that the resulting 3D array will have a size of [D, H, W]. /// [Pure] - public static T[,,] ToArray(this Texture3D texture) + public static T[,,] ToArray(this Texture3D source) where T : unmanaged { - T[,,] data = new T[texture.Depth, texture.Height, texture.Width]; + T[,,] data = new T[source.Depth, source.Height, source.Width]; - texture.CopyTo(data); + source.CopyTo(data); return data; } @@ -37,247 +37,247 @@ public static class Texture3DExtensions /// Reads the contents of the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. /// /// The input 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected /// layout of the input array has to be of shape [depth, height, width]. /// - public static void CopyTo(this Texture3D texture, T[,,] destination) + public static void CopyTo(this Texture3D source, T[,,] destination) where T : unmanaged { - Guard.IsEqualTo(destination.GetLength(0), texture.Depth, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(1), texture.Height, nameof(destination)); - Guard.IsEqualTo(destination.GetLength(2), texture.Width, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(0), source.Depth, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(1), source.Height, nameof(destination)); + Guard.IsEqualTo(destination.GetLength(2), source.Width, nameof(destination)); - texture.CopyTo(ref destination[0, 0, 0], destination.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + source.CopyTo(ref destination[0, 0, 0], destination.Length, 0, 0, 0, source.Width, source.Height, source.Depth); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. - public static void CopyTo(this Texture3D texture, T[] destination, int offset) + /// The starting offset within to write data to. + public static void CopyTo(this Texture3D source, T[] destination, int destinationOffset) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + source.CopyTo(destination.AsSpan(destinationOffset), 0, 0, 0, source.Width, source.Height, source.Depth); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. + /// The starting offset within to write data to. /// The horizontal range of items to copy. /// The vertical range of items to copy. /// The depthwise range of items to copy. - public static void CopyTo(this Texture3D texture, T[] destination, int offset, Range x, Range y, Range z) + public static void CopyTo(this Texture3D source, T[] destination, int destinationOffset, Range x, Range y, Range z) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), x, y, z); + source.CopyTo(destination.AsSpan(destinationOffset), x, y, z); } /// /// Reads the contents of the specified range from the current instance and writes them into a target array. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input array to write data to. - /// The starting offset within to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The starting offset within to write data to. /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - public static void CopyTo(this Texture3D texture, T[] destination, int offset, int x, int y, int z, int width, int height, int depth) + public static void CopyTo(this Texture3D source, T[] destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffset, int width, int height, int depth) where T : unmanaged { - texture.CopyTo(destination.AsSpan(offset), x, y, z, width, height, depth); + source.CopyTo(destination.AsSpan(destinationOffset), sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - public static void CopyTo(this Texture3D texture, Span destination) + public static void CopyTo(this Texture3D source, Span destination) where T : unmanaged { - texture.CopyTo(destination, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + source.CopyTo(destination, 0, 0, 0, source.Width, source.Height, source.Depth); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. /// The horizontal range in the source texture. /// The vertical range in the source texture. /// The depthwise range in the source texture. - public static void CopyTo(this Texture3D texture, Span destination, Range x, Range y, Range z) + public static void CopyTo(this Texture3D source, Span destination, Range x, Range y, Range z) where T : unmanaged { - var (offsetX, width) = x.GetOffsetAndLength(texture.Width); - var (offsetY, height) = y.GetOffsetAndLength(texture.Height); - var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + var (offsetX, width) = x.GetOffsetAndLength(source.Width); + var (offsetY, height) = y.GetOffsetAndLength(source.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(source.Depth); - texture.CopyTo(destination, offsetX, offsetY, offsetZ, width, height, depth); + source.CopyTo(destination, offsetX, offsetY, offsetZ, width, height, depth); } /// /// Reads the contents of the specified range from the current instance and writes them into a target . /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The input to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - public static void CopyTo(this Texture3D texture, Span destination, int x, int y, int z, int width, int height, int depth) + public static void CopyTo(this Texture3D source, Span destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - texture.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, x, y, z, width, height, depth); + source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this Texture3D texture, ReadBackTexture3D destination) + public static void CopyTo(this Texture3D source, ReadBackTexture3D destination) where T : unmanaged { - texture.CopyTo(destination, 0, 0, 0, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - public static void CopyTo(this Texture3D texture, ReadBackTexture3D destination, int x, int y, int z, int width, int height, int depth) + public static void CopyTo(this Texture3D source, ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - texture.CopyTo(destination, 0, 0, 0, x, y, z, width, height, depth); + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } /// /// Writes the contents of a given array to the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. /// /// The source 3D array needs to have each 2D plane stacked on the depth axis. That is, the expected /// layout of the input array has to be of shape [depth, height, width]. /// - public static void CopyFrom(this Texture3D texture, T[,,] source) + public static void CopyFrom(this Texture3D destination, T[,,] source) where T : unmanaged { - Guard.IsEqualTo(source.GetLength(0), texture.Depth, nameof(source)); - Guard.IsEqualTo(source.GetLength(1), texture.Height, nameof(source)); - Guard.IsEqualTo(source.GetLength(2), texture.Width, nameof(source)); + Guard.IsEqualTo(source.GetLength(0), destination.Depth, nameof(source)); + Guard.IsEqualTo(source.GetLength(1), destination.Height, nameof(source)); + Guard.IsEqualTo(source.GetLength(2), destination.Width, nameof(source)); - texture.CopyFrom(ref source[0, 0, 0], source.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + destination.CopyFrom(ref source[0, 0, 0], source.Length, 0, 0, 0, destination.Width, destination.Height, destination.Depth); } /// /// Writes the contents of a given array to the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - public static void CopyFrom(this Texture3D texture, T[] source) + public static void CopyFrom(this Texture3D destination, T[] source) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), 0, 0, 0, texture.Width, texture.Height, texture.Depth); + destination.CopyFrom(source.AsSpan(), 0, 0, 0, destination.Width, destination.Height, destination.Depth); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. /// The depthwise range of items to write. - public static void CopyFrom(this Texture3D texture, T[] source, Range x, Range y, Range z) + public static void CopyFrom(this Texture3D destination, T[] source, Range x, Range y, Range z) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), x, y, z); + destination.CopyFrom(source.AsSpan(), x, y, z); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. - public static void CopyFrom(this Texture3D texture, T[] source, int x, int y, int z, int width, int height, int depth) + public static void CopyFrom(this Texture3D destination, T[] source, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - texture.CopyFrom(source.AsSpan(), x, y, z, width, height, depth); + destination.CopyFrom(source.AsSpan(), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The starting offset within to read data from. + /// The starting offset within to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. /// The depthwise range of items to write. - public static void CopyFrom(this Texture3D texture, T[] source, int offset, Range x, Range y, Range z) + public static void CopyFrom(this Texture3D destination, T[] source, int sourceOffset, Range x, Range y, Range z) where T : unmanaged { - texture.CopyFrom(source.AsSpan(offset), x, y, z); + destination.CopyFrom(source.AsSpan(sourceOffset), x, y, z); } /// /// Writes the contents of a given array to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input array to read data from. - /// The starting offset within to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. + /// The starting offset within to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. - public static void CopyFrom(this Texture3D texture, T[] source, int offset, int x, int y, int z, int width, int height, int depth) + public static void CopyFrom(this Texture3D destination, T[] source, int sourceOffset, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - texture.CopyFrom(source.AsSpan(offset), x, y, z, width, height, depth); + destination.CopyFrom(source.AsSpan(sourceOffset), destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } /// @@ -285,48 +285,48 @@ public static class Texture3DExtensions /// The input data will be written to the start of the texture, and all input items will be copied. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - public static void CopyFrom(this Texture3D texture, ReadOnlySpan source) + public static void CopyFrom(this Texture3D destination, ReadOnlySpan source) where T : unmanaged { - texture.CopyFrom(source, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + destination.CopyFrom(source, 0, 0, 0, destination.Width, destination.Height, destination.Depth); } /// /// Writes the contents of a given to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. /// The horizontal range of items to write. /// The vertical range of items to write. /// The depthwise range of items to write. - public static void CopyFrom(this Texture3D texture, ReadOnlySpan source, Range x, Range y, Range z) + public static void CopyFrom(this Texture3D destination, ReadOnlySpan source, Range x, Range y, Range z) where T : unmanaged { - var (offsetX, width) = x.GetOffsetAndLength(texture.Width); - var (offsetY, height) = y.GetOffsetAndLength(texture.Height); - var (offsetZ, depth) = z.GetOffsetAndLength(texture.Depth); + var (offsetX, width) = x.GetOffsetAndLength(destination.Width); + var (offsetY, height) = y.GetOffsetAndLength(destination.Height); + var (offsetZ, depth) = z.GetOffsetAndLength(destination.Depth); - texture.CopyFrom(source, offsetX, offsetY, offsetZ, width, height, depth); + destination.CopyFrom(source, offsetX, offsetY, offsetZ, width, height, depth); } /// /// Writes the contents of a given to a specified area of the current instance. /// /// The type of items stored on the texture. - /// The target instance to write data to. + /// The target instance to write data to. /// The input to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offseet in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offseet in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. - public static void CopyFrom(this Texture3D texture, ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + public static void CopyFrom(this Texture3D destination, ReadOnlySpan source, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) where T : unmanaged { - texture.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, x, y, z, width, height, depth); + destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 263a15a1..6e569b89 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -156,27 +156,27 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// The target memory area to write data to. /// The size of the memory area to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - internal void CopyTo(ref T destination, int size, int x, int y, int z, int width, int height, int depth) + internal void CopyTo(ref T destination, int size, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - Guard.IsInRange(x, 0, Width, nameof(x)); - Guard.IsInRange(y, 0, Height, nameof(y)); - Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(sourceOffsetZ, 0, Depth, nameof(sourceOffsetZ)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); - Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); - Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( @@ -210,9 +210,9 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper destinationY: 0, destinationZ: 0, d3D12ResourceSource: D3D12Resource, - sourceX: (uint)x, - sourceY: (uint)y, - sourceZ: (ushort)z, + sourceX: (uint)sourceOffsetX, + sourceY: (uint)sourceOffsetY, + sourceZ: (ushort)sourceOffsetZ, (uint)width, (uint)height, (ushort)depth); @@ -244,16 +244,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// Reads the contents of the specified range from the current instance and writes them into a instance. /// /// The target instance to write data to. - /// The horizontal offset within . - /// The vertical offset within . - /// The depthwise offset within . - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset within . + /// The vertical offset within . + /// The depthwise offset within . /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - internal void CopyTo(ReadBackTexture3D destination, int destinationX, int destinationY, int destinationZ, int sourceX, int sourceY, int sourceZ, int width, int height, int depth) + internal void CopyTo(ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -262,24 +262,24 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper destination.ThrowIfDeviceMismatch(GraphicsDevice); destination.ThrowIfDisposed(); - Guard.IsInRange(destinationX, 0, destination.Width, nameof(destinationX)); - Guard.IsInRange(destinationY, 0, destination.Height, nameof(destinationY)); - Guard.IsInRange(destinationZ, 0, destination.Depth, nameof(destinationZ)); - Guard.IsInRange(sourceX, 0, Width, nameof(sourceX)); - Guard.IsInRange(sourceY, 0, Height, nameof(sourceY)); - Guard.IsInRange(sourceZ, 0, Depth, nameof(sourceZ)); + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(sourceOffsetZ, 0, Depth, nameof(sourceOffsetZ)); + Guard.IsInRange(destinationOffsetX, 0, destination.Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, destination.Height, nameof(destinationOffsetY)); + Guard.IsInRange(destinationOffsetZ, 0, destination.Depth, nameof(destinationOffsetZ)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth, nameof(depth)); - Guard.IsBetweenOrEqualTo(destinationX + width, 1, destination.Width, nameof(destinationX)); - Guard.IsBetweenOrEqualTo(destinationY + height, 1, destination.Height, nameof(destinationY)); - Guard.IsBetweenOrEqualTo(destinationZ + depth, 1, destination.Depth, nameof(destinationZ)); - Guard.IsLessThanOrEqualTo(sourceX + width, Width, nameof(sourceX)); - Guard.IsLessThanOrEqualTo(sourceY + height, Height, nameof(sourceY)); - Guard.IsLessThanOrEqualTo(sourceZ + depth, Depth, nameof(sourceZ)); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -293,13 +293,13 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: destination.D3D12Resource, d3D12PlacedSubresourceFootprintDestination, - (uint)destinationX, - (uint)destinationY, - (ushort)destinationZ, + (uint)destinationOffsetX, + (uint)destinationOffsetY, + (ushort)destinationOffsetZ, d3D12ResourceSource: D3D12Resource, - (uint)sourceX, - (uint)sourceY, - (ushort)sourceZ, + (uint)sourceOffsetX, + (uint)sourceOffsetY, + (ushort)sourceOffsetZ, (uint)width, (uint)height, (ushort)depth); @@ -318,27 +318,27 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// The input memory area to read data from. /// The size of the memory area to read data from. - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. - internal void CopyFrom(ref T source, int size, int x, int y, int z, int width, int height, int depth) + internal void CopyFrom(ref T source, int size, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); ThrowIfDisposed(); - Guard.IsInRange(x, 0, Width, nameof(x)); - Guard.IsInRange(y, 0, Height, nameof(y)); - Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsInRange(destinationOffsetX, 0, Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, Height, nameof(destinationOffsetY)); + Guard.IsInRange(destinationOffsetZ, 0, Depth, nameof(destinationOffsetZ)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); - Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); - Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); - Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth, nameof(size)); GraphicsDevice.D3D12Device->GetCopyableFootprint( @@ -380,9 +380,9 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, - destinationX: (uint)x, - destinationY: (uint)y, - destinationZ: (ushort)z, + destinationX: (uint)destinationOffsetX, + destinationY: (uint)destinationOffsetY, + destinationZ: (ushort)destinationOffsetZ, d3D12ResourceSource: d3D12Resource.Get(), &d3D12PlacedSubresourceFootprintSource, sourceX: 0, @@ -404,16 +404,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// Writes the contents of a given instance to a specified area of the current instance. /// /// The input instance to read data from. - /// The horizontal offset within . - /// The vertical offset within . - /// The depthwise offset within . - /// The horizontal offset in the destination texture. - /// The vertical offset in the destination texture. - /// The depthwise offset in the destination texture. + /// The horizontal offset within . + /// The vertical offset within . + /// The depthwise offset within . + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The depth of the memory area to write to. - internal void CopyFrom(UploadTexture3D source, int sourceX, int sourceY, int sourceZ, int destinationX, int destinationY, int destinationZ, int width, int height, int depth) + internal void CopyFrom(UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) { GraphicsDevice.ThrowIfDisposed(); @@ -422,24 +422,24 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); - Guard.IsInRange(sourceX, 0, source.Width, nameof(sourceX)); - Guard.IsInRange(sourceY, 0, source.Height, nameof(sourceY)); - Guard.IsInRange(sourceZ, 0, source.Depth, nameof(sourceZ)); - Guard.IsInRange(destinationX, 0, Width, nameof(destinationX)); - Guard.IsInRange(destinationY, 0, Height, nameof(destinationY)); - Guard.IsInRange(destinationZ, 0, Depth, nameof(destinationZ)); + Guard.IsInRange(sourceOffsetX, 0, source.Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, source.Height, nameof(sourceOffsetY)); + Guard.IsInRange(sourceOffsetZ, 0, source.Depth, nameof(sourceOffsetZ)); + Guard.IsInRange(destinationOffsetX, 0, Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, Height, nameof(destinationOffsetY)); + Guard.IsInRange(destinationOffsetZ, 0, Depth, nameof(destinationOffsetZ)); Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); Guard.IsBetweenOrEqualTo(width, 1, source.Width, nameof(width)); Guard.IsBetweenOrEqualTo(height, 1, source.Height, nameof(height)); Guard.IsBetweenOrEqualTo(depth, 1, source.Depth, nameof(depth)); - Guard.IsLessThanOrEqualTo(sourceX + width, source.Width, nameof(sourceX)); - Guard.IsLessThanOrEqualTo(sourceY + height, source.Height, nameof(sourceY)); - Guard.IsLessThanOrEqualTo(sourceZ + depth, source.Depth, nameof(sourceZ)); - Guard.IsLessThanOrEqualTo(destinationX + width, Width, nameof(destinationX)); - Guard.IsLessThanOrEqualTo(destinationY + height, Height, nameof(destinationY)); - Guard.IsLessThanOrEqualTo(destinationZ + depth, Depth, nameof(destinationZ)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, source.Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, source.Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, source.Depth, nameof(sourceOffsetZ)); + Guard.IsLessThanOrEqualTo(destinationOffsetX + width, Width, nameof(destinationOffsetX)); + Guard.IsLessThanOrEqualTo(destinationOffsetY + height, Height, nameof(destinationOffsetY)); + Guard.IsLessThanOrEqualTo(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); @@ -452,14 +452,14 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper { copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( d3D12ResourceDestination: D3D12Resource, - (uint)destinationX, - (uint)destinationY, - (ushort)destinationZ, + (uint)destinationOffsetX, + (uint)destinationOffsetY, + (ushort)destinationOffsetZ, d3D12ResourceSource: source.D3D12Resource, d3D12PlacedSubresourceFootprintSource, - (uint)sourceX, - (uint)sourceY, - (ushort)sourceZ, + (uint)sourceOffsetX, + (uint)sourceOffsetY, + (ushort)sourceOffsetZ, (uint)width, (uint)height, (ushort)depth); From 391c809f8d44e1a39f3ced77ff617899c6b68c6f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 14:27:33 +0100 Subject: [PATCH 14/19] Add mirrored Texture3D extensions for transfers --- .../Extensions/ReadBackTexture3DExtensions.cs | 60 ++++++++++++++++ .../Extensions/Texture3DExtensions.cs | 72 +++++++++++++++++++ .../Extensions/UploadTexture3DExtensions.cs | 39 +++++++--- 3 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs diff --git a/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs new file mode 100644 index 00000000..80024868 --- /dev/null +++ b/src/ComputeSharp/Graphics/Extensions/ReadBackTexture3DExtensions.cs @@ -0,0 +1,60 @@ +using ComputeSharp.Resources; + +namespace ComputeSharp; + +/// +/// A that contains extension methods for the type. +/// +public static class ReadBackTexture3DExtensions +{ + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this ReadBackTexture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index 23d56dda..c0622aeb 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -180,6 +180,27 @@ public static class Texture3DExtensions source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this Texture3D source, ReadBackTexture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } + /// /// Writes the contents of a given array to the current instance. /// @@ -329,4 +350,55 @@ public static class Texture3DExtensions { destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this Texture3D destination, UploadTexture3D source) + where T : unmanaged + { + destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of an instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, UploadTexture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } } diff --git a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs index 6bd980f8..52778dbc 100644 --- a/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/UploadTexture3DExtensions.cs @@ -11,29 +11,50 @@ public static class UploadTexture3DExtensions /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - public static void CopyTo(this UploadTexture3D texture, Texture3D destination) + public static void CopyTo(this UploadTexture3D source, Texture3D destination) where T : unmanaged { - destination.CopyFrom(texture, 0, 0, 0, 0, 0, 0, texture.Width, texture.Height, texture.Depth); + destination.CopyFrom(source, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); } /// /// Reads the contents of a instance and writes them into a target instance. /// /// The type of items stored on the texture. - /// The input instance to read data from. + /// The input instance to read data from. /// The target instance to write data to. - /// The horizontal offset in the source texture. - /// The vertical offset in the source texture. - /// The depthwise offset in the source texture. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. /// The width of the memory area to copy. /// The height of the memory area to copy. /// The depth of the memory area to copy. - public static void CopyTo(this UploadTexture3D texture, Texture3D destination, int x, int y, int z, int width, int height, int depth) + public static void CopyTo(this UploadTexture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) where T : unmanaged { - destination.CopyFrom(texture, x, y, z, 0, 0, 0, width, height, depth); + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this UploadTexture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } } From 3570b4d65e69efac53b0fe7d043a78a302b02054 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 17:27:52 +0100 Subject: [PATCH 15/19] Add new APIs to copy between Texture3D-s --- .../Extensions/Texture3DExtensions.cs | 102 +++++++ .../Resources/Abstract/Texture2D{T}.cs | 2 +- .../Resources/Abstract/Texture3D{T}.cs | 77 +++++ tests/ComputeSharp.Tests/Texture3DTests.cs | 283 ++++++++++++++++++ 4 files changed, 463 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index c0622aeb..e9ee10de 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -150,6 +150,57 @@ public static class Texture3DExtensions source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + public static void CopyTo(this Texture3D source, Texture3D destination) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } + /// /// Reads the contents of a instance and writes them into a target instance. /// @@ -351,6 +402,57 @@ public static class Texture3DExtensions destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this Texture3D destination, Texture3D source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } + /// /// Reads the contents of an instance and writes them into a target instance. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4c2f723e..efd1761e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -226,7 +226,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// - /// The target instance to write data to. + /// The target instance to write data to. /// The horizontal offset within . /// The vertical offset within . /// The horizontal offset in the source texture. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 6e569b89..b2a26d23 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -240,6 +240,83 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + /// Reads the contents of the specified range from the current instance and writes them into a instance. + /// + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset within . + /// The vertical offset within . + /// The depthwise offset within . + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + internal void CopyTo(Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(sourceOffsetZ, 0, Depth, nameof(sourceOffsetZ)); + Guard.IsInRange(destinationOffsetX, 0, destination.Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, destination.Height, nameof(destinationOffsetY)); + Guard.IsInRange(destinationOffsetZ, 0, destination.Depth, nameof(destinationOffsetZ)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); + + 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->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: destination.D3D12Resource, + (uint)destinationOffsetX, + (uint)destinationOffsetY, + (ushort)destinationOffsetZ, + d3D12ResourceSource: D3D12Resource, + (uint)sourceOffsetX, + (uint)sourceOffsetY, + (ushort)sourceOffsetZ, + (uint)width, + (uint)height, + (ushort)depth); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// /// Reads the contents of the specified range from the current instance and writes them into a instance. /// diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 2591ce8c..bab480cb 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -194,6 +194,289 @@ public partial class Texture3DTests texture.CopyTo(result, x, y, z, width, height, depth); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 3)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 1, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 1, 512, 512, 2)] + // [Data(512, 512, 4, 512, 512, 4, 0, 0, 1, 0, 0, 2, 512, 512, 2)] + // This test fails, only on WARP devices, and only if not run on its own. It seems + // to be possibly caused by a bug in ID3D12GraphicsCommandList::CopyTextureRegion. + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(512, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(1024, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 2)] + public void CopyTo_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth * sourceDepth).Select(static i => (float)i).ToArray(); + + using Texture3D source = device.Get().AllocateTexture3D(sourceType, array, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + + float[,,] stack = destination.ToArray(); + + for (int k = 0; k < destinationDepth; k++) + { + ReadOnlySpan2D result = new(stack, k); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (k >= destinationOffsetZ && + k < destinationOffsetZ + copyDepth && + i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceZ = k - destinationOffsetZ + sourceOffsetZ; + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + ReadOnlySpan2D expected = new(array, sourceZ * sourceHeight * sourceWidth, sourceHeight, sourceWidth, 0); + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 2055, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, -1, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, int.MaxValue, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 1025, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, -1, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, int.MaxValue, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, 6)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, -1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, int.MaxValue)] + [Data(512, 512, 3, 512, 512, 3, 450, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 450, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 5, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 450, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 450, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 4, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, -1, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, -1, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, -1, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, -1, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, -1, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, -1, 128, 128, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyTo_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + using Texture3D source = device.Get().AllocateTexture3D(sourceType, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 3)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 1, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 1, 512, 512, 2)] + // [Data(512, 512, 4, 512, 512, 4, 0, 0, 1, 0, 0, 2, 512, 512, 2)] + // TODO: see explanation above in CopyTo_TextureToVoid_Ok + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(512, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(1024, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 2)] + public void CopyFrom_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth * sourceDepth).Select(static i => (float)i).ToArray(); + + using Texture3D source = device.Get().AllocateTexture3D(sourceType, array, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + + float[,,] stack = destination.ToArray(); + + for (int k = 0; k < destinationDepth; k++) + { + ReadOnlySpan2D result = new(stack, k); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (k >= destinationOffsetZ && + k < destinationOffsetZ + copyDepth && + i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceZ = k - destinationOffsetZ + sourceOffsetZ; + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + ReadOnlySpan2D expected = new(array, sourceZ * sourceHeight * sourceWidth, sourceHeight, sourceWidth, 0); + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 2055, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, -1, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, int.MaxValue, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 1025, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, -1, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, int.MaxValue, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, 6)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, -1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, int.MaxValue)] + [Data(512, 512, 3, 512, 512, 3, 450, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 450, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 5, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 450, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 450, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 4, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, -1, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, -1, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, -1, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, -1, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, -1, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, -1, 128, 128, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyFrom_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + using Texture3D source = device.Get().AllocateTexture3D(sourceType, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + } + [CombinatorialTestMethod] [AllDevices] public void Dispatch_ReadOnlyTexture3D(Device device) From da90dd9157aaa07ca6834a50380e65ce1914a0f3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 17:46:08 +0100 Subject: [PATCH 16/19] Add Texture3D.ToArray overload --- .../Extensions/Texture3DExtensions.cs | 29 ++++++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 2 +- tests/ComputeSharp.Tests/Texture3DTests.cs | 58 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index e9ee10de..e38b317d 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -33,6 +33,35 @@ public static class Texture3DExtensions return data; } + /// + /// Reads the contents of the current instance and returns an array. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + /// A array with the contents of the current texture. + public static unsafe T[,,] ToArray(this Texture3D source, int x, int y, int z, int width, int height, int depth) + where T : unmanaged + { + Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); + Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + Guard.IsGreaterThanOrEqualTo(depth, 0, nameof(depth)); + + T[,,] data = new T[depth, height, width]; + + fixed (T* p = data) + { + source.CopyTo(new Span(p, width * height * depth), x, y, z, width, height, depth); + } + + return data; + } + /// /// Reads the contents of the current instance and writes them into a target array. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index fb2934d3..728369b9 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -146,8 +146,8 @@ public partial class Texture2DTests [AllDevices] [Resource(typeof(ReadOnlyTexture2D<>))] [Resource(typeof(ReadWriteTexture2D<>))] - [Data(0, -1, 50, 50)] [Data(-1, 0, 50, 50)] + [Data(0, -1, 50, 50)] [Data(12, 0, -1, 50)] [Data(12, 0, 20, -1)] [Data(12, 20, 20, 50)] diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index bab480cb..2797a423 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -131,6 +131,64 @@ public partial class Texture3DTests texture.Dispose(); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(0, 0, 0, 64, 64, 8)] + [Data(0, 14, 0, 64, 50, 1)] + [Data(14, 0, 0, 50, 64, 1)] + [Data(0, 0, 3, 50, 64, 1)] + [Data(10, 10, 1, 54, 54, 4)] + [Data(20, 20, 3, 32, 27, 3)] + [Data(40, 2, 4, 4, 62, 4)] + [Data(0, 60, 1, 64, 4, 6)] + [Data(63, 2, 2, 1, 60, 5)] + [Data(2, 63, 7, 60, 1, 1)] + public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int y, int z, int width, int height, int depth) + { + float[] array = Enumerable.Range(0, 64 * 64 * 8).Select(static i => (float)i).ToArray(); + + using Texture3D texture = device.Get().AllocateTexture3D(textureType, array, 64, 64, 8); + + float[,,] result = texture.ToArray(x, y, z, width, height, depth); + + Assert.AreEqual(depth, result.GetLength(0)); + Assert.AreEqual(height, result.GetLength(1)); + Assert.AreEqual(width, result.GetLength(2)); + + for (int k = 0; k < depth; k++) + { + Span2D expected = new Span2D(array, 64 * 64 * (k + z), 64, 64, 0).Slice(y, x, height, width); + Span2D data = new(result, k); + + CollectionAssert.AreEqual(expected.ToArray(), data.ToArray()); + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(-1, 0, 0, 50, 50, 1)] + [Data(0, -1, 0, 50, 50, 1)] + [Data(0, 0, -1, 50, 50, 1)] + [Data(12, 0, 0, -1, 50, 1)] + [Data(12, 0, 0, 20, -1, 0)] + [Data(12, 0, 0, 20, 2, -1)] + [Data(12, 20, 0, 20, 50, 2)] + [Data(12, 20, 0, 60, 20, 3)] + [Data(12, 10, 6, 20, 20, 10)] + [Data(80, 20, 0, 40, 20, 1)] + [Data(0, 80, 0, 40, 20, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int y, int z, int width, int height, int depth) + { + using Texture3D texture = device.Get().AllocateTexture3D(textureType, 64, 64, 8); + + _ = texture.ToArray(x, y, z, width, height, depth); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture3D<>))] From 21ae8a57bc4d3885bf9d564aa4369bba9dc60843 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Nov 2021 16:18:33 +0100 Subject: [PATCH 17/19] Add debug view resource tests --- .../Resources/Debug/BufferDebugView{T}.cs | 2 +- .../Resources/Debug/Texture2DDebugView{T}.cs | 2 +- .../Resources/Debug/Texture3DDebugView{T}.cs | 2 +- .../Debug/TransferBufferDebugView{T}.cs | 2 +- .../ComputeSharp.Tests.Internals.csproj | 11 + .../DebugViewTests.cs | 321 ++++++++++++++++++ 6 files changed, 336 insertions(+), 4 deletions(-) create mode 100644 tests/ComputeSharp.Tests.Internals/DebugViewTests.cs diff --git a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs index 44a88976..4f123a0f 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/BufferDebugView{T}.cs @@ -18,7 +18,7 @@ internal sealed class BufferDebugView { if (buffer is not null) { - var items = GC.AllocateUninitializedArray(buffer.Length); + T[] items = GC.AllocateUninitializedArray(buffer.Length); buffer.CopyTo(ref items[0], 0, buffer.Length); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs index bcfcf0d5..88020a15 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/Texture2DDebugView{T}.cs @@ -17,7 +17,7 @@ internal sealed class Texture2DDebugView { if (texture is not null) { - var items = new T[texture.Height, texture.Width]; + T[,] items = new T[texture.Height, texture.Width]; texture.CopyTo(ref items[0, 0], items.Length, 0, 0, texture.Width, texture.Height); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs index 7d1a3591..4da600b1 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/Texture3DDebugView{T}.cs @@ -17,7 +17,7 @@ internal sealed class Texture3DDebugView { if (texture is not null) { - var items = new T[texture.Depth, texture.Height, texture.Width]; + T[,,] items = new T[texture.Depth, texture.Height, texture.Width]; texture.CopyTo(ref items[0, 0, 0], items.Length, 0, 0, 0, texture.Width, texture.Height, texture.Depth); diff --git a/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs b/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs index e90b1027..a42c8df6 100644 --- a/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Debug/TransferBufferDebugView{T}.cs @@ -18,7 +18,7 @@ internal sealed class TransferBufferDebugView { if (buffer is not null) { - var items = GC.AllocateUninitializedArray(buffer.Length); + T[] items = GC.AllocateUninitializedArray(buffer.Length); buffer.Span.CopyTo(items); diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 670731c9..5828f8b3 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -19,4 +19,15 @@ PrivateAssets="contentfiles;build"/> + + + + + + + + + + + diff --git a/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs b/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs new file mode 100644 index 00000000..2831ede4 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/DebugViewTests.cs @@ -0,0 +1,321 @@ +using System; +using System.Linq; +using ComputeSharp.Resources; +using ComputeSharp.Resources.Debug; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Internals; + +[TestClass] +[TestCategory("Buffer")] +public partial class DebugViewTests +{ + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(32)] + [Data(256)] + [Data(1024)] + public void BufferDebugView_NotNull(Device device, Type bufferType, int size) + { + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); + + using Buffer buffer = device.Get().AllocateBuffer(bufferType, data); + + BufferDebugView view = new(buffer); + + Assert.IsNotNull(view.Items); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void BufferDebugView_Null() + { + using Buffer buffer = null!; + + BufferDebugView view = new(buffer); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(32, 32)] + [Data(1024, 1024)] + [Data(667, 480)] + public void Texture2DDebugView_NotNull(Device device, Type textureType, int width, int height) + { + float[] data = Enumerable.Range(0, width * height).Select(static i => (float)i).ToArray(); + + using Texture2D texture = device.Get().AllocateTexture2D(textureType, data, width, height); + + Texture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [Data(32, 32)] + [Data(512, 512)] + [Data(667, 480)] + public void Texture2DDebugView_WithTPixel_NotNull(Device device, Type textureType, int width, int height) + { + Random random = new(42); + Rgba32[,] data = new Rgba32[height, width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[i, j] = new Rgba32((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + } + } + + using Texture2D texture = device.Get().AllocateTexture2D(textureType, width, height); + + texture.CopyFrom(data); + + Texture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void Texture2DDebugView_Null() + { + using Texture2D texture = null!; + + Texture2DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void Texture3DDebugView_NotNull(Device device, Type textureType, int width, int height, int depth) + { + float[] data = Enumerable.Range(0, width * height * depth).Select(static i => (float)i).ToArray(); + + using Texture3D texture = device.Get().AllocateTexture3D(textureType, data, width, height, depth); + + Texture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<,>))] + [Resource(typeof(ReadWriteTexture3D<,>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void Texture3DDebugView_WithTPixel_NotNull(Device device, Type textureType, int width, int height, int depth) + { + Random random = new(42); + Rgba32[,,] data = new Rgba32[depth, height, width]; + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[k, i, j] = new Rgba32((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + } + } + } + + using Texture3D texture = device.Get().AllocateTexture3D(textureType, width, height, depth); + + texture.CopyFrom(data); + + Texture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void Texture3DDebugView_Null() + { + using Texture3D texture = null!; + + Texture3DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + [Data(32)] + [Data(1024)] + [Data(667)] + public void TransferBufferDebugView_NotNull(Device device, Type bufferType, int size) + { + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); + + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, size); + + data.AsSpan().CopyTo(buffer.Span); + + TransferBufferDebugView view = new(buffer); + + Assert.IsNotNull(view.Items); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferBufferDebugView_Null() + { + using TransferBuffer buffer = null!; + + TransferBufferDebugView view = new(buffer); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + [Data(32, 32)] + [Data(1024, 1024)] + [Data(667, 480)] + public void TransferTexture2DDebugView_NotNull(Device device, Type textureType, int width, int height) + { + Random random = new(42); + float[,] data = new float[height, width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[i, j] = random.NextSingle(); + } + } + + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(textureType, width, height); + + for (int i = 0; i < height; i++) + { + Span row = texture.View.GetRowSpan(i); + + for (int j = 0; j < width; j++) + { + row[j] = data[i, j]; + } + } + + TransferTexture2DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), height); + Assert.AreEqual(view.Items.GetLength(1), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferTexture2DDebugView_Null() + { + using Texture2D texture = null!; + + Texture2DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + [Data(32, 32, 4)] + [Data(1024, 1024, 3)] + [Data(667, 480, 7)] + public void TransferTexture3DDebugView_NotNull(Device device, Type textureType, int width, int height, int depth) + { + Random random = new(42); + float[,,] data = new float[depth, height, width]; + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + data[k, i, j] = random.NextSingle(); + } + } + } + + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(textureType, width, height, depth); + + for (int k = 0; k < depth; k++) + { + for (int i = 0; i < height; i++) + { + Span row = texture.View.GetRowSpan(i, k); + + for (int j = 0; j < width; j++) + { + row[j] = data[k, i, j]; + } + } + } + + TransferTexture3DDebugView view = new(texture); + + Assert.IsNotNull(view.Items); + Assert.AreEqual(view.Items.GetLength(0), depth); + Assert.AreEqual(view.Items.GetLength(1), height); + Assert.AreEqual(view.Items.GetLength(2), width); + + CollectionAssert.AreEqual(view.Items, data); + } + + [TestMethod] + public void TransferTexture3DDebugView_Null() + { + using Texture3D texture = null!; + + Texture3DDebugView view = new(texture); + + Assert.IsNull(view.Items); + } +} From e3c79f41700c0534fed77c80768c4c847bce9173 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Nov 2021 17:07:27 +0100 Subject: [PATCH 18/19] Add [AdditionalData] type to test projects --- .../ComputeSharp.Tests.Internals.csproj | 1 + .../Attributes/AdditionalDataAttribute.cs | 24 ++++ .../Attributes/AdditionalResourceAttribute.cs | 7 + .../CombinatorialTestMethodAttribute.cs | 133 ++++++++++++++++-- .../Attributes/DataAttribute.cs | 4 + .../Attributes/DeviceAttribute.cs | 4 + .../Attributes/ResourceAttribute.cs | 7 + 7 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 5828f8b3..4faf1cc4 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -20,6 +20,7 @@ + diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs new file mode 100644 index 00000000..6922c6ee --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs @@ -0,0 +1,24 @@ +using System; + +namespace ComputeSharp.Tests.Attributes; + +/// +/// An attribute to use with adding additional arbitrary input data. +/// +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class AdditionalDataAttribute : Attribute +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The additional input data. + public AdditionalDataAttribute(params object[] data) + { + Data = data; + } + + /// + /// Gets the additional input data for the current attribute. + /// + public object[] Data { get; } +} diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs index 342db016..bc2cedab 100644 --- a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs @@ -8,10 +8,17 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class AdditionalResourceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The additional resource type. public AdditionalResourceAttribute(Type type) { Type = type; } + /// + /// Gets the additional resource type for the current attribute. + /// public Type Type { get; } } diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs index 0e85b112..b0465feb 100644 --- a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs @@ -39,6 +39,7 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes Type[] resources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); Type[] additionalResources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); + object[][] additionalData = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); if (devices.Length > 0) { @@ -54,13 +55,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device, type, additionalType }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device, type, additionalType }.Concat(items).ToArray(); + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return new object[] { device, type, additionalType }.Concat(items).Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type, additionalType }.Concat(items).ToArray(); + } } } } @@ -69,13 +88,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device, type }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device, type }.Concat(items).ToArray(); + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return new object[] { device, type }.Concat(items).Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type }.Concat(items).ToArray(); + } } } } @@ -90,13 +127,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device }.Concat(items).ToArray(); + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return new object[] { device }.Concat(items).Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device }.Concat(items).ToArray(); + } } } } @@ -112,13 +167,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { type, additionalType }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { type, additionalType }.Concat(items).ToArray(); + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return new object[] { type, additionalType }.Concat(items).Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type, additionalType }.Concat(items).ToArray(); + } } } } @@ -127,13 +200,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { type }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { type }.Concat(items).ToArray(); + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return new object[] { type }.Concat(items).Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type }.Concat(items).ToArray(); + } } } } @@ -148,13 +239,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return Array.Empty(); } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return items; + foreach (object[] items in data) + { + foreach (object[] additionalItems in additionalData) + { + yield return items.Concat(additionalItems).ToArray(); + } + } + } + else + { + foreach (object[] items in data) + { + yield return items; + } } } } diff --git a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs index 806263ab..c4f63acf 100644 --- a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs @@ -8,6 +8,10 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class DataAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The input data. public DataAttribute(params object[] data) { Data = data; diff --git a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs index aa100468..7c249b42 100644 --- a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs @@ -9,6 +9,10 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public sealed class DeviceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The target device type to use.. public DeviceAttribute(Device device) { Device = device; diff --git a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs index 2cd0d476..72f9d75a 100644 --- a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs @@ -8,10 +8,17 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class ResourceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The resource type. public ResourceAttribute(Type type) { Type = type; } + /// + /// Gets the resource type for the current attribute. + /// public Type Type { get; } } From adde64fdeb077381cfa1ad3bf07a8d871cb77aa5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Nov 2021 17:13:51 +0100 Subject: [PATCH 19/19] Fix constant buffer allocation with invalid alignment --- .../Core/Helpers/AlignmentHelper.cs | 13 +++++++ .../Graphics/Resources/Abstract/Buffer{T}.cs | 10 ++++-- tests/ComputeSharp.Tests/BufferTests.cs | 36 ++++++++++++------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs b/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs index 7ea5a097..846d2a6b 100644 --- a/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs +++ b/src/ComputeSharp/Core/Helpers/AlignmentHelper.cs @@ -21,6 +21,19 @@ internal static class AlignmentHelper return (size + alignment - 1) & ~(alignment - 1); } + /// + /// Pads the input size so that its pitch is aligned to a multiple of a specified length. + /// + /// The input size to pad, if needed. + /// The target alignment. + /// The padded value relative to the given inputs. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static nint Pad(nint size, nint alignment) + { + return (size + alignment - 1) & ~(alignment - 1); + } + /// /// Aligns a given offset to a specified boundary, if a given size increment would have crossed it. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index ce442d70..034f0c6e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -1,6 +1,7 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -72,11 +73,14 @@ public unsafe abstract class Buffer : NativeObject UnsupportedDoubleOperationsException.Throw(); } - SizeInBytes = checked((nint)(length * elementSizeInBytes)); + nint usableSizeInBytes = checked((nint)(length * elementSizeInBytes)); + nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, Windows.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes; + + SizeInBytes = usableSizeInBytes; GraphicsDevice = device; Length = length; - this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes); + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); device.RegisterAllocatedResource(); @@ -85,7 +89,7 @@ public unsafe abstract class Buffer : NativeObject switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), SizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 2eb9a06e..a4833d8f 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -18,12 +18,16 @@ public partial class BufferTests [Resource(typeof(ReadWriteBuffer<>))] [Data(AllocationMode.Default)] [Data(AllocationMode.Clear)] - public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) + [AdditionalData(128)] + [AdditionalData(768)] + [AdditionalData(1024)] + [AdditionalData(443)] + public void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode, int size) { - using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128, allocationMode); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, size, allocationMode); Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Length, 128); + Assert.AreEqual(buffer.Length, size); Assert.AreSame(buffer.GraphicsDevice, device.Get()); if (allocationMode == AllocationMode.Clear) @@ -53,14 +57,18 @@ public partial class BufferTests [Resource(typeof(ConstantBuffer<>))] [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] - public void Allocate_FromArray(Device device, Type bufferType) + [Data(128)] + [Data(768)] + [Data(1024)] + [Data(443)] + public void Allocate_FromArray(Device device, Type bufferType, int size) { - float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); using Buffer buffer = device.Get().AllocateBuffer(bufferType, data); Assert.IsNotNull(buffer); - Assert.AreEqual(buffer.Length, 128); + Assert.AreEqual(buffer.Length, size); Assert.AreSame(buffer.GraphicsDevice, device.Get()); float[] result = buffer.ToArray(); @@ -75,18 +83,22 @@ public partial class BufferTests [Resource(typeof(ConstantBuffer<>))] [Resource(typeof(ReadOnlyBuffer<>))] [Resource(typeof(ReadWriteBuffer<>))] - [Data(typeof(ConstantBuffer<>))] - [Data(typeof(ReadOnlyBuffer<>))] - [Data(typeof(ReadWriteBuffer<>))] - public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType) + [AdditionalResource(typeof(ConstantBuffer<>))] + [AdditionalResource(typeof(ReadOnlyBuffer<>))] + [AdditionalResource(typeof(ReadWriteBuffer<>))] + [Data(128)] + [Data(768)] + [Data(1024)] + [Data(443)] + public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType, int size) { - float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); + float[] data = Enumerable.Range(0, size).Select(static i => (float)i).ToArray(); using Buffer source = device.Get().AllocateBuffer(sourceType, data); using Buffer destination = device.Get().AllocateBuffer(destinationType, source); Assert.IsNotNull(destination); - Assert.AreEqual(destination.Length, 128); + Assert.AreEqual(destination.Length, size); Assert.AreSame(destination.GraphicsDevice, device.Get()); float[] result = destination.ToArray();