From b21193288195686ff2126c9e43abd65507f03e6e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 May 2022 19:24:11 +0200 Subject: [PATCH] Enable generation for ID2D1Shader.GetPixelOptions API --- ...ator.CreateGetPixelOptionsMethod.Syntax.cs | 36 +++++++++++++++++++ ...erGenerator.CreateGetPixelOptionsMethod.cs | 31 ++++++++++++++++ .../ID2D1ShaderGenerator.cs | 24 +++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs new file mode 100644 index 00000000..499462da --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs @@ -0,0 +1,36 @@ +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 GetPixelOptions + { + /// + /// Creates a instance for the GetPixelOptions method. + /// + /// The pixel options for the shader. + /// The resulting instance for the GetPixelOptions method. + public static MethodDeclarationSyntax GetSyntax(D2D1PixelOption pixelOptions) + { + // This code produces a method declaration as follows: + // + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetPixelOptions() + // { + // return ; + // } + return + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetPixelOptions))) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)pixelOptions))))); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs new file mode 100644 index 00000000..c4787c3d --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs @@ -0,0 +1,31 @@ +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the GetPixelOptions method. + /// + private static partial class GetPixelOptions + { + /// + /// Extracts the pixel options for the current shader. + /// + /// The input instance to process. + /// The pixel options for the shader. + public static void GetInfo(INamedTypeSymbol structDeclarationSymbol, out D2D1PixelOption pixelOptions) + { + if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DPixelOptionsAttribute", out AttributeData? attributeData)) + { + pixelOptions = (D2D1PixelOption)attributeData!.ConstructorArguments[0].Value!; + } + else + { + pixelOptions = D2D1PixelOption.None; + } + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index b3314ae2..73b8ac3f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -305,5 +305,29 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadInputDescriptions)}", compilationUnit.ToFullString()); }); + + // Get the pixel options, again independently from other generation steps. + // This can also be immediately cached at a fine grained level. + IncrementalValuesProvider<(HierarchyInfo Hierarchy, D2D1PixelOption Options)> pixelOptionsInfo = + shaderDeclarations + .Select(static (item, token) => + { + // GetPixelOptions() info + GetPixelOptions.GetInfo(item.Symbol, out D2D1PixelOption pixelOptions); + + token.ThrowIfCancellationRequested(); + + return (item.Hierarchy, pixelOptions); + }) + .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); + + // Generate the GetPixelOptions() methods + context.RegisterSourceOutput(pixelOptionsInfo, static (context, item) => + { + MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.Options); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getPixelOptionsMethod, false); + + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetPixelOptions)}", compilationUnit.ToFullString()); + }); } }