From 0bfe836a4ebe1228eda58451571ed92ef9db128d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 22 May 2022 01:40:09 +0200 Subject: [PATCH] Capture D2D1CompileOptions in source generator --- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++++++++ ...haderGenerator.CreateLoadBytecodeMethod.cs | 26 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 2 ++ .../Models/HlslShaderSourceInfo.cs | 8 +++++- 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index 89d1c79e..72d09bf1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -50,3 +50,4 @@ CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0042 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0043 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0044 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 9a54a56e..0e8167b3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -618,4 +618,20 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true, description: "A D2D1 shader must only have unique indices for all of its [D2DInputDescription] attributes.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for the PackMatrixColumnMajor option being used. + /// + /// Format: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute". + /// + /// + public static readonly DiagnosticDescriptor InvalidPackMatrixColumnMajorOption = new DiagnosticDescriptor( + id: "CMPSD2D0044", + title: "Invalid PackMatrixColumnMajor compile option", + messageFormat: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A D2D1 shader generated with ComputeSharp.D2D1 cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.", + 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 a9454d6b..417ce8e9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -41,6 +41,32 @@ partial class ID2D1ShaderGenerator return null; } + /// + /// Extracts the compile options for the current shader. + /// + /// 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, INamedTypeSymbol structDeclarationSymbol) + { + if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) + { + D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; + + if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0) + { + diagnostics.Add( + InvalidPackMatrixColumnMajorOption, + structDeclarationSymbol, + structDeclarationSymbol); + } + + return options; + } + + return null; + } + /// /// Extracts the metadata definition for the current shader. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 3ac5ab2e..e79cf32c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -93,11 +93,13 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the shader profile and linking info for LoadBytecode() D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol); + D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol); bool isLinkingSupported = diagnostics.Count == 0 && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount); HlslShaderSourceInfo sourceInfo = new( hlslSource, shaderProfile, + compileOptions, isLinkingSupported, diagnostics.Count > 0); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs index 76f7c60d..efb0eb1a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderSourceInfo.cs @@ -5,6 +5,12 @@ /// /// The HLSL source. /// The shader profile to use to compile the shader, if requested. +/// The compile options to use to compile the shader. /// Whether the shader supports linking. /// Whether any errors have been detected, and therefore the shader compilation should be skipped. -internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, bool HasErrors); +internal sealed record HlslShaderSourceInfo( + string HlslSource, + D2D1ShaderProfile? ShaderProfile, + D2D1CompileOptions? CompileOptions, + bool IsLinkingSupported, + bool HasErrors);