Merge pull request #14 from Sergio0694/feature_readonly-buffers-support
Readonly buffers support
このコミットが含まれているのは:
13ファイル変更
+382
-146
検索条件に一致するファイルはありません
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.Contracts;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed buffer stored on GPU memory, that can be used from an HLSL shader
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public abstract class HlslBuffer<T> : Buffer<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslBuffer{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="heapType">The heap type for the current buffer</param>
|
||||
protected internal HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device, size, sizeInBytes, heapType) { }
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and returns an array
|
||||
/// </summary>
|
||||
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
|
||||
[Pure]
|
||||
public T[] GetData()
|
||||
{
|
||||
T[] data = new T[Size];
|
||||
GetData(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
|
||||
public abstract void GetData(Span<T> span);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
|
||||
public abstract void SetData(Span<T> span);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="HlslBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> to read data from</param>
|
||||
public abstract void SetData(HlslBuffer<T> buffer);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="HlslBuffer{T}"/> to the current <see cref="HlslBuffer{T}"/> instance, using a temporary CPU buffer
|
||||
/// </summary>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> to read data from</param>
|
||||
protected void SetDataWithCpuBuffer(HlslBuffer<T> buffer)
|
||||
{
|
||||
// Create a temporary array
|
||||
T[] array = ArrayPool<T>.Shared.Rent(buffer.Size);
|
||||
Span<T> span = array.AsSpan(0, buffer.Size);
|
||||
|
||||
// Get the unpadded data from the read write buffer
|
||||
buffer.GetData(span);
|
||||
|
||||
// Set the data, adding the padding if needed
|
||||
SetData(span);
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
@@ -18,12 +16,13 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
|
||||
/// <param name="heapType">The heap type for the current buffer</param>
|
||||
protected internal Buffer(GraphicsDevice device, int size, HeapType heapType) : base(device)
|
||||
protected internal Buffer(GraphicsDevice device, int size, int sizeInBytes, HeapType heapType) : base(device)
|
||||
{
|
||||
Size = size;
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
SizeInBytes = Size * ElementSizeInBytes;
|
||||
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
|
||||
HeapType = heapType;
|
||||
|
||||
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
|
||||
@@ -45,6 +44,21 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
protected int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
|
||||
/// </summary>
|
||||
protected int ElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a constant buffer
|
||||
/// </summary>
|
||||
@@ -88,88 +102,9 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
|
||||
/// </summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
public int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of each <typeparamref name="T"/> value contained in the buffer
|
||||
/// </summary>
|
||||
public int ElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the heap type being targeted by the current buffer
|
||||
/// </summary>
|
||||
public HeapType HeapType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and returns an array
|
||||
/// </summary>
|
||||
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer</returns>
|
||||
[Pure]
|
||||
public T[] GetData()
|
||||
{
|
||||
T[] data = new T[Size];
|
||||
GetData(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to write data to</param>
|
||||
public void GetData(Span<T> span)
|
||||
{
|
||||
// Create a temporary readback buffer if the current buffer type is read write (can't be read from directly)
|
||||
if (HeapType == HeapType.Default)
|
||||
{
|
||||
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Readback);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
|
||||
readbackBaffer.GetData(span);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Map and read the memory back directly
|
||||
Map(0);
|
||||
MemoryHelper.Copy(MappedResource, span);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Span{T}"/> to the current <see cref="Buffer{T}"/> instance
|
||||
/// </summary>
|
||||
/// <param name="span">The input <see cref="Span{T}"/> to read data from</param>
|
||||
public void SetData(Span<T> span)
|
||||
{
|
||||
// Create a temporary upload buffer if the current buffer type is read write (can't be written to directly)
|
||||
if (HeapType == HeapType.Default)
|
||||
{
|
||||
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, HeapType.Upload);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
uploadBuffer.SetData(span);
|
||||
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Map and copy the memory directly
|
||||
Map(0);
|
||||
MemoryHelper.Copy(span, MappedResource);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
@@ -74,6 +75,23 @@ namespace ComputeSharp
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadWriteBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadWriteBuffer<T> AllocateReadWriteBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
{
|
||||
ReadWriteBuffer<T> readWriteBuffer = new ReadWriteBuffer<T>(device, buffer.Size);
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters
|
||||
/// </summary>
|
||||
@@ -122,5 +140,22 @@ namespace ComputeSharp
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new constant buffer with the specified parameters
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to store in the buffer</typeparam>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="buffer">The input <see cref="HlslBuffer{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer<T> readOnlyBuffer = new ReadOnlyBuffer<T>(device, buffer.Size);
|
||||
readOnlyBuffer.SetData(buffer);
|
||||
|
||||
return readOnlyBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
@@ -7,14 +11,28 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadOnlyBuffer<T> : Buffer<T> where T : unmanaged
|
||||
public sealed class ReadOnlyBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadOnlyBuffer{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { }
|
||||
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, HeapType.Upload)
|
||||
{
|
||||
PaddedElementSizeInBytes = SizeInBytes / Size;
|
||||
IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
internal int PaddedElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
@@ -22,5 +40,88 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
|
||||
public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void GetData(Span<T> span)
|
||||
{
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
// Create the temporary array
|
||||
byte[] temporaryArray = ArrayPool<byte>.Shared.Rent(SizeInBytes);
|
||||
Span<byte> temporarySpan = temporaryArray.AsSpan();
|
||||
|
||||
// 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);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Directly copy the data back if there is no padding
|
||||
Map(0);
|
||||
MemoryHelper.Copy(MappedResource, span);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(Span<T> span)
|
||||
{
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Directly copy the input span if there is no padding
|
||||
Map(0);
|
||||
MemoryHelper.Copy(span, MappedResource);
|
||||
Unmap(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(HlslBuffer<T> buffer)
|
||||
{
|
||||
if (buffer is ReadOnlyBuffer<T>)
|
||||
{
|
||||
// Directly copy the input buffer, if possible
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
@@ -7,14 +11,14 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadWriteBuffer<T> : Buffer<T> where T : unmanaged
|
||||
public sealed class ReadWriteBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadWriteBuffer{T}"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance</param>
|
||||
/// <param name="size">The number of items to store in the current buffer</param>
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { }
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf<T>(), HeapType.Default) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
@@ -26,5 +30,48 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
|
||||
set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void GetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> readbackBaffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, HeapType.Readback);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
|
||||
readbackBaffer.Map(0);
|
||||
MemoryHelper.Copy(readbackBaffer.MappedResource, span);
|
||||
readbackBaffer.Unmap(0);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> uploadBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, HeapType.Upload);
|
||||
|
||||
uploadBuffer.Map(0);
|
||||
MemoryHelper.Copy(span, uploadBuffer.MappedResource);
|
||||
uploadBuffer.Unmap(0);
|
||||
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(HlslBuffer<T> buffer)
|
||||
{
|
||||
if (buffer is ReadOnlyBuffer<T> readOnlyBuffer && !readOnlyBuffer.IsPaddingPresent)
|
||||
{
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush(true);
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,15 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> value
|
||||
/// </summary>
|
||||
/// <param name="type">The input <see cref="Type"/> instance to check</param>
|
||||
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadOnlyBuffer{T}"/> instance</returns>
|
||||
[Pure]
|
||||
public static bool IsReadOnlyBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(ReadOnlyBuffer<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given <see cref="Type"/> is an HLSL known type
|
||||
/// </summary>
|
||||
|
||||
@@ -12,10 +12,15 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// </summary>
|
||||
public bool IsConstantBuffer { get; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not this constant buffer actually contains a single, explicit variable
|
||||
/// </summary>
|
||||
public bool IsSingleValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the current constant buffer field
|
||||
/// </summary>
|
||||
public int ConstantBufferIndex { get; set; }
|
||||
public int ConstantBufferIndex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBufferFieldInfo"/> instance with the specified parameters
|
||||
@@ -23,9 +28,11 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// <param name="fieldType">The type of the current field</param>
|
||||
/// <param name="fieldName">The name of the current field</param>
|
||||
/// <param name="bufferIndex">The index of the current constant buffer field</param>
|
||||
public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName)
|
||||
/// <param name="singleValue">Whether or not this constant buffer actually contains a single, explicit variable</param>
|
||||
public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex, bool singleValue) : base(fieldType, fieldName)
|
||||
{
|
||||
ConstantBufferIndex = bufferIndex;
|
||||
IsSingleValue = singleValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ static {{&FieldType}} {{FieldName}} = {{FieldValue}};
|
||||
{{#IsConstantBuffer}}
|
||||
cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}})
|
||||
{
|
||||
{{&FieldType}} {{FieldName}};
|
||||
{{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}};
|
||||
}
|
||||
{{/IsConstantBuffer}}
|
||||
{{!Read write buffer}}
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace ComputeSharp.Shaders
|
||||
commandList.SetPipelineState(pipelineState);
|
||||
|
||||
// Load the captured buffers
|
||||
foreach (var buffer in shaderLoader.ReadWriteBuffers.Select((r, i) => (Resource: r, Index: i)))
|
||||
foreach (var buffer in shaderLoader.Buffers.Select((r, i) => (Resource: r, Index: i)))
|
||||
{
|
||||
commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource);
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ namespace ComputeSharp.Shaders.Translation
|
||||
public RootParameter[] RootParameters { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ordered collection of read write buffers used as fields in the current shader
|
||||
/// Gets the ordered collection of buffers used as fields in the current shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<GraphicsResource> ReadWriteBuffers { get; private set; }
|
||||
public IReadOnlyList<GraphicsResource> Buffers { get; private set; }
|
||||
|
||||
private readonly List<FieldInfoBase> _FieldsInfo = new List<FieldInfoBase>();
|
||||
|
||||
@@ -106,8 +106,9 @@ namespace ComputeSharp.Shaders.Translation
|
||||
if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body");
|
||||
|
||||
List<DescriptorRange> descriptorRanges = new List<DescriptorRange>();
|
||||
List<GraphicsResource> readWriteBuffers = new List<GraphicsResource>();
|
||||
List<GraphicsResource> buffers = new List<GraphicsResource>();
|
||||
int readWriteBuffersCount = 0;
|
||||
int readOnlyBuffersCount = 0;
|
||||
|
||||
foreach (FieldInfo fieldInfo in ShaderFields)
|
||||
{
|
||||
@@ -124,11 +125,23 @@ namespace ComputeSharp.Shaders.Translation
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
readWriteBuffers.Add((GraphicsResource)fieldValue);
|
||||
buffers.Add((GraphicsResource)fieldValue);
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType);
|
||||
processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++);
|
||||
}
|
||||
else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType))
|
||||
{
|
||||
// Read only buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, readOnlyBuffersCount);
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
buffers.Add((GraphicsResource)fieldValue);
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, false);
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(fieldType))
|
||||
{
|
||||
// Static scalar field
|
||||
@@ -141,7 +154,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
}
|
||||
|
||||
RootParameters = descriptorRanges.Select(range => new RootParameter(ShaderVisibility.All, range)).ToArray();
|
||||
ReadWriteBuffers = readWriteBuffers;
|
||||
Buffers = buffers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする