diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
index 0de6514d..daf3fcea 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs
@@ -1,4 +1,4 @@
-using System;
+using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
@@ -20,10 +20,9 @@ partial class D2DPixelShaderSourceGenerator
///
/// Validates that the return type of the annotated method is valid and returns the type name.
///
- /// The collection of produced instances.
/// The input instance to process.
- /// The HLSL source to compile, if present.
- public static string? GetInvalidReturnType(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol)
+ /// The fully qualified type name, if invalid.
+ public static string? GetInvalidReturnType(IMethodSymbol methodSymbol)
{
if (methodSymbol.ReturnType is not INamedTypeSymbol
{
@@ -33,13 +32,6 @@ partial class D2DPixelShaderSourceGenerator
TypeArguments: [{ SpecialType: SpecialType.System_Byte }]
})
{
- diagnostics.Add(
- InvalidD2DPixelShaderSourceMethodReturnType,
- methodSymbol,
- methodSymbol.Name,
- methodSymbol.ContainingType,
- methodSymbol.ReturnType);
-
return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
@@ -47,34 +39,27 @@ partial class D2DPixelShaderSourceGenerator
}
///
- /// Extracts the HLSL source from a method with the annotation.
+ /// Extracts the HLSL source from an annotated method.
///
- /// The collection of produced instances.
/// The input instance to process.
/// The HLSL source to compile, if present.
- public static string GetHlslSource(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol)
+ public static string? GetHlslSource(IMethodSymbol methodSymbol)
{
- _ = methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData);
-
- if (!attributeData!.TryGetConstructorArgument(0, out string? hlslSource))
+ if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData) &&
+ attributeData!.TryGetConstructorArgument(0, out string? hlslSource))
{
- diagnostics.Add(
- InvalidD2DPixelShaderSource,
- methodSymbol,
- methodSymbol.Name,
- methodSymbol.ContainingType);
+ return hlslSource;
}
- return hlslSource ?? "";
+ return null;
}
///
/// 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))
{
@@ -86,22 +71,15 @@ partial class D2DPixelShaderSourceGenerator
return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!;
}
- diagnostics.Add(
- MissingShaderProfileForD2DPixelShaderSource,
- methodSymbol,
- methodSymbol.Name,
- methodSymbol.ContainingType);
-
- return D2D1ShaderProfile.PixelShader50;
+ return null;
}
///
/// Extracts the compile options for the current shader.
///
- /// The collection of produced instances.
/// The input instance to process.
/// The compile options to use to compile the shader, if present.
- public static D2D1CompileOptions GetCompileOptions(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol)
+ public static D2D1CompileOptions GetCompileOptions(IMethodSymbol methodSymbol)
{
if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData))
{
@@ -113,12 +91,6 @@ partial class D2DPixelShaderSourceGenerator
return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!;
}
- diagnostics.Add(
- MissingCompileOptionsForD2DPixelShaderSource,
- methodSymbol,
- methodSymbol.Name,
- methodSymbol.ContainingType);
-
return D2D1CompileOptions.Default;
}
@@ -127,38 +99,34 @@ partial class D2DPixelShaderSourceGenerator
///
/// The input instance to process.
/// The source instance.
- /// The collection of produced instances.
- public static void GetInfoDiagnostics(
- IMethodSymbol methodSymbol,
- HlslBytecodeInfo info,
- ImmutableArrayBuilder diagnostics)
+ /// The collection of produced instances.
+ public static ImmutableArray GetInfoDiagnostics(IMethodSymbol methodSymbol, HlslBytecodeInfo info)
{
- DiagnosticInfo? diagnostic = null;
-
if (info is HlslBytecodeInfo.Win32Error win32Error)
{
- diagnostic = DiagnosticInfo.Create(
+ DiagnosticInfo diagnostic = DiagnosticInfo.Create(
D2DPixelShaderSourceCompilationFailedWithWin32Exception,
methodSymbol,
methodSymbol,
methodSymbol.ContainingType,
win32Error.HResult,
win32Error.Message);
+
+ return ImmutableArray.Create(diagnostic);
}
else if (info is HlslBytecodeInfo.CompilerError fxcError)
{
- diagnostic = DiagnosticInfo.Create(
+ DiagnosticInfo diagnostic = DiagnosticInfo.Create(
D2DPixelShaderSourceCompilationFailedWithFxcCompilationException,
methodSymbol,
methodSymbol,
methodSymbol.ContainingType,
fxcError.Message);
+
+ return ImmutableArray.Create(diagnostic);
}
- if (diagnostic is not null)
- {
- diagnostics.Add(diagnostic);
- }
+ return ImmutableArray.Empty;
}
///
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
index c26dfc4a..0573a8fd 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs
@@ -35,23 +35,26 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.TargetNode;
IMethodSymbol methodSymbol = (IMethodSymbol)context.TargetSymbol;
- using ImmutableArrayBuilder diagnostics = new();
-
// Get the remaining info for the current shader
ImmutableArray modifiers = methodDeclaration.Modifiers.Select(token => (ushort)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);
- bool isCompilationEnabled = diagnostics.Count == 0;
+ string? invalidReturnType = Execute.GetInvalidReturnType(methodSymbol);
+ string? requestedHlslSource = Execute.GetHlslSource(methodSymbol);
+ string effectiveHlslSource = requestedHlslSource ?? "";
+ D2D1CompileOptions compileOptions = Execute.GetCompileOptions(methodSymbol);
+
+ // For the shader profile, reuse the same logic as in D2D1PixelShaderDescriptorGenerator.
+ // The only difference is that in this case, we also skip if the source is null.
+ D2D1ShaderProfile? requestedShaderProfile = Execute.GetShaderProfile(methodSymbol);
+ D2D1ShaderProfile effectiveShaderProfile = requestedShaderProfile ?? D2D1ShaderProfile.PixelShader50;
+ bool isCompilationEnabled = requestedHlslSource is not null && requestedShaderProfile is not null;
token.ThrowIfCancellationRequested();
// Prepare the key to cache the bytecode (just like the main D2D1 generator)
HlslBytecodeInfoKey hlslInfoKey = new(
- hlslSource,
- shaderProfile,
+ effectiveHlslSource,
+ effectiveShaderProfile,
compileOptions,
isCompilationEnabled);
@@ -60,8 +63,8 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
token.ThrowIfCancellationRequested();
- // Append any diagnostic for the shader compilation
- Execute.GetInfoDiagnostics(methodSymbol, hlslInfo, diagnostics);
+ // Get any diagnostics for the shader compilation
+ ImmutableArray diagnostics = Execute.GetInfoDiagnostics(methodSymbol, hlslInfo);
token.ThrowIfCancellationRequested();
@@ -77,7 +80,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
InvalidReturnType: invalidReturnType,
HlslInfoKey: hlslInfoKey,
HlslInfo: hlslInfo,
- Diagnostcs: diagnostics.ToImmutable());
+ Diagnostcs: diagnostics);
})
.WithTrackingName(WellKnownTrackingNames.Execute);
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs
new file mode 100644
index 00000000..7b4464e3
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceAttributeUseAnalyzer.cs
@@ -0,0 +1,59 @@
+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 is using [D2DPixelShaderSource] with an invalid argument.
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidD2DPixelShaderSourceAttributeUseAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSource);
+
+ ///
+ public override void Initialize(AnalysisContext context)
+ {
+ context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
+ context.EnableConcurrentExecution();
+
+ context.RegisterCompilationStartAction(static context =>
+ {
+ // Get the [D2DPixelShaderSource] symbol
+ if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol)
+ {
+ return;
+ }
+
+ context.RegisterSymbolAction(context =>
+ {
+ // Only partial definition methods are possible targets
+ if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
+ {
+ return;
+ }
+
+ // Ignore methods without [D2DPixelShaderSource]
+ if (!methodSymbol.TryGetAttributeWithType(d2DPixelShaderSourceAttributeSymbol, out AttributeData? attributeData))
+ {
+ return;
+ }
+
+ // Emit a diagnostic if there is no valid HLSL source argument
+ if (!attributeData.TryGetConstructorArgument(0, out string? hlslSource))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidD2DPixelShaderSource,
+ methodSymbol.Locations.First(),
+ methodSymbol.Name,
+ methodSymbol.ContainingType));
+ }
+ }, SymbolKind.Method);
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs
new file mode 100644
index 00000000..e30cd119
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer.cs
@@ -0,0 +1,65 @@
+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 [D2DPixelShaderSource] is used on a method with an incorrect return type.
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class InvalidD2DPixelShaderSourceMethodReturnTypeAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DPixelShaderSourceMethodReturnType);
+
+ ///
+ public override void Initialize(AnalysisContext context)
+ {
+ context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
+ context.EnableConcurrentExecution();
+
+ context.RegisterCompilationStartAction(static context =>
+ {
+ // Get the [D2DPixelShaderSource] symbol
+ if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute") is not { } d2DPixelShaderSourceAttributeSymbol)
+ {
+ return;
+ }
+
+ // Also get the constructed ReadOnlySpan type
+ INamedTypeSymbol readOnlySpanSymbol = context.Compilation.GetTypeByMetadataName("System.ReadOnlySpan`1")!;
+ INamedTypeSymbol byteSymbol = context.Compilation.GetSpecialType(SpecialType.System_Byte);
+ INamedTypeSymbol readOnlySpanOfByteSymbol = readOnlySpanSymbol.Construct(byteSymbol);
+
+ context.RegisterSymbolAction(context =>
+ {
+ // Only partial definition methods are possible targets
+ if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
+ {
+ return;
+ }
+
+ // Ignore methods without [D2DPixelShaderSource]
+ if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol))
+ {
+ return;
+ }
+
+ // Emit a diagnostic if the return type is not ReadOnlySpan
+ if (!SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, readOnlySpanOfByteSymbol))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ InvalidD2DPixelShaderSourceMethodReturnType,
+ methodSymbol.Locations.First(),
+ methodSymbol.Name,
+ methodSymbol.ContainingType,
+ methodSymbol.ReturnType));
+ }
+ }, SymbolKind.Method);
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs
new file mode 100644
index 00000000..654ed07d
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer.cs
@@ -0,0 +1,61 @@
+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 [D2DCompileOptions].
+///
+[DiagnosticAnalyzer(LanguageNames.CSharp)]
+public sealed class MissingD2DCompileOptionsOnD2DPixelShaderSourceMethodAnalyzer : DiagnosticAnalyzer
+{
+ ///
+ public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(MissingCompileOptionsForD2DPixelShaderSource);
+
+ ///
+ 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.D2DCompileOptionsAttribute") is not { } d2DCompileOptionsAttributeSymbol)
+ {
+ return;
+ }
+
+ context.RegisterSymbolAction(context =>
+ {
+ // Only partial definition methods are possible targets
+ if (context.Symbol is not IMethodSymbol { IsPartialDefinition: true } methodSymbol)
+ {
+ return;
+ }
+
+ // Ignore methods without [D2DPixelShaderSource]
+ if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol))
+ {
+ return;
+ }
+
+ // Emit a diagnostic if there is no compile options available at any level
+ if (!methodSymbol.HasAttributeWithType(d2DCompileOptionsAttributeSymbol) &&
+ !methodSymbol.ContainingAssembly.HasAttributeWithType(d2DCompileOptionsAttributeSymbol))
+ {
+ context.ReportDiagnostic(Diagnostic.Create(
+ MissingCompileOptionsForD2DPixelShaderSource,
+ methodSymbol.Locations.First(),
+ methodSymbol.Name,
+ methodSymbol.ContainingType));
+ }
+ }, SymbolKind.Method);
+ });
+ }
+}
\ No newline at end of file
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..6eb10b60
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingD2DShaderProfileOnD2DPixelShaderSourceMethodAnalyzer.cs
@@ -0,0 +1,61 @@
+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;
+ }
+
+ // Ignore methods without [D2DPixelShaderSource]
+ if (!methodSymbol.HasAttributeWithType(d2DPixelShaderSourceAttributeSymbol))
+ {
+ 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
diff --git a/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs
index da780e56..f1887143 100644
--- a/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs
+++ b/src/ComputeSharp.D2D1/Attributes/D2DPixelShaderSourceAttribute.cs
@@ -21,7 +21,7 @@ namespace ComputeSharp.D2D1;
/// float3 rgb = saturate(1.0 - color.rgb);
/// return float4(rgb, 1);
/// }
-/// """")]
+/// """)]
/// static partial ReadOnlySpan<byte> GetBytecode();
///
///
diff --git a/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs
index 6450f6bf..c1c13fe8 100644
--- a/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration/Extensions/AttributeDataExtensions.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Extensions;
@@ -31,9 +32,9 @@ internal static class AttributeDataExtensions
/// The index of the argument to try to retrieve.
/// The resulting argument, if it was found.
/// Whether or not an argument of type at position was found.
- public static bool TryGetConstructorArgument(this AttributeData attributeData, int index, out T? result)
+ public static bool TryGetConstructorArgument(this AttributeData attributeData, int index, [NotNullWhen(true)] out T? result)
{
- if (attributeData.ConstructorArguments.Length >= index + 1 &&
+ if (attributeData.ConstructorArguments.Length > index &&
attributeData.ConstructorArguments[index].Value is T argument)
{
result = argument;
diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs
similarity index 99%
rename from tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs
rename to tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs
index 548bf50f..c834aab6 100644
--- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs
+++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs
@@ -6,7 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.D2D1.Tests.SourceGenerators;
[TestClass]
-public class Test_Analyzers
+public class Test_D2DPixelShaderDescriptorGenerator_Analyzers
{
[TestMethod]
public async Task MissingD2DPixelShaderDescriptor_ComputeShader()
diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs
new file mode 100644
index 00000000..840b9713
--- /dev/null
+++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Analyzers.cs
@@ -0,0 +1,267 @@
+using System.Threading.Tasks;
+using ComputeSharp.D2D1.SourceGenerators;
+using ComputeSharp.Tests.SourceGenerators.Helpers;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.D2D1.Tests.SourceGenerators;
+
+[TestClass]
+public class Test_D2DPixelShaderSourceGenerator_Analyzers
+{
+ [TestMethod]
+ public async Task InvalidD2DPixelShaderSourceMethodReturnType_ReadOnlySpanOfByte_DoesNotWarn()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial ReadOnlySpan InvertEffect();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ [DataRow("TimeSpan")]
+ [DataRow("ReadOnlyMemory")]
+ [DataRow("ReadOnlySpan")]
+ [DataRow("string")]
+ [DataRow("object")]
+ [DataRow("byte")]
+ [DataRow("dynamic")]
+ [DataRow("byte*")]
+ public async Task InvalidD2DPixelShaderSourceMethodReturnType_InvalidType_Warns(string returnType)
+ {
+ string source = $$""""
+ using System;
+ using ComputeSharp.D2D1;
+
+ public unsafe partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial {{returnType}} {|CMPSD2D0057:InvertEffect|}();
+
+ public static partial {{returnType}} InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingShaderProfileForD2DPixelShaderSource_ShaderProfileOnMethod_DoesNotWarn()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
+ public static partial ReadOnlySpan InvertEffect();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingShaderProfileForD2DPixelShaderSource_ShaderProfileOnAssembly_DoesNotWarn()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial ReadOnlySpan InvertEffect();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingShaderProfileForD2DPixelShaderSource_MissingShaderProfile_Warns()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial ReadOnlySpan {|CMPSD2D0055:InvertEffect|}();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingCompileOptionsForD2DPixelShaderSource_CompileOptionsOnMethod_DoesNotWarn()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)]
+ public static partial ReadOnlySpan InvertEffect();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingCompileOptionsForD2DPixelShaderSource_CompileOptionsOnAssembly_DoesNotWarn()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ [assembly: D2DCompileOptions(D2D1CompileOptions.OptimizationLevel0)]
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial ReadOnlySpan InvertEffect();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task MissingCompileOptionsForD2DPixelShaderSource_MissingCompileOptions_Warns()
+ {
+ const string source = """"
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource("""
+ #define D2D_INPUT_COUNT 0
+
+ #include "d2d1effecthelpers.hlsli"
+
+ D2D_PS_ENTRY(Execute)
+ {
+ return 0;
+ }
+ """)]
+ public static partial ReadOnlySpan {|CMPSD2D0056:InvertEffect|}();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """";
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+
+ [TestMethod]
+ public async Task InvalidD2DPixelShaderSource_NullArgument_Warns()
+ {
+ const string source = """
+ using System;
+ using ComputeSharp.D2D1;
+
+ public partial class MyClass
+ {
+ [D2DPixelShaderSource(null)]
+ public static partial ReadOnlySpan {|CMPSD2D0052:InvertEffect|}();
+
+ public static partial ReadOnlySpan InvertEffect() => default;
+ }
+ """;
+
+ await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source);
+ }
+}
\ No newline at end of file