Remove symbols from ShaderMethodSourceGenerator pipeline

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 20:49:17 +02:00
1 親 a205efe6fa
コミット d8aefe3128
2ファイル変更
+63 -28

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

+43
ファイルの表示
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing all necessary info for a full generation pass for a shader method.
/// </summary>
/// <param name="HlslMethodSource">The processed HLSL source for the shader method.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
internal sealed record ShaderMethodInfo(HlslMethodSourceInfo HlslMethodSource, ImmutableArray<DiagnosticInfo> Diagnostcs)
{
/// <inheritdoc/>
public bool Equals(ShaderMethodInfo? 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="ShaderMethodInfo"/>.
/// </summary>
private sealed class Comparer : Comparer<ShaderMethodInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, ShaderMethodInfo obj)
{
hashCode.Add(obj.HlslMethodSource);
hashCode.AddRange(obj.Diagnostcs);
}
/// <inheritdoc/>
protected override bool AreEqual(ShaderMethodInfo x, ShaderMethodInfo y)
{
return
x.HlslMethodSource == y.HlslMethodSource &&
x.Diagnostcs.SequenceEqual(y.Diagnostcs);
}
}
}
+20 -28
ファイルの表示
@@ -28,42 +28,34 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Get all declared methods (including global function declarations) with the [ShaderMethod] attribute
IncrementalValuesProvider<(MethodDeclarationSyntax Syntax, IMethodSymbol Symbol)> methodDeclarationsAndSymbols =
// Get all declared methods (including global function declarations) with the [ShaderMethod] attribute, and their info
IncrementalValuesProvider<ShaderMethodInfo> methodInfoWithErrors =
context.SyntaxProvider
.ForAttributeWithMetadataName(
typeof(ShaderMethodAttribute).FullName,
static (node, token) => node is MethodDeclarationSyntax { AttributeLists.Count: > 0 } or LocalFunctionStatementSyntax { Parent: GlobalStatementSyntax { Parent: CompilationUnitSyntax }, AttributeLists.Count: > 0 },
static (context, token) => ((MethodDeclarationSyntax)context.TargetNode, (IMethodSymbol)context.TargetSymbol));
static (node, _) => node is MethodDeclarationSyntax { AttributeLists.Count: > 0 } or LocalFunctionStatementSyntax { Parent: GlobalStatementSyntax { Parent: CompilationUnitSyntax } },
static (context, token) =>
{
HlslMethodSourceInfo hlslMethodSourceInfo = Execute.GetData(
context.SemanticModel.Compilation,
(CSharpSyntaxNode)context.TargetNode,
(IMethodSymbol)context.TargetSymbol,
out ImmutableArray<DiagnosticInfo> diagnostics);
// Get the source info for each method
IncrementalValuesProvider<Result<HlslMethodSourceInfo>> methodSourceInfoWithErrors =
methodDeclarationsAndSymbols
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
{
HlslMethodSourceInfo sourceInfo = Execute.GetData(
item.Right,
item.Left.Syntax,
item.Left.Symbol,
out ImmutableArray<DiagnosticInfo> diagnostics);
return new ShaderMethodInfo(hlslMethodSourceInfo, diagnostics);
});
return new Result<HlslMethodSourceInfo>(sourceInfo, diagnostics);
});
// Output the diagnostics
context.ReportDiagnostics(methodSourceInfoWithErrors.Select(static (item, token) => item.Errors));
// Get the filtered sequence to enable caching
IncrementalValuesProvider<HlslMethodSourceInfo> methodSourceInfo =
methodSourceInfoWithErrors
.Select(static (item, token) => item.Value);
// Output the diagnostics, if any
context.ReportDiagnostics(
methodInfoWithErrors
.Select(static (item, _) => item.Diagnostcs)
.WithComparer(EqualityComparer<DiagnosticInfo>.Default.ForImmutableArray()));
// Generate the [ShaderMethodSource] attributes
context.RegisterSourceOutput(methodSourceInfo, static (context, item) =>
context.RegisterSourceOutput(methodInfoWithErrors, static (context, item) =>
{
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item);
string filename = item.MetadataName.Replace('`', '-').Replace('+', '.');
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item.HlslMethodSource);
string filename = item.HlslMethodSource.MetadataName.Replace('`', '-').Replace('+', '.');
context.AddSource($"{filename}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});