diff --git a/Directory.Build.props b/Directory.Build.props
index 11df4cd6..a648d365 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -40,6 +40,13 @@
See: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1060.
-->
$(NoWarn);CA1060
+
+
+ $(NoWarn);CS8500
diff --git a/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs b/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs
new file mode 100644
index 00000000..4399ec7c
--- /dev/null
+++ b/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Immutable;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using ComputeSharp.D2D1.__Internals;
+using ComputeSharp.D2D1.Interop;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp.D2D1.Shaders.Loaders;
+
+///
+/// A resource texture index array loader for D2D1 pixel shaders.
+///
+internal struct D2D1ImmutableArrayResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
+{
+ ///
+ /// The array of indices for resource textures for the current effect.
+ ///
+ private ImmutableArray indices;
+
+ ///
+ /// Gets the resulting array of indices for resource textures for the current effect.
+ ///
+ /// An array of indices for resource textures for the current effect.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public ImmutableArray GetResultingIndices()
+ {
+ return this.indices;
+ }
+
+ ///
+ unsafe void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan data)
+ {
+ if (data.IsEmpty)
+ {
+ this.indices = ImmutableArray.Empty;
+ }
+ else
+ {
+ ReadOnlySpan descriptions = MemoryMarshal.Cast(data);
+
+ int[] indices = new int[descriptions.Length];
+
+ for (int i = 0; i < descriptions.Length; i++)
+ {
+ indices[i] = descriptions[i].Index;
+ }
+
+ this.indices = *(ImmutableArray*)&indices;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs b/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs
new file mode 100644
index 00000000..3ceecfe4
--- /dev/null
+++ b/src/ComputeSharp.D2D1.Uwp/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using ComputeSharp.D2D1.__Internals;
+using ComputeSharp.D2D1.Interop;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp.D2D1.Shaders.Loaders;
+
+///
+/// A resource texture index bitmask loader for D2D1 pixel shaders.
+///
+internal struct D2D1IndexBitmaskResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
+{
+ ///
+ /// The bitmask of indices for resource textures in the target shader.
+ ///
+ private int indexBitmask;
+
+ ///
+ /// Gets the resulting bitmask of indices for resource textures in the target shader.
+ ///
+ /// A bitmask of indices for resource textures in the target shader.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public int GetResultingIndexBitmask()
+ {
+ return this.indexBitmask;
+ }
+
+ ///
+ void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan data)
+ {
+ int indexBitmask = 0;
+
+ foreach (ref readonly D2D1ResourceTextureDescription description in MemoryMarshal.Cast(data))
+ {
+ indexBitmask |= 1 << description.Index;
+ }
+
+ this.indexBitmask = indexBitmask;
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
index 3f29f9b5..b8157ebf 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
@@ -195,7 +195,7 @@ unsafe partial class PixelShaderEffect
this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty();
// Loop over all effect inputs and update the cache back as well
- for (int i = 0; i < 16; i++)
+ for (int i = 0; i < SourceCollection.Count; i++)
{
// If this index has been explicitly requested to be skipped, just dispose the source reference.
// This is the case when the effect is being unrealized, and callers will set this value on their own.
@@ -277,12 +277,13 @@ unsafe partial class PixelShaderEffect
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
}
- // Set all available resource texture managers (only set those that are not null, as they're all null by default anyway)
- for (int i = 0; i < 16; i++)
+ // Set all available resource texture managers (only set those that are not null, as they're all null by default anyway).
+ // The loop only goes over the indices of valid slots for resource textures for the current effect, and ignores the others.
+ foreach (int index in ResourceTextureManagerCollection.Indices)
{
- if (ResourceTextureManagers.Storage[i] is D2D1ResourceTextureManager resourceTextureManager)
+ if (ResourceTextureManagers.Storage[index] is D2D1ResourceTextureManager resourceTextureManager)
{
- D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, i);
+ D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, index);
}
}
}
@@ -295,7 +296,7 @@ unsafe partial class PixelShaderEffect
/// The instance in use.
private void RefreshInputs(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
{
- for (int i = 0; i < 16; i++)
+ for (int i = 0; i < SourceCollection.Count; i++)
{
// Retrieve the managed wrapper for the current source
IGraphicsEffectSource? source = GetD2DInput(i);
diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs
index e214a282..f0d90bd3 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs
@@ -1,8 +1,14 @@
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;
///
@@ -13,6 +19,11 @@ partial class PixelShaderEffect
///
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.
///
@@ -27,6 +38,11 @@ partial class PixelShaderEffect
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.
///
@@ -37,8 +53,23 @@ partial class PixelShaderEffect
[DisallowNull]
public D2D1ResourceTextureManager? this[int index]
{
- get => Owner.GetResourceTextureManager(index);
- set => Owner.SetResourceTextureManager(value, 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);
+ }
}
///
@@ -50,5 +81,49 @@ partial class PixelShaderEffect
/// 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();
+ }
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs
index b9e0afca..19ccd9c0 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs
@@ -1,4 +1,7 @@
using System;
+using System.Runtime.CompilerServices;
+using ComputeSharp.D2D1.Helpers;
+using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Uwp.Buffers;
using Windows.Graphics.Effects;
@@ -26,6 +29,11 @@ partial class PixelShaderEffect
Owner = owner;
}
+ ///
+ /// Gets the actual input count for the current effect.
+ ///
+ internal static int Count { get; } = D2D1PixelShader.GetInputCount();
+
///
/// Gets or sets the source at a specified index.
///
@@ -34,8 +42,18 @@ partial class PixelShaderEffect
/// Thrown if is not a valid index for the current effect.
public IGraphicsEffectSource? this[int index]
{
- get => Owner.GetSource(index);
- set => Owner.SetSource(value, index);
+ get
+ {
+ ValidateIndex(index);
+
+ return Owner.GetSource(index);
+ }
+ set
+ {
+ ValidateIndex(index);
+
+ Owner.SetSource(value, index);
+ }
}
///
@@ -47,5 +65,19 @@ partial class PixelShaderEffect
/// Gets a reference to the value containing the available -s.
///
internal ref GraphicsEffectSourceBuffer 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 >= Count)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid source index for the current effect.");
+ }
+ }
}
}
\ No newline at end of file