From 5285a02db95a0e12ebaa42069db885bb3cc618d4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Nov 2024 19:40:14 -0800 Subject: [PATCH 1/9] Fix namespace for '[HlslIntrinsicName]' type --- .../Intrinsics/Attributes/HlslIntrinsicNameAttribute.cs | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.g.cs | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.tt | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.cs | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.g.cs | 2 +- src/ComputeSharp.Core/Intrinsics/Hlsl.tt | 2 +- src/ComputeSharp.Core/Primitives/Float/Float2.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/Float3.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/Float4.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/FloatMxN.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int2.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int3.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int4.g.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/IntMxN.g.cs | 2 +- src/ComputeSharp.Core/Primitives/MatrixType.ttinclude | 2 +- src/ComputeSharp.Core/Primitives/VectorType.ttinclude | 2 +- .../Mappings/HlslKnownKeywords.cs | 2 +- .../Mappings/HlslKnownMethods.cs | 2 +- .../Mappings/HlslKnownKeywords.cs | 2 +- .../Mappings/HlslKnownMethods.cs | 2 +- .../Mappings/HlslKnownOperators.cs | 2 +- 23 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/ComputeSharp.Core/Intrinsics/Attributes/HlslIntrinsicNameAttribute.cs b/src/ComputeSharp.Core/Intrinsics/Attributes/HlslIntrinsicNameAttribute.cs index 8d847093..2a3ae320 100644 --- a/src/ComputeSharp.Core/Intrinsics/Attributes/HlslIntrinsicNameAttribute.cs +++ b/src/ComputeSharp.Core/Intrinsics/Attributes/HlslIntrinsicNameAttribute.cs @@ -1,7 +1,7 @@ using System; using System.Diagnostics; -namespace ComputeSharp.Core.Intrinsics.Attributes; +namespace ComputeSharp.Core.Intrinsics; /// /// An attribute indicating the native member name of a given HLSL intrinsic. diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.g.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.g.cs index 848410e7..962fe432 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.g.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.g.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.tt b/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.tt index 0619fda2..cd09db1f 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.tt +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Casts.tt @@ -2,7 +2,7 @@ <#@ assembly name="System.Core" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".g.cs"#> -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs index a3352f9b..d3e4e53a 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt index 3d0fac53..7e79be7e 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt @@ -1,5 +1,5 @@ <#@include file="Hlsl.Void.ttinclude" #> -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs index 80a48e29..33c85335 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.g.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.g.cs index a4e9b674..ce8b2c81 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.g.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.g.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.tt b/src/ComputeSharp.Core/Intrinsics/Hlsl.tt index fee433f0..57c6459d 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.tt +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.tt @@ -1,5 +1,5 @@ <#@include file="Hlsl.ttinclude" #> -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0022 diff --git a/src/ComputeSharp.Core/Primitives/Float/Float2.g.cs b/src/ComputeSharp.Core/Primitives/Float/Float2.g.cs index 5795eb00..1d999a59 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float2.g.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float2.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Float/Float3.g.cs b/src/ComputeSharp.Core/Primitives/Float/Float3.g.cs index 1a557d8a..a11ea465 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float3.g.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float3.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Float/Float4.g.cs b/src/ComputeSharp.Core/Primitives/Float/Float4.g.cs index c325b6d3..15197bb0 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float4.g.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float4.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Float/FloatMxN.g.cs b/src/ComputeSharp.Core/Primitives/Float/FloatMxN.g.cs index 982f252a..b16726b3 100644 --- a/src/ComputeSharp.Core/Primitives/Float/FloatMxN.g.cs +++ b/src/ComputeSharp.Core/Primitives/Float/FloatMxN.g.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Int/Int2.g.cs b/src/ComputeSharp.Core/Primitives/Int/Int2.g.cs index 82e8bddc..eac55805 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int2.g.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int2.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Int/Int3.g.cs b/src/ComputeSharp.Core/Primitives/Int/Int3.g.cs index 54c96875..b1af3517 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int3.g.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int3.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Int/Int4.g.cs b/src/ComputeSharp.Core/Primitives/Int/Int4.g.cs index 49588ee4..0dc12e11 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int4.g.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int4.g.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #nullable enable #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/Int/IntMxN.g.cs b/src/ComputeSharp.Core/Primitives/Int/IntMxN.g.cs index d5742dff..57a4bb71 100644 --- a/src/ComputeSharp.Core/Primitives/Int/IntMxN.g.cs +++ b/src/ComputeSharp.Core/Primitives/Int/IntMxN.g.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable CS0660, CS0661 diff --git a/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude b/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude index 18182ecc..979cf08b 100644 --- a/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude +++ b/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude @@ -20,7 +20,7 @@ using System.Runtime.InteropServices; <# if (typeName == "Float" || typeName == "Int") { - WriteLine("using ComputeSharp.Core.Intrinsics.Attributes;"); + WriteLine("using ComputeSharp.Core.Intrinsics;"); } #> diff --git a/src/ComputeSharp.Core/Primitives/VectorType.ttinclude b/src/ComputeSharp.Core/Primitives/VectorType.ttinclude index a0bcea0e..b1e7961d 100644 --- a/src/ComputeSharp.Core/Primitives/VectorType.ttinclude +++ b/src/ComputeSharp.Core/Primitives/VectorType.ttinclude @@ -73,7 +73,7 @@ void GenerateVectorProperties(string typeName, int elementSize) if (elementTypeName == "Float" || elementTypeName == "Int") { - WriteLine("using ComputeSharp.Core.Intrinsics.Attributes;"); + WriteLine("using ComputeSharp.Core.Intrinsics;"); } #> diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownKeywords.cs index 00885bbe..8ea470fe 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; using ComputeSharp.D2D1; namespace ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs index 506de94b..3d8228e7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; using ComputeSharp.D2D1; namespace ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs index 9662e2e3..87e146fa 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; #pragma warning disable IDE0055 diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs index 19410359..b08976a0 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; namespace ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs index b800f567..cd4250e7 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Operations; From 150e2540ba82be8c830a6e7b25e9905650ba01f6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Nov 2024 19:40:24 -0800 Subject: [PATCH 2/9] Add and apply 'HlslD2DIntrinsicInputTypeAttribute' type --- .../ComputeSharp.D2D1.SourceGenerators.csproj | 2 ++ .../HlslD2DIntrinsicInputTypeAttribute.cs | 19 +++++++++++++++++++ src/ComputeSharp.D2D1/Intrinsics/D2D.cs | 9 ++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 2ac45341..11887f91 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -61,7 +61,9 @@ + + diff --git a/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs b/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs new file mode 100644 index 00000000..57936251 --- /dev/null +++ b/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs @@ -0,0 +1,19 @@ +using System; +using System.Diagnostics; +using ComputeSharp.D2D1.Interop; + +namespace ComputeSharp.D2D1.Intrinsics; + +/// +/// An attribute indicating the input type associated with a given D2D HLSL intrinsic. +/// +/// The input type for the current instance. +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +[Conditional("SOURCE_GENERATOR")] +internal sealed class HlslD2DIntrinsicInputTypeAttribute(D2D1PixelShaderInputType inputType) : Attribute +{ + /// + /// Gets the input type for the current instance. + /// + public D2D1PixelShaderInputType InputType { get; } = inputType; +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Intrinsics/D2D.cs b/src/ComputeSharp.D2D1/Intrinsics/D2D.cs index 7a9b1dce..08e88015 100644 --- a/src/ComputeSharp.D2D1/Intrinsics/D2D.cs +++ b/src/ComputeSharp.D2D1/Intrinsics/D2D.cs @@ -1,4 +1,6 @@ -using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.Core.Intrinsics; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Intrinsics; #pragma warning disable IDE0022 @@ -17,6 +19,7 @@ public static class D2D /// The color from the target input at the current coordinate, in INPUTN format. /// This method is only available for simple inputs. [HlslIntrinsicName("D2DGetInput")] + [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Simple)] public static Float4 GetInput(int index) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(GetInput)}({typeof(int)})"); /// @@ -26,6 +29,7 @@ public static class D2D /// The input coordinate, in TEXCOORDN format. /// This method is only available for complex inputs. [HlslIntrinsicName("D2DGetInputCoordinate")] + [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Complex)] public static Float4 GetInputCoordinate(int index) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(GetInputCoordinate)}({typeof(int)})"); /// @@ -44,6 +48,7 @@ public static class D2D /// The sampled value from the texture, in TEXCOORDN format. /// This method is only available for complex inputs. [HlslIntrinsicName("D2DSampleInput")] + [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Complex)] public static Float4 SampleInput(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(SampleInput)}({typeof(int)}, {typeof(Float2)})"); /// @@ -54,6 +59,7 @@ public static class D2D /// The sampled value from the texture, in TEXCOORDN format. /// This method is only available for complex inputs. [HlslIntrinsicName("D2DSampleInputAtOffset")] + [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Complex)] public static Float4 SampleInputAtOffset(int index, Float2 offset) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(SampleInputAtOffset)}({typeof(int)}, {typeof(Float2)})"); /// @@ -64,5 +70,6 @@ public static class D2D /// The sampled value from the texture, in TEXCOORDN format. /// This method is only available for complex inputs. [HlslIntrinsicName("D2DSampleInputAtPosition")] + [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Complex)] public static Float4 SampleInputAtPosition(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(SampleInputAtPosition)}({typeof(int)}, {typeof(Float2)})"); } \ No newline at end of file From a9a5b164d373680c80d54112420cc782602d5007 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Nov 2024 19:40:41 -0800 Subject: [PATCH 3/9] Add 'ISymbol.FirstAncestorOrSelf' extension --- .../Extensions/ISymbolExtensions.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index c52abfae..92d122e3 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics.CodeAnalysis; using System.Threading; using Microsoft.CodeAnalysis; @@ -155,4 +156,24 @@ internal static class ISymbolExtensions return candidateNode is not null; } + + /// + /// Gets the first symbol of a specific type among the ancestors of a given symbol. + /// + /// The input symbol to start the search from. + /// An optional predicate to filter symbols. + /// The resulting symbol, if a match was found. + public static TSymbol? FirstAncestorOrSelf(this ISymbol symbol, Func? predicate = null) + where TSymbol : class, ISymbol + { + for (ISymbol? parentSymbol = symbol; parentSymbol is not null; parentSymbol = parentSymbol.ContainingSymbol) + { + if (parentSymbol is TSymbol targetSymbol && (predicate?.Invoke(targetSymbol) is not false)) + { + return targetSymbol; + } + } + + return null; + } } \ No newline at end of file From 4044cff3e80770d860e13cb7cae44db5270e7dff Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Nov 2024 19:59:37 -0800 Subject: [PATCH 4/9] Add draft 'InvalidD2DInputArgumentAnalyzer' --- ...xelShaderDescriptorGenerator.InputTypes.cs | 2 +- .../InvalidD2DInputArgumentAnalyzer.cs | 165 ++++++++++++++++++ 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.cs index 6802773c..4d134694 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.cs @@ -13,7 +13,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// A helper with all logic to generate the InputTypes properties. /// - private static partial class InputTypes + internal static partial class InputTypes { /// /// Extracts the input info for the current shader. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs new file mode 100644 index 00000000..1fd30eca --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs @@ -0,0 +1,165 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Operations; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates an error whenever an invocation to a D2D intrinsic references an out of range or invalid shader input. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer +{ + /// + public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2DEffectIdAttributeValue]; + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // If we can't get the D2D methods map, we have to stop right away + if (!TryBuildMethodSymbolMap(context.Compilation, out ImmutableDictionary? methodSymbols)) + { + return; + } + + context.CancellationToken.ThrowIfCancellationRequested(); + + // Get the '[D2DInputCount]' symbol, which we need to validate the source input arguments + if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DInputCountAttribute") is not { } d2DInputCountAttributeSymbol) + { + return; + } + + // We want to register a callback for each method invocation, as all 'D2D' intrinsics would map to one + context.RegisterOperationAction(context => + { + IInvocationOperation operation = (IInvocationOperation)context.Operation; + + // Cheap initial filter: we only care about static methods from the 'D2D' type + if (operation.TargetMethod is not { IsStatic: false, ContainingType.Name: "D2D" } targetMethodSymbol) + { + return; + } + + // Second cheap inital filter: we only care about invocations with a constant 'int' argument in first position. + // While we're validating this, let's also get the 'index' parameter, since we need to validate it anyway. + if (operation.Arguments is not [ILiteralOperation { Type.SpecialType: SpecialType.System_Int32, ConstantValue: { HasValue: true, Value: int index } }]) + { + return; + } + + // Validate that the target method is one of the ones we care about, and get the target input type + if (!methodSymbols.TryGetValue(targetMethodSymbol, out D2D1PixelShaderInputType targetInputType)) + { + return; + } + + // We have matched a target symbol, so let's try to get the parent shader + if (context.ContainingSymbol.FirstAncestorOrSelf() is not { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // We found a containing type, make sure it has '[D2DInputCount]' + if (!typeSymbol.HasAttributeWithType(d2DInputCountAttributeSymbol)) + { + return; + } + + // At this point we can assume the shader type is mostly valid: let's get the actual input counts and types + D2DPixelShaderDescriptorGenerator.InputTypes.GetInfo( + typeSymbol, + out int inputCount, + out _, + out _, + out ImmutableArray inputTypes); + + // First validation: the index must be in range + if ((uint)index >= (uint)inputCount) + { + // TODO: emit diagnostic + } + + // Second validation: the input type must match + if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType) + { + // TODO: emit diagnostic + } + }, OperationKind.Invocation); + }); + } + + /// + /// Tries to build a map of instances for all D2D intrinsics and their associated D2D input type. + /// + /// The to consider for analysis. + /// The resulting mapping of resolved instances. + /// Whether all requested instances could be resolved. + private static bool TryBuildMethodSymbolMap(Compilation compilation, [NotNullWhen(true)] out ImmutableDictionary? methodSymbols) + { + // Get the 'D2D' symbol, to get methods from it + if (compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D") is not { } d2DSymbol) + { + methodSymbols = null; + + return false; + } + + // Get the symbols for all relevant 'D2D' methods + List d2DMethodSymbols = + [ + d2DSymbol.GetMethod(nameof(D2D.GetInput)), + d2DSymbol.GetMethod(nameof(D2D.GetInputCoordinate)), + d2DSymbol.GetMethod(nameof(D2D.SampleInput)), + d2DSymbol.GetMethod(nameof(D2D.SampleInputAtOffset)), + d2DSymbol.GetMethod(nameof(D2D.GetInput)) + ]; + + ImmutableDictionary.Builder inputTypeMethodMap = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); + + // Validate all methods and build the map + foreach (IMethodSymbol? d2DMethodSymbol in d2DMethodSymbols) + { + // We failed to find a symbol (shouldn't really ever happen), just stop here + if (d2DMethodSymbol is null) + { + methodSymbols = null; + + return false; + } + + // Lookup the attribute to get the D2D input type + if (!d2DMethodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.Intrinsics.HlslD2DIntrinsicInputTypeAttribute", out AttributeData? attributeData)) + { + methodSymbols = null; + + return false; + } + + // Verify we can extract the D2D input type + if (attributeData.ConstructorArguments is not [{ Kind: TypedConstantKind.Primitive, Type.TypeKind: TypeKind.Enum, Value: int inputType }]) + { + methodSymbols = null; + + return false; + } + + inputTypeMethodMap.Add(d2DMethodSymbol, (D2D1PixelShaderInputType)inputType); + } + + methodSymbols = inputTypeMethodMap.ToImmutable(); + + return true; + } +} \ No newline at end of file From 65715a54395ed04de804ed0bbc4b31aad64c3c25 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 19 Nov 2024 22:11:40 -0800 Subject: [PATCH 5/9] Declare and emit new diagnostics --- .../AnalyzerReleases.Shipped.md | 2 ++ .../InvalidD2DInputArgumentAnalyzer.cs | 20 ++++++++++-- .../Diagnostics/DiagnosticDescriptors.cs | 32 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 4887a339..6dd956e8 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -89,3 +89,5 @@ CMPSD2D0079 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://gith CMPSD2D0080 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0081 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0082 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0083 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0084 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs index 1fd30eca..c2d0fc07 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs @@ -17,7 +17,11 @@ namespace ComputeSharp.D2D1.SourceGenerators; public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer { /// - public override ImmutableArray SupportedDiagnostics { get; } = [InvalidD2DEffectIdAttributeValue]; + public override ImmutableArray SupportedDiagnostics { get; } = + [ + IndexOutOfRangeForD2DIntrinsic, + InvalidInputTypeForD2DIntrinsic + ]; /// public override void Initialize(AnalysisContext context) @@ -88,13 +92,23 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer // First validation: the index must be in range if ((uint)index >= (uint)inputCount) { - // TODO: emit diagnostic + context.ReportDiagnostic(Diagnostic.Create( + IndexOutOfRangeForD2DIntrinsic, + operation.Syntax.GetLocation(), + targetMethodSymbol.Name, + index, + typeSymbol, + inputCount)); } // Second validation: the input type must match if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType) { - // TODO: emit diagnostic + context.ReportDiagnostic(Diagnostic.Create( + InvalidInputTypeForD2DIntrinsic, + operation.Syntax.GetLocation(), + targetMethodSymbol.Name, + index)); } }, OperationKind.Invocation); }); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 6e8dd5b9..148bedf5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1237,4 +1237,36 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "Shaders can only be annotated with [D2DRequiresDoublePrecisionSupport] to perform validation for use of double precision operations if they are precompiled.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a D2D intrinsic call with an out of range input. + /// + /// Format: "The D2D intrinsic '{0}' cannot be used for input {1}, as the containing shader {2} only declares {3} input(s) (use a valid argument for the D2D intrinsic, or adjust the number of inputs for the shader using [D2DInputCount])". + /// + /// + public static readonly DiagnosticDescriptor IndexOutOfRangeForD2DIntrinsic = new( + id: "CMPSD2D0083", + title: "Index out of range for D2D intrinsic", + messageFormat: "The D2D intrinsic '{0}' cannot be used for input {1}, as the containing shader {2} only declares {3} input(s) (use a valid argument for the D2D intrinsic, or adjust the number of inputs for the shader using [D2DInputCount])", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "D2D intrinsics must be used with valid input indices (the number of inputs for a shader can be configured using [D2DInputCount]).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a D2D intrinsic call with an invalid input type. + /// + /// Format: "The D2D intrinsic '{0}' cannot be used for input {1}, as it does not have the right input type (you can configure the input type of each input using [D2DInputSimple] and [D2DInputComplex] on the shader type)". + /// + /// + public static readonly DiagnosticDescriptor InvalidInputTypeForD2DIntrinsic = new( + id: "CMPSD2D0084", + title: "Invalid input type for D2D intrinsic", + messageFormat: "The D2D intrinsic '{0}' cannot be used for input {1}, as it does not have the right input type (you can configure the input type of each input using [D2DInputSimple] and [D2DInputComplex] on the shader type)", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "D2D intrinsics must be used with compatible input types (the input type of each shader input can be configured using [D2DInputSimple] and [D2DInputComplex] on the shader type).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file From dfd5c095c308dd9f9b7419a222c4675fd19f26e4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Nov 2024 15:34:06 -0800 Subject: [PATCH 6/9] Fix logic in 'InvalidD2DInputArgumentAnalyzer' --- .../InvalidD2DInputArgumentAnalyzer.cs | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs index c2d0fc07..b970bb47 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs @@ -1,7 +1,8 @@ -using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; +using System.Reflection; using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Intrinsics; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -51,14 +52,14 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer IInvocationOperation operation = (IInvocationOperation)context.Operation; // Cheap initial filter: we only care about static methods from the 'D2D' type - if (operation.TargetMethod is not { IsStatic: false, ContainingType.Name: "D2D" } targetMethodSymbol) + if (operation.TargetMethod is not { IsStatic: true, ContainingType.Name: "D2D" } targetMethodSymbol) { return; } // Second cheap inital filter: we only care about invocations with a constant 'int' argument in first position. // While we're validating this, let's also get the 'index' parameter, since we need to validate it anyway. - if (operation.Arguments is not [ILiteralOperation { Type.SpecialType: SpecialType.System_Int32, ConstantValue: { HasValue: true, Value: int index } }]) + if (operation.Arguments is not [{ Value.ConstantValue: { HasValue: true, Value: int index } }, ..]) { return; } @@ -100,10 +101,9 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer typeSymbol, inputCount)); } - - // Second validation: the input type must match - if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType) + else if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType) { + // Second validation: the input type must match context.ReportDiagnostic(Diagnostic.Create( InvalidInputTypeForD2DIntrinsic, operation.Syntax.GetLocation(), @@ -131,13 +131,13 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer } // Get the symbols for all relevant 'D2D' methods - List d2DMethodSymbols = + IMethodSymbol?[] d2DMethodSymbols = [ d2DSymbol.GetMethod(nameof(D2D.GetInput)), d2DSymbol.GetMethod(nameof(D2D.GetInputCoordinate)), d2DSymbol.GetMethod(nameof(D2D.SampleInput)), d2DSymbol.GetMethod(nameof(D2D.SampleInputAtOffset)), - d2DSymbol.GetMethod(nameof(D2D.GetInput)) + d2DSymbol.GetMethod(nameof(D2D.SampleInputAtPosition)) ]; ImmutableDictionary.Builder inputTypeMethodMap = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); @@ -153,21 +153,8 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer return false; } - // Lookup the attribute to get the D2D input type - if (!d2DMethodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.Intrinsics.HlslD2DIntrinsicInputTypeAttribute", out AttributeData? attributeData)) - { - methodSymbols = null; - - return false; - } - - // Verify we can extract the D2D input type - if (attributeData.ConstructorArguments is not [{ Kind: TypedConstantKind.Primitive, Type.TypeKind: TypeKind.Enum, Value: int inputType }]) - { - methodSymbols = null; - - return false; - } + // Lookup the attribute to get the D2D input type (the attribute only exists on the 'D2D' type loaded in the analyzer) + D2D1PixelShaderInputType inputType = typeof(D2D).GetMethod(d2DMethodSymbol.Name).GetCustomAttribute()!.InputType; inputTypeMethodMap.Add(d2DMethodSymbol, (D2D1PixelShaderInputType)inputType); } From a0a269a4e6335f2cceef9b7fcd59902f5805aed8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Nov 2024 15:34:15 -0800 Subject: [PATCH 7/9] Add unit tests for new analyzer --- ...ixelShaderDescriptorGenerator_Analyzers.cs | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index d715ee7e..93fff339 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -1202,4 +1202,196 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task IndexOutOfRangeForD2DIntrinsic_NoCalls_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(0)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task IndexOutOfRangeForD2DIntrinsic_InvalidCall_WithNoD2DInputCount_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(10); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task IndexOutOfRangeForD2DIntrinsic_ValidArgument1_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(1)] + [D2DInputSimple(0)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + [DataRow(0)] + [DataRow(1)] + [DataRow(2)] + [DataRow(4)] + public async Task IndexOutOfRangeForD2DIntrinsic_ValidArgument2_DoesNotWarn(int index) + { + string source = $$""" + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(5)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + [D2DInputSimple(2)] + [D2DInputSimple(4)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput({{index}}); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + [DataRow(-1)] + [DataRow(0)] + [DataRow(1)] + [DataRow(16)] + public async Task IndexOutOfRangeForD2DIntrinsic_InvalidArgument1_Warns(int index) + { + string source = $$""" + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(0)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return {|CMPSD2D0083:D2D.GetInput({{index}})|}; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + [DataRow(-1)] + [DataRow(5)] + [DataRow(6)] + [DataRow(16)] + public async Task IndexOutOfRangeForD2DIntrinsic_InvalidArgument2_Warns(int index) + { + string source = $$""" + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(5)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + return {|CMPSD2D0083:D2D.GetInput({{index}})|}; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task InvalidInputTypeForD2DIntrinsic_ValidArguments_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(2)] + [D2DInputSimple(0)] + [D2DInputComplex(1)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + D2D.GetInput(0); + D2D.GetInputCoordinate(1); + D2D.SampleInput(1, 0); + D2D.SampleInputAtOffset(1, 0); + D2D.SampleInputAtPosition(1, 0); + + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } + + [TestMethod] + public async Task InvalidInputTypeForD2DIntrinsic_InvalidArguments_Warns() + { + const string source = """ + using ComputeSharp.D2D1; + using float4 = ComputeSharp.Float4; + + [D2DInputCount(2)] + [D2DInputSimple(0)] + [D2DInputComplex(1)] + internal readonly partial struct MyType : ID2D1PixelShader + { + public float4 Execute() + { + {|CMPSD2D0084:D2D.GetInput(1)|}; + {|CMPSD2D0084:D2D.GetInputCoordinate(0)|}; + {|CMPSD2D0084:D2D.SampleInput(0, 0)|}; + {|CMPSD2D0084:D2D.SampleInputAtOffset(0, 0)|}; + {|CMPSD2D0084:D2D.SampleInputAtPosition(0, 0)|}; + + return 0; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file From 984f834a3f31e3cd42be11f503d69e239e4b8864 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Nov 2024 16:03:12 -0800 Subject: [PATCH 8/9] Update all tests to address new analyzer errors --- tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs | 5 ++++- .../ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs | 6 ++++-- .../Tests/PixelShaderEffectTests.cs | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index fd0c2545..aa085cc8 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -558,12 +558,15 @@ namespace ComputeSharp.D2D1.Tests [D2DInputCount(1)] [D2DInputComplex(0)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] + [D2DRequiresScenePosition] [D2DGeneratedPixelShaderDescriptor] internal readonly partial struct ComplexShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader { public float4 Execute() { - return D2D.GetInput(0); + float2 uv = D2D.GetScenePosition().XY; + + return D2D.SampleInput(0, uv); } } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index a447701d..64507197 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -21,7 +21,7 @@ public partial class D2D1ReflectionServicesTests Assert.AreEqual(""" #define D2D_INPUT_COUNT 3 #define D2D_INPUT0_SIMPLE - #define D2D_INPUT1_COMPLEX + #define D2D_INPUT1_SIMPLE #define D2D_INPUT2_COMPLEX #define D2D_REQUIRES_SCENE_POSITION @@ -52,7 +52,7 @@ public partial class D2D1ReflectionServicesTests [D2DInputCount(3)] [D2DInputSimple(0)] - [D2DInputComplex(1)] + [D2DInputSimple(1)] [D2DInputComplex(2)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader41)] @@ -98,6 +98,7 @@ public partial class D2D1ReflectionServicesTests Assert.AreEqual(""" #define D2D_INPUT_COUNT 1 + #define D2D_INPUT0_SIMPLE #include "d2d1effecthelpers.hlsli" @@ -113,6 +114,7 @@ public partial class D2D1ReflectionServicesTests } [D2DInputCount(1)] + [D2DInputSimple(0)] [D2DRequiresDoublePrecisionSupport] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs index 7207cded..318d43b8 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/PixelShaderEffectTests.cs @@ -473,6 +473,7 @@ public partial class PixelShaderEffectTests } [D2DInputCount(1)] + [D2DInputSimple(0)] [D2DGeneratedPixelShaderDescriptor] [AutoConstructor] internal readonly partial struct ShaderWithSomePropertiesAndInputs : ID2D1PixelShader From 6386c4c27e34bd907341b53942d1391b4b5fc874 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Nov 2024 14:53:03 -0800 Subject: [PATCH 9/9] Update 'D2D.GetInput' to support all input types --- .../InvalidD2DInputArgumentAnalyzer.cs | 23 ++++++++++++------- .../HlslD2DIntrinsicInputTypeAttribute.cs | 4 ++++ src/ComputeSharp.D2D1/Intrinsics/D2D.cs | 2 -- ...ixelShaderDescriptorGenerator_Analyzers.cs | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs index b970bb47..18359efb 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DInputArgumentAnalyzer.cs @@ -33,7 +33,7 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer context.RegisterCompilationStartAction(static context => { // If we can't get the D2D methods map, we have to stop right away - if (!TryBuildMethodSymbolMap(context.Compilation, out ImmutableDictionary? methodSymbols)) + if (!TryBuildMethodSymbolMap(context.Compilation, out ImmutableDictionary? methodSymbols)) { return; } @@ -65,7 +65,7 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer } // Validate that the target method is one of the ones we care about, and get the target input type - if (!methodSymbols.TryGetValue(targetMethodSymbol, out D2D1PixelShaderInputType targetInputType)) + if (!methodSymbols.TryGetValue(targetMethodSymbol, out D2D1PixelShaderInputType? targetInputType)) { return; } @@ -101,7 +101,7 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer typeSymbol, inputCount)); } - else if ((D2D1PixelShaderInputType)inputTypes[index] != targetInputType) + else if (targetInputType is not null && (D2D1PixelShaderInputType)inputTypes[index] != targetInputType) { // Second validation: the input type must match context.ReportDiagnostic(Diagnostic.Create( @@ -120,7 +120,7 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer /// The to consider for analysis. /// The resulting mapping of resolved instances. /// Whether all requested instances could be resolved. - private static bool TryBuildMethodSymbolMap(Compilation compilation, [NotNullWhen(true)] out ImmutableDictionary? methodSymbols) + private static bool TryBuildMethodSymbolMap(Compilation compilation, [NotNullWhen(true)] out ImmutableDictionary? methodSymbols) { // Get the 'D2D' symbol, to get methods from it if (compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2D") is not { } d2DSymbol) @@ -140,7 +140,7 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer d2DSymbol.GetMethod(nameof(D2D.SampleInputAtPosition)) ]; - ImmutableDictionary.Builder inputTypeMethodMap = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); + ImmutableDictionary.Builder inputTypeMethodMap = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); // Validate all methods and build the map foreach (IMethodSymbol? d2DMethodSymbol in d2DMethodSymbols) @@ -154,9 +154,16 @@ public sealed class InvalidD2DInputArgumentAnalyzer : DiagnosticAnalyzer } // Lookup the attribute to get the D2D input type (the attribute only exists on the 'D2D' type loaded in the analyzer) - D2D1PixelShaderInputType inputType = typeof(D2D).GetMethod(d2DMethodSymbol.Name).GetCustomAttribute()!.InputType; - - inputTypeMethodMap.Add(d2DMethodSymbol, (D2D1PixelShaderInputType)inputType); + if (typeof(D2D).GetMethod(d2DMethodSymbol.Name).GetCustomAttribute() is { } hlslD2DIntrinsicInputTypeAttribute) + { + inputTypeMethodMap.Add(d2DMethodSymbol, hlslD2DIntrinsicInputTypeAttribute.InputType); + } + else + { + // If the method is not annotated, we stil track it, but we will not indicate any exclusive input type. + // This means that the input index validation logic will still work, but we'll skip the input type checks. + inputTypeMethodMap.Add(d2DMethodSymbol, null); + } } methodSymbols = inputTypeMethodMap.ToImmutable(); diff --git a/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs b/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs index 57936251..2e7b726b 100644 --- a/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs +++ b/src/ComputeSharp.D2D1/Intrinsics/Attributes/HlslD2DIntrinsicInputTypeAttribute.cs @@ -8,6 +8,10 @@ namespace ComputeSharp.D2D1.Intrinsics; /// An attribute indicating the input type associated with a given D2D HLSL intrinsic. /// /// The input type for the current instance. +/// +/// If this attribute is not present, methods will be considered as supporting all input types. +/// This matches the behavior for , when not present. +/// [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] [Conditional("SOURCE_GENERATOR")] internal sealed class HlslD2DIntrinsicInputTypeAttribute(D2D1PixelShaderInputType inputType) : Attribute diff --git a/src/ComputeSharp.D2D1/Intrinsics/D2D.cs b/src/ComputeSharp.D2D1/Intrinsics/D2D.cs index 08e88015..5cb1aa31 100644 --- a/src/ComputeSharp.D2D1/Intrinsics/D2D.cs +++ b/src/ComputeSharp.D2D1/Intrinsics/D2D.cs @@ -17,9 +17,7 @@ public static class D2D /// /// The index of the input texture to get the input from. /// The color from the target input at the current coordinate, in INPUTN format. - /// This method is only available for simple inputs. [HlslIntrinsicName("D2DGetInput")] - [HlslD2DIntrinsicInputType(D2D1PixelShaderInputType.Simple)] public static Float4 GetInput(int index) => throw new InvalidExecutionContextException($"{typeof(D2D)}.{nameof(GetInput)}({typeof(int)})"); /// diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index 93fff339..658f71f9 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -1354,6 +1354,7 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers public float4 Execute() { D2D.GetInput(0); + D2D.GetInput(1); D2D.GetInputCoordinate(1); D2D.SampleInput(1, 0); D2D.SampleInputAtOffset(1, 0); @@ -1381,7 +1382,6 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers { public float4 Execute() { - {|CMPSD2D0084:D2D.GetInput(1)|}; {|CMPSD2D0084:D2D.GetInputCoordinate(0)|}; {|CMPSD2D0084:D2D.SampleInput(0, 0)|}; {|CMPSD2D0084:D2D.SampleInputAtOffset(0, 0)|};