Make all models directly equatable

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 14:48:33 +02:00
1 親 f27e98f2fa
コミット 87f2e04ca0
23ファイル変更
+287 -169

検索条件に一致するファイルはありません

+7 -14
ファイルの表示
@@ -63,8 +63,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Get the hierarchy info and delegate field names for each shader type
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchIdInfo Info)> hierarchyAndDispatchIdInfo =
shaderDeclarations
.Select(static (item, token) => (item.Hierarchy, new DispatchIdInfo(GetDispatchId.GetInfo(item.Symbol))))
.WithComparers(HierarchyInfo.Comparer.Default, DispatchIdInfo.Comparer.Default);
.Select(static (item, token) => (item.Hierarchy, new DispatchIdInfo(GetDispatchId.GetInfo(item.Symbol))));
// Generate the GetDispatchId() methods
context.RegisterSourceOutput(hierarchyAndDispatchIdInfo.Combine(supportsDynamicShaders), static (context, item) =>
@@ -141,14 +140,12 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Filter all items to enable caching at a coarse level, and remove diagnostics
IncrementalValuesProvider<(DispatchDataInfo Dispatch, HlslShaderSourceInfo Hlsl, ThreadIdsInfo ThreadIds)> shaderInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Dispatch.Value, item.Hlsl.Value, item.ThreadIds.Value))
.WithComparers(DispatchDataInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default, EqualityComparer<ThreadIdsInfo>.Default);
.Select(static (item, token) => (item.Dispatch.Value, item.Hlsl.Value, item.ThreadIds.Value));
// Get a filtered sequence to enable caching
IncrementalValuesProvider<DispatchDataInfo> dispatchDataInfo =
shaderInfo
.Select(static (item, token) => item.Dispatch)
.WithComparer(DispatchDataInfo.Comparer.Default);
.Select(static (item, token) => item.Dispatch);
// Generate the LoadDispatchData() methods
context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
@@ -167,8 +164,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo SourceInfo, bool SupportsDynamicShaders)> hlslSourceInfo =
shaderInfo
.Combine(supportsDynamicShaders)
.Select(static (item, token) => (item.Left.Dispatch.Hierarchy, item.Left.Hlsl, item.Right))
.WithComparers(HierarchyInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default, EqualityComparer<bool>.Default);
.Select(static (item, token) => (item.Left.Dispatch.Hierarchy, item.Left.Hlsl, item.Right));
// Generate the BuildHlslSource() methods
context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
@@ -193,8 +189,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
return (item.Dispatch.Hierarchy, dispatchMetadataInfo);
})
.WithComparers(HierarchyInfo.Comparer.Default, DispatchMetadataInfo.Comparer.Default);
});
// Generate the LoadDispatchMetadata() methods
context.RegisterSourceOutput(dispatchMetadataInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
@@ -208,8 +203,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Transform the raw HLSL source to compile
IncrementalValuesProvider<(HierarchyInfo Hierarchy, string Hlsl, ThreadIdsInfo ThreadIds)> shaderBytecodeInfo =
shaderInfo
.Select(static (item, token) => (item.Dispatch.Hierarchy, BuildHlslSource.GetNonDynamicHlslSource(item.Hlsl), item.ThreadIds))
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<string>.Default, EqualityComparer<ThreadIdsInfo>.Default);
.Select(static (item, token) => (item.Dispatch.Hierarchy, BuildHlslSource.GetNonDynamicHlslSource(item.Hlsl), item.ThreadIds));
// Compile the requested shader bytecodes
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
@@ -251,8 +245,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Get the filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
embeddedBytecodeWithErrors
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo));
// Generate the TryGetBytecode() methods
context.RegisterSourceOutput(embeddedBytecode.Combine(supportsDynamicShaders), static (context, item) =>
+11 -5
ファイルの表示
@@ -22,17 +22,23 @@ internal sealed record DispatchDataInfo(
int ResourceCount,
int Root32BitConstantCount)
{
/// <inheritdoc/>
public bool Equals(DispatchDataInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchDataInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<DispatchDataInfo, Comparer>
private sealed class Comparer : Comparer<DispatchDataInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, DispatchDataInfo obj)
{
hashCode.Add(HierarchyInfo.Comparer.Default.GetHashCode(obj.Hierarchy));
hashCode.Add(obj.Hierarchy);
hashCode.Add(obj.Type);
hashCode.AddRange(obj.FieldInfos, FieldInfo.Comparer.Default);
hashCode.AddRange(obj.FieldInfos);
hashCode.Add(obj.ResourceCount);
hashCode.Add(obj.Root32BitConstantCount);
}
@@ -41,9 +47,9 @@ internal sealed record DispatchDataInfo(
protected override bool AreEqual(DispatchDataInfo x, DispatchDataInfo y)
{
return
HierarchyInfo.Comparer.Default.Equals(x.Hierarchy, y.Hierarchy) &&
x.Hierarchy == y.Hierarchy &&
x.Type == y.Type &&
x.FieldInfos.SequenceEqual(y.FieldInfos, FieldInfo.Comparer.Default) &&
x.FieldInfos.SequenceEqual(y.FieldInfos) &&
x.ResourceCount == y.ResourceCount &&
x.Root32BitConstantCount == y.Root32BitConstantCount;
}
+7 -1
ファイルの表示
@@ -12,10 +12,16 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <param name="Delegates">The list of delegate field names for the shader.</param>
internal sealed record DispatchIdInfo(ImmutableArray<string> Delegates)
{
/// <inheritdoc/>
public bool Equals(DispatchIdInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchIdInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<DispatchIdInfo, Comparer>
private sealed class Comparer : Comparer<DispatchIdInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, DispatchIdInfo obj)
+7 -1
ファイルの表示
@@ -17,10 +17,16 @@ internal sealed record DispatchMetadataInfo(
bool IsSamplerUsed,
ImmutableArray<ResourceDescriptor> ResourceDescriptors)
{
/// <inheritdoc/>
public bool Equals(DispatchMetadataInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchMetadataInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<DispatchMetadataInfo, Comparer>
private sealed class Comparer : Comparer<DispatchMetadataInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, DispatchMetadataInfo obj)
+7 -1
ファイルの表示
@@ -15,10 +15,16 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<byte> Bytecode)
{
/// <inheritdoc/>
public bool Equals(EmbeddedBytecodeInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="EmbeddedBytecodeInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<EmbeddedBytecodeInfo, Comparer>
private sealed class Comparer : Comparer<EmbeddedBytecodeInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeInfo obj)
+89 -58
ファイルの表示
@@ -17,7 +17,37 @@ internal abstract record FieldInfo
/// <param name="FieldName">The name of the resource field.</param>
/// <param name="TypeName">The full metadata name for the resource type.</param>
/// <param name="Offset">The offset for the resource within the root signature.</param>
public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo;
public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo
{
/// <inheritdoc/>
public bool Equals(Resource? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="Resource"/>.
/// </summary>
private sealed class Comparer : Comparer<Resource, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, Resource obj)
{
hashCode.Add(obj.FieldName);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.Offset);
}
/// <inheritdoc/>
protected override bool AreEqual(Resource x, Resource y)
{
return
x.FieldName == y.FieldName &&
x.TypeName == y.TypeName &&
x.Offset == y.Offset;
}
}
}
/// <summary>
/// A captured primitive value (either a scalar, a vector, or a linear matrix).
@@ -25,7 +55,37 @@ internal abstract record FieldInfo
/// <param name="FieldPath">The path of the field with respect to the shader instance.</param>
/// <param name="TypeName">The full metadata name for the primitive type.</param>
/// <param name="Offset">The byte offset of the value within the root signature.</param>
public sealed record Primitive(ImmutableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo;
public sealed record Primitive(ImmutableArray<string> FieldPath, string TypeName, int Offset) : FieldInfo
{
/// <inheritdoc/>
public bool Equals(Primitive? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="Primitive"/>.
/// </summary>
private sealed class Comparer : Comparer<Primitive, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, Primitive obj)
{
hashCode.AddRange(obj.FieldPath);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.Offset);
}
/// <inheritdoc/>
protected override bool AreEqual(Primitive x, Primitive y)
{
return
x.FieldPath.SequenceEqual(y.FieldPath) &&
x.TypeName == y.TypeName &&
x.Offset == y.Offset;
}
}
}
/// <summary>
/// A captured non linear matrix value.
@@ -42,70 +102,41 @@ internal abstract record FieldInfo
string ElementName,
int Rows,
int Columns,
ImmutableArray<int> Offsets) : FieldInfo;
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="FieldInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<FieldInfo, Comparer>
ImmutableArray<int> Offsets) : FieldInfo
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, FieldInfo obj)
{
switch (obj)
{
case Resource resource:
hashCode.Add(resource.FieldName);
hashCode.Add(resource.TypeName);
hashCode.Add(resource.Offset);
break;
case Primitive primitive:
hashCode.AddRange(primitive.FieldPath);
hashCode.Add(primitive.TypeName);
hashCode.Add(primitive.Offset);
break;
case NonLinearMatrix matrix:
hashCode.AddRange(matrix.FieldPath);
hashCode.Add(matrix.TypeName);
hashCode.Add(matrix.ElementName);
hashCode.Add(matrix.Rows);
hashCode.Add(matrix.Columns);
hashCode.AddRange(matrix.Offsets);
break;
default:
break;
}
}
public bool Equals(NonLinearMatrix? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
protected override bool AreEqual(FieldInfo x, FieldInfo y)
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="NonLinearMatrix"/>.
/// </summary>
private sealed class Comparer : Comparer<NonLinearMatrix, Comparer>
{
if (x is Resource resourceX && y is Resource resourceY)
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, NonLinearMatrix obj)
{
return
resourceX.FieldName == resourceY.FieldName &&
resourceX.TypeName == resourceY.TypeName &&
resourceX.Offset == resourceY.Offset;
}
else if (x is Primitive primitiveX && y is Primitive primitiveY)
{
return
primitiveX.FieldPath.SequenceEqual(primitiveY.FieldPath) &&
primitiveX.TypeName == primitiveY.TypeName &&
primitiveX.Offset == primitiveY.Offset;
}
else if (x is NonLinearMatrix matrixX && y is NonLinearMatrix matrixY)
{
return
matrixX.FieldPath.SequenceEqual(matrixY.FieldPath) &&
matrixX.TypeName == matrixY.TypeName &&
matrixX.ElementName == matrixY.ElementName &&
matrixX.Rows == matrixY.Rows &&
matrixY.Columns == matrixY.Columns &&
matrixX.Offsets.SequenceEqual(matrixY.Offsets);
hashCode.AddRange(obj.FieldPath);
hashCode.Add(obj.TypeName);
hashCode.Add(obj.ElementName);
hashCode.Add(obj.Rows);
hashCode.Add(obj.Columns);
hashCode.AddRange(obj.Offsets);
}
return false;
/// <inheritdoc/>
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);
}
}
}
}
+7 -1
ファイルの表示
@@ -21,10 +21,16 @@ internal sealed record HlslMethodSourceInfo(
ImmutableArray<(string Name, string Value)> DefinedConstants,
ImmutableArray<(string Signature, string Definition)> DependentMethods)
{
/// <inheritdoc/>
public bool Equals(HlslMethodSourceInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HlslMethodSourceInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<HlslMethodSourceInfo, Comparer>
private sealed class Comparer : Comparer<HlslMethodSourceInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, HlslMethodSourceInfo obj)
+7 -1
ファイルの表示
@@ -39,10 +39,16 @@ internal sealed record HlslShaderSourceInfo(
ImmutableArray<string> MethodSignatures,
ImmutableArray<string> Delegates)
{
/// <inheritdoc/>
public bool Equals(HlslShaderSourceInfo? obj) => Comparer.Default.Equals(this, obj);
/// <inheritdoc/>
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HlslShaderSourceInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<HlslShaderSourceInfo, Comparer>
private sealed class Comparer : Comparer<HlslShaderSourceInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, HlslShaderSourceInfo obj)
+1 -2
ファイルの表示
@@ -57,8 +57,7 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
// Get the filtered sequence to enable caching
IncrementalValuesProvider<HlslMethodSourceInfo> methodSourceInfo =
methodSourceInfoWithErrors
.Select(static (item, token) => item.Value)
.WithComparer(HlslMethodSourceInfo.Comparer.Default);
.Select(static (item, token) => item.Value);
// Generate the [ShaderMethodSource] attributes
context.RegisterSourceOutput(methodSourceInfo, static (context, item) =>