diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
index f9e79082..d4fa2be1 100644
--- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
@@ -3,6 +3,8 @@ 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,33 +18,38 @@ namespace ComputeSharp.SourceGenerators.Mappings
///
/// The mapping of supported known types to HLSL types.
///
- private static readonly IReadOnlyDictionary KnownTypes = new Dictionary
+ private static readonly IReadOnlyDictionary KnownTypes = BuildKnownTypes();
+
+ ///
+ /// Builds the mapping of known primitive types.
+ ///
+ [Pure]
+ private static IReadOnlyDictionary BuildKnownTypes()
{
- [typeof(bool).FullName] = "bool",
- [typeof(Bool).FullName] = "bool",
- [typeof(Bool2).FullName] = "bool2",
- [typeof(Bool3).FullName] = "bool3",
- [typeof(Bool4).FullName] = "bool4",
- [typeof(int).FullName] = "int",
- [typeof(Int2).FullName] = "int2",
- [typeof(Int3).FullName] = "int3",
- [typeof(Int4).FullName] = "int4",
- [typeof(uint).FullName] = "uint",
- [typeof(UInt2).FullName] = "uint2",
- [typeof(UInt3).FullName] = "uint3",
- [typeof(UInt4).FullName] = "uint4",
- [typeof(float).FullName] = "float",
- [typeof(Float2).FullName] = "float2",
- [typeof(Float3).FullName] = "float3",
- [typeof(Float4).FullName] = "float4",
- [typeof(Vector2).FullName] = "float2",
- [typeof(Vector3).FullName] = "float3",
- [typeof(Vector4).FullName] = "float4",
- [typeof(double).FullName] = "double",
- [typeof(Double2).FullName] = "double2",
- [typeof(Double3).FullName] = "double3",
- [typeof(Double4).FullName] = "double4"
- };
+ Dictionary knownTypes = new()
+ {
+ [typeof(bool).FullName] = "bool",
+ [typeof(int).FullName] = "int",
+ [typeof(uint).FullName] = "uint",
+ [typeof(float).FullName] = "float",
+ [typeof(Vector2).FullName] = "float2",
+ [typeof(Vector3).FullName] = "float3",
+ [typeof(Vector4).FullName] = "float4",
+ [typeof(double).FullName] = "double"
+ };
+
+ // 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)
+ {
+ knownTypes.Add(type.FullName, type.Name.ToLower());
+ }
+
+ return knownTypes;
+ }
///
/// Gets the known HLSL vector types available as mapped types.
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index af938698..964aeae1 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -574,14 +574,36 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// the property access to the corresponding HLSL syntax. For instance, m[M11, M12] will become m._m00_m01.
if (HlslKnownMembers.IsKnownMatrixIndexer(propertyName))
{
+ bool isValid = true;
+
+ // Validate the arguments
foreach (ArgumentSyntax argument in node.ArgumentList.Arguments)
{
if (this.semanticModel.GetOperation(argument.Expression) is not IFieldReferenceOperation fieldReference ||
!HlslKnownMembers.IsKnownMatrixIndex(fieldReference.Field.GetFullMetadataName()))
{
this.context.ReportDiagnostic(NonConstantMatrixSwizzledIndex, argument);
+
+ isValid = false;
}
}
+
+ if (isValid)
+ {
+ // Rewrite the indexer as a property access
+ string hlslPropertyName = string.Join("",
+ from argument in node.ArgumentList.Arguments
+ let fieldReference = (IFieldReferenceOperation)this.semanticModel.GetOperation(argument.Expression)!
+ let fieldName = fieldReference.Field.Name
+ let coordinate = int.Parse(fieldName.Substring(1)) - 11
+ let indices = coordinate > 0 ? coordinate.ToString() : "00"
+ select $"_m{indices}");
+
+ return MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ updatedNode.Expression,
+ IdentifierName(hlslPropertyName));
+ }
}
}