Add aggregate model for D2D1PixelShaderSourceInfo

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-17 16:54:11 +02:00
1 親 2b01a73233
コミット bdaf402a4c
2ファイル変更
+76 -28

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

+26 -28
ファイルの表示
@@ -21,65 +21,63 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Get all method declarations with the [D2DPixelShaderSource] attribute and gather all necessary info
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source, ImmutableArray<Diagnostic> Diagnostics)> shaderInfoWithErrors =
IncrementalValuesProvider<D2D1PixelShaderSourceInfo> shaderInfoWithErrors =
context.SyntaxProvider
.ForAttributeWithMetadataName(
typeof(D2DPixelShaderSourceAttribute).FullName,
static (node, _) => node is MethodDeclarationSyntax,
static (context, _) =>
static (context, token) =>
{
MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.TargetNode;
IMethodSymbol methodSymbol = (IMethodSymbol)context.TargetSymbol;
ImmutableArray<Diagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
// Get all necessary info for the current shader
// Get the processed HLSL first and check for cancellation
string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol);
token.ThrowIfCancellationRequested();
// Get the remaining info for the current shader
ImmutableArray<SyntaxKind> modifiers = methodDeclaration.Modifiers.Select(token => token.Kind()).ToImmutableArray();
string methodName = methodSymbol.Name;
string? invalidReturnType = Execute.GetInvalidReturnType(diagnostics, methodSymbol);
string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol);
D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol);
D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol);
HlslShaderMethodSourceInfo sourceInfo = new(
modifiers,
methodName,
invalidReturnType,
hlslSource,
shaderProfile,
compileOptions,
HasErrors: diagnostics.Count > 0);
HierarchyInfo hierarchy = HierarchyInfo.From(methodSymbol.ContainingType!);
return (hierarchy, sourceInfo, diagnostics.ToImmutable());
return new D2D1PixelShaderSourceInfo(
Hierarchy: HierarchyInfo.From(methodSymbol.ContainingType!),
HlslShaderMethodSource: new HlslShaderMethodSourceInfo(
modifiers,
methodName,
invalidReturnType,
hlslSource,
shaderProfile,
compileOptions,
HasErrors: diagnostics.Count > 0),
Diagnostcs: diagnostics.ToImmutable());
});
// Output the diagnostics
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostics));
// Filter all items to enable caching at a coarse level, and remove diagnostics
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source)> shaderInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Hierarchy, item.Source));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostcs));
// Compile the requested shader bytecodes
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
shaderInfo
shaderInfoWithErrors
.Select(static (item, token) =>
{
ImmutableArray<byte> bytecode = Execute.GetBytecode(
item.Source,
item.HlslShaderMethodSource,
token,
out DiagnosticInfo? diagnostic);
token.ThrowIfCancellationRequested();
EmbeddedBytecodeMethodInfo bytecodeInfo = new(
item.Source.Modifiers,
item.Source.MethodName,
item.Source.InvalidReturnType,
item.Source.HlslSource,
item.HlslShaderMethodSource.Modifiers,
item.HlslShaderMethodSource.MethodName,
item.HlslShaderMethodSource.InvalidReturnType,
item.HlslShaderMethodSource.HlslSource,
bytecode);
return (item.Hierarchy, bytecodeInfo, diagnostic);
+50
ファイルの表示
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// A model representing all necessary info for a full generation pass for a [D2D1PixelShaderSource] attribute.
/// </summary>
/// <param name="Hierarchy">The hierarchy info for the shader type.</param>
/// <param name="HlslShaderMethodSource">The processed HLSL source for the shader.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
internal sealed record D2D1PixelShaderSourceInfo(
HierarchyInfo Hierarchy,
HlslShaderMethodSourceInfo HlslShaderMethodSource,
ImmutableArray<Diagnostic> Diagnostcs)
{
/// <inheritdoc/>
public bool Equals(D2D1PixelShaderSourceInfo? 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="D2D1PixelShaderSourceInfo"/>.
/// </summary>
private sealed class Comparer : Comparer<D2D1PixelShaderSourceInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, D2D1PixelShaderSourceInfo obj)
{
hashCode.Add(obj.Hierarchy);
hashCode.Add(obj.HlslShaderMethodSource);
hashCode.AddRange(obj.Diagnostcs);
}
/// <inheritdoc/>
protected override bool AreEqual(D2D1PixelShaderSourceInfo x, D2D1PixelShaderSourceInfo y)
{
return
x.Hierarchy == y.Hierarchy &&
x.HlslShaderMethodSource == y.HlslShaderMethodSource &&
x.Diagnostcs.SequenceEqual(y.Diagnostcs);
}
}
}