Merge pull request #19 from Sergio0694/feature_readonly-buffer
Readonly buffers, mapped to shader resources
このコミットが含まれているのは:
23ファイル変更
+557
-318
検索条件に一致するファイルはありません
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics.Contracts;
|
||||
using SharpDX.Direct3D12;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
@@ -17,8 +17,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// <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) { }
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
private protected HlslBuffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device, size, sizeInBytes, bufferType) { }
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="HlslBuffer{T}"/> instance and returns an array
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed structured buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public abstract class HlslStructuredBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslStructuredBuffer{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="bufferType">The buffer type for the current buffer</param>
|
||||
internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf<T>(), bufferType) { }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void GetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(this, 0, transferBuffer, 0, SizeInBytes);
|
||||
copyCommandList.Flush();
|
||||
|
||||
transferBuffer.Map(0);
|
||||
MemoryHelper.Copy(transferBuffer.MappedResource, span);
|
||||
transferBuffer.Unmap(0);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(Span<T> span)
|
||||
{
|
||||
using Buffer<T> transferBuffer = new Buffer<T>(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer);
|
||||
|
||||
transferBuffer.Map(0);
|
||||
MemoryHelper.Copy(span, transferBuffer.MappedResource);
|
||||
transferBuffer.Unmap(0);
|
||||
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(transferBuffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void SetData(HlslBuffer<T> buffer)
|
||||
{
|
||||
if (!buffer.IsPaddingPresent)
|
||||
{
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
|
||||
|
||||
copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes);
|
||||
copyCommandList.Flush();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
-21
@@ -1,6 +1,8 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using SharpDX.Direct3D12;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
@@ -17,29 +19,36 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
/// <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, int sizeInBytes, HeapType heapType) : base(device)
|
||||
/// <param name="bufferType">The buffer type for the current buffer</param>
|
||||
internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) : base(device)
|
||||
{
|
||||
Size = size;
|
||||
SizeInBytes = sizeInBytes; // Not necessarily a multiple of the element size, as there could be padding
|
||||
ElementSizeInBytes = Unsafe.SizeOf<T>();
|
||||
HeapType = heapType;
|
||||
BufferType = bufferType;
|
||||
PaddedElementSizeInBytes = sizeInBytes / size;
|
||||
|
||||
ResourceFlags flags = heapType == HeapType.Default ? ResourceFlags.AllowUnorderedAccess : ResourceFlags.None;
|
||||
ResourceDescription description = ResourceDescription.Buffer(SizeInBytes, flags);
|
||||
ResourceStates resourceStates = heapType switch
|
||||
// Determine the right heap type and flags
|
||||
(HeapType heapType, ResourceFlags flags, ResourceStates states) = bufferType switch
|
||||
{
|
||||
HeapType.Upload => ResourceStates.GenericRead,
|
||||
HeapType.Readback => ResourceStates.CopyDestination,
|
||||
_ => ResourceStates.Common
|
||||
BufferType.Constant => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead),
|
||||
BufferType.ReadOnly => (HeapType.Default, ResourceFlags.None, ResourceStates.Common),
|
||||
BufferType.ReadWrite => (HeapType.Default, ResourceFlags.AllowUnorderedAccess, ResourceStates.Common),
|
||||
BufferType.ReadBack => (HeapType.Readback, ResourceFlags.None, ResourceStates.CopyDestination),
|
||||
BufferType.Transfer => (HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead),
|
||||
_ => throw new ArgumentException($"Invalid buffer type {bufferType}", nameof(bufferType))
|
||||
};
|
||||
|
||||
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, resourceStates);
|
||||
// Create the native resource
|
||||
ResourceDescription description = ResourceDescription.Buffer(sizeInBytes, flags);
|
||||
NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states);
|
||||
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = heapType switch
|
||||
// Create the resource handles, if needed
|
||||
(NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = bufferType switch
|
||||
{
|
||||
HeapType.Default => CreateUnorderedAccessView(),
|
||||
HeapType.Upload => CreateConstantBufferView(),
|
||||
BufferType.Constant => CreateConstantBufferView(),
|
||||
BufferType.ReadOnly => CreateShaderResourceView(),
|
||||
BufferType.ReadWrite => CreateUnorderedAccessView(),
|
||||
_ => default
|
||||
};
|
||||
}
|
||||
@@ -60,9 +69,19 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
protected int ElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the heap type being targeted by the current buffer
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// </summary>
|
||||
internal HeapType HeapType { get; }
|
||||
internal int PaddedElementSizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not there is some padding between elements in the current buffer
|
||||
/// </summary>
|
||||
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the buffer type for the current <see cref="Buffer{T}"/> instance
|
||||
/// </summary>
|
||||
internal BufferType BufferType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a constant buffer
|
||||
@@ -86,6 +105,22 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView()
|
||||
{
|
||||
(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate();
|
||||
|
||||
ShaderResourceViewDescription description = new ShaderResourceViewDescription
|
||||
{
|
||||
Shader4ComponentMapping = 5768,
|
||||
Dimension = ShaderResourceViewDimension.Buffer,
|
||||
Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle);
|
||||
|
||||
return (cpuHandle, gpuHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the descriptors for a read write buffer
|
||||
/// </summary>
|
||||
@@ -98,11 +133,7 @@ namespace ComputeSharp.Graphics.Buffers
|
||||
UnorderedAccessViewDescription description = new UnorderedAccessViewDescription
|
||||
{
|
||||
Dimension = UnorderedAccessViewDimension.Buffer,
|
||||
Buffer =
|
||||
{
|
||||
ElementCount = Size,
|
||||
StructureByteStride = ElementSizeInBytes
|
||||
}
|
||||
Buffer = { ElementCount = Size, StructureByteStride = ElementSizeInBytes }
|
||||
};
|
||||
|
||||
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
/// <summary>
|
||||
/// 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 ConstantBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBuffer{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 ConstantBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, BufferType.Constant) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <typeparamref name="T"/> value from the current constant buffer
|
||||
/// </summary>
|
||||
/// <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 ConstantBuffer<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();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace ComputeSharp.Graphics.Buffers.Enums
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see langword="enum"/> that indicates the type of a given HLSL buffer
|
||||
/// </summary>
|
||||
internal enum BufferType
|
||||
{
|
||||
/// <summary>
|
||||
/// A constant buffer, with items aligned to 16 bytes, used for individual values or very small arrays, mapped to <see cref="SharpDX.Direct3D12.HeapType.Upload"/>
|
||||
/// </summary>
|
||||
Constant,
|
||||
|
||||
/// <summary>
|
||||
/// A readonly buffer, that can store arbitrary arrays of values, mapped to <see cref="SharpDX.Direct3D12.HeapType.Default"/>
|
||||
/// </summary>
|
||||
ReadOnly,
|
||||
|
||||
/// <summary>
|
||||
/// A read write buffer, that can store arbitrary arrays with both read and write access, mapped to <see cref="SharpDX.Direct3D12.HeapType.Default"/>
|
||||
/// </summary>
|
||||
ReadWrite,
|
||||
|
||||
/// <summary>
|
||||
/// A readback buffer, used as temporary buffer to read data back from read write buffers, mapped to <see cref="SharpDX.Direct3D12.HeapType.Readback"/>
|
||||
/// </summary>
|
||||
ReadBack,
|
||||
|
||||
/// <summary>
|
||||
/// A transfer buffer, used as a temporary buffer to set data to buffers in the default heap, mapped to <see cref="SharpDX.Direct3D12.HeapType.Upload"/>
|
||||
/// </summary>
|
||||
Transfer
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,152 @@ namespace ComputeSharp
|
||||
/// </summary>
|
||||
public static class BufferExtensions
|
||||
{
|
||||
/// <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="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
{
|
||||
return device.AllocateConstantBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <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="array">The input 2D <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input 2D array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, T[,] array) where T : unmanaged
|
||||
{
|
||||
fixed (T* p = array)
|
||||
{
|
||||
Span<T> span = new Span<T>(p, array.Length);
|
||||
return device.AllocateConstantBuffer(span);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
{
|
||||
ConstantBuffer<T> buffer = new ConstantBuffer<T>(device, span.Length);
|
||||
buffer.SetData(span);
|
||||
|
||||
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="ConstantBuffer{T}"/> instance with the contents of the input <see cref="HlslBuffer{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConstantBuffer<T> AllocateConstantBuffer<T>(this GraphicsDevice device, HlslBuffer<T> buffer) where T : unmanaged
|
||||
{
|
||||
ConstantBuffer<T> constantBuffer = new ConstantBuffer<T>(device, buffer.Size);
|
||||
constantBuffer.SetData(buffer);
|
||||
|
||||
return constantBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly 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="size">The size of the buffer to allocate</param>
|
||||
/// <returns>A zeroed <see cref="ReadOnlyBuffer{T}"/> instance of size <paramref name="size"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, int size) where T : unmanaged
|
||||
{
|
||||
return new ReadOnlyBuffer<T>(device, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly 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="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadOnlyBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly 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="array">The input 2D <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input 2D array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[,] array) where T : unmanaged
|
||||
{
|
||||
fixed (T* p = array)
|
||||
{
|
||||
Span<T> span = new Span<T>(p, array.Length);
|
||||
return device.AllocateReadOnlyBuffer(span);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly 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="span">The input <see cref="Span{T}"/> with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A read write <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input <see cref="Span{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer<T> buffer = new ReadOnlyBuffer<T>(device, span.Length);
|
||||
buffer.SetData(span);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new readonly 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="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> readWriteBuffer = new ReadOnlyBuffer<T>(device, buffer.Size);
|
||||
readWriteBuffer.SetData(buffer);
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new read write buffer with the specified parameters
|
||||
/// </summary>
|
||||
@@ -91,71 +237,5 @@ namespace ComputeSharp
|
||||
|
||||
return readWriteBuffer;
|
||||
}
|
||||
|
||||
/// <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="array">The input <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[] array) where T : unmanaged
|
||||
{
|
||||
return device.AllocateReadOnlyBuffer(array.AsSpan());
|
||||
}
|
||||
|
||||
/// <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="array">The input 2D <typeparamref name="T"/> array with the data to copy on the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance with the contents of the input 2D array</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, T[,] array) where T : unmanaged
|
||||
{
|
||||
fixed (T* p = array)
|
||||
{
|
||||
Span<T> span = new Span<T>(p, array.Length);
|
||||
return device.AllocateReadOnlyBuffer(span);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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="span">The input <see cref="Span{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="Span{T}"/></returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ReadOnlyBuffer<T> AllocateReadOnlyBuffer<T>(this GraphicsDevice device, Span<T> span) where T : unmanaged
|
||||
{
|
||||
ReadOnlyBuffer<T> buffer = new ReadOnlyBuffer<T>(device, span.Length);
|
||||
buffer.SetData(span);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,15 +23,15 @@ namespace ComputeSharp.Graphics.Buffers.Extensions
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the buffer</param>
|
||||
/// <param name="data">The input <see cref="object"/> to copy to the allocated buffer</param>
|
||||
/// <returns>A constant <see cref="ReadOnlyBuffer{T}"/> instance (as a <see cref="GraphicsResource"/>) with the input data</returns>
|
||||
/// <returns>A constant <see cref="ConstantBuffer{T}"/> instance (as a <see cref="GraphicsResource"/>) with the input data</returns>
|
||||
[Pure]
|
||||
public static GraphicsResource AllocateReadOnlyBufferFromReflectedSingleValue(this GraphicsDevice device, object data)
|
||||
public static GraphicsResource AllocateConstantBufferFromReflectedSingleValue(this GraphicsDevice device, object data)
|
||||
{
|
||||
Type dataType = data.GetType();
|
||||
if (!TypeMapping.TryGetValue(dataType, out var info))
|
||||
{
|
||||
// Get the generic buffer constructor
|
||||
Type bufferType = typeof(ReadOnlyBuffer<>).MakeGenericType(dataType);
|
||||
Type bufferType = typeof(ConstantBuffer<>).MakeGenericType(dataType);
|
||||
info.Constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First();
|
||||
|
||||
// Create the reusable array
|
||||
|
||||
@@ -1,128 +1,27 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> representing a typed read write buffer stored on GPU memory
|
||||
/// A <see langword="class"/> representing a typed readonly buffer stored on GPU memory
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffer</typeparam>
|
||||
public sealed class ReadOnlyBuffer<T> : HlslBuffer<T> where T : unmanaged
|
||||
public sealed class ReadOnlyBuffer<T> : HlslStructuredBuffer<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, size * (Unsafe.SizeOf<T>() / 16 + 1) * 16, HeapType.Upload)
|
||||
{
|
||||
PaddedElementSizeInBytes = SizeInBytes / Size;
|
||||
IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes;
|
||||
}
|
||||
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, BufferType.ReadOnly) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of the current buffer
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write 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
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the value to get</param>
|
||||
/// <param name="i">The index of the value to get or set</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();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using SharpDX.Direct3D12;
|
||||
using CommandList = ComputeSharp.Graphics.Commands.CommandList;
|
||||
using ComputeSharp.Graphics.Buffers.Enums;
|
||||
|
||||
namespace ComputeSharp.Graphics.Buffers
|
||||
{
|
||||
@@ -11,14 +8,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> : HlslBuffer<T> where T : unmanaged
|
||||
public sealed class ReadWriteBuffer<T> : HlslStructuredBuffer<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, size * Unsafe.SizeOf<T>(), HeapType.Default) { }
|
||||
internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, BufferType.ReadWrite) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
|
||||
@@ -30,48 +27,5 @@ 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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/// <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();
|
||||
}
|
||||
else SetDataWithCpuBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(Matrix4x4).FullName] = "float4x4",
|
||||
[typeof(ThreadIds).FullName] = "uint3",
|
||||
[typeof(ReadOnlyBuffer<>).FullName] = "StructuredBuffer",
|
||||
[typeof(ReadWriteBuffer<>).FullName] = "RWStructuredBuffer"
|
||||
};
|
||||
|
||||
@@ -54,13 +55,13 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
type == typeof(Matrix4x4);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> value
|
||||
/// Checks whether or not the input type is a <see cref="ConstantBuffer{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="ReadWriteBuffer{T}"/> instance</returns>
|
||||
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ConstantBuffer{T}"/> instance</returns>
|
||||
[Pure]
|
||||
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
|
||||
public static bool IsConstantBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(ConstantBuffer<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> value
|
||||
@@ -69,7 +70,16 @@ namespace ComputeSharp.Shaders.Mappings
|
||||
/// <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<>);
|
||||
type.GetGenericTypeDefinition() == typeof(ReadOnlyBuffer<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{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="ReadWriteBuffer{T}"/> instance</returns>
|
||||
[Pure]
|
||||
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
|
||||
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given <see cref="Type"/> is an HLSL known type
|
||||
|
||||
+3
-3
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a shader field
|
||||
/// </summary>
|
||||
internal abstract class FieldInfoBase
|
||||
internal abstract class FieldInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the current field
|
||||
@@ -16,11 +16,11 @@
|
||||
public string FieldName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="FieldInfoBase"/> instance with the specified parameters
|
||||
/// Creates a new <see cref="FieldInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="fieldType">The type of the current field</param>
|
||||
/// <param name="fieldName">The name of the current field</param>
|
||||
protected FieldInfoBase(string fieldType, string fieldName)
|
||||
protected FieldInfo(string fieldType, string fieldName)
|
||||
{
|
||||
FieldType = fieldType;
|
||||
FieldName = fieldName;
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a shader buffer field
|
||||
/// </summary>
|
||||
internal abstract class HlslBufferInfo : FieldInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the index of the current buffer, relative to its own type
|
||||
/// </summary>
|
||||
public int BufferIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslBufferInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <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 buffer field</param>
|
||||
protected HlslBufferInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName)
|
||||
{
|
||||
BufferIndex = bufferIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a constant buffer field
|
||||
/// </summary>
|
||||
internal sealed class ConstantBufferFieldInfo : FieldInfoBase
|
||||
internal sealed class ConstantBufferFieldInfo : HlslBufferInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not the current <see cref="FieldInfoBase"/> instance represents a constant buffer (always <see langword="true"/>)
|
||||
/// Gets whether or not the current <see cref="FieldInfo"/> instance represents a constant buffer (always <see langword="true"/>)
|
||||
/// </summary>
|
||||
public bool IsConstantBuffer { get; } = true;
|
||||
|
||||
@@ -17,11 +17,6 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// </summary>
|
||||
public bool IsSingleValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the current constant buffer field
|
||||
/// </summary>
|
||||
public int ConstantBufferIndex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ConstantBufferFieldInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
@@ -29,9 +24,8 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// <param name="fieldName">The name of the current field</param>
|
||||
/// <param name="bufferIndex">The index of the current constant buffer field</param>
|
||||
/// <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)
|
||||
public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex, bool singleValue) : base(fieldType, fieldName, bufferIndex)
|
||||
{
|
||||
ConstantBufferIndex = bufferIndex;
|
||||
IsSingleValue = singleValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a readonly buffer field
|
||||
/// </summary>
|
||||
internal sealed class ReadOnlyBufferFieldInfo : HlslBufferInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not the current <see cref="FieldInfo"/> instance represents a readonly buffer (always <see langword="true"/>)
|
||||
/// </summary>
|
||||
public bool IsReadOnlyBuffer { get; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadOnlyBufferFieldInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <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 readonly buffer field</param>
|
||||
public ReadOnlyBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { }
|
||||
}
|
||||
}
|
||||
@@ -5,27 +5,19 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a read write buffer field
|
||||
/// </summary>
|
||||
internal sealed class ReadWriteBufferFieldInfo : FieldInfoBase
|
||||
internal sealed class ReadWriteBufferFieldInfo : HlslBufferInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not the current <see cref="FieldInfoBase"/> instance represents a read write buffer (always <see langword="true"/>)
|
||||
/// Gets whether or not the current <see cref="FieldInfo"/> instance represents a read write buffer (always <see langword="true"/>)
|
||||
/// </summary>
|
||||
public bool IsReadWriteBuffer { get; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the current read write buffer field
|
||||
/// </summary>
|
||||
public int ReadWriteBufferIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReadWriteBufferFieldInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <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 read write buffer field</param>
|
||||
public ReadWriteBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName)
|
||||
{
|
||||
ReadWriteBufferIndex = bufferIndex;
|
||||
}
|
||||
public ReadWriteBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { }
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a static scalar field
|
||||
/// </summary>
|
||||
internal sealed class StaticScalarFieldInfo : FieldInfoBase
|
||||
internal sealed class StaticScalarFieldInfo : FieldInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether or not the current <see cref="FieldInfoBase"/> instance represents a static scalar value (always <see langword="true"/>)
|
||||
/// Gets whether or not the current <see cref="FieldInfo"/> instance represents a static scalar value (always <see langword="true"/>)
|
||||
/// </summary>
|
||||
public bool IsStaticScalarField { get; } = true;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace ComputeSharp.Shaders.Renderer.Models
|
||||
/// <summary>
|
||||
/// Gets the list of fields being present in the current shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<FieldInfoBase> FieldsList { get; set; }
|
||||
public IReadOnlyList<FieldInfo> FieldsList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the total number of threads to run on the X axis
|
||||
|
||||
@@ -5,14 +5,18 @@ static {{&FieldType}} {{FieldName}} = {{FieldValue}};
|
||||
{{/IsStaticScalarField}}
|
||||
{{!Constant buffer}}
|
||||
{{#IsConstantBuffer}}
|
||||
cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}})
|
||||
cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}})
|
||||
{
|
||||
{{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}};
|
||||
}
|
||||
{{/IsConstantBuffer}}
|
||||
{{!Readonly buffer}}
|
||||
{{#IsReadOnlyBuffer}}
|
||||
{{&FieldType}} {{FieldName}} : register(t{{BufferIndex}});
|
||||
{{/IsReadOnlyBuffer}}
|
||||
{{!Read write buffer}}
|
||||
{{#IsReadWriteBuffer}}
|
||||
{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{ReadWriteBufferIndex}});
|
||||
{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}});
|
||||
{{/IsReadWriteBuffer}}
|
||||
|
||||
{{/FieldsList}}
|
||||
@@ -21,8 +25,8 @@ cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}})
|
||||
[NumThreads({{NumThreadsX}}, {{NumThreadsY}}, {{NumThreadsZ}})]
|
||||
void CSMain(uint3 {{ThreadsIdsVariableName}} : SV_DispatchThreadId)
|
||||
{
|
||||
if ({{ThreadsIdsVariableName}}.x < {{ThreadsX}} &&
|
||||
{{ThreadsIdsVariableName}}.y < {{ThreadsY}} &&
|
||||
{{ThreadsIdsVariableName}}.z < {{ThreadsZ}})
|
||||
if ({{ThreadsIdsVariableName}}.x < {{ThreadsX}} &&
|
||||
{{ThreadsIdsVariableName}}.y < {{ThreadsY}} &&
|
||||
{{ThreadsIdsVariableName}}.z < {{ThreadsZ}})
|
||||
{{&ShaderBody}}
|
||||
}
|
||||
@@ -99,7 +99,7 @@ namespace ComputeSharp.Shaders
|
||||
List<GraphicsResource> buffers = new List<GraphicsResource>();
|
||||
foreach (var variable in shaderLoader.CapturedConstantBufferValues)
|
||||
{
|
||||
GraphicsResource resource = device.AllocateReadOnlyBufferFromReflectedSingleValue(variable.Value);
|
||||
GraphicsResource resource = device.AllocateConstantBufferFromReflectedSingleValue(variable.Value);
|
||||
commandList.SetComputeRootDescriptorTable(variable.Index, resource);
|
||||
buffers.Add(resource);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using SharpDX.Direct3D12;
|
||||
using FieldInfo = ComputeSharp.Shaders.Renderer.Models.Fields.Abstract.FieldInfo;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field is uninitialized
|
||||
|
||||
@@ -39,7 +40,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// The sequence of fields in the targeted closure
|
||||
/// </summary>
|
||||
private readonly IReadOnlyList<FieldInfo> ShaderFields;
|
||||
private readonly IReadOnlyList<System.Reflection.FieldInfo> ShaderFields;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderLoader"/> with the specified parameters
|
||||
@@ -68,12 +69,12 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues { get; private set; }
|
||||
|
||||
private readonly List<FieldInfoBase> _FieldsInfo = new List<FieldInfoBase>();
|
||||
private readonly List<FieldInfo> _FieldsInfo = new List<FieldInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of <see cref="FieldInfoBase"/> items for the shader fields
|
||||
/// Gets the collection of <see cref="FieldInfo"/> items for the shader fields
|
||||
/// </summary>
|
||||
public IReadOnlyList<FieldInfoBase> FieldsInfo => _FieldsInfo;
|
||||
public IReadOnlyList<FieldInfo> FieldsInfo => _FieldsInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the <see cref="ThreadIds"/> variable used as input for the shader method
|
||||
@@ -113,18 +114,42 @@ namespace ComputeSharp.Shaders.Translation
|
||||
List<DescriptorRange> descriptorRanges = new List<DescriptorRange>();
|
||||
List<(int, GraphicsResource)> buffers = new List<(int, GraphicsResource)>();
|
||||
List<(int, object)> variables = new List<(int, object)>();
|
||||
int readWriteBuffersCount = 0;
|
||||
int constantBuffersCount = 0;
|
||||
int readOnlyBuffersCount = 0;
|
||||
int readWriteBuffersCount = 0;
|
||||
|
||||
foreach (FieldInfo fieldInfo in ShaderFields)
|
||||
foreach (System.Reflection.FieldInfo fieldInfo in ShaderFields)
|
||||
{
|
||||
Type fieldType = fieldInfo.FieldType;
|
||||
string fieldName = fieldInfo.Name;
|
||||
object fieldValue = fieldInfo.GetValue(ShaderInstance);
|
||||
FieldInfoBase processedFieldInfo;
|
||||
FieldInfo processedFieldInfo;
|
||||
|
||||
// Read write buffer
|
||||
if (HlslKnownTypes.IsReadWriteBufferType(fieldType))
|
||||
// Constant buffer
|
||||
if (HlslKnownTypes.IsConstantBufferType(fieldType))
|
||||
{
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount);
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue));
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++, false);
|
||||
}
|
||||
else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType))
|
||||
{
|
||||
// Root parameter for a readonly buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, readOnlyBuffersCount);
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Reference to the underlying buffer
|
||||
buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue));
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType);
|
||||
processedFieldInfo = new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++);
|
||||
}
|
||||
else if (HlslKnownTypes.IsReadWriteBufferType(fieldType))
|
||||
{
|
||||
// Root parameter for a read write buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount);
|
||||
@@ -136,22 +161,10 @@ namespace ComputeSharp.Shaders.Translation
|
||||
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((descriptorRanges.Count - 1, (GraphicsResource)fieldValue));
|
||||
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, false);
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(fieldType))
|
||||
{
|
||||
// Read only buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, readOnlyBuffersCount);
|
||||
// Constant buffer
|
||||
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount);
|
||||
descriptorRanges.Add(range);
|
||||
|
||||
// Register the captured value
|
||||
@@ -159,7 +172,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
|
||||
// Constant buffer field
|
||||
string typeName = HlslKnownTypes.GetMappedName(fieldType);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++, true);
|
||||
processedFieldInfo = new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++, true);
|
||||
}
|
||||
else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}");
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace ComputeSharp.NetCore.Tests.Internals
|
||||
{
|
||||
object value = 77;
|
||||
|
||||
using ReadOnlyBuffer<int> buffer = (ReadOnlyBuffer<int>)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value);
|
||||
using ConstantBuffer<int> buffer = (ConstantBuffer<int>)Gpu.Default.AllocateConstantBufferFromReflectedSingleValue(value);
|
||||
int[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result.Length == 1);
|
||||
@@ -26,7 +26,7 @@ namespace ComputeSharp.NetCore.Tests.Internals
|
||||
{
|
||||
object value = 3.14f;
|
||||
|
||||
using ReadOnlyBuffer<float> buffer = (ReadOnlyBuffer<float>)Gpu.Default.AllocateReadOnlyBufferFromReflectedSingleValue(value);
|
||||
using ConstantBuffer<float> buffer = (ConstantBuffer<float>)Gpu.Default.AllocateConstantBufferFromReflectedSingleValue(value);
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result.Length == 1);
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace ComputeSharp.NetCore.Tests
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> buffer = Gpu.Default.AllocateReadOnlyBuffer(array);
|
||||
using ConstantBuffer<float> buffer = Gpu.Default.AllocateConstantBuffer(array);
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(array.AsSpan().ContentEquals(result));
|
||||
@@ -52,7 +52,7 @@ namespace ComputeSharp.NetCore.Tests
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(array);
|
||||
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
|
||||
using ReadWriteBuffer<float> destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
@@ -68,7 +68,7 @@ namespace ComputeSharp.NetCore.Tests
|
||||
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);
|
||||
using ConstantBuffer<float> destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
@@ -82,8 +82,8 @@ namespace ComputeSharp.NetCore.Tests
|
||||
{
|
||||
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);
|
||||
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
|
||||
using ConstantBuffer<float> destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer);
|
||||
|
||||
float[] sourceResult = sourceBuffer.GetData();
|
||||
float[] destinationResult = destinationBuffer.GetData();
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace ComputeSharp.NetCore.Tests
|
||||
{
|
||||
float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
|
||||
|
||||
using ReadOnlyBuffer<float> input = Gpu.Default.AllocateReadOnlyBuffer(source);
|
||||
using ConstantBuffer<float> input = Gpu.Default.AllocateConstantBuffer(source);
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(source.Length);
|
||||
|
||||
Action<ThreadIds> action = id => buffer[id.X] = input[id.X];
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする