diff --git a/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs index 0e7a9ce8..a6752761 100644 --- a/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs +++ b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs @@ -26,7 +26,7 @@ namespace ComputeSharp public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods, string[] types) { ShaderTypeName = shaderTypeName; - Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => arg[1]); + Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2])); Methods = methods.Cast().ToDictionary(static method => method[0], static method => method[1]); Types = types; } @@ -39,7 +39,7 @@ namespace ComputeSharp /// /// Gets the mapping of field names. /// - internal IReadOnlyDictionary Fields { get; } + internal IReadOnlyDictionary Fields { get; } /// /// Gets the mapping of methods and their source code. diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index 9cf7fb83..0887e8fb 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -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 /// 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(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 _HlslMappedVectorTypes { get; } = new HashSet(new[] { typeof(Bool2), typeof(Bool3), typeof(Bool4), @@ -54,23 +19,6 @@ namespace ComputeSharp.Shaders.Mappings typeof(Double2), typeof(Double3), typeof(Double4) }); - /// - /// Gets the known HLSL vector types available as mapped types - /// - public static IReadOnlyCollection HlslMappedVectorTypes => _HlslMappedVectorTypes; - - /// - /// Checks whether or not the input type is a known HLSL-compatible type - /// - /// The input instance to check - /// A indicating whether the input is in fact a known HLSL type - [Pure] - public static bool IsKnownType(Type type) => IsKnownScalarType(type) || - IsKnownVectorType(type) || - IsConstantBufferType(type) || - IsReadOnlyBufferType(type) || - IsReadWriteBufferType(type); - /// /// Checks whether or not the input type is a known scalar type /// @@ -131,39 +79,5 @@ namespace ComputeSharp.Shaders.Mappings [Pure] public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>); - - /// - /// 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.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; - } - - /// - /// 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/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index edaa246c..221deb27 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -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 && diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 24bd33b5..6ec864fa 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -102,16 +102,18 @@ namespace ComputeSharp.SourceGenerators { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { + 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]); } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 1f3814fe..6969ffc1 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -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" }; /// @@ -68,6 +68,38 @@ namespace ComputeSharp.SourceGenerators.Mappings typeName == "ComputeSharp.ReadWriteBuffer`1"; } + /// + /// Gets the mapped HLSL-compatible type name for the input type symbol. + /// + /// The input type to map. + /// The HLSL-compatible type name that can be used in an HLSL shader. + 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]; + } + /// /// Gets the mapped HLSL-compatible type name for the input type name. ///