diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index eb8d1951..2061543e 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -1,5 +1,4 @@ -using System.Collections.Immutable; -using System.Linq; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; @@ -131,9 +130,7 @@ internal static class ISymbolExtensions /// Whether or not has an attribute with the specified name. public static bool HasAttributeWithFullyQualifiedName(this ISymbol symbol, string name) { - ImmutableArray attributes = symbol.GetAttributes(); - - foreach (AttributeData attribute in attributes) + foreach (AttributeData attribute in symbol.GetAttributes()) { if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true) { @@ -153,9 +150,7 @@ internal static class ISymbolExtensions /// Whether or not has an attribute with the specified name. public static bool TryGetAttributeWithFullMetadataName(this ISymbol symbol, string name, out AttributeData? attributeData) { - ImmutableArray attributes = symbol.GetAttributes(); - - foreach (AttributeData attribute in attributes) + foreach (AttributeData attribute in symbol.GetAttributes()) { if (attribute.AttributeClass?.GetFullMetadataName() == name) { diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index 2bb75f0e..a4b31e9c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -31,21 +31,19 @@ partial class IShaderGenerator /// /// 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 resulting diagnostics from the processing operation. /// The resulting info on the processed shader. public static HlslShaderSourceInfo GetInfo( + ImmutableArray.Builder diagnostics, Compilation compilation, StructDeclarationSyntax structDeclaration, - INamedTypeSymbol structDeclarationSymbol, - out ImmutableArray diagnostics) + INamedTypeSymbol structDeclarationSymbol) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Detect invalid properties - DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol); + DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol); // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); @@ -55,7 +53,7 @@ partial class IShaderGenerator // A given type can only represent a single shader type if (structDeclarationSymbol.AllInterfaces.Count(static interfaceSymbol => interfaceSymbol is { Name: nameof(IComputeShader) } or { IsGenericType: true, Name: nameof(IPixelShader) }) > 1) { - builder.Add(MultipleShaderTypesImplemented, structDeclarationSymbol, structDeclarationSymbol); + diagnostics.Add(MultipleShaderTypesImplemented, structDeclarationSymbol, structDeclarationSymbol); } // Explore the syntax tree and extract the processed info @@ -63,19 +61,17 @@ partial class IShaderGenerator var pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }); var isComputeShader = pixelShaderSymbol is null; var implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); - var (resourceFields, valueFields) = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes, isComputeShader); + var (resourceFields, valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader); var delegateInstanceFields = GetDispatchId.GetInfo(structDeclarationSymbol); - var sharedBuffers = GetSharedBuffers(builder, structDeclarationSymbol, discoveredTypes); - var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader); + var sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes); + var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader); var implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?); - var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, 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); - diagnostics = builder.ToImmutable(); - // Get the HLSL source data with the intermediate info return GetHlslSourceInfo( definedConstants, diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 2326268c..8443bf2c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -3,7 +3,6 @@ using System.Collections.Immutable; using System.ComponentModel; using System.Globalization; using System.IO; -using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -33,33 +32,26 @@ partial class IShaderGenerator /// /// Gets the thread ids values for a given shader type, if available. /// + /// The collection of produced instances. /// The input instance to process. /// Indicates whether or not the shader is dynamic (ie. captures delegates). /// Indicates whether or not dynamic shaders are supported. - /// The resulting diagnostics from the processing operation. /// The thread ids for the precompiled shader, if available. public static ThreadIdsInfo GetInfo( + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, bool isDynamicShader, - bool supportsDynamicShaders, - out ImmutableArray diagnostics) + bool supportsDynamicShaders) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - - AttributeData? attribute = structDeclarationSymbol - .GetAttributes() - .FirstOrDefault(static a => a.AttributeClass?.ToDisplayString() == typeof(EmbeddedBytecodeAttribute).FullName); - - if (attribute is null) + // Try to get the attribute that controls shader precompilation (has to be present when ComputeSharp.Dynamic is not referenced) + if (!structDeclarationSymbol.TryGetAttributeWithFullMetadataName(typeof(EmbeddedBytecodeAttribute).FullName, out AttributeData? attribute)) { // Emit the diagnostics if dynamic shaders are not supported if (!supportsDynamicShaders) { - builder.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol); + diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol); } - diagnostics = builder.ToImmutable(); - return new(true, 0, 0, 0); } @@ -68,7 +60,7 @@ partial class IShaderGenerator int threadsZ; // Check for a dispatch axis argument first - if (attribute.ConstructorArguments.Length == 1) + if (attribute!.ConstructorArguments.Length == 1) { int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!; @@ -87,9 +79,7 @@ partial class IShaderGenerator // Validate the dispatch axis argument if ((threadsX, threadsY, threadsZ) is (0, 0, 0)) { - builder.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol); - - diagnostics = builder.ToImmutable(); + diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol); return new(true, 0, 0, 0); } @@ -104,9 +94,7 @@ partial class IShaderGenerator explicitThreadsZ is < 1 or > 64) { // Failed to validate the thread number arguments - builder.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol); - - diagnostics = builder.ToImmutable(); + diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol); return new(true, 0, 0, 0); } @@ -120,14 +108,17 @@ partial class IShaderGenerator // If the current shader is dynamic, emit a diagnostic error if (isDynamicShader) { - builder.Add(EmbeddedBytecodeWithDynamicShader, structDeclarationSymbol, structDeclarationSymbol); - - diagnostics = builder.ToImmutable(); + diagnostics.Add(EmbeddedBytecodeWithDynamicShader, structDeclarationSymbol, structDeclarationSymbol); return new(true, 0, 0, 0); } - diagnostics = builder.ToImmutable(); + // Ensure the bytecode generation is disabled if any errors are present. This step is done last to ensure that + // all available diagnostics are still emitted correctly before reaching this point, instead of being disabled. + if (diagnostics.Count > 0) + { + return new(true, 0, 0, 0); + } return new(false, threadsX, threadsY, threadsZ); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 33bd6a0f..bfdb56ec 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -24,18 +24,18 @@ partial class IShaderGenerator /// /// Explores a given type hierarchy and generates statements to load fields. /// + /// The collection of produced instances. /// The current shader type being explored. /// The type of shader currently being processed. /// The total number of captured resources in the shader. /// 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, ShaderType shaderType, out int resourceCount, - out int root32BitConstantCount, - out ImmutableArray diagnostics) + out int root32BitConstantCount) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( @@ -88,8 +88,6 @@ partial class IShaderGenerator } } - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Setup the resource and byte offsets for tracking. Pixel shaders have only two // implicitly captured int values, as they're always dispatched over a 2D texture. bool isComputeShader = shaderType == ShaderType.ComputeShader; @@ -124,11 +122,9 @@ partial class IShaderGenerator if (rootSignatureDwordSize > 64) { - builder.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); + diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); } - diagnostics = builder.ToImmutable(); - return fieldInfos; } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 6d112bd5..3c207729 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -64,20 +64,20 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // LoadDispatchData() info ImmutableArray fieldInfos = LoadDispatchData.GetInfo( + diagnostics, typeSymbol, shaderType, out int resourceCount, - out int root32BitConstantCount, - out ImmutableArray dispatchDataDiagnostics); + out int root32BitConstantCount); token.ThrowIfCancellationRequested(); // BuildHlslSource() info HlslShaderSourceInfo hlslSourceInfo = BuildHlslSource.GetInfo( + diagnostics, context.SemanticModel.Compilation, typeDeclaration, - typeSymbol, - out ImmutableArray hlslSourceDiagnostics); + typeSymbol); token.ThrowIfCancellationRequested(); @@ -92,20 +92,13 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // TryGetBytecode() info ThreadIdsInfo threadIds = LoadBytecode.GetInfo( + diagnostics, typeSymbol, !hlslSourceInfo.Delegates.IsEmpty, - IsDynamicCompilationSupported(context.SemanticModel.Compilation), - out ImmutableArray threadIdsDiagnostics); + IsDynamicCompilationSupported(context.SemanticModel.Compilation)); token.ThrowIfCancellationRequested(); - // Ensure the bytecode generation is disabled if any errors are present - if (!dispatchDataDiagnostics.IsDefaultOrEmpty || - !hlslSourceDiagnostics.IsDefaultOrEmpty) - { - threadIds = new ThreadIdsInfo(true, 0, 0, 0); - } - return new ShaderInfo( Hierarchy: HierarchyInfo.From(typeSymbol), DispatchId: new DispatchIdInfo(dispatchIdInfo),