diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
index c3219549..48e18ec3 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs
@@ -18,14 +18,12 @@ partial class ComputeShaderDescriptorGenerator
private static class ConstantBuffer
{
///
- /// The collection of produced instances.
///
///
/// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.
///
///
public static void GetInfo(
- ImmutableArrayBuilder diagnostics,
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
bool isPixelShaderLike,
@@ -54,7 +52,7 @@ partial class ComputeShaderDescriptorGenerator
///
/// The input instance with gathered shader info.
/// The instance to write into.
- public static void WriteLoadConstantBufferSyntax(ShaderInfo info, IndentedTextWriter writer)
+ public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
{
string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs
deleted file mode 100644
index 33736b41..00000000
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using ComputeSharp.SourceGeneration.Extensions;
-using ComputeSharp.SourceGeneration.Helpers;
-using ComputeSharp.SourceGenerators.Models;
-
-namespace ComputeSharp.SourceGenerators;
-
-///
-partial class ComputeShaderDescriptorGenerator
-{
- ///
- partial class DispatchDataLoading
- {
- ///
- /// Writes the LoadGraphicsResources method.
- ///
- /// The input instance with gathered shader info.
- /// The instance to write into.
- public static void WriteLoadGraphicsResourcesSyntax(ShaderInfo info, IndentedTextWriter writer)
- {
- string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
-
- writer.WriteLine("/// ");
- writer.WriteGeneratedAttributes(GeneratorName);
- writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)");
-
- using (writer.WriteBlock())
- {
- // Generate loading statements for each captured resource
- foreach (ResourceInfo resource in info.Resources)
- {
- writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});");
- }
- }
- }
- }
-}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs
deleted file mode 100644
index 01105349..00000000
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs
+++ /dev/null
@@ -1,197 +0,0 @@
-using System;
-using System.Collections.Immutable;
-using ComputeSharp.Core.Helpers;
-using ComputeSharp.SourceGeneration.Extensions;
-using ComputeSharp.SourceGeneration.Helpers;
-using ComputeSharp.SourceGeneration.Mappings;
-using ComputeSharp.SourceGeneration.Models;
-using ComputeSharp.SourceGenerators.Models;
-using Microsoft.CodeAnalysis;
-using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
-
-namespace ComputeSharp.SourceGenerators;
-
-///
-partial class ComputeShaderDescriptorGenerator
-{
- ///
- /// A helper with all logic to generate the dispatch data loading methods.
- ///
- private static partial class DispatchDataLoading
- {
- ///
- /// 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.
- /// The total number of captured resources in the shader.
- /// The sequence of instances for all captured values.
- /// The sequence of instances for all captured resources.
- public static void GetInfo(
- ImmutableArrayBuilder diagnostics,
- Compilation compilation,
- ITypeSymbol structDeclarationSymbol,
- bool isPixelShaderLike,
- out int constantBufferSizeInBytes,
- out int resourceCount,
- out ImmutableArray fields,
- out ImmutableArray resources)
- {
- static void GetInfo(
- Compilation compilation,
- ITypeSymbol currentTypeSymbol,
- ImmutableArray fieldPath,
- ref int resourceOffset,
- ref int rawDataOffset,
- ImmutableArrayBuilder fields,
- ImmutableArrayBuilder resources)
- {
- bool isFirstField = true;
-
- 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;
- }
-
- // 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();
-
- // The first item in each nested struct needs to be aligned to 16 bytes
- if (isFirstField && fieldPath.Length > 0)
- {
- rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
-
- isFirstField = false;
- }
-
- if (HlslKnownTypes.IsTypedResourceType(typeName))
- {
- resources.Add(new ResourceInfo(fieldName, typeName, resourceOffset++));
- }
- else if (HlslKnownTypes.IsKnownHlslType(typeName))
- {
- // TODO: share the logic and fix the field path parts
- fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName, null)), typeName, ref rawDataOffset));
- }
- else if (fieldSymbol.Type.IsUnmanagedType)
- {
- // Custom struct type defined by the user
- GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName, null)), 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 rawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3);
- int resourceOffset = 0;
-
- using (ImmutableArrayBuilder fieldBuilder = new())
- using (ImmutableArrayBuilder resourceBuilder = new())
- {
- // Traverse all shader fields and gather info, and update the tracking offsets
- GetInfo(
- compilation,
- structDeclarationSymbol,
- ImmutableArray.Empty,
- ref resourceOffset,
- ref rawDataOffset,
- fieldBuilder,
- resourceBuilder);
-
- 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(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:
- // - Root constants cost 1 DWORD each, since they are 32-bit values.
- // - Descriptor tables cost 1 DWORD each.
- // - Root descriptors (64-bit GPU virtual addresses) cost 2 DWORDs each.
- // So here we check whether the current signature respects that constraint,
- // and emit a build error otherwise. For more info on this, see the docs here:
- // https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits.
- int root32BitConstantCount = (constantBufferSizeInBytes / sizeof(int)) + resourceCount;
-
- if (root32BitConstantCount > 64)
- {
- diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
- }
- }
-
- ///
- /// Gets info on a given captured HLSL primitive type (either a scalar, a vector or a matrix).
- ///
- /// The current path of the field with respect to the shader instance.
- /// The type name currently being read.
- /// The current offset within the loaded data buffer.
- private static FieldInfo GetHlslKnownTypeFieldInfo(
- ImmutableArray fieldPath,
- string typeName,
- ref int rawDataOffset)
- {
- // For scalar, vector and linear matrix types, serialize the value normally.
- // Only the initial alignment needs to be considered, while data is packed.
- (int fieldSize, int fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
-
- // Check if the current type is a matrix type with more than one row. In this
- // case, each row is aligned as if it was a separate array, so the start of
- // each row needs to be at a multiple of 16 bytes (a float4 register).
- if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out string? elementName, out int rows, out int columns))
- {
- using ImmutableArrayBuilder builder = new();
-
- for (int j = 0; j < rows; j++)
- {
- int startingRawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16);
-
- builder.Add(startingRawDataOffset);
-
- rawDataOffset = startingRawDataOffset + (fieldPack * columns);
- }
-
- return new FieldInfo.NonLinearMatrix(
- fieldPath,
- typeName,
- elementName!,
- rows,
- columns,
- builder.ToImmutable());
- }
- else
- {
- // Calculate the right offset with 16-bytes padding (HLSL constant buffer).
- // Since we're in a constant buffer, we need to both pad the starting offset
- // to be aligned to the packing size of the type, and also to align the initial
- // offset to ensure that values do not cross 16 bytes boundaries either.
- int startingRawDataOffset = AlignmentHelper.AlignToBoundary(
- AlignmentHelper.Pad(rawDataOffset, fieldPack),
- fieldSize,
- 16);
-
- rawDataOffset = startingRawDataOffset + fieldSize;
-
- return new FieldInfo.Primitive(fieldPath, typeName, startingRawDataOffset);
- }
- }
- }
-}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
index c8a1a0ca..a081e890 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
@@ -16,7 +16,7 @@ partial class ComputeShaderDescriptorGenerator
///
/// The input instance with gathered shader info.
/// The instance to write into.
- public static void WriteHlslBytecodeSyntax(ShaderInfo info, IndentedTextWriter writer)
+ public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs
new file mode 100644
index 00000000..ec151f45
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs
@@ -0,0 +1,103 @@
+using System;
+using System.Collections.Immutable;
+using ComputeSharp.SourceGeneration.Extensions;
+using ComputeSharp.SourceGeneration.Helpers;
+using ComputeSharp.SourceGeneration.Mappings;
+using ComputeSharp.SourceGeneration.Models;
+using ComputeSharp.SourceGenerators.Models;
+using Microsoft.CodeAnalysis;
+using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.SourceGenerators;
+
+///
+partial class ComputeShaderDescriptorGenerator
+{
+ ///
+ /// A helper with all logic to generate the resources loading code.
+ ///
+ private static class Resources
+ {
+ ///
+ /// 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.
+ public static void GetInfo(
+ ImmutableArrayBuilder diagnostics,
+ Compilation compilation,
+ ITypeSymbol structDeclarationSymbol,
+ int constantBufferSizeInBytes,
+ out ImmutableArray resources)
+ {
+ int resourceOffset = 0;
+
+ using ImmutableArrayBuilder resourceBuilder = new();
+
+ foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
+ {
+ // Only process instance fields
+ 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 just like other fields)
+ if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation))
+ {
+ continue;
+ }
+
+ string fieldName = fieldSymbol.Name;
+ string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
+
+ // Check if the field is a resource (note: resources can only be top level fields)
+ if (HlslKnownTypes.IsTypedResourceType(typeName))
+ {
+ resourceBuilder.Add(new ResourceInfo(fieldName, typeName, resourceOffset++));
+ }
+ }
+
+ resources = resourceBuilder.ToImmutable();
+
+ // A shader root signature has a maximum size of 64 DWORDs, so 256 bytes.
+ // Loaded values in the root signature have the following costs:
+ // - Root constants cost 1 DWORD each, since they are 32-bit values.
+ // - Descriptor tables cost 1 DWORD each.
+ // - Root descriptors (64-bit GPU virtual addresses) cost 2 DWORDs each.
+ // So here we check whether the current signature respects that constraint,
+ // and emit a build error otherwise. For more info on this, see the docs here:
+ // https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits.
+ if ((constantBufferSizeInBytes / sizeof(int)) + resourceOffset > 64)
+ {
+ diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
+ }
+ }
+
+ ///
+ /// Writes the LoadGraphicsResources method.
+ ///
+ /// The input instance with gathered shader info.
+ /// The instance to write into.
+ public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
+ {
+ string typeName = info.Hierarchy.Hierarchy[0].QualifiedName;
+
+ writer.WriteLine("/// ");
+ writer.WriteGeneratedAttributes(GeneratorName);
+ writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)");
+
+ using (writer.WriteBlock())
+ {
+ // Generate loading statements for each captured resource
+ foreach (ResourceInfo resource in info.Resources)
+ {
+ writer.WriteLine($"loader.LoadGraphicsResource(shader.{resource.FieldName}, {resource.Offset});");
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
index 34906171..37da4b50 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
@@ -60,14 +60,21 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
using ImmutableArrayBuilder diagnostics = new();
// Get the fields info
- DispatchDataLoading.GetInfo(
- diagnostics,
+ ConstantBuffer.GetInfo(
context.SemanticModel.Compilation,
typeSymbol,
isPixelShaderLike,
out int constantBufferSizeInBytes,
- out int resourceCount,
- out ImmutableArray fieldInfos,
+ out ImmutableArray fieldInfos);
+
+ token.ThrowIfCancellationRequested();
+
+ // Get the resources info
+ Resources.GetInfo(
+ diagnostics,
+ context.SemanticModel.Compilation,
+ typeSymbol,
+ constantBufferSizeInBytes,
out ImmutableArray resourceInfo);
token.ThrowIfCancellationRequested();
@@ -131,7 +138,6 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
IsPixelShaderLike: isPixelShaderLike,
IsSamplerUsed: isSamplerUsed,
ConstantBufferSizeInBytes: constantBufferSizeInBytes,
- ResourceCount: resourceCount,
Fields: fieldInfos,
Resources: resourceInfo,
ResourceDescriptors: resourceDescriptors,
@@ -160,9 +166,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
declaredMembers.Add(MetadataProperties.WriteIsStaticSamplerRequiredSyntax);
declaredMembers.Add(ResourceDescriptorRanges.WriteSyntax);
declaredMembers.Add(HlslSource.WriteSyntax);
- declaredMembers.Add(HlslBytecode.WriteHlslBytecodeSyntax);
- declaredMembers.Add(ConstantBuffer.WriteLoadConstantBufferSyntax);
- declaredMembers.Add(DispatchDataLoading.WriteLoadGraphicsResourcesSyntax);
+ declaredMembers.Add(HlslBytecode.WriteSyntax);
+ declaredMembers.Add(ConstantBuffer.WriteSyntax);
+ declaredMembers.Add(Resources.WriteSyntax);
using ImmutableArrayBuilder> additionalTypes = new();
using ImmutableHashSetBuilder usingDirectives = new();
diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs
index ed828230..c2617537 100644
--- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs
+++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs
@@ -13,7 +13,6 @@ namespace ComputeSharp.SourceGenerators.Models;
/// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.
/// Whether or not the static sampler is used.
/// 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.
@@ -28,7 +27,6 @@ internal sealed record ShaderInfo(
bool IsPixelShaderLike,
bool IsSamplerUsed,
int ConstantBufferSizeInBytes,
- int ResourceCount,
EquatableArray Fields,
EquatableArray Resources,
EquatableArray ResourceDescriptors,