diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 37d98a29..5ead35b8 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -5,6 +5,7 @@ using ComputeSharp.Graphics.Buffers.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; +using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Graphics.Buffers.Abstract @@ -30,15 +31,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public override unsafe void GetData(Span span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.ReadBack); + int + byteOffset = offset * ElementSizeInBytes, + byteSize = count * ElementSizeInBytes; + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, byteSize); using (CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.CopyBufferRegion(D3D12Resource, offset * ElementSizeInBytes, transferBuffer.D3D12Resource, 0, count * ElementSizeInBytes); + copyCommandList.CopyBufferRegion(D3D12Resource, byteOffset, d3D12Resource.Get(), 0, byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } - using ID3D12ResourceMap resource = transferBuffer.D3D12Resource->Map(); + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); MemoryHelper.Copy(resource.Pointer, 0, span, count); } @@ -46,16 +51,20 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// public override unsafe void SetData(ReadOnlySpan span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Upload); + int + byteOffset = offset * ElementSizeInBytes, + byteSize = count * ElementSizeInBytes; - using (ID3D12ResourceMap resource = transferBuffer.D3D12Resource->Map()) + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, byteSize); + + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { MemoryHelper.Copy(span, resource.Pointer, 0, count); } using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyBufferRegion(transferBuffer.D3D12Resource, 0, D3D12Resource, offset * ElementSizeInBytes, count * ElementSizeInBytes); + copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, byteOffset, byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs index a189aff4..77aa09c8 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs @@ -52,12 +52,12 @@ namespace ComputeSharp.Graphics.Buffers internal readonly BufferType BufferType; /// - /// Creates a new instance with the specified parameters + /// 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 + /// 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. internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) { this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes); @@ -70,16 +70,13 @@ namespace ComputeSharp.Graphics.Buffers GraphicsDevice = device; - if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite) - { - GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); - switch (bufferType) - { - case BufferType.Constant: CreateConstantBufferView(); break; - case BufferType.ReadOnly: CreateShaderResourceView(); break; - case BufferType.ReadWrite: CreateUnorderedAccessView(); break; - } + switch (bufferType) + { + case BufferType.Constant: CreateConstantBufferView(); break; + case BufferType.ReadOnly: CreateShaderResourceView(); break; + case BufferType.ReadWrite: CreateUnorderedAccessView(); break; } }