diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs index 33386412..14511432 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs @@ -2,6 +2,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; namespace ComputeSharp.D2D1.SourceGenerators; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs index a6232d49..42d8754b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs @@ -2,6 +2,7 @@ using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; namespace ComputeSharp.D2D1.SourceGenerators; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs index 7f7dd538..7e1e5bc4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using System.Linq; using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; -using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index f8c64c28..5c009c45 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -21,6 +21,7 @@ + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs similarity index 50% rename from src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs rename to src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs index c49ba8b6..10da8292 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/FieldInfo.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs @@ -1,25 +1,27 @@ using ComputeSharp.SourceGeneration.Helpers; -namespace ComputeSharp.D2D1.SourceGenerators.Models; +namespace ComputeSharp.SourceGeneration.Models; /// /// A base type for a model describing a given captured field in a shader. /// -internal abstract record FieldInfo +/// The path of the field with respect to the shader instance. +/// The full metadata name for the primitive type. +internal abstract record FieldInfo(EquatableArray FieldPath, string TypeName) { /// /// A captured primitive value (either a scalar, a vector, or a linear matrix). /// - /// The path of the field with respect to the shader instance. - /// The full metadata name for the primitive type. + /// + /// /// The byte offset of the value within the root signature. - public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo; + public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo(FieldPath, TypeName); /// /// A captured non linear matrix value. /// - /// The path of the field with respect to the shader instance. - /// The full metadata name for the matrix type. + /// + /// /// The name of each element in the matrix type. /// The number of rows in the matrix. /// The number of columns in the matrix. @@ -30,5 +32,5 @@ internal abstract record FieldInfo string ElementName, int Rows, int Columns, - EquatableArray Offsets) : FieldInfo; + EquatableArray Offsets) : FieldInfo(FieldPath, TypeName); } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs index 9deae88d..7a8669d7 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs @@ -1,5 +1,6 @@ using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Models; namespace ComputeSharp.SourceGenerators; @@ -103,12 +104,9 @@ partial class ComputeShaderDescriptorGenerator using (writer.WriteBlock()) { // Generate loading statements for each captured resource - foreach (FieldInfo fieldInfo in info.Fields) + foreach (ResourceInfo resource in info.Resources) { - if (fieldInfo is FieldInfo.Resource resource) - { - writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});"); - } + writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});"); } } } diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs index e95b012a..f94f561f 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.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; @@ -30,79 +27,89 @@ partial class ComputeShaderDescriptorGenerator /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The size of the shader constant buffer. /// The total number of captured resources in the shader. - /// The sequence of instances for all captured resources and values. - public static ImmutableArray GetInfo( + /// The sequence of instances for all captured values. + /// The sequence of instances for all captured resources. + public static void GetInfo( ImmutableArrayBuilder diagnostics, ITypeSymbol structDeclarationSymbol, bool isPixelShaderLike, out int constantBufferSizeInBytes, - out int resourceCount) + out int resourceCount, + out ImmutableArray fields, + out ImmutableArray resources) { - // Helper method that uses boxes instead of ref-s (illegal in enumerators) - static IEnumerable GetCapturedFieldInfos( + static void GetInfo( ITypeSymbol currentTypeSymbol, ImmutableArray fieldPath, - StrongBox resourceOffset, - StrongBox rawDataOffset) + ref int resourceOffset, + ref int rawDataOffset, + ImmutableArrayBuilder fields, + ImmutableArrayBuilder resources) { 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 + if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } fieldSymbol) + { + continue; + } + string fieldName = fieldSymbol.Name; string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName(); // 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; } if (HlslKnownTypes.IsTypedResourceType(typeName)) { - yield return new FieldInfo.Resource(fieldName, typeName, resourceOffset.Value++); + resources.Add(new ResourceInfo(fieldName, typeName, resourceOffset++)); } else if (HlslKnownTypes.IsKnownHlslType(typeName)) { - yield return GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset.Value); + fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(fieldName), typeName, ref rawDataOffset)); } else if (fieldSymbol.Type.IsUnmanagedType) { // Custom struct type defined by the user - foreach (FieldInfo fieldInfo in GetCapturedFieldInfos(fieldSymbol.Type, fieldPath.Add(fieldName), resourceOffset, rawDataOffset)) - { - yield return fieldInfo; - } + GetInfo(fieldSymbol.Type, fieldPath.Add(fieldName), ref resourceOffset, ref rawDataOffset, fields, resources); } } } // Setup the resource and byte offsets for tracking. Pixel shaders have only two // implicitly captured int values, as they're always dispatched over a 2D texture. - int initialRawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3); - StrongBox resourceOffsetAsBox = new(); - StrongBox rawDataOffsetAsBox = new(initialRawDataOffset); + int rawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3); + int resourceOffset = 0; - // Traverse all shader fields and gather info, and update the tracking offsets - ImmutableArray fieldInfos = GetCapturedFieldInfos( - structDeclarationSymbol, - ImmutableArray.Empty, - resourceOffsetAsBox, - rawDataOffsetAsBox).ToImmutableArray(); + using (ImmutableArrayBuilder fieldBuilder = new()) + using (ImmutableArrayBuilder resourceBuilder = new()) + { + // Traverse all shader fields and gather info, and update the tracking offsets + GetInfo( + structDeclarationSymbol, + ImmutableArray.Empty, + ref resourceOffset, + ref rawDataOffset, + fieldBuilder, + resourceBuilder); - resourceCount = resourceOffsetAsBox.Value; + resourceCount = resourceOffset; + fields = fieldBuilder.ToImmutable(); + resources = resourceBuilder.ToImmutable(); + } // After all the captured fields have been processed, ensure the reported byte size for // the local variables is padded to a multiple of a 32 bit value. This is necessary to // enable loading all the dispatch data after reinterpreting it to a sequence of values // of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds. - constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)); + constantBufferSizeInBytes = AlignmentHelper.Pad(rawDataOffset, sizeof(int)); // A shader root signature has a maximum size of 64 DWORDs, so 256 bytes. // Loaded values in the root signature have the following costs: @@ -118,8 +125,6 @@ partial class ComputeShaderDescriptorGenerator { diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); } - - return fieldInfos; } /// diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.cs index 2adf9096..ab12214b 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.cs @@ -18,12 +18,12 @@ partial class ComputeShaderDescriptorGenerator /// /// Indicates whether the current shader uses an implicit texture. /// Whether the static sampler is used by the shader. - /// The sequence of captured fields for the shader. + /// The sequence of captured resources for the shader. /// The metadata info for the shader. public static ImmutableArray GetInfo( bool isImplicitTextureUsed, bool isSamplerUsed, - ImmutableArray capturedFields) + ImmutableArray resources) { using ImmutableArrayBuilder descriptors = new(); @@ -38,7 +38,7 @@ partial class ComputeShaderDescriptorGenerator } // Populate the sequence of resource descriptors - foreach (FieldInfo.Resource resource in capturedFields.OfType()) + foreach (ResourceInfo resource in resources) { if (HlslKnownTypes.IsConstantBufferType(resource.TypeName)) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index f81d68b9..ce59ce78 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -51,13 +51,15 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener using ImmutableArrayBuilder diagnostics = new(); - // Dispatch data loading info - ImmutableArray fieldInfos = DispatchDataLoading.GetInfo( + // Get the fields info + DispatchDataLoading.GetInfo( diagnostics, typeSymbol, isPixelShaderLike, out int constantBufferSizeInBytes, - out int resourceCount); + out int resourceCount, + out ImmutableArray fieldInfos, + out ImmutableArray resourceInfo); token.ThrowIfCancellationRequested(); @@ -91,7 +93,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener ImmutableArray resourceDescriptors = ResourceDescriptorRanges.GetInfo( isImplicitTextureUsed, isSamplerUsed, - fieldInfos); + resourceInfo); token.ThrowIfCancellationRequested(); @@ -122,6 +124,7 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener ConstantBufferSizeInBytes: constantBufferSizeInBytes, ResourceCount: resourceCount, Fields: fieldInfos, + Resources: resourceInfo, ResourceDescriptors: resourceDescriptors, HlslInfoKey: hlslInfoKey, HlslInfo: hlslInfo, diff --git a/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs b/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs deleted file mode 100644 index 207507f7..00000000 --- a/src/ComputeSharp.SourceGenerators/Models/FieldInfo.cs +++ /dev/null @@ -1,42 +0,0 @@ -using ComputeSharp.SourceGeneration.Helpers; - -namespace ComputeSharp.SourceGenerators.Models; - -/// -/// A base type for a model describing a given captured field in a shader. -/// -internal abstract record FieldInfo -{ - /// - /// A captured resource (either a buffer or a texture). - /// - /// The name of the resource field. - /// The full metadata name for the resource type. - /// The offset for the resource within the root signature. - public sealed record Resource(string FieldName, string TypeName, int Offset) : FieldInfo; - - /// - /// A captured primitive value (either a scalar, a vector, or a linear matrix). - /// - /// The path of the field with respect to the shader instance. - /// The full metadata name for the primitive type. - /// The byte offset of the value within the root signature. - public sealed record Primitive(EquatableArray FieldPath, string TypeName, int Offset) : FieldInfo; - - /// - /// A captured non linear matrix value. - /// - /// The path of the field with respect to the shader instance. - /// The full metadata name for the matrix type. - /// The name of each element in the matrix type. - /// The number of rows in the matrix. - /// The number of columns in the matrix. - /// The sequence of byte offsets for each row within the root signature. - public sealed record NonLinearMatrix( - EquatableArray FieldPath, - string TypeName, - string ElementName, - int Rows, - int Columns, - EquatableArray Offsets) : FieldInfo; -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs new file mode 100644 index 00000000..49fc0c32 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs @@ -0,0 +1,9 @@ +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model describing a captured resource (either a buffer or a texture). +/// +/// The name of the resource field. +/// The full metadata name for the resource type. +/// The offset for the resource within the root signature. +public sealed record ResourceInfo(string FieldName, string TypeName, int Offset); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index 4cf56167..68909fb9 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -15,6 +15,7 @@ namespace ComputeSharp.SourceGenerators.Models; /// The size of the shader constant buffer. /// The total number of captured resources in the shader. /// The description on shader instance fields. +/// The description on shader captured resources. /// The sequence of resource descriptors for the shader. /// The key with processed info on the shader. /// The value with processed info on the shader. @@ -29,6 +30,7 @@ internal sealed record ShaderInfo( int ConstantBufferSizeInBytes, int ResourceCount, EquatableArray Fields, + EquatableArray Resources, EquatableArray ResourceDescriptors, HlslBytecodeInfoKey HlslInfoKey, HlslBytecodeInfo HlslInfo,