using System; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Shaders.Loaders; using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Collections; #pragma warning disable CS0618, CS8767 namespace ComputeSharp.D2D1.Uwp; /// partial class PixelShaderEffect { /// /// Represents the collection of objects in a instance. /// public sealed class ResourceTextureManagerCollection : IList, IReadOnlyList, IList, IFixedCountList { /// /// The bitmask of valid indices for resource texture managers in the current shader type. /// private static readonly int IndexBitmask = GetIndexBitmask(); /// /// The fixed buffer of instances. /// private ResourceTextureManagerBuffer fixedBuffer; /// /// Creates a new instance with the specified parameters. /// /// The owning instance. internal ResourceTextureManagerCollection(PixelShaderEffect owner) { Owner = owner; } /// public int Count => Indices.Length; /// /// Gets or sets the object at a specified index. /// /// The index of the source to get or set. /// The object at the specified index. /// Thrown if the value is set to . /// Thrown if is not a valid index for the current effect. [DisallowNull] public D2D1ResourceTextureManager? this[int index] { get { ValidateIndex(index); return Owner.GetResourceTextureManager(index); } set { if (value is null) { ThrowHelper.ThrowArgumentNullException(nameof(value), "Input resource texture managers cannot be null."); } ValidateIndex(index); Owner.SetResourceTextureManager(value, index); } } /// object? IList.this[int index] { get => this[index]; set => this[index] = (D2D1ResourceTextureManager?)value!; } /// /// Gets the collection of valid resource texture indices for the current effect. /// internal static ImmutableArray Indices { get; } = GetIndices(); /// /// The owning instance. /// internal PixelShaderEffect Owner { get; } /// /// Gets a reference to the value containing the available -s. /// internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer; /// ImmutableArray IFixedCountList.Indices => Indices; /// bool ICollection.IsReadOnly => false; /// bool IList.IsReadOnly => false; /// bool IList.IsFixedSize => true; /// bool ICollection.IsSynchronized => true; /// object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for ResourceTextureManagerCollection."); /// void ICollection.Add(D2D1ResourceTextureManager? item) { throw new NotSupportedException("ICollection.Add is not supported for ResourceTextureManagerCollection."); } /// int IList.Add(object value) { throw new NotSupportedException("IList.Add is not supported for ResourceTextureManagerCollection."); } /// void ICollection.Clear() { throw new NotSupportedException("ICollection.Clear is not supported for ResourceTextureManagerCollection."); } /// void IList.Clear() { throw new NotSupportedException("IList.Clear is not supported for ResourceTextureManagerCollection."); } /// bool ICollection.Contains(D2D1ResourceTextureManager? item) { return FixedCountList.IndexOf(this, item) != -1; } /// bool IList.Contains(object value) { return FixedCountList.IndexOf(this, (D2D1ResourceTextureManager?)value) != -1; } /// void ICollection.CopyTo(D2D1ResourceTextureManager?[] array, int arrayIndex) { FixedCountList.CopyTo(this, array, arrayIndex); } /// void ICollection.CopyTo(Array array, int index) { FixedCountList.CopyTo(this, array, index); } /// IEnumerator IEnumerable.GetEnumerator() { return FixedCountList.GetEnumerator(this); } /// IEnumerator IEnumerable.GetEnumerator() { return FixedCountList.GetEnumerator(this); } /// int IList.IndexOf(D2D1ResourceTextureManager? item) { return FixedCountList.IndexOf(this, item); } /// int IList.IndexOf(object value) { return FixedCountList.IndexOf(this, (D2D1ResourceTextureManager?)value); } /// void IList.Insert(int index, D2D1ResourceTextureManager? item) { throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); } /// void IList.Insert(int index, object value) { throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); } /// bool ICollection.Remove(D2D1ResourceTextureManager? item) { throw new NotSupportedException("ICollection.Remove is not supported for ResourceTextureManagerCollection."); } /// void IList.Remove(object value) { throw new NotSupportedException("IList.Remove is not supported for ResourceTextureManagerCollection."); } /// void IList.RemoveAt(int index) { throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); } /// void IList.RemoveAt(int index) { throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); } /// /// Validates the input index for . /// /// The index to validate. /// Thrown if is out of range. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void ValidateIndex(int index) { if ((uint)index >= 16 || (IndexBitmask & (1 << index)) == 0) { ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid resource texture manager index for the current effect."); } } /// /// Gets the bitmask of indices for resource textures in the target shader. /// /// The bitmask of indices for resource textures in the target shader. private static int GetIndexBitmask() { D2D1IndexBitmaskResourceTextureDescriptionsLoader bitmaskLoader = default; Unsafe.SkipInit(out T shader); shader.LoadResourceTextureDescriptions(ref bitmaskLoader); return bitmaskLoader.GetResultingIndexBitmask(); } /// /// Gets the collection of valid indices for the current effect. /// /// The collection of valid indices for the current effect. private static ImmutableArray GetIndices() { D2D1ImmutableArrayResourceTextureDescriptionsLoader indicesLoader = default; Unsafe.SkipInit(out T shader); shader.LoadResourceTextureDescriptions(ref indicesLoader); return indicesLoader.GetResultingIndices(); } } }