diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs index 2f125cf0..0014a534 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/MissingPixelShaderDescriptorOnPixelShaderAnalyzer.cs @@ -47,7 +47,7 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderAnalyzer : Diagnost } // Emit a diagnostic if the descriptor is missing for the shader type - if (!typeSymbol.HasInterfaceWithType(d2D1PixelShaderDescriptorSymbol) && + if (!HasD2D1PixelShaderDescriptorInterface(typeSymbol, d2D1PixelShaderDescriptorSymbol) && !typeSymbol.HasAttributeWithType(d2DGeneratedPixelShaderDescriptorAttributeSymbol)) { context.ReportDiagnostic(Diagnostic.Create( @@ -58,4 +58,23 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderAnalyzer : Diagnost }, SymbolKind.NamedType); }); } + + /// + /// Checks whether a given type implements the shader descriptor interface. + /// + /// The type to check. + /// The type symbol for ID2D1PixelShaderDescriptor<T>. + /// + private static bool HasD2D1PixelShaderDescriptorInterface(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderDescriptorSymbol) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, d2D1PixelShaderDescriptorSymbol)) + { + return true; + } + } + + return false; + } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs index 61db689f..632c4ea2 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/MissingComputeShaderDescriptorOnComputeShaderAnalyzer.cs @@ -48,7 +48,7 @@ public sealed class MissingComputeShaderDescriptorOnComputeShaderAnalyzer : Diag } // Emit a diagnostic if the descriptor is missing for the shader type - if (!typeSymbol.HasInterfaceWithType(computeShaderDescriptorSymbol) && + if (!HasComputeShaderDescriptorInterface(typeSymbol, computeShaderDescriptorSymbol) && !typeSymbol.HasAttributeWithType(generatedComputeShaderDescriptorAttributeSymbol)) { context.ReportDiagnostic(Diagnostic.Create( @@ -83,4 +83,23 @@ public sealed class MissingComputeShaderDescriptorOnComputeShaderAnalyzer : Diag return false; } + + /// + /// Checks whether a given type implements the shader descriptor interface. + /// + /// The type to check. + /// The type symbol for IComputeShaderDescriptor<T>. + /// + private static bool HasComputeShaderDescriptorInterface(INamedTypeSymbol typeSymbol, INamedTypeSymbol computeShaderDescriptorSymbol) + { + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) + { + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, computeShaderDescriptorSymbol)) + { + return true; + } + } + + return false; + } } \ 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 a3f01820..34c551c4 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_Analyzers.cs @@ -8,6 +8,93 @@ namespace ComputeSharp.D2D1.Tests.SourceGenerators; [TestClass] public class Test_Analyzers { + [TestMethod] + public async Task MissingComputeShaderDescriptor_ComputeShader() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + + internal partial struct {|CMPSD2D0065:MyShader|} : ID2D1PixelShader + { + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingComputeShaderDescriptor_ManuallyImplemented_DoesNotWarn() + { + const string source = """ + using System; + using ComputeSharp; + using ComputeSharp.D2D1; + using ComputeSharp.D2D1.Descriptors; + using ComputeSharp.D2D1.Interop; + + internal partial struct MyShader : ID2D1PixelShader, ID2D1PixelShaderDescriptor + { + public static ref readonly Guid EffectId => throw new NotImplementedException(); + + public static string? EffectDisplayName => throw new NotImplementedException(); + + public static string? EffectDescription => throw new NotImplementedException(); + + public static string? EffectCategory => throw new NotImplementedException(); + + public static string? EffectAuthor => throw new NotImplementedException(); + + public static int ConstantBufferSize => throw new NotImplementedException(); + + public static int InputCount => throw new NotImplementedException(); + + public static int ResourceTextureCount => throw new NotImplementedException(); + + public static ReadOnlyMemory InputTypes => throw new NotImplementedException(); + + public static ReadOnlyMemory InputDescriptions => throw new NotImplementedException(); + + public static ReadOnlyMemory ResourceTextureDescriptions => throw new NotImplementedException(); + + public static D2D1PixelOptions PixelOptions => throw new NotImplementedException(); + + public static D2D1BufferPrecision BufferPrecision => throw new NotImplementedException(); + + public static D2D1ChannelDepth ChannelDepth => throw new NotImplementedException(); + + public static D2D1ShaderProfile ShaderProfile => throw new NotImplementedException(); + + public static D2D1CompileOptions CompileOptions => throw new NotImplementedException(); + + public static string HlslSource => throw new NotImplementedException(); + + public static ReadOnlyMemory HlslBytecode => throw new NotImplementedException(); + + public static MyShader CreateFromConstantBuffer(ReadOnlySpan buffer) + { + throw new NotImplementedException(); + } + + public static void LoadConstantBuffer(in MyShader shader, ref TLoader loader) where TLoader : struct, ID2D1ConstantBufferLoader + { + throw new NotImplementedException(); + } + + public Float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + [TestMethod] public async Task NotReadOnlyShaderType_WithFields_Warns() { diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs index c5c42200..88fe2236 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_Analyzers.cs @@ -9,6 +9,90 @@ namespace ComputeSharp.Tests.SourceGenerators; [TestCategory("Analyzers")] public class Test_Analyzers { + [TestMethod] + public async Task MissingComputeShaderDescriptor_ComputeShader() + { + const string source = """ + using ComputeSharp; + + internal partial struct {|CMPS0053:MyShader|} : IComputeShader + { + public ReadWriteBuffer buffer; + + public void Execute() + { + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingComputeShaderDescriptor_ComputeShaderOfT() + { + const string source = """ + using ComputeSharp; + using float4 = ComputeSharp.Float4; + + internal partial struct {|CMPS0053:MyShader|} : IComputeShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task MissingComputeShaderDescriptor_ManuallyImplemented_DoesNotWarn() + { + const string source = """ + using System; + using ComputeSharp; + using ComputeSharp.Descriptors; + using ComputeSharp.Interop; + + internal partial struct MyShader : IComputeShader, IComputeShaderDescriptor + { + public static int ThreadsX => throw new NotImplementedException(); + + public static int ThreadsY => throw new NotImplementedException(); + + public static int ThreadsZ => throw new NotImplementedException(); + + public static int ConstantBufferSize => throw new NotImplementedException(); + + public static bool IsStaticSamplerRequired => throw new NotImplementedException(); + + public static ReadOnlyMemory ResourceDescriptorRanges => throw new NotImplementedException(); + + public static string HlslSource => throw new NotImplementedException(); + + public static ReadOnlyMemory HlslBytecode => throw new NotImplementedException(); + + public static void LoadConstantBuffer(in MyShader shader, ref TLoader loader, int x, int y, int z) where TLoader : struct, IConstantBufferLoader + { + throw new NotImplementedException(); + } + + public static void LoadGraphicsResources(in MyShader shader, ref TLoader loader) where TLoader : struct, IGraphicsResourceLoader + { + throw new NotImplementedException(); + } + + public void Execute() + { + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + [TestMethod] public async Task NotAccessibleNestedShaderType() {