From ed24a3e4dc584b5f5dd065202b62ff1da43fb8f7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 18:02:48 +0200 Subject: [PATCH] Improve generated HLSL bytecode MemoryManager --- ...nerator.CreateLoadBytecodeMethod.Syntax.cs | 88 +++++++++++++------ 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs index 7b782c29..0ac2d906 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs @@ -94,10 +94,17 @@ partial class ID2D1ShaderGenerator } else { - memoryExpression = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("HlslBytecodeMemoryManager"), - IdentifierName("Memory")); + // Create a ReadOnlyMemory instance from the memory manager: + // + // HlslBytecodeMemoryManager.Instance.Memory + memoryExpression = + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("HlslBytecodeMemoryManager"), + IdentifierName("Instance")), + IdentifierName("Memory")); TypeDeclarationSyntax memoryManagerDeclaration = GetMemoryManagerDeclaration(bytecodeInfo); string bytecodeLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(bytecodeInfo.Bytecode.AsSpan()); @@ -158,34 +165,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 HlslBytecodeMemoryManager().CreateMemory(); + // /// The singleton instance to use. + // public static readonly HlslBytecodeMemoryManager Instance = new(); memberDeclarations.Add( FieldDeclaration( - VariableDeclaration( - GenericName(Identifier("global::System.ReadOnlyMemory")) - .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)))) + VariableDeclaration(IdentifierName("HlslBytecodeMemoryManager")) .AddVariables( - VariableDeclarator(Identifier("Memory")) - .WithInitializer( - EqualsValueClause( - InvocationExpression( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - ObjectCreationExpression(IdentifierName("HlslBytecodeMemoryManager")).WithArgumentList(ArgumentList()), - IdentifierName("CreateMemory"))) - .AddArgumentListArguments(Argument( - LiteralExpression( - SyntaxKind.NumericLiteralExpression, - Literal(bytecodeInfo.Bytecode.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."""))); // Construct the RVA span property: // @@ -208,12 +202,50 @@ partial class ID2D1ShaderGenerator .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)) .WithLeadingTrivia(Comment("/// The RVA data with the HLSL bytecode."))); + // 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(PredefinedType(Token(SyntaxKind.ByteKeyword))), + 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 GetSpan() method: // // /// // 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( @@ -240,7 +272,11 @@ partial class ID2D1ShaderGenerator IdentifierName("GetReference"))) .AddArgumentListArguments(Argument(IdentifierName("Data")))) .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))), - Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(bytecodeInfo.Bytecode.Length)))))) + Argument( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("Data"), + IdentifierName("Length")))))) .WithLeadingTrivia(Comment("/// "))); // Add the Pin(int elementIndex) method: