From 307f87f70f817192b34f0951b33570dc21cd5e35 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Aug 2019 23:51:59 +0200 Subject: [PATCH 01/10] Updated buffer base APIs definition --- .../Buffers/Abstract/HlslBuffer{T}.cs | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 44d94af1..09e384e2 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -25,10 +25,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// A array with the contents of the current buffer [Pure] - public T[] GetData() + public T[] GetData() => GetData(0, Size); + + /// + /// Reads the contents of the current instance in a given range and returns an array + /// + /// 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 T[] GetData(int offset, int count) { - T[] data = new T[Size]; - GetData(data); + T[] data = new T[count]; + GetData(data, offset, count); return data; } @@ -37,7 +46,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// Reads the contents of the current instance and writes them into a target /// /// The input to write data to - public abstract void GetData(Span span); + public void GetData(Span span) => GetData(span, 0, Size); + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target + /// + /// The input to write data to + /// The offset to start reading data from + /// The number of items to read + public abstract void GetData(Span span, int offset, int count); /// /// Writes the contents of a given array to the current instance @@ -45,11 +62,27 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to read data from public void SetData(T[] array) => SetData(array.AsSpan()); + /// + /// Writes the contents of a given array to a specified area of the current instance + /// + /// The input array to read data from + /// The offset to start writing data to + /// The number of items to write + public void SetData(T[] array, int offset, int count) { } + /// /// Writes the contents of a given to the current instance /// /// The input to read data from - public abstract void SetData(Span span); + public void SetData(Span span) => SetData(span, 0, Size); + + /// + /// Writes the contents of a given to a specified area of the current instance + /// + /// The input to read data from + /// The offset to start writing data to + /// The number of items to write + public abstract void SetData(Span span, int offset, int count); /// /// Writes the contents of a given to the current instance From d26e38ec0265c291d70b3484ce4468a3d6df6952 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 00:25:41 +0200 Subject: [PATCH 02/10] Updated MemoryHelper class --- .../Helpers/MemoryHelper.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index ad79aa4b..560db122 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -13,11 +13,15 @@ namespace ComputeSharp.Graphics.Helpers /// /// The type of values in the input /// The source to read + /// The source offset to start reading data from /// The for the destination memory area - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(Span source, IntPtr destination) where T : unmanaged + /// The destination offset to start writing data to + /// The total number of items to copy + public static unsafe void Copy(Span source, int sourceOffset, IntPtr destination, int destinationOffset, int count) where T : unmanaged { - source.CopyTo(new Span(destination.ToPointer(), source.Length)); + ref T rout = ref Unsafe.AsRef(destination.ToPointer()); + void* target = Unsafe.AsPointer(ref Unsafe.Add(ref rout, destinationOffset)); + source.Slice(sourceOffset, count).CopyTo(new Span(target, count)); } /// @@ -25,11 +29,16 @@ namespace ComputeSharp.Graphics.Helpers /// /// The type of values to read /// The that indicates the memory area to read from + /// The source offset to start reading data from /// The destination to write + /// The destination offset to start writing data to + /// The total number of items to copy [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(IntPtr source, Span destination) where T : unmanaged + public static unsafe void Copy(IntPtr source, int sourceOffset, Span destination, int destinationOffset, int count) where T : unmanaged { - new Span(source.ToPointer(), destination.Length).CopyTo(destination); + ref T rin = ref Unsafe.AsRef(source.ToPointer()); + void* target = Unsafe.AsPointer(ref Unsafe.Add(ref rin, sourceOffset)); + new Span(target, count).CopyTo(destination.Slice(destinationOffset, count)); } } } From 19454b4e586ff7d0a852caa17bb31113f5f3fa3a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 00:28:30 +0200 Subject: [PATCH 03/10] Refactored buffers with new APIs --- .../Abstract/HlslStructuredBuffer{T}.cs | 16 +++++++-------- .../Buffers/ConstantBuffer{T}.cs | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 944adb56..7829264f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -22,31 +22,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf(), bufferType) { } /// - public override void GetData(Span span) + public override void GetData(Span span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); + using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(this, 0, transferBuffer, 0, SizeInBytes); + copyCommandList.CopyBufferRegion(this, offset, transferBuffer, 0, count * ElementSizeInBytes); copyCommandList.Flush(); transferBuffer.Map(0); - MemoryHelper.Copy(transferBuffer.MappedResource, span); + MemoryHelper.Copy(transferBuffer.MappedResource, 0, span, 0, count); transferBuffer.Unmap(0); } /// - public override void SetData(Span span) + public override void SetData(Span span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); + using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); transferBuffer.Map(0); - MemoryHelper.Copy(span, transferBuffer.MappedResource); + MemoryHelper.Copy(span, 0, transferBuffer.MappedResource, 0, count); transferBuffer.Unmap(0); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(transferBuffer, 0, this, 0, SizeInBytes); + copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset, count * ElementSizeInBytes); copyCommandList.Flush(); } diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 85321b41..6e19703b 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -41,17 +41,17 @@ namespace ComputeSharp.Graphics.Buffers public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); /// - public override void GetData(Span span) + public override void GetData(Span span, int offset, int count) { if (IsPaddingPresent) { // Create the temporary array - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(); + byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(count * PaddedElementSizeInBytes); // Copy the padded data to the temporary array Map(0); - MemoryHelper.Copy(MappedResource, temporarySpan); + MemoryHelper.Copy(MappedResource, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes); Unmap(0); ref byte tin = ref temporarySpan.GetPinnableReference(); @@ -70,19 +70,19 @@ namespace ComputeSharp.Graphics.Buffers { // Directly copy the data back if there is no padding Map(0); - MemoryHelper.Copy(MappedResource, span); + MemoryHelper.Copy(MappedResource, offset, span, 0, count); Unmap(0); } } /// - public override void SetData(Span span) + public override void SetData(Span span, int offset, int count) { if (IsPaddingPresent) { // Create the temporary array - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer + byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes); // Array pool arrays can be longer ref T tin = ref span.GetPinnableReference(); ref byte tout = ref temporarySpan.GetPinnableReference(); @@ -95,7 +95,7 @@ namespace ComputeSharp.Graphics.Buffers // Copy the padded data to the GPU Map(0); - MemoryHelper.Copy(temporarySpan, MappedResource); + MemoryHelper.Copy(temporarySpan, 0, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes); Unmap(0); ArrayPool.Shared.Return(temporaryArray); @@ -104,7 +104,7 @@ namespace ComputeSharp.Graphics.Buffers { // Directly copy the input span if there is no padding Map(0); - MemoryHelper.Copy(span, MappedResource); + MemoryHelper.Copy(span, 0, MappedResource, offset, count); Unmap(0); } } From 228c4787d4a744d2818c9efa1ae333fb733c6bd5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 00:44:18 +0200 Subject: [PATCH 04/10] Fixed a small bug --- src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 6e19703b..9a7d76fc 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -47,7 +47,7 @@ namespace ComputeSharp.Graphics.Buffers { // Create the temporary array byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(count * PaddedElementSizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes); // Copy the padded data to the temporary array Map(0); From 4b513d13ea1ff2a6fddb13da9d001a93bd8fc355 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:04:44 +0200 Subject: [PATCH 05/10] Fixed parsing of static fields and properties --- src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index 70b07b48..efaed376 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -132,8 +132,6 @@ namespace ComputeSharp.Shaders.Mappings return $"{containingMemberSymbol.Name}{mapped}"; } - if (memberSymbol.IsStatic) return $"{containingMemberSymbol.Name}::{memberSymbol.Name}"; - return null; } } From 9765bd4b14de6bf66af4ab4ad0bfb4fe480d6836 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:06:19 +0200 Subject: [PATCH 06/10] Minor code tweaks --- src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index 3c6bc225..a1297d66 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -112,19 +112,20 @@ namespace ComputeSharp.Shaders.Extensions // Constant replacement if the value is a readonly scalar value if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberInfo.MemberType)) { - return memberInfo.GetValue(null) switch + LiteralExpressionSyntax expression = memberInfo.GetValue(null) switch { true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)), IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))), _ => throw new InvalidOperationException($"Invalid field of type {memberInfo.MemberType}") }; + return expression.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); } // Captured member, treat it like any other captured variable in the closure string name = $"{containingMemberSymbolInfo.Symbol.Name}_{memberInfo.Name}"; variable = (name, memberInfo); - return SyntaxFactory.IdentifierName(name); + return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); } return node; From b54fcdb5194a5c8bbe4bbc836768df8dc4e6f87e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:20:03 +0200 Subject: [PATCH 07/10] Fixed a bug in the constant shader APIs --- src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 9a7d76fc..152c3681 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -58,7 +58,7 @@ namespace ComputeSharp.Graphics.Buffers ref T tout = ref span.GetPinnableReference(); // Copy the padded data to the target span, removing the padding - for (int i = 0; i < Size; i++) + for (int i = 0; i < count; i++) { ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes); Unsafe.Add(ref tout, i) = Unsafe.As(ref rsource); @@ -87,7 +87,7 @@ namespace ComputeSharp.Graphics.Buffers ref byte tout = ref temporarySpan.GetPinnableReference(); // Copy the input data to the temporary array and add the padding - for (int i = 0; i < Size; i++) + for (int i = 0; i < count; i++) { ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes); Unsafe.As(ref rtarget) = Unsafe.Add(ref tin, i); From 7797d97e89ec05038d5723f34b40c3d0ecee9b2e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:24:39 +0200 Subject: [PATCH 08/10] Implemented missing method --- src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 09e384e2..76b0e267 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -68,7 +68,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The input array to read data from /// The offset to start writing data to /// The number of items to write - public void SetData(T[] array, int offset, int count) { } + public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count); /// /// Writes the contents of a given to the current instance From dd5663c5e0066c240c519af54fe3b597f3ae2730 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:28:25 +0200 Subject: [PATCH 09/10] Fixed range buffer copy --- .../Buffers/Abstract/HlslStructuredBuffer{T}.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 7829264f..29a747f7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -27,7 +27,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(this, offset, transferBuffer, 0, count * ElementSizeInBytes); + copyCommandList.CopyBufferRegion(this, offset * ElementSizeInBytes, transferBuffer, 0, count * ElementSizeInBytes); copyCommandList.Flush(); transferBuffer.Map(0); @@ -46,7 +46,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset, count * ElementSizeInBytes); + copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset * ElementSizeInBytes, count * ElementSizeInBytes); copyCommandList.Flush(); } From 26a4833108ef6f44bbe15c0112854e294986123d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 01:28:33 +0200 Subject: [PATCH 10/10] Added buffer tests for new APIs --- tests/ComputeSharp.Tests/BufferTests.cs | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 2b0b00c9..edffb41b 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -91,5 +91,59 @@ namespace ComputeSharp.Tests Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult)); } + + [TestMethod] + public void ConstantBufferGetRangeData() + { + float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); + + using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); + + float[] sourceResult = sourceBuffer.GetData(128, 100); + + Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult)); + } + + [TestMethod] + public void ConstantBufferSetRangeData() + { + float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray(); + + using ConstantBuffer sourceBuffer = Gpu.Default.AllocateConstantBuffer(array); + + array.AsSpan(56, 100).Clear(); + sourceBuffer.SetData(new float[100], 56, 100); + + float[] sourceResult = sourceBuffer.GetData(); + + Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); + } + + [TestMethod] + public void ReadWriteBufferGetRangeData() + { + float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray(); + + using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); + + float[] sourceResult = sourceBuffer.GetData(128, 100); + + Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult)); + } + + [TestMethod] + public void ReadWriteBufferSetRangeData() + { + float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray(); + + using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array); + + array.AsSpan(56, 100).Clear(); + sourceBuffer.SetData(new float[100], 56, 100); + + float[] sourceResult = sourceBuffer.GetData(); + + Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult)); + } } }