Add diagnostics for [D2D] attributes

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:31 +02:00
1 親 d0d254b054
コミット a67237370a
3ファイル変更
+139 -2

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

+5
ファイルの表示
@@ -41,3 +41,8 @@ CMPSD2D0031 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0032 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0033 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0034 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0035 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0036 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0037 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0038 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0039 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+81 -1
ファイルの表示
@@ -459,7 +459,7 @@ internal static class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an embedded bytecode shader failed due to an HLSL compilation exception.
/// <para>
/// Format: <c>"The shader of type {{0}} failed to compile due to an HLSL compiler error (Message: "{1}")"</c>.
/// Format: <c>"The shader of type {0} failed to compile due to an HLSL compiler error (Message: "{1}")"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor EmbeddedBytecodeFailedWithDxcCompilationException = new DiagnosticDescriptor(
@@ -471,4 +471,84 @@ internal static class DiagnosticDescriptors
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 invalid D2D input count value.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using an incorrect value for [D2DInputCount] (the number of inputs must be in the [1, 8] range)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DInputCount = new DiagnosticDescriptor(
id: "CMPSD2D0035",
title: "Invalid D2D1 shader input count",
messageFormat: "The D2D1 shader of type {0} is using an incorrect value for [D2DInputCount] (the number of inputs must be in the [1, 8] range)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader must have a number of inputs in the [1, 8] range.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for repeated indices for D2D simple indices.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputSimple] attributes"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor RepeatedD2DInputSimpleIndices = new DiagnosticDescriptor(
id: "CMPSD2D0036",
title: "Repeated D2D1 shader simple input indices",
messageFormat: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputSimple] attributes",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader must only have unique indices for all of its [D2DInputSimple] attributes.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for repeated indices for D2D complex indices.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputComplex] attributes"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor RepeatedD2DInputComplexIndices = new DiagnosticDescriptor(
id: "CMPSD2D0037",
title: "Repeated D2D1 shader complex input indices",
messageFormat: "The D2D1 shader of type {0} is using repeated indices for some of its [D2DInputComplex] attributes",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader must only have unique indices for all of its [D2DInputComplex] attributes.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for invalid combination of simple and complex indices.
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using some indices in common for simple and complex input indices"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidSimpleAndComplexIndicesCombination = new DiagnosticDescriptor(
id: "CMPSD2D0038",
title: "Invalid D2D1 shader simple and complex indices combination",
messageFormat: "The D2D1 shader of type {0} is using some indices in common for simple and complex input indices",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader cannot use the same indices to indicate both simple and complex inputs.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader with an out of range input index (or more).
/// <para>
/// Format: <c>"The D2D1 shader of type {0} is using some out of range input indices"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor OutOfRangeInputIndex = new DiagnosticDescriptor(
id: "CMPSD2D0039",
title: "Our of range D2D1 shader input indices",
messageFormat: "The D2D1 shader of type {0} is using some out of range input indices",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A D2D1 shader must have all the indices of its simple and complex inputs in the valid range (between 1 and the number of declared inputs).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
@@ -60,6 +60,7 @@ partial class ID2D1ShaderGenerator
// Gather the shader metadata
GatherD2D1AttributeInfo(
builder,
structDeclarationSymbol,
out int inputCount,
out ImmutableArray<int> inputSimpleIndices,
@@ -365,18 +366,25 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Extracts the metadata definition for the current shader.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
/// <param name="requiresScenePosition">Whether the shader requires the scene position.</param>
private static void GatherD2D1AttributeInfo(
ImmutableArray<Diagnostic>.Builder diagnostics,
INamedTypeSymbol structDeclarationSymbol,
out int inputCount,
out ImmutableArray<int> inputSimpleIndices,
out ImmutableArray<int> inputComplexIndices,
out bool requiresScenePosition)
{
// We need a separate local here so we can keep the original value to apply
// diagnostics to, but without returning invalid values to the caller which
// might cause generator errors (eg. -1 would cause other code to just throw).
int rawInputCount = 0;
inputCount = 0;
requiresScenePosition = false;
@@ -388,7 +396,8 @@ partial class ID2D1ShaderGenerator
switch (attributeData.AttributeClass?.GetFullMetadataName())
{
case "ComputeSharp.D2D1.D2DInputCountAttribute":
inputCount = (int)attributeData.ConstructorArguments[0].Value!;
rawInputCount = (int)attributeData.ConstructorArguments[0].Value!;
inputCount = Math.Max(rawInputCount, 0);
break;
case "ComputeSharp.D2D1.D2DInputSimpleAttribute":
inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!);
@@ -406,6 +415,49 @@ partial class ID2D1ShaderGenerator
inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable();
inputComplexIndices = inputComplexIndicesBuilder.ToImmutable();
// Validate the input count
if (rawInputCount is not (>= 1 and <= 8))
{
diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
return;
}
HashSet<int> inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
HashSet<int> inputComplexIndicesAsSet = new(inputComplexIndicesBuilder);
// All simple indices must be unique
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
{
diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
return;
}
// All complex indices must be unique
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
{
diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
return;
}
// Simple and complex indices can't have indices in common
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
{
diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
return;
}
int maxAllowedInputIndex = rawInputCount - 1;
// All simple indices must be in range
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => i < 0 || i > maxAllowedInputIndex))
{
diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>