From 5b971b1b3d5875d0e44e3ed2c3c34589245e7f64 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 13:59:06 +0200 Subject: [PATCH 1/3] Add NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase --- .../AnalyzerReleases.Shipped.md | 1 + ...haderDescriptorAttributeTargetAnalyzer.cs} | 6 +- ...ShaderDescriptorAttributeTargetAnalyzer.cs | 19 ++++ .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++ ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + ...erDescriptorAttributeTargetAnalyzerBase.cs | 91 +++++++++++++++++++ .../Extensions/ISymbolExtensions.cs | 11 +++ .../AnalyzerReleases.Shipped.md | 1 + ...ShaderDescriptorAttributeTargetAnalyzer.cs | 19 ++++ .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++ 10 files changed, 178 insertions(+), 3 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/{NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs => NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs} (58%) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs create mode 100644 src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs create mode 100644 src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index e98b7f21..82766b2e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -74,3 +74,4 @@ CMPSD2D0064 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0065 | ComputeSharp.D2D1.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0066 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0067 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0068 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs similarity index 58% rename from src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs rename to src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs index 6252c770..00dfe59a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer.cs @@ -7,12 +7,12 @@ namespace ComputeSharp.D2D1.SourceGenerators; /// [DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase +public sealed class NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer : NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase { /// - /// Creates a new instance. + /// Creates a new instance. /// - public NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer() + public NotAccessibleD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer() : base(NotAccessibleTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute, "ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") { } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..d01b4a97 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer : NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleFieldTypeInD2DGeneratedShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleFieldTypeInTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute, "ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index b167e000..9b4bda1c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -1008,4 +1008,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DGeneratedPixelShaderDescriptor] attribute requires target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a field in a type using the [D2DGeneratedPixelShaderDescriptor] attribute has a type that is not accessible from its containing assembly. + /// + /// Format: "The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleFieldTypeInTargetTypeForD2DGeneratedPixelShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPSD2D0068", + title: "Invalid [D2DGeneratedPixelShaderDescriptor] attribute target", + messageFormat: """The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)""", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DGeneratedPixelShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 702fe210..86c8f648 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -9,6 +9,7 @@ ComputeSharp.SourceGeneration.Hlsl + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs new file mode 100644 index 00000000..b0165b0f --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -0,0 +1,91 @@ +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace ComputeSharp.SourceGeneration.Diagnostics; + +/// +/// A diagnostic analyzer that generates an error if a field in a target shader type is of a type that is not accessible from its containing assembly. +/// +public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +{ + /// + /// The instance to use. + /// + private readonly DiagnosticDescriptor diagnosticDescriptor; + + /// + /// The fully qualified type name of the target attribute. + /// + private readonly string generatedShaderDescriptorFullyQualifiedTypeName; + + /// + /// Creates a new instance with the specified arguments. + /// + /// The instance to use. + /// The fully qualified type name of the target attribute. + private protected NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) + { + this.diagnosticDescriptor = diagnosticDescriptor; + this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; + + SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); + } + + /// + public sealed override ImmutableArray SupportedDiagnostics { get; } + + /// + public sealed override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(context => + { + // Get the symbol for the target attribute type + if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types are possible targets + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) + { + return; + } + + // If the target type is not using the attribute, there's nothing left to do + if (!typeSymbol.TryGetAttributeWithType(generatedShaderDescriptorAttributeSymbol, out AttributeData? attribute)) + { + return; + } + + foreach (ISymbol memberSymbol in typeSymbol.GetMembers()) + { + // Only select allowed fields (ie. skip constants, static fields, etc.) + if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol fieldTypeSymbol, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }) + { + continue; + } + + // Emit a diagnostic if the field type is not accessible + if (!fieldTypeSymbol.IsAccessibleFromCompilationAssembly(context.Compilation)) + { + context.ReportDiagnostic(Diagnostic.Create( + this.diagnosticDescriptor, + attribute.GetLocation(), + typeSymbol, + memberSymbol.Name, + fieldTypeSymbol)); + } + } + }, SymbolKind.NamedType); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 558e8bbf..52063552 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -34,6 +34,17 @@ internal static class ISymbolExtensions return compilation.IsSymbolAccessibleWithin(symbol, assemblySymbol); } + /// + /// Checks whether a given symbol is accessible from the assembly of a given compilation (including eg. through nested types). + /// + /// The input instance. + /// The instance currently in use. + /// Whether is accessible from the assembly for . + public static bool IsAccessibleFromCompilationAssembly(this ISymbol symbol, Compilation compilation) + { + return compilation.IsSymbolAccessibleWithin(symbol, compilation.Assembly); + } + /// /// Gets the fully qualified name for a given symbol. /// diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 731e8941..d89faabf 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -61,3 +61,4 @@ CMPS0052 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Ser CMPS0053 | ComputeSharp.Shaders | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0054 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0055 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0056 | ComputeSharp.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs new file mode 100644 index 00000000..dd0672aa --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer.cs @@ -0,0 +1,19 @@ +using ComputeSharp.SourceGeneration.Diagnostics; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.SourceGenerators; + +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer : NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase +{ + /// + /// Creates a new instance. + /// + public NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer() + : base(NotAccessibleFieldTypeInTargetTypeForGeneratedComputeShaderDescriptorAttribute, "ComputeSharp.GeneratedComputeShaderDescriptorAttribute") + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 60469ac0..e6536130 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -784,4 +784,20 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [GeneratedComputeShaderDescriptor] attribute requires target types to be accessible from their containing assembly.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when a field in a type using the [GeneratedComputeShaderDescriptor] attribute has a type that is not accessible from its containing assembly. + /// + /// Format: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)". + /// + /// + public static readonly DiagnosticDescriptor NotAccessibleFieldTypeInTargetTypeForGeneratedComputeShaderDescriptorAttribute = new DiagnosticDescriptor( + id: "CMPS0056", + title: "Invalid [GeneratedComputeShaderDescriptor] attribute target", + messageFormat: """The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly (type {0} has a field "{1}" of type {2} that has less effective accessibility than internal)""", + category: "ComputeSharp.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [GeneratedComputeShaderDescriptor] attribute requires the type of all fields of target types to be accessible from their containing assembly.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file From 17527764b914d3985887f99fcc6c08bb354820e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 14:01:03 +0200 Subject: [PATCH 2/3] Add unit test for new analyzer --- .../AnalyzerTests.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs index 6861d648..ec84415e 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/AnalyzerTests.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Tests.SourceGenerators; public class AnalyzerTests { [TestMethod] - public async Task InvalidShaderField() + public async Task NotAccessibleNestedShaderType() { const string source = """ using ComputeSharp; @@ -33,4 +33,35 @@ public class AnalyzerTests await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); } + + [TestMethod] + public async Task NotAccessibleShaderFieldType() + { + const string source = """ + using ComputeSharp; + + namespace MyFancyApp.Sample; + + public partial class Foo + { + [{|CMPS0056:GeneratedComputeShaderDescriptor|}] + private partial struct MyShader : IComputeShader + { + public ReadWriteBuffer buffer; + public Bar bar; + + public void Execute() + { + } + } + + private struct Bar + { + public int X; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); + } } \ No newline at end of file From 76f27250f4a8fdca6fc570e59a965c3e21ea5b06 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 14:04:43 +0200 Subject: [PATCH 3/3] Skip shader fields of not accessible types --- ...elShaderDescriptorGenerator.LoadConstantBuffer.cs | 12 +++++++++++- .../D2DPixelShaderDescriptorGenerator.cs | 1 + ...eShaderDescriptorGenerator.DispatchDataLoading.cs | 12 +++++++++++- .../ComputeShaderDescriptorGenerator.cs | 1 + 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs index 7e1e5bc4..cb0e00d6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs @@ -25,16 +25,19 @@ partial class D2DPixelShaderDescriptorGenerator /// Explores a given type hierarchy and generates statements to load fields. /// /// The collection of produced instances. + /// The input object currently in use. /// The current shader type being explored. /// The size of the shader constant buffer. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( ImmutableArrayBuilder diagnostics, + Compilation compilation, ITypeSymbol structDeclarationSymbol, out int constantBufferSizeInBytes) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( + Compilation compilation, ITypeSymbol currentTypeSymbol, ImmutableArray fieldPath, StrongBox rawDataOffset) @@ -47,6 +50,12 @@ partial class D2DPixelShaderDescriptorGenerator where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } select fieldSymbol) { + // Skip fields of not accessible types (the analyzer will handle this) + if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation)) + { + continue; + } + string fieldName = fieldSymbol.Name; string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName(); @@ -65,7 +74,7 @@ partial class D2DPixelShaderDescriptorGenerator else if (fieldSymbol.Type.IsUnmanagedType) { // Custom struct type defined by the user - foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(fieldSymbol.Type, fieldPath.Add(fieldName), rawDataOffset)) + foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), rawDataOffset)) { yield return fieldInfo; } @@ -78,6 +87,7 @@ partial class D2DPixelShaderDescriptorGenerator // Traverse all shader fields and gather info, and update the tracking offsets ImmutableArray fieldInfos = GetCapturedFieldInfos( + compilation, structDeclarationSymbol, ImmutableArray.Empty, rawDataOffsetAsBox).ToImmutableArray(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 8ab8cc4f..089fb533 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -85,6 +85,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene // LoadDispatchData() info ImmutableArray fieldInfos = LoadConstantBuffer.GetInfo( diagnostics, + context.SemanticModel.Compilation, typeSymbol, out int constantBufferSizeInBytes); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs index f94f561f..c9c25e3f 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs @@ -23,6 +23,7 @@ partial class ComputeShaderDescriptorGenerator /// Explores a given type hierarchy and generates statements to load fields. /// /// The collection of produced instances. + /// The input object currently in use. /// The current shader type being explored. /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The size of the shader constant buffer. @@ -31,6 +32,7 @@ partial class ComputeShaderDescriptorGenerator /// The sequence of instances for all captured resources. public static void GetInfo( ImmutableArrayBuilder diagnostics, + Compilation compilation, ITypeSymbol structDeclarationSymbol, bool isPixelShaderLike, out int constantBufferSizeInBytes, @@ -39,6 +41,7 @@ partial class ComputeShaderDescriptorGenerator out ImmutableArray resources) { static void GetInfo( + Compilation compilation, ITypeSymbol currentTypeSymbol, ImmutableArray fieldPath, ref int resourceOffset, @@ -56,6 +59,12 @@ partial class ComputeShaderDescriptorGenerator continue; } + // Skip fields of not accessible types (the analyzer will handle this) + if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation)) + { + continue; + } + string fieldName = fieldSymbol.Name; string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName(); @@ -78,7 +87,7 @@ partial class ComputeShaderDescriptorGenerator else if (fieldSymbol.Type.IsUnmanagedType) { // Custom struct type defined by the user - GetInfo(fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources); + GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources); } } } @@ -93,6 +102,7 @@ partial class ComputeShaderDescriptorGenerator { // Traverse all shader fields and gather info, and update the tracking offsets GetInfo( + compilation, structDeclarationSymbol, ImmutableArray.Empty, ref resourceOffset, diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index d25f8e90..7de05e5b 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -60,6 +60,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener // Get the fields info DispatchDataLoading.GetInfo( diagnostics, + context.SemanticModel.Compilation, typeSymbol, isPixelShaderLike, out int constantBufferSizeInBytes,