Merge pull request #672 from Sergio0694/dev/readonly-shader-analyzers

Add new analyzers to warn on not readonly shader types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-11-24 02:04:38 +01:00
コミット f6326c8249
26ファイル変更
+357 -191

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

+1
ファイルの表示
@@ -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)
@@ -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;
/// <inheritdoc/>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class NotReadOnlyPixelShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase
{
/// <summary>
/// Creates a new <see cref="NotReadOnlyPixelShaderTypeWithFieldsAnalyzer"/> instance.
/// </summary>
public NotReadOnlyPixelShaderTypeWithFieldsAnalyzer()
: base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.D2D1.ID2D1PixelShader"))
{
}
}
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when a shader type with any fields is not readonly.
/// <para>
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
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");
}
@@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\NotReadOnlyShaderTypeWithFieldsAnalyzerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\Analyzers\MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Diagnostics\DiagnosticDescriptors.cs" />
@@ -8,26 +8,11 @@ namespace ComputeSharp.SourceGeneration.Diagnostics;
/// <summary>
/// A diagnostic analyzer that generates an error if the <c>AllowUnsafeBlocks</c> compilation option is not set.
/// </summary>
public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : DiagnosticAnalyzer
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) : DiagnosticAnalyzer
{
/// <summary>
/// The <see cref="DiagnosticDescriptor"/> instance to use.
/// </summary>
private readonly DiagnosticDescriptor diagnosticDescriptor;
/// <summary>
/// Creates a new <see cref="MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase"/> instance with the specified arguments.
/// </summary>
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
private protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor)
{
this.diagnosticDescriptor = diagnosticDescriptor;
SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor);
}
/// <inheritdoc/>
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor);
/// <inheritdoc/>
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));
}
});
}
@@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics;
/// <summary>
/// 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.
/// </summary>
public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
/// <param name="generatedShaderDescriptorFullyQualifiedTypeName">The fully qualified type name of the target attribute.</param>
public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer
{
/// <summary>
/// The <see cref="DiagnosticDescriptor"/> instance to use.
/// </summary>
private readonly DiagnosticDescriptor diagnosticDescriptor;
/// <summary>
/// The fully qualified type name of the target attribute.
/// </summary>
private readonly string generatedShaderDescriptorFullyQualifiedTypeName;
/// <summary>
/// Creates a new <see cref="NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase"/> instance with the specified arguments.
/// </summary>
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
/// <param name="generatedShaderDescriptorFullyQualifiedTypeName">The fully qualified type name of the target attribute.</param>
private protected NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
string generatedShaderDescriptorFullyQualifiedTypeName)
{
this.diagnosticDescriptor = diagnosticDescriptor;
this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName;
SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor);
}
/// <inheritdoc/>
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor);
/// <inheritdoc/>
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,
@@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics;
/// <summary>
/// A diagnostic analyzer that generates an error if a target shader type is not accessible from its containing assembly.
/// </summary>
public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
/// <param name="generatedShaderDescriptorFullyQualifiedTypeName">The fully qualified type name of the target attribute.</param>
public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer
{
/// <summary>
/// The <see cref="DiagnosticDescriptor"/> instance to use.
/// </summary>
private readonly DiagnosticDescriptor diagnosticDescriptor;
/// <summary>
/// The fully qualified type name of the target attribute.
/// </summary>
private readonly string generatedShaderDescriptorFullyQualifiedTypeName;
/// <summary>
/// Creates a new <see cref="NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase"/> instance with the specified arguments.
/// </summary>
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
/// <param name="generatedShaderDescriptorFullyQualifiedTypeName">The fully qualified type name of the target attribute.</param>
private protected NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
string generatedShaderDescriptorFullyQualifiedTypeName)
{
this.diagnosticDescriptor = diagnosticDescriptor;
this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName;
SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor);
}
/// <inheritdoc/>
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor);
/// <inheritdoc/>
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));
}
@@ -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;
/// <summary>
/// A diagnostic analyzer that generates a warning if a shader type is not readonly.
/// </summary>
/// <param name="diagnosticDescriptor">The <see cref="DiagnosticDescriptor"/> instance to use.</param>
/// <param name="shaderInterfaceTypeFullyQualifiedMetadataNames">The fully qualified metadata names of all candidate shader interface types.</param>
public abstract class NotReadOnlyShaderTypeWithFieldsAnalyzerBase(
DiagnosticDescriptor diagnosticDescriptor,
ImmutableArray<string> shaderInterfaceTypeFullyQualifiedMetadataNames) : DiagnosticAnalyzer
{
/// <inheritdoc/>
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor);
/// <inheritdoc/>
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<INamedTypeSymbol>? 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);
});
}
/// <summary>
/// Checks whether a given type is a shader type.
/// </summary>
/// <param name="typeSymbol">The type to check.</param>
/// <param name="typeSymbols">The list of candidate type symbols for shader interface types.</param>
/// <returns></returns>
private static bool IsShaderType(INamedTypeSymbol typeSymbol, ImmutableHashSet<INamedTypeSymbol> 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;
}
}
+1
ファイルの表示
@@ -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)
@@ -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;
/// <inheritdoc/>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class NotReadOnlyComputeShaderTypeWithFieldsAnalyzer : NotReadOnlyShaderTypeWithFieldsAnalyzerBase
{
/// <summary>
/// Creates a new <see cref="NotReadOnlyComputeShaderTypeWithFieldsAnalyzer"/> instance.
/// </summary>
public NotReadOnlyComputeShaderTypeWithFieldsAnalyzer()
: base(NotReadOnlyShaderType, ImmutableArray.Create("ComputeSharp.IComputeShader", "ComputeSharp.IComputeShader`1"))
{
}
}
+16
ファイルの表示
@@ -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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when a shader type with any fields is not readonly.
/// <para>
/// Format: <c>"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)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor NotReadOnlyShaderType = new(
id: "CMPS0057",
title: "Not readonly shader type (using IComputeShader or IComputeShader<T>)",
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");
}
+54
ファイルの表示
@@ -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<NotReadOnlyPixelShaderTypeWithFieldsAnalyzer>.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<NotReadOnlyPixelShaderTypeWithFieldsAnalyzer>.VerifyAnalyzerAsync(source);
}
}
@@ -1812,6 +1812,7 @@ public class Test_D2DPixelShaderSourceGenerator
await CSharpAnalyzerWithLanguageVersionTest<MissingPixelShaderDescriptorOnPixelShaderAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotReadOnlyPixelShaderTypeWithFieldsAnalyzer>.VerifyAnalyzerAsync(source);
// Get all assembly references for the .NET TFM and ComputeSharp
IEnumerable<MetadataReference> metadataReferences =
+8 -8
ファイルの表示
@@ -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()
{
+8 -8
ファイルの表示
@@ -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<float> myTexture;
readonly D2D1ResourceTexture1D<float> 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<float4> myTexture1;
readonly D2D1ResourceTexture1D<float4> myTexture1;
[D2DResourceTextureIndex(5)]
D2D1ResourceTexture2D<float4> myTexture2;
readonly D2D1ResourceTexture2D<float4> myTexture2;
[D2DResourceTextureIndex(6)]
D2D1ResourceTexture3D<float4> myTexture3;
readonly D2D1ResourceTexture3D<float4> myTexture3;
[D2DResourceTextureIndex(7)]
D2D1ResourceTexture2D<float> myTexture4;
readonly D2D1ResourceTexture2D<float> myTexture4;
public Float4 Execute()
{
+16 -16
ファイルの表示
@@ -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<float4> source;
private readonly D2D1ResourceTexture2D<float4> 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<float4> source;
private readonly D2D1ResourceTexture2D<float4> 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<float4> source;
private readonly D2D1ResourceTexture3D<float4> 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<float> source;
private readonly D2D1ResourceTexture1D<float> 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<float> source;
private readonly D2D1ResourceTexture2D<float> 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<float> source;
private readonly D2D1ResourceTexture3D<float> 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<float4> source;
private readonly D2D1ResourceTexture2D<float4> source;
public float4 Execute()
{
+4 -5
ファイルの表示
@@ -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()
{
+3 -8
ファイルの表示
@@ -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;
/// <inheritdoc/>
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))
+2 -5
ファイルの表示
@@ -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;
/// <inheritdoc/>
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);
}
+8 -10
ファイルの表示
@@ -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;
/// <inheritdoc/>
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;
+1 -1
ファイルの表示
@@ -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]
+2 -2
ファイルの表示
@@ -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()
{
+8 -8
ファイルの表示
@@ -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()
{
+42 -42
ファイルの表示
@@ -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<float> _;
public readonly ReadWriteBuffer<float> _;
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<float> _0;
public ReadWriteBuffer<float> _1;
public int a;
public int b;
public int c;
public readonly ReadWriteBuffer<float> _0;
public readonly ReadWriteBuffer<float> _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<float> _;
public float3 f3;
public int a;
public int b;
public double2 d2;
public int c;
public int d;
public readonly ReadWriteBuffer<float> _;
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<float> _;
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<float> _;
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<float> _0;
public SimpleTypes a;
public readonly ReadWriteBuffer<float> _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<float> _0;
public CustomType1 a;
public readonly ReadWriteBuffer<float> _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<float> a;
public ReadWriteBuffer<float> b;
public ReadWriteBuffer<float> c;
public ReadWriteBuffer<float> x;
public ReadWriteBuffer<float> y;
public ReadWriteBuffer<float> z;
public int dataLoader;
public int device;
public int dummy;
public readonly ReadWriteBuffer<float> a;
public readonly ReadWriteBuffer<float> b;
public readonly ReadWriteBuffer<float> c;
public readonly ReadWriteBuffer<float> x;
public readonly ReadWriteBuffer<float> y;
public readonly ReadWriteBuffer<float> z;
public readonly int dataLoader;
public readonly int device;
public readonly int dummy;
public void Execute()
{
@@ -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<NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
}
[TestMethod]
public async Task NotReadOnlyShaderType()
{
const string source = """
using ComputeSharp;
namespace MyFancyApp.Sample;
internal partial struct {|CMPS0057:MyShader|} : IComputeShader
{
public ReadWriteBuffer<float> buffer;
public void Execute()
{
}
}
""";
await CSharpAnalyzerWithLanguageVersionTest<NotReadOnlyComputeShaderTypeWithFieldsAnalyzer>.VerifyAnalyzerAsync(source);
}
}
+1
ファイルの表示
@@ -899,6 +899,7 @@ public class Test_D2DPixelShaderSourceGenerator
await CSharpAnalyzerWithLanguageVersionTest<MissingComputeShaderDescriptorOnComputeShaderAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotReadOnlyComputeShaderTypeWithFieldsAnalyzer>.VerifyAnalyzerAsync(source);
// Get all assembly references for the .NET TFM and ComputeSharp
IEnumerable<MetadataReference> metadataReferences =