diff --git a/src/ComputeSharp.Core/Attributes/FieldAttribute.cs b/src/ComputeSharp.Core/Attributes/FieldAttribute.cs index 2cb59602..98ee33d2 100644 --- a/src/ComputeSharp.Core/Attributes/FieldAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/FieldAttribute.cs @@ -7,7 +7,7 @@ namespace ComputeSharp /// This is necessary to be used instead of manually declare fields, so that the fields can be /// declared with the right offset into the type data, as HLSL requires precise alignment for values. /// - [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)] public sealed class FieldAttribute : Attribute { /// diff --git a/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs index 27ec7a6d..0449cb68 100644 --- a/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs @@ -26,46 +26,39 @@ namespace ComputeSharp.SourceGenerators public void Execute(GeneratorExecutionContext context) { // Find all the [Field] usages - var attributes = new Queue>( + var attributes = new Queue>( from tree in context.Compilation.SyntaxTrees from structDeclaration in tree.GetRoot().DescendantNodes().OfType() let semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree) let structSymbol = semanticModel.GetDeclaredSymbol(structDeclaration) from attribute in structSymbol.GetAttributes() where attribute.AttributeClass is { Name: nameof(FieldAttribute) } - group attribute by structSymbol into groups + group attribute by structDeclaration into groups select groups); - foreach (IGrouping group in attributes) + foreach (IGrouping group in attributes) { - StructDeclarationSyntax structDeclaration = null!; - SemanticModel semanticModel = null!;// context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree); - INamedTypeSymbol structDeclarationSymbol = null!;// semanticModel.GetDeclaredSymbol(structDeclaration)!; + SemanticModel semanticModel = context.Compilation.GetSemanticModel(group.Key.SyntaxTree); + INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(group.Key)!; - _ = GetProcessedFields(group); + var generatedTypeInfo = GetProcessedFields(group); // Extract the info on the type to process var namespaceName = structDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)); - var structName = structDeclaration.Identifier.Text; - var structModifiers = structDeclaration.Modifiers; + var structName = group.Key.Identifier.Text; + var structModifiers = group.Key.Modifiers; - // Create the constructor declaration for the type. This will - // produce a constructor with simple initialization of all variables: - // - // public MyType(Foo a, Bar b, Baz c, ...) - // { - // this.a = a; - // this.b = b; - // this.c = c; - // ... - // } + // Declare the partial type with the right layout attribute to track the size and + // the HLSL packing, and add the generated field declarations requested by the user. var structDeclarationSyntax = - StructDeclaration(structName).WithModifiers(structModifiers); + StructDeclaration(structName).WithModifiers(structModifiers) + .AddAttributeLists(AttributeList(SingletonSeparatedList(generatedTypeInfo.Layout))) + .AddMembers(generatedTypeInfo.Fields.ToArray()); TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax; // Add all parent types in ascending order, if any - foreach (var parentType in structDeclaration.Ancestors().OfType()) + foreach (var parentType in group.Key.Ancestors().OfType()) { typeDeclarationSyntax = parentType .WithMembers(SingletonList(typeDeclarationSyntax)) @@ -75,7 +68,6 @@ namespace ComputeSharp.SourceGenerators } // Create the compilation unit with the namespace and target member. - // From this, we can finally generate the source code to output. var source = CompilationUnit().AddMembers( NamespaceDeclaration(IdentifierName(namespaceName)).AddMembers(typeDeclarationSyntax)) @@ -150,6 +142,9 @@ namespace ComputeSharp.SourceGenerators fieldDeclarations.Add(fieldDeclaration); } + // Add the trailing padding, if needed + size += size % pack; + // Get the attribute for the layout of the entire type var layoutAttribute = Attribute(IdentifierName(typeof(StructLayoutAttribute).FullName)).AddArgumentListArguments( @@ -157,9 +152,9 @@ namespace ComputeSharp.SourceGenerators SyntaxKind.SimpleMemberAccessExpression, IdentifierName(typeof(LayoutKind).FullName), IdentifierName(nameof(LayoutKind.Explicit)))), - AttributeArgument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(8))) + AttributeArgument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(size))) .WithNameEquals(NameEquals(IdentifierName(nameof(StructLayoutAttribute.Size)))), - AttributeArgument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(16))) + AttributeArgument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(pack))) .WithNameEquals(NameEquals(IdentifierName(nameof(StructLayoutAttribute.Pack))))); return (layoutAttribute, fieldDeclarations);