From 71fad3a8fdaa8ea910aa8fea7f66d07e9e82baf5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 18 Dec 2021 13:11:19 +0100 Subject: [PATCH] Add diagnostic when [EmbeddedBytecode] is missing --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 28 +++++++++++++++---- ...haderGenerator.CreateLoadBytecodeMethod.cs | 14 ++++++++-- .../IShaderGenerator.cs | 28 ++++++++++--------- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 74fadd53..571bb103 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -53,3 +53,4 @@ CMPS0043 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentatio CMPS0044 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0045 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0046 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0047 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 7f11ded0..4eedd7f1 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -576,7 +576,7 @@ internal static class DiagnosticDescriptors public static readonly DiagnosticDescriptor MultipleShaderTypesImplemented = new( id: "CMPS0042", title: "Multiple shader implementations for type declaration", - messageFormat: $"The shader of type {{0}} cannot implement more than one shader interface", + messageFormat: "The shader of type {0} cannot implement more than one shader interface", category: typeof(IShaderGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, @@ -592,7 +592,7 @@ internal static class DiagnosticDescriptors public static readonly DiagnosticDescriptor EmbeddedBytecodeWithDynamicShader = new( id: "CMPS0043", title: "Embedded bytecode for dynamic shader", - messageFormat: $"The shader of type {{0}} cannot be precompiled, as it is dynamic (it captures delegate fields)", + messageFormat: "The shader of type {0} cannot be precompiled, as it is dynamic (it captures delegate fields)", category: typeof(IShaderGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, @@ -608,7 +608,7 @@ internal static class DiagnosticDescriptors public static readonly DiagnosticDescriptor InvalidEmbeddedBytecodeThreadIds = new( id: "CMPS0044", title: "Invalid thread ids for shader with embedded bytecode", - messageFormat: $"The shader of type {{0}} is annotated with invalid thread ids values ({{1}}, {{2}}, {{3}})", + messageFormat: "The shader of type {0} is annotated with invalid thread ids values ({1}, {2}, {3})", category: typeof(IShaderGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, @@ -618,13 +618,13 @@ internal static class DiagnosticDescriptors /// /// Gets a for an embedded bytecode shader failed due to a Win32 exception. /// - /// Format: "The shader of type {{0}} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: "{2}")". + /// Format: "The shader of type {0} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: "{2}")". /// /// public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithWin32Exception = new( id: "CMPS0045", title: "Embedded bytecode compilation failed due to Win32 exception", - messageFormat: $"The shader of type {{0}} failed to compile due to a Win32 exception (HRESULT: {{1:X8}}, Message: \"{{2}}\")", + messageFormat: "The shader of type {0} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: \"{2}\")", category: typeof(IShaderGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, @@ -640,10 +640,26 @@ internal static class DiagnosticDescriptors public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithHlslCompilationException = new( id: "CMPS0046", title: "Embedded bytecode compilation failed due to an HLSL compiler error", - messageFormat: $"The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: \"{{1}}\")", + messageFormat: "The shader of type {0} failed to compile due to an HLSL compiler error (Message: \"{1}\")", category: typeof(IShaderGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, description: "The embedded bytecode for a shader failed to be compiled due to an HLSL compiler error.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader without the embedded bytecode attribute, when dynamic shaders are not supported. + /// + /// Format: "The shader of type {0} needs to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute), as dynamic shader compilation is not supported". + /// + /// + public static readonly DiagnosticDescriptor MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported = new( + id: "CMPS0047", + title: "Embedded bytecode compilation failed due to an HLSL compiler error", + messageFormat: "The shader of type {0} needs to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute), as dynamic shader compilation is not supported", + category: typeof(IShaderGenerator).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "When dynamic shader compilation is not supported (ie. when ComputeSharp.Dynamic is not referenced), all shaders need to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 8a75495f..e53ac4c4 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -32,26 +32,34 @@ public sealed partial class IShaderGenerator /// /// 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( INamedTypeSymbol structDeclarationSymbol, bool isDynamicShader, + bool supportsDynamicShaders, out ImmutableArray diagnostics) { + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + AttributeData? attribute = structDeclarationSymbol .GetAttributes() .FirstOrDefault(static a => a.AttributeClass?.ToDisplayString() == typeof(EmbeddedBytecodeAttribute).FullName); if (attribute is null) { - diagnostics = ImmutableArray.Empty; + // Emit the diagnostics if dynamic shaders are not supported + if (!supportsDynamicShaders) + { + builder.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol); + } + + diagnostics = builder.ToImmutable(); return new(true, 0, 0, 0); } - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); - // Validate the thread number arguments if (attribute.ConstructorArguments.Length != 3 || attribute.ConstructorArguments[0].Value is not int threadsX || diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 6e59bbf6..0e42e6af 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -29,6 +29,12 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator compilation.Options is CSharpCompilationOptions { AllowUnsafe: true } && compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.SkipLocalsInitAttribute") is not null); + // Check whether or not dynamic shaders are supported + IncrementalValueProvider supportsDynamicShaders = + context.CompilationProvider + .Select(static (compilation, token) => compilation.ReferencedAssemblyNames.Any( + static identity => identity.Name is "ComputeSharp.Dynamic")); + // Get all declared struct types and their type symbols IncrementalValuesProvider<(StructDeclarationSyntax Syntax, INamedTypeSymbol Symbol)> structDeclarations = context.SyntaxProvider @@ -73,20 +79,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // same step as parts are shared in following sub-branches in the incremental generator pipeline. IncrementalValuesProvider<(Result Dispatch, Result Hlsl, Result ThreadIds)> shaderInfoWithErrors = shaderDeclarations + .Combine(supportsDynamicShaders) .Combine(context.CompilationProvider) .Select(static (item, token) => { // LoadDispatchData() info ImmutableArray fieldInfos = LoadDispatchData.GetInfo( - item.Left.Symbol, - item.Left.Type, + item.Left.Left.Symbol, + item.Left.Left.Type, out int resourceCount, out int root32BitConstantCount, out ImmutableArray dispatchDataDiagnostics); DispatchDataInfo dispatchDataInfo = new( - item.Left.Hierarchy, - item.Left.Type, + item.Left.Left.Hierarchy, + item.Left.Left.Type, fieldInfos, resourceCount, root32BitConstantCount); @@ -94,14 +101,15 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // BuildHlslString() info HlslShaderSourceInfo hlslSourceInfo = BuildHlslString.GetInfo( item.Right, - item.Left.Syntax, - item.Left.Symbol, + item.Left.Left.Syntax, + item.Left.Left.Symbol, out ImmutableArray hlslSourceDiagnostics); // TryGetBytecode() info ThreadIdsInfo threadIds = LoadBytecode.GetInfo( - item.Left.Symbol, + item.Left.Left.Symbol, !hlslSourceInfo.Delegates.IsEmpty, + item.Left.Right, out ImmutableArray threadIdsDiagnostics); return ( @@ -140,12 +148,6 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}", SourceText.From(compilationUnit.ToFullString(), Encoding.UTF8)); }); - // Check whether or not dynamic shaders are supported - IncrementalValueProvider supportsDynamicShaders = - context.CompilationProvider - .Select(static (compilation, token) => compilation.ReferencedAssemblyNames.Any( - static identity => identity.Name is "ComputeSharp.Dynamic")); - // Get the filtered sequence to enable caching IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo SourceInfo, bool SupportsDynamicShaders)> hlslSourceInfo = shaderInfo