diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 703bc324..557e8428 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -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 /// /// 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 heap type for the current buffer - 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(); - 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 }; } + /// + /// Gets the size of the current buffer, as in the number of values it contains + /// + public int Size { get; } + + /// + /// Gets the size in bytes of the current buffer + /// + protected int SizeInBytes { get; } + + /// + /// Gets the size in bytes of each value contained in the buffer + /// + protected int ElementSizeInBytes { get; } + /// /// Creates the descriptors for a constant buffer /// @@ -88,88 +102,9 @@ namespace ComputeSharp.Graphics.Buffers return (cpuHandle, gpuHandle); } - /// - /// Gets the size of the current buffer, as in the number of values it contains - /// - public int Size { get; } - - /// - /// Gets the size in bytes of the current buffer - /// - public int SizeInBytes { get; } - - /// - /// Gets the size in bytes of each value contained in the buffer - /// - public int ElementSizeInBytes { get; } - /// /// Gets the heap type being targeted by the current buffer /// public HeapType HeapType { get; } - - /// - /// Reads the contents of the current instance and returns an array - /// - /// A array with the contents of the current buffer - [Pure] - public T[] GetData() - { - T[] data = new T[Size]; - GetData(data); - - 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) - { - // 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 readbackBaffer = new Buffer(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); - } - } - - /// - /// Writes the contents of a given to the current instance - /// - /// The input to read data from - public void SetData(Span 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 uploadBuffer = new Buffer(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); - } - } } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index c86d4084..f0f2a2a0 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -1,4 +1,6 @@ using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Abstract; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Buffers @@ -7,14 +9,22 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed read write buffer stored on GPU memory /// /// The type of items stored on the buffer - public sealed class ReadOnlyBuffer : Buffer where T : unmanaged + public sealed class ReadOnlyBuffer : HlslBuffer 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 ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Upload) { } + internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf() / 16 + 1), HeapType.Upload) + { + PaddedElementSizeInBytes = SizeInBytes / Size; + } + + /// + /// Gets the size in bytes of the current buffer + /// + private int PaddedElementSizeInBytes { get; } /// /// Gets a single value from the current read write buffer @@ -22,5 +32,17 @@ namespace ComputeSharp.Graphics.Buffers /// 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[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + + /// + public override void GetData(Span span) + { + throw new NotImplementedException(); + } + + /// + public override void SetData(Span span) + { + throw new NotImplementedException(); + } } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index ded6d1e9..2a2a4c9e 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,4 +1,6 @@ using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Buffers.Abstract; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics.Buffers @@ -7,14 +9,17 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed read write buffer stored on GPU memory /// /// The type of items stored on the buffer - public sealed class ReadWriteBuffer : Buffer where T : unmanaged + public sealed class ReadWriteBuffer : HlslBuffer 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 ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, HeapType.Default) { } + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, size * Unsafe.SizeOf(), HeapType.Default) + { + + } /// /// Gets or sets a single value from the current read write buffer @@ -26,5 +31,17 @@ 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"); } + + /// + public override void GetData(Span span) + { + throw new NotImplementedException(); + } + + /// + public override void SetData(Span span) + { + throw new NotImplementedException(); + } } }