From e405ad0f92135f0508bc9398ffb302f656f8a6b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Feb 2021 23:35:46 +0100 Subject: [PATCH] Added TextureView3D type --- .../Resources/Views/TextureView3D{T}.cs | 440 ++++++++++++++++++ 1 file changed, 440 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Resources/Views/TextureView3D{T}.cs diff --git a/src/ComputeSharp.Graphics/Resources/Views/TextureView3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/TextureView3D{T}.cs new file mode 100644 index 00000000..f203cde8 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Views/TextureView3D{T}.cs @@ -0,0 +1,440 @@ +using System; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.Resources.Views; + +#pragma warning disable CS0809 + +namespace ComputeSharp +{ + /// + /// The mirrors the type but representing a memory + /// area spanning 3 dimensions. The underlying buffer can be discontiguous across both the height and the depth. + /// It is recommended to execute as much computation on the GPU side as possible. + /// + /// The type of items in the current instance. + public readonly unsafe ref partial struct TextureView3D + where T : unmanaged + { + // The internal layout of a 3D texture mapped in memory is the same as that of a + // 3D texture, with different 3D slices being stacked along the depth axis. Each + // slice also has the same padding as the one between rows, which means that if + // rows have no padding, all 3D slices are contiguous in memory as well. + // The memory representation essentially looks like this: + // + // ________________width______________ + // / __pointer \ + // / / \ + // | 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 | -- | -- | + // ` ` ` ` ` ` ` ` ` ` ` + // ` ` ` ` ` ` ` ` ` ` ` + // | 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 | -- | -- | + // | 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_________________________/ + // + // Note that the depth stride is omitted after the last 3D slice. + + /// + /// The pointer to the first element of the target 3D region. + /// + private readonly T* pointer; + + /// + /// The width of the specified 3D texture. + /// + private readonly int width; + + /// + /// The height of the specified 3D region. + /// + private readonly int height; + + /// + /// The depth of the specified 3D region. + /// + private readonly int depth; + + /// + /// The stride of the specified 3D region. + /// + private readonly int strideInBytes; + + /// + /// Initializes a new instance of the struct with the specified parameters. + /// + /// The pointer to the start of the memory area to map. + /// The width of the 3D memory area to map. + /// The height of the 3D memory area to map. + /// The depth of the 3D memory area to map. + /// The stride in bytes of the 3D memory area to map. + internal TextureView3D(T* pointer, int width, int height, int depth, int strideInBytes) + { + this.pointer = pointer; + this.width = width; + this.height = height; + this.depth = depth; + this.strideInBytes = strideInBytes; + } + + /// + /// Gets an empty instance. + /// + public static TextureView3D Empty => default; + + /// + /// Gets a value indicating whether the current instance is empty. + /// + public bool IsEmpty + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.width == 0; + } + + /// + /// Gets the total length of the current instance. + /// + public int Length + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.width * this.height * this.depth; + } + + /// + /// Gets the width of the underlying 3D memory area. + /// + public int Width + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.width; + } + + /// + /// Gets the height of the underlying 3D memory area. + /// + public int Height + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.height; + } + + /// + /// Gets the depth of the underlying 3D memory area. + /// + public int Depth + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.depth; + } + + /// + /// 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. + /// The target depth to get the element from. + /// A reference to the element at the specified indices. + /// + /// Thrown when either , or are invalid. + /// + public ref T this[int x, int y, int z] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if ((uint)x >= (uint)this.width || + (uint)y >= (uint)this.height || + (uint)z >= (uint)this.depth) + { + ThrowHelper.ThrowIndexOutOfRangeException(); + } + + return ref *((T*)((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes)) + x); + } + } + + /// + /// Clears the contents of the current instance. + /// + public void Clear() + { + if (IsEmpty) + { + return; + } + + if (TryGetSpan(out Span span)) + { + span.Clear(); + } + else + { + for (int z = 0; z < this.depth; z++) + { + for (int y = 0; y < this.height; y++) + { + GetRowSpan(y, z).Clear(); + } + } + } + } + + /// + /// Copies the contents of this into a destination instance. + /// + /// The destination instance. + /// + /// Thrown when is shorter than the source instance. + /// + public void CopyTo(Span destination) + { + if (IsEmpty) + { + return; + } + + if (TryGetSpan(out Span span)) + { + span.CopyTo(destination); + } + else + { + for (int z = 0, j = 0; z < this.depth; z++) + { + for (int y = 0; y < this.height; y++, j += this.width) + { + GetRowSpan(y, z).CopyTo(destination.Slice(j)); + } + } + } + } + + /// + /// Copies the contents of this into a destination instance. + /// 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. + /// + public void CopyTo(TextureView3D destination) + { + if (destination.width != this.width || + destination.width != this.height) + { + ThrowHelper.ThrowArgumentException(); + } + + if (IsEmpty) + { + return; + } + + if (destination.TryGetSpan(out Span span)) + { + CopyTo(span); + } + else + { + for (int z = 0; z < this.depth; z++) + { + for (int y = 0; y < this.height; y++) + { + GetRowSpan(y, z).CopyTo(destination.GetRowSpan(y, z)); + } + } + } + } + + /// + /// Attempts to copy the current instance to a destination . + /// + /// The target of the copy operation. + /// Whether or not the operation was successful. + public bool TryCopyTo(Span destination) + { + if (destination.Length >= Length) + { + CopyTo(destination); + + return true; + } + + return false; + } + + /// + /// Attempts to copy the current instance to a destination . + /// + /// The target of the copy operation. + /// Whether or not the operation was successful. + public bool TryCopyTo(TextureView3D destination) + { + if (destination.width == this.width || + destination.height == this.height) + { + CopyTo(destination); + + return true; + } + + return false; + } + + /// + /// Fills the elements of this span with a specified value. + /// + /// The value to assign to each element of the instance. + public void Fill(T value) + { + if (IsEmpty) + { + return; + } + + if (TryGetSpan(out Span span)) + { + span.Fill(value); + } + else + { + for (int z = 0; z < this.depth; z++) + { + for (int y = 0; y < this.height; y++) + { + GetRowSpan(y, z).Fill(value); + } + } + } + } + + /// + /// Returns a reference to the first element within the current instance, with no bounds check. + /// + /// A reference to the first element within the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T* DangerousGetAddressAndByteStride(out int strideInBytes) + { + strideInBytes = this.strideInBytes; + + return this.pointer; + } + + /// + /// Gets a for a specified row. + /// + /// The index of the target row to retrieve. + /// The depth of the row to retrieve. + /// + /// Throw when either or are out of range. + /// + /// The resulting row . + [Pure] + public Span GetRowSpan(int y, int z) + { + if ((uint)y >= (uint)this.height) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); + } + + if ((uint)z >= (uint)this.depth) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForZ(); + } + + return new((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes), this.width); + } + + /// + /// Tries to get a instance, if the underlying buffer is contiguous and small enough. + /// + /// The resulting , in case of success. + /// Whether or not was correctly assigned. + public bool TryGetSpan(out Span span) + { + if (this.strideInBytes == this.width) + { + span = new(this.pointer, Length); + + return true; + } + + span = default; + + return false; + } + + /// + /// Copies the contents of the current instance into a new 3D array. + /// + /// A 3D array containing the data in the current instance. + [Pure] + public T[,,] ToArray() + { + T[,,] array = new T[this.depth, this.height, this.width]; + + fixed (T* pointer = array) + { + CopyTo(new Span(pointer, Length)); + } + + return array; + } + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Equals() on Span will always throw an exception. Use == instead.")] + public override bool Equals(object? obj) + { + throw new NotSupportedException("ComputeSharp.TextureView3D.Equals(object) is not supported"); + } + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("GetHashCode() on Span will always throw an exception.")] + public override int GetHashCode() + { + throw new NotSupportedException("ComputeSharp.TextureView3D.GetHashCode() is not supported"); + } + + /// + public override string ToString() + { + return $"ComputeSharp.TextureView3D<{typeof(T)}>[{Width}, {Height}, {Depth}]"; + } + + /// + /// Checks whether two instances are equal. + /// + /// The first instance to compare. + /// The second instance to compare. + /// Whether or not and are equal. + public static bool operator ==(TextureView3D left, TextureView3D right) + { + return left.pointer == right.pointer; + } + + /// + /// Checks whether two instances are not equal. + /// + /// The first instance to compare. + /// The second instance to compare. + /// Whether or not and are not equal. + public static bool operator !=(TextureView3D left, TextureView3D right) + { + return !(left == right); + } + } +}