diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
index 2ef9b893..a18bbba2 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs
@@ -8,7 +8,6 @@ using ComputeSharp.D2D1Interop.SourceGenerators.SyntaxRewriters;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
-using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
index 6c0c0b78..6ba508be 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs
@@ -7,7 +7,6 @@ using ComputeSharp.Core.Helpers;
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Mappings;
-using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using static ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics.DiagnosticDescriptors;
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs
index a767134f..e7e60609 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs
@@ -5,7 +5,7 @@ using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text.RegularExpressions;
-using ComputeSharp.SourceGenerators.Mappings;
+using ComputeSharp.SourceGeneration.Mappings;
namespace ComputeSharp.D2D1Interop.SourceGenerators.Mappings;
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs
index e373817b..33dc9cbe 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs
@@ -1,163 +1,13 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Numerics;
-using System.Text.RegularExpressions;
-using ComputeSharp.SourceGeneration.Extensions;
+using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
-#pragma warning disable RS1024
+namespace ComputeSharp.SourceGeneration.Mappings;
-namespace ComputeSharp.SourceGenerators.Mappings;
-
-///
-/// A that contains and maps known HLSL type names to common .NET types.
-///
-internal static class HlslKnownTypes
+///
+partial class HlslKnownTypes
{
- ///
- /// Gets the set of HLSL vector types.
- ///
- public static IReadOnlyCollection KnownVectorTypes { 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 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 KnownHlslTypes = BuildKnownHlslTypes();
-
- ///
- /// Builds the mapping of known primitive types.
- ///
- private static IReadOnlyDictionary BuildKnownHlslTypes()
- {
- 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 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());
- }
-
- return knownTypes;
- }
-
- ///
- /// 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, vector or matrix type.
- public static bool IsKnownHlslType(string typeName)
- {
- return KnownHlslTypes.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 KnownVectorTypes.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);
- }
-
- ///
- /// Checks whether or not a given type name is a non linear matrix type.
- /// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
- ///
- /// The input type name to check.
- /// The element name of the matrix type.
- /// The number of rows in the matrix type.
- /// The number of columns in the matrix type.
- /// 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))
- {
- var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
-
- if (match.Success)
- {
- elementName = match.Groups[1].Value;
- rows = int.Parse(match.Groups[2].Value);
- columns = int.Parse(match.Groups[3].Value);
-
- return true;
- }
- }
-
- elementName = null;
- rows = columns = 0;
-
- return false;
- }
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input type symbol.
- ///
- /// The input type to map.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static string GetMappedName(INamedTypeSymbol typeSymbol)
+ ///
+ public static partial string GetMappedName(INamedTypeSymbol typeSymbol)
{
string typeName = typeSymbol.GetFullMetadataName();
@@ -170,140 +20,4 @@ internal static class HlslKnownTypes
// The captured field is of a custom struct type
return typeName.ToHlslIdentifierName();
}
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input element type symbol.
- ///
- /// The input type to map.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static string GetMappedElementName(IArrayTypeSymbol typeSymbol)
- {
- string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
-
- if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
- {
- return mapped;
- }
-
- return elementTypeName.ToHlslIdentifierName();
- }
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input type name.
- ///
- /// The input type name to map.
- /// The resulting mapped type name, if found.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static bool TryGetMappedName(string originalName, 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();
- }
-
- ///
- /// Gets the sequence of unique custom types from a collection of discovered types.
- ///
- /// The input collection of discovered types.
- /// The list of unique custom types.
- public static IEnumerable GetCustomTypes(IEnumerable discoveredTypes)
- {
- // Local function to recursively gather nested types
- static void ExploreTypes(INamedTypeSymbol type, HashSet customTypes)
- {
- if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
-
- if (!customTypes.Add(type)) return;
-
- foreach (var field in type.GetMembers().OfType())
- {
- if (field.IsStatic) continue;
-
- ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
- }
- }
-
- HashSet customTypes = new(SymbolEqualityComparer.Default);
-
- // Explore all input types and their nested types
- foreach (INamedTypeSymbol type in discoveredTypes)
- {
- ExploreTypes(type, customTypes);
- }
-
- return OrderByDependency(customTypes);
- }
-
- ///
- /// Orders the input sequence of types so that they can be declared in HLSL successfully.
- ///
- /// The input collection of types to declare.
- /// The same list as input, but in a valid HLSL declaration order.
- private static IEnumerable OrderByDependency(IEnumerable types)
- {
- Queue<(INamedTypeSymbol Type, HashSet Fields)> queue = new();
-
- // Build a mapping of type dependencies for all the captured types. A type depends on another
- // when the latter is a field in the first type. HLSL requires custom types to be declared in
- // order of usage, so we need to ensure that types are declared in an order that guarantees
- // that no type will be referenced before being defined. To do so, we can create a mapping of
- // types and their dependencies, and iteratively remove items from the map when they have no
- // dependencies left. When one type is processed and removed, it is also removed from the list
- // of dependencies of all other remaining types in the map, until there is none left.
- foreach (var type in types)
- {
- HashSet dependencies = new(SymbolEqualityComparer.Default);
-
- // Only add other custom types as dependencies, and ignore HLSL types
- foreach (var field in type.GetMembers().OfType())
- {
- if (field.IsStatic) continue;
-
- INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
-
- if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
- {
- _ = dependencies.Add(fieldType);
- }
- }
-
- queue.Enqueue((type, dependencies));
- }
-
- while (queue.Count > 0)
- {
- var entry = queue.Dequeue();
-
- // No dependencies left, we can declare this type
- if (entry.Fields.Count == 0)
- {
- // Remove the current type from dependencies of others
- foreach (var pair in queue)
- {
- _ = pair.Fields.Remove(entry.Type);
- }
-
- yield return entry.Type;
- }
- else queue.Enqueue(entry);
- }
- }
}
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
index 9930377e..fb3038a9 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs
@@ -7,7 +7,6 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Mappings;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
-using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 688fc4eb..98346984 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -7,7 +7,6 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Mappings;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
-using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
index 3687dbc4..4d694555 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
@@ -14,5 +14,6 @@
+
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
index 009428ab..86d574e5 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
-using ComputeSharp.SourceGenerators.Mappings;
+using ComputeSharp.SourceGeneration.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs
new file mode 100644
index 00000000..acb2fb10
--- /dev/null
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs
@@ -0,0 +1,297 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Numerics;
+using System.Text.RegularExpressions;
+using ComputeSharp.SourceGeneration.Extensions;
+using Microsoft.CodeAnalysis;
+
+#pragma warning disable RS1024
+
+namespace ComputeSharp.SourceGeneration.Mappings;
+
+///
+/// A that contains and maps known HLSL type names to common .NET types.
+///
+internal static partial class HlslKnownTypes
+{
+ ///
+ /// Gets the set of HLSL vector types.
+ ///
+ public static IReadOnlyCollection KnownVectorTypes { 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 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 KnownHlslTypes = BuildKnownHlslTypes();
+
+ ///
+ /// Builds the mapping of known primitive types.
+ ///
+ private static IReadOnlyDictionary BuildKnownHlslTypes()
+ {
+ 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 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());
+ }
+
+ return knownTypes;
+ }
+
+ ///
+ /// 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, vector or matrix type.
+ public static bool IsKnownHlslType(string typeName)
+ {
+ return KnownHlslTypes.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 KnownVectorTypes.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);
+ }
+
+ ///
+ /// Checks whether or not a given type name is a non linear matrix type.
+ /// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
+ ///
+ /// The input type name to check.
+ /// The element name of the matrix type.
+ /// The number of rows in the matrix type.
+ /// The number of columns in the matrix type.
+ /// 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))
+ {
+ var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
+
+ if (match.Success)
+ {
+ elementName = match.Groups[1].Value;
+ rows = int.Parse(match.Groups[2].Value);
+ columns = int.Parse(match.Groups[3].Value);
+
+ return true;
+ }
+ }
+
+ elementName = null;
+ rows = columns = 0;
+
+ return false;
+ }
+
+ ///
+ /// Gets the mapped HLSL-compatible type name for the input type symbol.
+ ///
+ /// The input type to map.
+ /// The HLSL-compatible type name that can be used in an HLSL shader.
+ public static partial string GetMappedName(INamedTypeSymbol typeSymbol);
+
+ ///
+ /// Gets the mapped HLSL-compatible type name for the input element type symbol.
+ ///
+ /// The input type to map.
+ /// The HLSL-compatible type name that can be used in an HLSL shader.
+ public static string GetMappedElementName(IArrayTypeSymbol typeSymbol)
+ {
+ string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
+
+ if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
+ {
+ return mapped;
+ }
+
+ return elementTypeName.ToHlslIdentifierName();
+ }
+
+ ///
+ /// Gets the mapped HLSL-compatible type name for the input type name.
+ ///
+ /// The input type name to map.
+ /// The resulting mapped type name, if found.
+ /// The HLSL-compatible type name that can be used in an HLSL shader.
+ public static bool TryGetMappedName(string originalName, 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();
+ }
+
+ ///
+ /// Gets the sequence of unique custom types from a collection of discovered types.
+ ///
+ /// The input collection of discovered types.
+ /// The list of unique custom types.
+ public static IEnumerable GetCustomTypes(IEnumerable discoveredTypes)
+ {
+ // Local function to recursively gather nested types
+ static void ExploreTypes(INamedTypeSymbol type, HashSet customTypes)
+ {
+ if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
+
+ if (!customTypes.Add(type)) return;
+
+ foreach (var field in type.GetMembers().OfType())
+ {
+ if (field.IsStatic) continue;
+
+ ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
+ }
+ }
+
+ HashSet customTypes = new(SymbolEqualityComparer.Default);
+
+ // Explore all input types and their nested types
+ foreach (INamedTypeSymbol type in discoveredTypes)
+ {
+ ExploreTypes(type, customTypes);
+ }
+
+ return OrderByDependency(customTypes);
+ }
+
+ ///
+ /// Orders the input sequence of types so that they can be declared in HLSL successfully.
+ ///
+ /// The input collection of types to declare.
+ /// The same list as input, but in a valid HLSL declaration order.
+ private static IEnumerable OrderByDependency(IEnumerable types)
+ {
+ Queue<(INamedTypeSymbol Type, HashSet Fields)> queue = new();
+
+ // Build a mapping of type dependencies for all the captured types. A type depends on another
+ // when the latter is a field in the first type. HLSL requires custom types to be declared in
+ // order of usage, so we need to ensure that types are declared in an order that guarantees
+ // that no type will be referenced before being defined. To do so, we can create a mapping of
+ // types and their dependencies, and iteratively remove items from the map when they have no
+ // dependencies left. When one type is processed and removed, it is also removed from the list
+ // of dependencies of all other remaining types in the map, until there is none left.
+ foreach (var type in types)
+ {
+ HashSet dependencies = new(SymbolEqualityComparer.Default);
+
+ // Only add other custom types as dependencies, and ignore HLSL types
+ foreach (var field in type.GetMembers().OfType())
+ {
+ if (field.IsStatic) continue;
+
+ INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
+
+ if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
+ {
+ _ = dependencies.Add(fieldType);
+ }
+ }
+
+ queue.Enqueue((type, dependencies));
+ }
+
+ while (queue.Count > 0)
+ {
+ var entry = queue.Dequeue();
+
+ // No dependencies left, we can declare this type
+ if (entry.Fields.Count == 0)
+ {
+ // Remove the current type from dependencies of others
+ foreach (var pair in queue)
+ {
+ _ = pair.Fields.Remove(entry.Type);
+ }
+
+ yield return entry.Type;
+ }
+ else queue.Enqueue(entry);
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs
index 07196ae7..b89652a4 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchMetadataMethod.cs
@@ -1,5 +1,5 @@
using System.Collections.Immutable;
-using ComputeSharp.SourceGenerators.Mappings;
+using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGenerators.Models;
namespace ComputeSharp.SourceGenerators;
diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs
index a3d99549..31b68852 100644
--- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs
@@ -5,6 +5,7 @@ using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text.RegularExpressions;
+using ComputeSharp.SourceGeneration.Mappings;
namespace ComputeSharp.SourceGenerators.Mappings;
diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
index 76982d56..109e1b16 100644
--- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
@@ -1,19 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Numerics;
-using System.Text.RegularExpressions;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
-#pragma warning disable RS1024
+namespace ComputeSharp.SourceGeneration.Mappings;
-namespace ComputeSharp.SourceGenerators.Mappings;
-
-///
-/// A that contains and maps known HLSL type names to common .NET types.
-///
-internal static class HlslKnownTypes
+///
+partial class HlslKnownTypes
{
///
/// Gets the known HLSL dispatch types.
@@ -25,82 +19,6 @@ internal static class HlslKnownTypes
typeof(GridIds)
};
- ///
- /// Gets the set of HLSL vector types.
- ///
- public static IReadOnlyCollection KnownVectorTypes { 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 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 KnownHlslTypes = BuildKnownHlslTypes();
-
- ///
- /// Builds the mapping of known primitive types.
- ///
- private static IReadOnlyDictionary BuildKnownHlslTypes()
- {
- 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 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());
- }
-
- return knownTypes;
- }
-
///
/// Checks whether or not a given type name matches a constant buffer type.
///
@@ -199,73 +117,8 @@ internal static class HlslKnownTypes
};
}
- ///
- /// 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, vector or matrix type.
- public static bool IsKnownHlslType(string typeName)
- {
- return KnownHlslTypes.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 KnownVectorTypes.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);
- }
-
- ///
- /// Checks whether or not a given type name is a non linear matrix type.
- /// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
- ///
- /// The input type name to check.
- /// The element name of the matrix type.
- /// The number of rows in the matrix type.
- /// The number of columns in the matrix type.
- /// 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))
- {
- var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
-
- if (match.Success)
- {
- elementName = match.Groups[1].Value;
- rows = int.Parse(match.Groups[2].Value);
- columns = int.Parse(match.Groups[3].Value);
-
- return true;
- }
- }
-
- elementName = null;
- rows = columns = 0;
-
- return false;
- }
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input type symbol.
- ///
- /// The input type to map.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static string GetMappedName(INamedTypeSymbol typeSymbol)
+ ///
+ public static partial string GetMappedName(INamedTypeSymbol typeSymbol)
{
// Delegate types just return an empty string, as they're not actually
// used in the generated shaders, but just mapped to a function at runtime.
@@ -336,140 +189,4 @@ internal static class HlslKnownTypes
// Construct the HLSL type name
return $"RWTexture2D";
}
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input element type symbol.
- ///
- /// The input type to map.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static string GetMappedElementName(IArrayTypeSymbol typeSymbol)
- {
- string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
-
- if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
- {
- return mapped;
- }
-
- return elementTypeName.ToHlslIdentifierName();
- }
-
- ///
- /// Gets the mapped HLSL-compatible type name for the input type name.
- ///
- /// The input type name to map.
- /// The resulting mapped type name, if found.
- /// The HLSL-compatible type name that can be used in an HLSL shader.
- public static bool TryGetMappedName(string originalName, 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();
- }
-
- ///
- /// Gets the sequence of unique custom types from a collection of discovered types.
- ///
- /// The input collection of discovered types.
- /// The list of unique custom types.
- public static IEnumerable GetCustomTypes(IEnumerable discoveredTypes)
- {
- // Local function to recursively gather nested types
- static void ExploreTypes(INamedTypeSymbol type, HashSet customTypes)
- {
- if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
-
- if (!customTypes.Add(type)) return;
-
- foreach (var field in type.GetMembers().OfType())
- {
- if (field.IsStatic) continue;
-
- ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
- }
- }
-
- HashSet customTypes = new(SymbolEqualityComparer.Default);
-
- // Explore all input types and their nested types
- foreach (INamedTypeSymbol type in discoveredTypes)
- {
- ExploreTypes(type, customTypes);
- }
-
- return OrderByDependency(customTypes);
- }
-
- ///
- /// Orders the input sequence of types so that they can be declared in HLSL successfully.
- ///
- /// The input collection of types to declare.
- /// The same list as input, but in a valid HLSL declaration order.
- private static IEnumerable OrderByDependency(IEnumerable types)
- {
- Queue<(INamedTypeSymbol Type, HashSet Fields)> queue = new();
-
- // Build a mapping of type dependencies for all the captured types. A type depends on another
- // when the latter is a field in the first type. HLSL requires custom types to be declared in
- // order of usage, so we need to ensure that types are declared in an order that guarantees
- // that no type will be referenced before being defined. To do so, we can create a mapping of
- // types and their dependencies, and iteratively remove items from the map when they have no
- // dependencies left. When one type is processed and removed, it is also removed from the list
- // of dependencies of all other remaining types in the map, until there is none left.
- foreach (var type in types)
- {
- HashSet dependencies = new(SymbolEqualityComparer.Default);
-
- // Only add other custom types as dependencies, and ignore HLSL types
- foreach (var field in type.GetMembers().OfType())
- {
- if (field.IsStatic) continue;
-
- INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
-
- if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
- {
- _ = dependencies.Add(fieldType);
- }
- }
-
- queue.Enqueue((type, dependencies));
- }
-
- while (queue.Count > 0)
- {
- var entry = queue.Dequeue();
-
- // No dependencies left, we can declare this type
- if (entry.Fields.Count == 0)
- {
- // Remove the current type from dependencies of others
- foreach (var pair in queue)
- {
- _ = pair.Fields.Remove(entry.Type);
- }
-
- yield return entry.Type;
- }
- else queue.Enqueue(entry);
- }
- }
}