Add HlslKnownOperators type

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-28 22:32:49 +02:00
1 親 0aae5f847b
コミット a3b86a8ef8
2ファイル変更
+61

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

@@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownFields.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownKeywords.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownProperties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownSizes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownTypes.cs" />
+60
ファイルの表示
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ComputeSharp.Core.Intrinsics.Attributes;
namespace ComputeSharp.SourceGeneration.Mappings;
/// <summary>
/// A <see langword="class"/> that contains and maps known HLSL operators names to common .NET methods.
/// </summary>
internal static partial class HlslKnownOperators
{
/// <summary>
/// The mapping of supported known operators to HLSL names.
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownOperators = BuildKnownOperatorsMap();
/// <summary>
/// Builds the mapping of supported known operators to HLSL names.
/// </summary>
private static IReadOnlyDictionary<string, string> BuildKnownOperatorsMap()
{
Dictionary<string, string> knownOperators = new();
// Programmatically load mappings for the intrinsic operators on each HLSL primitive type
foreach (var item in
from type in HlslKnownTypes.KnownVectorTypes.Concat(HlslKnownTypes.KnownMatrixTypes)
from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
where method.IsSpecialName && method.Name.StartsWith("op_")
let attribute = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()
where attribute is not null
select (Type: type, Operator: method, IntrinsicName: attribute.Name, attribute.RequiresParametersMatching))
{
if (!item.RequiresParametersMatching)
{
throw new NotSupportedException("All intrinsic operators must require parameter matching.");
}
// The key is in the format "<TYPE_NAME>.<METHOD_NAME>(<PARAMETER_TYPES>)"
knownOperators.Add(
$"{item.Type.FullName}{Type.Delimiter}{item.Operator.Name}({string.Join(", ", item.Operator.GetParameters().Select(static p => p.ParameterType.Name))})",
item.IntrinsicName);
}
return knownOperators;
}
/// <summary>
/// Tries to get the mapped HLSL-compatible method name for the input operator name.
/// </summary>
/// <param name="name">The input fully qualified operator name.</param>
/// <param name="parameterTypeNames">The sequence of type names for the method.</param>
/// <param name="mapped">The mapped name, if one is found.</param>
/// <returns>The HLSL-compatible method name that can be used in an HLSL shader.</returns>
public static bool TryGetMappedName(string name, IEnumerable<string> parameterTypeNames, out string? mapped)
{
return KnownOperators.TryGetValue($"{name}({string.Join(", ", parameterTypeNames)})", out mapped);
}
}