diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs
index 44d94af1..76b0e267 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) => SetData(array.AsSpan(), offset, 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
diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs
index 944adb56..29a747f7 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 * ElementSizeInBytes, 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 * ElementSizeInBytes, count * ElementSizeInBytes);
copyCommandList.Flush();
}
diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
index 85321b41..152c3681 100644
--- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs
@@ -41,24 +41,24 @@ 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(0, 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();
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);
@@ -70,24 +70,24 @@ 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();
// 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);
@@ -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);
}
}
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));
}
}
}
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;
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;
}
}
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));
+ }
}
}