diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 6fe7a3a5..45dfa3bb 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -82,3 +82,7 @@ CMPSD2D0072 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0073 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0074 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0075 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0076 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0077 | ComputeSharp.D2D1.Shaders | Info | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0078 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0079 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnAssemblyAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnAssemblyAnalyzer.cs new file mode 100644 index 00000000..bed88bf1 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnAssemblyAnalyzer.cs @@ -0,0 +1,39 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates diagnostics for the [D2DEnableRuntimeCompilation] attribute, when used on an assembly. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class D2DEnableRuntimeCompilationOnAssemblyAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(D2DRuntimeCompilationOnAssemblyNotNecessary); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationAction(static context => + { + IAssemblySymbol assemblySymbol = context.Compilation.Assembly; + + // Emit a diagnostic if an assembly has both [D2DShaderProfile] and [D2DEnableRuntimeCompilation] (the latter is useless) + if (assemblySymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out _) && + assemblySymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DEnableRuntimeCompilationAttribute", out AttributeData? attributeData)) + { + context.ReportDiagnostic(Diagnostic.Create( + D2DRuntimeCompilationOnAssemblyNotNecessary, + attributeData.GetLocation(), + assemblySymbol)); + } + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnTypeAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnTypeAnalyzer.cs new file mode 100644 index 00000000..2d64da80 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/D2DEnableRuntimeCompilationOnTypeAnalyzer.cs @@ -0,0 +1,97 @@ +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates diagnostics for the [D2DEnableRuntimeCompilation] attribute, when used on a type. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class D2DEnableRuntimeCompilationOnTypeAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create( + D2DRuntimeCompilationDisabled, + D2DRuntimeCompilationAlreadyEnabled, + D2DRuntimeCompilationOnTypeNotNecessary); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the [D2DShaderProfile], [D2DEnableRuntimeCompilation] and [D2DGeneratedPixelShaderDescriptor] symbols + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute") is not { } d2DShaderProfileAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEnableRuntimeCompilationAttribute") is not { } d2DEnableRuntimeCompilationAttributeSymbol || + context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") is not { } d2DGeneratedPixelShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // Skip the type if it's not a generated shader descriptor target + if (!typeSymbol.HasAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol)) + { + return; + } + + // This analyzer will detect the following cases: + // 1) Shader not precompiled, and missing [D2DEnableRuntimeCompilation] (D2DRuntimeCompilationDisabled) + // 2) Shader with [D2DEnableRuntimeCompilation] on the assembly and also on the shader type (D2DRuntimeCompilationAlreadyEnabled) + // 3) Shader being precompiled, but also with [D2DEnableRuntimeCompilation] (D2DRuntimeCompilationNotNecessary) + bool hasD2DShaderProfileAttributeOnAssembly = typeSymbol.ContainingAssembly.HasAttributeWithType(d2DShaderProfileAttributeSymbol); + bool hasD2DShaderProfileAttributeOnType = typeSymbol.HasAttributeWithType(d2DShaderProfileAttributeSymbol); + bool hasD2DEnableRuntimeCompilationAttributeOnAssembly = typeSymbol.ContainingAssembly.HasAttributeWithType(d2DEnableRuntimeCompilationAttributeSymbol); + bool hasD2DEnableRuntimeCompilationAttributeOnType = typeSymbol.TryGetAttributeWithType( + d2DEnableRuntimeCompilationAttributeSymbol, + out AttributeData? d2DEnableRuntimeCompilationAttributeData); + + // If [D2DEnableRuntimeCompilation] is present on the type and also on the assembly, emit a diagnostic (2) + if (hasD2DEnableRuntimeCompilationAttributeOnAssembly && hasD2DEnableRuntimeCompilationAttributeOnType) + { + context.ReportDiagnostic(Diagnostic.Create( + D2DRuntimeCompilationAlreadyEnabled, + d2DEnableRuntimeCompilationAttributeData!.GetLocation(), + typeSymbol)); + } + + // Check if the shader is precompiled: + // - If it is, we should check that it's not annotated with [D2DEnableRuntimeCompilation] + // - If it is not, we should check that it is annotated with [D2DEnableRuntimeCompilation] + if (hasD2DShaderProfileAttributeOnType || hasD2DShaderProfileAttributeOnAssembly) + { + // Emit a diagnostic if [D2DEnableRuntimeCompilation] is present on the type (3) + if (hasD2DEnableRuntimeCompilationAttributeOnType) + { + context.ReportDiagnostic(Diagnostic.Create( + D2DRuntimeCompilationOnTypeNotNecessary, + d2DEnableRuntimeCompilationAttributeData!.GetLocation(), + typeSymbol)); + } + } + else if (!hasD2DEnableRuntimeCompilationAttributeOnType && !hasD2DEnableRuntimeCompilationAttributeOnAssembly) + { + // Emit a diagnostic if there is no [D2DEnableRuntimeCompilation] set anywhere (1) + context.ReportDiagnostic(Diagnostic.Create( + D2DRuntimeCompilationDisabled, + typeSymbol.Locations.FirstOrDefault(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index ec8921e5..e76019d2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1125,4 +1125,69 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "Methods from the Math and MathF types cannot be used in a shader, and equivalent APIs from the Hlsl type should be used instead.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader not precompiled but with runtime compilation disabled. + /// + /// Format: "The shader {0} is not precompiled, but runtime compilation is not enabled (either precompile the shader by using [D2DShaderProfile], which is recommended, or enable runtime compilation using [D2DEnableRuntimeCompilation])". + /// + /// + public static readonly DiagnosticDescriptor D2DRuntimeCompilationDisabled = new( + id: "CMPSD2D0076", + title: "D2D runtime compilation disabled", + messageFormat: "The shader {0} is not precompiled, but runtime compilation is not enabled (either precompile the shader by using [D2DShaderProfile], which is recommended, or enable runtime compilation using [D2DEnableRuntimeCompilation])", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Shaders must either be precompiled using [D2DShaderProfile], which is recommended, or runtime compilation for shaders must be enabled using [D2DEnableRuntimeCompilation].", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an unnecessary [D2DEnableRuntimeCompilation] use. + /// + /// Format: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but its containing assembly is already annotated with this attribute (so using it again on the shader type is unnecessary)". + /// + /// + public static readonly DiagnosticDescriptor D2DRuntimeCompilationAlreadyEnabled = new( + id: "CMPSD2D0077", + title: "D2D runtime compilation already enabled", + messageFormat: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but its containing assembly is already annotated with this attribute (so using it again on the shader type is unnecessary)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Info, + isEnabledByDefault: true, + description: "If an assembly is annotated with [D2DEnableRuntimeCompilation], that applies to all shader types within it. That means that using [D2DEnableRuntimeCompilation] on shader types too is unnecessary.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an unnecessary [D2DEnableRuntimeCompilation] use on . + /// + /// Format: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but it is also being precompiled, as it has [D2DShaderProfile] on the type or containing assembly (so using [D2DEnableRuntimeCompilation] is unnecessary)". + /// + /// + public static readonly DiagnosticDescriptor D2DRuntimeCompilationOnTypeNotNecessary = new( + id: "CMPSD2D0078", + title: "D2D runtime compilation on type is not necessary", + messageFormat: "The shader {0} is annotated with [D2DEnableRuntimeCompilation], but it is also being precompiled, as it has [D2DShaderProfile] on the type or containing assembly (so using [D2DEnableRuntimeCompilation] is unnecessary)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "If a shader is precompiled (ie. it has [D2DShaderProfile] on the type declaration or on its containing assembly), also using [D2DEnableRuntimeCompilation] is unnecessary.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an unnecessary [D2DEnableRuntimeCompilation] use on an assembly. + /// + /// Format: "The assembly {0} is annotated with [D2DEnableRuntimeCompilation], but it is also has an assembly-level [D2DShaderProfile] attribute, which will cause all shaders to be precompiled (so using [D2DEnableRuntimeCompilation] is unnecessary)". + /// + /// + public static readonly DiagnosticDescriptor D2DRuntimeCompilationOnAssemblyNotNecessary = new( + id: "CMPSD2D0079", + title: "D2D runtime compilation on assembly is not necessary", + messageFormat: "The assembly {0} is annotated with [D2DEnableRuntimeCompilation], but it is also has an assembly-level [D2DShaderProfile] attribute, which will cause all shaders to be precompiled (so using [D2DEnableRuntimeCompilation] is unnecessary)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "If an assembly is using [D2DShaderProfile] (meaning that all shaders declared within it will be precompiled), also using [D2DEnableRuntimeCompilation] is unnecessary.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp", + customTags: WellKnownDiagnosticTags.CompilationEnd); } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.EffectGraph.cs index 0d3753c3..628e0894 100644 --- a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.EffectGraph.cs +++ b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.EffectGraph.cs @@ -228,9 +228,7 @@ partial class CanvasEffect /// This interface only implemented by and it's not meant to be implemented by external types. /// /// - protected interface IEffectNode - { - } + protected interface IEffectNode; /// /// A marker interface for a generic effect node that was registered from an value. @@ -246,16 +244,12 @@ partial class CanvasEffect /// /// protected interface IEffectNode : IEffectNode - where T : ICanvasImage - { - } + where T : ICanvasImage; /// /// A marker type for an effect node that can be registered and retrieved from an value. /// /// The type of associated with the current effect node. protected sealed class EffectNode : IEffectNode - where T : class, ICanvasImage - { - } + where T : class, ICanvasImage; } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEnableRuntimeCompilation.cs b/src/ComputeSharp.D2D1/Attributes/D2DEnableRuntimeCompilation.cs new file mode 100644 index 00000000..503bf6fe --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DEnableRuntimeCompilation.cs @@ -0,0 +1,27 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute that indicates that runtime compilation of D2D shaders is enabled. +/// This attribute is required for all shaders that are not compiled at build time. +/// +/// +/// +/// Precompiling shaders at build time is recommended, as it enables additional validation +/// and diagnostics, and provides better performance at runtime when dispatching the shaders. +/// +/// +/// This option can be used in advanced scenarios, for instance when trying to minimize +/// binary size (which should only be done after carefully measuring the actual difference). +/// +/// +/// Note that not using this attribute does not mean that trying to compile shaders at runtime will +/// fail. The purpose of this attribute is to allow declaring shader types that are not precompiled. +/// All other features, such as manually compiling a shader type with specific compile options or +/// with a different shader profile, or manually compiling a shader from source at runtime, are +/// always supported, regardless of whether nor not this attribute is used. +/// +/// +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] +public sealed class D2DEnableRuntimeCompilationAttribute : Attribute; \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DGeneratedPixelShaderDescriptorAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DGeneratedPixelShaderDescriptorAttribute.cs index da27d2fe..92c531b3 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DGeneratedPixelShaderDescriptorAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DGeneratedPixelShaderDescriptorAttribute.cs @@ -19,6 +19,4 @@ namespace ComputeSharp.D2D1; /// /// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] -public sealed class D2DGeneratedPixelShaderDescriptorAttribute : Attribute -{ -} \ No newline at end of file +public sealed class D2DGeneratedPixelShaderDescriptorAttribute : Attribute; \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs index 8355261d..fb4ecb2e 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DRequiresScenePositionAttribute.cs @@ -12,6 +12,4 @@ namespace ComputeSharp.D2D1; /// For more info, see . /// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] -public sealed class D2DRequiresScenePositionAttribute : Attribute -{ -} \ No newline at end of file +public sealed class D2DRequiresScenePositionAttribute : Attribute; \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/DiagnosticDescriptors.cs index fc3653a2..c15a6ad1 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/DiagnosticDescriptors.cs @@ -5,6 +5,4 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A container for all instances for errors reported by analyzers in this project. /// -internal static partial class DiagnosticDescriptors -{ -} \ No newline at end of file +internal static partial class DiagnosticDescriptors; \ No newline at end of file diff --git a/src/ComputeSharp/Attributes/GeneratedComputeShaderDescriptorAttribute.cs b/src/ComputeSharp/Attributes/GeneratedComputeShaderDescriptorAttribute.cs index f92c1fe2..0424254d 100644 --- a/src/ComputeSharp/Attributes/GeneratedComputeShaderDescriptorAttribute.cs +++ b/src/ComputeSharp/Attributes/GeneratedComputeShaderDescriptorAttribute.cs @@ -22,6 +22,4 @@ namespace ComputeSharp; /// /// [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] -public sealed class GeneratedComputeShaderDescriptorAttribute : Attribute -{ -} \ No newline at end of file +public sealed class GeneratedComputeShaderDescriptorAttribute : Attribute; \ No newline at end of file diff --git a/src/ComputeSharp/Attributes/GloballyCoherentAttribute.cs b/src/ComputeSharp/Attributes/GloballyCoherentAttribute.cs index a04f23d8..a2b6e550 100644 --- a/src/ComputeSharp/Attributes/GloballyCoherentAttribute.cs +++ b/src/ComputeSharp/Attributes/GloballyCoherentAttribute.cs @@ -21,6 +21,4 @@ namespace ComputeSharp; /// /// [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] -public sealed class GloballyCoherentAttribute : Attribute -{ -} \ No newline at end of file +public sealed class GloballyCoherentAttribute : Attribute; \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs index c5744a83..cc4e3c5f 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs @@ -196,4 +196,154 @@ public class Test_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task ShaderWithNoPrecompiledBytecode_WithNoD2DEnableRuntimeCompilation_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct {|CMPSD2D0076:MyType|} : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithNoPrecompiledBytecode_WithD2DEnableRuntimeCompilation_OnType_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + [D2DEnableRuntimeCompilation] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithNoPrecompiledBytecode_WithD2DEnableRuntimeCompilation_OnAssembly_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [assembly: D2DEnableRuntimeCompilation] + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithNoPrecompiledBytecode_WithUnnecessaryD2DEnableRuntimeCompilation_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [assembly: D2DEnableRuntimeCompilation] + + [D2DInputCount(0)] + [{|CMPSD2D0077:D2DEnableRuntimeCompilation|}] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithPrecompiledBytecode_FromAssembly_WithUnnecessaryD2DEnableRuntimeCompilation_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + + [D2DInputCount(0)] + [{|CMPSD2D0078:D2DEnableRuntimeCompilation|}] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task ShaderWithPrecompiledBytecode_FromType_WithUnnecessaryD2DEnableRuntimeCompilation_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [{|CMPSD2D0078:D2DEnableRuntimeCompilation|}] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct MyType : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task UnnecessaryD2DEnableRuntimeCompilation_OnAssembly_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + + [assembly: D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [assembly: {|CMPSD2D0079:D2DEnableRuntimeCompilation|}] + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index 6d4a81c8..d9d60411 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -30,6 +30,8 @@ public class Test_D2DPixelShaderSourceGenerator using float3 = global::ComputeSharp.Float3; using float4 = global::ComputeSharp.Float4; + [assembly: D2DEnableRuntimeCompilation] + namespace MyNamespace; [D2DInputCount(0)] @@ -679,6 +681,8 @@ public class Test_D2DPixelShaderSourceGenerator using float3 = global::ComputeSharp.Float3; using float4 = global::ComputeSharp.Float4; + [assembly: D2DEnableRuntimeCompilation] + namespace MyNamespace; [D2DEffectId("B30E1384-DEE1-4200-9312-321521DF3B00")] @@ -985,6 +989,8 @@ public class Test_D2DPixelShaderSourceGenerator using float3 = global::ComputeSharp.Float3; using float4 = global::ComputeSharp.Float4; + [assembly: D2DEnableRuntimeCompilation] + namespace MyNamespace; [D2DInputCount(0)] @@ -1280,6 +1286,8 @@ public class Test_D2DPixelShaderSourceGenerator using ComputeSharp.D2D1; using float4 = global::ComputeSharp.Float4; + [assembly: D2DEnableRuntimeCompilation] + namespace MyNamespace; [D2DInputCount(2)] @@ -1566,6 +1574,8 @@ public class Test_D2DPixelShaderSourceGenerator using int2 = global::ComputeSharp.Int2; using float4 = global::ComputeSharp.Float4; + [assembly: D2DEnableRuntimeCompilation] + namespace MyNamespace; [D2DInputCount(0)] @@ -1802,6 +1812,8 @@ public class Test_D2DPixelShaderSourceGenerator private static async Task VerifyGeneratedDiagnosticsAsync(string source, (string Filename, string Source) result) { // Also validate all analyzers + 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/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 1a426ebe..f28031b7 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; @@ -7,6 +8,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +[assembly: D2DEnableRuntimeCompilation] + #pragma warning disable IDE0022, IDE0044 namespace ComputeSharp.D2D1.Tests; diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs index be42c656..830dfc69 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.WinUI.Tests.Helpers; using Microsoft.Graphics.Canvas; @@ -8,6 +9,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +[assembly: D2DEnableRuntimeCompilation] + namespace ComputeSharp.D2D1.WinUI.Tests; [TestClass] diff --git a/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs index 7194b7b1..4da97a93 100644 --- a/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs @@ -6,6 +6,4 @@ namespace ComputeSharp.Tests.Attributes; /// An attribute to use with targeting all supported devices. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] -public sealed class AllDevicesAttribute : Attribute -{ -} \ No newline at end of file +public sealed class AllDevicesAttribute : Attribute; \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 5adf186b..8c507492 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -838,9 +838,7 @@ namespace ComputeSharp.Tests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - internal readonly partial struct ShaderWithPartialDeclarations : IComputeShader - { - } + internal readonly partial struct ShaderWithPartialDeclarations : IComputeShader; partial struct ShaderWithPartialDeclarations {