From 74f60ad5c2eafd297e80e4ec56ee136a4fad82ae Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Feb 2021 00:48:41 +0100 Subject: [PATCH] Added error for shaders with no resources --- .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++++++++ .../IComputeShaderSourceGenerator.cs | 25 +++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 0468bcb3..48acd529 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -70,5 +70,21 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A group shared field must be static.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for a shader with no resources. + /// + /// Format: "The compute shader of type {0} contains no resources to work on.". + /// + /// + public static DiagnosticDescriptor MissingShaderResources { get; } = new( + id: "CMPS0005", + title: "Missing shader resources", + messageFormat: "The compute shader of type {0} contains no resources to work on.", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "A compute shader must contain at least one resource.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index b690a243..bf54077a 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -93,6 +93,8 @@ namespace ComputeSharp.SourceGenerators INamedTypeSymbol structDeclarationSymbol, ICollection types) { + bool hlslResourceFound = false; + foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { if (fieldSymbol.IsStatic) continue; @@ -123,13 +125,18 @@ namespace ComputeSharp.SourceGenerators string metadataName = typeSymbol.GetFullMetadataName(); - // Track the type of items in the current buffer - if (HlslKnownTypes.IsStructuredBufferType(metadataName)) + // Allowed fields must be either resources, unmanaged values or delegates + if (HlslKnownTypes.IsTypedResourceType(metadataName)) { - types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); + hlslResourceFound = true; + + // Track the type of items in the current buffer + if (HlslKnownTypes.IsStructuredBufferType(metadataName)) + { + types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); + } } - else if (!HlslKnownTypes.IsTypedResourceType(metadataName) && - !typeSymbol.IsUnmanagedType && + else if (!typeSymbol.IsUnmanagedType && typeSymbol.TypeKind != TypeKind.Delegate) { // Shaders can only capture valid HLSL resource types or unmanaged types @@ -148,6 +155,14 @@ namespace ComputeSharp.SourceGenerators // Yield back the current mapping for the name (if the name used a reserved keyword) yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName }; } + + if (!hlslResourceFound) + { + context.ReportDiagnostic(Diagnostic.Create( + DiagnosticDescriptors.MissingShaderResources, + structDeclarationSymbol.Locations.FirstOrDefault(), + structDeclarationSymbol)); + } } ///