using System; using System.ComponentModel; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; #pragma warning disable CS0809, CA1065 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 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 { Guard.IsInRange(x, 0, this.width); Guard.IsInRange(y, 0, this.height); Guard.IsInRange(z, 0, this.depth); 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 doesn't match the size of the current instance. public void CopyTo(TextureView3D destination) { if (destination.width != this.width || destination.height != this.height || destination.depth != this.depth) { 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 && destination.depth == this.depth) { 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. [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. /// Thrown when either or are out of range. /// The resulting row . public Span GetRowSpan(int y, int z) { Guard.IsInRange(y, 0, this.height); Guard.IsInRange(z, 0, this.depth); 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); 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. /// /// 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. 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 TextureView3D 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 TextureView3D 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); } }