Enable caching for DispatchId info

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 11:17:05 +01:00
1 親 ed75dd4b5a
コミット be614c5af5
2ファイル変更
+62 -5

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

+7 -5
ファイルの表示
@@ -59,14 +59,15 @@ public sealed partial class IShaderGenerator2 : IIncrementalGenerator
.Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol), item.Type!));
// Get the hierarchy info and delegate field names for each shader type
IncrementalValuesProvider<(HierarchyInfo Hierarchy, ImmutableArray<string> Delegates)> hierarchyInfoAndDelegateFieldNames =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchIdInfo Info)> hierarchyAndDispatchIdInfo =
shaderDeclarations
.Select(static (item, token) => (item.Hierarchy, GetDelegateFieldNames(item.Symbol)));
.Select(static (item, token) => (item.Hierarchy, new DispatchIdInfo(GetDelegateFieldNames(item.Symbol))))
.WithComparers(HierarchyInfo.Comparer.Default, DispatchIdInfo.Comparer.Default);
// Generate the GetDispatchId() methods
context.RegisterSourceOutput(hierarchyInfoAndDelegateFieldNames.Combine(canUseSkipLocalsInit), static (context, item) =>
context.RegisterSourceOutput(hierarchyAndDispatchIdInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
{
MethodDeclarationSyntax getDispatchIdMethod = CreateGetDispatchIdMethod(item.Left.Delegates);
MethodDeclarationSyntax getDispatchIdMethod = CreateGetDispatchIdMethod(item.Left.Info.Delegates);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, getDispatchIdMethod, item.Right);
context.AddSource($"{item.Left.Hierarchy.FilenameHint}.GetDispatchId", SourceText.From(compilationUnit.ToFullString(), Encoding.UTF8));
@@ -99,7 +100,8 @@ public sealed partial class IShaderGenerator2 : IIncrementalGenerator
// Get a filtered sequence to enable caching
IncrementalValuesProvider<DispatchDataInfo> dispatchDataInfo =
dispatchDataInfoWithErrors
.Select(static (item, token) => item.Value);
.Select(static (item, token) => item.Value)
.WithComparer(DispatchDataInfo.Comparer.Default);
// Generate the LoadDispatchData() methods
context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
+55
ファイルの表示
@@ -0,0 +1,55 @@
using ComputeSharp.SourceGenerators.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a shader dispatch id.
/// </summary>
/// <param name="Delegates">The list of delegate field names for the shader.</param>
internal sealed record DispatchIdInfo(ImmutableArray<string> Delegates)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchIdInfo"/>.
/// </summary>
public sealed class Comparer : IEqualityComparer<DispatchIdInfo>
{
/// <summary>
/// The singleton <see cref="Comparer"/> instance.
/// </summary>
public static Comparer Default { get; } = new();
/// <inheritdoc/>
public bool Equals(DispatchIdInfo? x, DispatchIdInfo? 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 x.Delegates.SequenceEqual(y.Delegates);
}
/// <inheritdoc/>
public int GetHashCode(DispatchIdInfo obj)
{
HashCode hashCode = default;
hashCode.AddRange(obj.Delegates);
return hashCode.ToHashCode();
}
}
}