diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMethods.cs index 2b1d389d..807d76f9 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -1,142 +1,19 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Numerics; using System.Reflection; using ComputeSharp.Core.Intrinsics.Attributes; +using ComputeSharp.D2D1Interop; -namespace ComputeSharp.D2D1Interop.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; -/// -/// A that contains and maps known HLSL method names to common .NET methods. -/// -internal static class HlslKnownMethods +/// +partial class HlslKnownMethods { - /// - /// The mapping of supported known methods to HLSL names. - /// - private static readonly IReadOnlyDictionary KnownMethods = BuildKnownMethodsMap(); + /// - /// - /// Builds the mapping of supported known methods to HLSL names. - /// - private static IReadOnlyDictionary BuildKnownMethodsMap() + static partial void AddKnownMethods(IDictionary knownMethods) { - Dictionary knownMethods = new() - { - [$"{typeof(Math).FullName}.{nameof(Math.Abs)}"] = "abs", - [$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max", - [$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min", - [$"{typeof(Math).FullName}.{nameof(Math.Pow)}"] = "pow", - [$"{typeof(Math).FullName}.{nameof(Math.Sin)}"] = "sin", - [$"{typeof(Math).FullName}.{nameof(Math.Sinh)}"] = "sinh", - [$"{typeof(Math).FullName}.{nameof(Math.Asin)}"] = "asin", - [$"{typeof(Math).FullName}.{nameof(Math.Cos)}"] = "cos", - [$"{typeof(Math).FullName}.{nameof(Math.Cosh)}"] = "cosh", - [$"{typeof(Math).FullName}.{nameof(Math.Acos)}"] = "acos", - [$"{typeof(Math).FullName}.{nameof(Math.Tan)}"] = "tan", - [$"{typeof(Math).FullName}.{nameof(Math.Tanh)}"] = "tanh", - [$"{typeof(Math).FullName}.{nameof(Math.Atan)}"] = "atan", - [$"{typeof(Math).FullName}.{nameof(Math.Atan2)}"] = "atan2", - [$"{typeof(Math).FullName}.{nameof(Math.Ceiling)}"] = "ceil", - [$"{typeof(Math).FullName}.{nameof(Math.Floor)}"] = "floor", - [$"{typeof(Math).FullName}.Clamp"] = "clamp", - [$"{typeof(Math).FullName}.{nameof(Math.Exp)}"] = "exp", - [$"{typeof(Math).FullName}.{nameof(Math.Log)}"] = "log", - [$"{typeof(Math).FullName}.{nameof(Math.Log10)}"] = "log10", - [$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max", - [$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min", - [$"{typeof(Math).FullName}.{nameof(Math.Round)}"] = "round", - [$"{typeof(Math).FullName}.{nameof(Math.Sqrt)}"] = "sqrt", - [$"{typeof(Math).FullName}.{nameof(Math.Sign)}"] = "sign", - [$"{typeof(Math).FullName}.{nameof(Math.Truncate)}"] = "trunc", - - ["System.MathF.Abs"] = "abs", - ["System.MathF.Max"] = "max", - ["System.MathF.Min"] = "min", - ["System.MathF.Pow"] = "pow", - ["System.MathF.Sin"] = "sin", - ["System.MathF.Sinh"] = "sinh", - ["System.MathF.Asin"] = "asin", - ["System.MathF.Cos"] = "cos", - ["System.MathF.Cosh"] = "cosh", - ["System.MathF.Acos"] = "acos", - ["System.MathF.Tan"] = "tan", - ["System.MathF.Tanh"] = "tanh", - ["System.MathF.Atan"] = "atan", - ["System.MathF.Atan2"] = "atan2", - ["System.MathF.Ceiling"] = "ceil", - ["System.MathF.Floor"] = "floor", - ["System.MathF.Clamp"] = "clamp", - ["System.MathF.Exp"] = "exp", - ["System.MathF.Log"] = "log", - ["System.MathF.Log10"] = "log10", - ["System.MathF.Max"] = "max", - ["System.MathF.Min"] = "min", - ["System.MathF.Round"] = "round", - ["System.MathF.Sqrt"] = "sqrt", - ["System.MathF.Sign"] = "sign", - ["System.MathF.Truncate"] = "trunc", - - [$"{typeof(float).FullName}.IsFinite"] = "isfinite", - [$"{typeof(float).FullName}.{nameof(float.IsInfinity)}"] = "isinf", - [$"{typeof(float).FullName}.{nameof(float.IsNaN)}"] = "isnan", - - [$"{typeof(double).FullName}.IsFinite"] = "isfinite", - [$"{typeof(double).FullName}.{nameof(double.IsInfinity)}"] = "isinf", - [$"{typeof(double).FullName}.{nameof(double.IsNaN)}"] = "isnan", - - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Abs)}"] = "abs", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Clamp)}"] = "clamp", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Distance)}"] = "distance", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Dot)}"] = "dot", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Lerp)}"] = "lerp", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Max)}"] = "max", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Min)}"] = "min", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Reflect)}"] = "reflect", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Transform)}"] = "mul", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.TransformNormal)}"] = "mul", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Normalize)}"] = "normalize", - - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Abs)}"] = "abs", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Clamp)}"] = "clamp", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Cross)}"] = "cross", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Distance)}"] = "distance", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Dot)}"] = "dot", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Lerp)}"] = "lerp", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Max)}"] = "max", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Min)}"] = "min", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Reflect)}"] = "reflect", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Transform)}"] = "mul", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.TransformNormal)}"] = "mul", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Normalize)}"] = "normalize", - - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Abs)}"] = "abs", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Clamp)}"] = "clamp", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Distance)}"] = "distance", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Dot)}"] = "dot", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Lerp)}"] = "lerp", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Max)}"] = "max", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Min)}"] = "min", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Transform)}"] = "mul", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Normalize)}"] = "normalize" - }; - - // Programmatically load mappings from the Hlsl class as well - foreach (var method in - from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static) - group method by method.Name - into groups - select groups.First()) - { - // Check whether the current method should be translated with the original name - // or with the lowercase version. This is needed because all C# methods are exposed - // with the upper camel case format, while HLSL intrinsics use multiple different formats. - string hlslName = method.GetCustomAttribute()?.Name ?? method.Name; - - knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName); - } - // Programmatically load mappings from the D2D1 class as well foreach (var method in from method in typeof(D2D1).GetMethods(BindingFlags.Public | BindingFlags.Static) @@ -148,18 +25,5 @@ internal static class HlslKnownMethods knownMethods.Add($"{typeof(D2D1).FullName}{Type.Delimiter}{method.Name}", hlslName); } - - return knownMethods; - } - - /// - /// Tries to get the mapped HLSL-compatible method name for the input method name. - /// - /// The input fully qualified method name. - /// The mapped name, if one is found. - /// The HLSL-compatible member name that can be used in an HLSL shader. - public static bool TryGetMappedName(string name, out string? mapped) - { - return KnownMethods.TryGetValue(name, out mapped); } } diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 6ad3a01d..688fc4eb 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -6,6 +6,7 @@ using System.Linq; using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index a8d70c45..9e25b3f0 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -5,6 +5,7 @@ using System.Globalization; using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index f80ef5b4..3687dbc4 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -12,6 +12,7 @@ + \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs similarity index 94% rename from src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs rename to src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs index 39130e76..7a6f51ed 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs @@ -5,12 +5,12 @@ using System.Numerics; using System.Reflection; using ComputeSharp.Core.Intrinsics.Attributes; -namespace ComputeSharp.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; /// /// A that contains and maps known HLSL method names to common .NET methods. /// -internal static class HlslKnownMethods +internal static partial class HlslKnownMethods { /// /// The mapping of supported known methods to HLSL names. @@ -137,9 +137,18 @@ internal static class HlslKnownMethods knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName); } + // Let other types inject additional methods + AddKnownMethods(knownMethods); + return knownMethods; } + /// + /// Adds more known methods to the mapping to use. + /// + /// The mapping of known methods being built. + static partial void AddKnownMethods(IDictionary knownMethods); + /// /// Tries to get the mapped HLSL-compatible method name for the input method name. /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 8738951c..588f84c3 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 36fecebd..2e1d8578 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -4,7 +4,7 @@ using System.Collections.Immutable; using System.Globalization; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; -using ComputeSharp.SourceGenerators.Diagnostics; +using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp;