diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs index 77f5e977..994adc48 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs @@ -78,7 +78,7 @@ partial class ID2D1ShaderGenerator Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)), Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword)), Argument(IdentifierName("shaderProfile")), - Argument(LiteralExpression(SyntaxKind.TrueLiteralExpression)))); + Argument(LiteralExpression(bytecodeInfo.isLinkingSupported ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression)))); // If there is no precompiled bytecode, just return the dynamic path if (bytecodeInfo.Bytecode.IsDefaultOrEmpty) diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 243947c7..a5df02f2 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Immutable; using System.ComponentModel; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using ComputeSharp.D2D1Interop.Exceptions; @@ -36,24 +37,48 @@ partial class ID2D1ShaderGenerator return null; } + /// + /// Extracts the metadata definition for the current shader. + /// + /// The input instance to process. + /// Whether the shader only has simple inputs. + public static bool IsSimpleInputShader(INamedTypeSymbol structDeclarationSymbol) + { + if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedName("ComputeSharp.D2D1Interop.D2DInputCountAttribute", out AttributeData? inputCountAttribute)) + { + return false; + } + + // Build a map of all simple inputs (unmarked inputs default to being complex) + int inputCount = (int)inputCountAttribute!.ConstructorArguments[0].Value!; + bool[] simpleInputsMap = new bool[inputCount]; + + foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) + { + switch (attributeData.AttributeClass?.GetFullMetadataName()) + { + case "ComputeSharp.D2D1Interop.D2DInputSimpleAttribute": + simpleInputsMap[(int)attributeData.ConstructorArguments[0].Value!] = true; + break; + } + } + + return simpleInputsMap.All(static x => x); + } + /// /// Gets a instance with the logic to try to get a compiled shader bytecode. /// - /// The generated HLSL source code (ignoring captured delegates, if present). - /// The shader profile to use to compile the shader, if requested. + /// The source info for the shader to compile. /// The used to cancel the operation, if needed. /// The resulting diagnostic from the processing operation, if any. /// The instance with the compiled shader bytecode. - public static unsafe ImmutableArray GetBytecode( - string hlslSource, - D2D1ShaderProfile? shaderProfile, - CancellationToken token, - out DiagnosticInfo? diagnostic) + public static unsafe ImmutableArray GetBytecode(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; // No embedded shader was requested - if (shaderProfile is null) + if (sourceInfo.ShaderProfile is null) { diagnostic = null; @@ -65,7 +90,10 @@ partial class ID2D1ShaderGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode - using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), shaderProfile.Value, false); + using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile( + sourceInfo.HlslSource.AsSpan(), + sourceInfo.ShaderProfile.Value, + sourceInfo.IsLinkingSupported); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index 5bb32159..2dea769d 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -18,10 +18,9 @@ partial class ID2D1ShaderGenerator /// /// Creates a instance for the LoadDispatchDataMethod method. /// - /// The array of values for all captured fields. - /// The total number of needed 32 bit constants in the shader root signature. + /// The dispatch info gathered for the current shader. /// The resulting instance for the LoadDispatchDataMethod method. - public static MethodDeclarationSyntax GetSyntax(ImmutableArray fieldInfos, int root32BitConstantsCount) + public static MethodDeclarationSyntax GetSyntax(DispatchDataInfo dispatchInfo) { // This code produces a method declaration as follows: // @@ -35,7 +34,7 @@ partial class ID2D1ShaderGenerator .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TLoader"))) .AddParameterListParameters(Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader"))) - .WithBody(Block(GetDispatchDataLoadingStatements(fieldInfos, root32BitConstantsCount))); + .WithBody(Block(GetDispatchDataLoadingStatements(dispatchInfo.FieldInfos, dispatchInfo.Root32BitConstantCount))); } /// diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs index 26bec932..b142236f 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs @@ -51,7 +51,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the dispatch data, HLSL source and embedded bytecode info. This info is computed on the // same step as parts are shared in following sub-branches in the incremental generator pipeline. - IncrementalValuesProvider<(Result Dispatch, Result SourceInfo, Result ProfileInfo)> shaderInfoWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source)> shaderInfoWithErrors = shaderDeclarations .Combine(context.CompilationProvider) .Select(static (item, token) => @@ -64,12 +64,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - DispatchDataInfo dispatchDataInfo = new( - item.Left.Hierarchy, - fieldInfos, - root32BitConstantCount); - - token.ThrowIfCancellationRequested(); + DispatchDataInfo dispatchDataInfo = new(fieldInfos, root32BitConstantCount); // Get HLSL source for BuildHlslSource() string hlslSource = BuildHlslSource.GetHlslSource( @@ -80,35 +75,43 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - // Get the shader profile + // Get the shader profile and linking info for LoadBytecode() D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol); + bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol); + + HlslShaderSourceInfo sourceInfo = new( + hlslSource, + shaderProfile, + isLinkingSupported); + + token.ThrowIfCancellationRequested(); return ( + item.Left.Hierarchy, new Result(dispatchDataInfo, dispatchDataDiagnostics), - new Result(hlslSource, hlslSourceDiagnostics), - new Result(shaderProfile, ImmutableArray.Empty)); + new Result(sourceInfo, hlslSourceDiagnostics)); }); // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors)); - context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.SourceInfo.Errors)); + context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors)); // Filter all items to enable caching at a coarse level, and remove diagnostics - IncrementalValuesProvider<(DispatchDataInfo Dispatch, string HlslSource, D2D1ShaderProfile? ShaderProfile)> shaderInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors - .Select(static (item, token) => (item.Dispatch.Value, item.SourceInfo.Value, item.ProfileInfo.Value)) - .WithComparers(DispatchDataInfo.Comparer.Default, EqualityComparer.Default, EqualityComparer.Default); + .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.Source.Value)) + .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default); // Get a filtered sequence to enable caching - IncrementalValuesProvider dispatchDataInfo = + IncrementalValuesProvider< (HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = shaderInfo - .Select(static (item, token) => item.Dispatch) - .WithComparer(DispatchDataInfo.Comparer.Default); + .Select(static (item, token) => (item.Hierarchy, item.Dispatch)) + .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default); // Generate the LoadDispatchData() methods context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) => { - MethodDeclarationSyntax loadDispatchDataMethod = LoadDispatchData.GetSyntax(item.Left.FieldInfos, item.Left.Root32BitConstantCount); + MethodDeclarationSyntax loadDispatchDataMethod = LoadDispatchData.GetSyntax(item.Left.Dispatch); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, loadDispatchDataMethod, item.Right); context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}", compilationUnit.ToFullString()); @@ -117,7 +120,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the filtered sequence to enable caching IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo = shaderInfo - .Select(static (item, token) => (item.Dispatch.Hierarchy, item.HlslSource)) + .Select(static (item, token) => (item.Hierarchy, item.Source.HlslSource)) .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); // Generate the BuildHlslSource() methods @@ -130,21 +133,21 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator }); // Get a filtered sequence to enable caching - IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile)> shaderBytecodeInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source)> shaderBytecodeInfo = shaderInfo - .Select(static (item, token) => (item.Dispatch.Hierarchy, item.HlslSource, item.ShaderProfile)) - .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default, EqualityComparer.Default); + .Select(static (item, token) => (item.Hierarchy, item.Source)) + .WithComparers(HierarchyInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default); // Compile the requested shader bytecodes IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = shaderBytecodeInfo .Select(static (item, token) => { - ImmutableArray bytecode = LoadBytecode.GetBytecode(item.HlslSource, item.ShaderProfile, token, out DiagnosticInfo? diagnostic); + ImmutableArray bytecode = LoadBytecode.GetBytecode(item.Source, token, out DiagnosticInfo? diagnostic); token.ThrowIfCancellationRequested(); - EmbeddedBytecodeInfo bytecodeInfo = new(item.HlslSource, bytecode); + EmbeddedBytecodeInfo bytecodeInfo = new(item.Source.HlslSource, item.Source.IsLinkingSupported, bytecode); return (item.Hierarchy, bytecodeInfo, diagnostic); }); diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/DispatchDataInfo.cs index d2f2b1fd..76d4b579 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/DispatchDataInfo.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/DispatchDataInfo.cs @@ -8,13 +8,9 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models; /// /// A model representing gathered info on a shader dispatch data. /// -/// The hierarchy data for the shader. /// The description on shader instance fields. /// The size of the shader root signature, in 32 bit constants. -internal sealed record DispatchDataInfo( - HierarchyInfo Hierarchy, - ImmutableArray FieldInfos, - int Root32BitConstantCount) +internal sealed record DispatchDataInfo(ImmutableArray FieldInfos, int Root32BitConstantCount) { /// /// An implementation for . @@ -45,7 +41,6 @@ internal sealed record DispatchDataInfo( } return - HierarchyInfo.Comparer.Default.Equals(x.Hierarchy, y.Hierarchy) && x.FieldInfos.SequenceEqual(y.FieldInfos, FieldInfo.Comparer.Default) && x.Root32BitConstantCount == y.Root32BitConstantCount; } @@ -55,7 +50,6 @@ internal sealed record DispatchDataInfo( { HashCode hashCode = default; - hashCode.Add(HierarchyInfo.Comparer.Default.GetHashCode(obj.Hierarchy)); hashCode.AddRange(obj.FieldInfos, FieldInfo.Comparer.Default); hashCode.Add(obj.Root32BitConstantCount); diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs index 5242cbd0..10c36d2c 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs @@ -9,8 +9,9 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models; /// A model representing a shared and its compiled bytecode, if available. /// /// The HLSL source for the shader. +/// Whether linking is supported for the current shader. /// The compiled shader bytecode, if available. -internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray Bytecode) +internal sealed record EmbeddedBytecodeInfo(string HlslSource, bool isLinkingSupported, ImmutableArray Bytecode) { /// /// An implementation for . @@ -42,6 +43,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray /// A model for extracted info on a processed HLSL shader. /// -/// The hierarchy data for the shader. /// The HLSL source. /// The shader profile to use to compile the shader, if requested. -internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile) +/// Whether the shader supports linking. +internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported) { /// /// An implementation for . @@ -40,9 +40,9 @@ internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string Hlsl } return - HierarchyInfo.Comparer.Default.Equals(x.Hierarchy, y.Hierarchy) && x.HlslSource == y.HlslSource && - x.ShaderProfile == y.ShaderProfile; + x.ShaderProfile == y.ShaderProfile && + x.IsLinkingSupported == y.IsLinkingSupported; } /// @@ -50,9 +50,9 @@ internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string Hlsl { HashCode hashCode = default; - hashCode.Add(HierarchyInfo.Comparer.Default.GetHashCode(obj.Hierarchy)); hashCode.Add(obj.HlslSource); hashCode.Add(obj.ShaderProfile); + hashCode.Add(obj.IsLinkingSupported); return hashCode.ToHashCode(); }