From f8f2e9c1e64c0ea480b8661cb89fc80c448efabc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Dec 2023 23:12:03 +0100 Subject: [PATCH] Use concrete types for all generator mapping fields --- .../Mappings/HlslKnownMethods.cs | 26 +++---- .../Mappings/HlslKnownProperties.cs | 8 +-- .../Mappings/HlslKnownFields.cs | 2 +- .../Mappings/HlslKnownKeywords.cs | 2 +- .../Mappings/HlslKnownMethods.cs | 26 +++---- .../Mappings/HlslKnownOperators.cs | 2 +- .../Mappings/HlslKnownProperties.cs | 71 +++++++++---------- .../Mappings/HlslKnownSizes.cs | 2 +- .../Mappings/HlslKnownTypes.cs | 8 +-- .../Mappings/HlslKnownMethods.cs | 4 +- .../Mappings/HlslKnownProperties.cs | 8 +-- 11 files changed, 79 insertions(+), 80 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs index 24ae776a..506de94b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -21,6 +21,19 @@ partial class HlslKnownMethods return name is "ComputeSharp.D2D1.D2D.GetScenePosition" or "ComputeSharp.D2D1.D2D.SampleInputAtPosition"; } + /// + private static partial Dictionary BuildKnownResourceSamplers() + { + return new() + { + [$"ComputeSharp.D2D1.D2D1ResourceTexture1D`1.Sample({typeof(float).FullName})"] = null, + [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2", + [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(Float2).FullName})"] = null, + [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName})"] = "float3", + [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(Float3).FullName})"] = null + }; + } + /// static partial void AddKnownMethods(IDictionary knownMethods) @@ -37,17 +50,4 @@ partial class HlslKnownMethods knownMethods.Add($"{typeof(D2D).FullName}{Type.Delimiter}{method.Name}", hlslName); } } - - /// - private static partial IReadOnlyDictionary BuildKnownResourceSamplers() - { - return new Dictionary - { - [$"ComputeSharp.D2D1.D2D1ResourceTexture1D`1.Sample({typeof(float).FullName})"] = null, - [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(float).FullName}, {typeof(float).FullName})"] = "float2", - [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Sample({typeof(Float2).FullName})"] = null, - [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName})"] = "float3", - [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.Sample({typeof(Float3).FullName})"] = null - }; - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs index af7c3fb6..2c750be2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Mappings/HlslKnownProperties.cs @@ -6,9 +6,9 @@ namespace ComputeSharp.SourceGeneration.Mappings; partial class HlslKnownProperties { /// - private static partial IReadOnlyDictionary BuildKnownResourceIndexers() + private static partial Dictionary BuildKnownResourceIndexers() { - return new Dictionary + return new() { [$"ComputeSharp.D2D1.D2D1ResourceTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.D2D1.D2D1ResourceTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" @@ -16,9 +16,9 @@ partial class HlslKnownProperties } /// - private static partial IReadOnlyDictionary BuildKnownSizeAccessors() + private static partial Dictionary BuildKnownSizeAccessors() { - return new Dictionary + return new() { ["ComputeSharp.D2D1.D2D1ResourceTexture1D`1.Width"] = (1, 0), ["ComputeSharp.D2D1.D2D1ResourceTexture2D`1.Width"] = (2, 0), diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs index aabbf373..3e56fdc1 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs @@ -10,7 +10,7 @@ internal static partial class HlslKnownFields /// /// The mapping of supported known fields to HLSL names. /// - private static readonly IReadOnlyDictionary KnownFields = BuildKnownFieldsMap(); + private static readonly Dictionary KnownFields = BuildKnownFieldsMap(); /// /// Builds the mapping of supported known fields to HLSL names. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs index 12b6050e..9662e2e3 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs @@ -16,7 +16,7 @@ internal static partial class HlslKnownKeywords /// /// The mapping of known HLSL keywords. /// - private static readonly IReadOnlyCollection KnownKeywords = BuildKnownKeywordsMap(); + private static readonly HashSet KnownKeywords = BuildKnownKeywordsMap(); /// /// Builds the mapping of all known HLSL keywords. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs index 4e2b14e6..19410359 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs @@ -14,12 +14,17 @@ internal static partial class HlslKnownMethods /// /// The mapping of supported known methods to HLSL names. /// - private static readonly IReadOnlyDictionary KnownMethods = BuildKnownMethodsMap(); + private static readonly Dictionary KnownMethods = BuildKnownMethodsMap(); /// /// The mapping of supported known methods that require parameters mapping to HLSL names. /// - private static readonly IReadOnlyDictionary KnownMethodsParametersMapping = BuildKnownMethodsParametersMappingMap(); + 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. @@ -94,23 +99,18 @@ internal static partial class HlslKnownMethods 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); - /// - /// The mapping of supported known samplers to HLSL resource type names. - /// - private static readonly IReadOnlyDictionary KnownResourceSamplers = BuildKnownResourceSamplers(); - - /// - /// 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 IReadOnlyDictionary BuildKnownResourceSamplers(); - /// /// Tries to get the mapped HLSL-compatible sampler resource type name for the input indexer name. /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs index 15dd8294..3403bac6 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs @@ -14,7 +14,7 @@ internal static partial class HlslKnownOperators /// /// The mapping of supported known operators to HLSL names. /// - private static readonly IReadOnlyDictionary KnownOperators = BuildKnownOperatorsMap(); + private static readonly Dictionary KnownOperators = BuildKnownOperatorsMap(); /// /// Builds the mapping of supported known operators to HLSL names. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs index 49583f0f..e91156dd 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -15,7 +14,27 @@ internal static partial class HlslKnownProperties /// /// The mapping of supported known properties to HLSL names. /// - private static readonly IReadOnlyDictionary KnownProperties = BuildKnownPropertiesMap(); + private static readonly Dictionary KnownProperties = BuildKnownPropertiesMap(); + + /// + /// The mapping of supported known properties to HLSL names. + /// + private static readonly HashSet KnownMatrixIndexers = BuildKnownMatrixIndexers(); + + /// + /// The mapping of supported known members to HLSL names. + /// + private static readonly HashSet KnownMatrixIndices = BuildKnownMatrixIndices(); + + /// + /// The mapping of supported known indexers to HLSL resource type names. + /// + private static readonly Dictionary KnownResourceIndexers = BuildKnownResourceIndexers(); + + /// + /// The mapping of supported known size accessors for HLSL resource types. + /// + private static readonly Dictionary KnownSizeAccessors = BuildKnownSizeAccessors(); /// /// Builds the mapping of supported known properties to HLSL names. @@ -143,23 +162,12 @@ internal static partial class HlslKnownProperties return knownProperties; } - /// - /// Adds more known members to the mapping to use. - /// - /// The mapping of known properties being built. - static partial void AddKnownProperties(IDictionary knownProperties); - - /// - /// The mapping of supported known properties to HLSL names. - /// - private static readonly IReadOnlyCollection KnownMatrixIndexers = BuildKnownMatrixIndexers(); - /// /// Builds the mapping of swizzled matrix indexer properties. /// - private static IReadOnlyCollection BuildKnownMatrixIndexers() + private static HashSet BuildKnownMatrixIndexers() { - return ( + return new( from type in Assembly.GetExecutingAssembly().ExportedTypes where Regex.IsMatch(type.FullName, @"ComputeSharp\.(Bool|Double|Float|Int|UInt)[1-4]x[1-4]") from property in type.GetProperties() @@ -167,45 +175,36 @@ internal static partial class HlslKnownProperties where indices.Length > 0 && indices.All(static index => index.ParameterType == typeof(MatrixIndex)) let metadataName = $"{type.FullName}.this[{string.Join(", ", indices.Select(index => index.ParameterType))}]" - select metadataName).ToImmutableHashSet(); + select metadataName); } - /// - /// The mapping of supported known members to HLSL names. - /// - private static readonly IReadOnlyCollection KnownMatrixIndices = BuildKnownMatrixIndices(); - /// /// Builds the mapping of swizzled matrix indices. /// - private static IReadOnlyCollection BuildKnownMatrixIndices() + private static HashSet BuildKnownMatrixIndices() { - return ( + return new( from name in Enum.GetNames(typeof(MatrixIndex)) - select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet(); + select $"{typeof(MatrixIndex).FullName}.{name}"); } - /// - /// The mapping of supported known indexers to HLSL resource type names. - /// - private static readonly IReadOnlyDictionary KnownResourceIndexers = BuildKnownResourceIndexers(); - /// /// Builds the mapping of supported known indexers to HLSL resource type names. /// /// The mapping of supported known indexers to HLSL resource type names. - private static partial IReadOnlyDictionary BuildKnownResourceIndexers(); - - /// - /// The mapping of supported known size accessors for HLSL resource types. - /// - private static readonly IReadOnlyDictionary KnownSizeAccessors = BuildKnownSizeAccessors(); + private static partial Dictionary BuildKnownResourceIndexers(); /// /// Builds the mapping of supported known size accessors for HLSL resource types. /// /// The mapping of supported known size accessors for HLSL resource types. - private static partial IReadOnlyDictionary BuildKnownSizeAccessors(); + private static partial Dictionary BuildKnownSizeAccessors(); + + /// + /// Adds more known members to the mapping to use. + /// + /// The mapping of known properties being built. + static partial void AddKnownProperties(IDictionary knownProperties); /// /// Checks whether or not a given property fullname matches a matrix swizzled indexer. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs index 45308371..99caf1cc 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs @@ -14,7 +14,7 @@ internal static class HlslKnownSizes /// /// The mapping of supported known sizes to HLSL type names. /// - private static readonly IReadOnlyDictionary KnownSizes = BuildKnownSizesMap(); + private static readonly Dictionary KnownSizes = BuildKnownSizesMap(); /// /// Builds the mapping of known type sizes and alignments. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs index f3496cf5..4d62654f 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs @@ -57,22 +57,22 @@ internal static partial class HlslKnownTypes /// /// The mapping of supported known vector types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownVectorTypeMetadataNames = BuildKnownVectorTypeMetadataNames(); + private static readonly Dictionary KnownVectorTypeMetadataNames = BuildKnownVectorTypeMetadataNames(); /// /// The mapping of supported known matrix types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownMatrixTypesMetadataNames = BuildKnownMatrixTypeMetadataNames(); + private static readonly Dictionary KnownMatrixTypesMetadataNames = BuildKnownMatrixTypeMetadataNames(); /// /// The mapping of supported known types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownHlslTypeMetadataNames = BuildKnownHlslTypeMetadataNames(); + private static readonly Dictionary KnownHlslTypeMetadataNames = BuildKnownHlslTypeMetadataNames(); /// /// The mapping of type info for all supported known matrix types to HLSL types. /// - private static readonly IReadOnlyDictionary KnownNonLinearMatrixTypeInfo = BuildKnownNonLinearMatrixTypeInfo(); + private static readonly Dictionary KnownNonLinearMatrixTypeInfo = BuildKnownNonLinearMatrixTypeInfo(); /// /// Builds the mapping of known HLSL vector types. diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs index 3f83ba16..d829bf30 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMethods.cs @@ -6,9 +6,9 @@ namespace ComputeSharp.SourceGeneration.Mappings; partial class HlslKnownMethods { /// - private static partial IReadOnlyDictionary BuildKnownResourceSamplers() + private static partial Dictionary BuildKnownResourceSamplers() { - return new Dictionary + return new() { [$"ComputeSharp.ReadOnlyTexture1D`2.Sample({typeof(float).FullName})"] = null, [$"ComputeSharp.IReadOnlyTexture1D`1.Sample({typeof(float).FullName})"] = null, diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs index 0d85be99..c3ff1ab2 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs @@ -9,9 +9,9 @@ namespace ComputeSharp.SourceGeneration.Mappings; partial class HlslKnownProperties { /// - private static partial IReadOnlyDictionary BuildKnownResourceIndexers() + private static partial Dictionary BuildKnownResourceIndexers() { - return new Dictionary + return new() { [$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", @@ -33,9 +33,9 @@ partial class HlslKnownProperties } /// - private static partial IReadOnlyDictionary BuildKnownSizeAccessors() + private static partial Dictionary BuildKnownSizeAccessors() { - return new Dictionary + return new() { ["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0), ["ComputeSharp.Resources.Texture1D`1.Width"] = (1, 0),