Finished support for mapping type names from SG

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-29 23:29:47 +01:00
1 親 d0bfcefc79
コミット 0723c04b59
5ファイル変更
+46 -113

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

-86
ファイルの表示
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Text.RegularExpressions;
namespace ComputeSharp.Shaders.Mappings
{
@@ -11,40 +10,6 @@ namespace ComputeSharp.Shaders.Mappings
/// </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(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",
[typeof(ThreadIds).FullName!] = "uint3",
[typeof(ReadOnlyBuffer<>).FullName!] = "StructuredBuffer",
[typeof(ReadWriteBuffer<>).FullName!] = "RWStructuredBuffer"
};
private static HashSet<Type> _HlslMappedVectorTypes { get; } = new HashSet<Type>(new[]
{
typeof(Bool2), typeof(Bool3), typeof(Bool4),
@@ -54,23 +19,6 @@ namespace ComputeSharp.Shaders.Mappings
typeof(Double2), typeof(Double3), typeof(Double4)
});
/// <summary>
/// Gets the known HLSL vector types available as mapped types
/// </summary>
public static IReadOnlyCollection<Type> HlslMappedVectorTypes => _HlslMappedVectorTypes;
/// <summary>
/// Checks whether or not the input type is a known HLSL-compatible 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 a known HLSL type</returns>
[Pure]
public static bool IsKnownType(Type type) => IsKnownScalarType(type) ||
IsKnownVectorType(type) ||
IsConstantBufferType(type) ||
IsReadOnlyBufferType(type) ||
IsReadWriteBufferType(type);
/// <summary>
/// Checks whether or not the input type is a known scalar type
/// </summary>
@@ -131,39 +79,5 @@ namespace ComputeSharp.Shaders.Mappings
[Pure]
public static bool IsReadWriteBufferType(Type type) => type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>);
/// <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.IsArray ? type.GetElementType()! : type;
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}";
}
}
}
+5 -20
ファイルの表示
@@ -176,7 +176,7 @@ namespace ComputeSharp.Shaders.Translation
private void LoadFieldInfo(object shader, ReadableMember memberInfo)
{
Type fieldType = memberInfo.MemberType;
string fieldName = Attribute.Fields[memberInfo.Name];
(string hlslName, string hlslType) = Attribute.Fields[memberInfo.Name];
// Constant buffer
if (HlslKnownTypes.IsConstantBufferType(fieldType))
@@ -184,12 +184,8 @@ namespace ComputeSharp.Shaders.Translation
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedMembers.Add(memberInfo);
string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]);
this.hlslBuffersInfo.Add(new HlslBufferInfo.Constant(typeName, fieldName, (int)this.constantBuffersCount++));
this.hlslBuffersInfo.Add(new HlslBufferInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++));
}
else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType))
{
@@ -197,12 +193,8 @@ namespace ComputeSharp.Shaders.Translation
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedMembers.Add(memberInfo);
string typeName = HlslKnownTypes.GetMappedName(fieldType);
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadOnly(typeName, fieldName, (int)this.readOnlyBuffersCount++));
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++));
}
else if (HlslKnownTypes.IsReadWriteBufferType(fieldType))
{
@@ -210,20 +202,13 @@ namespace ComputeSharp.Shaders.Translation
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount);
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
this.capturedMembers.Add(memberInfo);
string typeName = HlslKnownTypes.GetMappedName(fieldType);
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadWrite(typeName, fieldName, (int)this.readWriteBuffersCount++));
this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++));
}
else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType))
{
this.capturedMembers.Add(memberInfo);
string typeName = HlslKnownTypes.GetMappedName(fieldType);
this.fieldsInfo.Add(new CapturedFieldInfo(typeName, fieldName));
this.fieldsInfo.Add(new CapturedFieldInfo(hlslType, hlslName));
}
else if (fieldType.IsDelegate() &&
memberInfo.GetValue(shader) is Delegate func &&