Skip to content

Commit 5c83ac3

Browse files
authored
Merge pull request #399 from Sergio0694/dev/missing-d2d-count-diagnostic
Add diagnostic for missing [D2DInputCount] attribute
2 parents ee936f8 + 3fb94da commit 5c83ac3

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ CMPSD2D0054 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
6464
CMPSD2D0055 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
6565
CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
6666
CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
67+
CMPSD2D0058 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)

src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ partial class DiagnosticDescriptors
737737
/// </summary>
738738
public static readonly DiagnosticDescriptor InvalidResourceTextureElementType = new DiagnosticDescriptor(
739739
id: "CMPSD2D0051",
740-
title: "Missing [D2DResourceTextureIndex] attribute",
740+
title: "Invalid D2D1 resource texture element type",
741741
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)",
742742
category: "ComputeSharp.D2D1.Shaders",
743743
defaultSeverity: DiagnosticSeverity.Error,
@@ -840,4 +840,20 @@ partial class DiagnosticDescriptors
840840
isEnabledByDefault: true,
841841
description: "Methods using using [D2DPixelShaderSource] must use ReadOnlySpan<byte> as the return type.",
842842
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
843+
844+
/// <summary>
845+
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DInputCount]</c> attribute is missing.
846+
/// <para>
847+
/// Format: <c>"The D2D1 shader of type {0} is not annotated with the [D2DInputCount] attribute (it is mandatory for all D2D1 shader types)"</c>.
848+
/// </para>
849+
/// </summary>
850+
public static readonly DiagnosticDescriptor MissingD2DInputCountAttribute = new DiagnosticDescriptor(
851+
id: "CMPSD2D0058",
852+
title: "Missing [D2DResourceTextureIndex] attribute",
853+
messageFormat: "The D2D1 shader of type {0} is not annotated with the [D2DInputCount] attribute (it is mandatory for all D2D1 shader types)",
854+
category: "ComputeSharp.D2D1.Shaders",
855+
defaultSeverity: DiagnosticSeverity.Error,
856+
isEnabledByDefault: true,
857+
description: "A D2D1 shader must be annotated with the [D2DInputCount] attribute.",
858+
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
843859
}

src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static void GetInfo(
3737
// diagnostics to, but without returning invalid values to the caller which
3838
// might cause generator errors (eg. -1 would cause other code to just throw).
3939
int rawInputCount = 0;
40+
bool isInputCountPresent = false;
4041

4142
inputCount = 0;
4243

@@ -50,6 +51,7 @@ public static void GetInfo(
5051
case "ComputeSharp.D2D1.D2DInputCountAttribute":
5152
rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
5253
inputCount = Math.Max(rawInputCount, 0);
54+
isInputCountPresent = true;
5355
break;
5456
case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
5557
inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
@@ -66,6 +68,14 @@ public static void GetInfo(
6668
inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
6769
combinedInputTypes = ImmutableArray<uint>.Empty;
6870

71+
// Ensure that the input count is present
72+
if (!isInputCountPresent)
73+
{
74+
diagnostics.Add(MissingD2DInputCountAttribute, structDeclarationSymbol, structDeclarationSymbol);
75+
76+
return;
77+
}
78+
6979
// Validate the input count
7080
if (rawInputCount is not (>= 0 and <= 8))
7181
{

tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void MapOutputToInputs(in Rectangle output, Span<Rectangle> inputs)
4545
{
4646
}
4747

48+
[D2DInputCount(0)]
4849
public partial struct Shader : ID2D1PixelShader
4950
{
5051
public float4 Execute() => default;

tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/// </summary>
66
internal sealed partial class D2D1ResourceTextureUninitializedFieldDiagnosticSuppressorTests
77
{
8+
[D2DInputCount(0)]
89
public readonly partial struct MyShader : ID2D1PixelShader
910
{
1011
// This test just needs to validate the project builds fine with this shader.

0 commit comments

Comments
 (0)