diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 7f490bdf..c9693689 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -41,3 +41,8 @@ CMPSD2D0031 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0032 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0033 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0034 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0035 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0036 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0037 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0038 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0039 | 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 0175dd9a..28f15412 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -459,7 +459,7 @@ internal static class DiagnosticDescriptors /// /// Gets a for an embedded bytecode shader failed due to an HLSL compilation exception. /// - /// Format: "The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: "{1}")". + /// Format: "The shader of type {0} failed to compile due to an HLSL compiler error (Message: "{1}")". /// /// public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithDxcCompilationException = new DiagnosticDescriptor( @@ -471,4 +471,84 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true, description: "The embedded bytecode for a shader failed to be compiled due to an HLSL compiler error.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a invalid D2D input count value. + /// + /// Format: "The D2D1 shader of type {0} is using an incorrect value for [D2DInputCount] (the number of inputs must be in the [1, 8] range)". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DInputCount = new DiagnosticDescriptor( + id: "CMPSD2D0035", + title: "Invalid D2D1 shader input count", + messageFormat: "The D2D1 shader of type {0} is using an incorrect value for [D2DInputCount] (the number of inputs must be in the [1, 8] range)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader must have a number of inputs in the [1, 8] range.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for repeated indices for D2D simple indices. + /// + /// Format: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputSimple] attributes". + /// + /// + public static readonly DiagnosticDescriptor RepeatedD2DInputSimpleIndices = new DiagnosticDescriptor( + id: "CMPSD2D0036", + title: "Repeated D2D1 shader simple input indices", + messageFormat: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputSimple] attributes", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader must only have unique indices for all of its [D2DInputSimple] attributes.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for repeated indices for D2D complex indices. + /// + /// Format: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputComplex] attributes". + /// + /// + public static readonly DiagnosticDescriptor RepeatedD2DInputComplexIndices = new DiagnosticDescriptor( + id: "CMPSD2D0037", + title: "Repeated D2D1 shader complex input indices", + messageFormat: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputComplex] attributes", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader must only have unique indices for all of its [D2DInputComplex] attributes.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for invalid combination of simple and complex indices. + /// + /// Format: "The D2D1 shader of type {0} is using some indices in common for simple and complex input indices". + /// + /// + public static readonly DiagnosticDescriptor InvalidSimpleAndComplexIndicesCombination = new DiagnosticDescriptor( + id: "CMPSD2D0038", + title: "Invalid D2D1 shader simple and complex indices combination", + messageFormat: "The D2D1 shader of type {0} is using some indices in common for simple and complex input indices", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader cannot use the same indices to indicate both simple and complex inputs.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader with an out of range input index (or more). + /// + /// Format: "The D2D1 shader of type {0} is using some out of range input indices". + /// + /// + public static readonly DiagnosticDescriptor OutOfRangeInputIndex = new DiagnosticDescriptor( + id: "CMPSD2D0039", + title: "Our of range D2D1 shader input indices", + messageFormat: "The D2D1 shader of type {0} is using some out of range input indices", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader must have all the indices of its simple and complex inputs in the valid range (between 1 and the number of declared inputs).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index d39b44d9..8cdb0a82 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -60,6 +60,7 @@ partial class ID2D1ShaderGenerator // Gather the shader metadata GatherD2D1AttributeInfo( + builder, structDeclarationSymbol, out int inputCount, out ImmutableArray inputSimpleIndices, @@ -365,18 +366,25 @@ partial class ID2D1ShaderGenerator /// /// Extracts the metadata definition for the current shader. /// + /// The collection of produced instances. /// The input instance to process. /// The number of shader inputs to declare. /// The indicess of the simple shader inputs. /// The indicess of the complex shader inputs. /// Whether the shader requires the scene position. private static void GatherD2D1AttributeInfo( + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, out int inputCount, out ImmutableArray inputSimpleIndices, out ImmutableArray inputComplexIndices, out bool requiresScenePosition) { + // We need a separate local here so we can keep the original value to apply + // 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; + inputCount = 0; requiresScenePosition = false; @@ -388,7 +396,8 @@ partial class ID2D1ShaderGenerator switch (attributeData.AttributeClass?.GetFullMetadataName()) { case "ComputeSharp.D2D1.D2DInputCountAttribute": - inputCount = (int)attributeData.ConstructorArguments[0].Value!; + rawInputCount = (int)attributeData.ConstructorArguments[0].Value!; + inputCount = Math.Max(rawInputCount, 0); break; case "ComputeSharp.D2D1.D2DInputSimpleAttribute": inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); @@ -406,6 +415,49 @@ partial class ID2D1ShaderGenerator inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable(); inputComplexIndices = inputComplexIndicesBuilder.ToImmutable(); + + // Validate the input count + if (rawInputCount is not (>= 1 and <= 8)) + { + diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder); + HashSet inputComplexIndicesAsSet = new(inputComplexIndicesBuilder); + + // All simple indices must be unique + if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count) + { + diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + // All complex indices must be unique + if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count) + { + diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + // Simple and complex indices can't have indices in common + if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any()) + { + diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + int maxAllowedInputIndex = rawInputCount - 1; + + // All simple indices must be in range + if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => i < 0 || i > maxAllowedInputIndex)) + { + diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol); + } } ///