diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 4f513246..72af86c8 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -40,51 +40,7 @@ namespace ComputeSharp.SourceGenerators if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); - BlockSyntax block = Block(); - int - resourceOffset = 0, - rawDataOffset = sizeof(int) * 3; - - foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) - { - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; - string typeName = typeSymbol.GetFullMetadataName(); - - if (HlslKnownTypes.IsTypedResourceType(typeName)) - { - block = block.AddStatements(ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"), - ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)")))); - } - else if (HlslKnownTypes.IsScalarOrVectorType(typeName)) - { - var info = HlslKnownSizes.GetTypeInfo(typeName); - - // Calculate the right offset with 16-bytes padding (HLSL constant buffer). - // Since we're in a constant buffer, we need to both pad the starting offset - // to be aligned to the packing size of the type, and also to align the initial - // offset to ensure that values do not cross 16 bytes boundaries either. - rawDataOffset = AlignmentHelper.AlignToBoundary( - AlignmentHelper.Pad(rawDataOffset, info.Pack), - info.Size, - 16); - - block = block.AddStatements(ExpressionStatement( - ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})"))); - - rawDataOffset += info.Size; - } - } - - // After all the captured fields have been processed, ansure the reported byte size for - // the local variables is padded to the standard alignment for constant buffers. This is - // necessary to enable loading all the dispatch data after reinterpreting it to a sequence - // of values of size 16 bytes (in particular, Int4 is used), without reading out of bounds. - rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16); - - block = block.AddStatements(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset)))); + BlockSyntax block = Block(GetDispatchDataLoadingStatements(structDeclarationSymbol)); // 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: @@ -141,23 +97,57 @@ namespace ComputeSharp.SourceGenerators } /// - /// Gets a sequence of captured delegate fields to process. + /// Gets a sequence of statements to load the dispatch data for a given shader /// /// The input instance to process. - /// A sequence of captured delegate members in . + /// The sequence of instances to load shader dispatch data. [Pure] - private static IEnumerable GetDelegateMemberNames(INamedTypeSymbol structDeclarationSymbol) + private static IEnumerable GetDispatchDataLoadingStatements(INamedTypeSymbol structDeclarationSymbol) { + int + resourceOffset = 0, + rawDataOffset = sizeof(int) * 3; + foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; + string typeName = typeSymbol.GetFullMetadataName(); - if (typeSymbol.TypeKind != TypeKind.Delegate) continue; + if (HlslKnownTypes.IsTypedResourceType(typeName)) + { + yield return ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + ParseExpression($"Unsafe.Add(ref r0, {resourceOffset++})"), + ParseExpression($"GraphicsResourceHelper.ValidateAndGetGpuDescriptorHandle(shader.{fieldSymbol.Name}, device)"))); + } + else if (HlslKnownTypes.IsScalarOrVectorType(typeName)) + { + var (fieldSize, fieldPack) = HlslKnownSizes.GetTypeInfo(typeName); - _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); + // Calculate the right offset with 16-bytes padding (HLSL constant buffer). + // Since we're in a constant buffer, we need to both pad the starting offset + // to be aligned to the packing size of the type, and also to align the initial + // offset to ensure that values do not cross 16 bytes boundaries either. + rawDataOffset = AlignmentHelper.AlignToBoundary( + AlignmentHelper.Pad(rawDataOffset, fieldPack), + fieldSize, + 16); - yield return mapping ?? fieldSymbol.Name; + yield return ExpressionStatement( + ParseExpression($"Unsafe.WriteUnaligned(ref Unsafe.Add(ref r1, {rawDataOffset}), shader.{fieldSymbol.Name})")); + + rawDataOffset += fieldSize; + } } + + // After all the captured fields have been processed, ansure the reported byte size for + // the local variables is padded to the standard alignment for constant buffers. This is + // necessary to enable loading all the dispatch data after reinterpreting it to a sequence + // of values of size 16 bytes (in particular, Int4 is used), without reading out of bounds. + rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16); + + yield return ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset))); } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index bd2fbc6e..9fe81a0f 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -39,40 +39,7 @@ namespace ComputeSharp.SourceGenerators if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); - 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"))); + BlockSyntax block = Block(GetDelegateHashCodeStatements(structDeclarationSymbol)); // 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: @@ -126,12 +93,12 @@ namespace ComputeSharp.SourceGenerators } /// - /// Gets a sequence of captured delegate fields to process. + /// Gets a sequence of statements to process the hashcode of the captured delegates. /// /// The input instance to process. - /// A sequence of captured delegate members in . + /// The sequence of instances to hash the captured delegates. [Pure] - private static IEnumerable GetDelegateMemberNames(INamedTypeSymbol structDeclarationSymbol) + private static IEnumerable GetDelegateHashCodeStatements(INamedTypeSymbol structDeclarationSymbol) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { @@ -141,8 +108,16 @@ namespace ComputeSharp.SourceGenerators _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - yield return mapping ?? fieldSymbol.Name; + string fieldName = mapping ?? fieldSymbol.Name; + + // hash += hash << 5; + yield return ExpressionStatement(ParseExpression($"hash += hash << 5")); + + // hash += shader.Field[#i].Method.GetHashCode(); + yield return ExpressionStatement(ParseExpression($"hash += shader.{fieldName}.Method.GetHashCode()")); } + + yield return ReturnStatement(IdentifierName("hash")); } } }