From 5777e17e038046b324a3d0ff3a8c772e3285a756 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Aug 2022 17:52:48 +0200 Subject: [PATCH 1/2] Add diagnostic for missing [D2DRequiresScenePosition] --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 +++++++ ...erGenerator.CreateBuildHlslSourceMethod.cs | 47 ++++++++++++++++--- .../Mappings/HlslKnownMethods.cs | 11 +++++ .../SyntaxRewriters/HlslSourceRewriter.cs | 5 ++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 12 ++++- .../SyntaxRewriters/StaticFieldRewriter.cs | 9 +++- 7 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 72d09bf1..7bd22acc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -51,3 +51,4 @@ CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0042 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0043 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0044 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0045 | 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 8f46d380..760a0a62 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -634,4 +634,20 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true, description: "A D2D1 shader generated with ComputeSharp.D2D1 (or an assembly) cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DRequiresPosition] attribute is missing. + /// + /// Format: "The D2D1 shader of type {0} is calling a D2D1 API or another method that require the [D2DRequiresPosition] attribute to be used, but the shader type is not annotated accordingly". + /// + /// + public static readonly DiagnosticDescriptor MissingD2DRequiresPositionAttribute = new DiagnosticDescriptor( + id: "CMPSD2D0045", + title: "Missing [D2DRequiresPosition] attribute", + messageFormat: "The D2D1 shader of type {0} is calling a D2D1 API or another method that require the [D2DRequiresPosition] attribute to be used, but the shader type is not annotated accordingly", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader using functionality that needs position info (ie. when [D2DRequiresPosition] is used) needs to be annotated accordingly.", + 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 a895ec4c..1f454394 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -56,15 +56,18 @@ partial class ID2D1ShaderGenerator // Explore the syntax tree and extract the processed info var semanticModelProvider = new SemanticModelProvider(compilation); var valueFields = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes); - var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions); - var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions); + var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, out bool methodsNeedD2D1RequiresPosition); + var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresPosition); // Process the discovered types and constants var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes); var definedConstants = GetDefinedConstants(constantDefinitions); // Check whether the scene position is required - bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol); + bool requiresScenePosition = GetD2DRequiresScenePositionInfo(structDeclarationSymbol); + + // Emit diagnostics for incorrect scene position uses + ReportInvalidD2DRequiresPositionUse(diagnostics, structDeclarationSymbol, requiresScenePosition, methodsNeedD2D1RequiresPosition || fieldsNeedD2D1RequiresPosition); // Get the HLSL source return GetHlslSource( @@ -143,6 +146,7 @@ partial class ID2D1ShaderGenerator /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// Whether or not the shader needs the [D2DRequiresPosition] annotation. /// A sequence of static constant fields in . private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( ImmutableArray.Builder diagnostics, @@ -150,10 +154,13 @@ partial class ID2D1ShaderGenerator StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, - IDictionary constantDefinitions) + IDictionary constantDefinitions, + out bool needsD2D1RequiresPosition) { ImmutableArray<(string, string, string?)>.Builder builder = ImmutableArray.CreateBuilder<(string, string, string?)>(); + needsD2D1RequiresPosition = false; + foreach (var fieldDeclaration in structDeclaration.Members.OfType()) { foreach (var variableDeclarator in fieldDeclaration.Declaration.Variables) @@ -190,6 +197,8 @@ partial class ID2D1ShaderGenerator string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString(); + needsD2D1RequiresPosition |= staticFieldRewriter.NeedsD2DRequiresPositionAttribute; + builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); } } @@ -207,6 +216,7 @@ partial class ID2D1ShaderGenerator /// The collection of currently discovered types. /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. + /// Whether or not the shader needs the [D2DRequiresPosition] annotation. /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods( ImmutableArray.Builder diagnostics, @@ -215,7 +225,8 @@ partial class ID2D1ShaderGenerator SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary staticMethods, - IDictionary constantDefinitions) + IDictionary constantDefinitions, + out bool needsD2D1RequiresPosition) { // Find all declared methods in the type ImmutableArray methodDeclarations = ( @@ -226,6 +237,8 @@ partial class ID2D1ShaderGenerator string? entryPoint = null; ImmutableArray<(string, string)>.Builder methods = ImmutableArray.CreateBuilder<(string, string)>(); + needsD2D1RequiresPosition = false; + foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!; @@ -253,6 +266,9 @@ partial class ID2D1ShaderGenerator // Rewrite the method syntax tree MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); + // Update the position requirement + needsD2D1RequiresPosition |= shaderSourceRewriter.NeedsD2DRequiresPositionAttribute; + // Emit the extracted local functions first foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { @@ -395,12 +411,31 @@ partial class ID2D1ShaderGenerator } } + /// + /// Reports diagnostics for invalid uses of [D2DRequiresPosition] in a shader. + /// + /// The collection of produced instances. + /// The input instance to process. + /// Whether the shader type is declaring the need for scene position. + /// Whether the shader is using APIs that rely on scene position. + private static void ReportInvalidD2DRequiresPositionUse( + ImmutableArray.Builder diagnostics, + INamedTypeSymbol structDeclarationSymbol, + bool requiresScenePosition, + bool usesPositionDependentMethods) + { + if (!requiresScenePosition && usesPositionDependentMethods) + { + diagnostics.Add(MissingD2DRequiresPositionAttribute, structDeclarationSymbol, structDeclarationSymbol); + } + } + /// /// Gets whether or not a given shader requires the scene position. /// /// The input instance to process. /// Whether requires the scene position. - private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol) + private static bool GetD2DRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol) { return structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", out _); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs index 99964394..05c451f4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -10,6 +10,17 @@ namespace ComputeSharp.SourceGeneration.Mappings; /// partial class HlslKnownMethods { + /// + /// Checks whether or not a method name (previous matched with + /// needs the [D2DRequiresPosition] annotation on its containing shader in order to be used. + /// + /// The fully qualified metadata name. + /// Whether the method needs the [D2DRequiresPosition] annotation. + public static bool NeedsD2DRequiresPositionAttribute(string name) + { + return name == "ComputeSharp.D2D1.D2D.GetScenePosition"; + } + /// static partial void AddKnownMethods(IDictionary knownMethods) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index af78bc83..26f9c756 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -65,6 +65,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter Diagnostics = diagnostics; } + /// + /// Gets whether or not the shader needs the [D2DRequiresPosition] attribute. + /// + public bool NeedsD2DRequiresPositionAttribute { get; protected set; } + /// public override SyntaxNode VisitCastExpression(CastExpressionSyntax node) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 2e32afce..31f325a4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -77,7 +77,7 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. /// The collection of produced instances. - public ShaderSourceRewriter( + private ShaderSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary staticMethods, @@ -278,11 +278,19 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter operation.TargetMethod is IMethodSymbol method && method.IsStatic) { + string metadataName = method.GetFullMetadataName(); + // If the invocation consists of invoking a static method that has a direct // mapping to HLSL, rewrite the expression in the current invocation node. // For instance: Math.Abs(expr) => abs(expr). - if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping)) + if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping)) { + // Track whether the method needs [D2DRequiresPosition] + if (HlslKnownMethods.NeedsD2DRequiresPositionAttribute(metadataName)) + { + NeedsD2DRequiresPositionAttribute = true; + } + return updatedNode.WithExpression(ParseExpression(mapping!)); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 56623724..f4ae8d82 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -94,9 +94,16 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter operation.TargetMethod is IMethodSymbol method && method.IsStatic) { + string metadataName = method.GetFullMetadataName(); + // Rewrite HLSL intrinsic methods - if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping)) + if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping)) { + if (HlslKnownMethods.NeedsD2DRequiresPositionAttribute(metadataName)) + { + NeedsD2DRequiresPositionAttribute = true; + } + return updatedNode.WithExpression(ParseExpression(mapping!)); } } From 55ad39b817bdb75e828f02e31d9b6220c90fbcc9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Aug 2022 18:04:24 +0200 Subject: [PATCH 2/2] Tweak diagnostics, add SampleInputAtPosition checks too --- .../Diagnostics/DiagnosticDescriptors.cs | 6 +++--- .../Mappings/HlslKnownMethods.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 760a0a62..086410da 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -638,16 +638,16 @@ internal static class DiagnosticDescriptors /// /// Gets a for when the [D2DRequiresPosition] attribute is missing. /// - /// Format: "The D2D1 shader of type {0} is calling a D2D1 API or another method that require the [D2DRequiresPosition] attribute to be used, but the shader type is not annotated accordingly". + /// Format: "The D2D1 shader of type {0} is using D2D1 APIs that require the [D2DRequiresPosition] attribute to be used (that is, D2D1.GetScenePosition() and D2D1.SampleInputAtPosition(int, float2)), but the shader type is not annotated accordingly". /// /// public static readonly DiagnosticDescriptor MissingD2DRequiresPositionAttribute = new DiagnosticDescriptor( id: "CMPSD2D0045", title: "Missing [D2DRequiresPosition] attribute", - messageFormat: "The D2D1 shader of type {0} is calling a D2D1 API or another method that require the [D2DRequiresPosition] attribute to be used, but the shader type is not annotated accordingly", + messageFormat: "The D2D1 shader of type {0} is using D2D1 APIs that require the [D2DRequiresPosition] attribute to be used (that is, D2D.GetScenePosition() and D2D.SampleInputAtPosition(int, float2)), but the shader type is not annotated accordingly", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "A D2D1 shader using functionality that needs position info (ie. when [D2DRequiresPosition] is used) needs to be annotated accordingly.", + description: "A D2D1 shader using functionality that needs position info (ie. when [D2DRequiresPosition] is used, which is mandatory when calling either D2D.GetScenePosition() or D2D.SampleInputAtPosition(int, float2)) needs to be annotated accordingly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs index 05c451f4..5c502afc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -18,7 +18,7 @@ partial class HlslKnownMethods /// Whether the method needs the [D2DRequiresPosition] annotation. public static bool NeedsD2DRequiresPositionAttribute(string name) { - return name == "ComputeSharp.D2D1.D2D.GetScenePosition"; + return name is "ComputeSharp.D2D1.D2D.GetScenePosition" or "ComputeSharp.D2D1.D2D.SampleInputAtPosition"; } ///