Refactored and moved HlslKnownTypes class

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-11 16:36:24 +02:00
1 親 17cad72dbb
コミット 84e6ba2713
3ファイル変更
+85 -59

検索条件に一致するファイルはありません

+81
ファイルの表示
@@ -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
{
/// <summary>
/// A <see langword="class"/> that contains and maps known HLSL type names to common .NET types
/// </summary>
internal static class HlslKnownTypes
{
/// <summary>
/// The mapping of supported known types to HLSL types
/// </summary>
private static readonly IReadOnlyDictionary<string, string> KnownTypes = new Dictionary<string, string>
{
[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"
};
/// <summary>
/// Checks whether or not a given <see cref="Type"/> is an HLSL known type
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact an HLSL known type</returns>
[Pure]
public static bool IsKnownType(Type type)
{
Type declaredType = type.GetElementOrDeclaredType();
string fullname = $"{declaredType.Namespace}{Type.Delimiter}{declaredType.Name}";
return KnownTypes.ContainsKey(fullname);
}
/// <summary>
/// Gets the mapped HLSL-compatible type name for the input <see cref="Type"/> instance
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to map</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader</returns>
[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;
}
/// <summary>
/// Gets the mapped HLSL-compatible type name for the input type name
/// </summary>
/// <param name="name">The input type name to map</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader</returns>
[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}";
}
}
}
+2 -58
ファイルの表示
@@ -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<string, string> knownTypes = new Dictionary<string, string>()
{
{ 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<string, string> knownMethods = new Dictionary<string, string>()
+2 -1
ファイルの表示
@@ -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<TRoot>(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())
{