Merge pull request #14 from Sergio0694/feature_readonly-buffers-support
Readonly buffers support
このコミットが含まれているのは:
13ファイル変更
+382
-146
検索条件に一致するファイルはありません
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using ComputeSharp.NetCore.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
@@ -10,10 +11,9 @@ namespace ComputeSharp.NetCore.Tests
|
||||
public class BufferTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetData()
|
||||
public void ReadWriteBufferGetSetDataFromArray()
|
||||
{
|
||||
float[] array = new float[4096];
|
||||
array.AsSpan().FillRandom();
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer(array);
|
||||
float[] result = buffer.GetData();
|
||||
@@ -22,15 +22,74 @@ namespace ComputeSharp.NetCore.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadOnlyBufferGetSetData()
|
||||
public void ReadOnlyBufferGetSetDataFromArray()
|
||||
{
|
||||
float[] array = new float[4096];
|
||||
array.AsSpan().FillRandom();
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> buffer = Gpu.Default.AllocateReadOnlyBuffer(array);
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(result));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetDataFromReadWriteBuffer()
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
|
||||
using ReadWriteBuffer<float> destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadWriteBufferGetSetDataFromReadOnlyBuffer()
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(array);
|
||||
using ReadWriteBuffer<float> destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadOnlyBufferGetSetDataFromReadWriteBuffer()
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
|
||||
using ReadOnlyBuffer<float> destinationBuffer = Gpu.Default.AllocateReadOnlyBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ReadOnlyBufferGetSetDataFromReadOnlyBuffer()
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(array);
|
||||
using ReadOnlyBuffer<float> destinationBuffer = Gpu.Default.AllocateReadOnlyBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,21 +9,6 @@ namespace ComputeSharp.NetCore.Tests.Extensions
|
||||
/// </summary>
|
||||
public static class SpanExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Fills an input <see cref="Span{T}"/> instance with random <see langword="float"/> values
|
||||
/// </summary>
|
||||
/// <param name="span">The target <see cref="Span{T}"/> to fill</param>
|
||||
public static void FillRandom(this Span<float> span)
|
||||
{
|
||||
if (span.Length == 0) return;
|
||||
|
||||
var l = span.Length;
|
||||
ref var rspan = ref span.GetPinnableReference();
|
||||
|
||||
for (var i = 0; i < l; i++)
|
||||
Unsafe.Add(ref rspan, i) = (float)ConcurrentRandom.Instance.NextDouble();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The maximum threshold to validate two <see langword="float"/> values as being equal
|
||||
/// </summary>
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace System
|
||||
{
|
||||
/// <summary>
|
||||
/// A thread-safe <see cref="Random"/> wrapper class
|
||||
/// </summary>
|
||||
public static class ConcurrentRandom
|
||||
{
|
||||
// Shared seed
|
||||
private static int _Seed = Environment.TickCount;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ThreadLocal{T}"/> object that provides the <see cref="Random"/> instances
|
||||
/// </summary>
|
||||
private static readonly ThreadLocal<Random> _Instance = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref _Seed)));
|
||||
|
||||
/// <summary>
|
||||
/// Gets a thread-safe <see cref="Random"/> instance
|
||||
/// </summary>
|
||||
public static Random Instance
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _Instance.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする