Add additional diagnostics for invalid shader discovered types

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:33 +02:00
1 親 7318f5bd54
コミット d51c0f5bf0
9ファイル変更
+173 -32

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

+1
ファイルの表示
@@ -56,3 +56,4 @@ CMPS0046 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser
CMPS0047 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0048 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0049 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0050 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
+16
ファイルの表示
@@ -696,4 +696,20 @@ internal static class DiagnosticDescriptors
isEnabledByDefault: true,
description: "Shaders can only invoke methods that are either HLSL intrinsics or with source available for analysis.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid discovered type.
/// <para>
/// Format: <c>"The compute shader or method {0} uses the invalid type {1}"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidDiscoveredType = new DiagnosticDescriptor(
id: "CMPS0050",
title: "Invalid discovered type",
messageFormat: "The compute shader or method {0} uses the invalid type {1} (only some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used)",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Shaders and shader methods can only use supported types (some .NET primitives and vector types, HLSL primitive, vector and matrix types, and custom types containing these types can be used).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+16 -3
ファイルの表示
@@ -67,7 +67,7 @@ partial class IShaderGenerator
var sharedBuffers = GetSharedBuffers(builder, structDeclarationSymbol, discoveredTypes);
var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader);
var implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
var declaredTypes = GetDeclaredTypes(discoveredTypes);
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
var definedConstants = GetDefinedConstants(constantDefinitions);
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -432,12 +432,19 @@ partial class IShaderGenerator
/// Gets the sequence of processed discovered custom types.
/// </summary>
/// <param name="types">The sequence of discovered custom types.</param>
/// <param name="sourceSymbol">The symbol for the current object being processed.</param>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(IEnumerable<INamedTypeSymbol> types)
internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(
ImmutableArray<Diagnostic>.Builder diagnostics,
ISymbol sourceSymbol,
IEnumerable<INamedTypeSymbol> types)
{
ImmutableArray<(string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string)>();
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
foreach (var type in HlslKnownTypes.GetCustomTypes(types))
// Process the discovered types
foreach (var type in HlslKnownTypes.GetCustomTypes(types, out invalidTypes))
{
var structType = type.GetFullMetadataName().ToHlslIdentifierName();
var structDeclaration = StructDeclaration(structType);
@@ -470,6 +477,12 @@ partial class IShaderGenerator
.ToFullString()));
}
// Process the invalid types
foreach (INamedTypeSymbol invalidType in invalidTypes)
{
diagnostics.Add(InvalidDiscoveredType, sourceSymbol, sourceSymbol, invalidType);
}
return builder.ToImmutable();
}
+10 -9
ファイルの表示
@@ -89,16 +89,20 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
IMethodSymbol methodSymbol,
out ImmutableArray<Diagnostic> diagnostics)
{
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// We need to sets to track all discovered custom types and static methods
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var semanticModel = new SemanticModelProvider(compilation);
var (entryPoint, dependentMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, constantDefinitions, out diagnostics);
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(discoveredTypes);
var (entryPoint, dependentMethods) = GetProcessedMethods(builder, methodDeclaration, semanticModel, discoveredTypes, constantDefinitions);
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(builder, methodSymbol, discoveredTypes);
var definedConstants = IShaderGenerator.BuildHlslSource.GetDefinedConstants(constantDefinitions);
diagnostics = builder.ToImmutable();
return new(
methodSymbol.GetFullMetadataName(includeParameters: true),
entryPoint,
@@ -110,25 +114,22 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
/// <summary>
/// Gets a sequence of processed methods from a target method declaration.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> instance for the current method.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the method to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and all captured methods).</returns>
private static (string TargetMethod, ImmutableArray<(string Signature, string Definition)> DependentMethods) GetProcessedMethods(
ImmutableArray<Diagnostic>.Builder diagnostics,
MethodDeclarationSyntax methodDeclaration,
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
out ImmutableArray<Diagnostic> diagnostics)
IDictionary<IFieldSymbol, string> constantDefinitions)
{
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, builder);
diagnostics = builder.ToImmutable();
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, diagnostics);
// Rewrite the method syntax tree
var targetMethod = shaderSourceRewriter