diff --git a/samples/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs index f07d4e82..677b70ad 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs @@ -79,7 +79,7 @@ public sealed partial class MainViewModel : ObservableObject /// /// Gets the available resolution scaling options (as percentage values). /// - public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; + public IList ResolutionScaleOptions { get; } = [25, 50, 75, 100]; /// /// Gets the collection of available compute shader. diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index 176e6dd7..6362852d 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -16,8 +16,8 @@ class Program static void Main() { // Prepare the mapping of available samples to choose from - (string ShaderName, Win32Application Application)[] samples = new[] - { + (string ShaderName, Win32Application Application)[] samples = + [ (nameof(HelloWorld), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds))), (nameof(FourColorGradient), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds))), (nameof(ColorfulInfinity), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds))), @@ -31,7 +31,7 @@ class Program (nameof(TriangleGridContouring), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds))), (nameof(ContouredLayers), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds, RustyMetal.Value))), (nameof(TerracedHills), Win32ApplicationFactory.Create(static time => new((float)time.TotalSeconds))), - }; + ]; int index; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 2358e15b..619708f1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -246,7 +246,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene item.Hierarchy.WriteSyntax( state: item, writer: writer, - baseTypes: new[] { $"global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{item.Hierarchy.Hierarchy[0].QualifiedName}>" }, + baseTypes: [$"global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{item.Hierarchy.Hierarchy[0].QualifiedName}>"], memberCallbacks: declaredMembers.WrittenSpan); // If any generated types are needed, they go into a separate namespace diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index cb3686f2..f861bea0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -94,7 +94,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato state: item, writer: writer, baseTypes: [], - memberCallbacks: new IndentedTextWriter.Callback[] { Execute.WriteSyntax }); + memberCallbacks: [Execute.WriteSyntax]); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{item.MethodName}.g.cs", writer.ToString()); }); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs index 085603ed..5067edb0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs @@ -26,13 +26,13 @@ public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer /// /// The mapping of diagnostics to their attribute type names. /// - private static readonly ImmutableDictionary AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(new[] - { + private static readonly ImmutableDictionary AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange( + [ new KeyValuePair("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue), new KeyValuePair("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue), new KeyValuePair("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue), new KeyValuePair("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue) - }); + ]); /// public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create( 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/HlslKnownFields.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs index a2466cae..aabbf373 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs @@ -15,9 +15,9 @@ internal static partial class HlslKnownFields /// /// Builds the mapping of supported known fields to HLSL names. /// - private static IReadOnlyDictionary BuildKnownFieldsMap() + private static Dictionary BuildKnownFieldsMap() { - Dictionary knownMembers = new() + return new() { [$"float.{nameof(float.NaN)}"] = "asfloat(0xFFC00000)", [$"float.{nameof(float.PositiveInfinity)}"] = "asfloat(0x7F800000)", @@ -27,8 +27,6 @@ internal static partial class HlslKnownFields [$"double.{nameof(double.PositiveInfinity)}"] = "asdouble(0x00000000, 0x7FF00000)", [$"double.{nameof(double.NegativeInfinity)}"] = "asdouble(0x00000000, 0xFFF00000)" }; - - return knownMembers; } /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs index 5d412298..12b6050e 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs @@ -21,7 +21,7 @@ internal static partial class HlslKnownKeywords /// /// Builds the mapping of all known HLSL keywords. /// - private static IReadOnlyCollection BuildKnownKeywordsMap() + private static HashSet BuildKnownKeywordsMap() { // HLSL keywords HashSet knownKeywords = new( @@ -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/HlslKnownMethods.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs index c47faa54..c5cb76a8 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMethods.cs @@ -25,7 +25,7 @@ internal static partial class HlslKnownMethods /// /// Builds the mapping of supported known methods to HLSL names. /// - private static IReadOnlyDictionary BuildKnownMethodsMap() + private static Dictionary BuildKnownMethodsMap() { Dictionary knownMethods = new() { @@ -162,7 +162,7 @@ internal static partial class HlslKnownMethods /// /// Builds the mapping of supported known methods that require parameters mapping to HLSL names. /// - private static IReadOnlyDictionary BuildKnownMethodsParametersMappingMap() + private static Dictionary BuildKnownMethodsParametersMappingMap() { ILookup methodsLookup = typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static).ToLookup(static method => method.Name); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownOperators.cs index 08c22239..15dd8294 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. /// - private static IReadOnlyDictionary BuildKnownOperatorsMap() + /// The mapping of supported known operators to HLSL names. + private static Dictionary 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..163b694c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs @@ -21,7 +21,7 @@ internal static partial class HlslKnownProperties /// /// Builds the mapping of supported known properties to HLSL names. /// - private static IReadOnlyDictionary BuildKnownPropertiesMap() + private static Dictionary BuildKnownPropertiesMap() { Dictionary knownProperties = new() { @@ -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/HlslKnownSizes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs index 55df54a2..1801751d 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownSizes.cs @@ -20,7 +20,7 @@ internal static class HlslKnownSizes /// /// Builds the mapping of known type sizes and alignments. /// - private static IReadOnlyDictionary BuildKnownSizesMap() + private static Dictionary BuildKnownSizesMap() { Dictionary knownSizes = new() { diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs index 1fad2424..97089fd5 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs @@ -1,13 +1,13 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Numerics; -using System.Text.RegularExpressions; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; -#pragma warning disable RS1024 +#pragma warning disable IDE0055, RS1024 namespace ComputeSharp.SourceGeneration.Mappings; @@ -17,22 +17,22 @@ 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; } = new[] - { + private static readonly Type[] KnownVectorTypes = + [ 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. + /// The set of HLSL matrix types. /// - public static IReadOnlyCollection KnownMatrixTypes { get; } = new[] - { + private static readonly Type[] KnownMatrixTypes = + [ typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4), typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4), typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4), @@ -53,17 +53,55 @@ internal static partial class HlslKnownTypes 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 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(); + + /// + /// The mapping of type info for all supported known matrix types to HLSL types. + /// + private static readonly IReadOnlyDictionary KnownNonLinearMatrixTypeInfo = BuildKnownNonLinearMatrixTypeInfo(); + + /// + /// Builds the mapping of known HLSL vector types. + /// + /// The mapping of known HLSL vector types. + private static Dictionary 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 Dictionary 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 Dictionary BuildKnownHlslTypeMetadataNames() { Dictionary knownTypes = new() { @@ -78,19 +116,74 @@ internal static partial class HlslKnownTypes [typeof(double).FullName] = "double" }; - // Add all the vector types - foreach (Type type in KnownVectorTypes) + IEnumerable> metadataNames = + knownTypes + .Concat(KnownVectorTypeMetadataNames) + .Concat(KnownMatrixTypesMetadataNames); + + return metadataNames.ToDictionary( + keySelector: static p => p.Key, + elementSelector: static p => p.Value); + } + + /// + /// Builds the mapping of type info for all supported known matrix types to HLSL types. + /// + /// The mapping of type info for all supported known matrix types to HLSL types. + private static Dictionary BuildKnownNonLinearMatrixTypeInfo() + { + Type[] knownTypes = + [ + 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(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4), + typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4), + typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4), + 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(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4), + typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4), + typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4), + typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4), + typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4), + typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4) + ]; + + static NonLinearMatrixTypeInfo CreateTypeInfo(Type type) { - knownTypes.Add(type.FullName, type.Name.ToLowerInvariant()); + // Extract the info for each non linear matrix type: + // - The name is just the prefix without the NxM part + // - The number of rows is the first suffix character + // - The number of columns is the last suffix character + return new( + ElementName: type.Name[..^3], + Rows: int.Parse(type.Name[^3].ToString(), CultureInfo.InvariantCulture), + Columns: int.Parse(type.Name[^1].ToString(), CultureInfo.InvariantCulture)); } - // Add all the matrix types - foreach (Type type in KnownMatrixTypes) - { - knownTypes.Add(type.FullName, type.Name.ToLowerInvariant()); - } + return knownTypes.ToDictionary( + keySelector: static type => type.FullName, + elementSelector: CreateTypeInfo); + } - 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 +193,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 +203,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 +213,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); } /// @@ -134,22 +227,16 @@ internal static partial class HlslKnownTypes /// Whether or not represents a non linear matrix type. public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns) { - if (KnownMatrixTypes.Any(type => type.FullName == typeName)) + if (KnownNonLinearMatrixTypeInfo.TryGetValue(typeName, out NonLinearMatrixTypeInfo? value)) { - Match match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$"); + (elementName, rows, columns) = value; - if (match.Success) - { - elementName = match.Groups[1].Value; - rows = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); - columns = int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); - - return true; - } + return true; } elementName = null; - rows = columns = 0; + rows = 0; + columns = 0; return false; } @@ -170,7 +257,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 +272,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]; } /// @@ -194,29 +281,9 @@ internal static partial class HlslKnownTypes /// The input type name to map. /// The resulting mapped type name, if found. /// Whether a mapped name was available. - public static bool TryGetMappedName(string originalName, out string? mappedName) + public static bool TryGetMappedName(string originalName, [NotNullWhen(true)] out string? mappedName) { - return KnownHlslTypes.TryGetValue(originalName, out mappedName); - } - - /// - /// Tracks an instance and returns an HLSL compatible type name. - /// - /// The input instance to process. - /// The collection of currently discovered types. - /// A type name that represents a type compatible with HLSL. - public static string TrackType(ITypeSymbol typeSymbol, ICollection discoveredTypes) - { - string typeName = typeSymbol.GetFullyQualifiedName(); - - discoveredTypes.Add((INamedTypeSymbol)typeSymbol); - - if (TryGetMappedName(typeName, out string? mappedName)) - { - return mappedName!; - } - - return typeName.ToHlslIdentifierName(); + return KnownHlslTypeMetadataNames.TryGetValue(originalName, out mappedName); } /// @@ -238,7 +305,7 @@ internal static partial class HlslKnownTypes return; } - if (KnownHlslTypes.ContainsKey(type.GetFullyQualifiedMetadataName())) + if (KnownHlslTypeMetadataNames.ContainsKey(type.GetFullyQualifiedMetadataName())) { return; } @@ -325,7 +392,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); @@ -356,4 +423,12 @@ internal static partial class HlslKnownTypes } } } + + /// + /// A simple model with info on a non linear matrix type. + /// + /// The element name of the matrix type. + /// The number of rows in the matrix type. + /// The number of columns in the matrix type. + private sealed record NonLinearMatrixTypeInfo(string ElementName, int Rows, int Columns); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs index cbff40dd..20cfcb0c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs @@ -192,7 +192,7 @@ internal static class HlslDefinitionsSyntaxProcessor structDeclaration = structDeclaration.AddMembers( FieldDeclaration(VariableDeclaration( - IdentifierName(mappedType!)).AddVariables( + IdentifierName(mappedType)).AddVariables( VariableDeclarator(Identifier(mappedName!))))); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Tracking.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Tracking.cs index 1c197f57..90a2c926 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Tracking.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.Tracking.cs @@ -18,13 +18,24 @@ partial class HlslSourceRewriter protected TypeSyntax TrackType(SyntaxNode node, SemanticModel semanticModel) { ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(node, CancellationToken).Type!; + + return TrackType(typeSymbol); + } + + /// + /// Tracks an instance and returns the HLSL compatible . + /// + /// The input instance to process. + /// A instance that represents a type compatible with HLSL. + protected TypeSyntax TrackType(ITypeSymbol typeSymbol) + { string typeName = typeSymbol.GetFullyQualifiedName(); DiscoveredTypes.Add((INamedTypeSymbol)typeSymbol); if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) { - return ParseTypeName(mappedName!); + return ParseTypeName(mappedName); } return ParseTypeName(typeName.ToHlslIdentifierName()); @@ -83,7 +94,7 @@ partial class HlslSourceRewriter if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) { - TypeSyntax newType = ParseTypeName(mappedName!); + TypeSyntax newType = ParseTypeName(mappedName); return node.ReplaceNode(targetType, newType); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index d0e378ad..a0d66410 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -627,7 +627,7 @@ internal sealed partial class ShaderSourceRewriter( // Track and rewrite the discarded declaration if (SemanticModel.For(node).GetOperation(node.Expression, CancellationToken) is IDiscardOperation operation) { - TypeSyntax typeSyntax = ParseTypeName(HlslKnownTypes.TrackType(operation.Type!, DiscoveredTypes)); + TypeSyntax typeSyntax = TrackType(operation.Type!); string identifier = $"__implicit{this.implicitVariables.Count}"; // Add the variable to the list of implicit declarations diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index 0c070ebc..baa69f59 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -183,7 +183,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener item.Hierarchy.WriteSyntax( state: item, writer: writer, - baseTypes: new[] { $"global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{item.Hierarchy.Hierarchy[0].QualifiedName}>" }, + baseTypes: [$"global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{item.Hierarchy.Hierarchy[0].QualifiedName}>"], memberCallbacks: declaredMembers.WrittenSpan); // Append any additional types as well diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 3f9b8879..0eb7cb21 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -12,12 +12,12 @@ partial class HlslKnownTypes /// /// Gets the known HLSL dispatch types. /// - public static IReadOnlyCollection HlslDispatchTypes { get; } = new[] - { + public static IReadOnlyCollection HlslDispatchTypes { get; } = + [ typeof(ThreadIds), typeof(GroupIds), typeof(GridIds) - }; + ]; /// /// Checks whether or not a given type name matches a constant buffer type. @@ -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(); } diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs index 26f08a89..6d4a81c8 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs @@ -1830,7 +1830,7 @@ public class Test_D2DPixelShaderSourceGenerator // Create the original compilation CSharpCompilation compilation = CSharpCompilation.Create( "original", - new SyntaxTree[] { sourceTree }, + [sourceTree], metadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index ce71764e..b678afca 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -101,14 +101,14 @@ namespace ComputeSharp.D2D1.Tests { CollectionAssert.AreEqual(new[] { - D2D1PixelShaderInputType.Simple, - D2D1PixelShaderInputType.Complex, - D2D1PixelShaderInputType.Simple, - D2D1PixelShaderInputType.Complex, - D2D1PixelShaderInputType.Complex, - D2D1PixelShaderInputType.Complex, - D2D1PixelShaderInputType.Simple - }, D2D1PixelShader.GetInputTypes().ToArray()); + D2D1PixelShaderInputType.Simple, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Simple, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Complex, + D2D1PixelShaderInputType.Simple + }, D2D1PixelShader.GetInputTypes().ToArray()); } [TestMethod] diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 1e91f772..ffbd74c8 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -1574,7 +1574,7 @@ public class DiagnosticsTests // Create the original compilation CSharpCompilation compilation = CSharpCompilation.Create( "original", - new SyntaxTree[] { sourceTree }, + [sourceTree], metadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs index 0a8c5d24..4ddd5cf4 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator.cs @@ -920,7 +920,7 @@ public class Test_ComputeShaderDescriptorGenerator // Create the original compilation CSharpCompilation compilation = CSharpCompilation.Create( "original", - new SyntaxTree[] { sourceTree }, + [sourceTree], metadataReferences, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 38366d53..2c7eb855 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -180,7 +180,7 @@ public class ShadersTests Action> action = new(RunComputeShader); - _ = action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, new[] { texture }); + _ = action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, [texture]); } else { @@ -196,7 +196,7 @@ public class ShadersTests Action> action = new(RunPixelShader); - _ = action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, new[] { texture }); + _ = action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, [texture]); } _ = image.DangerousTryGetSinglePixelMemory(out Memory memory);