using System.Collections.Immutable; using ComputeSharp.D2D1.__Internals; using ComputeSharp.D2D1.SourceGenerators.Models; 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 { /// private static partial class LoadResourceTextureDescriptions { /// /// Creates a instance for the LoadResourceTextureDescriptions method. /// /// The resource texture descriptions info gathered for the current shader. /// The resulting instance for the LoadResourceTextureDescriptions method. public static MethodDeclarationSyntax GetSyntax(ResourceTextureDescriptionsInfo resourceTextureDescriptionsInfo) { // This code produces a method declaration as follows: // // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadResourceTextureDescriptions(ref TLoader loader) // { // // } return MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadResourceTextureDescriptions))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TLoader"))) .AddParameterListParameters(Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader"))) .WithBody(Block(GetInputDescriptionsLoadingStatements(resourceTextureDescriptionsInfo.ResourceTextureDescriptions))); } /// /// Gets a sequence of statements to load the resource texture descriptions for a given shader. /// /// The array of values for all available resource texture descriptions. /// The sequence of instances to load the resource texture descriptions data. private static ImmutableArray GetInputDescriptionsLoadingStatements(ImmutableArray resourceTextureDescriptionsInfo) { // If there are no resource texture descriptions available, just load an empty buffer if (resourceTextureDescriptionsInfo.IsEmpty) { // loader.LoadResourceTextureDescriptions(default); return ImmutableArray.Create( ExpressionStatement( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("loader"), IdentifierName("LoadResourceTextureDescriptions"))) .AddArgumentListArguments(Argument( LiteralExpression( SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))))); } ImmutableArray.Builder statements = ImmutableArray.CreateBuilder(); // The size of the buffer with the resource texture descriptions is the number of resurce textures descriptions, // times the size of each input description, which is a struct containing two int-sized fields (index and rank). int resourceTextureDescriptionSizeInBytes = resourceTextureDescriptionsInfo.Length * sizeof(int) * 2; // global::System.Span data = stackalloc byte[]; statements.Insert(0, LocalDeclarationStatement( VariableDeclaration( GenericName(Identifier("global::System.Span")) .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)))) .AddVariables( VariableDeclarator(Identifier("data")) .WithInitializer(EqualsValueClause( StackAllocArrayCreationExpression( ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword))) .AddRankSpecifiers( ArrayRankSpecifier(SingletonSeparatedList( LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(resourceTextureDescriptionSizeInBytes))))))))))); // ref byte r0 = ref data[0]; statements.Insert(1, LocalDeclarationStatement( VariableDeclaration(RefType(PredefinedType(Token(SyntaxKind.ByteKeyword)))) .AddVariables( VariableDeclarator(Identifier("r0")) .WithInitializer(EqualsValueClause( RefExpression( ElementAccessExpression(IdentifierName("data")) .AddArgumentListArguments(Argument( LiteralExpression( SyntaxKind.NumericLiteralExpression, Literal(0)))))))))); int offset = 0; // Generate loading statements for each resource texture description foreach (ResourceTextureDescription resourceTextureDescription in resourceTextureDescriptionsInfo) { // Write the index of the current resource texture description: // // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint))) = ; statements.Add(ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){offset}))"), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)resourceTextureDescription.Index))))); // Write the rank of the current resource texture description: // // global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint) + 4)) = ; statements.Add(ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ParseExpression($"global::System.Runtime.CompilerServices.Unsafe.As(ref global::System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref r0, (nint){offset + 4}))"), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)resourceTextureDescription.Rank))))); offset += sizeof(int) * 2; } // loader.LoadInputDescriptions(data); statements.Add( ExpressionStatement( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("loader"), IdentifierName("LoadResourceTextureDescriptions"))) .AddArgumentListArguments(Argument(IdentifierName("data"))))); return statements.ToImmutable(); } } }