diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 02fa72d6..9507ad4e 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -52,3 +52,7 @@ CMPS0044 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentatio 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) +CMPS0048 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0049 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0050 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0051 | 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 c46faa7f..1e5cf347 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -630,4 +630,68 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true, description: "A shader type cannot implement more than one shader interface.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a dynamic shader annotated as an embedded bytecode. + /// + /// Format: "The shader of type {0} cannot be precompiled, as it is dynamic (it captures delegate fields)". + /// + /// + public static readonly DiagnosticDescriptor EmbeddedBytecodeWithDynamicShader = new( + id: "CMPS0048", + title: "Embedded bytecode for dynamic shader", + 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, + description: "A shader type cannot be precompiled if it captures one or more delegate values.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode shader with invalid thread ids values. + /// + /// Format: "The shader of type {0} is annotated with invalid thread ids values ({1}, {2}, {3})". + /// + /// + public static readonly DiagnosticDescriptor InvalidEmbeddedBytecodeThreadIds = new( + id: "CMPS0049", + 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}})", + category: typeof(IShaderGenerator).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The thread ids values for a shader marked as embedded bytecode have to be in the valid range.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// 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}")". + /// + /// + public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithWin32Exception = new( + id: "CMPS0050", + 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}}\")", + category: typeof(IShaderGenerator).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The embedded bytecode for a shader failed to be compiled due to a Win32 exception.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode shader failed due to an HLSL compilation exception. + /// + /// Format: "The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: "{1}")". + /// + /// + public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithHlslCompilationException = new( + id: "CMPS0051", + 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}}\")", + 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"); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs index 705f2b2f..78e78d53 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs @@ -8,11 +8,13 @@ using System.Runtime.InteropServices; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Shaders.Translation; +using ComputeSharp.SourceGenerators.Diagnostics; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; #pragma warning disable CS0618 @@ -52,51 +54,40 @@ public sealed partial class IShaderGenerator Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("bytecode")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("global::System.ReadOnlySpan"))) - .WithBody(GetShaderBytecodeBody(structDeclarationSymbol, isDynamicShader, hlslSource, out bytecodeLiterals)); + .WithBody(GetShaderBytecodeBody(context, structDeclarationSymbol, isDynamicShader, hlslSource, out bytecodeLiterals)); } /// /// Gets a instance with the logic to try to get a compiled shader bytecode. /// + /// The current generator context in use. /// The input instance to process. /// Indicates whether or not the shader is dynamic (ie. captures delegates). /// The generated HLSL source code (ignoring captured delegates, if present). /// The resulting bytecode literals to insert into the final source code. /// The instance to hash the input shader. [Pure] - private static unsafe BlockSyntax GetShaderBytecodeBody(INamedTypeSymbol structDeclarationSymbol, bool isDynamicShader, string hlslSource, out string? bytecodeLiterals) + private static unsafe BlockSyntax GetShaderBytecodeBody( + GeneratorExecutionContext context, + INamedTypeSymbol structDeclarationSymbol, + bool isDynamicShader, + string hlslSource, + out string? bytecodeLiterals) { AttributeData? attribute = structDeclarationSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(EmbeddedBytecodeAttribute) }); // No embedded shader was requested if (attribute is null) { - bytecodeLiterals = null; - - // This code produces a method declaration as follows: - // - // bytecode = default; - // - // return false; - return - Block( - ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - IdentifierName("bytecode"), - LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))), - ReturnStatement( - LiteralExpression( - SyntaxKind.FalseLiteralExpression))); + goto Failure; } // If the current shader is dynamic, emit a diagnostic error if (isDynamicShader) { - bytecodeLiterals = null; + context.ReportDiagnostic(EmbeddedBytecodeWithDynamicShader, structDeclarationSymbol, structDeclarationSymbol); - // TODO - return Block(); + goto Failure; } // Validate the thread number arguments @@ -107,16 +98,16 @@ public sealed partial class IShaderGenerator threadsY is < 1 or > 1024 || threadsZ is < 1 or > 64) { - bytecodeLiterals = null; + context.ReportDiagnostic(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol); - // TODO - return Block(); + goto Failure; } - LoadNativeDxcLibraries(); - try { + // Try to load dxcompiler.dll and dxil.dll + LoadNativeDxcLibraries(); + // Replace the actual thread num values hlslSource = hlslSource.Replace("", threadsX.ToString()).Replace("", threadsY.ToString()).Replace("", threadsZ.ToString()); @@ -184,20 +175,42 @@ public sealed partial class IShaderGenerator LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))), ReturnStatement(LiteralExpression(SyntaxKind.FalseLiteralExpression))); } - catch (Win32Exception) + catch (Win32Exception e) { - bytecodeLiterals = null; - - // TODO - return Block(); + context.ReportDiagnostic( + EmbeddedBytecodeFailedWithWin32Exception, + structDeclarationSymbol, + structDeclarationSymbol, + e.HResult, + e.Message); } - catch (HlslCompilationException) + catch (HlslCompilationException e) { - bytecodeLiterals = null; - - // TODO - return Block(); + context.ReportDiagnostic( + EmbeddedBytecodeFailedWithHlslCompilationException, + structDeclarationSymbol, + structDeclarationSymbol, + e.Message); } + + Failure: + bytecodeLiterals = null; + + // This code produces a method declaration as follows: + // + // bytecode = default; + // + // return false; + return + Block( + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + IdentifierName("bytecode"), + LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))), + ReturnStatement( + LiteralExpression( + SyntaxKind.FalseLiteralExpression))); } ///