From 6629e94f0abbce53bb3e081c3bebbeea74ce6d82 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 2 Feb 2021 00:06:07 +0100 Subject: [PATCH] Minor code refactoring and bug fixes --- .../Resources/Abstract/TransferBuffer{T}.cs | 16 ++++++---------- .../Resources/Abstract/TransferTexture2D{T}.cs | 2 +- .../Resources/ReadBackBuffer{T}.cs | 1 + .../Resources/UploadBuffer{T}.cs | 1 + 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs index 0c631c76..16cbf6b7 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -8,7 +8,7 @@ using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; -namespace ComputeSharp +namespace ComputeSharp.Resources { /// /// A representing a typed buffer stored on CPU memory, that can be used to transfer data to/from the GPU. @@ -22,11 +22,6 @@ namespace ComputeSharp /// private ComPtr d3D12Resource; - /// - /// The size in bytes of the current buffer (this value is never negative). - /// - private readonly nint sizeInBytes; - /// /// The pointer to the start of the mapped buffer data. /// @@ -49,8 +44,9 @@ namespace ComputeSharp GraphicsDevice = device; Length = length; - this.sizeInBytes = checked((nint)(length * (uint)sizeof(T))); - this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)this.sizeInBytes, device.IsCacheCoherentUMA); + ulong sizeInBytes = (uint)length * (uint)sizeof(T); + + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, device.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; } @@ -147,11 +143,11 @@ namespace ComputeSharp /// public override MemoryHandle Pin(int elementIndex = 0) { - Guard.IsInRange(elementIndex, 0, this.buffer.Length, nameof(elementIndex)); + Guard.IsEqualTo(elementIndex, 0, nameof(elementIndex)); this.buffer.ThrowIfDisposed(); - return new(this.buffer.mappedData + elementIndex); + return new(this.buffer.mappedData); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs index e74931a9..844d10c0 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -167,7 +167,7 @@ namespace ComputeSharp.Resources /// public override Span GetSpan() { - return new(this.texture.mappedData, this.texture.Height * this.texture.rowPitch); + return new(this.texture.mappedData, this.texture.Width * (this.texture.Width + this.texture.rowPitch)); } /// diff --git a/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs index dd8f50a3..4dad71ed 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Resources; using ComputeSharp.Resources.Views; namespace ComputeSharp diff --git a/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs index b7d155ad..27b0307d 100644 --- a/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Resources; using ComputeSharp.Resources.Views; namespace ComputeSharp