From c208dc29b18baf5dfbcb1cb77db13d2f6c34c673 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 17:51:50 +0100 Subject: [PATCH] Enabled shader generation for texture indexer overloads --- .../Extensions/ISymbolExtensions.cs | 20 ++++++++++++++++ .../Mappings/HlslKnownMembers.cs | 23 +++++++++++++++++++ .../Mappings/HlslKnownTypes.cs | 2 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 16 +++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index d1581c61..e08f19e8 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -74,6 +74,26 @@ namespace ComputeSharp.SourceGenerators.Extensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat); } + /// + /// Gets the full metadata name for a given instance. + /// + /// The input instance. + /// The full metadata name for . + [Pure] + public static string GetFullMetadataName(this IPropertySymbol symbol) + { + string declaringTypeName = symbol.ContainingType.GetFullMetadataName(); + + if (symbol.IsIndexer) + { + var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName())); + + return $"{declaringTypeName}.this[{parameters}]"; + } + + return $"{declaringTypeName}.{symbol.Name}"; + } + /// /// Gets a valid filename for a target symbol and generator type. /// diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 2ac0c8bf..7a7df32b 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -12,6 +12,17 @@ namespace ComputeSharp.SourceGenerators.Mappings /// internal static class HlslKnownMembers { + /// + /// The mapping of supported known indexers to HLSL vector type names. + /// + private static readonly IReadOnlyDictionary KnownIndexers = new Dictionary + { + [$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" + }; + /// /// The mapping of supported known members to HLSL names. /// @@ -162,5 +173,17 @@ namespace ComputeSharp.SourceGenerators.Mappings { return KnownMembers.TryGetValue(name, out mapped); } + + /// + /// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name. + /// + /// The input fully qualified indexer name. + /// The mapped type name, if one is found. + /// The HLSL-compatible type name that can be used in an HLSL shader for the given indexer. + [Pure] + public static bool TryGetMappedIndexerTypeName(string name, out string? mapped) + { + return KnownIndexers.TryGetValue(name, out mapped); + } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 2d2b0f32..b4332b8e 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] = "int3" }; /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 3cc8a414..0c274f7a 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -342,6 +342,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters return updatedNode; } + /// + public override SyntaxNode? VisitElementAccessExpression(ElementAccessExpressionSyntax node) + { + var updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!; + + if (this.semanticModel.GetOperation(node) is IPropertyReferenceOperation operation && + HlslKnownMembers.TryGetMappedIndexerTypeName(operation.Property.GetFullMetadataName(), out string? mapping)) + { + var index = InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments)); + + return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index)))); + } + + return updatedNode; + } + /// public override SyntaxNode? VisitArgument(ArgumentSyntax node) {