diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 577a87c4..350e30f0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -28,27 +28,25 @@ partial class ID2D1ShaderGenerator /// /// Gathers all necessary information on a transpiled HLSL source for a given shader type. /// + /// The collection of produced instances. /// The input object currently in use. /// The node to process. /// The for . /// The number of inputs for the shader. /// The indicess of the simple shader inputs. /// The indicess of the complex shader inputs. - /// The resulting diagnostics from the processing operation. /// The HLSL source for the shader. public static string GetHlslSource( + ImmutableArray.Builder diagnostics, Compilation compilation, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, int inputCount, ImmutableArray inputSimpleIndices, - ImmutableArray inputComplexIndices, - out ImmutableArray diagnostics) + ImmutableArray inputComplexIndices) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Properties are not supported - DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol); + DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol); // We need to sets to track all discovered custom types and static methods HashSet 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, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs index 5c45d3a8..06c844fb 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs @@ -19,22 +19,20 @@ partial class ID2D1ShaderGenerator /// /// Extracts the input info for the current shader. /// + /// The collection of produced instances. /// The input instance to process. /// The number of shader inputs to declare. /// The indicess of the simple shader inputs. /// The indices of the complex shader inputs. /// The combined and serialized input types for each available input. - /// The collection of produced instances. public static void GetInfo( + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, out int inputCount, out ImmutableArray inputSimpleIndices, out ImmutableArray inputComplexIndices, - out ImmutableArray combinedInputTypes, - out ImmutableArray diagnostics) + out ImmutableArray combinedInputTypes) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // 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 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(); } } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs index a6e21e6f..f68e9c16 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -23,14 +23,14 @@ partial class ID2D1ShaderGenerator /// /// Explores a given type hierarchy and generates statements to load fields. /// + /// The collection of produced instances. /// The current shader type being explored. /// The total number of needed 32 bit constants in the shader root signature. - /// The resulting diagnostics from the processing operation. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( + ImmutableArray.Builder diagnostics, ITypeSymbol structDeclarationSymbol, - out int root32BitConstantCount, - out ImmutableArray diagnostics) + out int root32BitConstantCount) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( @@ -78,8 +78,6 @@ partial class ID2D1ShaderGenerator } } - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Setup the resource and byte offsets for tracking StrongBox 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; } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 41f8e262..3ac5ab2e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -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 Dispatch, Result InputTypes, Result Source)> shaderInfoWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source, ImmutableArray Diagnostics)> shaderInfoWithErrors = shaderDeclarations .Combine(context.CompilationProvider) .Select(static (item, token) => { + ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); + // LoadDispatchData() info ImmutableArray fieldInfos = LoadDispatchData.GetInfo( + diagnostics, item.Left.Symbol, - out int root32BitConstantCount, - out ImmutableArray 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 inputSimpleIndices, out ImmutableArray inputComplexIndices, - out ImmutableArray inputTypes, - out ImmutableArray inputTypesDiagnostics); + out ImmutableArray 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 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, dispatchDataDiagnostics), - new Result(inputTypesInfo, inputTypesDiagnostics), - new Result(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.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.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.Default); - // Get a filtered sequence to enable caching IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo = shaderInfo