diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 0dfa9366..df77bc64 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -76,3 +76,4 @@ CMPSD2D0066 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0067 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0068 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0069 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0070 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs new file mode 100644 index 00000000..b860616a --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyPixelShaderTypeWithFieldsAnalyzer.cs @@ -0,0 +1,20 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotReadOnlyPixelShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotReadOnlyPixelShaderTypeWithFieldsAnalyzer() + : base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.D2D1.ID2D1PixelShader")) + { + } +} \ 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 b18c63ae..1cf84379 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1038,4 +1038,20 @@ partial class DiagnosticDescriptors 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).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a shader type with any fields is not readonly. + /// + /// Format: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)". + /// + /// + public static readonly DiagnosticDescriptor NotReadOnlyShaderType = new( + id: "CMPSD2D0070", + title: "Not readonly shader type (using ID2D1PixelShader)", + messageFormat: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shader types should be readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 0f3f97f3..ed307b28 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -10,6 +10,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs index 1b719501..8f620e8b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs @@ -8,26 +8,11 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if the AllowUnsafeBlocks compilation option is not set. /// -public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - private protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) - { - this.diagnosticDescriptor = diagnosticDescriptor; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -40,7 +25,7 @@ public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : Di // Check whether unsafe blocks are available, and emit an error if they are not if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) { - context.ReportDiagnostic(Diagnostic.Create(this.diagnosticDescriptor, location: null)); + context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, location: null)); } }); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs index b0165b0f..cd91417a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if a field in a target shader type is of a type that is not accessible from its containing assembly. /// -public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +/// The fully qualified type name of the target attribute. +public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// The fully qualified type name of the target attribute. - /// - private readonly string generatedShaderDescriptorFullyQualifiedTypeName; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - /// The fully qualified type name of the target attribute. - private protected NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( - DiagnosticDescriptor diagnosticDescriptor, - string generatedShaderDescriptorFullyQualifiedTypeName) - { - this.diagnosticDescriptor = diagnosticDescriptor; - this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -47,7 +26,7 @@ public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttribute context.RegisterCompilationStartAction(context => { // Get the symbol for the target attribute type - if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) { return; } @@ -78,7 +57,7 @@ public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttribute if (!fieldTypeSymbol.IsAccessibleFromCompilationAssembly(context.Compilation)) { context.ReportDiagnostic(Diagnostic.Create( - this.diagnosticDescriptor, + diagnosticDescriptor, attribute.GetLocation(), typeSymbol, memberSymbol.Name, diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs index 9e050c6c..8c5ed57c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if a target shader type is not accessible from its containing assembly. /// -public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +/// The fully qualified type name of the target attribute. +public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// The fully qualified type name of the target attribute. - /// - private readonly string generatedShaderDescriptorFullyQualifiedTypeName; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - /// The fully qualified type name of the target attribute. - private protected NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( - DiagnosticDescriptor diagnosticDescriptor, - string generatedShaderDescriptorFullyQualifiedTypeName) - { - this.diagnosticDescriptor = diagnosticDescriptor; - this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -47,7 +26,7 @@ public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnaly context.RegisterCompilationStartAction(context => { // Get the symbol for the target attribute type - if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) { return; } @@ -65,7 +44,7 @@ public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnaly !typeSymbol.IsAccessibleFromContainingAssembly(context.Compilation)) { context.ReportDiagnostic(Diagnostic.Create( - this.diagnosticDescriptor, + diagnosticDescriptor, attribute.GetLocation(), typeSymbol)); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs new file mode 100644 index 00000000..1b70e96b --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs @@ -0,0 +1,82 @@ +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ComputeSharp.SourceGeneration.Diagnostics; + +/// +/// A diagnostic analyzer that generates a warning if a shader type is not readonly. +/// +/// The instance to use. +/// The fully qualified metadata names of all candidate shader interface types. +public abstract class NotReadOnlyShaderTypeWithFieldsAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + ImmutableArray shaderInterfaceTypeFullyQualifiedMetadataNames) : DiagnosticAnalyzer +{ + /// + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); + + /// + public sealed override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(context => + { + // Get the candidate type symbols + if (!context.Compilation.TryBuildNamedTypeSymbolSet(shaderInterfaceTypeFullyQualifiedMetadataNames, out ImmutableHashSet? typeSymbols)) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types that are not readonly are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct, IsReadOnly: false } typeSymbol) + { + return; + } + + // In order to trigger the analyzer, we must have at least one field. + // If not (eg. shaders that just return a constant color), no need. + if (!typeSymbol.GetMembers().Any(static m => m is IFieldSymbol { IsStatic: false, IsConst: false, IsFixedSizeBuffer: false })) + { + return; + } + + // Emit a diagnostic if the type is a shader type + if (IsShaderType(typeSymbol, typeSymbols)) + { + context.ReportDiagnostic(Diagnostic.Create( + diagnosticDescriptor, + typeSymbol.Locations.FirstOrDefault(), + typeSymbol)); + } + }, SymbolKind.NamedType); + }); + } + + /// + /// Checks whether a given type is a shader type. + /// + /// The type to check. + /// The list of candidate type symbols for shader interface types. + /// + private static bool IsShaderType(INamedTypeSymbol typeSymbol, ImmutableHashSet typeSymbols) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + // Look for either the interface itself, if not generic, or the unbounded type if it's generic + if ((!interfaceSymbol.IsGenericType && typeSymbols.Contains(interfaceSymbol)) || + (interfaceSymbol.IsGenericType && typeSymbols.Contains(interfaceSymbol.ConstructedFrom))) + { + return true; + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index d89faabf..2be10a4e 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -62,3 +62,4 @@ CMPS0053 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/S CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0055 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0057 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs new file mode 100644 index 00000000..39b7889b --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotReadOnlyComputeShaderTypeWithFieldsAnalyzer.cs @@ -0,0 +1,20 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotReadOnlyComputeShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotReadOnlyComputeShaderTypeWithFieldsAnalyzer() + : base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.IComputeShader", "ComputeSharp.IComputeShader`1")) + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 158b37e3..1219c4d1 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -798,4 +798,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a shader type with any fields is not readonly. + /// + /// Format: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)". + /// + /// + public static readonly DiagnosticDescriptor NotReadOnlyShaderType = new( + id: "CMPS0057", + title: "Not readonly shader type (using IComputeShader or IComputeShader)", + messageFormat: "The shader of type {0} is not readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone)", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true, + description: "Shader types should be readonly (shaders cannot mutate their instance state while running, so shader types not being readonly makes them error prone).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ 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 new file mode 100644 index 00000000..a3f01820 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs @@ -0,0 +1,54 @@ +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_Analyzers +{ + [TestMethod] + public async Task NotReadOnlyShaderType_WithFields_Warns() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + namespace MyFancyApp.Sample; + + internal partial struct {|CMPSD2D0070:MyShader|} : ID2D1PixelShader + { + public float number; + + public Float4 Execute() + { + return this.number; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task NotReadOnlyShaderType_WithNoFields_DoesNotWarn() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + namespace MyFancyApp.Sample; + + internal partial struct MyShader : ID2D1PixelShader + { + public Float4 Execute() + { + return default; + } + } + """; + + 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 87ca85c0..26f08a89 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -1812,6 +1812,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); // Get all assembly references for the .NET TFM and ComputeSharp IEnumerable metadataReferences = diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index f0d927bd..bdcfa835 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -122,9 +122,9 @@ public partial class D2D1PixelShaderEffectTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct NullConstantBufferShader : ID2D1PixelShader + internal readonly partial struct NullConstantBufferShader : ID2D1PixelShader { - private float dummy; + private readonly float dummy; public float4 Execute() { @@ -154,13 +154,13 @@ public partial class D2D1PixelShaderEffectTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ConstantBufferSizeTestShader : ID2D1PixelShader + internal readonly partial struct ConstantBufferSizeTestShader : ID2D1PixelShader { - private float a; - private float b; - private float3 c; - private int d; - private int e; + private readonly float a; + private readonly float b; + private readonly float3 c; + private readonly int d; + private readonly int e; public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index b2b61c73..54f7d40b 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -336,10 +336,10 @@ namespace ComputeSharp.D2D1.Tests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader + internal readonly partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - D2D1ResourceTexture1D myTexture; + readonly D2D1ResourceTexture1D myTexture; public Float4 Execute() { @@ -355,21 +355,21 @@ namespace ComputeSharp.D2D1.Tests [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithResourceTextures : ID2D1PixelShader + internal readonly partial struct ShaderWithResourceTextures : ID2D1PixelShader { - float number; + readonly float number; [D2DResourceTextureIndex(4)] - D2D1ResourceTexture1D myTexture1; + readonly D2D1ResourceTexture1D myTexture1; [D2DResourceTextureIndex(5)] - D2D1ResourceTexture2D myTexture2; + readonly D2D1ResourceTexture2D myTexture2; [D2DResourceTextureIndex(6)] - D2D1ResourceTexture3D myTexture3; + readonly D2D1ResourceTexture3D myTexture3; [D2DResourceTextureIndex(7)] - D2D1ResourceTexture2D myTexture4; + readonly D2D1ResourceTexture2D myTexture4; public Float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 44fdd57b..850933b3 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -171,10 +171,10 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - internal partial struct DummyShaderWithResourceTexture : ID2D1PixelShader + internal readonly partial struct DummyShaderWithResourceTexture : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture2D source; + private readonly D2D1ResourceTexture2D source; public float4 Execute() { @@ -460,10 +460,10 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - internal partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader + internal readonly partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture2D source; + private readonly D2D1ResourceTexture2D source; public float4 Execute() { @@ -536,12 +536,12 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader + internal readonly partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader { - private int height; + private readonly int height; [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture3D source; + private readonly D2D1ResourceTexture3D source; public float4 Execute() { @@ -624,10 +624,10 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader + internal readonly partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture1D source; + private readonly D2D1ResourceTexture1D source; public float4 Execute() { @@ -792,10 +792,10 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader + internal readonly partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture2D source; + private readonly D2D1ResourceTexture2D source; public float4 Execute() { @@ -896,12 +896,12 @@ public partial class D2D1ResourceTextureManagerTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader + internal readonly partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader { - private int height; + private readonly int height; [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture3D source; + private readonly D2D1ResourceTexture3D source; public float4 Execute() { @@ -939,10 +939,10 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] - internal partial struct NullResourceTextureShader : ID2D1PixelShader + internal readonly partial struct NullResourceTextureShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] - private D2D1ResourceTexture2D source; + private readonly D2D1ResourceTexture2D source; public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs index 364892eb..345bf960 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1TransformMapperTests.cs @@ -261,8 +261,7 @@ public partial class D2D1TransformMapperTests Assert.AreEqual(shader.Width, this.ExpectedWidth); Assert.AreEqual(shader.Height, this.ExpectedHeight); - shader.Width = inputs[0].Width; - shader.Height = inputs[0].Height; + shader = new ShaderWithDispatchArea(inputs[0].Width, inputs[0].Height); drawInfoUpdateContext.SetConstantBuffer(in shader); @@ -286,10 +285,10 @@ public partial class D2D1TransformMapperTests [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithDispatchArea : ID2D1PixelShader + internal readonly partial struct ShaderWithDispatchArea : ID2D1PixelShader { - public int Width; - public int Height; + public readonly int Width; + public readonly int Height; public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/CheckerboardClipEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/CheckerboardClipEffect.cs index 2d448d99..f3580c9d 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/CheckerboardClipEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/CheckerboardClipEffect.cs @@ -5,13 +5,8 @@ namespace ComputeSharp.D2D1.Tests.Effects; [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -public partial struct CheckerboardClipEffect : ID2D1PixelShader +public readonly partial struct CheckerboardClipEffect(int cellSize) : ID2D1PixelShader { - public int width; - public int height; - public int cellSize; - /// public float4 Execute() { @@ -19,8 +14,8 @@ public partial struct CheckerboardClipEffect : ID2D1PixelShader uint x = (uint)Hlsl.Floor(position.X); uint y = (uint)Hlsl.Floor(position.Y); - uint cellX = (uint)(int)Hlsl.Floor(x / this.cellSize); - uint cellY = (uint)(int)Hlsl.Floor(y / this.cellSize); + uint cellX = (uint)(int)Hlsl.Floor(x / cellSize); + uint cellY = (uint)(int)Hlsl.Floor(y / cellSize); if ((cellX % 2 == 0 && cellY % 2 == 0) || (cellX % 2 == 1 && cellY % 2 == 1)) diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs index cca2b3a4..635add2e 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithThresholdEffect.cs @@ -4,16 +4,13 @@ namespace ComputeSharp.D2D1.Tests.Effects; [D2DInputSimple(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -public partial struct InvertWithThresholdEffect : ID2D1PixelShader +public readonly partial struct InvertWithThresholdEffect(float number) : ID2D1PixelShader { - public float number; - /// public float4 Execute() { float4 color = D2D.GetInput(0); - float3 rgb = Hlsl.Saturate(this.number - color.RGB); + float3 rgb = Hlsl.Saturate(number - color.RGB); return new(rgb, 1); } diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs index 951a9073..3310aee3 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/ZonePlateEffect.cs @@ -6,23 +6,21 @@ namespace ComputeSharp.D2D1.Tests.Effects; [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -public readonly partial struct ZonePlateEffect : ID2D1PixelShader +public readonly partial struct ZonePlateEffect( + int width, + int height, + float diameter) : ID2D1PixelShader { - private readonly int width; - private readonly int height; - private readonly float diameter; - /// public float4 Execute() { float2 scenePos = D2D.GetScenePosition().XY; - float xo = scenePos.X - (this.width >> 1); - float yo = scenePos.Y - (this.height >> 1); + float xo = scenePos.X - (width >> 1); + float yo = scenePos.Y - (height >> 1); - float rm = 0.5f * this.diameter; - float km = 0.7f / this.diameter * MathF.PI; + float rm = 0.5f * diameter; + float km = 0.7f / diameter * MathF.PI; float w = rm / 10.0f; float yd = yo * yo; diff --git a/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs b/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs index 2063222d..6782a7f5 100644 --- a/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs @@ -52,7 +52,7 @@ public class EffectsTests [TestMethod] public void CheckerboardClip() { - D2D1TestRunner.RunAndCompareShader(new CheckerboardClipEffect(1280, 840, 32), null, "Landscape.png", "Landscape_CheckerboardClip.png"); + D2D1TestRunner.RunAndCompareShader(new CheckerboardClipEffect(32), null, "Landscape.png", "Landscape_CheckerboardClip.png"); } [TestMethod] diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs index 2c853e65..ec4e2425 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/CanvasEffectTests.cs @@ -701,9 +701,9 @@ public partial class CanvasEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithNoInputs : ID2D1PixelShader + internal readonly partial struct ShaderWithNoInputs : ID2D1PixelShader { - public int Value; + public readonly int Value; public float4 Execute() { diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs index 24674079..30a5a699 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs @@ -456,13 +456,13 @@ public partial class PixelShaderEffectTests [D2DInputCount(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithSomeProperties : ID2D1PixelShader + internal readonly partial struct ShaderWithSomeProperties : ID2D1PixelShader { - public float A; - public int B; - public int2 C; - public uint2x2 D; - public float2 E; + public readonly float A; + public readonly int B; + public readonly int2 C; + public readonly uint2x2 D; + public readonly float2 E; public float4 Execute() { @@ -473,9 +473,9 @@ public partial class PixelShaderEffectTests [D2DInputCount(1)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] - internal partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader + internal readonly partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader { - public int Number; + public readonly int Number; public float4 Execute() { diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index f28a526f..df813a78 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -14,9 +14,9 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct CapturedResourceShader : IComputeShader + public readonly partial struct CapturedResourceShader : IComputeShader { - public ReadWriteBuffer _; + public readonly ReadWriteBuffer _; public void Execute() { @@ -50,13 +50,13 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct MultipleResourcesAndPrimitivesShader : IComputeShader + public readonly partial struct MultipleResourcesAndPrimitivesShader : IComputeShader { - public ReadWriteBuffer _0; - public ReadWriteBuffer _1; - public int a; - public int b; - public int c; + public readonly ReadWriteBuffer _0; + public readonly ReadWriteBuffer _1; + public readonly int a; + public readonly int b; + public readonly int c; public void Execute() { @@ -96,15 +96,15 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct ScalarAndVectorTypesShader : IComputeShader + public readonly partial struct ScalarAndVectorTypesShader : IComputeShader { - public ReadWriteBuffer _; - public float3 f3; - public int a; - public int b; - public double2 d2; - public int c; - public int d; + public readonly ReadWriteBuffer _; + public readonly float3 f3; + public readonly int a; + public readonly int b; + public readonly double2 d2; + public readonly int c; + public readonly int d; public void Execute() { @@ -147,17 +147,17 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct ScalarVectorAndMatrixTypesShader : IComputeShader + public readonly partial struct ScalarVectorAndMatrixTypesShader : IComputeShader { - public ReadWriteBuffer _; - public float2x3 f2x3; - public int a; - public Int1x3 i1x3; - public double2 d2; - public int c; - public Int1x2 i1x2; - public int2x2 i2x2; - public int d; + public readonly ReadWriteBuffer _; + public readonly float2x3 f2x3; + public readonly int a; + public readonly Int1x3 i1x3; + public readonly double2 d2; + public readonly int c; + public readonly Int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; public void Execute() { @@ -233,10 +233,10 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct FlatCustomTypeShader : IComputeShader + public readonly partial struct FlatCustomTypeShader : IComputeShader { - public ReadWriteBuffer _0; - public SimpleTypes a; + public readonly ReadWriteBuffer _0; + public readonly SimpleTypes a; public void Execute() { @@ -325,10 +325,10 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct NestedCustomTypesShader : IComputeShader + public readonly partial struct NestedCustomTypesShader : IComputeShader { - public ReadWriteBuffer _0; - public CustomType1 a; + public readonly ReadWriteBuffer _0; + public readonly CustomType1 a; public void Execute() { @@ -417,17 +417,17 @@ public partial class ShaderDataLoaderTests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - public partial struct AmbiguousNamesShader : IComputeShader + public readonly partial struct AmbiguousNamesShader : IComputeShader { - public ReadWriteBuffer a; - public ReadWriteBuffer b; - public ReadWriteBuffer c; - public ReadWriteBuffer x; - public ReadWriteBuffer y; - public ReadWriteBuffer z; - public int dataLoader; - public int device; - public int dummy; + public readonly ReadWriteBuffer a; + public readonly ReadWriteBuffer b; + public readonly ReadWriteBuffer c; + public readonly ReadWriteBuffer x; + public readonly ReadWriteBuffer y; + public readonly ReadWriteBuffer z; + public readonly int dataLoader; + public readonly int device; + public readonly int dummy; public void Execute() { diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs similarity index 76% rename from tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs rename to tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs index ec84415e..113cd657 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs @@ -7,7 +7,7 @@ namespace ComputeSharp.Tests.SourceGenerators; [TestClass] [TestCategory("Analyzers")] -public class AnalyzerTests +public class Test_Analyzers { [TestMethod] public async Task NotAccessibleNestedShaderType() @@ -64,4 +64,25 @@ public class AnalyzerTests await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task NotReadOnlyShaderType() + { + const string source = """ + using ComputeSharp; + + namespace MyFancyApp.Sample; + + internal partial struct {|CMPS0057:MyShader|} : IComputeShader + { + public ReadWriteBuffer buffer; + + public void Execute() + { + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index d1df6c74..86ed4d84 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -899,6 +899,7 @@ public class Test_D2DPixelShaderSourceGenerator await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); // Get all assembly references for the .NET TFM and ComputeSharp IEnumerable metadataReferences =