From 0aeb912184f88d6e2fed5ab51f5ab2415415cdf2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 28 Dec 2020 17:44:47 +0100 Subject: [PATCH] Implemented hash logic for captured fields --- .../IComputeShaderHashCodeGenerator.cs | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index 7cf539b8..11c178f6 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -1,7 +1,10 @@ -using System.Collections.Immutable; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics.Contracts; using System.Linq; using System.Text; using ComputeSharp.SourceGenerators.Extensions; +using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -36,7 +39,41 @@ namespace ComputeSharp.SourceGenerators if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); - BlockSyntax block = Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, ParseToken("0")))); + + BlockSyntax block = Block(); + + // Process all the captured delegate types + foreach (var fieldName in GetDelegateMemberNames(structDeclarationSymbol)) + { + // hash += hash << 5; + block = block.AddStatements(ExpressionStatement( + AssignmentExpression( + SyntaxKind.AddAssignmentExpression, + IdentifierName("hash"), + BinaryExpression(SyntaxKind.LeftShiftExpression, + IdentifierName("hash"), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(5)))))); + + // hash += shader.Field[#i].Method.GetHashCode(); + block = block.AddStatements(ExpressionStatement( + AssignmentExpression( + SyntaxKind.AddAssignmentExpression, + IdentifierName("hash"), + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("shader"), + IdentifierName(fieldName)), + IdentifierName("Method")), + IdentifierName("GetHashCode")))))); + } + + // return hash; + block = block.AddStatements(ReturnStatement(IdentifierName("hash"))); // Create a static method to create the combined hashcode for a given shader type. // This code takes a block syntax and produces a compilation unit as follows: @@ -88,6 +125,26 @@ namespace ComputeSharp.SourceGenerators context.AddSource(structDeclarationSymbol.GetGeneratedFileName(), SourceText.From(source, Encoding.UTF8)); } } + + /// + /// Gets a sequence of captured delegate fields to process. + /// + /// The input instance to process. + /// A sequence of captured delegate members in . + [Pure] + private static IEnumerable GetDelegateMemberNames(INamedTypeSymbol structDeclarationSymbol) + { + foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) + { + INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; + + if (typeSymbol.TypeKind != TypeKind.Delegate) continue; + + _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); + + yield return mapping ?? fieldSymbol.Name; + } + } } }