diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs index 73f981cd..db5ee135 100644 --- a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs +++ b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.Core.SourceGenerators.Models; @@ -11,38 +7,4 @@ namespace ComputeSharp.Core.SourceGenerators.Models; /// /// The values for the constructor. /// The names of ignored fields that should be defaulted. -internal sealed record ConstructorInfo(ImmutableArray Parameters, ImmutableArray DefaultedFields) -{ - /// - public bool Equals(ConstructorInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, ConstructorInfo obj) - { - hashCode.AddRange(obj.Parameters); - hashCode.AddRange(obj.DefaultedFields); - } - - /// - protected override bool AreEqual(ConstructorInfo x, ConstructorInfo y) - { - return - x.Parameters.SequenceEqual(y.Parameters) && - x.DefaultedFields.SequenceEqual(y.DefaultedFields); - } - } -} \ No newline at end of file +internal sealed record ConstructorInfo(EquatableArray Parameters, EquatableArray DefaultedFields); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs index 44a47de8..e55cccc9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs @@ -87,7 +87,7 @@ partial class D2DPixelShaderSourceGenerator // return MethodDeclaration(returnType, Identifier(bytecodeInfo.MethodName)) - .AddModifiers(bytecodeInfo.Modifiers.Select(Token).ToArray()) + .AddModifiers(bytecodeInfo.Modifiers.Select(static m => Token((SyntaxKind)m)).ToArray()) .AddAttributeLists( AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 0e653800..1c58c1fb 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; @@ -41,7 +40,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato token.ThrowIfCancellationRequested(); // Get the remaining info for the current shader - ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => token.Kind()).ToImmutableArray(); + ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => (ushort)token.Kind()).ToImmutableArray(); string methodName = methodSymbol.Name; string? invalidReturnType = Execute.GetInvalidReturnType(diagnostics, methodSymbol); D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol); @@ -63,8 +62,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato // Output the diagnostics context.ReportDiagnostics( shaderInfoWithErrors - .Select(static (item, _) => item.Diagnostcs) - .WithComparer(EqualityComparer.Default.ForImmutableArray())); + .Select(static (item, _) => item.Diagnostcs)); // Compile the requested shader bytecodes IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DeferredDiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 01ee8226..ed1238b5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; @@ -135,8 +134,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Output the diagnostics, if any context.ReportDiagnostics( shaderInfoWithErrors - .Select(static (item, _) => item.Diagnostcs) - .WithComparer(EqualityComparer.Default.ForImmutableArray())); + .Select(static (item, _) => item.Diagnostcs)); // Get the GetInputCount() info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs index 20a81557..f355b6e6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -16,40 +12,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; internal sealed record D2D1PixelShaderSourceInfo( HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo HlslShaderMethodSource, - ImmutableArray Diagnostcs) -{ - /// - public bool Equals(D2D1PixelShaderSourceInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, D2D1PixelShaderSourceInfo obj) - { - hashCode.Add(obj.Hierarchy); - hashCode.Add(obj.HlslShaderMethodSource); - hashCode.AddRange(obj.Diagnostcs); - } - - /// - protected override bool AreEqual(D2D1PixelShaderSourceInfo x, D2D1PixelShaderSourceInfo y) - { - return - x.Hierarchy == y.Hierarchy && - x.HlslShaderMethodSource == y.HlslShaderMethodSource && - x.Diagnostcs.SequenceEqual(y.Diagnostcs); - } - } -} \ No newline at end of file + EquatableArray Diagnostcs); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index f79ebb90..5674a782 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -28,52 +24,4 @@ internal sealed record D2D1ShaderInfo( OutputBufferInfo OutputBuffer, InputDescriptionsInfo InputDescriptions, D2D1PixelOptions PixelOptions, - ImmutableArray Diagnostcs) -{ - /// - public bool Equals(D2D1ShaderInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, D2D1ShaderInfo obj) - { - hashCode.Add(obj.Hierarchy); - hashCode.Add(obj.DispatchData); - hashCode.Add(obj.InputTypes); - hashCode.Add(obj.ResourceTextureDescriptions); - hashCode.Add(obj.HlslShaderSource); - hashCode.Add(obj.OutputBuffer); - hashCode.Add(obj.InputDescriptions); - hashCode.Add(obj.PixelOptions); - hashCode.AddRange(obj.Diagnostcs); - } - - /// - protected override bool AreEqual(D2D1ShaderInfo x, D2D1ShaderInfo y) - { - return - x.Hierarchy == y.Hierarchy && - x.DispatchData == y.DispatchData && - x.InputTypes == y.InputTypes && - x.ResourceTextureDescriptions == y.ResourceTextureDescriptions && - x.HlslShaderSource == y.HlslShaderSource && - x.OutputBuffer == y.OutputBuffer && - x.InputDescriptions == y.InputDescriptions && - x.PixelOptions == y.PixelOptions && - x.Diagnostcs.SequenceEqual(y.Diagnostcs); - } - } -} \ No newline at end of file + EquatableArray Diagnostcs); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs index c8aa95ed..f2bb9a0a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/DispatchDataInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -11,38 +7,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// /// The description on shader instance fields. /// The size of the shader constant buffer. -internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, int ConstantBufferSizeInBytes) -{ - /// - public bool Equals(DispatchDataInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DispatchDataInfo obj) - { - hashCode.AddRange(obj.FieldInfos); - hashCode.Add(obj.ConstantBufferSizeInBytes); - } - - /// - protected override bool AreEqual(DispatchDataInfo x, DispatchDataInfo y) - { - return - x.FieldInfos.SequenceEqual(y.FieldInfos) && - x.ConstantBufferSizeInBytes == y.ConstantBufferSizeInBytes; - } - } -} \ No newline at end of file +internal sealed record DispatchDataInfo(EquatableArray FieldInfos, int ConstantBufferSizeInBytes); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs index 1d65fbc8..72a49d1e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -13,42 +9,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// The shader profile to use to compile the shader, if requested. /// The compile options to use to compile the shader. /// The compiled shader bytecode, if available. -internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, D2D1CompileOptions? CompileOptions, ImmutableArray Bytecode) -{ - /// - public bool Equals(EmbeddedBytecodeInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeInfo obj) - { - hashCode.Add(obj.HlslSource); - hashCode.Add(obj.ShaderProfile); - hashCode.Add(obj.CompileOptions); - hashCode.AddBytes(obj.Bytecode.AsSpan()); - } - - /// - protected override bool AreEqual(EmbeddedBytecodeInfo x, EmbeddedBytecodeInfo y) - { - return - x.HlslSource == y.HlslSource && - x.ShaderProfile == y.ShaderProfile && - x.CompileOptions == y.CompileOptions && - x.Bytecode.SequenceEqual(y.Bytecode); - } - } -} \ No newline at end of file +internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, D2D1CompileOptions? CompileOptions, EquatableArray Bytecode); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs index e2f43b8b..56ef702d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs @@ -1,64 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Runtime.InteropServices; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; -using Microsoft.CodeAnalysis.CSharp; namespace ComputeSharp.D2D1.SourceGenerators.Models; /// /// A model representing info on a shader method that has requested to be precompiled at build time. /// -/// The modifiers for the annotated method. +/// The modifiers for the annotated method (these are values, which can't be equated directly). /// The name of the annotated method. /// The fully qualified name of the return type, if invalid. /// The HLSL source. /// The compiled shader bytecode, if available. internal sealed record EmbeddedBytecodeMethodInfo( - ImmutableArray Modifiers, + EquatableArray Modifiers, string MethodName, string? InvalidReturnType, string HlslSource, - ImmutableArray Bytecode) -{ - /// - public bool Equals(EmbeddedBytecodeMethodInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeMethodInfo obj) - { - hashCode.AddRange(obj.Modifiers); - hashCode.Add(obj.MethodName); - hashCode.Add(obj.InvalidReturnType); - hashCode.Add(obj.HlslSource); - hashCode.AddBytes(obj.Bytecode.AsSpan()); - } - - /// - protected override bool AreEqual(EmbeddedBytecodeMethodInfo x, EmbeddedBytecodeMethodInfo y) - { - return - MemoryMarshal.Cast(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast(y.Modifiers.AsSpan())) && - x.MethodName == y.MethodName && - x.InvalidReturnType == y.InvalidReturnType && - x.HlslSource == y.HlslSource && - x.Bytecode.SequenceEqual(y.Bytecode); - } - } -} \ No newline at end of file + EquatableArray Bytecode); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs index 89ff1281..c49ba8b6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -17,43 +13,7 @@ internal abstract record FieldInfo /// The path of the field with respect to the shader instance. /// The full metadata name for the primitive type. /// The byte offset of the value within the root signature. - public sealed record Primitive(ImmutableArray FieldPath, string TypeName, int Offset) : FieldInfo - { - /// - public bool Equals(Primitive? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, Primitive obj) - { - hashCode.AddRange(obj.FieldPath); - hashCode.Add(obj.TypeName); - hashCode.Add(obj.Offset); - } - - /// - protected override bool AreEqual(Primitive x, Primitive y) - { - return - x.FieldPath.SequenceEqual(y.FieldPath) && - x.TypeName == y.TypeName && - x.Offset == y.Offset; - } - } - } + public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo; /// /// A captured non linear matrix value. @@ -65,52 +25,10 @@ internal abstract record FieldInfo /// The number of columns in the matrix. /// The sequence of byte offsets for each row within the root signature. public sealed record NonLinearMatrix( - ImmutableArray FieldPath, + EquatableArray FieldPath, string TypeName, string ElementName, int Rows, int Columns, - ImmutableArray Offsets) : FieldInfo - { - /// - public bool Equals(NonLinearMatrix? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, NonLinearMatrix obj) - { - hashCode.AddRange(obj.FieldPath); - hashCode.Add(obj.TypeName); - hashCode.Add(obj.ElementName); - hashCode.Add(obj.Rows); - hashCode.Add(obj.Columns); - hashCode.AddRange(obj.Offsets); - } - - /// - protected override bool AreEqual(NonLinearMatrix x, NonLinearMatrix y) - { - return - x.FieldPath.SequenceEqual(y.FieldPath) && - x.TypeName == y.TypeName && - x.ElementName == y.ElementName && - x.Rows == y.Rows && - x.Columns == y.Columns && - x.Offsets.SequenceEqual(y.Offsets); - } - } - } + EquatableArray Offsets) : FieldInfo; } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs index 277c0cea..75ff95e0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs @@ -1,16 +1,11 @@ -using System; -using System.Collections.Immutable; -using System.Runtime.InteropServices; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; -using Microsoft.CodeAnalysis.CSharp; namespace ComputeSharp.D2D1.SourceGenerators.Models; /// /// A model for extracted info on a processed HLSL shader method. /// -/// The modifiers for the annotated method. +/// The modifiers for the annotated method (these are values, which can't be equated directly). /// The name of the annotated method. /// The fully qualified name of the return type, if invalid. /// The HLSL source. @@ -18,54 +13,10 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// The compile options to use to compile the shader. /// Whether any errors have been detected, and therefore the shader compilation should be skipped. internal sealed record HlslShaderMethodSourceInfo( - ImmutableArray Modifiers, + EquatableArray Modifiers, string MethodName, string? InvalidReturnType, string HlslSource, D2D1ShaderProfile ShaderProfile, D2D1CompileOptions CompileOptions, - bool HasErrors) -{ - /// - public bool Equals(HlslShaderMethodSourceInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, HlslShaderMethodSourceInfo obj) - { - hashCode.AddRange(obj.Modifiers); - hashCode.Add(obj.MethodName); - hashCode.Add(obj.InvalidReturnType); - hashCode.Add(obj.HlslSource); - hashCode.Add(obj.ShaderProfile); - hashCode.Add(obj.CompileOptions); - hashCode.Add(obj.HasErrors); - } - - /// - protected override bool AreEqual(HlslShaderMethodSourceInfo x, HlslShaderMethodSourceInfo y) - { - return - MemoryMarshal.Cast(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast(y.Modifiers.AsSpan())) && - x.MethodName == y.MethodName && - x.InvalidReturnType == y.InvalidReturnType && - x.HlslSource == y.HlslSource && - x.ShaderProfile == y.ShaderProfile && - x.CompileOptions == y.CompileOptions && - x.HasErrors == y.HasErrors; - } - } -} \ No newline at end of file + bool HasErrors); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputDescriptionsInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputDescriptionsInfo.cs index e0500622..2ddb65f3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputDescriptionsInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputDescriptionsInfo.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -9,35 +6,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// A model representing gathered input descriptions for a shader. /// /// The input descriptions for a given shader. -internal sealed record InputDescriptionsInfo(ImmutableArray InputDescriptions) -{ - /// - public bool Equals(InputDescriptionsInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, InputDescriptionsInfo obj) - { - hashCode.AddRange(obj.InputDescriptions); - } - - /// - protected override bool AreEqual(InputDescriptionsInfo x, InputDescriptionsInfo y) - { - return x.InputDescriptions.SequenceEqual(y.InputDescriptions); - } - } -} \ No newline at end of file +internal sealed record InputDescriptionsInfo(EquatableArray InputDescriptions); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs index f393e71e..bdbb2116 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -10,35 +6,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// A model representing gathered info on a shader input types. /// /// The input types for a given shader. -internal sealed record InputTypesInfo(ImmutableArray InputTypes) -{ - /// - public bool Equals(InputTypesInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, InputTypesInfo obj) - { - hashCode.AddRange(obj.InputTypes); - } - - /// - protected override bool AreEqual(InputTypesInfo x, InputTypesInfo y) - { - return x.InputTypes.SequenceEqual(y.InputTypes); - } - } -} \ No newline at end of file +internal sealed record InputTypesInfo(EquatableArray InputTypes); \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/ResourceTextureDescriptionsInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/ResourceTextureDescriptionsInfo.cs index 40349dd3..4661508c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/ResourceTextureDescriptionsInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/ResourceTextureDescriptionsInfo.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -9,35 +6,4 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// A model representing gathered resource texture descriptions for a shader. /// /// The resource textures for the current shader. -internal sealed record ResourceTextureDescriptionsInfo(ImmutableArray ResourceTextureDescriptions) -{ - /// - public bool Equals(ResourceTextureDescriptionsInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, ResourceTextureDescriptionsInfo obj) - { - hashCode.AddRange(obj.ResourceTextureDescriptions); - } - - /// - protected override bool AreEqual(ResourceTextureDescriptionsInfo x, ResourceTextureDescriptionsInfo y) - { - return x.ResourceTextureDescriptions.SequenceEqual(y.ResourceTextureDescriptions); - } - } -} \ No newline at end of file +internal sealed record ResourceTextureDescriptionsInfo(EquatableArray ResourceTextureDescriptions); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 834ad93c..05057cf3 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -13,11 +13,8 @@ - - - - + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs index 4d6d3717..033be458 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs @@ -1,4 +1,3 @@ -using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; @@ -57,7 +56,7 @@ internal static class DiagnosticsExtensions /// /// The input instance. /// The input sequence of diagnostics. - public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics) + public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics) { context.RegisterSourceOutput(diagnostics, static (context, diagnostics) => { diff --git a/src/ComputeSharp.SourceGeneration/Extensions/HashCodeExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/HashCodeExtensions.cs deleted file mode 100644 index 15592c78..00000000 --- a/src/ComputeSharp.SourceGeneration/Extensions/HashCodeExtensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace ComputeSharp.SourceGeneration.Extensions; - -/// -/// Extension methods for . -/// -internal static class HashCodeExtensions -{ - /// - /// Adds all items from a given instance to an hashcode. - /// - /// The type of items to hash. - /// The target instance. - /// The input items to hash. - public static void AddRange(this ref HashCode hashCode, ImmutableArray items) - { - foreach (T item in items) - { - hashCode.Add(item); - } - } - - /// - /// Adds all items from a given instance to an hashcode. - /// - /// The type of items to hash. - /// The target instance. - /// A comparer to get hashcodes for items. - /// The input items to hash. - public static void AddRange(this ref HashCode hashCode, ImmutableArray items, IEqualityComparer comparer) - { - foreach (T item in items) - { - hashCode.Add(item, comparer); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs deleted file mode 100644 index ff7734fb..00000000 --- a/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace ComputeSharp.SourceGeneration.Extensions; - -/// -/// Extension methods for . -/// -internal static class IEqualityComparerExtensions -{ - /// - /// Gets an for an sequence. - /// - /// The type of items to compare. - /// The compare for individual items. - public static IEqualityComparer> ForImmutableArray(this IEqualityComparer comparer) - { - return new Comparer(comparer); - } - - /// - /// An implementation for an value. - /// - private sealed class Comparer : IEqualityComparer> - { - /// - /// The comparer. - /// - private readonly IEqualityComparer comparer; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The comparer. - public Comparer(IEqualityComparer comparer) - { - this.comparer = comparer; - } - - /// - public bool Equals(ImmutableArray x, ImmutableArray y) - { - return - x == y || - x.SequenceEqual(y, this.comparer); - } - - /// - public int GetHashCode(ImmutableArray obj) - { - HashCode hashCode = default; - - hashCode.AddRange(obj, this.comparer); - - return hashCode.ToHashCode(); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ImmutableArrayExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ImmutableArrayExtensions.cs deleted file mode 100644 index 9b8b6a77..00000000 --- a/src/ComputeSharp.SourceGeneration/Extensions/ImmutableArrayExtensions.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace ComputeSharp.SourceGeneration.Extensions; - -/// -/// Extension methods for . -/// -internal static class ImmutableArrayExtensions -{ - /// - /// Compares two sequences for equality. - /// - /// The items to compare. - /// The left sequence of items. - /// The right sequence of items. - /// Whether or not equals . - public static bool SequenceEqual(this ImmutableArray left, ImmutableArray right) - where T : IEquatable - { - return left.AsSpan().SequenceEqual(right.AsSpan()); - } - - /// - /// Compares two sequences for equality. - /// - /// The items to compare. - /// The left sequence of items. - /// The right sequence of items. - /// The instance used to compare items. - /// Whether or not equals . - public static bool SequenceEqual(this ImmutableArray left, ImmutableArray right, IEqualityComparer comparer) - { - if (left.Length != right.Length) - { - return false; - } - - for (int i = 0; i < left.Length; i++) - { - if (!comparer.Equals(left[i], right[i])) - { - return false; - } - } - - return true; - } -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Helpers/Comparer{T,TSelf}.cs b/src/ComputeSharp.SourceGeneration/Helpers/Comparer{T,TSelf}.cs deleted file mode 100644 index 000ccd39..00000000 --- a/src/ComputeSharp.SourceGeneration/Helpers/Comparer{T,TSelf}.cs +++ /dev/null @@ -1,68 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; - -namespace ComputeSharp.SourceGeneration.Helpers; - -/// -/// A base implementation for instances. -/// -/// The type of items to compare. -/// The concrete comparer type. -internal abstract class Comparer : IEqualityComparer - where TSelf : Comparer, new() -{ - /// - /// The singleton instance. - /// - public static TSelf Default { get; } = new(); - - /// - public bool Equals(T? x, T? y) - { - if (x is null && y is null) - { - return true; - } - - if (x is null || y is null) - { - return false; - } - - if (ReferenceEquals(x, y)) - { - return true; - } - - return AreEqual(x, y); - } - - /// - public int GetHashCode(T obj) - { - HashCode hashCode = default; - - AddToHashCode(ref hashCode, obj); - - return hashCode.ToHashCode(); - } - - /// - /// Adds the current instance to an incremental value. - /// - /// The target value. - /// The instance being inspected. - protected abstract void AddToHashCode(ref HashCode hashCode, T obj); - - /// - /// Compares two instances for equality. - /// - /// The first instance to compare. - /// The second instance to compare. - /// Whether or not and are equal. - protected abstract bool AreEqual(T x, T y); -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Helpers/EquatableArray{T}.cs b/src/ComputeSharp.SourceGeneration/Helpers/EquatableArray{T}.cs new file mode 100644 index 00000000..6ef3d6b8 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Helpers/EquatableArray{T}.cs @@ -0,0 +1,236 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace ComputeSharp.SourceGeneration.Helpers; + +/// +/// Extensions for . +/// +internal static class EquatableArray +{ + /// + /// Creates an instance from a given . + /// + /// The type of items in the input array. + /// The input instance. + /// An instance from a given . + public static EquatableArray AsEquatableArray(this ImmutableArray array) + where T : IEquatable + { + return new(array); + } +} + +/// +/// An imutable, equatable array. This is equivalent to but with value equality support. +/// +/// The type of values in the array. +internal readonly struct EquatableArray : IEquatable>, IEnumerable + where T : IEquatable +{ + /// + /// The underlying array. + /// + private readonly T[]? array; + + /// + /// Creates a new instance. + /// + /// The input to wrap. + public EquatableArray(ImmutableArray array) + { + this.array = Unsafe.As, T[]?>(ref array); + } + + /// + /// Gets a reference to an item at a specified position within the array. + /// + /// The index of the item to retrieve a reference to. + /// A reference to an item at a specified position within the array. + public ref readonly T this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => ref AsImmutableArray().ItemRef(index); + } + + /// + /// Gets a value indicating whether the current array is empty. + /// + public bool IsEmpty + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => AsImmutableArray().IsEmpty; + } + + /// + /// Gets a value indicating whether the current array is default or empty. + /// + public bool IsDefaultOrEmpty + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => AsImmutableArray().IsDefaultOrEmpty; + } + + /// + /// Gets the length of the current array. + /// + public int Length + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => AsImmutableArray().Length; + } + + /// + public bool Equals(EquatableArray array) + { + return AsSpan().SequenceEqual(array.AsSpan()); + } + + /// + public override bool Equals(object? obj) + { + return obj is EquatableArray array && Equals(this, array); + } + + /// + public override unsafe int GetHashCode() + { + if (this.array is not T[] array) + { + return 0; + } + + HashCode hashCode = default; + + if (typeof(T) == typeof(byte)) + { + ReadOnlySpan span = array; + ref T r0 = ref MemoryMarshal.GetReference(span); + ref byte r1 = ref Unsafe.As(ref r0); + + fixed (byte* p = &r1) + { + ReadOnlySpan bytes = new(p, span.Length); + + hashCode.AddBytes(bytes); + } + } + else + { + foreach (T item in array) + { + hashCode.Add(item); + } + } + + return hashCode.ToHashCode(); + } + + /// + /// Gets an instance from the current . + /// + /// The from the current . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ImmutableArray AsImmutableArray() + { + return Unsafe.As>(ref Unsafe.AsRef(in this.array)); + } + + /// + /// Creates an instance from a given . + /// + /// The input instance. + /// An instance from a given . + public static EquatableArray FromImmutableArray(ImmutableArray array) + { + return new(array); + } + + /// + /// Returns a wrapping the current items. + /// + /// A wrapping the current items. + public ReadOnlySpan AsSpan() + { + return AsImmutableArray().AsSpan(); + } + + /// + /// Copies the contents of this instance. to a mutable array. + /// + /// The newly instantiated array. + public T[] ToArray() + { + return AsImmutableArray().ToArray(); + } + + /// + /// Gets an value to traverse items in the current array. + /// + /// An value to traverse items in the current array. + public ImmutableArray.Enumerator GetEnumerator() + { + return AsImmutableArray().GetEnumerator(); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)AsImmutableArray()).GetEnumerator(); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)AsImmutableArray()).GetEnumerator(); + } + + /// + /// Implicitly converts an to . + /// + /// An instance from a given . + public static implicit operator EquatableArray(ImmutableArray array) + { + return FromImmutableArray(array); + } + + /// + /// Implicitly converts an to . + /// + /// An instance from a given . + public static implicit operator ImmutableArray(EquatableArray array) + { + return array.AsImmutableArray(); + } + + /// + /// Checks whether two values are the same. + /// + /// The first value. + /// The second value. + /// Whether and are equal. + public static bool operator ==(EquatableArray left, EquatableArray right) + { + return left.Equals(right); + } + + /// + /// Checks whether two values are not the same. + /// + /// The first value. + /// The second value. + /// Whether and are not equal. + public static bool operator !=(EquatableArray left, EquatableArray right) + { + return !left.Equals(right); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs index 47d6c1b2..a88117ba 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; @@ -13,20 +10,8 @@ namespace ComputeSharp.SourceGeneration.Models; /// /// The wrapped instance. /// The diagnostic arguments. -internal sealed record DeferredDiagnosticInfo(DiagnosticDescriptor Descriptor, ImmutableArray Arguments) +internal sealed record DeferredDiagnosticInfo(DiagnosticDescriptor Descriptor, EquatableArray Arguments) { - /// - public bool Equals(DeferredDiagnosticInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - /// /// Creates a new instance with the specified parameters. /// @@ -37,25 +22,4 @@ internal sealed record DeferredDiagnosticInfo(DiagnosticDescriptor Descriptor, I { return new(descriptor, args.Select(static arg => arg.ToString()).ToImmutableArray()); } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DeferredDiagnosticInfo obj) - { - hashCode.Add(obj.Descriptor); - hashCode.AddRange(obj.Arguments); - } - - /// - protected override bool AreEqual(DeferredDiagnosticInfo x, DeferredDiagnosticInfo y) - { - return - x.Descriptor.Equals(y.Descriptor) && - x.Arguments.SequenceEqual(y.Arguments); - } - } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs index 24937c14..e7009ff6 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; @@ -16,24 +13,8 @@ namespace ComputeSharp.SourceGeneration.Models; /// The tree to use as location for the diagnostic, if available. /// The span to use as location for the diagnostic. /// The diagnostic arguments. -internal sealed record DiagnosticInfo( - DiagnosticDescriptor Descriptor, - SyntaxTree? SyntaxTree, - TextSpan TextSpan, - ImmutableArray Arguments) +internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, SyntaxTree? SyntaxTree, TextSpan TextSpan, EquatableArray Arguments) { - /// - public bool Equals(DiagnosticInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - /// /// Creates a new instance with the state from this model. /// @@ -75,29 +56,4 @@ internal sealed record DiagnosticInfo( return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray()); } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DiagnosticInfo obj) - { - hashCode.Add(obj.Descriptor); - hashCode.Add(obj.SyntaxTree); - hashCode.Add(obj.TextSpan); - hashCode.AddRange(obj.Arguments); - } - - /// - protected override bool AreEqual(DiagnosticInfo x, DiagnosticInfo y) - { - return - x.Descriptor.Equals(y.Descriptor) && - x.SyntaxTree == y.SyntaxTree && - x.TextSpan.Equals(y.TextSpan) && - x.Arguments.SequenceEqual(y.Arguments); - } - } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index a9450d58..0a72429c 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; @@ -18,7 +15,7 @@ namespace ComputeSharp.SourceGeneration.Models; /// The fully qualified metadata name for the current type. /// Gets the namespace for the current type. /// Gets the sequence of type definitions containing the current type. -internal sealed partial record HierarchyInfo(string FilenameHint, string FullyQualifiedMetadataName, string Namespace, ImmutableArray Hierarchy) +internal sealed partial record HierarchyInfo(string FilenameHint, string FullyQualifiedMetadataName, string Namespace, EquatableArray Hierarchy) { /// /// Creates a new instance from a given . @@ -110,41 +107,4 @@ internal sealed partial record HierarchyInfo(string FilenameHint, string FullyQu .AddMembers(typeDeclarationSyntax)) .NormalizeWhitespace(eol: "\n"); } - - /// - public bool Equals(HierarchyInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, HierarchyInfo obj) - { - hashCode.Add(obj.FilenameHint); - hashCode.Add(obj.FullyQualifiedMetadataName); - hashCode.Add(obj.Namespace); - hashCode.AddRange(obj.Hierarchy); - } - - /// - protected override bool AreEqual(HierarchyInfo x, HierarchyInfo y) - { - return - x.FilenameHint == y.FilenameHint && - x.FullyQualifiedMetadataName == y.FullyQualifiedMetadataName && - x.Namespace == y.Namespace && - x.Hierarchy.SequenceEqual(y.Hierarchy); - } - } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 569bd3ee..f5288781 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; @@ -118,8 +117,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // Output the diagnostics, if any context.ReportDiagnostics( shaderInfoWithErrors - .Select(static (item, _) => item.Diagnostcs) - .WithComparer(EqualityComparer.Default.ForImmutableArray())); + .Select(static (item, _) => item.Diagnostcs)); // Get the GetDispatchId() info (hierarchy and dispatch id info) IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchIdInfo DispatchId)> dispatchIdInfo = diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs index 4d9e53b0..bf68a1ae 100644 --- a/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -15,44 +11,6 @@ namespace ComputeSharp.SourceGenerators.Models; /// The size of the shader root signature, in 32 bit constants. internal sealed record DispatchDataInfo( ShaderType Type, - ImmutableArray FieldInfos, + EquatableArray FieldInfos, int ResourceCount, - int Root32BitConstantCount) -{ - /// - public bool Equals(DispatchDataInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DispatchDataInfo obj) - { - hashCode.Add(obj.Type); - hashCode.AddRange(obj.FieldInfos); - hashCode.Add(obj.ResourceCount); - hashCode.Add(obj.Root32BitConstantCount); - } - - /// - protected override bool AreEqual(DispatchDataInfo x, DispatchDataInfo y) - { - return - x.Type == y.Type && - x.FieldInfos.SequenceEqual(y.FieldInfos) && - x.ResourceCount == y.ResourceCount && - x.Root32BitConstantCount == y.Root32BitConstantCount; - } - } -} \ No newline at end of file + int Root32BitConstantCount); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs index c28aa8b2..1b47c460 100644 --- a/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchIdInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -10,35 +6,4 @@ namespace ComputeSharp.SourceGenerators.Models; /// A model representing gathered info on a shader dispatch id. /// /// The list of delegate field names for the shader. -internal sealed record DispatchIdInfo(ImmutableArray Delegates) -{ - /// - public bool Equals(DispatchIdInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DispatchIdInfo obj) - { - hashCode.AddRange(obj.Delegates); - } - - /// - protected override bool AreEqual(DispatchIdInfo x, DispatchIdInfo y) - { - return x.Delegates.SequenceEqual(y.Delegates); - } - } -} \ No newline at end of file +internal sealed record DispatchIdInfo(EquatableArray Delegates); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchMetadataInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchMetadataInfo.cs index 8ebe65d7..1cba4ce6 100644 --- a/src/ComputeSharp.SourceGenerators/Models/DispatchMetadataInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchMetadataInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -12,43 +8,4 @@ namespace ComputeSharp.SourceGenerators.Models; /// The size of the shader root signature, in 32 bit constants. /// Whether or not the static sampler is used. /// The sequence of resource descriptors for the shader. -internal sealed record DispatchMetadataInfo( - int Root32BitConstantCount, - bool IsSamplerUsed, - ImmutableArray ResourceDescriptors) -{ - /// - public bool Equals(DispatchMetadataInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, DispatchMetadataInfo obj) - { - hashCode.Add(obj.Root32BitConstantCount); - hashCode.Add(obj.IsSamplerUsed); - hashCode.AddRange(obj.ResourceDescriptors); - } - - /// - protected override bool AreEqual(DispatchMetadataInfo x, DispatchMetadataInfo y) - { - return - x.Root32BitConstantCount == y.Root32BitConstantCount && - x.IsSamplerUsed == y.IsSamplerUsed && - x.ResourceDescriptors.SequenceEqual(y.ResourceDescriptors); - } - } -} \ No newline at end of file +internal sealed record DispatchMetadataInfo(int Root32BitConstantCount, bool IsSamplerUsed, EquatableArray ResourceDescriptors); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs index 50c2dee6..f6fec7a6 100644 --- a/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -13,42 +9,4 @@ namespace ComputeSharp.SourceGenerators.Models; /// The thread ids value for the Y axis. /// The thread ids value for the Z axis. /// The compiled shader bytecode, if available. -internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray Bytecode) -{ - /// - public bool Equals(EmbeddedBytecodeInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeInfo obj) - { - hashCode.Add(obj.X); - hashCode.Add(obj.Y); - hashCode.Add(obj.Z); - hashCode.AddBytes(obj.Bytecode.AsSpan()); - } - - /// - protected override bool AreEqual(EmbeddedBytecodeInfo x, EmbeddedBytecodeInfo y) - { - return - x.X == y.X && - x.Y == y.Y && - x.Z == y.Z && - x.Bytecode.SequenceEqual(y.Bytecode); - } - } -} \ No newline at end of file +internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, EquatableArray Bytecode); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs b/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs index c24c7744..207507f7 100644 --- a/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -17,43 +13,7 @@ internal abstract record FieldInfo /// The name of the resource field. /// The full metadata name for the resource type. /// The offset for the resource within the root signature. - public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo - { - /// - public bool Equals(Resource? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, Resource obj) - { - hashCode.Add(obj.FieldName); - hashCode.Add(obj.TypeName); - hashCode.Add(obj.Offset); - } - - /// - protected override bool AreEqual(Resource x, Resource y) - { - return - x.FieldName == y.FieldName && - x.TypeName == y.TypeName && - x.Offset == y.Offset; - } - } - } + public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo; /// /// A captured primitive value (either a scalar, a vector, or a linear matrix). @@ -61,43 +21,7 @@ internal abstract record FieldInfo /// The path of the field with respect to the shader instance. /// The full metadata name for the primitive type. /// The byte offset of the value within the root signature. - public sealed record Primitive(ImmutableArray FieldPath, string TypeName, int Offset) : FieldInfo - { - /// - public bool Equals(Primitive? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, Primitive obj) - { - hashCode.AddRange(obj.FieldPath); - hashCode.Add(obj.TypeName); - hashCode.Add(obj.Offset); - } - - /// - protected override bool AreEqual(Primitive x, Primitive y) - { - return - x.FieldPath.SequenceEqual(y.FieldPath) && - x.TypeName == y.TypeName && - x.Offset == y.Offset; - } - } - } + public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo; /// /// A captured non linear matrix value. @@ -109,52 +33,10 @@ internal abstract record FieldInfo /// The number of columns in the matrix. /// The sequence of byte offsets for each row within the root signature. public sealed record NonLinearMatrix( - ImmutableArray FieldPath, + EquatableArray FieldPath, string TypeName, string ElementName, int Rows, int Columns, - ImmutableArray Offsets) : FieldInfo - { - /// - public bool Equals(NonLinearMatrix? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, NonLinearMatrix obj) - { - hashCode.AddRange(obj.FieldPath); - hashCode.Add(obj.TypeName); - hashCode.Add(obj.ElementName); - hashCode.Add(obj.Rows); - hashCode.Add(obj.Columns); - hashCode.AddRange(obj.Offsets); - } - - /// - protected override bool AreEqual(NonLinearMatrix x, NonLinearMatrix y) - { - return - x.FieldPath.SequenceEqual(y.FieldPath) && - x.TypeName == y.TypeName && - x.ElementName == y.ElementName && - x.Rows == y.Rows && - x.Columns == y.Columns && - x.Offsets.SequenceEqual(y.Offsets); - } - } - } + EquatableArray Offsets) : FieldInfo; } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/HlslMethodSourceInfo.cs b/src/ComputeSharp.SourceGenerators/Models/HlslMethodSourceInfo.cs index 52a7f469..78b83804 100644 --- a/src/ComputeSharp.SourceGenerators/Models/HlslMethodSourceInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/HlslMethodSourceInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -17,46 +13,6 @@ namespace ComputeSharp.SourceGenerators.Models; internal sealed record HlslMethodSourceInfo( string MetadataName, string EntryPoint, - ImmutableArray<(string Name, string Definition)> DefinedTypes, - ImmutableArray<(string Name, string Value)> DefinedConstants, - ImmutableArray<(string Signature, string Definition)> DependentMethods) -{ - /// - public bool Equals(HlslMethodSourceInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, HlslMethodSourceInfo obj) - { - hashCode.Add(obj.MetadataName); - hashCode.Add(obj.EntryPoint); - hashCode.AddRange(obj.DefinedTypes); - hashCode.AddRange(obj.DefinedConstants); - hashCode.AddRange(obj.DependentMethods); - } - - /// - protected override bool AreEqual(HlslMethodSourceInfo x, HlslMethodSourceInfo y) - { - return - x.MetadataName == y.MetadataName && - x.EntryPoint == y.EntryPoint && - x.DefinedTypes.SequenceEqual(y.DefinedTypes) && - x.DefinedConstants.SequenceEqual(y.DefinedConstants) && - x.DependentMethods.SequenceEqual(y.DependentMethods); - } - } -} \ No newline at end of file + EquatableArray<(string Name, string Definition)> DefinedTypes, + EquatableArray<(string Name, string Value)> DefinedConstants, + EquatableArray<(string Signature, string Definition)> DependentMethods); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.SourceGenerators/Models/HlslShaderSourceInfo.cs index 67ba89dc..e115d2ed 100644 --- a/src/ComputeSharp.SourceGenerators/Models/HlslShaderSourceInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/HlslShaderSourceInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGenerators.Models; @@ -34,65 +30,7 @@ internal sealed record HlslShaderSourceInfo( string EntryPoint, string? ImplicitTextureType, bool IsSamplerUsed, - ImmutableArray DefinedTypes, - ImmutableArray DefinedConstants, - ImmutableArray MethodSignatures, - ImmutableArray Delegates) -{ - /// - public bool Equals(HlslShaderSourceInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, HlslShaderSourceInfo obj) - { - hashCode.Add(obj.HeaderAndThreadsX); - hashCode.Add(obj.ThreadsY); - hashCode.Add(obj.ThreadsZ); - hashCode.Add(obj.Defines); - hashCode.Add(obj.StaticFieldsAndDeclaredTypes); - hashCode.Add(obj.CapturedFieldsAndResourcesAndForwardDeclarations); - hashCode.Add(obj.CapturedMethods); - hashCode.Add(obj.EntryPoint); - hashCode.Add(obj.ImplicitTextureType); - hashCode.Add(obj.IsSamplerUsed); - hashCode.AddRange(obj.DefinedTypes); - hashCode.AddRange(obj.DefinedConstants); - hashCode.AddRange(obj.MethodSignatures); - hashCode.AddRange(obj.Delegates); - } - - /// - protected override bool AreEqual(HlslShaderSourceInfo x, HlslShaderSourceInfo y) - { - return - x.HeaderAndThreadsX == y.HeaderAndThreadsX && - x.ThreadsY == y.ThreadsY && - x.ThreadsZ == y.ThreadsZ && - x.Defines == y.Defines && - x.StaticFieldsAndDeclaredTypes == y.StaticFieldsAndDeclaredTypes && - x.CapturedFieldsAndResourcesAndForwardDeclarations == y.CapturedFieldsAndResourcesAndForwardDeclarations && - x.CapturedMethods == y.CapturedMethods && - x.EntryPoint == y.EntryPoint && - x.ImplicitTextureType == y.ImplicitTextureType && - x.IsSamplerUsed == y.IsSamplerUsed && - x.DefinedTypes.SequenceEqual(y.DefinedTypes) && - x.DefinedConstants.SequenceEqual(y.DefinedTypes) && - x.MethodSignatures.SequenceEqual(y.MethodSignatures) && - x.Delegates.SequenceEqual(y.Delegates); - } - } -} \ No newline at end of file + EquatableArray DefinedTypes, + EquatableArray DefinedConstants, + EquatableArray MethodSignatures, + EquatableArray Delegates); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index 7823f689..70a7ebfc 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -24,48 +20,4 @@ internal sealed record ShaderInfo( DispatchMetadataInfo DispatchMetadata, HlslShaderSourceInfo HlslShaderSource, ThreadIdsInfo ThreadIds, - ImmutableArray Diagnostcs) -{ - /// - public bool Equals(ShaderInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, ShaderInfo obj) - { - hashCode.Add(obj.Hierarchy); - hashCode.Add(obj.DispatchId); - hashCode.Add(obj.DispatchData); - hashCode.Add(obj.DispatchMetadata); - hashCode.Add(obj.HlslShaderSource); - hashCode.Add(obj.ThreadIds); - hashCode.AddRange(obj.Diagnostcs); - } - - /// - protected override bool AreEqual(ShaderInfo x, ShaderInfo y) - { - return - x.Hierarchy == y.Hierarchy && - x.DispatchId == y.DispatchId && - x.DispatchData == y.DispatchData && - x.DispatchMetadata == y.DispatchMetadata && - x.HlslShaderSource == y.HlslShaderSource && - x.ThreadIds == y.ThreadIds && - x.Diagnostcs.SequenceEqual(y.Diagnostcs); - } - } -} \ No newline at end of file + EquatableArray Diagnostcs); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderMethodInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderMethodInfo.cs index 7fabe44c..f2745cc3 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderMethodInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderMethodInfo.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; @@ -12,38 +8,4 @@ namespace ComputeSharp.SourceGenerators.Models; /// /// The processed HLSL source for the shader method. /// The discovered diagnostics, if any. -internal sealed record ShaderMethodInfo(HlslMethodSourceInfo HlslMethodSource, ImmutableArray Diagnostcs) -{ - /// - public bool Equals(ShaderMethodInfo? obj) - { - return Comparer.Default.Equals(this, obj); - } - - /// - public override int GetHashCode() - { - return Comparer.Default.GetHashCode(this); - } - - /// - /// An implementation for . - /// - private sealed class Comparer : Comparer - { - /// - protected override void AddToHashCode(ref HashCode hashCode, ShaderMethodInfo obj) - { - hashCode.Add(obj.HlslMethodSource); - hashCode.AddRange(obj.Diagnostcs); - } - - /// - protected override bool AreEqual(ShaderMethodInfo x, ShaderMethodInfo y) - { - return - x.HlslMethodSource == y.HlslMethodSource && - x.Diagnostcs.SequenceEqual(y.Diagnostcs); - } - } -} \ No newline at end of file +internal sealed record ShaderMethodInfo(HlslMethodSourceInfo HlslMethodSource, EquatableArray Diagnostcs); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs index 92128a0d..21e5888a 100644 --- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs @@ -48,8 +48,7 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator // Output the diagnostics, if any context.ReportDiagnostics( methodInfoWithErrors - .Select(static (item, _) => item.Diagnostcs) - .WithComparer(EqualityComparer.Default.ForImmutableArray())); + .Select(static (item, _) => item.Diagnostcs)); // Generate the [ShaderMethodSource] attributes context.RegisterSourceOutput(methodInfoWithErrors, static (context, item) =>