diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs new file mode 100644 index 00000000..94057b71 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs @@ -0,0 +1,113 @@ +using System; +using System.Text; +using ComputeSharp.D2D1.__Internals; +using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Models; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class EffectId + { + /// + /// Creates a instance for the EffectId property. + /// + /// The input instance. + /// An opaque instance to transform the final tree into text. + /// The resulting instance for the EffectId property. + public static PropertyDeclarationSyntax GetSyntax(HierarchyInfo info, out Func fixup) + { + // Create the local declaration: + // + // global::System.ReadOnlySpan bytes = new byte[] { __EMBEDDED_EFFECT_ID_BYTES }; + LocalDeclarationStatementSyntax guidBytesDeclaration = + LocalDeclarationStatement( + VariableDeclaration( + GenericName(Identifier("global::System.ReadOnlySpan")) + .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)))) + .AddVariables( + VariableDeclarator(Identifier("bytes")) + .WithInitializer( + EqualsValueClause( + ArrayCreationExpression( + ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword))) + .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) + .WithInitializer( + InitializerExpression( + SyntaxKind.ArrayInitializerExpression, + SingletonSeparatedList(IdentifierName("__EMBEDDED_EFFECT_ID_BYTES")))))))); + + // Prepare the initialization text + string effectIdLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(new byte[] { 1, 2, 3, 4 }.AsSpan()); + + // Set the fixup function + fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_EFFECT_ID_BYTES", effectIdLiterals), Encoding.UTF8); + + // Prepare the return statement: + // + // return + // ref global::System.Runtime.CompilerServices.Unsafe.As( + // ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(bytes)); + ReturnStatementSyntax returnStatement = + ReturnStatement( + RefExpression( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.Unsafe"), + GenericName(Identifier("As")) + .AddTypeArgumentListArguments( + PredefinedType(Token(SyntaxKind.ByteKeyword)), + IdentifierName("global::System.Guid")))) + .AddArgumentListArguments( + Argument( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.InteropServices.MemoryMarshal"), + IdentifierName("GetReference"))) + .AddArgumentListArguments(Argument(IdentifierName("bytes")))) + .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword))))); + + // This code produces a method declaration as follows: + // + // readonly ref readonly global::System.Guid global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectId + // { + // [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + // get + // { + // + // + // } + // } + return + PropertyDeclaration( + RefType(IdentifierName("global::System.Guid")).WithReadOnlyKeyword(Token(SyntaxKind.ReadOnlyKeyword)), + Identifier(nameof(EffectId))) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .AddAccessorListAccessors( + AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) + .AddAttributeLists( + AttributeList(SingletonSeparatedList( + Attribute(IdentifierName("global::System.Runtime.CompilerServices.MethodImpl")) + .AddArgumentListArguments( + AttributeArgument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.MethodImplOptions"), + IdentifierName("AggressiveInlining"))))))) + .AddBodyStatements(guidBytesDeclaration, returnStatement)); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 4bec8a17..703bda66 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -136,7 +136,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator shaderInfoWithErrors .Select(static (item, _) => item.Diagnostcs)); - // Get the EffectDisplayName info (hierarchy) + // Get the EffectDisplayName and EffectId info (hierarchy) IncrementalValuesProvider hierarchyInfo = shaderInfoWithErrors .Select(static (item, _) => item.Hierarchy); @@ -150,6 +150,16 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); + // Generate the EffectId properties + context.RegisterSourceOutput(hierarchyInfo, static (context, item) => + { + PropertyDeclarationSyntax effectDisplayNameProperty = EffectId.GetSyntax(item, out Func fixup); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item, effectDisplayNameProperty, canUseSkipLocalsInit: false); + SourceText text = fixup(compilationUnit); + + context.AddSource($"{item.FullyQualifiedMetadataName}.{nameof(EffectId)}.g.cs", text); + }); + // Get the GetInputCount() info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = shaderInfoWithErrors diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 89a2a96f..3a6b4ce0 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -11,6 +11,14 @@ namespace ComputeSharp.D2D1.__Internals; [Obsolete("This interface is not intended to be used directly by user code")] public interface ID2D1Shader { + /// + /// Gets the effect id of the D2D effect using this shader. + /// + /// + /// This only applies to effects created from . + /// + ref readonly Guid EffectId { get; } + /// /// Gets the display name of the D2D effect using this shader. ///