From 5223615824113da18aa6f2decfb60996474d2d16 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Feb 2021 19:30:21 +0100 Subject: [PATCH] Code refactoring to TransferTexture2D --- .../Abstract/TransferTexture2D{T}.cs | 56 +----------- .../Resources/Views/TextureView2D{T}.cs | 86 +++++++++---------- 2 files changed, 44 insertions(+), 98 deletions(-) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 47e0c28f..69d87725 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -1,6 +1,4 @@ -using System; -using System.Buffers; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -123,57 +121,5 @@ namespace ComputeSharp.Resources GraphicsDeviceMismatchException.Throw(this, device); } } - - /// - /// A implementation wrapping a instance. - /// - private sealed class MemoryManager : MemoryManager - { - /// - /// The in use. - /// - private readonly TransferTexture2D texture; - - /// - /// Creates a new instance for a given texture. - /// - /// The in use. - public MemoryManager(TransferTexture2D texture) - { - this.texture = texture; - } - - /// - public override Memory Memory - { - get => ThrowHelper.ThrowNotSupportedException>(); - } - - /// - public override Span GetSpan() - { - return new(this.texture.mappedData, this.texture.Width * (this.texture.Width + this.texture.rowPitch)); - } - - /// - public override MemoryHandle Pin(int elementIndex = 0) - { - Guard.IsEqualTo(elementIndex, 0, nameof(elementIndex)); - - this.texture.ThrowIfDisposed(); - - return new(this.texture.mappedData); - } - - /// - public override void Unpin() - { - } - - /// - protected override void Dispose(bool disposing) - { - } - } } } diff --git a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs index 78f3a5af..9ba78ec2 100644 --- a/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Views/TextureView2D{T}.cs @@ -27,13 +27,13 @@ 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_____... + // _____________________stride______... // 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 | -- | -- | |-height // | -- | -- | XX | XX | XX | XX | XX | XX | -- | -- |_| // | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | // | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | @@ -73,8 +73,8 @@ namespace ComputeSharp internal TextureView2D(T* pointer, int width, int height, int strideInBytes) { this.pointer = pointer; - this.height = height; this.width = width; + this.height = height; this.strideInBytes = strideInBytes; } @@ -122,60 +122,60 @@ namespace ComputeSharp /// /// Gets the element at the specified zero-based indices. /// - /// The target row to get the element from. - /// The target column to get the element from. + /// 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. + /// Thrown when either or are invalid. /// - public ref T this[int row, int column] + public ref T this[int x, int y] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if ((uint)row >= (uint)Height || - (uint)column >= (uint)this.width) + if ((uint)x >= (uint)this.width || + (uint)y >= (uint)this.height) { ThrowHelper.ThrowIndexOutOfRangeException(); } - return ref *(((T*)((byte*)this.pointer + (row * this.strideInBytes))) + column); + return ref *(((T*)((byte*)this.pointer + (y * this.strideInBytes))) + x); } } /// /// Gets the element at the specified zero-based indices. /// - /// The target row to get the element from. - /// The target column to get the element from. + /// 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. + /// Thrown when either or are invalid. /// - public ref T this[Index row, Index column] + public ref T this[Index x, Index y] { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ref this[row.GetOffset(Height), column.GetOffset(this.width)]; + get => ref this[x.GetOffset(this.width), y.GetOffset(this.height)]; } /// /// Slices the current instance with the specified parameters. /// - /// The target range of rows to select. - /// The target range of columns to select. + /// The target range of columns to select. + /// The target range of rows to select. /// - /// Thrown when either or are invalid. + /// Thrown when either or are invalid. /// /// A new instance representing a slice of the current one. - public TextureView2D this[Range rows, Range columns] + public TextureView2D this[Range x, Range y] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var (row, height) = rows.GetOffsetAndLength(Height); - var (column, width) = columns.GetOffsetAndLength(this.width); + var (column, width) = x.GetOffsetAndLength(this.width); + var (row, height) = y.GetOffsetAndLength(this.height); - return Slice(row, column, height, width); + return Slice(column, row, width, height); } } @@ -195,7 +195,7 @@ namespace ComputeSharp } else { - for (int i = 0; i < Height; i++) + for (int i = 0; i < this.height; i++) { GetRowSpan(i).Clear(); } @@ -227,7 +227,7 @@ namespace ComputeSharp ThrowHelper.ThrowArgumentExceptionForDestinationTooShort(); } - for (int i = 0, j = 0; i < Height; i++, j += this.width) + for (int i = 0, j = 0; i < this.height; i++, j += this.width) { GetRowSpan(i).CopyTo(destination.Slice(j)); } @@ -244,8 +244,8 @@ namespace ComputeSharp /// public void CopyTo(TextureView2D destination) { - if (destination.Height != Height || - destination.width != this.width) + if (destination.width != this.width || + destination.width != this.height) { ThrowHelper.ThrowArgumentException(); } @@ -261,7 +261,7 @@ namespace ComputeSharp } else { - for (int i = 0; i < Height; i++) + for (int i = 0; i < this.height; i++) { GetRowSpan(i).CopyTo(destination.GetRowSpan(i)); } @@ -292,8 +292,8 @@ namespace ComputeSharp /// Whether or not the operation was successful. public bool TryCopyTo(TextureView2D destination) { - if (destination.Height == Height && - destination.Width == this.width) + if (destination.width == this.width || + destination.height == this.height) { CopyTo(destination); @@ -321,7 +321,7 @@ namespace ComputeSharp else { // Fill one row at a time - for (int i = 0; i < Height; i++) + for (int i = 0; i < this.height; i++) { GetRowSpan(i).Fill(value); } @@ -344,8 +344,8 @@ namespace ComputeSharp /// /// Slices the current instance with the specified parameters. /// - /// The target row to map within the current instance. - /// The target column to map within the current instance. + /// 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. /// @@ -354,29 +354,29 @@ namespace ComputeSharp /// /// A new instance representing a slice of the current one. [Pure] - public TextureView2D Slice(int row, int column, int height, int width) + public TextureView2D Slice(int x, int y, int width, int height) { - if ((uint)row >= Height) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); - } - - if ((uint)column >= this.width) + if ((uint)x >= this.width) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForColumn(); } - if ((uint)height > (Height - row)) + if ((uint)y >= this.height) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); + } + + if ((uint)height > (this.height - y)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForHeight(); } - if ((uint)width > (this.width - column)) + if ((uint)width > (this.width - x)) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForWidth(); } - T* pointer = (T*)((byte*)this.pointer + (row * this.strideInBytes)) + column; + T* pointer = (T*)((byte*)this.pointer + (y * this.strideInBytes)) + x; return new(pointer, width, height, this.strideInBytes); } @@ -390,7 +390,7 @@ namespace ComputeSharp [Pure] public Span GetRowSpan(int row) { - if ((uint)row >= (uint)Height) + if ((uint)row >= (uint)this.height) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); }