Merge pull request #332 from Sergio0694/dev/d2drequiressceneposition-diagnostics

Add diagnostic for missing [D2DRequiresPosition] attribute
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-08-07 18:41:56 +02:00
コミット 049e18e92d
7ファイル変更
+92 -9

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

+1
ファイルの表示
@@ -51,3 +51,4 @@ CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0042 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0043 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0044 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0045 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+16
ファイルの表示
@@ -634,4 +634,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "A D2D1 shader generated with ComputeSharp.D2D1 (or an assembly) cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DRequiresPosition]</c> attribute is missing.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using D2D1 APIs that require the [D2DRequiresPosition] attribute to be used (that is, D2D1.GetScenePosition() and D2D1.SampleInputAtPosition(int, float2)), but the shader type is not annotated accordingly"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor MissingD2DRequiresPositionAttribute = new DiagnosticDescriptor(
id: "CMPSD2D0045",
title: "Missing [D2DRequiresPosition] attribute",
messageFormat: "The D2D1 shader of type {0} is using D2D1 APIs that require the [D2DRequiresPosition] attribute to be used (that is, D2D.GetScenePosition() and D2D.SampleInputAtPosition(int, float2)), but the shader type is not annotated accordingly",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader using functionality that needs position info (ie. when [D2DRequiresPosition] is used, which is mandatory when calling either D2D.GetScenePosition() or D2D.SampleInputAtPosition(int, float2)) needs to be annotated accordingly.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
@@ -56,15 +56,18 @@ partial class ID2D1ShaderGenerator
// Explore the syntax tree and extract the processed info
var semanticModelProvider = new SemanticModelProvider(compilation);
var valueFields = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, out bool methodsNeedD2D1RequiresPosition);
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresPosition);
// Process the discovered types and constants
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
// Check whether the scene position is required
bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
bool requiresScenePosition = GetD2DRequiresScenePositionInfo(structDeclarationSymbol);
// Emit diagnostics for incorrect scene position uses
ReportInvalidD2DRequiresPositionUse(diagnostics, structDeclarationSymbol, requiresScenePosition, methodsNeedD2D1RequiresPosition || fieldsNeedD2D1RequiresPosition);
// Get the HLSL source
return GetHlslSource(
@@ -143,6 +146,7 @@ partial class ID2D1ShaderGenerator
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="needsD2D1RequiresPosition">Whether or not the shader needs the <c>[D2DRequiresPosition]</c> annotation.</param>
/// <returns>A sequence of static constant fields in <paramref name="structDeclarationSymbol"/>.</returns>
private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields(
ImmutableArray<Diagnostic>.Builder diagnostics,
@@ -150,10 +154,13 @@ partial class ID2D1ShaderGenerator
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions)
IDictionary<IFieldSymbol, string> constantDefinitions,
out bool needsD2D1RequiresPosition)
{
ImmutableArray<(string, string, string?)>.Builder builder = ImmutableArray.CreateBuilder<(string, string, string?)>();
needsD2D1RequiresPosition = false;
foreach (var fieldDeclaration in structDeclaration.Members.OfType<FieldDeclarationSyntax>())
{
foreach (var variableDeclarator in fieldDeclaration.Declaration.Variables)
@@ -190,6 +197,8 @@ partial class ID2D1ShaderGenerator
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
needsD2D1RequiresPosition |= staticFieldRewriter.NeedsD2DRequiresPositionAttribute;
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
}
}
@@ -207,6 +216,7 @@ partial class ID2D1ShaderGenerator
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="needsD2D1RequiresPosition">Whether or not the shader needs the <c>[D2DRequiresPosition]</c> annotation.</param>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods(
ImmutableArray<Diagnostic>.Builder diagnostics,
@@ -215,7 +225,8 @@ partial class ID2D1ShaderGenerator
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
IDictionary<IFieldSymbol, string> constantDefinitions)
IDictionary<IFieldSymbol, string> constantDefinitions,
out bool needsD2D1RequiresPosition)
{
// Find all declared methods in the type
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = (
@@ -226,6 +237,8 @@ partial class ID2D1ShaderGenerator
string? entryPoint = null;
ImmutableArray<(string, string)>.Builder methods = ImmutableArray.CreateBuilder<(string, string)>();
needsD2D1RequiresPosition = false;
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!;
@@ -253,6 +266,9 @@ partial class ID2D1ShaderGenerator
// Rewrite the method syntax tree
MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
// Update the position requirement
needsD2D1RequiresPosition |= shaderSourceRewriter.NeedsD2DRequiresPositionAttribute;
// Emit the extracted local functions first
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
@@ -395,12 +411,31 @@ partial class ID2D1ShaderGenerator
}
}
/// <summary>
/// Reports diagnostics for invalid uses of <c>[D2DRequiresPosition]</c> in a shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="requiresScenePosition">Whether the shader type is declaring the need for scene position.</param>
/// <param name="usesPositionDependentMethods">Whether the shader is using APIs that rely on scene position.</param>
private static void ReportInvalidD2DRequiresPositionUse(
ImmutableArray<Diagnostic>.Builder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
bool requiresScenePosition,
bool usesPositionDependentMethods)
{
if (!requiresScenePosition && usesPositionDependentMethods)
{
diagnostics.Add(MissingD2DRequiresPositionAttribute, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>
/// Gets whether or not a given shader requires the scene position.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <remarks>Whether <paramref name="structDeclarationSymbol"/> requires the scene position.</remarks>
private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol)
private static bool GetD2DRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol)
{
return structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", out _);
}
+11
ファイルの表示
@@ -10,6 +10,17 @@ namespace ComputeSharp.SourceGeneration.Mappings;
/// <inheritdoc/>
partial class HlslKnownMethods
{
/// <summary>
/// Checks whether or not a method name (previous matched with <see cref="TryGetMappedName(string, out string?)"/>
/// needs the <c>[D2DRequiresPosition]</c> annotation on its containing shader in order to be used.
/// </summary>
/// <param name="name">The fully qualified metadata name.</param>
/// <returns>Whether the method needs the <c>[D2DRequiresPosition]</c> annotation.</returns>
public static bool NeedsD2DRequiresPositionAttribute(string name)
{
return name is "ComputeSharp.D2D1.D2D.GetScenePosition" or "ComputeSharp.D2D1.D2D.SampleInputAtPosition";
}
/// <inheritdoc/>
static partial void AddKnownMethods(IDictionary<string, string> knownMethods)
+5
ファイルの表示
@@ -65,6 +65,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
Diagnostics = diagnostics;
}
/// <summary>
/// Gets whether or not the shader needs the <c>[D2DRequiresPosition]</c> attribute.
/// </summary>
public bool NeedsD2DRequiresPositionAttribute { get; protected set; }
/// <inheritdoc/>
public override SyntaxNode VisitCastExpression(CastExpressionSyntax node)
{
+10 -2
ファイルの表示
@@ -77,7 +77,7 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
public ShaderSourceRewriter(
private ShaderSourceRewriter(
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
@@ -278,11 +278,19 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter
operation.TargetMethod is IMethodSymbol method &&
method.IsStatic)
{
string metadataName = method.GetFullMetadataName();
// If the invocation consists of invoking a static method that has a direct
// mapping to HLSL, rewrite the expression in the current invocation node.
// For instance: Math.Abs(expr) => abs(expr).
if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping))
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping))
{
// Track whether the method needs [D2DRequiresPosition]
if (HlslKnownMethods.NeedsD2DRequiresPositionAttribute(metadataName))
{
NeedsD2DRequiresPositionAttribute = true;
}
return updatedNode.WithExpression(ParseExpression(mapping!));
}
+8 -1
ファイルの表示
@@ -94,9 +94,16 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter
operation.TargetMethod is IMethodSymbol method &&
method.IsStatic)
{
string metadataName = method.GetFullMetadataName();
// Rewrite HLSL intrinsic methods
if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping))
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping))
{
if (HlslKnownMethods.NeedsD2DRequiresPositionAttribute(metadataName))
{
NeedsD2DRequiresPositionAttribute = true;
}
return updatedNode.WithExpression(ParseExpression(mapping!));
}
}