From 84e6ba27138586216fa0e1b11dcbb1005735e2a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 11 Aug 2019 16:36:24 +0200 Subject: [PATCH] Refactored and moved HlslKnownTypes class --- .../Mappings/HlslKnownTypes.cs | 81 +++++++++++++++++++ .../ShaderGenerator.cs | 60 +------------- .../SyntaxNodeExtensions.cs | 3 +- 3 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs diff --git a/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs b/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs new file mode 100644 index 00000000..25a7f9bf --- /dev/null +++ b/src/DirectX12GameEngine.Shaders/Mappings/HlslKnownTypes.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Numerics; +using System.Text.RegularExpressions; +using DirectX12GameEngine.Shaders.Primitives; + +namespace DirectX12GameEngine.Shaders.Mappings +{ + /// + /// A that contains and maps known HLSL type names to common .NET types + /// + internal static class HlslKnownTypes + { + /// + /// The mapping of supported known types to HLSL types + /// + private static readonly IReadOnlyDictionary KnownTypes = new Dictionary + { + [typeof(bool).FullName] = "bool", + [typeof(uint).FullName] = "uint", + [typeof(int).FullName] = "int", + [typeof(double).FullName] = "double", + [typeof(float).FullName] = "float", + [typeof(Vector2).FullName] = "float2", + [typeof(Vector3).FullName] = "float3", + [typeof(Vector4).FullName] = "float4", + [typeof(Matrix4x4).FullName] = "float4x4", + [typeof(ThreadIds).FullName] = "uint3", + [typeof(RWBufferResource<>).FullName] = "RWBuffer" + }; + + /// + /// Checks whether or not a given is an HLSL known type + /// + /// The input instance to check + /// A indicating whether the input is in fact an HLSL known type + [Pure] + public static bool IsKnownType(Type type) + { + Type declaredType = type.GetElementOrDeclaredType(); + string fullname = $"{declaredType.Namespace}{Type.Delimiter}{declaredType.Name}"; + + return KnownTypes.ContainsKey(fullname); + } + + /// + /// Gets the mapped HLSL-compatible type name for the input instance + /// + /// The input instance to map + /// The HLSL-compatible type name that can be used in an HLSL shader + [Pure] + public static string GetMappedName(Type type) + { + Type declaredType = type.GetElementOrDeclaredType(); + string + fullname = $"{declaredType.Namespace}{Type.Delimiter}{declaredType.Name}", + mappedName = KnownTypes.TryGetValue(fullname, out string mapped) ? mapped : declaredType.Name, + hlslName = declaredType.IsGenericType ? mappedName + $"<{GetMappedName(declaredType.GetGenericArguments()[0])}>" : mappedName; + + return hlslName; + } + + /// + /// Gets the mapped HLSL-compatible type name for the input type name + /// + /// The input type name to map + /// The HLSL-compatible type name that can be used in an HLSL shader + [Pure] + public static string GetMappedName(string name) + { + int indexOfOpenBracket = name.IndexOf('<'); + string + genericArguments = indexOfOpenBracket >= 0 ? name.Substring(indexOfOpenBracket) : string.Empty, + polishedName = indexOfOpenBracket >= 0 ? name.Remove(indexOfOpenBracket) + "`1" : name, + mappedName = KnownTypes.TryGetValue(polishedName, out string mapped) ? mapped : Regex.Match(polishedName, @"[^\.]+$").Value; + + return $"{mappedName}{genericArguments}"; + } + } +} diff --git a/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs b/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs index 1bff7f5f..5660b72d 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Reflection.Metadata; using System.Text.RegularExpressions; using DirectX12GameEngine.Graphics.Buffers.Abstract; +using DirectX12GameEngine.Shaders.Mappings; using DirectX12GameEngine.Shaders.Primitives; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; @@ -163,7 +164,7 @@ namespace DirectX12GameEngine.Shaders { type = type.GetElementOrDeclaredType(); - if (HlslKnownTypes.ContainsKey(type) || collectedTypes.Any(d => d.Type == type)) return; + if (HlslKnownTypes.IsKnownType(type) || collectedTypes.Any(d => d.Type == type)) return; ShaderTypeDefinition shaderTypeDefinition = new ShaderTypeDefinition(type, obj); @@ -485,63 +486,6 @@ namespace DirectX12GameEngine.Shaders return new CSharpDecompiler(assemblyPath, resolver, decompilerSettings); } - internal static class HlslKnownTypes - { - private static readonly Dictionary knownTypes = new Dictionary() - { - { typeof(void).FullName, "void" }, - { typeof(bool).FullName, "bool" }, - { typeof(uint).FullName, "uint" }, - { typeof(int).FullName, "int" }, - { typeof(double).FullName, "double" }, - { typeof(float).FullName, "float" }, - { typeof(Vector2).FullName, "float2" }, - { typeof(Vector3).FullName, "float3" }, - { typeof(Vector4).FullName, "float4" }, - { typeof(Matrix4x4).FullName, "float4x4" }, - { typeof(ThreadIds).FullName, "uint3" }, - { typeof(RWBufferResource<>).FullName, "RWBuffer" }, - }; - - public static bool ContainsKey(Type type) - { - type = type.GetElementOrDeclaredType(); - string typeFullName = type.Namespace + Type.Delimiter + type.Name; - - return knownTypes.ContainsKey(typeFullName); - } - - public static bool ContainsKey(string name) - { - int indexOfOpenBracket = name.IndexOf('<'); - name = indexOfOpenBracket >= 0 ? name.Remove(indexOfOpenBracket) + "`1" : name; - - return knownTypes.ContainsKey(name); - } - - public static string GetMappedName(Type type) - { - type = type.GetElementOrDeclaredType(); - string typeFullName = type.Namespace + Type.Delimiter + type.Name; - - string mappedName = knownTypes.TryGetValue(typeFullName, out string mapped) ? mapped : type.Name; - - return type.IsGenericType ? mappedName + $"<{GetMappedName(type.GetGenericArguments()[0])}>" : mappedName; - } - - public static string GetMappedName(string name) - { - int indexOfOpenBracket = name.IndexOf('<'); - - string genericArguments = indexOfOpenBracket >= 0 ? name.Substring(indexOfOpenBracket) : ""; - name = indexOfOpenBracket >= 0 ? name.Remove(indexOfOpenBracket) + "`1" : name; - - string mappedName = knownTypes.TryGetValue(name, out string mapped) ? mapped : Regex.Match(name, @"[^\.]+$").Value; - - return mappedName + genericArguments; - } - } - internal static class HlslKnownMethods { private static readonly Dictionary knownMethods = new Dictionary() diff --git a/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs b/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs index beebe2e6..ba882bbb 100644 --- a/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs +++ b/src/DirectX12GameEngine.Shaders/SyntaxNodeExtensions.cs @@ -1,4 +1,5 @@ using System.Linq; +using DirectX12GameEngine.Shaders.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -9,7 +10,7 @@ namespace DirectX12GameEngine.Shaders { public static TRoot ReplaceType(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode { - string value = ShaderGenerator.HlslKnownTypes.GetMappedName(type.ToString()); + string value = HlslKnownTypes.GetMappedName(type.ToString()); if (value == type.ToString()) {