diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs index 5856846c..dd7f890b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs @@ -17,6 +17,11 @@ internal static partial class HlslKnownMethods /// private static readonly IReadOnlyDictionary KnownMethods = BuildKnownMethodsMap(); + /// + /// The mapping of supported known methods that require parameters mapping to HLSL names. + /// + private static readonly IReadOnlyDictionary KnownMethodsParametersMapping = BuildKnownMethodsParametersMappingMap(); + /// /// Builds the mapping of supported known methods to HLSL names. /// @@ -122,19 +127,32 @@ internal static partial class HlslKnownMethods [$"{typeof(Vector4).FullName}.{nameof(Vector4.Normalize)}"] = "normalize" }; - // Programmatically load mappings from the Hlsl class as well + // Programmatically load mappings from the Hlsl class as well (the ones with no parameter matching) 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; + HlslIntrinsicNameAttribute? attribute = method.GetCustomAttribute(); - knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName); + // If the intrinsic requires parameters matching, skip it. It will be tracked separately. + // To avoid processing all types again, add a special identifier for it to detect it. + if (attribute is { RequiresParametersMatching: true }) + { + knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching)); + + continue; + } + else + { + // 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 = attribute?.Name ?? method.Name; + + knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName); + } } // Let other types inject additional methods @@ -143,6 +161,38 @@ internal static partial class HlslKnownMethods return knownMethods; } + /// + /// Builds the mapping of supported known methods that require parameters mapping to HLSL names. + /// + private static IReadOnlyDictionary BuildKnownMethodsParametersMappingMap() + { + ILookup methodsLookup = typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static).ToLookup(static method => method.Name); + + Dictionary knownMethods = new(); + + // Go through the previously discovered methods to find the ones requiring parameters mapping + foreach (var pair in KnownMethods) + { + // Only process methods marked as requiring parameters mapping + if (pair.Value != nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching)) + { + continue; + } + + // Go through the intrinsics with the current name, and track the method name and parameters. We don't need + // to also track the fully qualified type name, as that part has already been matched previously at this point. + foreach (MethodInfo method in methodsLookup[pair.Key.Split('.').Last()]) + { + // The key is in the format "()", and the intrinsic name is guaranteed to be available + knownMethods.Add( + $"{method.Name}({string.Join(", ", method.GetParameters().Select(static p => p.ParameterType.Name))})", + method.GetCustomAttribute()!.Name); + } + } + + return knownMethods; + } + /// /// Adds more known methods to the mapping to use. /// @@ -176,9 +226,40 @@ internal static partial class HlslKnownMethods /// /// 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) + /// Whether the method is an intrinsic, but it requires matching parameters too. + /// Whether the input method was an intrinsic. + public static bool TryGetMappedName(string name, out string? mapped, out bool requiresParametersMapping) { - return KnownMethods.TryGetValue(name, out mapped); + if (KnownMethods.TryGetValue(name, out string? value)) + { + if (value == nameof(HlslIntrinsicNameAttribute.RequiresParametersMatching)) + { + mapped = null; + requiresParametersMapping = true; + } + else + { + mapped = value; + requiresParametersMapping = false; + } + + return true; + } + + mapped = null; + requiresParametersMapping = false; + + return false; + } + + /// + /// Gets the mapped HLSL-compatible method name for the input method name and parameters. + /// + /// The method name (previously validated with ). + /// The sequence of type names for the method. + /// The intrinsic name for the current method. + public static string GetMappedNameWithParameters(string name, IEnumerable parameterTypeNames) + { + return KnownMethodsParametersMapping[$"{name}({string.Join(", ", parameterTypeNames)})"]; } } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 38d46a49..f5334d9b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -353,8 +353,13 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter // 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(metadataName, out string? mapping)) + if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping, out bool requiresParametersMapping)) { + if (requiresParametersMapping) + { + mapping = HlslKnownMethods.GetMappedNameWithParameters(method.Name, method.Parameters.Select(static p => p.Type.Name)); + } + // Allow specialized types to track the method invocation, if needed TrackKnownMethodInvocation(metadataName); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index 319e43f4..8c871706 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Globalization; +using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -98,8 +99,13 @@ internal sealed partial class StaticFieldRewriter : HlslSourceRewriter method.IsStatic) { // Rewrite HLSL intrinsic methods - if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping)) + if (HlslKnownMethods.TryGetMappedName(method.GetFullMetadataName(), out string? mapping, out bool requiresParametersMapping)) { + if (requiresParametersMapping) + { + mapping = HlslKnownMethods.GetMappedNameWithParameters(method.Name, method.Parameters.Select(static p => p.Type.Name)); + } + return updatedNode.WithExpression(ParseExpression(mapping!)); } }