Optimize lookups for HLSL known types

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-08 13:20:47 +01:00
1 親 058db06971
コミット 0e82ce93e3
6ファイル変更
+81 -34

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

+2 -2
ファイルの表示
@@ -34,7 +34,7 @@ partial class HlslKnownTypes
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullyQualifiedMetadataName();
// Get the HLSL name for the type argument (it can only be either float or float4)
_ = KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType);
_ = KnownHlslTypeMetadataNames.TryGetValue(genericArgumentName, out string? mappedElementType);
return typeName switch
{
@@ -46,7 +46,7 @@ partial class HlslKnownTypes
}
// The captured field is of an HLSL primitive type
if (KnownHlslTypes.TryGetValue(typeName, out string? mappedType))
if (KnownHlslTypeMetadataNames.TryGetValue(typeName, out string? mappedType))
{
return mappedType;
}
+1 -1
ファイルの表示
@@ -47,7 +47,7 @@ internal static partial class HlslKnownKeywords
]);
// HLSL primitive names
foreach (Type? type in HlslKnownTypes.KnownVectorTypes.Concat(HlslKnownTypes.KnownMatrixTypes))
foreach (Type? type in HlslKnownTypes.EnumerateKnownVectorTypes().Concat(HlslKnownTypes.EnumerateKnownMatrixTypes()))
{
_ = knownKeywords.Add(type.Name.ToLowerInvariant());
}
+2 -1
ファイルの表示
@@ -19,13 +19,14 @@ internal static partial class HlslKnownOperators
/// <summary>
/// Builds the mapping of supported known operators to HLSL names.
/// </summary>
/// <returns>The mapping of supported known operators to HLSL names.</returns>
private static IReadOnlyDictionary<string, string> BuildKnownOperatorsMap()
{
Dictionary<string, string> knownOperators = [];
// Programmatically load mappings for the intrinsic operators on each HLSL primitive type
foreach ((Type Type, MethodInfo Operator, string IntrinsicName, bool RequiresParametersMatching) item in
from type in HlslKnownTypes.KnownVectorTypes.Concat(HlslKnownTypes.KnownMatrixTypes)
from type in HlslKnownTypes.EnumerateKnownVectorTypes().Concat(HlslKnownTypes.EnumerateKnownMatrixTypes())
from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
where method.IsSpecialName && method.Name.StartsWith("op_", StringComparison.InvariantCulture)
let attribute = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()
+2 -2
ファイルの表示
@@ -141,7 +141,7 @@ internal static partial class HlslKnownProperties
// Programmatically load mappings for the instance members of the HLSL vector types
foreach ((Type Type, PropertyInfo Property) item in
from type in HlslKnownTypes.KnownVectorTypes
from type in HlslKnownTypes.EnumerateKnownVectorTypes()
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
select (Type: type, Property: property))
{
@@ -150,7 +150,7 @@ internal static partial class HlslKnownProperties
// Load mappings for the matrix properties as well
foreach ((Type Type, PropertyInfo Property) item in
from type in HlslKnownTypes.KnownMatrixTypes
from type in HlslKnownTypes.EnumerateKnownMatrixTypes()
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
where Regex.IsMatch(property.Name, "^M[1-4]{2}$")
select (Type: type, Property: property))
+71 -25
ファイルの表示
@@ -17,9 +17,9 @@ namespace ComputeSharp.SourceGeneration.Mappings;
internal static partial class HlslKnownTypes
{
/// <summary>
/// Gets the set of HLSL vector types.
/// The set of HLSL vector types.
/// </summary>
public static IReadOnlyCollection<Type> KnownVectorTypes { get; } =
private static readonly Type[] KnownVectorTypes =
[
typeof(Bool2), typeof(Bool3), typeof(Bool4),
typeof(Int2), typeof(Int3), typeof(Int4),
@@ -29,9 +29,9 @@ internal static partial class HlslKnownTypes
];
/// <summary>
/// Gets the set of HLSL matrix types.
/// The set of HLSL matrix types.
/// </summary>
public static IReadOnlyCollection<Type> KnownMatrixTypes { get; } =
private static readonly Type[] KnownMatrixTypes =
[
typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4),
typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4),
@@ -55,15 +55,48 @@ internal static partial class HlslKnownTypes
typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4)
];
/// <summary>
/// The mapping of supported known vector types to HLSL types.
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownVectorTypeMetadataNames = BuildKnownVectorTypeMetadataNames();
/// <summary>
/// The mapping of supported known matrix types to HLSL types.
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownMatrixTypesMetadataNames = BuildKnownMatrixTypeMetadataNames();
/// <summary>
/// The mapping of supported known types to HLSL types.
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownHlslTypes = BuildKnownHlslTypes();
private static readonly IReadOnlyDictionary<string, string> KnownHlslTypeMetadataNames = BuildKnownHlslTypeMetadataNames();
/// <summary>
/// Builds the mapping of known HLSL vector types.
/// </summary>
/// <returns>The mapping of known HLSL vector types.</returns>
private static IReadOnlyDictionary<string, string> BuildKnownVectorTypeMetadataNames()
{
return KnownVectorTypes.ToDictionary(
keySelector: static type => type.FullName,
elementSelector: static type => type.Name.ToLowerInvariant());
}
/// <summary>
/// Builds the mapping of known HLSL matrix types.
/// </summary>
/// <returns>The mapping of known HLSL matrix types.</returns>
private static IReadOnlyDictionary<string, string> BuildKnownMatrixTypeMetadataNames()
{
return KnownMatrixTypes.ToDictionary(
keySelector: static type => type.FullName,
elementSelector: static type => type.Name.ToLowerInvariant());
}
/// <summary>
/// Builds the mapping of known primitive types.
/// </summary>
private static IReadOnlyDictionary<string, string> BuildKnownHlslTypes()
/// <returns>The mapping of known primitive types.</returns>
private static IReadOnlyDictionary<string, string> BuildKnownHlslTypeMetadataNames()
{
Dictionary<string, string> knownTypes = new()
{
@@ -78,19 +111,32 @@ internal static partial class HlslKnownTypes
[typeof(double).FullName] = "double"
};
// Add all the vector types
foreach (Type type in KnownVectorTypes)
{
knownTypes.Add(type.FullName, type.Name.ToLowerInvariant());
}
IEnumerable<KeyValuePair<string, string>> metadataNames =
knownTypes
.Concat(KnownVectorTypeMetadataNames)
.Concat(KnownMatrixTypesMetadataNames);
// Add all the matrix types
foreach (Type type in KnownMatrixTypes)
{
knownTypes.Add(type.FullName, type.Name.ToLowerInvariant());
}
return metadataNames.ToDictionary(
keySelector: static p => p.Key,
elementSelector: static p => p.Value);
}
return knownTypes;
/// <summary>
/// Enumerates the known HLSL vector types.
/// </summary>
/// <returns>The sequence of all known HLSL vector types.</returns>
public static IEnumerable<Type> EnumerateKnownVectorTypes()
{
return KnownVectorTypes;
}
/// <summary>
/// Enumerates the known HLSL matrix types.
/// </summary>
/// <returns>The sequence of all known HLSL matrix types.</returns>
public static IEnumerable<Type> EnumerateKnownMatrixTypes()
{
return KnownMatrixTypes;
}
/// <summary>
@@ -100,7 +146,7 @@ internal static partial class HlslKnownTypes
/// <returns>Whether or not <paramref name="typeName"/> represents a scalar, vector or matrix type.</returns>
public static bool IsKnownHlslType(string typeName)
{
return KnownHlslTypes.ContainsKey(typeName);
return KnownHlslTypeMetadataNames.ContainsKey(typeName);
}
/// <summary>
@@ -110,7 +156,7 @@ internal static partial class HlslKnownTypes
/// <returns>Whether or not <paramref name="typeName"/> represents a vector type.</returns>
public static bool IsVectorType(string typeName)
{
return KnownVectorTypes.Any(type => type.FullName == typeName);
return KnownVectorTypeMetadataNames.ContainsKey(typeName);
}
/// <summary>
@@ -120,7 +166,7 @@ internal static partial class HlslKnownTypes
/// <returns>Whether or not <paramref name="typeName"/> represents a matrix type.</returns>
public static bool IsMatrixType(string typeName)
{
return KnownMatrixTypes.Any(type => type.FullName == typeName);
return KnownMatrixTypesMetadataNames.ContainsKey(typeName);
}
/// <summary>
@@ -170,7 +216,7 @@ internal static partial class HlslKnownTypes
{
string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullyQualifiedMetadataName();
if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
if (KnownHlslTypeMetadataNames.TryGetValue(elementTypeName, out string? mapped))
{
return mapped;
}
@@ -185,7 +231,7 @@ internal static partial class HlslKnownTypes
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
public static string GetMappedName(string originalName)
{
return KnownHlslTypes[originalName];
return KnownHlslTypeMetadataNames[originalName];
}
/// <summary>
@@ -196,7 +242,7 @@ internal static partial class HlslKnownTypes
/// <returns>Whether a mapped name was available.</returns>
public static bool TryGetMappedName(string originalName, out string? mappedName)
{
return KnownHlslTypes.TryGetValue(originalName, out mappedName);
return KnownHlslTypeMetadataNames.TryGetValue(originalName, out mappedName);
}
/// <summary>
@@ -238,7 +284,7 @@ internal static partial class HlslKnownTypes
return;
}
if (KnownHlslTypes.ContainsKey(type.GetFullyQualifiedMetadataName()))
if (KnownHlslTypeMetadataNames.ContainsKey(type.GetFullyQualifiedMetadataName()))
{
return;
}
@@ -325,7 +371,7 @@ internal static partial class HlslKnownTypes
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullyQualifiedMetadataName()) &&
if (!KnownHlslTypeMetadataNames.ContainsKey(fieldType.GetFullyQualifiedMetadataName()) &&
!invalidTypes.Contains(fieldType))
{
_ = dependencies.Add(fieldType);
+3 -3
ファイルの表示
@@ -159,7 +159,7 @@ partial class HlslKnownTypes
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullyQualifiedMetadataName();
// If the current type is a custom type, format it as needed
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
if (!KnownHlslTypeMetadataNames.TryGetValue(genericArgumentName, out string? mappedElementType))
{
mappedElementType = genericArgumentName.ToHlslIdentifierName();
}
@@ -196,7 +196,7 @@ partial class HlslKnownTypes
}
// The captured field is of an HLSL primitive type
if (KnownHlslTypes.TryGetValue(typeName, out string? mappedType))
if (KnownHlslTypeMetadataNames.TryGetValue(typeName, out string? mappedType))
{
return mappedType;
}
@@ -215,7 +215,7 @@ partial class HlslKnownTypes
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.First()).GetFullyQualifiedMetadataName();
// If the current type is a custom type, format it as needed
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
if (!KnownHlslTypeMetadataNames.TryGetValue(genericArgumentName, out string? mappedElementType))
{
mappedElementType = genericArgumentName.ToHlslIdentifierName();
}