From befdd71fbf35b943b72abd528ada321bb85c16c7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 Nov 2020 18:04:55 +0100 Subject: [PATCH] Fixed ordering of custom struct declarations --- .../IComputeShaderSourceAttributeGenerator.cs | 2 +- .../Mappings/HlslKnownTypes.cs | 57 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 03c7dcb7..c8abc15a 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -176,7 +176,7 @@ namespace ComputeSharp.SourceGenerators foreach (var type in HlslKnownTypes.GetCustomTypes(types)) { var structType = type.GetFullMetadataName().Replace(".", "__"); - var structDeclaration = StructDeclaration(structType).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + var structDeclaration = StructDeclaration(structType); // Declare the fields of the current type foreach (var field in type.GetMembers().OfType()) diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 9e5eca2a..1f3814fe 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Numerics; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; @@ -105,7 +106,61 @@ namespace ComputeSharp.SourceGenerators.Mappings ExploreTypes(type, customTypes); } - return customTypes; + return OrderByDependency(customTypes); + } + + /// + /// Orders the input sequence of types so that they can be declared in HLSL successfully. + /// + /// The input collection of types to declare. + /// The same list as input, but in a valid HLSL declaration order. + [Pure] + private static IEnumerable OrderByDependency(IEnumerable types) + { + Queue<(INamedTypeSymbol Type, HashSet Fields)> queue = new(); + + // Build a mapping of type dependencies for all the captured types. A type depends on another + // when the latter is a field in the first type. HLSL requires custom types to be declared in + // order of usage, so we need to ensure that types are declared in an order that guarantees + // that no type will be referenced before being defined. To do so, we can create a mapping of + // types and their dependencies, and iteratively remove items from the map when they have no + // dependencies left. When one type is processed and removed, it is also removed from the list + // of dependencies of all other remaining types in the map, until there is none left. + foreach (var type in types) + { + HashSet dependencies = new(SymbolEqualityComparer.Default); + + // Only add other custom types as dependencies, and ignore HLSL types + foreach (var field in type.GetMembers().OfType()) + { + INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type; + + if (!KnownTypes.ContainsKey(fieldType.GetFullMetadataName())) + { + _ = dependencies.Add(fieldType); + } + } + + queue.Enqueue((type, dependencies)); + } + + while (queue.Count > 0) + { + var entry = queue.Dequeue(); + + // No dependencies left, we can declare this type + if (entry.Fields.Count == 0) + { + // Remove the current type from dependencies of others + foreach (var pair in queue) + { + _ = pair.Fields.Remove(entry.Type); + } + + yield return entry.Type; + } + else queue.Enqueue(entry); + } } } }