From 8fbd76526c6d4b5b04e1efa70a0246dfa1de7a61 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 7 Jun 2024 17:21:54 -0700 Subject: [PATCH] Repurpose linking analyzer to trivial sampling --- ...onsTrivialSamplingOnShaderTypeAnalyzer.cs} | 20 +-- .../Diagnostics/DiagnosticDescriptors.cs | 12 +- .../Test_D2DPixelShaderDescriptorGenerator.cs | 2 +- ...ixelShaderDescriptorGenerator_Analyzers.cs | 65 ++++++++++ ...ptionsEnableLinkingOnShaderTypeAnalyzer.cs | 117 ------------------ 5 files changed, 82 insertions(+), 134 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/{InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs => InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs} (71%) delete mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs similarity index 71% rename from src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs rename to src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs index 99e1c13c..d7033d76 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer.cs @@ -7,13 +7,13 @@ using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; /// -/// A diagnostic analyzer that generates an error whenever [D2DCompileOptions] is used on a shader type to request linking when not supported. +/// A diagnostic analyzer that generates an error whenever [D2DPixelOptions] is used on a shader type to incorrectly request trivial sampling. /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : DiagnosticAnalyzer +public sealed class InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer : DiagnosticAnalyzer { /// - public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2D1CompileOptionsEnableLinkingOnShaderType]; + public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2D1PixelOptionsTrivialSamplingOnShaderType]; /// public override void Initialize(AnalysisContext context) @@ -23,8 +23,8 @@ public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : context.RegisterCompilationStartAction(static context => { - // Get the [D2DCompileOptions], [D2DInputCount] and [D2DInputSimple] symbols - if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute") is not { } d2DCompileOptionsAttributeSymbol || + // Get the [D2DPixelOptions], [D2DInputCount] and [D2DInputSimple] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DPixelOptionsAttribute") is not { } d2DPixelOptionsAttributeSymbol || context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputCountAttribute") is not { } d2DInputCountAttributeSymbol || context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputSimpleAttribute") is not { } d2DInputSimpleAttributeSymbol) { @@ -39,9 +39,9 @@ public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : return; } - // If the type is not using [D2DCompileOptions] with D2D1CompileOptions.EnableLinking, there's nothing to do - if (!typeSymbol.TryGetAttributeWithType(d2DCompileOptionsAttributeSymbol, out AttributeData? compileOptionsAttribute) || - !((D2D1CompileOptions)compileOptionsAttribute.ConstructorArguments[0].Value!).HasFlag(D2D1CompileOptions.EnableLinking)) + // If the type is not using [D2DPixelOptions] with D2D1PixelOptions.TrivialSampling, there's nothing to do + if (!typeSymbol.TryGetAttributeWithType(d2DPixelOptionsAttributeSymbol, out AttributeData? pixelOptionsAttribute) || + !((D2D1PixelOptions)pixelOptionsAttribute.ConstructorArguments[0].Value!).HasFlag(D2D1PixelOptions.TrivialSampling)) { return; } @@ -57,8 +57,8 @@ public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : if (!D2DPixelShaderDescriptorGenerator.HlslBytecode.IsSimpleInputShader(typeSymbol, d2DInputSimpleAttributeSymbol, inputCount)) { context.ReportDiagnostic(Diagnostic.Create( - InvalidD2D1CompileOptionsEnableLinkingOnShaderType, - compileOptionsAttribute.GetLocation(), + InvalidD2D1PixelOptionsTrivialSamplingOnShaderType, + pixelOptionsAttribute.GetLocation(), typeSymbol)); } }, SymbolKind.NamedType); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 2e615f6c..23d1d418 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1024,19 +1024,19 @@ partial class DiagnosticDescriptors helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// - /// Gets a for an invalid use of [D2DCompileOptions] requesting to enable linking. + /// Gets a for an invalid use of [D2DPixelOptions] indicating trivial sampling. /// - /// Format: "The D2D1 shader of type {0} cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute, as it doesn't support linking (only D2D1 shaders with no complex inputs can use this option)". + /// Format: "The D2D1 shader of type {0} shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute, as it has one or more complex inputs (either mark the inputs as simple, or remove the trivial sampling option)". /// /// - public static readonly DiagnosticDescriptor InvalidD2D1CompileOptionsEnableLinkingOnShaderType = new( + public static readonly DiagnosticDescriptor InvalidD2D1PixelOptionsTrivialSamplingOnShaderType = new( id: "CMPSD2D0069", - title: "Invalid [D2DResourceTextureIndex] use", - messageFormat: """The D2D1 shader of type {0} cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute, as it doesn't support linking (only D2D1 shaders with no complex inputs can use this option)""", + title: "Invalid [D2DPixelOptions] use", + messageFormat: """The D2D1 shader of type {0} shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute, as it has one or more complex inputs (either mark the inputs as simple, or remove the trivial sampling option)""", category: "ComputeSharp.D2D1.Shaders", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: "A D2D1 shader cannot use D2D1CompileOptions.EnableLinking in its [D2DCompileOptions] attribute if it doesn't support linking (only D2D1 shaders with no complex inputs can use this option).", + description: "A D2D1 shader shouldn't use D2D1PixelOptions.TrivialSampling in its [D2DPixelOptions] attribute if it has one or more complex inputs (because trivial sampling shaders can only sample pixels at the same scene coordinate as the output pixel, a shader using this option should only have simple inputs).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); /// diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index f772dbcc..f2507eba 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -1784,7 +1784,7 @@ public class Test_D2DPixelShaderDescriptorGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index 31ea0031..12cf8f56 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -1117,4 +1117,69 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task TrivialSamplingWithComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task TrivialSamplingWithExplicitComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + [D2DInputComplex(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task TrivialSamplingWithLeftoverComplexInput_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(2)] + [D2DInputSimple(1)] + [{|CMPSD2D0069:D2DPixelOptions(D2D1PixelOptions.TrivialSampling)|}] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs deleted file mode 100644 index 6cd4b336..00000000 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System.Threading.Tasks; -using ComputeSharp.D2D1.SourceGenerators; -using ComputeSharp.Tests.SourceGenerators.Helpers; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.D2D1.Tests.SourceGenerators; - -[TestClass] -public class Test_InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer -{ - [TestMethod] - public async Task NoCompileOptionsDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(0)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task ValidCompileOptionsDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(0)] - [D2DCompileOptions(D2D1CompileOptions.PackMatrixRowMajor)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithNoInputCountDoesNotWarn() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputComplex(0)] - [D2DCompileOptions(D2D1CompileOptions.EnableLinking)] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithImplicitComplexInputWarns() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(2)] - [D2DInputSimple(1)] - [{|CMPSD2D0069:D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)|}] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } - - [TestMethod] - public async Task EnableLinkingWithExplicitComplexInputWarns() - { - const string source = """ - using ComputeSharp; - using ComputeSharp.D2D1; - - public partial class Foo - { - [D2DInputCount(2)] - [D2DInputSimple(0)] - [D2DInputComplex(1)] - [{|CMPSD2D0069:D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)|}] - private partial struct MyShader : ID2D1PixelShader - { - public Float4 Execute() => 0; - } - } - """; - - await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); - } -} \ No newline at end of file