Repurpose linking analyzer to trivial sampling

このコミットが含まれているのは:
Sergio Pedri committed 2024-06-07 17:21:54 -07:00
1 親 86077ea28c
コミット 8fbd76526c
5ファイル変更
+82 -134

検索条件に一致するファイルはありません

@@ -7,13 +7,13 @@ using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// 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.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer : DiagnosticAnalyzer
public sealed class InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [InvalidD2D1CompileOptionsEnableLinkingOnShaderType];
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [InvalidD2D1PixelOptionsTrivialSamplingOnShaderType];
/// <inheritdoc/>
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);
+6 -6
ファイルの表示
@@ -1024,19 +1024,19 @@ partial class DiagnosticDescriptors
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid use of <c>[D2DCompileOptions]</c> requesting to enable linking.
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid use of <c>[D2DPixelOptions]</c> indicating trivial sampling.
/// <para>
/// Format: <c>"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)"</c>.
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
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");
/// <summary>
@@ -1784,7 +1784,7 @@ public class Test_D2DPixelShaderDescriptorGenerator
await CSharpAnalyzerWithLanguageVersionTest<D2DEnableRuntimeCompilationOnTypeAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<ExceededPixelShaderDispatchDataSizeAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidAssemblyLevelCompileOptionsAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2DInputDescriptionAttributeUseAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2DRequiresDoublePrecisionSupportAnalyzer>.VerifyAnalyzerAsync(source);
@@ -1117,4 +1117,69 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers
await CSharpAnalyzerWithLanguageVersionTest<InvalidShaderTypeInputsAnalyzer>.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<InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer>.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<InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer>.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<InvalidD2D1PixelOptionsTrivialSamplingOnShaderTypeAnalyzer>.VerifyAnalyzerAsync(source);
}
}
@@ -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<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.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<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.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<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.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<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.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<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.VerifyAnalyzerAsync(source);
}
}