diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer.cs new file mode 100644 index 00000000..2a7e2d5b --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer.cs @@ -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 +{ + /// + /// 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 + { + /// + /// 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 HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf(), bufferType) { } + + /// + public override void GetData(Span span) + { + using Buffer transferBuffer = new Buffer(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); + } + + /// + public override void SetData(Span span) + { + using Buffer transferBuffer = new Buffer(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(); + } + + /// + public override void SetData(HlslBuffer buffer) + { + if (buffer is ConstantBuffer 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); + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index 144461a5..b6f7cf2d 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,10 +1,6 @@ using System; -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 { @@ -12,14 +8,14 @@ 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 : HlslBuffer where T : unmanaged + public sealed class ReadWriteBuffer : HlslStructuredBuffer 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, size * Unsafe.SizeOf(), BufferType.ReadWrite) { } + internal ReadWriteBuffer(GraphicsDevice device, int size) : base(device, size, BufferType.ReadWrite) { } /// /// Gets or sets a single value from the current read write buffer @@ -31,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"); } - - /// - public override void GetData(Span span) - { - using Buffer transferBuffer = new Buffer(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); - } - - /// - public override void SetData(Span span) - { - using Buffer transferBuffer = new Buffer(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(); - } - - /// - public override void SetData(HlslBuffer buffer) - { - if (buffer is ConstantBuffer 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); - } } }