diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
index 9b5ffd45..86dcf802 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
-using ComputeSharp.D2D1.SourceGenerators.Diagnostics;
using ComputeSharp.D2D1.SourceGenerators.SyntaxRewriters;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
@@ -24,7 +23,7 @@ partial class ID2D1ShaderGenerator
///
/// A helper with all logic to generate the BuildHlslSource method.
///
- internal static partial class BuildHlslSource
+ private static partial class BuildHlslSource
{
///
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
@@ -33,13 +32,17 @@ partial class ID2D1ShaderGenerator
/// The node to process.
/// The for .
/// The number of inputs for the shader.
+ /// The indicess of the simple shader inputs.
+ /// The indicess of the complex shader inputs.
/// The resulting diagnostics from the processing operation.
/// The HLSL source for the shader.
public static string GetHlslSource(
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
- out int inputCount,
+ int inputCount,
+ ImmutableArray inputSimpleIndices,
+ ImmutableArray inputComplexIndices,
out ImmutableArray diagnostics)
{
ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
@@ -62,14 +65,8 @@ partial class ID2D1ShaderGenerator
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
- // Gather the shader metadata
- GatherD2D1AttributeInfo(
- builder,
- structDeclarationSymbol,
- out inputCount,
- out ImmutableArray inputSimpleIndices,
- out ImmutableArray inputComplexIndices,
- out bool requiresScenePosition);
+ // Check whether the scene position is required
+ bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
diagnostics = builder.ToImmutable();
@@ -397,100 +394,21 @@ partial class ID2D1ShaderGenerator
}
///
- /// Extracts the metadata definition for the current shader.
+ /// Gets whether or not a given shader requires the scene position.
///
- /// The collection of produced instances.
/// The input instance to process.
- /// The number of shader inputs to declare.
- /// The indicess of the simple shader inputs.
- /// The indicess of the complex shader inputs.
- /// Whether the shader requires the scene position.
- private static void GatherD2D1AttributeInfo(
- ImmutableArray.Builder diagnostics,
- INamedTypeSymbol structDeclarationSymbol,
- out int inputCount,
- out ImmutableArray inputSimpleIndices,
- out ImmutableArray inputComplexIndices,
- out bool requiresScenePosition)
+ /// Whether requires the scene position.
+ private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol)
{
- // We need a separate local here so we can keep the original value to apply
- // diagnostics to, but without returning invalid values to the caller which
- // might cause generator errors (eg. -1 would cause other code to just throw).
- int rawInputCount = 0;
-
- inputCount = 0;
- requiresScenePosition = false;
-
- ImmutableArray.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder();
- ImmutableArray.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder();
-
foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes())
{
- switch (attributeData.AttributeClass?.GetFullMetadataName())
+ if (attributeData.AttributeClass?.GetFullMetadataName() is "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute")
{
- case "ComputeSharp.D2D1.D2DInputCountAttribute":
- rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
- inputCount = Math.Max(rawInputCount, 0);
- break;
- case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
- inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
- break;
- case "ComputeSharp.D2D1.D2DInputComplexAttribute":
- inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
- break;
- case "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute":
- requiresScenePosition = true;
- break;
- default:
- break;
+ return true;
}
}
- inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
- inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
-
- // Validate the input count
- if (rawInputCount is not (>= 0 and <= 8))
- {
- diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
-
- return;
- }
-
- // All simple indices must be in range
- if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
- {
- diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
-
- return;
- }
-
- HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
- HashSet inputComplexIndicesAsSet = new(inputComplexIndicesBuilder);
-
- // All simple indices must be unique
- if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
- {
- diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
-
- return;
- }
-
- // All complex indices must be unique
- if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
- {
- diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
-
- return;
- }
-
- // Simple and complex indices can't have indices in common
- if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
- {
- diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
-
- return;
- }
+ return false;
}
///
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs
index d0496ef4..7553f9b2 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs
@@ -13,7 +13,7 @@ partial class ID2D1ShaderGenerator
///
/// A helper with all logic to generate the GetInputCount method.
///
- partial class GetInputCount
+ private static partial class GetInputCount
{
///
/// Creates a instance for the GetInputCount method.
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs
new file mode 100644
index 00000000..ce8db4a9
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs
@@ -0,0 +1,81 @@
+using System.Collections.Immutable;
+using ComputeSharp.D2D1.__Internals;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp.D2D1.SourceGenerators;
+
+///
+partial class ID2D1ShaderGenerator
+{
+ ///
+ partial class GetInputType
+ {
+ ///
+ /// Creates a instance for the GetInputTypeMethod method.
+ ///
+ /// The input types for the shader.
+ /// The resulting instance for the GetInputTypeMethod method.
+ public static MethodDeclarationSyntax GetSyntax(ImmutableArray inputTypes)
+ {
+ // This code produces a method declaration as follows:
+ //
+ // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(uint index)
+ // {
+ // return ;
+ // }
+ return
+ MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType"))
+ .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
+ .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
+ .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))))
+ .WithBody(Block(ReturnStatement(GetInputTypesSwitchExpression(inputTypes))));
+ }
+
+ ///
+ /// Gets the expression to get the type of a given shader input from its index.
+ ///
+ /// The input types for the shader.
+ /// The expression to extract the values from .
+ private static ExpressionSyntax GetInputTypesSwitchExpression(ImmutableArray inputTypes)
+ {
+ // If there are no inputs, just return an unspecified value.
+ if (inputTypes.IsEmpty)
+ {
+ return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0));
+ }
+
+ // In order to avoid repeated branches, we can build a bitmask that stores in each
+ // bit whether the corresponding pixel shader input is simple or complex. This will
+ // allow the logic to be branchless and to just extract the target bit from the mask.
+ uint bitmask = 0;
+
+ for (int i = 0; i < inputTypes.Length; i++)
+ {
+ bitmask |= inputTypes[i] << i;
+ }
+
+ // If all inputs are simple, also just return 0
+ if (bitmask == 0)
+ {
+ return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0));
+ }
+
+ // This code produces a bitmask flag extraction expression as follows:
+ //
+ // ( >> (int)index) & 1;
+ return
+ BinaryExpression(
+ SyntaxKind.BitwiseAndExpression,
+ ParenthesizedExpression(
+ BinaryExpression(
+ SyntaxKind.RightShiftExpression,
+ LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(bitmask)),
+ CastExpression(PredefinedType(Token(SyntaxKind.IntKeyword)), IdentifierName("index")))),
+ LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)));
+ }
+ }
+}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs
new file mode 100644
index 00000000..926345cb
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using System.Linq;
+using ComputeSharp.SourceGeneration.Extensions;
+using Microsoft.CodeAnalysis;
+using static ComputeSharp.D2D1.SourceGenerators.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.D2D1.SourceGenerators;
+
+///
+partial class ID2D1ShaderGenerator
+{
+ ///
+ /// A helper with all logic to generate the GetInputTypeMethod method.
+ ///
+ private static partial class GetInputType
+ {
+ ///
+ /// Extracts the input info for the current shader.
+ ///
+ /// The collection of produced instances.
+ /// The input instance to process.
+ /// The number of shader inputs to declare.
+ /// The indicess of the simple shader inputs.
+ /// The indices of the complex shader inputs.
+ /// The combined and serialized input types for each available input.
+ public static void GetInfo(
+ INamedTypeSymbol structDeclarationSymbol,
+ out int inputCount,
+ out ImmutableArray inputSimpleIndices,
+ out ImmutableArray inputComplexIndices,
+ out ImmutableArray combinedInputTypes,
+ out ImmutableArray diagnostics)
+ {
+ ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
+
+ // We need a separate local here so we can keep the original value to apply
+ // diagnostics to, but without returning invalid values to the caller which
+ // might cause generator errors (eg. -1 would cause other code to just throw).
+ int rawInputCount = 0;
+
+ inputCount = 0;
+
+ ImmutableArray.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder();
+ ImmutableArray.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder();
+
+ foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes())
+ {
+ switch (attributeData.AttributeClass?.GetFullMetadataName())
+ {
+ case "ComputeSharp.D2D1.D2DInputCountAttribute":
+ rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
+ inputCount = Math.Max(rawInputCount, 0);
+ break;
+ case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
+ inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
+ break;
+ case "ComputeSharp.D2D1.D2DInputComplexAttribute":
+ inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
+ break;
+ default:
+ break;
+ }
+ }
+
+ inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
+ inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
+ combinedInputTypes = ImmutableArray.Empty;
+
+ // Validate the input count
+ if (rawInputCount is not (>= 0 and <= 8))
+ {
+ builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
+
+ goto End;
+ }
+
+ // All simple indices must be in range
+ if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
+ {
+ builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
+
+ goto End;
+ }
+
+ HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
+ HashSet inputComplexIndicesAsSet = new(inputComplexIndicesBuilder);
+
+ // All simple indices must be unique
+ if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
+ {
+ builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
+
+ goto End;
+ }
+
+ // All complex indices must be unique
+ if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
+ {
+ builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
+
+ goto End;
+ }
+
+ // Simple and complex indices can't have indices in common
+ if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
+ {
+ builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
+
+ goto End;
+ }
+
+ // Build the combined input types list now that inputs have been validated
+ ImmutableArray.Builder combinedBuilder = ImmutableArray.CreateBuilder(inputCount);
+
+ for (int i = 0; i < inputCount; i++)
+ {
+ // The D2D_INPUT_SIMPLE define is optional, and if omitted, the corresponding input
+ // will automatically be of complex type. So we just need to check whether the i-th
+ // input is present in the set of explicitly simple inputs. These values will map to
+ // values from D2D1PixelShaderInputType, which has Simple and Complex as members.
+ // So, simple inputs map to 0, and complex inputs map to 1.
+ combinedBuilder.Add(inputSimpleIndicesAsSet.Contains(i) ? 0u : 1u);
+ }
+
+ combinedInputTypes = combinedBuilder.MoveToImmutable();
+
+ End:
+ diagnostics = builder.ToImmutable();
+ }
+ }
+}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
index f616dd12..ca1ba01a 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -22,7 +22,7 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
///
- internal static partial class LoadBytecode
+ private static partial class LoadBytecode
{
///
/// Extracts the shader profile for the current shader.
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index 5851d946..12cd2d3b 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -49,9 +49,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
.Where(static item => IsD2D1PixelShaderType(item.Left.Symbol, item.Right))
.Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol)));
- // Get the dispatch data, HLSL source and embedded bytecode info. This info is computed on the
- // same step as parts are shared in following sub-branches in the incremental generator pipeline.
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source, int InputCount)> shaderInfoWithErrors =
+ // Get the dispatch data, input types, HLSL source and embedded bytecode info. This info is computed
+ // on the same step as parts are shared in following sub-branches in the incremental generator pipeline.
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result InputTypes, Result Source)> shaderInfoWithErrors =
shaderDeclarations
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
@@ -66,12 +66,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
DispatchDataInfo dispatchDataInfo = new(fieldInfos, root32BitConstantCount);
+ // Get the input info for GetInputInfo()
+ GetInputType.GetInfo(
+ item.Left.Symbol,
+ out int inputCount,
+ out ImmutableArray inputSimpleIndices,
+ out ImmutableArray inputComplexIndices,
+ out ImmutableArray inputTypes,
+ out ImmutableArray inputTypesDiagnostics);
+
+ InputTypesInfo inputTypesInfo = new(inputTypes);
+
// Get HLSL source for BuildHlslSource()
string hlslSource = BuildHlslSource.GetHlslSource(
item.Right,
item.Left.Syntax,
item.Left.Symbol,
- out int inputCount,
+ inputCount,
+ inputSimpleIndices,
+ inputComplexIndices,
out ImmutableArray hlslSourceDiagnostics);
token.ThrowIfCancellationRequested();
@@ -92,18 +105,19 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
return (
item.Left.Hierarchy,
new Result(dispatchDataInfo, dispatchDataDiagnostics),
- new Result(sourceInfo, hlslSourceDiagnostics),
- inputCount);
+ new Result(inputTypesInfo, inputTypesDiagnostics),
+ new Result(sourceInfo, hlslSourceDiagnostics));
});
// Output the diagnostics
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors));
+ context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.InputTypes.Errors));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors));
// Filter items to enable caching for the input count methods
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
shaderInfoWithErrors
- .Select(static (item, token) => (item.Hierarchy, item.InputCount))
+ .Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length))
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default);
// Generate the GetInputCount() methods
@@ -116,10 +130,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
});
// Filter all items to enable caching at a coarse level, and remove diagnostics
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo =
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
shaderInfoWithErrors
- .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.Source.Value))
- .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer.Default);
+ .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.InputTypes.Value, item.Source.Value))
+ .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer.Default);
+
+ // Get a filtered sequence to enable caching
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
+ shaderInfo
+ .Select(static (item, token) => (item.Hierarchy, item.InputTypes))
+ .WithComparers(HierarchyInfo.Comparer.Default, InputTypesInfo.Comparer.Default);
+
+ // Generate the GetInputType() methods
+ context.RegisterSourceOutput(inputTypesInfo, static (context, item) =>
+ {
+ MethodDeclarationSyntax getInputTypeMethod = GetInputType.GetSyntax(item.InputTypes.InputTypes);
+ CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputTypeMethod, false);
+
+ context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputType)}", compilationUnit.ToFullString());
+ });
// Get a filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo =
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs
new file mode 100644
index 00000000..0a3c10c2
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using ComputeSharp.SourceGeneration.Extensions;
+using ComputeSharp.SourceGeneration.Helpers;
+
+namespace ComputeSharp.D2D1.SourceGenerators.Models;
+
+///
+/// A model representing gathered info on a shader input types.
+///
+/// The input types for a given shader.
+internal sealed record InputTypesInfo(ImmutableArray InputTypes)
+{
+ ///
+ /// An implementation for .
+ ///
+ public sealed class Comparer : Comparer
+ {
+ ///
+ protected override void AddToHashCode(ref HashCode hashCode, InputTypesInfo obj)
+ {
+ hashCode.AddRange(obj.InputTypes);
+ }
+
+ ///
+ protected override bool AreEqual(InputTypesInfo x, InputTypesInfo y)
+ {
+ return x.InputTypes.SequenceEqual(y.InputTypes);
+ }
+ }
+}
diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
index 012eca14..a2163e54 100644
--- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
+++ b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
@@ -45,6 +45,18 @@ internal static partial class ThrowHelper
throw new ArgumentOutOfRangeException(name);
}
+ ///
+ /// Throws a new .
+ ///
+ /// The argument name.
+ /// The message to include in the exception.
+ /// Thrown with the specified parameter.
+ [DoesNotReturn]
+ public static void ThrowArgumentOutOfRangeException(string? name, string? message)
+ {
+ throw new ArgumentOutOfRangeException(name, message);
+ }
+
///
/// Throws a new .
///
diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
index 7d503b40..b9d2593a 100644
--- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
+++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
@@ -27,6 +27,16 @@ public interface ID2D1Shader
[Obsolete("This method is not intended to be used directly by user code")]
uint GetInputCount();
+ ///
+ /// Gets the input type for the input at the input index.
+ ///
+ /// The index of the shader input to get the type for.
+ /// The type of input at the specified index.
+ /// The return value if the input is out of range is undefined.
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This method is not intended to be used directly by user code")]
+ uint GetInputType(uint index);
+
///
/// Loads the dispatch data for the shader.
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs
index b9540bfb..d4dbe25c 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
+using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Shaders.Dispatching;
using ComputeSharp.D2D1.Shaders.Interop.Buffers;
using TerraFX.Interop.DirectX;
@@ -92,6 +93,26 @@ public static unsafe class D2D1InteropServices
return shader.GetInputCount();
}
+ ///
+ /// Gets the type of a given input for a D2D1 pixel shader.
+ ///
+ /// The type of D2D1 pixel shader to get the input type for.
+ /// The index of the input to get the type for.
+ /// The type of the input of the target D2D1 pixel shader at the specified index.
+ /// Thrown if is not in range for the available inputs for the shader type.
+ public static D2D1PixelShaderInputType GetPixelShaderInputType(uint index)
+ where T : unmanaged, ID2D1PixelShader
+ {
+ Unsafe.SkipInit(out T shader);
+
+ if (index >= shader.GetInputCount())
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is outside of range for the target pixel shader type.");
+ }
+
+ return (D2D1PixelShaderInputType)shader.GetInputType(index);
+ }
+
///
/// Gets the constant buffer from an input D2D1 pixel shader.
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs
new file mode 100644
index 00000000..19ae7aec
--- /dev/null
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderInputType.cs
@@ -0,0 +1,31 @@
+namespace ComputeSharp.D2D1.Interop;
+
+///
+/// Indicates the type of a given input for a D2D1 pixel shader.
+///
+public enum D2D1PixelShaderInputType
+{
+ ///
+ /// The input is of simple type.
+ ///
+ ///
+ ///
+ /// A pixel shader is of simple type when the consuming pixel shader requires only a single input value to perform its computation. This value would normally
+ /// come from sampling an input texture at the texture coordinate emitted by the vertex shader. Such a pixel shader is said to perform simple sampling.
+ ///
+ /// For more info, see .
+ ///
+ Simple,
+
+ ///
+ /// The input is of complex type.
+ ///
+ ///
+ ///
+ /// Some pixel shaders, such as a Gaussian blur, compute their output from multiple input samples
+ /// rather than just a single sample. Such a pixel shader is said to perform complex sampling.
+ ///
+ /// For more info, see .
+ ///
+ Complex
+}
diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs
index a7d0e73d..f7a26034 100644
--- a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs
@@ -46,6 +46,29 @@ internal static class IncrementalValuesProviderExtensions
return source.WithComparer(new Comparer(comparer1, comparer2, comparer3));
}
+ ///
+ /// Creates a new instance with a gven pair of comparers.
+ ///
+ /// The type of first items in each tuple.
+ /// The type of second items in each tuple.
+ /// The type of third items in each tuple.
+ /// The type of fourth items in each tuple.
+ /// The input instance.
+ /// An instance for items.
+ /// An instance for items.
+ /// An instance for items.
+ /// An instance for items.
+ /// An with the specified comparers applied to each item.
+ public static IncrementalValuesProvider<(T1, T2, T3, T4)> WithComparers(
+ this IncrementalValuesProvider<(T1, T2, T3, T4)> source,
+ IEqualityComparer comparer1,
+ IEqualityComparer comparer2,
+ IEqualityComparer comparer3,
+ IEqualityComparer comparer4)
+ {
+ return source.WithComparer(new Comparer(comparer1, comparer2, comparer3, comparer4));
+ }
+
///
/// An implementation for a value tuple.
///
@@ -140,4 +163,65 @@ internal static class IncrementalValuesProviderExtensions
this.comparer3.GetHashCode(obj.Item3));
}
}
+
+ ///
+ /// An implementation for a value tuple.
+ ///
+ public sealed class Comparer : IEqualityComparer<(T1, T2, T3, T4)>
+ {
+ ///
+ /// The comparer.
+ ///
+ private readonly IEqualityComparer comparer1;
+
+ ///
+ /// The comparer.
+ ///
+ private readonly IEqualityComparer comparer2;
+
+ ///
+ /// The comparer.
+ ///
+ private readonly IEqualityComparer comparer3;
+
+ ///
+ /// The comparer.
+ ///
+ private readonly IEqualityComparer comparer4;
+
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The comparer.
+ /// The comparer.
+ /// The comparer.
+ /// The comparer.
+ public Comparer(IEqualityComparer comparer1, IEqualityComparer comparer2, IEqualityComparer comparer3, IEqualityComparer comparer4)
+ {
+ this.comparer1 = comparer1;
+ this.comparer2 = comparer2;
+ this.comparer3 = comparer3;
+ this.comparer4 = comparer4;
+ }
+
+ ///
+ public bool Equals((T1, T2, T3, T4) x, (T1, T2, T3, T4) y)
+ {
+ return
+ this.comparer1.Equals(x.Item1, y.Item1) &&
+ this.comparer2.Equals(x.Item2, y.Item2) &&
+ this.comparer3.Equals(x.Item3, y.Item3) &&
+ this.comparer4.Equals(x.Item4, y.Item4);
+ }
+
+ ///
+ public int GetHashCode((T1, T2, T3, T4) obj)
+ {
+ return HashCode.Combine(
+ this.comparer1.GetHashCode(obj.Item1),
+ this.comparer2.GetHashCode(obj.Item2),
+ this.comparer3.GetHashCode(obj.Item3),
+ this.comparer4.GetHashCode(obj.Item4));
+ }
+ }
}
diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs
index 0e8ef2d8..9916fff0 100644
--- a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs
+++ b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs
@@ -6,7 +6,7 @@ namespace ComputeSharp.D2D1.Tests;
[TestClass]
[TestCategory("D2D1InteropServices")]
-public class D2D1InteropServicesTests
+public partial class D2D1InteropServicesTests
{
[TestMethod]
public unsafe void GetInputCount()
@@ -14,5 +14,32 @@ public class D2D1InteropServicesTests
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 1u);
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 1u);
Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 0u);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 7u);
+ }
+
+ [TestMethod]
+ public unsafe void GetInputType()
+ {
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(0), D2D1PixelShaderInputType.Simple);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(1), D2D1PixelShaderInputType.Complex);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(2), D2D1PixelShaderInputType.Simple);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(3), D2D1PixelShaderInputType.Complex);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(4), D2D1PixelShaderInputType.Complex);
+ Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputType(5), D2D1PixelShaderInputType.Complex);
+ }
+
+ [D2DInputCount(7)]
+ [D2DInputSimple(0)]
+ [D2DInputSimple(2)]
+ [D2DInputComplex(1)]
+ [D2DInputComplex(3)]
+ [D2DInputComplex(5)]
+ [D2DInputSimple(6)]
+ partial struct ShaderWithMultipleInputs : ID2D1PixelShader
+ {
+ public Float4 Execute()
+ {
+ return 0;
+ }
}
}
\ No newline at end of file