diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index f0709b41..227c5323 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -64,3 +64,4 @@ CMPSD2D0054 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0055 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0058 | 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 0c6712c9..4dceab78 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -737,7 +737,7 @@ partial class DiagnosticDescriptors /// public static readonly DiagnosticDescriptor InvalidResourceTextureElementType = new DiagnosticDescriptor( id: "CMPSD2D0051", - title: "Missing [D2DResourceTextureIndex] attribute", + title: "Invalid D2D1 resource texture element type", messageFormat: "The field \"{0}\" (in type {1}) using a D2D1 resource texture of type {2} has an invalid element type (only float and float4 type arguments are supported)", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, @@ -840,4 +840,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "Methods using using [D2DPixelShaderSource] must use ReadOnlySpan as the return type.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DInputCount] attribute is missing. + /// + /// Format: "The D2D1 shader of type {0} is not annotated with the [D2DInputCount] attribute (it is mandatory for all D2D1 shader types)". + /// + /// + public static readonly DiagnosticDescriptor MissingD2DInputCountAttribute = new DiagnosticDescriptor( + id: "CMPSD2D0058", + title: "Missing [D2DResourceTextureIndex] attribute", + messageFormat: "The D2D1 shader of type {0} is not annotated with the [D2DInputCount] attribute (it is mandatory for all D2D1 shader types)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader must be annotated with the [D2DInputCount] attribute.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs index e9904031..b6e85408 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs @@ -37,6 +37,7 @@ partial class ID2D1ShaderGenerator // diagnostics to, but without returning invalid values to the caller which // might cause generator errors (eg. -1 would cause other code to just throw). int rawInputCount = 0; + bool isInputCountPresent = false; inputCount = 0; @@ -50,6 +51,7 @@ partial class ID2D1ShaderGenerator case "ComputeSharp.D2D1.D2DInputCountAttribute": rawInputCount = (int)attributeData.ConstructorArguments[0].Value!; inputCount = Math.Max(rawInputCount, 0); + isInputCountPresent = true; break; case "ComputeSharp.D2D1.D2DInputSimpleAttribute": inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); @@ -66,6 +68,14 @@ partial class ID2D1ShaderGenerator inputComplexIndices = inputComplexIndicesBuilder.ToImmutable(); combinedInputTypes = ImmutableArray.Empty; + // Ensure that the input count is present + if (!isInputCountPresent) + { + diagnostics.Add(MissingD2DInputCountAttribute, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + // Validate the input count if (rawInputCount is not (>= 0 and <= 8)) { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 83119320..d2dc4479 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -45,6 +45,7 @@ public partial class D2D1PixelShaderEffectTests { } + [D2DInputCount(0)] public partial struct Shader : ID2D1PixelShader { public float4 Execute() => default; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs index 7a873e9a..7a633d51 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs @@ -5,6 +5,7 @@ /// internal sealed partial class D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests { + [D2DInputCount(0)] public readonly partial struct MyShader : ID2D1PixelShader { // This test just needs to validate the project builds fine with this shader.