Add MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2024-01-06 23:26:25 +01:00
1 親 0b9f9b304e
コミット 29d0ccca5c
3ファイル変更
+63 -12

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

+2 -9
ファイルの表示
@@ -63,10 +63,9 @@ partial class D2DPixelShaderSourceGenerator
/// <summary>
/// Extracts the shader profile for a target method, if present.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <returns>The shader profile to use to compile the shader, if present.</returns>
public static D2D1ShaderProfile GetShaderProfile(ImmutableArrayBuilder<DiagnosticInfo> diagnostics, IMethodSymbol methodSymbol)
public static D2D1ShaderProfile? GetShaderProfile(IMethodSymbol methodSymbol)
{
if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData))
{
@@ -78,13 +77,7 @@ partial class D2DPixelShaderSourceGenerator
return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!;
}
diagnostics.Add(
MissingShaderProfileForD2DPixelShaderSource,
methodSymbol,
methodSymbol.Name,
methodSymbol.ContainingType);
return D2D1ShaderProfile.PixelShader50;
return null;
}
/// <summary>
+6 -3
ファイルの表示
@@ -42,16 +42,19 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
string methodName = methodSymbol.Name;
string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol);
string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol);
D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, methodSymbol);
D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, methodSymbol);
bool isCompilationEnabled = diagnostics.Count == 0;
// For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator
D2D1ShaderProfile? requestedShaderProfile = Execute.GetShaderProfile(methodSymbol);
D2D1ShaderProfile effectiveShaderProfile = requestedShaderProfile ?? D2D1ShaderProfile.PixelShader50;
bool isCompilationEnabled = requestedShaderProfile is not null;
token.ThrowIfCancellationRequested();
// Prepare the key to cache the bytecode (just like the main D2D1 generator)
HlslBytecodeInfoKey hlslInfoKey = new(
hlslSource,
shaderProfile,
effectiveShaderProfile,
compileOptions,
isCompilationEnabled);
@@ -0,0 +1,55 @@
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates diagnostics when a method with [D2DPixelShaderSource] isn't using [D2DShaderProfile].
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(MissingShaderProfileForD2DPixelShaderSource);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the [D2DPixelShaderSource] and [D2DShaderProfile] symbols
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute") is not { } d2DShaderProfileAttributeSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only partial definition methods are possible targets
if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
{
return;
}
// Emit a diagnostic if there is no shader profile available at any level
if (!methodSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol) &&
!methodSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(
MissingShaderProfileForD2DPixelShaderSource,
methodSymbol.Locations.First(),
methodSymbol.Name,
methodSymbol.ContainingType));
}
}, SymbolKind.Method);
});
}
}