From bf9b1231b0f5ded42b8650e7e3dc5d40035a2a66 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 17:13:51 +0200 Subject: [PATCH 1/5] Remove [AutoConstructor] attributes --- .../Primitives/ComplexVector4.cs | 7 ++--- .../Attributes/AutoConstructorAttribute.cs | 17 ------------ .../Internals/AutoConstructorBehavior.cs | 12 --------- .../AutoConstructorBehaviorAttribute.cs | 26 ------------------- .../Resources/D2D1ResourceTexture1D{T}.cs | 1 - .../Resources/D2D1ResourceTexture2D{T}.cs | 1 - .../Resources/D2D1ResourceTexture3D{T}.cs | 1 - 7 files changed, 4 insertions(+), 61 deletions(-) delete mode 100644 src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs delete mode 100644 src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehavior.cs delete mode 100644 src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs index d8dabc18..a2e10c84 100644 --- a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs +++ b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs @@ -4,8 +4,6 @@ using System.Numerics; using System.Runtime.CompilerServices; -#pragma warning disable IDE0022 - namespace SixLabors.ImageSharp; /// @@ -30,5 +28,8 @@ internal struct ComplexVector4 /// The 'b' parameter, for the imaginary component /// The resulting value [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Vector4 WeightedSum(float a, float b) => (this.Real * a) + (this.Imaginary * b); + public readonly Vector4 WeightedSum(float a, float b) + { + return (this.Real * a) + (this.Imaginary * b); + } } \ No newline at end of file diff --git a/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs b/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs deleted file mode 100644 index c71aeaa2..00000000 --- a/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.ComponentModel; - -namespace ComputeSharp; - -/// -/// An attribute that indicates that a target shader type should get an automatic constructor for all fields. -/// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete( - "This API (and its accompanying source generator) is being removed in a future version of ComputeSharp. " + - "If you're relying on this functionality, consider switching to primary constructors, once they are supported. " + - "Otherwise, consider using alternative source generators providing similar functionality available on NuGet.")] -public sealed class AutoConstructorAttribute : Attribute -{ -} \ No newline at end of file diff --git a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehavior.cs b/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehavior.cs deleted file mode 100644 index 465bc2b7..00000000 --- a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehavior.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace ComputeSharp; - -/// -/// Indicates the behavior of fields annotated with . -/// -internal enum AutoConstructorBehavior -{ - /// - /// Causes to ignore fields and set their values to . - /// - IgnoreAndSetToDefault -} \ No newline at end of file diff --git a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs b/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs deleted file mode 100644 index 3c1c696a..00000000 --- a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -#pragma warning disable IDE0290 - -namespace ComputeSharp; - -/// -/// An attribute that indicates that a target shader type should get an automatic constructor for all fields. -/// -[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)] -internal sealed class AutoConstructorBehaviorAttribute : Attribute -{ - /// - /// Creates a new instance with the specified parameters. - /// - /// The value for the current instance. - public AutoConstructorBehaviorAttribute(AutoConstructorBehavior behavior) - { - Behavior = behavior; - } - - /// - /// Gets the value for the current instance. - /// - public AutoConstructorBehavior Behavior { get; } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs index e9fdc34d..7a321e10 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture1D{T}.cs @@ -7,7 +7,6 @@ namespace ComputeSharp.D2D1; /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// /// The type of items stored on the texture (only and are supported). -[AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] public readonly struct D2D1ResourceTexture1D where T : unmanaged { diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs index 2df57a16..c2f1c8c4 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture2D{T}.cs @@ -7,7 +7,6 @@ namespace ComputeSharp.D2D1; /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// /// The type of items stored on the texture (only and are supported). -[AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] public readonly struct D2D1ResourceTexture2D where T : unmanaged { diff --git a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs index b953356d..32cc272c 100644 --- a/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs +++ b/src/ComputeSharp.D2D1/Resources/D2D1ResourceTexture3D{T}.cs @@ -7,7 +7,6 @@ namespace ComputeSharp.D2D1; /// This can be used from D2D1 pixel shaders to be able to pass arbitrary inputs to them. /// /// The type of items stored on the texture (only and are supported). -[AutoConstructorBehavior(AutoConstructorBehavior.IgnoreAndSetToDefault)] public readonly struct D2D1ResourceTexture3D where T : unmanaged { From a20263728636e007d750017a06fcb075f6fa5bfe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 17:15:20 +0200 Subject: [PATCH 2/5] Remove [AutoConstructor] generator logic --- ComputeSharp.sln | 1 - .../AutoConstructorGenerator.cs | 149 ------------------ .../ComputeSharp.Core.SourceGenerators.csproj | 12 -- .../Models/ConstructorInfo.cs | 10 -- .../Models/ParameterInfo.cs | 8 - 5 files changed, 180 deletions(-) delete mode 100644 src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs delete mode 100644 src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs delete mode 100644 src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 6f92e011..4797a17e 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -512,7 +512,6 @@ Global src\ComputeSharp.CodeFixing\ComputeSharp.CodeFixing.projitems*{9b4448b1-200f-4966-8a13-a508691b3003}*SharedItemsImports = 5 src\ComputeSharp.Win32.D2D1\ComputeSharp.Win32.D2D1.projitems*{9da1da9f-f8b2-4b25-be80-c21f773029e3}*SharedItemsImports = 13 samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{9ea5ae9d-c39a-4f43-b03e-0a848ea2558a}*SharedItemsImports = 5 - src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{c12d7ace-98ed-4813-8118-6667c34f484f}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs deleted file mode 100644 index 0b980bc7..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ /dev/null @@ -1,149 +0,0 @@ -using System.Linq; -using System.Text; -using ComputeSharp.Core.SourceGenerators.Models; -using ComputeSharp.SourceGeneration.Extensions; -using ComputeSharp.SourceGeneration.Helpers; -using ComputeSharp.SourceGeneration.Models; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -#pragma warning disable CS0618, IDE0300 - -namespace ComputeSharp.Core.SourceGenerators; - -/// -/// A source generator creating constructors for types annotated with . -/// -[Generator(LanguageNames.CSharp)] -public sealed class AutoConstructorGenerator : IIncrementalGenerator -{ - /// - public void Initialize(IncrementalGeneratorInitializationContext context) - { - // Get all declared struct symbols with the [AutoConstructor] attribute and - // extract the type hierarchy and fields info for each of the annotated types - IncrementalValuesProvider<(HierarchyInfo Left, ConstructorInfo Right)> constructorInfo = - context.SyntaxProvider - .ForAttributeWithMetadataName( - typeof(AutoConstructorAttribute).FullName, - static (node, _) => node is StructDeclarationSyntax structDeclaration, - static (context, _) => - { - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)context.TargetSymbol; - - ConstructorInfo constructorInfo = Ctor.GetData(typeSymbol); - - // If the type has no parameters, just do nothing - if (constructorInfo.Parameters.IsEmpty) - { - return default; - } - - HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol); - - return (Hierarchy: hierarchyInfo, constructorInfo); - }) - .Where(static item => item.Hierarchy is not null); - - // Generate the constructors - context.RegisterSourceOutput(constructorInfo, static (context, item) => - { - CompilationUnitSyntax compilationUnit = Ctor.GetSyntax(item.Left, item.Right); - - context.AddSource($"{item.Left.FullyQualifiedMetadataName}.Ctor.g.cs", compilationUnit.GetText(Encoding.UTF8)); - }); - } - - /// - /// A helper with all logic to generate the constructor declarations. - /// - private static class Ctor - { - /// - /// Gets the sequence of items for all fields in the input type. - /// - /// The being inspected. - /// The instance for . - public static ConstructorInfo GetData(INamedTypeSymbol structDeclarationSymbol) - { - using ImmutableArrayBuilder parameters = new(); - using ImmutableArrayBuilder defaulted = new(); - - foreach (IFieldSymbol fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) - { - // Skip fields that are not instance ones (and also ignore generated fields and fixed size buffers) - if (fieldSymbol is not { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }) - { - continue; - } - - // Check whether the field is annotated to have a special behavior - if (fieldSymbol.Type.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.AutoConstructorBehaviorAttribute", out AttributeData? usageData) && - usageData.ConstructorArguments is [{ Value: (int)AutoConstructorBehavior.IgnoreAndSetToDefault }]) - { - defaulted.Add(fieldSymbol.Name); - } - else - { - // Track the field normally - string typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); - - parameters.Add(new ParameterInfo(typeName, fieldSymbol.Name)); - } - } - - return new(parameters.ToImmutable(), defaulted.ToImmutable()); - } - - /// - /// Gets the instance for a given type and sequence of parameters. - /// - /// The type hierarchy info. - /// The instance with the info for the constructor to generate. - /// A instance for a specified type. - public static CompilationUnitSyntax GetSyntax(HierarchyInfo hierarchyInfo, ConstructorInfo constructorInfo) - { - // 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; - // ... - // this.ignored1 = default; - // this.ignored2 = default; - // this.ignored3 = default; - // ... - // } - ConstructorDeclarationSyntax constructorDeclaration = - ConstructorDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName) - .AddModifiers(Token(SyntaxKind.PublicKeyword)) - .AddParameterListParameters(constructorInfo.Parameters.Select(field => Parameter(Identifier(field.Name)).WithType(IdentifierName(field.Type))).ToArray()) - .AddBodyStatements(constructorInfo.Parameters.Select(field => ParseStatement($"this.{field.Name} = {field.Name};")).ToArray()) - .AddBodyStatements(constructorInfo.DefaultedFields.Select(field => ParseStatement($"this.{field} = default;")).ToArray()); - - // Add the unsafe modifier, if needed - if (constructorInfo.Parameters.Any(static param => param.Type.Contains("*"))) - { - constructorDeclaration = constructorDeclaration.AddModifiers(Token(SyntaxKind.UnsafeKeyword)); - } - - // Constructor attributes - AttributeListSyntax[] attributes = - { - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).FullName))), - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).Assembly.GetName().Version.ToString())))))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))) - }; - - return hierarchyInfo.GetSyntax(constructorDeclaration.AddAttributeLists(attributes)); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj index 56bbb7f6..f42af995 100644 --- a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj +++ b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj @@ -2,15 +2,6 @@ netstandard2.0 - - - - - - - @@ -20,7 +11,4 @@ - - - diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs deleted file mode 100644 index db5ee135..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs +++ /dev/null @@ -1,10 +0,0 @@ -using ComputeSharp.SourceGeneration.Helpers; - -namespace ComputeSharp.Core.SourceGenerators.Models; - -/// -/// A model containiing information on a constructor to generate. -/// -/// The values for the constructor. -/// The names of ignored fields that should be defaulted. -internal sealed record ConstructorInfo(EquatableArray Parameters, EquatableArray DefaultedFields); \ No newline at end of file diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs deleted file mode 100644 index 3f45388a..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ComputeSharp.Core.SourceGenerators.Models; - -/// -/// A model describing info on a constructor parameter. -/// -/// The fully qualified type name. -/// The parameter name. -internal sealed record ParameterInfo(string Type, string Name); \ No newline at end of file From a4a78365637efd7152dfc52f0c7153e2037932fa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 02:14:01 +0100 Subject: [PATCH 3/5] Remove leftover unit tests for [AutoConstructor] --- .../AutoConstructorGeneratorTests.cs | 114 ------------------ .../AutoConstructorAttributeTests.cs | 89 -------------- 2 files changed, 203 deletions(-) delete mode 100644 tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs delete mode 100644 tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs deleted file mode 100644 index 7d897463..00000000 --- a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using ComputeSharp.Core.SourceGenerators; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.Tests.SourceGenerators; - -[TestClass] -[TestCategory("AutoConstructorGenerator")] -public class AutoConstructorGeneratorTests -{ - [TestMethod] - public void GenerateConstructor() - { - const string source = """ - using System.Numerics; - using ComputeSharp; - - namespace ComputeSharp - { - public class ReadWriteBuffer { } - } - - namespace MyFancyApp.Sample - { - public partial class IFoo : IFluff - where T : notnull, new() - { - public partial class Foo - { - [AutoConstructor] - public readonly partial struct Test - { - private readonly float a; - private readonly Vector2 b; - public readonly ReadWriteBuffer c; - public readonly ReadWriteBuffer d; - } - } - } - } - """; - - string expected = $$""" - // - #pragma warning disable - namespace MyFancyApp.Sample; - partial class IFoo - { - partial class Foo - { - partial struct Test - { - [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.Core.SourceGenerators.AutoConstructorGenerator", "{{typeof(AutoConstructorGenerator).Assembly.GetName().Version}}")] - [global::System.Diagnostics.DebuggerNonUserCode] - [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - public Test(float a, global::System.Numerics.Vector2 b, global::ComputeSharp.ReadWriteBuffer c, global::ComputeSharp.ReadWriteBuffer d) - { - this.a = a; - this.b = b; - this.c = c; - this.d = d; - } - } - } - } - """; - - VerifyGeneratedMethodLines(source, 0, expected); - } - - /// - /// Verifies the output of a source generator. - /// - /// The generator type to use. - /// The input source to process. - /// The target index to check in the resulting output. - /// The expected body to compare with the generated code. - private static void VerifyGeneratedMethodLines(string source, int index, string expectedBody) - where TGenerator : class, IIncrementalGenerator, new() - { - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); - - IEnumerable references = - from assembly in AppDomain.CurrentDomain.GetAssemblies() - where !assembly.IsDynamic - let reference = MetadataReference.CreateFromFile(assembly.Location) - select reference; - - CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - - IIncrementalGenerator generator = new TGenerator(); - - CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(generator); - - _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); - - string outputBody = outputCompilation.SyntaxTrees.Skip(index + 1).First().ToString(); - - string[] outputLines = outputBody.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - string[] expectedLines = expectedBody.Replace("\r", "").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - - Assert.AreEqual(expectedLines.Length, outputLines.Length); - - for (int i = 0; i < outputLines.Length; i++) - { - Assert.AreEqual(expectedLines[i], outputLines[i]); - } - } -} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs deleted file mode 100644 index 88254195..00000000 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.Tests; - -[TestClass] -[TestCategory("AutoConstructorAttribute")] -public unsafe partial class AutoConstructorAttributeTests -{ - [AutoConstructor] - public partial struct StructWithFixedField - { - public int a; - public float b; - public byte* c; - public fixed byte d[16]; - } - - [TestMethod] - public void GenerateConstructorWithInheritedUnsafeContextAndIgnoredFixedField() - { - byte c = 123; - - StructWithFixedField instance = new(42, 3.14f, &c); - - Assert.AreEqual(instance.a, 42); - Assert.AreEqual(instance.b, 3.14f); - Assert.IsTrue(instance.c == &c); - } - - public partial interface ISomeInterface - { - public partial record SomeRecord - { - public partial struct SomeStruct - { - [AutoConstructor] - public partial struct AnotherStruct - { - public int a; - public float b; - } - } - } - } - - [TestMethod] - public void GenerateConstructorWithNestedTypes() - { - int a = 42; - float b = 3.14f; - - ISomeInterface.SomeRecord.SomeStruct.AnotherStruct instance = new(a, b); - - Assert.AreEqual(instance.a, a); - Assert.AreEqual(instance.b, b); - } - - public partial interface IAnotherInterface - { - public int B { get; } - - public void Foo(); - } - - [AutoConstructor] - public partial struct AnotherStruct : IAnotherInterface - { - public int a; - public float b; - - int IAnotherInterface.B => 100; - - void IAnotherInterface.Foo() - { - } - } - - [TestMethod] - public void GenerateConstructorWithExplicitInterfaceMembers() - { - int a = 42; - float b = 3.14f; - - AnotherStruct instance = new(a, b); - - Assert.AreEqual(instance.a, a); - Assert.AreEqual(instance.b, b); - } -} \ No newline at end of file From 9f4028e949c2919b7b46f59f86d691851447ae16 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 02:20:47 +0100 Subject: [PATCH 4/5] Switch unit tests to AutoConstructor package --- .../ComputeSharp.D2D1.Tests.csproj | 1 + .../D2D1PixelShaderTests.cs | 11 +--- .../D2D1ResourceTextureManagerTests.cs | 18 ++---- .../Effects/BokehBlurEffect.cs | 19 +++--- .../ComputeSharp.D2D1.WinUI.Tests.csproj | 1 + .../ComputeSharp.Tests.DeviceLost.csproj | 1 + ...arp.Tests.DisableDynamicCompilation.csproj | 1 + .../Program.cs | 7 +-- .../ComputeSharp.Tests.Internals.csproj | 1 + .../ShaderDataLoaderTests.cs | 58 +++++++++---------- .../ComputeSharp.Tests/ComputeContextTests.cs | 1 - .../ComputeSharp.Tests.csproj | 1 + tests/ComputeSharp.Tests/IPixelShaderTests.cs | 1 - .../ComputeSharp.Tests/ShaderCompilerTests.cs | 6 +- 14 files changed, 56 insertions(+), 71 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index a3fffde8..e4a71062 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 54f7d40b..a3c7fcb8 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -2,7 +2,6 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp; using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; @@ -323,7 +322,6 @@ namespace ComputeSharp.D2D1.Tests [D2DInputComplex(3)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] internal partial struct ShaderWithoutResourceTextures : ID2D1PixelShader { public Float4 Execute() @@ -335,7 +333,6 @@ namespace ComputeSharp.D2D1.Tests [D2DInputCount(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] internal readonly partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader { [D2DResourceTextureIndex(0)] @@ -354,11 +351,8 @@ namespace ComputeSharp.D2D1.Tests [D2DInputComplex(3)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal readonly partial struct ShaderWithResourceTextures : ID2D1PixelShader + internal readonly partial struct ShaderWithResourceTextures(float number) : ID2D1PixelShader { - readonly float number; - [D2DResourceTextureIndex(4)] readonly D2D1ResourceTexture1D myTexture1; @@ -381,7 +375,7 @@ namespace ComputeSharp.D2D1.Tests float4 pixel6 = this.myTexture3.Sample(0.4f, 0.5f, 0.6f); float number1 = this.myTexture4[0, 2]; float number2 = this.myTexture4.Sample(0.4f, 0.6f); - float number3 = this.number; + float number3 = number; return 0; } @@ -1128,7 +1122,6 @@ namespace ComputeSharp.D2D1.Tests { [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] public readonly partial struct DeeplyNestedShader : ID2D1PixelShader { public float4 Execute() diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 850933b3..cee6a83d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -535,11 +535,8 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal readonly partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader + internal readonly partial struct IndexFrom3DResourceTextureShader(float height) : ID2D1PixelShader { - private readonly int height; - [D2DResourceTextureIndex(0)] private readonly D2D1ResourceTexture3D source; @@ -548,8 +545,8 @@ public partial class D2D1ResourceTextureManagerTests int2 xy = (int2)D2D.GetScenePosition().XY; int x = xy.X; - int y = (int)((uint)xy.Y % (uint)this.height); - int z = (int)((uint)xy.Y / (uint)this.height); + int y = (int)((uint)xy.Y % (uint)height); + int z = (int)((uint)xy.Y / (uint)height); return this.source[x, y, z]; } @@ -895,11 +892,8 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal readonly partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader + internal readonly partial struct CopyFromResourceTexture3DShader(float height) : ID2D1PixelShader { - private readonly int height; - [D2DResourceTextureIndex(0)] private readonly D2D1ResourceTexture3D source; @@ -908,8 +902,8 @@ public partial class D2D1ResourceTextureManagerTests int2 xy = (int2)D2D.GetScenePosition().XY; int x = xy.X; - int y = (int)((uint)xy.Y % (uint)this.height); - int z = (int)((uint)xy.Y / (uint)this.height); + int y = (int)((uint)xy.Y % (uint)height); + int z = (int)((uint)xy.Y / (uint)height); return this.source[x, y, z]; } diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index 283f6c6e..52e413cc 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -493,10 +493,9 @@ public sealed partial class BokehBlurEffect [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - public readonly partial struct Shader : ID2D1PixelShader + public readonly partial struct Shader(int kernelLength) : ID2D1PixelShader { - public readonly int kernelLength; + public readonly int kernelLength = kernelLength; [D2DResourceTextureIndex(1)] private readonly D2D1ResourceTexture1D kernel; @@ -543,12 +542,12 @@ public sealed partial class BokehBlurEffect [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - public readonly partial struct Shader : ID2D1PixelShader + public readonly partial struct Shader( + int kernelLength, + float z, + float w) : ID2D1PixelShader { - public readonly int kernelLength; - private readonly float z; - private readonly float w; + public readonly int kernelLength = kernelLength; [D2DResourceTextureIndex(2)] private readonly D2D1ResourceTexture1D kernelReals; @@ -574,7 +573,7 @@ public sealed partial class BokehBlurEffect result.Imaginary += (Vector4)((realFactor * sourceImaginary) + (imaginaryFactor * sourceReal)); } - return result.WeightedSum(this.z, this.w); + return result.WeightedSum(z, w); } } } @@ -588,7 +587,6 @@ public sealed partial class BokehBlurEffect [D2DOutputBuffer(D2D1BufferPrecision.Float32, D2D1ChannelDepth.Four)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] internal readonly partial struct GammaHighlight : ID2D1PixelShader { /// @@ -611,7 +609,6 @@ public sealed partial class BokehBlurEffect [D2DOutputBuffer(D2D1BufferPrecision.Float32, D2D1ChannelDepth.Four)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] internal readonly partial struct InverseGammaHighlight : ID2D1PixelShader { /// diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj b/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj index ae3d7cdf..4cedb5f2 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj index 2f541db4..f7937242 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj +++ b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj @@ -10,6 +10,7 @@ + diff --git a/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj b/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj index 40e033b2..22d5e6e8 100644 --- a/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj +++ b/tests/ComputeSharp.Tests.DisableDynamicCompilation/ComputeSharp.Tests.DisableDynamicCompilation.csproj @@ -5,6 +5,7 @@ + diff --git a/tests/ComputeSharp.Tests.GlobalStatements/Program.cs b/tests/ComputeSharp.Tests.GlobalStatements/Program.cs index db039501..9b792313 100644 --- a/tests/ComputeSharp.Tests.GlobalStatements/Program.cs +++ b/tests/ComputeSharp.Tests.GlobalStatements/Program.cs @@ -21,13 +21,10 @@ if (!numbers.SequenceEqual(result)) Console.WriteLine("Test passed successfully!"); -[AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] -public readonly partial struct ApplyFunctionShader : IComputeShader +public readonly partial struct ApplyFunctionShader(ReadWriteBuffer sourceTexture) : IComputeShader { - private readonly ReadWriteBuffer sourceTexture; - public static float AddHalf(float input) { return input + 0.5f; @@ -35,6 +32,6 @@ public readonly partial struct ApplyFunctionShader : IComputeShader public void Execute() { - this.sourceTexture[ThreadIds.X] = AddHalf(this.sourceTexture[ThreadIds.X]); + sourceTexture[ThreadIds.X] = AddHalf(sourceTexture[ThreadIds.X]); } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 56163588..8246c7ce 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -5,6 +5,7 @@ + diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index df813a78..6378936d 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -16,7 +16,7 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct CapturedResourceShader : IComputeShader { - public readonly ReadWriteBuffer _; + public readonly ReadWriteBuffer _buffer; public void Execute() { @@ -52,8 +52,8 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct MultipleResourcesAndPrimitivesShader : IComputeShader { - public readonly ReadWriteBuffer _0; - public readonly ReadWriteBuffer _1; + public readonly ReadWriteBuffer _buffer0; + public readonly ReadWriteBuffer _buffer1; public readonly int a; public readonly int b; public readonly int c; @@ -98,7 +98,7 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct ScalarAndVectorTypesShader : IComputeShader { - public readonly ReadWriteBuffer _; + public readonly ReadWriteBuffer _buffer; public readonly float3 f3; public readonly int a; public readonly int b; @@ -149,7 +149,7 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct ScalarVectorAndMatrixTypesShader : IComputeShader { - public readonly ReadWriteBuffer _; + public readonly ReadWriteBuffer _buffer; public readonly float2x3 f2x3; public readonly int a; public readonly Int1x3 i1x3; @@ -218,16 +218,16 @@ public partial class ShaderDataLoaderTests } [AutoConstructor] - public partial struct SimpleTypes + public readonly partial struct SimpleTypes { - public float2x3 f2x3; - public int a; - public Int1x3 i1x3; - public double2 d2; - public int c; - public Int1x2 i1x2; - public int2x2 i2x2; - public int d; + public readonly float2x3 f2x3; + public readonly int a; + public readonly Int1x3 i1x3; + public readonly double2 d2; + public readonly int c; + public readonly Int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; } [AutoConstructor] @@ -235,7 +235,7 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct FlatCustomTypeShader : IComputeShader { - public readonly ReadWriteBuffer _0; + public readonly ReadWriteBuffer _buffer0; public readonly SimpleTypes a; public void Execute() @@ -298,28 +298,28 @@ public partial class ShaderDataLoaderTests } [AutoConstructor] - public partial struct CustomType1 + public readonly partial struct CustomType1 { - public float3 a; - public SimpleTypes b; - public int c; - public CustomType2 d; - public CustomType3 e; + public readonly float3 a; + public readonly SimpleTypes b; + public readonly int c; + public readonly CustomType2 d; + public readonly CustomType3 e; } [AutoConstructor] - public partial struct CustomType2 + public readonly partial struct CustomType2 { - public int a; - public float2 b; - public CustomType3 c; + public readonly int a; + public readonly float2 b; + public readonly CustomType3 c; } [AutoConstructor] - public partial struct CustomType3 + public readonly partial struct CustomType3 { - public int a; - public float2 b; + public readonly int a; + public readonly float2 b; } [AutoConstructor] @@ -327,7 +327,7 @@ public partial class ShaderDataLoaderTests [GeneratedComputeShaderDescriptor] public readonly partial struct NestedCustomTypesShader : IComputeShader { - public readonly ReadWriteBuffer _0; + public readonly ReadWriteBuffer _buffer0; public readonly CustomType1 a; public void Execute() diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index b86e04ef..f0ede1eb 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1308,7 +1308,6 @@ public partial class ComputeContextTests } } - [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.XY)] [GeneratedComputeShaderDescriptor] internal readonly partial struct ClearPixelShader : IComputeShader diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 352b5d75..54473c12 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/tests/ComputeSharp.Tests/IPixelShaderTests.cs b/tests/ComputeSharp.Tests/IPixelShaderTests.cs index 9bb23915..70a652a6 100644 --- a/tests/ComputeSharp.Tests/IPixelShaderTests.cs +++ b/tests/ComputeSharp.Tests/IPixelShaderTests.cs @@ -34,7 +34,6 @@ public partial class IPixelShaderTests } } - [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.XY)] [GeneratedComputeShaderDescriptor] internal readonly partial struct EarlyReturnShader : IComputeShader diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 0115bac2..afdbb256 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -230,10 +230,10 @@ namespace ComputeSharp.Tests } [AutoConstructor] - public partial struct CustomStructType + public readonly partial struct CustomStructType { - public float2 a; - public int b; + public readonly float2 a; + public readonly int b; } [AutoConstructor] From 70a2117d00c170cf21aed01b6259ac78d2d17bb2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 02:29:11 +0100 Subject: [PATCH 5/5] Remove [AutoConstructor] from integration tests --- tests/ComputeSharp.Dxc.NuGet/Program.cs | 5 +---- tests/ComputeSharp.NuGet/Program.cs | 5 +---- tests/ComputeSharp.Pix.NuGet/Program.cs | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/tests/ComputeSharp.Dxc.NuGet/Program.cs b/tests/ComputeSharp.Dxc.NuGet/Program.cs index 0274e8b8..458b60af 100644 --- a/tests/ComputeSharp.Dxc.NuGet/Program.cs +++ b/tests/ComputeSharp.Dxc.NuGet/Program.cs @@ -33,13 +33,10 @@ Trace.Assert(shaderInfo.BoundResourceCount == 2); /// /// A sample kernel that requires dynamic compilation, as it's not precompiled. /// -[AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] -internal readonly partial struct MultiplyByTwo : IComputeShader +internal readonly partial struct MultiplyByTwo(ReadWriteBuffer buffer) : IComputeShader { - public readonly ReadWriteBuffer buffer; - /// public void Execute() { diff --git a/tests/ComputeSharp.NuGet/Program.cs b/tests/ComputeSharp.NuGet/Program.cs index 48fd9a52..42cc0f11 100644 --- a/tests/ComputeSharp.NuGet/Program.cs +++ b/tests/ComputeSharp.NuGet/Program.cs @@ -27,11 +27,8 @@ for (int i = 0; i < array.Length; i++) /// [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] -[AutoConstructor] -internal readonly partial struct MultiplyByTwo : IComputeShader +internal readonly partial struct MultiplyByTwo(ReadWriteBuffer buffer) : IComputeShader { - public readonly ReadWriteBuffer buffer; - /// public void Execute() { diff --git a/tests/ComputeSharp.Pix.NuGet/Program.cs b/tests/ComputeSharp.Pix.NuGet/Program.cs index 0cd931e5..1e2b97b5 100644 --- a/tests/ComputeSharp.Pix.NuGet/Program.cs +++ b/tests/ComputeSharp.Pix.NuGet/Program.cs @@ -33,11 +33,8 @@ for (int i = 0; i < array.Length; i++) /// [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] -[AutoConstructor] -internal readonly partial struct MultiplyByTwo : IComputeShader +internal readonly partial struct MultiplyByTwo(ReadWriteBuffer buffer) : IComputeShader { - public readonly ReadWriteBuffer buffer; - /// public void Execute() {