diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs index 30d69af3..a5ef6e11 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -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; } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs index 5d412298..05b81c63 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs @@ -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()); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs index 08c22239..daa0ef67 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs @@ -19,13 +19,14 @@ internal static partial class HlslKnownOperators /// /// Builds the mapping of supported known operators to HLSL names. /// + /// The mapping of supported known operators to HLSL names. private static IReadOnlyDictionary BuildKnownOperatorsMap() { Dictionary 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() diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs index 2b5fd0d5..2b2882f2 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs @@ -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)) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs index 07142e45..20d27c7e 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs @@ -17,9 +17,9 @@ namespace ComputeSharp.SourceGeneration.Mappings; internal static partial class HlslKnownTypes { /// - /// Gets the set of HLSL vector types. + /// The set of HLSL vector types. /// - public static IReadOnlyCollection 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 ]; /// - /// Gets the set of HLSL matrix types. + /// The set of HLSL matrix types. /// - public static IReadOnlyCollection 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) ]; + /// + /// The mapping of supported known vector types to HLSL types. + /// + private static readonly IReadOnlyDictionary KnownVectorTypeMetadataNames = BuildKnownVectorTypeMetadataNames(); + + /// + /// The mapping of supported known matrix types to HLSL types. + /// + private static readonly IReadOnlyDictionary KnownMatrixTypesMetadataNames = BuildKnownMatrixTypeMetadataNames(); + /// /// The mapping of supported known types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownHlslTypes = BuildKnownHlslTypes(); + private static readonly IReadOnlyDictionary KnownHlslTypeMetadataNames = BuildKnownHlslTypeMetadataNames(); + + /// + /// Builds the mapping of known HLSL vector types. + /// + /// The mapping of known HLSL vector types. + private static IReadOnlyDictionary BuildKnownVectorTypeMetadataNames() + { + return KnownVectorTypes.ToDictionary( + keySelector: static type => type.FullName, + elementSelector: static type => type.Name.ToLowerInvariant()); + } + + /// + /// Builds the mapping of known HLSL matrix types. + /// + /// The mapping of known HLSL matrix types. + private static IReadOnlyDictionary BuildKnownMatrixTypeMetadataNames() + { + return KnownMatrixTypes.ToDictionary( + keySelector: static type => type.FullName, + elementSelector: static type => type.Name.ToLowerInvariant()); + } /// /// Builds the mapping of known primitive types. /// - private static IReadOnlyDictionary BuildKnownHlslTypes() + /// The mapping of known primitive types. + private static IReadOnlyDictionary BuildKnownHlslTypeMetadataNames() { Dictionary 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> 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; + /// + /// Enumerates the known HLSL vector types. + /// + /// The sequence of all known HLSL vector types. + public static IEnumerable EnumerateKnownVectorTypes() + { + return KnownVectorTypes; + } + + /// + /// Enumerates the known HLSL matrix types. + /// + /// The sequence of all known HLSL matrix types. + public static IEnumerable EnumerateKnownMatrixTypes() + { + return KnownMatrixTypes; } /// @@ -100,7 +146,7 @@ internal static partial class HlslKnownTypes /// Whether or not represents a scalar, vector or matrix type. public static bool IsKnownHlslType(string typeName) { - return KnownHlslTypes.ContainsKey(typeName); + return KnownHlslTypeMetadataNames.ContainsKey(typeName); } /// @@ -110,7 +156,7 @@ internal static partial class HlslKnownTypes /// Whether or not represents a vector type. public static bool IsVectorType(string typeName) { - return KnownVectorTypes.Any(type => type.FullName == typeName); + return KnownVectorTypeMetadataNames.ContainsKey(typeName); } /// @@ -120,7 +166,7 @@ internal static partial class HlslKnownTypes /// Whether or not represents a matrix type. public static bool IsMatrixType(string typeName) { - return KnownMatrixTypes.Any(type => type.FullName == typeName); + return KnownMatrixTypesMetadataNames.ContainsKey(typeName); } /// @@ -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 /// The HLSL-compatible type name that can be used in an HLSL shader. public static string GetMappedName(string originalName) { - return KnownHlslTypes[originalName]; + return KnownHlslTypeMetadataNames[originalName]; } /// @@ -196,7 +242,7 @@ internal static partial class HlslKnownTypes /// Whether a mapped name was available. public static bool TryGetMappedName(string originalName, out string? mappedName) { - return KnownHlslTypes.TryGetValue(originalName, out mappedName); + return KnownHlslTypeMetadataNames.TryGetValue(originalName, out mappedName); } /// @@ -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); diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 4aabfd98..0eb7cb21 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -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(); }