From 1a8e653a39a04ea908301dcc2c3e250745c410cb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 Nov 2020 16:23:47 +0100 Subject: [PATCH] Enabled discovered types generation in shaders --- .../Attributes/IComputeShaderSource.cs | 13 +++++++++--- .../Renderer/HlslShaderRenderer.cs | 11 ++++++++-- .../Renderer/Models/ShaderInfo.cs | 9 ++++++-- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 3 ++- .../Translation/ShaderLoader{T}.cs | 17 +++++++++++---- .../IComputeShaderSourceAttributeGenerator.cs | 21 ++++++++++++++----- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs index 7d172755..0e7a9ce8 100644 --- a/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs +++ b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs @@ -20,13 +20,15 @@ namespace ComputeSharp /// Creates a new instance with the specified parameters. /// /// The fully qualified name of the shader type. - /// The name of the current method. - /// The source code of the target method. - public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods) + /// The mapped collection of shader fields. + /// The mapped collection of shader methods. + /// The collection of custom types. + public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods, string[] types) { ShaderTypeName = shaderTypeName; Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => arg[1]); Methods = methods.Cast().ToDictionary(static method => method[0], static method => method[1]); + Types = types; } /// @@ -44,6 +46,11 @@ namespace ComputeSharp /// internal IReadOnlyDictionary Methods { get; } + /// + /// Gets the collection of processed custom types. + /// + internal IReadOnlyCollection Types { get; } + /// /// Gets the associated instance for a specified type. /// diff --git a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs index 7c3be112..3f95cc37 100644 --- a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs @@ -27,10 +27,17 @@ namespace ComputeSharp.Shaders.Renderer builder.AppendLine("// AUTO GENERATED"); builder.AppendLine("// ================================================"); builder.AppendLine("// This shader was created by ComputeSharp."); - builder.AppendLine("// See: https://github.com/Sergio0694/ComputeSharp."); - builder.AppendLine(); + builder.AppendLine("// See: https://github.com/Sergio0694/ComputeSharp."); + + // Declared types + foreach (var type in info.DeclaredTypes) + { + builder.AppendLine(); + builder.AppendLine(type); + } // Captured variables + builder.AppendLine(); builder.AppendLine("cbuffer _ : register(b0)"); builder.AppendLine('{'); builder.AppendLine(" uint __x;"); diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 07e3bbb2..3ae877df 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -47,11 +47,16 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets or sets the list of static functions used by the shader. /// - public IReadOnlyList FunctionsList { get; init; } // TODO + public IReadOnlyCollection FunctionsList { get; init; } // TODO /// /// Gets or sets the list of local functions used by the shader. /// - public IReadOnlyList LocalFunctionsList { get; init; } + public IReadOnlyCollection LocalFunctionsList { get; init; } + + /// + /// Gets or sets the list of dependent types for the shader. + /// + public IReadOnlyCollection DeclaredTypes { get; init; } } } diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index e24a17be..81acf440 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -178,7 +178,8 @@ namespace ComputeSharp.Shaders ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, EntryPoint = shaderLoader.EntryPoint, FunctionsList = shaderLoader.FunctionsInfo, - LocalFunctionsList = shaderLoader.LocalFunctionsInfo + LocalFunctionsList = shaderLoader.LocalFunctionsInfo, + DeclaredTypes = shaderLoader.DeclaredTypes }); // Compile the loaded shader to HLSL bytecode diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 18bd856f..edaa246c 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -110,7 +110,12 @@ namespace ComputeSharp.Shaders.Translation /// /// Gets the collection of items for the shader. /// - public IReadOnlyList LocalFunctionsInfo => this.localFunctionsInfo; + public IReadOnlyCollection LocalFunctionsInfo => this.localFunctionsInfo; + + /// + /// Gets the collection of declared types for the shader. + /// + public IReadOnlyCollection DeclaredTypes { get; private set; } /// /// Loads and processes an input shadeer @@ -128,7 +133,7 @@ namespace ComputeSharp.Shaders.Translation object box = shader; @this.LoadFieldsInfo(box); - @this.LoadMethodSource(); + @this.LoadMethodMetadata(); @this.BuildDispatchDataLoader(); return @this; @@ -234,10 +239,11 @@ namespace ComputeSharp.Shaders.Translation } /// - /// Loads the entry method for the current shader being loaded + /// Loads the metadata info for the current shader. /// - private void LoadMethodSource() + private void LoadMethodMetadata() { + // Extract the method sources foreach (var method in Attribute.Methods) { switch (method.Key) @@ -246,6 +252,9 @@ namespace ComputeSharp.Shaders.Translation default: this.localFunctionsInfo.Add(method.Value); break; } } + + // Extract the dependent types + DeclaredTypes = Attribute.Types; } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 1446f39c..54c57acf 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -39,7 +39,7 @@ namespace ComputeSharp.SourceGenerators INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!; // Only process compute shader types - if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; + if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; // The list to use to track all discovered custom types HashSet discoveredTypes = new(SymbolEqualityComparer.Default); @@ -54,12 +54,22 @@ namespace ComputeSharp.SourceGenerators ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))) .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) - .AddExpressions(pairs.Select(pair => + .AddExpressions(pairs.Select(static pair => ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions( LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Key)), LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Value))))).ToArray())); } + // Helper that converts a sequence of strings into an array expression. + // That is, this applies the following transformation: + // - { "S1", "S2" } => new[] { "S1", "S2" } + static ImplicitArrayCreationExpressionSyntax ImplicitArrayExpression(IEnumerable values) + { + return + ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions( + values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray())); + } + // Create the compilation unit with the source attribute var source = CompilationUnit().AddAttributeLists( @@ -67,7 +77,8 @@ namespace ComputeSharp.SourceGenerators Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))), AttributeArgument(NestedPairsArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())), - AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray()))))) + AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())), + AttributeArgument(ImplicitArrayExpression(GetProcessedTypes(discoveredTypes)))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace() @@ -160,7 +171,7 @@ namespace ComputeSharp.SourceGenerators /// /// The sequence of discovered custom types. /// A sequence of custom type definitions to add to the shader source. - public static IEnumerable GetProcessedTypes(IEnumerable types) + public static IEnumerable GetProcessedTypes(IEnumerable types) { foreach (var type in HlslKnownTypes.GetCustomTypes(types)) { @@ -187,7 +198,7 @@ namespace ComputeSharp.SourceGenerators currentNamespace = currentNamespace.ContainingNamespace; } - yield return memberDeclaration; + yield return memberDeclaration.NormalizeWhitespace().ToFullString(); } } }