From 76f27250f4a8fdca6fc570e59a965c3e21ea5b06 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Oct 2023 14:04:43 +0200 Subject: [PATCH] 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,