diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 0f72f7b2..350bad4f 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -177,7 +177,7 @@ namespace ComputeSharp.SourceGenerators ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"), ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)")))); } - else if (HlslKnownTypes.IsScalarOrVectorType(typeName)) + else if (HlslKnownTypes.IsKnownHlslType(typeName)) { if (fieldSymbol.IsConst) continue; diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index a4f545f1..467b5110 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -185,7 +185,7 @@ namespace ComputeSharp.SourceGenerators.Mappings // Programmatically load mappings for the instance members of the HLSL vector types foreach (var item in - from type in HlslKnownTypes.HlslMappedVectorTypes + from type in HlslKnownTypes.KnownVectorTypes from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public) select (Type: type, Property: property)) { diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index b0e48a6e..acf84d2f 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Numerics; -using System.Reflection; -using System.Text.RegularExpressions; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; @@ -16,32 +14,64 @@ namespace ComputeSharp.SourceGenerators.Mappings internal static class HlslKnownTypes { /// - /// The collection of known matrix types. + /// Gets the known HLSL dispatch types. /// - private static readonly IReadOnlyCollection KnownMatrixTypes = BuildKnownMatrixTypes(); + public static IReadOnlyCollection HlslDispatchTypes { get; } = new[] + { + typeof(ThreadIds), + typeof(GroupIds), + typeof(WarpIds) + }; /// - /// Builds the mapping of known matrix types. + /// Gets the set of HLSL vector types. /// - [Pure] - private static IReadOnlyCollection BuildKnownMatrixTypes() + public static IReadOnlyCollection KnownVectorTypes { get; } = new[] { - return ( - from type in Assembly.GetExecutingAssembly().ExportedTypes - where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)") - select type).ToArray(); - } + typeof(Bool2), typeof(Bool3), typeof(Bool4), + typeof(Int2), typeof(Int3), typeof(Int4), + typeof(UInt2), typeof(UInt3), typeof(UInt4), + typeof(Float2), typeof(Float3), typeof(Float4), + typeof(Double2), typeof(Double3), typeof(Double4) + }; + + /// + /// Gets the set of HLSL matrix types. + /// + public static IReadOnlyCollection KnownMatrixTypes { get; } = new[] + { + typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4), + typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4), + typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4), + typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4), + typeof(Int1x1), typeof(Int1x2), typeof(Int1x3), typeof(Int1x4), + typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4), + typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4), + typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4), + typeof(UInt1x1), typeof(UInt1x2), typeof(UInt1x3), typeof(UInt1x4), + typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4), + typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4), + typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4), + typeof(Float1x1), typeof(Float1x2), typeof(Float1x3), typeof(Float1x4), + typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4), + typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4), + typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4), + typeof(Double1x1), typeof(Double1x2), typeof(Double1x3), typeof(Double1x4), + typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4), + typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4), + typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4) + }; /// /// The mapping of supported known types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownTypes = BuildKnownTypes(); + private static readonly IReadOnlyDictionary KnownHlslTypes = BuildKnownHlslTypes(); /// /// Builds the mapping of known primitive types. /// [Pure] - private static IReadOnlyDictionary BuildKnownTypes() + private static IReadOnlyDictionary BuildKnownHlslTypes() { Dictionary knownTypes = new() { @@ -55,7 +85,13 @@ namespace ComputeSharp.SourceGenerators.Mappings [typeof(double).FullName] = "double" }; - // Add all the mapped vector and matrix types + // Add all the vector types + foreach (var type in KnownVectorTypes) + { + knownTypes.Add(type.FullName, type.Name.ToLower()); + } + + // Add all the matrix types foreach (var type in KnownMatrixTypes) { knownTypes.Add(type.FullName, type.Name.ToLower()); @@ -64,28 +100,6 @@ namespace ComputeSharp.SourceGenerators.Mappings return knownTypes; } - /// - /// Gets the known HLSL vector types available as mapped types. - /// - public static IReadOnlyCollection HlslMappedVectorTypes { get; } = new[] - { - typeof(Bool2), typeof(Bool3), typeof(Bool4), - typeof(Int2), typeof(Int3), typeof(Int4), - typeof(UInt2), typeof(UInt3), typeof(UInt4), - typeof(Float2), typeof(Float3), typeof(Float4), - typeof(Double2), typeof(Double3), typeof(Double4) - }; - - /// - /// Gets the known HLSL dispatch types. - /// - public static IReadOnlyCollection HlslDispatchTypes { get; } = new[] - { - typeof(ThreadIds), - typeof(GroupIds), - typeof(WarpIds) - }; - /// /// Checks whether or not a given type name matches a structured buffer type. /// @@ -133,13 +147,13 @@ namespace ComputeSharp.SourceGenerators.Mappings } /// - /// Checks whether or not a given type name matches a scalar or vector type. + /// Checks whether or not a given type name matches a known HLSL primitive type (scalar, vector or matrix). /// /// The input type name to check. - /// Whether or not represents a scalar or vector. - public static bool IsScalarOrVectorType(string typeName) + /// Whether or not represents a scalar, vector or matrix type. + public static bool IsKnownHlslType(string typeName) { - return KnownTypes.ContainsKey(typeName); + return KnownHlslTypes.ContainsKey(typeName); } /// @@ -149,7 +163,7 @@ namespace ComputeSharp.SourceGenerators.Mappings /// Whether or not represents a vector type. public static bool IsVectorType(string typeName) { - return HlslMappedVectorTypes.Any(type => type.FullName == typeName); + return KnownVectorTypes.Any(type => type.FullName == typeName); } /// @@ -182,7 +196,7 @@ namespace ComputeSharp.SourceGenerators.Mappings string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName(); // If the current type is a custom type, format it as needed - if (!KnownTypes.TryGetValue(genericArgumentName, out string? mapped)) + if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mapped)) { mapped = genericArgumentName.Replace(".", "__"); } @@ -209,7 +223,7 @@ namespace ComputeSharp.SourceGenerators.Mappings }; } - return KnownTypes[typeName]; + return KnownHlslTypes[typeName]; } /// @@ -222,7 +236,7 @@ namespace ComputeSharp.SourceGenerators.Mappings { string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName(); - if (KnownTypes.TryGetValue(elementTypeName, out string? mapped)) + if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped)) { return mapped; } @@ -237,7 +251,7 @@ namespace ComputeSharp.SourceGenerators.Mappings /// The HLSL-compatible type name that can be used in an HLSL shader. public static bool TryGetMappedName(string originalName, out string? mappedName) { - return KnownTypes.TryGetValue(originalName, out mappedName); + return KnownHlslTypes.TryGetValue(originalName, out mappedName); } /// @@ -250,7 +264,7 @@ namespace ComputeSharp.SourceGenerators.Mappings // Local function to recursively gather nested types static void ExploreTypes(INamedTypeSymbol type, HashSet customTypes) { - if (KnownTypes.ContainsKey(type.GetFullMetadataName())) return; + if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return; if (!customTypes.Add(type)) return; @@ -301,7 +315,7 @@ namespace ComputeSharp.SourceGenerators.Mappings INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type; - if (!KnownTypes.ContainsKey(fieldType.GetFullMetadataName())) + if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName())) { _ = dependencies.Add(fieldType); }