diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs new file mode 100644 index 00000000..d0496ef4 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs @@ -0,0 +1,38 @@ +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 +{ + /// + /// A helper with all logic to generate the GetInputCount method. + /// + partial class GetInputCount + { + /// + /// Creates a instance for the GetInputCount method. + /// + /// The number of inputs for the shader. + /// The resulting instance for the GetInputCount method. + public static MethodDeclarationSyntax GetSyntax(int inputCount) + { + // This code produces a method declaration as follows: + // + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() + // { + // return ; + // } + return + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount")) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount))))); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 62b802d5..5851d946 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -51,7 +51,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // 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)> shaderInfoWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source, int InputCount)> shaderInfoWithErrors = shaderDeclarations .Combine(context.CompilationProvider) .Select(static (item, token) => @@ -92,13 +92,29 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return ( item.Left.Hierarchy, new Result(dispatchDataInfo, dispatchDataDiagnostics), - new Result(sourceInfo, hlslSourceDiagnostics)); + new Result(sourceInfo, hlslSourceDiagnostics), + inputCount); }); // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.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)) + .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); + + // Generate the GetInputCount() methods + context.RegisterSourceOutput(inputCountInfo, static (context, item) => + { + MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputCountMethod, false); + + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}", compilationUnit.ToFullString()); + }); + // Filter all items to enable caching at a coarse level, and remove diagnostics IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors @@ -106,7 +122,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer.Default); // Get a filtered sequence to enable caching - IncrementalValuesProvider< (HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = shaderInfo .Select(static (item, token) => (item.Hierarchy, item.Dispatch)) .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 8159e2ef..7d503b40 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -19,6 +19,14 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be called directly by user code")] void InitializeFromDispatchData(ReadOnlySpan data); + /// + /// Gets the number of inputs for the current shader. + /// + /// The number of inputs for the current shader. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + uint GetInputCount(); + /// /// 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 957cacd7..0ee94a6b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs @@ -79,6 +79,19 @@ public static unsafe class D2D1InteropServices return new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray(); } + /// + /// Gets the number of inputs from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to get the input count for. + /// The number of inputs for the D2D1 pixel shader of type . + public static uint GetPixelShaderInputCount() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + return shader.GetInputCount(); + } + /// /// Gets the constant buffer from an input D2D1 pixel shader. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index cc8fbf5a..8ba2107a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -89,7 +89,7 @@ internal unsafe partial struct PixelShaderEffect { // Load all shader properties Guid guid = typeof(T).GUID; - int inputCount = ((D2DInputCountAttribute[])typeof(T).GetCustomAttributes(typeof(D2DInputCountAttribute), false))[0].Count; + int inputCount = (int)D2D1InteropServices.GetPixelShaderInputCount(); ReadOnlyMemory buffer = D2D1InteropServices.LoadShaderBytecode(); byte* typeAssociatedMemory = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), buffer.Length); diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs new file mode 100644 index 00000000..0e8ef2d8 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/D2D1InteropServicesTests.cs @@ -0,0 +1,18 @@ +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Tests.Effects; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests; + +[TestClass] +[TestCategory("D2D1InteropServices")] +public class D2D1InteropServicesTests +{ + [TestMethod] + public unsafe void GetInputCount() + { + Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 1u); + Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 1u); + Assert.AreEqual(D2D1InteropServices.GetPixelShaderInputCount(), 0u); + } +} \ No newline at end of file