diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
index 1320d2f5..3b947ece 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -55,3 +55,4 @@ CMPSD2D0045 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0046 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0047 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0048 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPSD2D0049 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 7a916e99..d474d3dc 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -698,4 +698,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader must only have unique indices for all of its [D2DResourceTextureIndex] attributes.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for an invalid use of [D2DResourceTextureIndex].
+ ///
+ /// Format: "The field \"{0}\" (in type {1}) is using [D2DResourceTextureIndex] incorrectly (the attribute can only be used on D2D1 resource texture types, but the field is of type {2})".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor InvalidD2DResourceTextureIndexAttributeUse = new DiagnosticDescriptor(
+ id: "CMPSD2D0049",
+ title: "Invalid [D2DResourceTextureIndex] use",
+ messageFormat: "The field \"{0}\" (in type {1}) is using [D2DResourceTextureIndex] incorrectly (the attribute can only be used on D2D1 resource texture types, but the field is of type {2})",
+ category: "ComputeSharp.D2D1.Shaders",
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "A field is using [D2DResourceTextureIndex] incorrectly (the attribute can only be used on D2D1 resource texture types).",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs
index aaf1e680..4312e659 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs
@@ -106,5 +106,30 @@ partial class ID2D1ShaderGenerator
diagnostics.Add(RepeatedD2DResourceTextureIndices, structDeclarationSymbol, structDeclarationSymbol);
}
}
+
+ ///
+ /// Gets the diagnostics for a field with invalid [D2DResourceTextureIndex] attribute use, if that is the case.
+ ///
+ /// The input instance to process.
+ /// The resulting instance for , if needed.
+ public static Diagnostic? GetDiagnosticForFieldWithInvalidD2DResourceTextureIndexAttribute(IFieldSymbol fieldSymbol)
+ {
+ if (fieldSymbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DResourceTextureIndexAttribute"))
+ {
+ string metadataName = fieldSymbol.Type.GetFullMetadataName();
+
+ if (!HlslKnownTypes.IsResourceTextureType(metadataName))
+ {
+ return Diagnostic.Create(
+ InvalidD2DResourceTextureIndexAttributeUse,
+ fieldSymbol.Locations.FirstOrDefault(),
+ fieldSymbol.Name,
+ fieldSymbol.ContainingType,
+ fieldSymbol.Type);
+ }
+ }
+
+ return null;
+ }
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index cf925966..87076d30 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -380,5 +380,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetPixelOptions)}", compilationUnit.GetText(Encoding.UTF8));
});
+
+ // Get all diagnostics for invalid [D2DResourceTextureIndex] uses
+ IncrementalValuesProvider invalidD2DResourceTextureIndexUseErrors =
+ context.SyntaxProvider
+ .CreateSyntaxProvider(
+ static (node, _) => node is FieldDeclarationSyntax { AttributeLists.Count: > 0 },
+ static (context, _) => ((FieldDeclarationSyntax)context.Node).Declaration.Variables.Select(v => (IFieldSymbol)context.SemanticModel.GetDeclaredSymbol(v)!))
+ .SelectMany(static (item, _) => item)
+ .Select(static (item, token) => LoadResourceTextureDescriptions.GetDiagnosticForFieldWithInvalidD2DResourceTextureIndexAttribute(item))
+ .Where(static diagnostic => diagnostic is not null)!;
+
+ // Output the diagnostics
+ context.ReportDiagnostics(invalidD2DResourceTextureIndexUseErrors);
}
}
diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
index d12cb721..906998f8 100644
--- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs
@@ -134,6 +134,27 @@ internal static class ISymbolExtensions
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
}
+ ///
+ /// Checks whether or not a given symbol has an attribute with the specified full name.
+ ///
+ /// The input instance to check.
+ /// The attribute name to look for.
+ /// Whether or not has an attribute with the specified name.
+ public static bool HasAttributeWithFullyQualifiedName(this ISymbol symbol, string name)
+ {
+ ImmutableArray attributes = symbol.GetAttributes();
+
+ foreach (AttributeData attribute in attributes)
+ {
+ if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
///
/// Tries to get an attribute with the specified full metadata name.
///