From ecb90821dfd4b3a2146be312d0afc00668479cee Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 21 Sep 2024 17:59:42 +0200 Subject: [PATCH] Extend explicit entry points support to D2D shaders --- ...xelShaderDescriptorGenerator.HlslSource.cs | 27 ++++++++++++------- .../D2DPixelShaderDescriptorGenerator.cs | 5 +++- .../SyntaxRewriters/ShaderSourceRewriter.cs | 13 +++++++-- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 16 ----------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index dd2e7f86..91ea3a83 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -29,15 +29,17 @@ partial class D2DPixelShaderDescriptorGenerator /// The collection of produced instances. /// The input object currently in use. /// The for the shader type. + /// The shader interface type implemented by the shader type. /// The number of inputs for the shader. - /// The indicess of the simple shader inputs. - /// The indicess of the complex shader inputs. + /// The indices of the simple shader inputs. + /// The indices of the complex shader inputs. /// The used to cancel the operation, if needed. /// The HLSL source for the shader. public static string GetHlslSource( ImmutableArrayBuilder diagnostics, Compilation compilation, INamedTypeSymbol structDeclarationSymbol, + INamedTypeSymbol shaderInterfaceType, int inputCount, ImmutableArray inputSimpleIndices, ImmutableArray inputComplexIndices, @@ -46,6 +48,8 @@ partial class D2DPixelShaderDescriptorGenerator // Detect any invalid properties HlslDefinitionsSyntaxProcessor.DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol); + token.ThrowIfCancellationRequested(); + // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); Dictionary staticMethods = new(SymbolEqualityComparer.Default); @@ -72,6 +76,7 @@ partial class D2DPixelShaderDescriptorGenerator (string entryPoint, ImmutableArray processedMethods) = GetProcessedMethods( diagnostics, structDeclarationSymbol, + shaderInterfaceType, semanticModelProvider, discoveredTypes, staticMethods, @@ -302,6 +307,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The collection of produced instances. /// The type symbol for the shader type. + /// The shader interface type implemented by the shader type. /// The instance for the type to process. /// The collection of currently discovered types. /// The set of discovered and processed static methods. @@ -315,6 +321,7 @@ partial class D2DPixelShaderDescriptorGenerator private static (string EntryPoint, ImmutableArray Methods) GetProcessedMethods( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, + INamedTypeSymbol shaderInterfaceType, SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary staticMethods, @@ -327,6 +334,7 @@ partial class D2DPixelShaderDescriptorGenerator { using ImmutableArrayBuilder methods = new(); + IMethodSymbol entryPointInterfaceMethod = shaderInterfaceType.GetMethod("Execute")!; string? entryPoint = null; // By default, the scene position is not required. We will set this while @@ -341,16 +349,17 @@ partial class D2DPixelShaderDescriptorGenerator continue; } + // Ensure that we have accessible source information if (!methodSymbol.TryGetSyntaxNode(token, out MethodDeclarationSyntax? methodDeclaration)) { continue; } - bool isShaderEntryPoint = - methodSymbol.Name == "Execute" && - methodSymbol.ReturnType.HasFullyQualifiedMetadataName("ComputeSharp.Float4") && - methodSymbol.TypeParameters.Length == 0 && - methodSymbol.Parameters.Length == 0; + // Check whether the current method is the entry point (ie. it's implementing 'Execute'). + // This is the same logic as in the DX12 generator for compute shaders and pixel shaders. + bool isShaderEntryPoint = SymbolEqualityComparer.Default.Equals( + structDeclarationSymbol.FindImplementationForInterfaceMember(entryPointInterfaceMethod), + methodSymbol); // Except for the entry point, ignore explicit interface implementations if (!isShaderEntryPoint && !methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) @@ -460,8 +469,8 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The body of the entry point of the shader. /// The number of shader inputs to declare. - /// The indicess of the simple shader inputs. - /// The indicess of the complex shader inputs. + /// The indices of the simple shader inputs. + /// The indices of the complex shader inputs. /// Whether the shader requires the scene position. /// The series of statements to build the HLSL source to compile to execute the current shader. private static string GetHlslSource( diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index e3b47480..40f6ca7a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -53,8 +53,10 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene return default; } + INamedTypeSymbol shaderInterfaceType = context.SemanticModel.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader")!; + // Check that the shader implements the ID2D1PixelShader interface - if (!typeSymbol.HasInterfaceWithType(context.SemanticModel.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader")!)) + if (!typeSymbol.HasInterfaceWithType(shaderInterfaceType)) { return default; } @@ -142,6 +144,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene diagnostics, context.SemanticModel.Compilation, typeSymbol, + shaderInterfaceType, inputCount, inputSimpleIndices, inputComplexIndices, diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 685435fa..ba7b50ef 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -154,14 +154,23 @@ internal sealed partial class ShaderSourceRewriter( Diagnostics.Add(UnsafeModifierOnMethodOrFunction, node); } - if (updatedNode is not null) + // Add the tracked implicit declarations (at the start of the body). + // To optimize, we only do this if we do have any implicit variables. + if (this.implicitVariables.Count > 0) { BlockSyntax implicitBlock = Block(this.implicitVariables.Select(static v => LocalDeclarationStatement(v)).ToArray()); - // Add the tracked implicit declarations (at the start of the body) updatedNode = updatedNode.WithBody(implicitBlock).AddBodyStatements([.. updatedNode.Body!.Statements]); } + // The entry point might be an explicit interface method implementation. In that case, + // the transpiled method will have the rewritten interface name as a prefix for the + // method name, which we don't want (it's invalid HLSL). So in that case, remove it. + if (this.isEntryPoint && updatedNode.ExplicitInterfaceSpecifier is not null) + { + updatedNode = updatedNode.WithExplicitInterfaceSpecifier(null); + } + return updatedNode; } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs index 65c4d830..4fdb0251 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -24,22 +24,6 @@ internal abstract class ExecuteMethodRewriter(ShaderSourceRewriter shaderSourceR return (TNode?)base.Visit(node); } - /// - public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) - { - MethodDeclarationSyntax updatedNode = ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithModifiers(TokenList()); - - // The entry point might be an explicit interface method implementation. In that case, - // the transpiled method will have the rewritten interface name as a prefix for the - // method name, which we don't want (it's invalid HLSL). So in that case, remove it. - if (updatedNode.ExplicitInterfaceSpecifier is not null) - { - updatedNode = updatedNode.WithExplicitInterfaceSpecifier(null); - } - - return updatedNode; - } - /// public override SyntaxNode? VisitParameterList(ParameterListSyntax node) {