Optimize diagnostics flow in D2D1 source generator

このコミットが含まれているのは:
Sergio Pedri committed 2022-05-22 01:39:42 +02:00
1 親 a3a0425419
コミット 77c43a819e
4ファイル変更
+49 -62

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

@@ -28,27 +28,25 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> node to process.</param>
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
/// <param name="inputCount">The number of inputs for the shader.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>The HLSL source for the shader.</returns>
public static string GetHlslSource(
ImmutableArray<Diagnostic>.Builder diagnostics,
Compilation compilation,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
int inputCount,
ImmutableArray<int> inputSimpleIndices,
ImmutableArray<int> inputComplexIndices,
out ImmutableArray<Diagnostic> diagnostics)
ImmutableArray<int> inputComplexIndices)
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Properties are not supported
DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol);
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -57,19 +55,17 @@ partial class ID2D1ShaderGenerator
// Explore the syntax tree and extract the processed info
var semanticModelProvider = new SemanticModelProvider(compilation);
var valueFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
var valueFields = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Process the discovered types and constants
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
// Check whether the scene position is required
bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
diagnostics = builder.ToImmutable();
// Get the HLSL source
return GetHlslSource(
definedConstants,
+13 -18
ファイルの表示
@@ -19,22 +19,20 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Extracts the input info for the current shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
/// <param name="inputComplexIndices">The indices of the complex shader inputs.</param>
/// <param name="combinedInputTypes">The combined and serialized input types for each available input.</param>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
public static void GetInfo(
ImmutableArray<Diagnostic>.Builder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
out int inputCount,
out ImmutableArray<int> inputSimpleIndices,
out ImmutableArray<int> inputComplexIndices,
out ImmutableArray<uint> combinedInputTypes,
out ImmutableArray<Diagnostic> diagnostics)
out ImmutableArray<uint> combinedInputTypes)
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// We need a separate local here so we can keep the original value to apply
// diagnostics to, but without returning invalid values to the caller which
// might cause generator errors (eg. -1 would cause other code to just throw).
@@ -71,17 +69,17 @@ partial class ID2D1ShaderGenerator
// Validate the input count
if (rawInputCount is not (>= 0 and <= 8))
{
builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
goto End;
return;
}
// All simple indices must be in range
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
{
builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
goto End;
return;
}
HashSet<int> inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
@@ -90,25 +88,25 @@ partial class ID2D1ShaderGenerator
// All simple indices must be unique
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
{
builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
goto End;
return;
}
// All complex indices must be unique
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
{
builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
goto End;
return;
}
// Simple and complex indices can't have indices in common
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
{
builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
goto End;
return;
}
// Build the combined input types list now that inputs have been validated
@@ -125,9 +123,6 @@ partial class ID2D1ShaderGenerator
}
combinedInputTypes = combinedBuilder.MoveToImmutable();
End:
diagnostics = builder.ToImmutable();
}
}
}
@@ -23,14 +23,14 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Explores a given type hierarchy and generates statements to load fields.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
/// <param name="root32BitConstantCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
public static ImmutableArray<FieldInfo> GetInfo(
ImmutableArray<Diagnostic>.Builder diagnostics,
ITypeSymbol structDeclarationSymbol,
out int root32BitConstantCount,
out ImmutableArray<Diagnostic> diagnostics)
out int root32BitConstantCount)
{
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
static IEnumerable<FieldInfo> GetCapturedFieldInfos(
@@ -78,8 +78,6 @@ partial class ID2D1ShaderGenerator
}
}
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Setup the resource and byte offsets for tracking
StrongBox<int> rawDataOffsetAsBox = new();
@@ -107,11 +105,9 @@ partial class ID2D1ShaderGenerator
if (rootSignatureDwordSize > 64)
{
builder.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
diagnostics = builder.ToImmutable();
return fieldInfos;
}
+24 -24
ファイルの表示
@@ -51,16 +51,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Get the dispatch data, input types, 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<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<InputTypesInfo> InputTypes, Result<HlslShaderSourceInfo> Source)> shaderInfoWithErrors =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source, ImmutableArray<Diagnostic> Diagnostics)> shaderInfoWithErrors =
shaderDeclarations
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
{
ImmutableArray<Diagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
// LoadDispatchData() info
ImmutableArray<FieldInfo> fieldInfos = LoadDispatchData.GetInfo(
diagnostics,
item.Left.Symbol,
out int root32BitConstantCount,
out ImmutableArray<Diagnostic> dispatchDataDiagnostics);
out int root32BitConstantCount);
token.ThrowIfCancellationRequested();
@@ -68,56 +70,60 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
// Get the input info for GetInputInfo()
GetInputType.GetInfo(
diagnostics,
item.Left.Symbol,
out int inputCount,
out ImmutableArray<int> inputSimpleIndices,
out ImmutableArray<int> inputComplexIndices,
out ImmutableArray<uint> inputTypes,
out ImmutableArray<Diagnostic> inputTypesDiagnostics);
out ImmutableArray<uint> inputTypes);
InputTypesInfo inputTypesInfo = new(inputTypes);
// Get HLSL source for BuildHlslSource()
string hlslSource = BuildHlslSource.GetHlslSource(
diagnostics,
item.Right,
item.Left.Syntax,
item.Left.Symbol,
inputCount,
inputSimpleIndices,
inputComplexIndices,
out ImmutableArray<Diagnostic> hlslSourceDiagnostics);
inputComplexIndices);
token.ThrowIfCancellationRequested();
// Get the shader profile and linking info for LoadBytecode()
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol);
bool hasErrors = !dispatchDataDiagnostics.IsDefaultOrEmpty || !hlslSourceDiagnostics.IsDefaultOrEmpty;
bool isLinkingSupported = !hasErrors && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
bool isLinkingSupported = diagnostics.Count == 0 && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
HlslShaderSourceInfo sourceInfo = new(
hlslSource,
shaderProfile,
isLinkingSupported,
hasErrors);
diagnostics.Count > 0);
token.ThrowIfCancellationRequested();
return (
item.Left.Hierarchy,
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
new Result<InputTypesInfo>(inputTypesInfo, inputTypesDiagnostics),
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics));
dispatchDataInfo,
inputTypesInfo,
sourceInfo,
diagnostics.ToImmutableArray());
});
// Output the diagnostics
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.InputTypes.Errors));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors));
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics));
// Filter all items to enable caching at a coarse level, and remove diagnostics
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Hierarchy, item.Dispatch, item.InputTypes, item.Source))
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
// Filter items to enable caching for the input count methods
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length))
shaderInfo
.Select(static (item, token) => (item.Hierarchy, item.InputTypes.InputTypes.Length))
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<int>.Default);
// Generate the GetInputCount() methods
@@ -129,12 +135,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}", compilationUnit.ToFullString());
});
// Filter all items to enable caching at a coarse level, and remove diagnostics
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
shaderInfoWithErrors
.Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.InputTypes.Value, item.Source.Value))
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
// Get a filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
shaderInfo