From 6badb589f24188a394afc2cacf03aad816f4440a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Feb 2021 23:38:21 +0100 Subject: [PATCH] Minor bug fixes and code tweaks --- .../Abstract/TransferTexture2D{T}.cs | 8 +- .../Resources/Views/TextureView2D{T}.cs | 135 ++++-------------- 2 files changed, 29 insertions(+), 114 deletions(-) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 69d87725..26ca213a 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -29,9 +29,9 @@ namespace ComputeSharp.Resources private readonly T* mappedData; /// - /// The pitch size for each row within . + /// The stride in bytes for each row within . /// - private readonly int rowPitch; + private readonly int strideInBytes; /// /// Creates a new instance with the specified parameters. @@ -67,7 +67,7 @@ namespace ComputeSharp.Resources this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; - this.rowPitch = (int)((d3D12PlacedSubresourceFootprint.Footprint.RowPitch - rowSizeInBytes) / (uint)sizeof(T)); + this.strideInBytes = (int)d3D12PlacedSubresourceFootprint.Footprint.RowPitch; } /// @@ -98,7 +98,7 @@ namespace ComputeSharp.Resources { ThrowIfDisposed(); - return new(this.mappedData, Height, Width, this.rowPitch); + return new(this.mappedData, Height, Width, this.strideInBytes); } } diff --git a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs index 9ba78ec2..4fda03a4 100644 --- a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs @@ -27,21 +27,16 @@ namespace ComputeSharp // bytes, as the texture layout alignment is unrelated to the logical type in use. // Assuming we a stride that's greater than the logical rows, we might have: // - // _____________________stride______... - // pointer__ /________width_________ + // pointer__ _________width_________ // \/ \ - // | -- | -- | |- | -- | -- | -- | -- | -- | -- | -- |_ - // | -- | -- | XX | XX | XX | XX | XX | XX | -- | -- | | - // | -- | -- | XX | XX | XX | XX | XX | XX | -- | -- | | - // | -- | -- | XX | XX | XX | XX | XX | XX | -- | -- | |-height - // | -- | -- | XX | XX | XX | XX | XX | XX | -- | -- |_| - // | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | - // | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | - // ..._stride__/ - // - // The stride is used to calculate the offset between each discontiguous row, so - // that any arbitrary memory locations can be used to internally represent a 2D view. - // This gives users much more flexibility when creating spans from data. + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- |-| + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- | | + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- | |-height + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- | | + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- |-| + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- | + // | XX | XX | XX | XX | XX | XX | XX | XX | -- | -- | + // \________________stride_________________________/ /// /// The pointer to the first element of the target 2D region. @@ -93,7 +88,7 @@ namespace ComputeSharp } /// - /// Gets the length of the current instance. + /// Gets the total length of the current instance. /// public int Length { @@ -139,43 +134,7 @@ namespace ComputeSharp ThrowHelper.ThrowIndexOutOfRangeException(); } - return ref *(((T*)((byte*)this.pointer + (y * this.strideInBytes))) + x); - } - } - - /// - /// Gets the element at the specified zero-based indices. - /// - /// The target column to get the element from. - /// The target row to get the element from. - /// A reference to the element at the specified indices. - /// - /// Thrown when either or are invalid. - /// - public ref T this[Index x, Index y] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ref this[x.GetOffset(this.width), y.GetOffset(this.height)]; - } - - /// - /// Slices the current instance with the specified parameters. - /// - /// The target range of columns to select. - /// The target range of rows to select. - /// - /// Thrown when either or are invalid. - /// - /// A new instance representing a slice of the current one. - public TextureView2D this[Range x, Range y] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - var (column, width) = x.GetOffsetAndLength(this.width); - var (row, height) = y.GetOffsetAndLength(this.height); - - return Slice(column, row, width, height); + return ref *((T*)((byte*)this.pointer + (y * this.strideInBytes)) + x); } } @@ -195,9 +154,9 @@ namespace ComputeSharp } else { - for (int i = 0; i < this.height; i++) + for (int y = 0; y < this.height; y++) { - GetRowSpan(i).Clear(); + GetRowSpan(y).Clear(); } } } @@ -227,9 +186,9 @@ namespace ComputeSharp ThrowHelper.ThrowArgumentExceptionForDestinationTooShort(); } - for (int i = 0, j = 0; i < this.height; i++, j += this.width) + for (int y = 0, j = 0; y < this.height; y++, j += this.width) { - GetRowSpan(i).CopyTo(destination.Slice(j)); + GetRowSpan(y).CopyTo(destination.Slice(j)); } } } @@ -261,9 +220,9 @@ namespace ComputeSharp } else { - for (int i = 0; i < this.height; i++) + for (int y = 0; y < this.height; y++) { - GetRowSpan(i).CopyTo(destination.GetRowSpan(i)); + GetRowSpan(y).CopyTo(destination.GetRowSpan(y)); } } } @@ -320,10 +279,9 @@ namespace ComputeSharp } else { - // Fill one row at a time - for (int i = 0; i < this.height; i++) + for (int y = 0; y < this.height; y++) { - GetRowSpan(i).Fill(value); + GetRowSpan(y).Fill(value); } } } @@ -341,61 +299,21 @@ namespace ComputeSharp return this.pointer; } - /// - /// Slices the current instance with the specified parameters. - /// - /// The target column to map within the current instance. - /// The target row to map within the current instance. - /// The height to map within the current instance. - /// The width to map within the current instance. - /// - /// Thrown when either , or - /// are negative or not within the bounds that are valid for the current instance. - /// - /// A new instance representing a slice of the current one. - [Pure] - public TextureView2D Slice(int x, int y, int width, int height) - { - if ((uint)x >= this.width) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn(); - } - - if ((uint)y >= this.height) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); - } - - if ((uint)height > (this.height - y)) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight(); - } - - if ((uint)width > (this.width - x)) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth(); - } - - T* pointer = (T*)((byte*)this.pointer + (y * this.strideInBytes)) + x; - - return new(pointer, width, height, this.strideInBytes); - } - /// /// Gets a for a specified row. /// - /// The index of the target row to retrieve. - /// Throw when is out of range. + /// The index of the target row to retrieve. + /// Throw when is out of range. /// The resulting row . [Pure] - public Span GetRowSpan(int row) + public Span GetRowSpan(int y) { - if ((uint)row >= (uint)this.height) + if ((uint)y >= (uint)this.height) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); } - return new((byte*)this.pointer + (row * this.strideInBytes), this.width); + return new((byte*)this.pointer + (y * this.strideInBytes), this.width); } /// @@ -464,10 +382,7 @@ namespace ComputeSharp /// Whether or not and are equal. public static bool operator ==(TextureView2D left, TextureView2D right) { - return - left.pointer == right.pointer && - left.width == right.width && - left.height == right.height; + return left.pointer == right.pointer; } ///