From d383c4939aa9b18f2d21acafb1582702b1df8a60 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 17 Jan 2022 16:19:36 +0100 Subject: [PATCH] Bug fixes to TextureView[2D|3D], add GetDepthView(int) API --- .../Resources/Views/TextureView2D{T}.cs | 22 ++++++------- .../Resources/Views/TextureView3D{T}.cs | 33 ++++++++++++++----- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs index 74393f90..abf0f066 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs @@ -55,7 +55,7 @@ public readonly unsafe ref partial struct TextureView2D /// /// The row pitch of the specified 2D region. /// - private readonly int pitchInBytes; + private readonly int strideInBytes; /// /// Initializes a new instance of the struct with the specified parameters. @@ -69,7 +69,7 @@ public readonly unsafe ref partial struct TextureView2D this.pointer = pointer; this.width = width; this.height = height; - this.pitchInBytes = pitchInBytes; + this.strideInBytes = pitchInBytes; } /// @@ -130,7 +130,7 @@ public readonly unsafe ref partial struct TextureView2D Guard.IsInRange(x, 0, this.width, nameof(x)); Guard.IsInRange(y, 0, this.height, nameof(y)); - return ref *((T*)((byte*)this.pointer + (y * this.pitchInBytes)) + x); + return ref *((T*)((byte*)this.pointer + (y * this.strideInBytes)) + x); } } @@ -191,13 +191,11 @@ public readonly unsafe ref partial struct TextureView2D /// For this API to succeed, the target has to have the same shape as the current one. /// /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// + /// Thrown when doesn't match the size of the current instance. public void CopyTo(TextureView2D destination) { if (destination.width != this.width || - destination.width != this.height) + destination.height != this.height) { ThrowHelper.ThrowArgumentException(); } @@ -244,7 +242,7 @@ public readonly unsafe ref partial struct TextureView2D /// Whether or not the operation was successful. public bool TryCopyTo(TextureView2D destination) { - if (destination.width == this.width || + if (destination.width == this.width && destination.height == this.height) { CopyTo(destination); @@ -286,7 +284,7 @@ public readonly unsafe ref partial struct TextureView2D [MethodImpl(MethodImplOptions.AggressiveInlining)] public T* DangerousGetAddressAndByteStride(out int strideInBytes) { - strideInBytes = this.pitchInBytes; + strideInBytes = this.strideInBytes; return this.pointer; } @@ -295,13 +293,13 @@ public readonly unsafe ref partial struct TextureView2D /// Gets a for a specified row. /// /// The index of the target row to retrieve. - /// Throw when is out of range. + /// Thrown when is out of range. /// The resulting row . public Span GetRowSpan(int y) { Guard.IsInRange(y, 0, this.height, nameof(y)); - return new((byte*)this.pointer + (y * this.pitchInBytes), this.width); + return new((byte*)this.pointer + (y * this.strideInBytes), this.width); } /// @@ -311,7 +309,7 @@ public readonly unsafe ref partial struct TextureView2D /// Whether or not was correctly assigned. public bool TryGetSpan(out Span span) { - if (this.pitchInBytes == this.width) + if (this.strideInBytes == this.width) { span = new(this.pointer, Length); diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs index 489b636d..9dfd0212 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs @@ -221,13 +221,12 @@ public readonly unsafe ref partial struct TextureView3D /// For this API to succeed, the target has to have the same shape as the current one. /// /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// + /// Thrown when doesn't match the size of the current instance. public void CopyTo(TextureView3D destination) { if (destination.width != this.width || - destination.width != this.height) + destination.height != this.height || + destination.depth != this.depth) { ThrowHelper.ThrowArgumentException(); } @@ -277,8 +276,9 @@ public readonly unsafe ref partial struct TextureView3D /// Whether or not the operation was successful. public bool TryCopyTo(TextureView3D destination) { - if (destination.width == this.width || - destination.height == this.height) + if (destination.width == this.width && + destination.height == this.height && + destination.depth == this.depth) { CopyTo(destination); @@ -332,9 +332,7 @@ public readonly unsafe ref partial struct TextureView3D /// /// The index of the target row to retrieve. /// The depth of the row to retrieve. - /// - /// Throw when either or are out of range. - /// + /// Thrown when either or are out of range. /// The resulting row . public Span GetRowSpan(int y, int z) { @@ -344,6 +342,23 @@ public readonly unsafe ref partial struct TextureView3D return new((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes), this.width); } + /// + /// Gets a for a depth layer. + /// + /// The depth of the layer to retrieve. + /// Thrown when is out of range. + /// The resulting row . + public TextureView2D GetDepthView(int z) + { + Guard.IsInRange(z, 0, this.depth, nameof(z)); + + return new( + (T*)((byte*)this.pointer + (z * this.height * this.strideInBytes)), + this.width, + this.height, + this.strideInBytes); + } + /// /// Tries to get a instance, if the underlying buffer is contiguous and small enough. ///