diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index d4fa2be1..b0e48a6e 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -15,6 +15,23 @@ namespace ComputeSharp.SourceGenerators.Mappings /// internal static class HlslKnownTypes { + /// + /// The collection of known matrix types. + /// + private static readonly IReadOnlyCollection KnownMatrixTypes = BuildKnownMatrixTypes(); + + /// + /// Builds the mapping of known matrix types. + /// + [Pure] + private static IReadOnlyCollection BuildKnownMatrixTypes() + { + return ( + from type in Assembly.GetExecutingAssembly().ExportedTypes + where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)") + select type).ToArray(); + } + /// /// The mapping of supported known types to HLSL types. /// @@ -39,11 +56,7 @@ namespace ComputeSharp.SourceGenerators.Mappings }; // Add all the mapped vector and matrix types - foreach ( - var type in - from type in Assembly.GetExecutingAssembly().ExportedTypes - where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)") - select type) + foreach (var type in KnownMatrixTypes) { knownTypes.Add(type.FullName, type.Name.ToLower()); } @@ -120,15 +133,35 @@ namespace ComputeSharp.SourceGenerators.Mappings } /// - /// Checks whether or not a given type name matches a typed resource type. + /// Checks whether or not a given type name matches a scalar or vector type. /// /// The input type name to check. - /// Whether or not represents a typed resource type. + /// Whether or not represents a scalar or vector. public static bool IsScalarOrVectorType(string typeName) { return KnownTypes.ContainsKey(typeName); } + /// + /// Checks whether or not a given type name matches a vector type. + /// + /// The input type name to check. + /// Whether or not represents a vector type. + public static bool IsVectorType(string typeName) + { + return HlslMappedVectorTypes.Any(type => type.FullName == typeName); + } + + /// + /// Checks whether or not a given type name matches a matrix type. + /// + /// The input type name to check. + /// Whether or not represents a matrix type. + public static bool IsMatrixType(string typeName) + { + return KnownMatrixTypes.Any(type => type.FullName == typeName); + } + /// /// Gets the mapped HLSL-compatible type name for the input type symbol. /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 5707df86..aff31db5 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -240,7 +240,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); } - return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList); + // Add explicit casts for matrix constructors to help the overload resolution + if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType && + HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName())) + { + for (int i = 0; i < node.ArgumentList!.Arguments.Count; i++) + { + IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!; + INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type; + + updatedNode = updatedNode.ReplaceNode( + updatedNode.ArgumentList!.Arguments[i].Expression, + CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression)); + } + } + + return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList!); } /// @@ -271,6 +286,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return CastExpression(explicitType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); } + // Add explicit casts like with the explicit object creation expressions above + if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType && + HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName())) + { + for (int i = 0; i < node.ArgumentList.Arguments.Count; i++) + { + IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!; + INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type; + + updatedNode = updatedNode.ReplaceNode( + updatedNode.ArgumentList.Arguments[i].Expression, + CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression)); + } + } + return InvocationExpression(explicitType, updatedNode.ArgumentList); }