Enable linking support discovery and generation

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:10 +02:00
1 親 601cd3478e
コミット 8d3a7a0771
7ファイル変更
+78 -51

検索条件に一致するファイルはありません

@@ -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)
@@ -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;
}
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <returns>Whether the shader only has simple inputs.</returns>
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);
}
/// <summary>
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader, if requested.</param>
/// <param name="sourceInfo">The source info for the shader to compile.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
public static unsafe ImmutableArray<byte> GetBytecode(
string hlslSource,
D2D1ShaderProfile? shaderProfile,
CancellationToken token,
out DiagnosticInfo? diagnostic)
public static unsafe ImmutableArray<byte> GetBytecode(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic)
{
ImmutableArray<byte> bytecode = ImmutableArray<byte>.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<ID3DBlob> dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), shaderProfile.Value, false);
using ComPtr<ID3DBlob> dxcBlobBytecode = D2D1ShaderCompiler.Compile(
sourceInfo.HlslSource.AsSpan(),
sourceInfo.ShaderProfile.Value,
sourceInfo.IsLinkingSupported);
token.ThrowIfCancellationRequested();
@@ -18,10 +18,9 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadDispatchDataMethod</c> method.
/// </summary>
/// <param name="fieldInfos">The array of <see cref="FieldInfo"/> values for all captured fields.</param>
/// <param name="root32BitConstantsCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <param name="dispatchInfo">The dispatch info gathered for the current shader.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadDispatchDataMethod</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(ImmutableArray<FieldInfo> 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)));
}
/// <summary>
+27 -24
ファイルの表示
@@ -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<DispatchDataInfo> Dispatch, Result<string> SourceInfo, Result<D2D1ShaderProfile?> ProfileInfo)> shaderInfoWithErrors =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> 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>(dispatchDataInfo, dispatchDataDiagnostics),
new Result<string>(hlslSource, hlslSourceDiagnostics),
new Result<D2D1ShaderProfile?>(shaderProfile, ImmutableArray<Diagnostic>.Empty));
new Result<HlslShaderSourceInfo>(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<string>.Default, EqualityComparer<D2D1ShaderProfile?>.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> 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<string>.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<string>.Default, EqualityComparer<D2D1ShaderProfile?>.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<byte> bytecode = LoadBytecode.GetBytecode(item.HlslSource, item.ShaderProfile, token, out DiagnosticInfo? diagnostic);
ImmutableArray<byte> 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);
});
+1 -7
ファイルの表示
@@ -8,13 +8,9 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a shader dispatch data.
/// </summary>
/// <param name="Hierarchy">The hierarchy data for the shader.</param>
/// <param name="FieldInfos">The description on shader instance fields.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
internal sealed record DispatchDataInfo(
HierarchyInfo Hierarchy,
ImmutableArray<FieldInfo> FieldInfos,
int Root32BitConstantCount)
internal sealed record DispatchDataInfo(ImmutableArray<FieldInfo> FieldInfos, int Root32BitConstantCount)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchDataInfo"/>.
@@ -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);
+4 -1
ファイルの表示
@@ -9,8 +9,9 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
/// A model representing a shared and its compiled bytecode, if available.
/// </summary>
/// <param name="HlslSource">The HLSL source for the shader.</param>
/// <param name="isLinkingSupported">Whether linking is supported for the current shader.</param>
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray<byte> Bytecode)
internal sealed record EmbeddedBytecodeInfo(string HlslSource, bool isLinkingSupported, ImmutableArray<byte> Bytecode)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="EmbeddedBytecodeInfo"/>.
@@ -42,6 +43,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray<by
return
x.HlslSource == y.HlslSource &&
x.isLinkingSupported == y.isLinkingSupported &&
x.Bytecode.SequenceEqual(y.Bytecode);
}
@@ -51,6 +53,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray<by
HashCode hashCode = default;
hashCode.Add(obj.HlslSource);
hashCode.Add(obj.isLinkingSupported);
hashCode.AddBytes(obj.Bytecode.AsSpan());
return hashCode.ToHashCode();
+5 -5
ファイルの表示
@@ -6,10 +6,10 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
/// <summary>
/// A model for extracted info on a processed HLSL shader.
/// </summary>
/// <param name="Hierarchy">The hierarchy data for the shader.</param>
/// <param name="HlslSource">The HLSL source.</param>
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
internal sealed record HlslShaderSourceInfo(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile)
/// <param name="IsLinkingSupported">Whether the shader supports linking.</param>
internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HlslShaderSourceInfo"/>.
@@ -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;
}
/// <inheritdoc/>
@@ -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();
}