From ba0cb5c00ff4394064a2d7becde11ad232fd653c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 10 Apr 2022 22:03:01 +0200 Subject: [PATCH] Move HlslKnownKeywords to shared generation project --- .../Mappings/HlslKnownKeywords.cs | 2 +- .../SyntaxRewriters/HlslSourceRewriter.cs | 1 - .../SyntaxRewriters/ShaderSourceRewriter.cs | 1 - .../SyntaxRewriters/StaticFieldRewriter.cs | 1 - ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + .../Mappings/HlslKnownKeywords.cs | 4 +- .../Mappings/HlslKnownMembers.cs | 14 +- ...erGenerator.CreateBuildHlslSourceMethod.cs | 1 - ...rGenerator.CreateLoadDispatchDataMethod.cs | 1 - .../Mappings/HlslKnownKeywords.cs | 2 +- .../Mappings/HlslKnownMembers.cs | 238 +----------------- .../SyntaxRewriters/HlslSourceRewriter.cs | 1 - .../SyntaxRewriters/ShaderSourceRewriter.cs | 1 - .../SyntaxRewriters/StaticFieldRewriter.cs | 1 - 14 files changed, 25 insertions(+), 244 deletions(-) rename src/{ComputeSharp.D2D1Interop.SourceGenerators => ComputeSharp.SourceGeneration.Hlsl}/Mappings/HlslKnownMembers.cs (96%) diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs index f11527b8..96565e25 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.SourceGeneration.Mappings; partial class HlslKnownKeywords { /// - static partial void AddKnownKeyword(ICollection knownKeywords) + static partial void AddKnownKeywords(ICollection knownKeywords) { // D2D1 intrinsics method names foreach (var method in typeof(D2D1).GetMethods(BindingFlags.Public | BindingFlags.Static)) diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index fb3038a9..fc277419 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Globalization; using System.Linq; -using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 98346984..7a63a252 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Globalization; using System.Linq; -using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 9e25b3f0..42e92e28 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Globalization; -using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 4d694555..ec7eabad 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -12,6 +12,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs index eb7bb46c..8b15ca42 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownKeywords.cs @@ -42,7 +42,7 @@ internal static partial class HlslKnownKeywords } // Let other types inject additional keywords - AddKnownKeyword(knownKeywords); + AddKnownKeywords(knownKeywords); return knownKeywords; } @@ -51,7 +51,7 @@ internal static partial class HlslKnownKeywords /// Adds more known keywords to the collection to use. /// /// The collection of known keywords being built. - static partial void AddKnownKeyword(ICollection knownKeywords); + static partial void AddKnownKeywords(ICollection knownKeywords); /// /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMembers.cs similarity index 96% rename from src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs rename to src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMembers.cs index e7e60609..6cfa57c9 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownMembers.cs @@ -5,14 +5,13 @@ using System.Linq; using System.Numerics; using System.Reflection; using System.Text.RegularExpressions; -using ComputeSharp.SourceGeneration.Mappings; -namespace ComputeSharp.D2D1Interop.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; /// /// A that contains and maps known HLSL member names to common .NET members. /// -internal static class HlslKnownMembers +internal static partial class HlslKnownMembers { /// /// The mapping of supported known members to HLSL names. @@ -162,9 +161,18 @@ internal static class HlslKnownMembers knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"_m{row}{column}"); } + // Let other types inject additional members + AddKnownMembers(knownMembers); + return knownMembers; } + /// + /// Adds more known members to the mapping to use. + /// + /// The mapping of known members being built. + static partial void AddKnownMembers(IDictionary knownMembers); + /// /// The mapping of supported known members to HLSL names. /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index 5eeedb05..269986de 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -7,7 +7,6 @@ using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGenerators.Diagnostics; -using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.Models; using ComputeSharp.SourceGenerators.SyntaxRewriters; using Microsoft.CodeAnalysis; diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 9abecf89..f7a20523 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -6,7 +6,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Mappings; -using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs index e01acf36..40ce2cbb 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -6,7 +6,7 @@ namespace ComputeSharp.SourceGeneration.Mappings; internal static partial class HlslKnownKeywords { /// - static partial void AddKnownKeyword(ICollection knownKeywords) + static partial void AddKnownKeywords(ICollection knownKeywords) { // Dispatch type names knownKeywords.Add(nameof(ThreadIds)); diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 31b68852..6ccc2e1f 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -1,18 +1,12 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; -using System.Numerics; using System.Reflection; -using System.Text.RegularExpressions; -using ComputeSharp.SourceGeneration.Mappings; -namespace ComputeSharp.SourceGenerators.Mappings; +namespace ComputeSharp.SourceGeneration.Mappings; -/// -/// A that contains and maps known HLSL member names to common .NET members. -/// -internal static class HlslKnownMembers +/// +partial class HlslKnownMembers { /// /// The mapping of supported known indexers to HLSL resource type names. @@ -84,154 +78,9 @@ internal static class HlslKnownMembers ["ComputeSharp.IReadWriteNormalizedTexture3D`1.Depth"] = (3, 2) }; - /// - /// The mapping of supported known members to HLSL names. - /// - private static readonly IReadOnlyDictionary KnownMembers = BuildKnownMembersMap(); - - /// - /// Builds the mapping of supported known members to HLSL names. - /// - private static IReadOnlyDictionary BuildKnownMembersMap() + /// + static partial void AddKnownMembers(IDictionary knownMembers) { - Dictionary knownMembers = new() - { - [$"{typeof(Vector2).FullName}.{nameof(Vector2.X)}"] = "x", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Y)}"] = "y", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.Zero)}"] = "(float2)0", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.One)}"] = "float2(1.0f, 1.0f)", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitX)}"] = "float2(1.0f, 0.0f)", - [$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitY)}"] = "float2(0.0f, 1.0f)", - - [$"{typeof(Vector3).FullName}.{nameof(Vector3.X)}"] = "x", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Y)}"] = "y", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Z)}"] = "z", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.Zero)}"] = "(float3)0", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.One)}"] = "float3(1.0f, 1.0f, 1.0f)", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)", - [$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)", - - [$"{typeof(Vector4).FullName}.{nameof(Vector4.X)}"] = "x", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Y)}"] = "y", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Z)}"] = "z", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.W)}"] = "w", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.Zero)}"] = "(float4)0", - [$"{typeof(Vector4).FullName}.{nameof(Vector4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)", - - [$"{typeof(Bool2).FullName}.{nameof(Bool2.False)}"] = "(bool2)0", - [$"{typeof(Bool2).FullName}.{nameof(Bool2.True)}"] = "bool2(true, true)", - [$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueX)}"] = "bool2(true, false)", - [$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueY)}"] = "bool2(false, true)", - - [$"{typeof(Bool3).FullName}.{nameof(Bool3.False)}"] = "(bool3)0", - [$"{typeof(Bool3).FullName}.{nameof(Bool3.True)}"] = "bool3(true, true, true)", - [$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueX)}"] = "bool3(true, false, false)", - [$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueY)}"] = "bool3(false, true, false)", - [$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueZ)}"] = "bool3(false, false, true)", - - [$"{typeof(Bool4).FullName}.{nameof(Bool4.False)}"] = "(bool4)0", - [$"{typeof(Bool4).FullName}.{nameof(Bool4.True)}"] = "bool4(true, true, true, true)", - [$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueX)}"] = "bool4(true, false, false, false)", - [$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueY)}"] = "bool4(false, true, false, false)", - [$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueZ)}"] = "bool4(false, false, true, false)", - [$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueW)}"] = "bool4(false, false, false, true)", - - [$"{typeof(Int2).FullName}.{nameof(Int2.Zero)}"] = "(int2)0", - [$"{typeof(Int2).FullName}.{nameof(Int2.One)}"] = "int2(1, 1)", - [$"{typeof(Int2).FullName}.{nameof(Int2.UnitX)}"] = "int2(1, 0)", - [$"{typeof(Int2).FullName}.{nameof(Int2.UnitY)}"] = "int2(0, 1)", - - [$"{typeof(Int3).FullName}.{nameof(Int3.Zero)}"] = "(int3)0", - [$"{typeof(Int3).FullName}.{nameof(Int3.One)}"] = "int3(1, 1, 1)", - [$"{typeof(Int3).FullName}.{nameof(Int3.UnitX)}"] = "int3(1, 0, 0)", - [$"{typeof(Int3).FullName}.{nameof(Int3.UnitY)}"] = "int3(0, 1, 0)", - [$"{typeof(Int3).FullName}.{nameof(Int3.UnitZ)}"] = "int3(0, 0, 1)", - - [$"{typeof(Int4).FullName}.{nameof(Int4.Zero)}"] = "(int4)0", - [$"{typeof(Int4).FullName}.{nameof(Int4.One)}"] = "int4(1, 1, 1, 1)", - [$"{typeof(Int4).FullName}.{nameof(Int4.UnitX)}"] = "int4(1, 0, 0, 0)", - [$"{typeof(Int4).FullName}.{nameof(Int4.UnitY)}"] = "int4(0, 1, 0, 0)", - [$"{typeof(Int4).FullName}.{nameof(Int4.UnitZ)}"] = "int4(0, 0, 1, 0)", - [$"{typeof(Int4).FullName}.{nameof(Int4.UnitW)}"] = "int4(0, 0, 0, 1)", - - [$"{typeof(UInt2).FullName}.{nameof(UInt2.Zero)}"] = "(uint2)0", - [$"{typeof(UInt2).FullName}.{nameof(UInt2.One)}"] = "uint2(1u, 1u)", - [$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitX)}"] = "uint2(1u, 0u)", - [$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitY)}"] = "uint2(0u, 1u)", - - [$"{typeof(UInt3).FullName}.{nameof(UInt3.Zero)}"] = "(uint3)0", - [$"{typeof(UInt3).FullName}.{nameof(UInt3.One)}"] = "uint3(1u, 1u, 1u)", - [$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitX)}"] = "uint3(1u, 0u, 0u)", - [$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitY)}"] = "uint3(0u, 1u, 0u)", - [$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitZ)}"] = "uint3(0u, 0u, 1u)", - - [$"{typeof(UInt4).FullName}.{nameof(UInt4.Zero)}"] = "(uint4)0", - [$"{typeof(UInt4).FullName}.{nameof(UInt4.One)}"] = "uint4(1u, 1u, 1u, 1u)", - [$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitX)}"] = "uint4(1u, 0u, 0u, 0u)", - [$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitY)}"] = "uint4(0u, 1u, 0u, 0u)", - [$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitZ)}"] = "uint4(0u, 0u, 1u, 0u)", - [$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitW)}"] = "uint4(0u, 0u, 0u, 1u)", - - [$"{typeof(Float2).FullName}.{nameof(Float2.Zero)}"] = "(float2)0", - [$"{typeof(Float2).FullName}.{nameof(Float2.One)}"] = "float2(1.0f, 1.0f)", - [$"{typeof(Float2).FullName}.{nameof(Float2.UnitX)}"] = "float2(1.0f, 0.0f)", - [$"{typeof(Float2).FullName}.{nameof(Float2.UnitY)}"] = "float2(0.0f, 1.0f)", - - [$"{typeof(Float3).FullName}.{nameof(Float3.Zero)}"] = "(float3)0", - [$"{typeof(Float3).FullName}.{nameof(Float3.One)}"] = "float3(1.0f, 1.0f, 1.0f)", - [$"{typeof(Float3).FullName}.{nameof(Float3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)", - [$"{typeof(Float3).FullName}.{nameof(Float3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)", - [$"{typeof(Float3).FullName}.{nameof(Float3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)", - - [$"{typeof(Float4).FullName}.{nameof(Float4.Zero)}"] = "(float4)0", - [$"{typeof(Float4).FullName}.{nameof(Float4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)", - [$"{typeof(Float4).FullName}.{nameof(Float4.UnitX)}"] = "float4(1.0f, 0.0f, 0.0f, 0.0f)", - [$"{typeof(Float4).FullName}.{nameof(Float4.UnitY)}"] = "float4(0.0f, 1.0f, 0.0f, 0.0f)", - [$"{typeof(Float4).FullName}.{nameof(Float4.UnitZ)}"] = "float4(0.0f, 0.0f, 1.0f, 0.0f)", - [$"{typeof(Float4).FullName}.{nameof(Float4.UnitW)}"] = "float4(0.0f, 0.0f, 0.0f, 1.0f)", - - [$"{typeof(Double2).FullName}.{nameof(Double2.Zero)}"] = "(double2)0", - [$"{typeof(Double2).FullName}.{nameof(Double2.One)}"] = "double2(1.0, 1.0)", - [$"{typeof(Double2).FullName}.{nameof(Double2.UnitX)}"] = "double2(1.0, 0.0)", - [$"{typeof(Double2).FullName}.{nameof(Double2.UnitY)}"] = "double2(0.0, 1.0)", - - [$"{typeof(Double3).FullName}.{nameof(Double3.Zero)}"] = "(double3)0", - [$"{typeof(Double3).FullName}.{nameof(Double3.One)}"] = "double3(1.0, 1.0, 1.0)", - [$"{typeof(Double3).FullName}.{nameof(Double3.UnitX)}"] = "double3(1.0, 0.0, 0.0)", - [$"{typeof(Double3).FullName}.{nameof(Double3.UnitY)}"] = "double3(0.0, 1.0, 0.0)", - [$"{typeof(Double3).FullName}.{nameof(Double3.UnitZ)}"] = "double3(0.0, 0.0, 1.0)", - - [$"{typeof(Double4).FullName}.{nameof(Double4.Zero)}"] = "(double4)0", - [$"{typeof(Double4).FullName}.{nameof(Double4.One)}"] = "double4(1.0, 1.0, 1.0, 1.0)", - [$"{typeof(Double4).FullName}.{nameof(Double4.UnitX)}"] = "double4(1.0, 0.0, 0.0, 0.0)", - [$"{typeof(Double4).FullName}.{nameof(Double4.UnitY)}"] = "double4(0.0, 1.0, 0.0, 0.0)", - [$"{typeof(Double4).FullName}.{nameof(Double4.UnitZ)}"] = "double4(0.0, 0.0, 1.0, 0.0)", - [$"{typeof(Double4).FullName}.{nameof(Double4.UnitW)}"] = "double4(0.0, 0.0, 0.0, 1.0)" - }; - - // Programmatically load mappings for the instance members of the HLSL vector types - foreach (var item in - from type in HlslKnownTypes.KnownVectorTypes - from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public) - select (Type: type, Property: property)) - { - knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}"); - } - - // Load mappings for the matrix properties as well - foreach (var item in - from type in HlslKnownTypes.KnownMatrixTypes - from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public) - where Regex.IsMatch(property.Name, "^M[1-4]{2}$") - select (Type: type, Property: property)) - { - char row = (char)(item.Property.Name[1] - 1); - char column = (char)(item.Property.Name[2] - 1); - - knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"_m{row}{column}"); - } - // Store GroupIds.Index for a quicker comparison afterwards PropertyInfo groupindexProperty = typeof(GroupIds).GetProperty(nameof(GroupIds.Index), BindingFlags.Static | BindingFlags.Public); @@ -265,16 +114,16 @@ internal static class HlslKnownMembers break; case string name when name.Length == 2: { - string numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})"; - string denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})"; + string numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})"; + string denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})"; knownMembers.Add(key, $"{numerator} / {denominator}"); break; } case string name when name.Length == 3: { - string numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})"; - string denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})"; + string numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})"; + string denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})"; knownMembers.Add(key, $"{numerator} / {denominator}"); break; @@ -325,75 +174,6 @@ internal static class HlslKnownMembers break; } } - - return knownMembers; - } - - /// - /// The mapping of supported known members to HLSL names. - /// - private static readonly IReadOnlyCollection KnownMatrixIndexers = BuildKnownMatrixIndexers(); - - /// - /// Builds the mapping of swizzled matrix indexer properties. - /// - private static IReadOnlyCollection BuildKnownMatrixIndexers() - { - return ( - 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() - let indices = property.GetIndexParameters() - 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(); - } - - /// - /// 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() - { - return ( - from name in Enum.GetNames(typeof(MatrixIndex)) - select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet(); - } - - /// - /// Checks whether or not a given property fullname matches a matrix swizzled indexer. - /// - /// The input fully qualified member name. - /// Whether or not is a matrix swizzled indexer. - public static bool IsKnownMatrixIndexer(string name) - { - return KnownMatrixIndexers.Contains(name); - } - - /// - /// Checks whether or not a given property fullname matches a matrix swizzled index. - /// - /// The input fully qualified member name. - /// Whether or not is a matrix swizzled index. - public static bool IsKnownMatrixIndex(string name) - { - return KnownMatrixIndices.Contains(name); - } - - /// - /// Tries to get the mapped HLSL-compatible member name for the input member name. - /// - /// The input fully qualified member name. - /// The mapped name, if one is found. - /// The HLSL-compatible member name that can be used in an HLSL shader. - public static bool TryGetMappedName(string name, out string? mapped) - { - return KnownMembers.TryGetValue(name, out mapped); } /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index 6de039b6..357e0d11 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -6,7 +6,6 @@ using System.Linq; 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.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 588f84c3..f91c34aa 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -6,7 +6,6 @@ using System.Linq; 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.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 2e1d8578..ff98da30 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -5,7 +5,6 @@ using System.Globalization; 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;