using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ComputeSharp.Core.Intrinsics;
namespace ComputeSharp.SourceGeneration.Mappings;
///
/// A that contains and maps known HLSL method names to common .NET methods.
///
internal static partial class HlslKnownMethods
{
///
/// The mapping of supported known methods to HLSL names.
///
private static readonly Dictionary KnownMethods = BuildKnownMethodsMap();
///
/// The mapping of supported known methods that require parameters mapping to HLSL names.
///
private static readonly Dictionary KnownMethodsParametersMapping = BuildKnownMethodsParametersMappingMap();
///
/// The mapping of supported known samplers to HLSL resource type names.
///
private static readonly Dictionary KnownResourceSamplers = BuildKnownResourceSamplers();
///
/// Builds the mapping of supported known methods to HLSL names.
///
private static Dictionary BuildKnownMethodsMap()
{
Dictionary knownMethods = [];
// Programmatically load mappings from the Hlsl class as well (the ones with no parameter matching)
foreach (MethodInfo? method in
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
group method by method.Name
into groups
select groups.First())
{
HlslIntrinsicNameAttribute? attribute = method.GetCustomAttribute();
// 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
AddKnownMethods(knownMethods);
return knownMethods;
}
///
/// Builds the mapping of supported known methods that require parameters mapping to HLSL names.
///
private static Dictionary BuildKnownMethodsParametersMappingMap()
{
ILookup methodsLookup = typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static).ToLookup(static method => method.Name);
Dictionary knownMethods = [];
// Go through the previously discovered methods to find the ones requiring parameters mapping
foreach (KeyValuePair 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;
}
///
/// Builds the mapping of supported known samplers to HLSL resource type names.
///
/// The mapping of supported known samplers to HLSL resource type names.
private static partial Dictionary BuildKnownResourceSamplers();
///
/// 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 sampler resource type name for the input indexer name.
///
/// The input fully qualified indexer name.
/// The mapped type name, if one is found.
/// The HLSL-compatible type name that can be used in an HLSL shader for the given sampler.
public static bool TryGetMappedResourceSamplerAccessType(string name, out string? mapped)
{
return KnownResourceSamplers.TryGetValue(name, out mapped);
}
///
/// 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.
/// 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)
{
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)})"];
}
}