diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index 940fc21d..f3a3ae7e 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -40,15 +40,19 @@ namespace ComputeSharp.Shaders.Extensions /// /// The input to check and modify if needed /// The to use to load symbols for the input node + /// The info on parsed static fields, if any /// A instance that is compatible with HLSL [Pure] - public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel) + public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string name, FieldInfo FieldInfo)? variable) { SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression); SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name); ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault(); + // Set the variable to null, replace it later on if needed + variable = null; + // If the input member has no symbol or is not a field, property or method, just return it if (memberSymbol is null || memberSymbol.Kind != SymbolKind.Field && @@ -73,10 +77,11 @@ namespace ComputeSharp.Shaders.Extensions bool isReadonly; object memberValue; Type memberType; + FieldInfo? fieldInfo; switch (memberSymbol.Kind) { case SymbolKind.Field: - FieldInfo fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); isReadonly = fieldInfo.IsInitOnly; memberValue = fieldInfo.GetValue(null); memberType = fieldInfo.FieldType; @@ -86,6 +91,7 @@ namespace ComputeSharp.Shaders.Extensions isReadonly = !propertyInfo.CanWrite; memberValue = propertyInfo.GetValue(null); memberType = propertyInfo.PropertyType; + fieldInfo = null; break; default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}"); } @@ -101,6 +107,14 @@ namespace ComputeSharp.Shaders.Extensions _ => throw new InvalidOperationException($"Invalid field of type {memberType}") }; } + + // Captured variable, treat it like any other captured variable in the closure + if (!isReadonly && fieldInfo != null) + { + string name = $"{containingMemberSymbolInfo.Symbol.Name}_{fieldInfo.Name}"; + variable = (name, fieldInfo); + return SyntaxFactory.IdentifierName(name); + } } // Process the input node if it's a known method invocation diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 0d456606..6c1649d3 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -187,6 +187,14 @@ namespace ComputeSharp.Shaders.Translation ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel); root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root); + // Register the captured static fields + foreach (var item in syntaxRewriter.StaticFields) + { + _VariableFields.Add(item.FieldInfo); + string typeName = HlslKnownTypes.GetMappedName(item.FieldInfo.FieldType); + _FieldsList.Add(new CapturedFieldInfo(item.FieldInfo.FieldType, typeName, item.Name)); + } + // Get the thread ids identifier name and shader method body ThreadsIdsVariableName = root.ParameterList.Parameters.First().Identifier.Text; MethodBody = root.Body.ToFullString(); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index b2d1e121..477fe647 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -1,4 +1,6 @@ -using ComputeSharp.Shaders.Extensions; +using System.Collections.Generic; +using System.Reflection; +using ComputeSharp.Shaders.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -21,6 +23,13 @@ namespace ComputeSharp.Shaders.Translation /// public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel; + private readonly List<(string, FieldInfo)> _StaticFields = new List<(string, FieldInfo)>(); + + /// + /// Gets the collection of captured static fields used by the target code + /// + public IReadOnlyList<(string Name, FieldInfo FieldInfo)> StaticFields => _StaticFields; + /// public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) { @@ -89,8 +98,12 @@ namespace ComputeSharp.Shaders.Translation public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node); + SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable); - return node.ReplaceMember(SemanticModel); + // Register the captured member, if any + if (variable.HasValue) _StaticFields.Add(variable.Value); + + return syntaxNode; } } }