using System;
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;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Uwp;
///
partial class PixelShaderEffect
{
///
/// Represents the collection of objects in a instance.
///
public sealed class ResourceTextureManagerCollection
{
///
/// 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;
}
///
/// Gets the collection of valid resource texture indices for the current effect.
///
internal static ImmutableArray Indices { get; } = GetIndices();
///
/// 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);
}
}
///
/// The owning instance.
///
internal PixelShaderEffect Owner { get; }
///
/// Gets a reference to the value containing the available -s.
///
internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer;
///
/// 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();
}
}
}