Finished support for mapping type names from SG
このコミットが含まれているのは:
1 親
d0bfcefc79
コミット
0723c04b59
5ファイル変更
+46
-113
検索条件に一致するファイルはありません
@@ -26,7 +26,7 @@ namespace ComputeSharp
|
||||
public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods, string[] types)
|
||||
{
|
||||
ShaderTypeName = shaderTypeName;
|
||||
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => arg[1]);
|
||||
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
|
||||
Methods = methods.Cast<string[]>().ToDictionary(static method => method[0], static method => method[1]);
|
||||
Types = types;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ namespace ComputeSharp
|
||||
/// <summary>
|
||||
/// Gets the mapping of field names.
|
||||
/// </summary>
|
||||
internal IReadOnlyDictionary<string, string> Fields { get; }
|
||||
internal IReadOnlyDictionary<string, (string Name, string Type)> Fields { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapping of methods and their source code.
|
||||
|
||||
@@ -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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 &&
|
||||
|
||||
@@ -102,16 +102,18 @@ namespace ComputeSharp.SourceGenerators
|
||||
{
|
||||
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
|
||||
{
|
||||
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
|
||||
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
|
||||
|
||||
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
|
||||
|
||||
// Yield back the current mapping for the name (if the name used a reserved keyword)
|
||||
yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name };
|
||||
yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName };
|
||||
|
||||
// Track the type of items in the current buffer
|
||||
if (fieldSymbol.Type is INamedTypeSymbol fieldType &&
|
||||
HlslKnownTypes.IsStructuredBufferType(fieldType.GetFullMetadataName()))
|
||||
if (HlslKnownTypes.IsStructuredBufferType(typeSymbol.GetFullMetadataName()))
|
||||
{
|
||||
types.Add((INamedTypeSymbol)fieldType.TypeArguments[0]);
|
||||
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
[typeof(Double2).FullName] = "double2",
|
||||
[typeof(Double3).FullName] = "double3",
|
||||
[typeof(Double4).FullName] = "double4",
|
||||
[typeof(ThreadIds).FullName] = "uint3",
|
||||
[typeof(ThreadIds).FullName] = "uint3"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -68,6 +68,38 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
typeName == "ComputeSharp.ReadWriteBuffer`1";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type symbol.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The input type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
public static string GetMappedName(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
string typeName = typeSymbol.GetFullMetadataName();
|
||||
|
||||
// Special case for the structured buffer types
|
||||
if (IsStructuredBufferType(typeName))
|
||||
{
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName();
|
||||
|
||||
// If the current type is a custom type, format it as needed
|
||||
if (!KnownTypes.TryGetValue(genericArgumentName, out string? mapped))
|
||||
{
|
||||
mapped = genericArgumentName.Replace(".", "__");
|
||||
}
|
||||
|
||||
// Construct the HLSL type name
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>",
|
||||
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>",
|
||||
_ => throw new ArgumentException()
|
||||
};
|
||||
}
|
||||
|
||||
return KnownTypes[typeName];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type name.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする