From a6b87b551cd23ed55e5591ff8bb4d2176c97a70b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 16:33:51 +0200 Subject: [PATCH 1/6] Generate input types as ReadOnlyMemory --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 1 + ...nerator.CreateGetInputTypeMethod.Syntax.cs | 219 +++++++++++++++++- .../ID2D1ShaderGenerator.Helpers.cs | 12 +- .../ID2D1ShaderGenerator.cs | 4 +- .../Interfaces/__Internals/ID2D1Shader.cs | 9 +- .../Shaders/Interop/D2D1PixelShader.cs | 14 +- .../Effects/PixelShaderEffect.For{T}.cs | 5 +- .../Models/HierarchyInfo.cs | 55 +++-- 8 files changed, 268 insertions(+), 51 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 5691b5fb..5629c18d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -85,6 +85,7 @@ + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs index 6bb75339..0e194dc4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs @@ -1,5 +1,7 @@ using System.Collections.Immutable; using ComputeSharp.D2D1.__Internals; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -18,21 +20,218 @@ partial class ID2D1ShaderGenerator /// 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) + /// The resulting instance for the GetInputTypeMethod method. + public static (MemberDeclarationSyntax Member, MemberDeclarationSyntax Type) 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(nameof(GetInputType))) + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => InputTypesMemoryManager.Memory; + MemberDeclarationSyntax member = + PropertyDeclaration( + GenericName(Identifier("global::System.ReadOnlyMemory")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), + Identifier("InputTypes")) // TODO: use nameof() .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)))); + .WithExpressionBody(ArrowExpressionClause( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("InputTypesMemoryManager"), + IdentifierName("Memory")))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + + return (member, GetMemoryManagerDeclaration(inputTypes)); + } + + /// + /// Gets the memory manager declaration for the input type data. + /// + /// The input types for the shader. + /// The memory manager declaration for the input type data. + private static TypeDeclarationSyntax GetMemoryManagerDeclaration(ImmutableArray inputTypes) + { + // Create the MemoryManager declaration: + // + // /// + // /// implementation to get the input types. + // /// + // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")] + // [global::System.Diagnostics.DebuggerNonUserCode] + // [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + // file sealed InputTypesMemoryManager : global::System.Buffers.MemoryManager + // { + // } + TypeDeclarationSyntax typeDeclaration = + ClassDeclaration("InputTypesMemoryManager") + .AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.SealedKeyword)) + .AddBaseListTypes(SimpleBaseType( + GenericName(Identifier("global::System.Buffers.MemoryManager")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")))) + .AddAttributeLists( + AttributeList(SingletonSeparatedList( + Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))), + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))), + AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), + AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))) + .WithLeadingTrivia( + Comment("/// "), + Comment("""/// A implementation to get the input types."""), + Comment("/// ")); + + using ImmutableArrayBuilder memberDeclarations = ImmutableArrayBuilder.Rent(); + + // Declare the singleton property to get the memory instance: + // + // /// The singleton instance for the memory manager. + // public static new readonly global::System.ReadOnlyMemory Memory = new InputTypesMemoryManager().CreateMemory(); + memberDeclarations.Add( + FieldDeclaration( + VariableDeclaration( + GenericName(Identifier("global::System.ReadOnlyMemory")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType"))) + .AddVariables( + VariableDeclarator(Identifier("Memory")) + .WithInitializer( + EqualsValueClause( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + ObjectCreationExpression(IdentifierName("InputTypesMemoryManager")).WithArgumentList(ArgumentList()), + IdentifierName("CreateMemory"))) + .AddArgumentListArguments(Argument( + LiteralExpression( + SyntaxKind.NumericLiteralExpression, + Literal(inputTypes.Length)))))))) + .AddModifiers( + Token(SyntaxKind.PublicKeyword), + Token(SyntaxKind.StaticKeyword), + Token(SyntaxKind.NewKeyword), + Token(SyntaxKind.ReadOnlyKeyword)) + .WithLeadingTrivia(Comment("""/// The singleton instance for the memory manager."""))); + + using (ImmutableArrayBuilder inputTypeExpressions = ImmutableArrayBuilder.Rent()) + { + // Build the sequence of expressions for all input types + foreach (uint inputType in inputTypes) + { + inputTypeExpressions.Add( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType"), + IdentifierName(inputType == 0 ? "Simple" : "Complex"))); + } + + // Construct the RVA span property: + // + // /// The RVA data with the input type info. + // private static global::System.ReadOnlySpan Data => new[] { }; + memberDeclarations.Add( + PropertyDeclaration( + GenericName(Identifier("global::System.ReadOnlySpan")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), + Identifier("Data")) + .AddModifiers(Token(SyntaxKind.PrivateKeyword), Token(SyntaxKind.StaticKeyword)) + .WithExpressionBody( + ArrowExpressionClause( + ImplicitArrayCreationExpression( + InitializerExpression( + SyntaxKind.ArrayInitializerExpression, + SeparatedList(inputTypeExpressions.ToArray()))))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)) + .WithLeadingTrivia(Comment("/// The RVA data with the input type info."))); + } + + // Add the GetSpan() method: + // + // /// + // public override unsafe global::System.Span GetSpan + // { + // return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), ); + // } + memberDeclarations.Add( + MethodDeclaration( + GenericName(Identifier("global::System.Span")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), + Identifier("GetSpan")) + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword), Token(SyntaxKind.UnsafeKeyword)) + .AddBodyStatements( + ReturnStatement( + ImplicitObjectCreationExpression() + .AddArgumentListArguments( + Argument( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.Unsafe"), + IdentifierName("AsPointer"))) + .AddArgumentListArguments( + Argument( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.InteropServices.MemoryMarshal"), + IdentifierName("GetReference"))) + .AddArgumentListArguments(Argument(IdentifierName("Data")))) + .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))), + Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes.Length)))))) + .WithLeadingTrivia(Comment("/// "))); + + // Add the Pin(int elementIndex) method: + // + // /// + // public override unsafe global::System.Buffers.MemoryHandle Pin(int elementIndex) + // { + // return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this); + // } + memberDeclarations.Add( + MethodDeclaration(IdentifierName("global::System.Buffers.MemoryHandle"), Identifier("Pin")) + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword), Token(SyntaxKind.UnsafeKeyword)) + .AddParameterListParameters(Parameter(Identifier("elementIndex")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword)))) + .AddBodyStatements( + ReturnStatement( + ImplicitObjectCreationExpression() + .AddArgumentListArguments( + Argument( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.Unsafe"), + IdentifierName("AsPointer"))) + .AddArgumentListArguments( + Argument( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::System.Runtime.CompilerServices.Unsafe"), + IdentifierName("AsRef"))) + .AddArgumentListArguments( + Argument( + ElementAccessExpression(IdentifierName("Data")) + .AddArgumentListArguments(Argument(IdentifierName("elementIndex")))) + .WithRefOrOutKeyword(Token(SyntaxKind.InKeyword)))) + .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))), + Argument(ThisExpression()).WithNameColon(NameColon(IdentifierName("pinnable")))))) + .WithLeadingTrivia(Comment("/// "))); + + // Add the empty Unpin() method + memberDeclarations.Add( + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("Unpin")) + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)) + .WithBody(Block()) + .WithLeadingTrivia(Comment("/// "))); + + // Add the empty Dispose(bool disposing) method + memberDeclarations.Add( + MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("Dispose")) + .AddModifiers(Token(SyntaxKind.ProtectedKeyword), Token(SyntaxKind.OverrideKeyword)) + .AddParameterListParameters( + Parameter(Identifier("disposing")).WithType(PredefinedType(Token(SyntaxKind.BoolKeyword)))) + .WithBody(Block()) + .WithLeadingTrivia(Comment("/// "))); + + return typeDeclaration.AddMembers(memberDeclarations.ToArray()); } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index 58baed10..ab1f0d5d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -38,13 +38,15 @@ partial class ID2D1ShaderGenerator /// Creates a instance wrapping the given method. /// /// The instance for the current type. - /// The item to insert. + /// The item to insert. /// Whether [SkipLocalsInit] can be used. - /// A object wrapping . + /// Additional member declarations to also emit, if any. + /// A object wrapping . private static CompilationUnitSyntax GetCompilationUnitFromMember( HierarchyInfo hierarchyInfo, - MemberDeclarationSyntax memberDeclarationSyntax, - bool canUseSkipLocalsInit) + MemberDeclarationSyntax memberDeclaration, + bool canUseSkipLocalsInit, + params MemberDeclarationSyntax[] additionalMemberDeclarations) { // Method attributes List attributes = new() @@ -71,6 +73,6 @@ partial class ID2D1ShaderGenerator attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit"))))); } - return hierarchyInfo.GetSyntax(memberDeclarationSyntax.AddAttributeLists(attributes.ToArray())); + return hierarchyInfo.GetSyntax(new MemberDeclarationSyntax[] { memberDeclaration.AddAttributeLists(attributes.ToArray()) }, additionalMemberDeclarations); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 8684d51e..9df520a3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -265,8 +265,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Generate the GetInputType() methods context.RegisterSourceOutput(inputTypesInfo, static (context, item) => { - MethodDeclarationSyntax getInputTypeMethod = GetInputType.GetSyntax(item.InputTypes.InputTypes); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false); + (MemberDeclarationSyntax property, MemberDeclarationSyntax type) = GetInputType.GetSyntax(item.InputTypes.InputTypes); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, property, canUseSkipLocalsInit: false, additionalMemberDeclarations: type); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputType)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 07f99fb6..861c1dd7 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using ComputeSharp.D2D1.Interop; namespace ComputeSharp.D2D1.__Internals; @@ -77,14 +78,12 @@ public interface ID2D1Shader uint GetInputCount(); /// - /// Gets the input type for the input at the input index. + /// Gets the input types for the current shader. /// - /// 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. + /// The input types for the current shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - uint GetInputType(uint index); + ReadOnlyMemory InputTypes { get; } /// /// Loads the input descriptions for the shader, if any. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 3f09285b..b146d8ac 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -259,20 +259,16 @@ public static class D2D1PixelShader } /// - /// Gets the type of a given input for a D2D1 pixel shader. + /// Gets the input types 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 GetInputType(int index) + /// The type of D2D1 pixel shader to get the input types for. + /// The input types of the target D2D1 pixel shader. + public static ReadOnlyMemory GetInputTypes() where T : unmanaged, ID2D1PixelShader { Unsafe.SkipInit(out T shader); - default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, (int)shader.GetInputCount()); - - return (D2D1PixelShaderInputType)shader.GetInputType((uint)index); + return shader.InputTypes; } /// 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 52eb31d9..133025da 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -175,10 +175,7 @@ internal unsafe partial struct PixelShaderEffect int inputCount = D2D1PixelShader.GetInputCount(); D2D1PixelShaderInputType* inputTypes = (D2D1PixelShaderInputType*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), sizeof(D2D1PixelShaderInputType) * inputCount); - for (int i = 0; i < inputCount; i++) - { - inputTypes[i] = D2D1PixelShader.GetInputType(i); - } + D2D1PixelShader.GetInputTypes().Span.CopyTo(new Span(inputTypes, inputCount)); // Prepare the input descriptions ReadOnlyMemory inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions(); diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index d2118b3e..c0ef8fd0 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -1,3 +1,4 @@ +using System; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; @@ -47,6 +48,17 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName, /// The member declarations to add to the generated type. /// A instance for the current hierarchy. public CompilationUnitSyntax GetSyntax(params MemberDeclarationSyntax[] memberDeclarations) + { + return GetSyntax(memberDeclarations, Array.Empty()); + } + + /// + /// Creates a instance for the current hierarchy. + /// + /// The member declarations to add to the generated type. + /// Additional top-level member declarations, if any. + /// A instance for the current hierarchy. + public CompilationUnitSyntax GetSyntax(MemberDeclarationSyntax[] memberDeclarations, params MemberDeclarationSyntax[] additionalMemberDeclarations) { // Create the partial type declaration with for the current hierarchy. // This code produces a type declaration as follows: @@ -78,6 +90,8 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName, Comment("// "), Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))); + CompilationUnitSyntax compilationUnitSyntax; + if (Namespace is "") { // If there is no namespace, attach the pragma directly to the declared type, @@ -85,24 +99,33 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName, // // // - return + compilationUnitSyntax = CompilationUnit() - .AddMembers(typeDeclarationSyntax.WithLeadingTrivia(syntaxTriviaList)) - .NormalizeWhitespace(eol: "\n"); + .AddMembers(typeDeclarationSyntax.WithLeadingTrivia(syntaxTriviaList)); + } + else + { + // Create the compilation unit with disabled warnings, target namespace and generated type. + // This will produce code as follows: + // + // + // namespace ; + // + // + compilationUnitSyntax = + CompilationUnit().AddMembers( + FileScopedNamespaceDeclaration(IdentifierName(Namespace)) + .WithLeadingTrivia(syntaxTriviaList) + .AddMembers(typeDeclarationSyntax)); } - // Create the compilation unit with disabled warnings, target namespace and generated type. - // This will produce code as follows: - // - // - // namespace ; - // - // - return - CompilationUnit().AddMembers( - FileScopedNamespaceDeclaration(IdentifierName(Namespace)) - .WithLeadingTrivia(syntaxTriviaList) - .AddMembers(typeDeclarationSyntax)) - .NormalizeWhitespace(eol: "\n"); + // Add any additional members, if any + if (additionalMemberDeclarations.Length > 0) + { + compilationUnitSyntax = compilationUnitSyntax.AddMembers(additionalMemberDeclarations); + } + + // Normalize and return the tree + return compilationUnitSyntax.NormalizeWhitespace(eol: "\n"); } } \ No newline at end of file From 41ca3a0b1d10b2c323d26f21925bfbcd486557f5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 16:49:45 +0200 Subject: [PATCH 2/6] Rename and update source generator files --- ...nerator.CreateInputTypesProperty.Syntax.cs} | 18 +++++++++--------- ...haderGenerator.CreateInputTypesProperty.cs} | 4 ++-- .../ID2D1ShaderGenerator.cs | 14 +++++++------- .../Interfaces/__Internals/ID2D1Shader.cs | 1 - 4 files changed, 18 insertions(+), 19 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs => ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs} (96%) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs => ID2D1ShaderGenerator.CreateInputTypesProperty.cs} (98%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs similarity index 96% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs index 0e194dc4..66612f3d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs @@ -14,24 +14,26 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - partial class GetInputType + partial class InputTypes { /// - /// Creates a instance for the GetInputTypeMethod method. + /// Creates a instance for the InputTypes property. /// /// The input types for the shader. - /// The resulting instance for the GetInputTypeMethod method. - public static (MemberDeclarationSyntax Member, MemberDeclarationSyntax Type) GetSyntax(ImmutableArray inputTypes) + /// An additional instance needed by the generated code. + /// The resulting instance for the InputTypes property. + public static PropertyDeclarationSyntax GetSyntax(ImmutableArray inputTypes, out TypeDeclarationSyntax additionalType) { + additionalType = GetMemoryManagerDeclaration(inputTypes); + // This code produces a method declaration as follows: // // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => InputTypesMemoryManager.Memory; - MemberDeclarationSyntax member = + return PropertyDeclaration( GenericName(Identifier("global::System.ReadOnlyMemory")) .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), - Identifier("InputTypes")) // TODO: use nameof() + Identifier(nameof(InputTypes))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .WithExpressionBody(ArrowExpressionClause( @@ -40,8 +42,6 @@ partial class ID2D1ShaderGenerator IdentifierName("InputTypesMemoryManager"), IdentifierName("Memory")))) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); - - return (member, GetMemoryManagerDeclaration(inputTypes)); } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.cs similarity index 98% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.cs index 4d79a732..7b2d19a7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.cs @@ -12,9 +12,9 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetInputType method. + /// A helper with all logic to generate the InputTypes properties. /// - private static partial class GetInputType + private static partial class InputTypes { /// /// Extracts the input info for the current shader. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 9df520a3..289258ac 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -91,8 +91,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - // Get the input info for GetInputInfo() - GetInputType.GetInfo( + // Get the input info for InputTypes + InputTypes.GetInfo( diagnostics, typeSymbol, out int inputCount, @@ -257,18 +257,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the GetInputType() info (hierarchy and input types) + // Get the InputTypes info (hierarchy and input types) IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.InputTypes)); - // Generate the GetInputType() methods + // Generate the InputTypes properties context.RegisterSourceOutput(inputTypesInfo, static (context, item) => { - (MemberDeclarationSyntax property, MemberDeclarationSyntax type) = GetInputType.GetSyntax(item.InputTypes.InputTypes); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, property, canUseSkipLocalsInit: false, additionalMemberDeclarations: type); + MemberDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax additionalType); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputTypesProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalType); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputType)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputTypes)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the LoadResourceTextureDescriptions() info (hierarchy and resource texture descriptions) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 861c1dd7..d826f0c6 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -80,7 +80,6 @@ public interface ID2D1Shader /// /// Gets the input types for the current shader. /// - /// The input types for the current shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] ReadOnlyMemory InputTypes { get; } From 5829bfe9ec1a52d7f204a1256e3e53f043e0d382 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 17:09:12 +0200 Subject: [PATCH 3/6] Handle InputTypes generation for no inputs --- ...nerator.CreateInputTypesProperty.Syntax.cs | 32 +++++++++++++------ .../ID2D1ShaderGenerator.cs | 4 +-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs index 66612f3d..3a767a23 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Immutable; using ComputeSharp.D2D1.__Internals; using ComputeSharp.SourceGeneration.Helpers; @@ -20,15 +21,32 @@ partial class ID2D1ShaderGenerator /// Creates a instance for the InputTypes property. /// /// The input types for the shader. - /// An additional instance needed by the generated code. + /// Any additional instances needed by the generated code, if needed. /// The resulting instance for the InputTypes property. - public static PropertyDeclarationSyntax GetSyntax(ImmutableArray inputTypes, out TypeDeclarationSyntax additionalType) + public static PropertyDeclarationSyntax GetSyntax(ImmutableArray inputTypes, out TypeDeclarationSyntax[] additionalTypes) { - additionalType = GetMemoryManagerDeclaration(inputTypes); + ExpressionSyntax memoryExpression; + + // If there are no inputs, simply return a default expression. + // Otherwise, declare the memory manager and access it. + if (inputTypes.Length == 0) + { + memoryExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); + additionalTypes = Array.Empty(); + } + else + { + memoryExpression = MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("InputTypesMemoryManager"), + IdentifierName("Memory")); + + additionalTypes = new TypeDeclarationSyntax[] { GetMemoryManagerDeclaration(inputTypes) }; + } // This code produces a method declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => InputTypesMemoryManager.Memory; + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => ; return PropertyDeclaration( GenericName(Identifier("global::System.ReadOnlyMemory")) @@ -36,11 +54,7 @@ partial class ID2D1ShaderGenerator Identifier(nameof(InputTypes))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithExpressionBody(ArrowExpressionClause( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("InputTypesMemoryManager"), - IdentifierName("Memory")))) + .WithExpressionBody(ArrowExpressionClause(memoryExpression)) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 289258ac..43553b4b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -265,8 +265,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Generate the InputTypes properties context.RegisterSourceOutput(inputTypesInfo, static (context, item) => { - MemberDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax additionalType); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputTypesProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalType); + MemberDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax[] additionalTypes); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputTypesProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalTypes); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputTypes)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); From 46a070c03dc4c6d7563a22d7cede2ee3d7966553 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 17:09:21 +0200 Subject: [PATCH 4/6] Update unit tests for new InputTypes property --- .../D2D1PixelShaderTests.cs | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index abbee5eb..815651b5 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -23,14 +23,40 @@ public partial class D2D1PixelShaderTests } [TestMethod] - public unsafe void GetInputType() + public unsafe void GetInputTypes_NoInputs() { - Assert.AreEqual(D2D1PixelShader.GetInputType(0), D2D1PixelShaderInputType.Simple); - Assert.AreEqual(D2D1PixelShader.GetInputType(1), D2D1PixelShaderInputType.Complex); - Assert.AreEqual(D2D1PixelShader.GetInputType(2), D2D1PixelShaderInputType.Simple); - Assert.AreEqual(D2D1PixelShader.GetInputType(3), D2D1PixelShaderInputType.Complex); - Assert.AreEqual(D2D1PixelShader.GetInputType(4), D2D1PixelShaderInputType.Complex); - Assert.AreEqual(D2D1PixelShader.GetInputType(5), D2D1PixelShaderInputType.Complex); + ReadOnlyMemory inputTypes = D2D1PixelShader.GetInputTypes(); + + Assert.AreEqual(0, inputTypes.Length); + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(inputTypes, out MemoryManager? _)); + } + + [TestMethod] + public unsafe void GetInputTypes_MultipleInputs() + { + CollectionAssert.AreEqual(new[] + { + D2D1PixelShaderInputType.Simple, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Simple, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Simple + }, D2D1PixelShader.GetInputTypes().ToArray()); + } + + [TestMethod] + public unsafe void GetInputTypes_SameManager() + { + ReadOnlyMemory inputTypes1 = D2D1PixelShader.GetInputTypes(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes1, out MemoryManager? memoryManager1)); + + ReadOnlyMemory inputTypes2 = D2D1PixelShader.GetInputTypes(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes2, out MemoryManager? memoryManager2)); + Assert.AreSame(memoryManager1, memoryManager2); } [D2DInputCount(7)] From be95b2bfc87710775789e07d4f9bb816fc4d95cd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 17:31:02 +0200 Subject: [PATCH 5/6] Remove leftover GetInputTypesSwitchExpression method --- ...nerator.CreateInputTypesProperty.Syntax.cs | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs index 3a767a23..f6ee5550 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs @@ -247,48 +247,5 @@ partial class ID2D1ShaderGenerator return typeDeclaration.AddMembers(memberDeclarations.ToArray()); } - - /// - /// 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))); - } } } \ No newline at end of file From f61cee8fdf3bf68df8b8fe42a45422a58373b49c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 20:13:27 +0200 Subject: [PATCH 6/6] Optimize generated Memory for input types --- ...nerator.CreateInputTypesProperty.Syntax.cs | 86 +++++++++++++------ 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs index f6ee5550..e7cd8e02 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs @@ -36,9 +36,16 @@ partial class ID2D1ShaderGenerator } else { - memoryExpression = MemberAccessExpression( + // Create a ReadOnlyMemory instance from the memory manager: + // + // InputTypesMemoryManager.Instance.Memory + memoryExpression = + MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("InputTypesMemoryManager"), + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("InputTypesMemoryManager"), + IdentifierName("Instance")), IdentifierName("Memory")); additionalTypes = new TypeDeclarationSyntax[] { GetMemoryManagerDeclaration(inputTypes) }; @@ -46,7 +53,7 @@ partial class ID2D1ShaderGenerator // This code produces a method declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => ; + // readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => ; return PropertyDeclaration( GenericName(Identifier("global::System.ReadOnlyMemory")) @@ -96,34 +103,21 @@ partial class ID2D1ShaderGenerator using ImmutableArrayBuilder memberDeclarations = ImmutableArrayBuilder.Rent(); - // Declare the singleton property to get the memory instance: + // Declare the singleton property to get the memory manager: // - // /// The singleton instance for the memory manager. - // public static new readonly global::System.ReadOnlyMemory Memory = new InputTypesMemoryManager().CreateMemory(); + // /// The singleton instance to use. + // public static readonly InputTypesMemoryManager Instance = new(); memberDeclarations.Add( FieldDeclaration( - VariableDeclaration( - GenericName(Identifier("global::System.ReadOnlyMemory")) - .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType"))) + VariableDeclaration(IdentifierName("InputTypesMemoryManager")) .AddVariables( - VariableDeclarator(Identifier("Memory")) - .WithInitializer( - EqualsValueClause( - InvocationExpression( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - ObjectCreationExpression(IdentifierName("InputTypesMemoryManager")).WithArgumentList(ArgumentList()), - IdentifierName("CreateMemory"))) - .AddArgumentListArguments(Argument( - LiteralExpression( - SyntaxKind.NumericLiteralExpression, - Literal(inputTypes.Length)))))))) + VariableDeclarator(Identifier("Instance")) + .WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))) .AddModifiers( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword), - Token(SyntaxKind.NewKeyword), Token(SyntaxKind.ReadOnlyKeyword)) - .WithLeadingTrivia(Comment("""/// The singleton instance for the memory manager."""))); + .WithLeadingTrivia(Comment("""/// The singleton instance to use."""))); using (ImmutableArrayBuilder inputTypeExpressions = ImmutableArrayBuilder.Rent()) { @@ -162,7 +156,7 @@ partial class ID2D1ShaderGenerator // /// // public override unsafe global::System.Span GetSpan // { - // return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), ); + // return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), Data.Length); // } memberDeclarations.Add( MethodDeclaration( @@ -189,7 +183,49 @@ partial class ID2D1ShaderGenerator IdentifierName("GetReference"))) .AddArgumentListArguments(Argument(IdentifierName("Data")))) .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))), - Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes.Length)))))) + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("Data"), + IdentifierName("Length")))))) + .WithLeadingTrivia(Comment("/// "))); + + // Override the Memory property: + // + // /// + // public override global::System.Memory Memory + // { + // [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + // get => CreateMemory(Data.Length); + // } + memberDeclarations.Add( + PropertyDeclaration( + GenericName(Identifier("global::System.Memory")) + .AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")), + Identifier("Memory")) + .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)) + .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"))))))) + .WithExpressionBody( + ArrowExpressionClause( + InvocationExpression(IdentifierName("CreateMemory")) + .AddArgumentListArguments( + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("Data"), + IdentifierName("Length")))))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken))) .WithLeadingTrivia(Comment("/// "))); // Add the Pin(int elementIndex) method: