diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
index 973c044b..7c6f5154 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
@@ -63,10 +63,9 @@ partial class D2DPixelShaderSourceGenerator
///
/// Extracts the shader profile for a target method, if present.
///
- /// The collection of produced instances.
/// The input instance to process.
/// The shader profile to use to compile the shader, if present.
- public static D2D1ShaderProfile GetShaderProfile(ImmutableArrayBuilder 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;
}
///
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
index 57082fdd..78e3eef2 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
@@ -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);
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs
new file mode 100644
index 00000000..411c7afc
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs
@@ -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;
+
+///
+/// A diagnostic analyzer that generates diagnostics when a method with [D2DPixelShaderSource] isn't using [D2DShaderProfile].
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingShaderProfileForD2DPixelShaderSource);
+
+ ///
+ 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);
+ });
+ }
+}
\ No newline at end of file