diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs
index 836ddd60..4cda30e7 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs
@@ -69,12 +69,12 @@ partial class D2DPixelShaderDescriptorGenerator
}
///
- /// Registers a callback to generate an additional type, if needed.
+ /// Registers a callback to generate additional types, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional types.
/// The using directives needed by the generated code.
- public static void RegisterAdditionalTypeSyntax(
+ public static void RegisterAdditionalTypesSyntax(
D2D1ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs
index ca098146..6b581f35 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs
@@ -36,12 +36,12 @@ partial class D2DPixelShaderDescriptorGenerator
}
///
- /// Registers a callback to generate an additional type, if needed.
+ /// Registers a callback to generate additional types, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional types.
/// The using directives needed by the generated code.
- public static void RegisterAdditionalTypeSyntax(
+ public static void RegisterAdditionalTypesSyntax(
D2D1ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs
index 5ed34b34..07944b28 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs
@@ -34,12 +34,12 @@ partial class D2DPixelShaderDescriptorGenerator
}
///
- /// Registers a callback to generate an additional type, if needed.
+ /// Registers a callback to generate additional types, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional types.
/// The using directives needed by the generated code.
- public static void RegisterAdditionalTypeSyntax(
+ public static void RegisterAdditionalTypesSyntax(
D2D1ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
index 5322c88c..8eddfaaa 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs
@@ -236,9 +236,9 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene
using ImmutableHashSetBuilder usingDirectives = new();
ConstantBufferSyntaxProcessor.RegisterAdditionalTypesSyntax(GeneratorName, BindingDirection.TwoWay, item, additionalTypes, usingDirectives);
- InputDescriptions.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
- InputTypes.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
- HlslBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
+ InputDescriptions.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
+ InputTypes.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
+ HlslBytecode.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
using IndentedTextWriter writer = new();
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
index f251d0c9..707826ca 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs
@@ -119,7 +119,7 @@ partial class ConstantBufferSyntaxProcessor
string fullyQualifiedTypeName = info.Hierarchy.GetFullyQualifiedTypeName();
writer.WriteLine($"""/// """);
- writer.WriteLine($"""/// A type containing marshalling logic for shaders of type .""");
+ writer.WriteLine($"""/// A type containing marshalling logic for the constant buffer in shaders of type .""");
writer.WriteLine($"""/// """);
writer.WriteGeneratedAttributes(ConstantBufferSyntaxProcessor.generatorName!, useFullyQualifiedTypeNames: false);
writer.WriteLine($"""file static class ConstantBufferMarshaller""");
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
index a081e890..03a39a11 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs
@@ -34,12 +34,12 @@ partial class ComputeShaderDescriptorGenerator
}
///
- /// Registers a callback to generate an additional type, if needed.
+ /// Registers a callback to generate additional types, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional types.
/// The using directives needed by the generated code.
- public static void RegisterAdditionalTypeSyntax(
+ public static void RegisterAdditionalTypesSyntax(
ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs
index 065b7544..e475aa08 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs
@@ -4,6 +4,7 @@ using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
+using ComputeSharp.SourceGeneration.SyntaxProcessors;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
@@ -49,13 +50,18 @@ partial class ComputeShaderDescriptorGenerator
continue;
}
- string fieldName = fieldSymbol.Name;
+ // Try to get the name to use for the field and the accessor
+ if (!ConstantBufferSyntaxProcessor.TryGetFieldAccessorName(fieldSymbol, out string? fieldName, out string? unspeakableName))
+ {
+ continue;
+ }
+
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));
+ resourceBuilder.Add(new ResourceInfo(fieldName, unspeakableName, typeName));
}
}
@@ -90,12 +96,101 @@ partial class ComputeShaderDescriptorGenerator
using (writer.WriteBlock())
{
- // Generate loading statements for each captured resource
- for (int i = 0; i < info.Resources.Length; i++)
+ // Nothing to do if there are no resources
+ if (info.Resources.IsEmpty)
{
- writer.WriteLine($"loader.LoadGraphicsResource(shader.{info.Resources[i].FieldName}, {i});");
+ return;
+ }
+
+ // Delegate to the generated loader type
+ writer.WriteLine("global::ComputeSharp.Generated.GraphicsResourcesLoader.LoadGraphicsResources(in shader, ref loader);");
+ }
+ }
+
+ ///
+ /// Registers a callback to generate additional types, if needed.
+ ///
+ /// The input instance with gathered shader info.
+ /// The registered callbacks to generate additional types.
+ /// The using directives needed by the generated code.
+ public static void RegisterAdditionalTypesSyntax(
+ ShaderInfo info,
+ ImmutableArrayBuilder> callbacks,
+ ImmutableHashSetBuilder usingDirectives)
+ {
+ // If there are no resources, there's no need for any additional types
+ if (info.Resources.IsEmpty)
+ {
+ return;
+ }
+
+ _ = usingDirectives.Add("global::System.CodeDom.Compiler");
+ _ = usingDirectives.Add("global::System.Diagnostics");
+ _ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
+ _ = usingDirectives.Add("global::System.Runtime.CompilerServices");
+ _ = usingDirectives.Add("global::ComputeSharp.Descriptors");
+
+ // Declare the GraphicsResourcesLoader type
+ static void Callback(ShaderInfo info, IndentedTextWriter writer)
+ {
+ string fullyQualifiedTypeName = info.Hierarchy.GetFullyQualifiedTypeName();
+
+ writer.WriteLine($"""/// """);
+ writer.WriteLine($"""/// A type containing loading logic for graphics resources in shaders of type .""");
+ writer.WriteLine($"""/// """);
+ writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false);
+ writer.WriteLine($"""file static class GraphicsResourcesLoader""");
+
+ using (writer.WriteBlock())
+ {
+ // Define the FromManaged method (managed shader type to native constant buffer)
+ writer.WriteLine($$"""/// """);
+ writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]""");
+ writer.WriteLine($"""[SkipLocalsInit]""");
+ writer.WriteLine($"public static void LoadGraphicsResources(in {fullyQualifiedTypeName} shader, ref TLoader loader)");
+ writer.WriteLine(" where TLoader : struct, IGraphicsResourceLoader");
+
+ using (writer.WriteBlock())
+ {
+ // Generate loading statements for each captured resource.
+ // Each loading statement will invoke the generated accessor.
+ for (int i = 0; i < info.Resources.Length; i++)
+ {
+ writer.WriteLine($"loader.LoadGraphicsResource(shader.{info.Resources[i].FieldName}(), {i});");
+ }
+ }
+
+ // Define all field accessors (always used, just like for constant buffer fields, see remarks there)
+ foreach (ResourceInfo resourceInfo in info.Resources)
+ {
+ writer.WriteLine();
+
+ // Generate the correct field accessor depending on whether the field can be referenced directly
+ if (resourceInfo.UnspeakableName is null)
+ {
+ writer.WriteLine($"""
+ ///
+ /// The input value.
+ /// A reference to .
+ [UnsafeAccessor(UnsafeAccessorKind.Field)]
+ private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
+ """, isMultiline: true);
+ }
+ else
+ {
+ writer.WriteLine($"""
+ /// Gets a reference to the unspeakable field "{resourceInfo.FieldName}" of type .
+ /// The input value.
+ /// A reference to the unspeakable field "{resourceInfo.FieldName}".
+ [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{resourceInfo.UnspeakableName}")]
+ private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
+ """, isMultiline: true);
+ }
+ }
}
}
+
+ callbacks.Add(Callback);
}
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
index 37da4b50..0483a1dc 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs
@@ -174,8 +174,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
using ImmutableHashSetBuilder usingDirectives = new();
ConstantBufferSyntaxProcessor.RegisterAdditionalTypesSyntax(GeneratorName, BindingDirection.OneWay, item, additionalTypes, usingDirectives);
+ Resources.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
ResourceDescriptorRanges.RegisterAdditionalDataMemberSyntax(item, additionalTypes, usingDirectives);
- HlslBytecode.RegisterAdditionalTypeSyntax(item, additionalTypes, usingDirectives);
+ HlslBytecode.RegisterAdditionalTypesSyntax(item, additionalTypes, usingDirectives);
using IndentedTextWriter writer = new();
diff --git a/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs
index dab66a81..2e738721 100644
--- a/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs
+++ b/src/ComputeSharp.SourceGenerators/Models/ResourceInfo.cs
@@ -4,5 +4,6 @@ namespace ComputeSharp.SourceGenerators.Models;
/// A model describing a captured resource (either a buffer or a texture).
///
/// The name of the resource field.
+/// The unspeakable name of the field, if present.
/// The full metadata name for the resource type.
-public sealed record ResourceInfo(string FieldName, string TypeName);
\ No newline at end of file
+public sealed record ResourceInfo(string FieldName, string? UnspeakableName, string TypeName);
\ No newline at end of file