From b9313ca6d89806e4f3f8e4961796b17e321e6c7a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 18 Dec 2021 12:43:59 +0100 Subject: [PATCH] Skip generating BuildHlslString body when dynamic support is missing --- ...ator.CreateBuildHlslStringMethod.Syntax.cs | 34 +++++++++++++++++-- .../IShaderGenerator.cs | 23 +++++++------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.Syntax.cs index 5b4e860a..ce88d435 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.Syntax.cs @@ -21,14 +21,18 @@ public sealed partial class IShaderGenerator /// Creates a instance for the BuildHlslString method. /// /// The input instance to use. + /// Indicates whether or not dynamic shaders are supported. /// The resulting instance for the BuildHlslString method. - public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo) + public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders) { - ImmutableArray bodyStatements = GenerateRenderMethodBody(hlslSourceInfo); + // Generate the necessary body statements depending on whether dynamic shaders are supported + ImmutableArray bodyStatements = supportsDynamicShaders + ? GenerateRenderMethodBody(hlslSourceInfo) + : GenerateEmptyMethodBody(); // This code produces a method declaration as follows: // - // readonly void global::ComputeSharp.__Internals.IShader.BuildHlslString(ref global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) + // readonly void global::ComputeSharp.__Internals.IShader.BuildHlslString(out global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) // { // // } @@ -46,6 +50,30 @@ public sealed partial class IShaderGenerator .WithBody(Block(bodyStatements)); } + /// + /// Produces the series of statements for the empty fallback method. + /// + /// A series of statements for when dynamic shaders are not supported. + private static ImmutableArray GenerateEmptyMethodBody() + { + // builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(0); + return + ImmutableArray.Create( + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + IdentifierName("builder"), + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"), + IdentifierName("Create"))) + .AddArgumentListArguments( + Argument(LiteralExpression( + SyntaxKind.NumericLiteralExpression, + Literal(0))))))); + } + /// /// Produces the series of statements to build the current HLSL source. /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 4507d253..6e59bbf6 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -138,18 +138,25 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, loadDispatchDataMethod, item.Right); context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}", SourceText.From(compilationUnit.ToFullString(), Encoding.UTF8)); - }); + }); + + // Check whether or not dynamic shaders are supported + IncrementalValueProvider supportsDynamicShaders = + context.CompilationProvider + .Select(static (compilation, token) => compilation.ReferencedAssemblyNames.Any( + static identity => identity.Name is "ComputeSharp.Dynamic")); // Get the filtered sequence to enable caching - IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo SourceInfo)> hlslSourceInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo SourceInfo, bool SupportsDynamicShaders)> hlslSourceInfo = shaderInfo - .Select(static (item, token) => (item.Dispatch.Hierarchy, item.Hlsl)) - .WithComparers(HierarchyInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default); + .Combine(supportsDynamicShaders) + .Select(static (item, token) => (item.Left.Dispatch.Hierarchy, item.Left.Hlsl, item.Right)) + .WithComparers(HierarchyInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default, EqualityComparer.Default); // Generate the BuildHlslString() methods context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit), static (context, item) => { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslString.GetSyntax(item.Left.SourceInfo); + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslString.GetSyntax(item.Left.SourceInfo, item.Left.SupportsDynamicShaders); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, item.Right); context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslString)}", SourceText.From(compilationUnit.ToFullString(), Encoding.UTF8)); @@ -224,12 +231,6 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo)) .WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default); - // Check whether or not dynamic shaders are supported - IncrementalValueProvider supportsDynamicShaders = - context.CompilationProvider - .Select(static (compilation, token) => compilation.ReferencedAssemblyNames.Any( - static identity => identity.Name is "ComputeSharp.Dynamic")); - // Generate the TryGetBytecode() methods context.RegisterSourceOutput(embeddedBytecode.Combine(supportsDynamicShaders), static (context, item) => {