Add diagnostic when [EmbeddedBytecode] is missing

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-19 16:47:34 +01:00
1 親 b9313ca6d8
コミット 71fad3a8fd
4ファイル変更
+49 -22

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -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)
+22 -6
ファイルの表示
@@ -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
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader failed due to a Win32 exception.
/// <para>
/// Format: <c>"The shader of type {{0}} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: "{2}")"</c>.
/// Format: <c>"The shader of type {0} failed to compile due to a Win32 exception (HRESULT: {1:X8}, Message: "{2}")"</c>.
/// </para>
/// </summary>
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");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader without the embedded bytecode attribute, when dynamic shaders are not supported.
/// <para>
/// Format: <c>"The shader of type {0} needs to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute), as dynamic shader compilation is not supported"</c>.
/// </para>
/// </summary>
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");
}
+11 -3
ファイルの表示
@@ -32,26 +32,34 @@ public sealed partial class IShaderGenerator
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="isDynamicShader">Indicates whether or not the shader is dynamic (ie. captures delegates).</param>
/// <param name="supportsDynamicShaders">Indicates whether or not dynamic shaders are supported.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>The thread ids for the precompiled shader, if available.</returns>
public static ThreadIdsInfo GetInfo(
INamedTypeSymbol structDeclarationSymbol,
bool isDynamicShader,
bool supportsDynamicShaders,
out ImmutableArray<Diagnostic> diagnostics)
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
AttributeData? attribute = structDeclarationSymbol
.GetAttributes()
.FirstOrDefault(static a => a.AttributeClass?.ToDisplayString() == typeof(EmbeddedBytecodeAttribute).FullName);
if (attribute is null)
{
diagnostics = ImmutableArray<Diagnostic>.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<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// Validate the thread number arguments
if (attribute.ConstructorArguments.Length != 3 ||
attribute.ConstructorArguments[0].Value is not int threadsX ||
+15 -13
ファイルの表示
@@ -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<bool> 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<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> Hlsl, Result<ThreadIdsInfo> ThreadIds)> shaderInfoWithErrors =
shaderDeclarations
.Combine(supportsDynamicShaders)
.Combine(context.CompilationProvider)
.Select(static (item, token) =>
{
// LoadDispatchData() info
ImmutableArray<FieldInfo> 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<Diagnostic> 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<Diagnostic> hlslSourceDiagnostics);
// TryGetBytecode() info
ThreadIdsInfo threadIds = LoadBytecode.GetInfo(
item.Left.Symbol,
item.Left.Left.Symbol,
!hlslSourceInfo.Delegates.IsEmpty,
item.Left.Right,
out ImmutableArray<Diagnostic> 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<bool> 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