diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer2{T}.cs new file mode 100644 index 00000000..298d19a8 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer2{T}.cs @@ -0,0 +1,115 @@ +using System; +using System.Buffers; +using System.Diagnostics.Contracts; +using ComputeSharp.Graphics.Buffers.Enums; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// A representing a typed buffer stored on GPU memory, that can be used from an HLSL shader + /// + /// The type of items stored on the buffer + public abstract class HlslBuffer2 : Buffer2 where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + /// The size in bytes for the current buffer + /// The buffer type for the current buffer + private protected HlslBuffer2(GraphicsDevice2 device, int size, int sizeInBytes, BufferType bufferType) + : base(device, size, sizeInBytes, bufferType) + { + } + + /// + /// Reads the contents of the current instance and returns an array + /// + /// A array with the contents of the current buffer + [Pure] + public T[] GetData() => GetData(0, Size); + + /// + /// Reads the contents of the current instance in a given range and returns an array + /// + /// The offset to start reading data from + /// The number of items to read + /// A array with the contents of the specified range from the current buffer + [Pure] + public T[] GetData(int offset, int count) + { + T[] data = new T[count]; + GetData(data, offset, count); + + return data; + } + + /// + /// Reads the contents of the current instance and writes them into a target + /// + /// The input to write data to + public void GetData(Span span) => GetData(span, 0, Size); + + /// + /// Reads the contents of the specified range from the current instance and writes them into a target + /// + /// The input to write data to + /// The offset to start reading data from + /// The number of items to read + public abstract void GetData(Span span, int offset, int count); + + /// + /// Writes the contents of a given array to the current instance + /// + /// The input array to read data from + public void SetData(T[] array) => SetData(array.AsSpan()); + + /// + /// Writes the contents of a given array to a specified area of the current instance + /// + /// The input array to read data from + /// The offset to start writing data to + /// The number of items to write + public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count); + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public void SetData(ReadOnlySpan span) => SetData(span, 0, Size); + + /// + /// Writes the contents of a given to a specified area of the current instance + /// + /// The input to read data from + /// The offset to start writing data to + /// The number of items to write + public abstract void SetData(ReadOnlySpan span, int offset, int count); + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public abstract void SetData(HlslBuffer2 buffer); + + /// + /// Writes the contents of a given to the current instance, using a temporary CPU buffer + /// + /// The input to read data from + protected void SetDataWithCpuBuffer(HlslBuffer2 buffer) + { + // Create a temporary array + T[] array = ArrayPool.Shared.Rent(buffer.Size); + Span 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.Shared.Return(array); + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer2{T}.cs new file mode 100644 index 00000000..0333b579 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer2{T}.cs @@ -0,0 +1,74 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Helpers; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// A representing a typed structured buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public abstract class HlslStructuredBuffer2 : HlslBuffer2 + where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + /// The buffer type for the current buffer + internal HlslStructuredBuffer2(GraphicsDevice2 device, int size, BufferType bufferType) + : base(device, size, size * Unsafe.SizeOf(), bufferType) + { + } + + /// + public override unsafe void GetData(Span span, int offset, int count) + { + using Buffer2 transferBuffer = new Buffer2(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.ReadBack); + + using (CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) + { + copyCommandList.CopyBufferRegion(D3D12Resource, offset * ElementSizeInBytes, transferBuffer.D3D12Resource, 0, count * ElementSizeInBytes); + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using MappedResource resource = transferBuffer.MapResource(); + + MemoryHelper.Copy(resource.Pointer, 0, span, 0, count); + } + + /// + public override unsafe void SetData(ReadOnlySpan span, int offset, int count) + { + using Buffer2 transferBuffer = new Buffer2(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); + + using (MappedResource resource = transferBuffer.MapResource()) + { + MemoryHelper.Copy(span, resource.Pointer, 0, count); + } + + using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.CopyBufferRegion(transferBuffer.D3D12Resource, 0, D3D12Resource, offset * ElementSizeInBytes, count * ElementSizeInBytes); + copyCommandList.ExecuteAndWaitForCompletion(); + } + + /// + public override unsafe void SetData(HlslBuffer2 buffer) + { + if (!buffer.IsPaddingPresent) + { + // Directly copy the input buffer + using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes); + copyCommandList.ExecuteAndWaitForCompletion(); + } + else SetDataWithCpuBuffer(buffer); + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 0ccdafc6..1a60a5f1 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -11,7 +11,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// A representing a typed structured buffer stored on GPU memory /// /// The type of items stored on the buffer - public abstract class HlslStructuredBuffer : HlslBuffer where T : unmanaged + public abstract class HlslStructuredBuffer : HlslBuffer + where T : unmanaged { /// /// Creates a new instance with the specified parameters diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer2{T}.cs new file mode 100644 index 00000000..b2bda877 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer2{T}.cs @@ -0,0 +1,111 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Helpers; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; + +namespace ComputeSharp +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ConstantBuffer2 : HlslBuffer2 + where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ConstantBuffer2(GraphicsDevice2 device, int size) + : base(device, size, size * GetPaddedSize(), BufferType.Constant) + { + } + + /// + /// Gets the right padded size for elements to store in the current instance + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int GetPaddedSize() + { + int size = Unsafe.SizeOf(); + + return (size + 15) & ~15; + } + + /// + /// Gets a single value from the current constant buffer + /// + /// The index of the value to get + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer)}[int]"); + + /// + public override unsafe void GetData(Span span, int offset, int count) + { + using MappedResource resource = MapResource(); + + if (IsPaddingPresent) + { + ref T spanRef = ref MemoryMarshal.GetReference(span); + ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer); + + // Move to the initial ref + resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); + + for (var i = 0; i < count; i++) + { + ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); + + Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef); + } + } + else MemoryHelper.Copy(resource.Pointer, offset, span, 0, count); + } + + /// + public override unsafe void SetData(ReadOnlySpan span, int offset, int count) + { + using MappedResource resource = MapResource(); + + if (IsPaddingPresent) + { + ref T spanRef = ref MemoryMarshal.GetReference(span); + ref byte resourceRef = ref Unsafe.AsRef((void*)resource.Pointer); + + // Move to the initial offset + resourceRef = ref Unsafe.Add(ref resourceRef, offset * GetPaddedSize()); + + for (var i = 0; i < count; i++) + { + ref byte targetRef = ref Unsafe.Add(ref resourceRef, i * GetPaddedSize()); + + Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i); + } + } + else MemoryHelper.Copy(span, resource.Pointer, offset, count); + } + + /// + public override unsafe void SetData(HlslBuffer2 buffer) + { + if (buffer is ConstantBuffer2) + { + // Directly copy the input buffer, if possible + using CommandList2 copyCommandList = new CommandList2(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.CopyBufferRegion(buffer.D3D12Resource, 0, D3D12Resource, 0, SizeInBytes); + copyCommandList.ExecuteAndWaitForCompletion(); + } + else SetDataWithCpuBuffer(buffer); + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer2{T}.cs new file mode 100644 index 00000000..e4af7c6e --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer2{T}.cs @@ -0,0 +1,32 @@ +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadOnlyBuffer2 : HlslStructuredBuffer2 + where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadOnlyBuffer2(GraphicsDevice2 device, int size) + : base(device, size, BufferType.ReadOnly) + { + } + + /// + /// Gets a single value from the current read write buffer + /// + /// The index of the value to get + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer)}[int]"); + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer2{T}.cs new file mode 100644 index 00000000..bea19683 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer2{T}.cs @@ -0,0 +1,32 @@ +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; + +namespace ComputeSharp +{ + /// + /// A representing a typed read write buffer stored on GPU memory + /// + /// The type of items stored on the buffer + public sealed class ReadWriteBuffer2 : HlslStructuredBuffer2 + where T : unmanaged + { + /// + /// Creates a new instance with the specified parameters + /// + /// The associated with the current instance + /// The number of items to store in the current buffer + internal ReadWriteBuffer2(GraphicsDevice2 device, int size) + : base(device, size, BufferType.ReadWrite) + { + } + + /// + /// Gets or sets a single value from the current read write buffer + /// + /// The index of the value to get or set + /// This API can only be used from a compute shader, and will always throw if used anywhere else + public ref T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer)}[int]"); + } +}