diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 2678d236..8d27f716 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -58,3 +58,8 @@ CMPSD2D0048 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0049 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0050 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0051 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0052 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0053 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0054 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0055 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0056 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 297e8903..a02ae0a9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.ComponentModel; -using System.Linq; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using ComputeSharp.D2D1.Exceptions; using ComputeSharp.D2D1.Shaders.Translation; @@ -12,11 +9,9 @@ using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; @@ -34,16 +29,20 @@ partial class D2DPixelShaderSourceGenerator /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. - public static string? GetHlslSource(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static string GetHlslSource(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { _ = methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData); if (!attributeData!.TryGetConstructorArgument(0, out string? hlslSource)) { - // TODO: no string diagnostic + diagnostics.Add( + InvalidD2DPixelShaderSource, + methodSymbol, + methodSymbol.Name, + methodSymbol.ContainingType); } - return hlslSource; + return hlslSource ?? ""; } /// @@ -52,7 +51,7 @@ partial class D2DPixelShaderSourceGenerator /// The collection of produced instances. /// The input instance to process. /// The shader profile to use to compile the shader, if present. - public static D2D1ShaderProfile? GetShaderProfile(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static D2D1ShaderProfile GetShaderProfile(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DEmbeddedBytecodeAttribute", out AttributeData? attributeData)) { @@ -64,9 +63,13 @@ partial class D2DPixelShaderSourceGenerator return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; } - // TODO: diagnostics + diagnostics.Add( + MissingShaderProfileForD2DPixelShaderSource, + methodSymbol, + methodSymbol.Name, + methodSymbol.ContainingType); - return null; + return D2D1ShaderProfile.PixelShader50; } /// @@ -75,7 +78,7 @@ partial class D2DPixelShaderSourceGenerator /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. - public static D2D1CompileOptions? GetCompileOptions(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static D2D1CompileOptions GetCompileOptions(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { @@ -87,9 +90,13 @@ partial class D2DPixelShaderSourceGenerator return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; } - // TODO: diagnostics + diagnostics.Add( + MissingCompileOptionsForD2DPixelShaderSource, + methodSymbol, + methodSymbol.Name, + methodSymbol.ContainingType); - return null; + return D2D1CompileOptions.Default; } /// @@ -118,8 +125,8 @@ partial class D2DPixelShaderSourceGenerator // Compile the shader bytecode using the requested parameters using ComPtr dxcBlobBytecode = D3DCompiler.Compile( sourceInfo.HlslSource.AsSpan(), - sourceInfo.ShaderProfile!.Value, - sourceInfo.CompileOptions!.Value); + sourceInfo.ShaderProfile, + sourceInfo.CompileOptions); token.ThrowIfCancellationRequested(); @@ -134,14 +141,16 @@ partial class D2DPixelShaderSourceGenerator catch (Win32Exception e) { diagnostic = new DiagnosticInfo( - null!, // TODO + D2DPixelShaderSourceCompilationFailedWithWin32Exception, + sourceInfo.MethodName, e.HResult, ID2D1ShaderGenerator.LoadBytecode.FixupExceptionMessage(e.Message)); } catch (FxcCompilationException e) { diagnostic = new DiagnosticInfo( - null!, // TODO + D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, + sourceInfo.MethodName, ID2D1ShaderGenerator.LoadBytecode.FixupExceptionMessage(e.Message)); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 81da5d56..6183d68c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -41,9 +41,9 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato // Get all necessary info for the current shader ImmutableArray modifiers = item.Syntax.Modifiers.Select(token => token.Kind()).ToImmutableArray(); string methodName = item.Symbol.Name; - string? hlslSource = Execute.GetHlslSource(diagnostics, item.Symbol) ?? ""; - D2D1ShaderProfile? shaderProfile = Execute.GetShaderProfile(diagnostics, item.Symbol); - D2D1CompileOptions? compileOptions = Execute.GetCompileOptions(diagnostics, item.Symbol); + string hlslSource = Execute.GetHlslSource(diagnostics, item.Symbol); + D2D1ShaderProfile shaderProfile = Execute.GetShaderProfile(diagnostics, item.Symbol); + D2D1CompileOptions compileOptions = Execute.GetCompileOptions(diagnostics, item.Symbol); HlslShaderMethodSourceInfo sourceInfo = new( modifiers, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 0a87ce13..17e40f99 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -463,7 +463,7 @@ partial class DiagnosticDescriptors /// Format: "The shader of type {0} failed to compile due to an HLSL compiler error (Message: "{1}")". /// /// - public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithDxcCompilationException = new DiagnosticDescriptor( + public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithFxcCompilationException = new DiagnosticDescriptor( id: "CMPSD2D0034", 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}\")", @@ -744,4 +744,84 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The element type of D2D1 resource texture fields in a D2D1 shader must be either float or float4.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid HLSL source in [D2DPixelShaderSource]. + /// + /// Format: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] with an invalid HLSL source argument". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DPixelShaderSource = new DiagnosticDescriptor( + id: "CMPSD2D0052", + title: "Invalid [D2DPixelShaderSource] HLSL source argument", + messageFormat: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] with an invalid HLSL source argument", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Methods using [D2DPixelShaderSource] must pass a valid string as the HLSL source argument.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode D2D shader failed due to a Win32 exception. + /// + /// Format: "Compiling the HLSL source for method \"{1}\" (in type {0}) failed due to a Win32 exception (HRESULT: {2:X8}, Message: \"{3}\")". + /// + /// + public static readonly DiagnosticDescriptor D2DPixelShaderSourceCompilationFailedWithWin32Exception = new DiagnosticDescriptor( + id: "CMPSD2D0053", + title: "D2D shader compilation failed due to Win32 exception", + messageFormat: "Compiling the HLSL source for method \"{1}\" (in type {0}) failed due to a Win32 exception (HRESULT: {2:X8}, Message: \"{3}\")", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The embedded bytecode for an input HLSl source failed to be compiled due to a Win32 exception.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode D2D shader failed due to an HLSL compilation exception. + /// + /// Format: "Compiling the HLSL source for method \"{1}\" (in type {0}) failed due to an HLSL compiler error (Message: \"{2}\")". + /// + /// + public static readonly DiagnosticDescriptor D2DPixelShaderSourceCompilationFailedWithFxcCompilationException = new DiagnosticDescriptor( + id: "CMPSD2D0054", + title: "D2D shader compilation failed due to an HLSL compiler error", + messageFormat: "Compiling the HLSL source for method \"{1}\" (in type {0}) failed due to an HLSL compiler error (Message: \"{2}\")", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The embedded bytecode for an input HLSl source failed to be compiled due to an HLSL compiler error.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode D2D shader with no shader profile set. + /// + /// Format: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has not specified the shader profile to use". + /// + /// + public static readonly DiagnosticDescriptor MissingShaderProfileForD2DPixelShaderSource = new DiagnosticDescriptor( + id: "CMPSD2D0055", + title: "Missing shader profile for D2D pixel shader source", + messageFormat: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has not specified the shader profile to use", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Precompiled shaders using [D2DPixelShaderSource] must explicitly indicate the shader profile to use.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode D2D shader with no compile options set. + /// + /// Format: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has not specified the compile options to use". + /// + /// + public static readonly DiagnosticDescriptor MissingCompileOptionsForD2DPixelShaderSource = new DiagnosticDescriptor( + id: "CMPSD2D0056", + title: "Missing compile options for D2D pixel shader source", + messageFormat: "The method \"{0}\" (in type {1}) is using [D2DPixelShaderSource] but has not specified the compile options to use", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "Precompiled shaders using [D2DPixelShaderSource] must explicitly indicate the compile options.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 97dacb14..61313333 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -198,7 +198,7 @@ partial class ID2D1ShaderGenerator catch (FxcCompilationException e) { options = default; - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); + diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithFxcCompilationException, FixupExceptionMessage(e.Message)); } End: diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs index d6662952..41563366 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs @@ -13,15 +13,15 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// The modifiers for the annotated method. /// The name of the annotated method. /// The HLSL source. -/// The shader profile to use to compile the shader, if requested. +/// The shader profile to use to compile the shader. /// The compile options to use to compile the shader. /// Whether any errors have been detected, and therefore the shader compilation should be skipped. internal sealed record HlslShaderMethodSourceInfo( ImmutableArray Modifiers, string MethodName, string HlslSource, - D2D1ShaderProfile? ShaderProfile, - D2D1CompileOptions? CompileOptions, + D2D1ShaderProfile ShaderProfile, + D2D1CompileOptions CompileOptions, bool HasErrors) { ///