Readonly buffer optimized when no padding is needed

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-13 21:15:33 +02:00
1 親 5ff2b2d4e0
コミット 96232a9538
1ファイル変更
+54 -28
+54 -28
ファイルの表示
@@ -38,46 +38,72 @@ namespace ComputeSharp.Graphics.Buffers
/// <inheritdoc/>
public override void GetData(Span<T> span)
{
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan();
Map(0);
MemoryHelper.Copy(MappedResource, temporarySpan);
Unmap(0);
ref byte tin = ref temporarySpan.GetPinnableReference();
ref T tout = ref span.GetPinnableReference();
for (int i = 0; i < Size; i++)
// Directly copy the data back if there is no padding
if (PaddedElementSizeInBytes == ElementSizeInBytes)
{
ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes);
Unsafe.Add(ref tout, i) = Unsafe.As<byte, T>(ref rsource);
Map(0);
MemoryHelper.Copy(MappedResource, span);
Unmap(0);
}
else
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan();
ArrayPool<byte>.Shared.Return(temporaryArray);
// Copy the padded data to the temporary array
Map(0);
MemoryHelper.Copy(MappedResource, temporarySpan);
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++)
{
ref byte rsource = ref Unsafe.Add(ref tin, i * PaddedElementSizeInBytes);
Unsafe.Add(ref tout, i) = Unsafe.As<byte, T>(ref rsource);
}
ArrayPool<byte>.Shared.Return(temporaryArray);
}
}
/// <inheritdoc/>
public override void SetData(Span<T> span)
{
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer
ref T tin = ref span.GetPinnableReference();
ref byte tout = ref temporarySpan.GetPinnableReference();
temporarySpan.Clear();
for (int i = 0; i < Size; i++)
// Directly copy the input span if there is no padding
if (PaddedElementSizeInBytes == ElementSizeInBytes)
{
ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes);
Unsafe.As<byte, T>(ref rtarget) = Unsafe.Add(ref tin, i);
Map(0);
MemoryHelper.Copy(span, MappedResource);
Unmap(0);
}
else
{
// Create the temporary array
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
Span<byte> temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer
ref T tin = ref span.GetPinnableReference();
ref byte tout = ref temporarySpan.GetPinnableReference();
Map(0);
MemoryHelper.Copy(temporarySpan, MappedResource);
Unmap(0);
temporarySpan.Clear();
ArrayPool<byte>.Shared.Return(temporaryArray);
// Copy the input data to the temporary array and add the padding
for (int i = 0; i < Size; i++)
{
ref byte rtarget = ref Unsafe.Add(ref tout, i * PaddedElementSizeInBytes);
Unsafe.As<byte, T>(ref rtarget) = Unsafe.Add(ref tin, i);
}
// Copy the padded data to the GPU
Map(0);
MemoryHelper.Copy(temporarySpan, MappedResource);
Unmap(0);
ArrayPool<byte>.Shared.Return(temporaryArray);
}
}
}
}