From ba9f421c97ff58286543d497c7e57c688e5c8140 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Oct 2023 16:53:47 +0200 Subject: [PATCH] Simplify and optimize ConstantBuffer.GetInfo method --- ...haderDescriptorGenerator.ConstantBuffer.cs | 59 ++++++++++--------- .../D2DPixelShaderDescriptorGenerator.cs | 5 +- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs index f528dd71..18bb162f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; -using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; @@ -28,28 +25,32 @@ partial class D2DPixelShaderDescriptorGenerator /// 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( + /// The sequence of instances for all captured values. + public static void GetInfo( ImmutableArrayBuilder diagnostics, Compilation compilation, ITypeSymbol structDeclarationSymbol, - out int constantBufferSizeInBytes) + out int constantBufferSizeInBytes, + out ImmutableArray fields) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) - static IEnumerable GetCapturedFieldInfos( + static void GetInfo( Compilation compilation, ITypeSymbol currentTypeSymbol, ImmutableArray fieldPath, - StrongBox rawDataOffset) + ref int rawDataOffset, + ImmutableArrayBuilder fields) { bool isFirstField = true; - foreach ( - IFieldSymbol fieldSymbol in - from fieldSymbol in currentTypeSymbol.GetMembers().OfType() - where fieldSymbol is { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } - select fieldSymbol) + foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers()) { + // Only process fields of a valid shape/type + if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } fieldSymbol) + { + continue; + } + // Skip fields of not accessible types (the analyzer will handle this) if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation)) { @@ -62,7 +63,7 @@ partial class D2DPixelShaderDescriptorGenerator // The first item in each nested struct needs to be aligned to 16 bytes if (isFirstField && fieldPath.Length > 0) { - rawDataOffset.Value = AlignmentHelper.Pad(rawDataOffset.Value, 16); + rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16); isFirstField = false; } @@ -71,7 +72,7 @@ partial class D2DPixelShaderDescriptorGenerator { ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName)); - yield return GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset.Value); + fields.Add(GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset)); } else if (fieldSymbol.Type.IsUnmanagedType) { @@ -79,25 +80,27 @@ partial class D2DPixelShaderDescriptorGenerator ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, nestedTypeName)); // Custom struct type defined by the user - foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(compilation, fieldSymbol.Type, nestedFieldPath, rawDataOffset)) - { - yield return fieldInfo; - } + GetInfo(compilation, fieldSymbol.Type, nestedFieldPath, ref rawDataOffset, fields); } } } // Setup the resource and byte offsets for tracking - StrongBox rawDataOffsetAsBox = new(); + int rawDataOffset = 0; - // Traverse all shader fields and gather info, and update the tracking offsets - ImmutableArray fieldInfos = GetCapturedFieldInfos( - compilation, - structDeclarationSymbol, - ImmutableArray.Empty, - rawDataOffsetAsBox).ToImmutableArray(); + using (ImmutableArrayBuilder fieldBuilder = new()) + { + // Traverse all shader fields and gather info, and update the tracking offsets + GetInfo( + compilation, + structDeclarationSymbol, + ImmutableArray.Empty, + ref rawDataOffset, + fieldBuilder); - constantBufferSizeInBytes = rawDataOffsetAsBox.Value; + constantBufferSizeInBytes = rawDataOffset; + fields = fieldBuilder.ToImmutable(); + } // The maximum size for a constant buffer is 64KB const int D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT = 4096; @@ -108,8 +111,6 @@ partial class D2DPixelShaderDescriptorGenerator { diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); } - - return fieldInfos; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index a8cc08f6..5f6d96c4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -83,11 +83,12 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene using ImmutableArrayBuilder diagnostics = new(); // Constant buffer info - ImmutableArray fieldInfos = ConstantBuffer.GetInfo( + ConstantBuffer.GetInfo( diagnostics, context.SemanticModel.Compilation, typeSymbol, - out int constantBufferSizeInBytes); + out int constantBufferSizeInBytes, + out ImmutableArray fieldInfos); token.ThrowIfCancellationRequested();